-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathj.js
More file actions
212 lines (212 loc) · 5.87 KB
/
j.js
File metadata and controls
212 lines (212 loc) · 5.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// By + Copyright 2022-2025 Greg Abbott (UK) v2024-09-25
const el={}
function get_by_id(x){return document.getElementById(x)}
function chain(seed,...fns){return fns.reduce((a,f)=>f(a),seed)}
const glon_textarea = get_by_id('glon_textarea')
const json_textarea = get_by_id('json_textarea')
let to_js_settings_fields=[
...get_by_id('to_js_settings_fields')
.getElementsByTagName('input')
]
const arrow_key_codes={
'37':'left',
'38':'up',
'39':'right',
'40':'down',
}
function get_key_code(e){return e.keyCode || e.charCode || e.which}
function strip_wiki_key_brackets(x){
return x.replace(/\[\[([^\]]*?)\]\]*/g,'$1')
}
function strip_markdown_bold(x){
return x.replace(/\*\*(.*?)\*\*/g, '$1')
}
function strip_wiki_value_brackets(s){
//starts `[[` and ends `]]`, does not end ]]]
if(/^\[\[.*?[^\]]\]\]$/.test(s)){
let mod =s.replace('[[','').replace(']]','')
return (mod.includes('[[')||mod.includes(']]'))?s:mod
}
return s
}
function convert_to_js(){
let errors_box = document.getElementById('logs_textarea')
let log_count_el = document.getElementById('log_count')
//Let frontend toggle hooks
let should_strip_key_bold=
get_by_id('strip_key_bold').checked
let should_strip_wiki_key_brackets=
get_by_id('strip_wiki_key_brackets').checked
let should_strip_wiki_value_brackets=
get_by_id('strip_wiki_value_brackets').checked
let log_errors_checkbox=get_by_id('log_errors')
const should_log_errors=log_errors_checkbox.checked
reset_logs()
const rv = chain(
glon_textarea.value,
glon.to_js(to_js_settings_fields.reduce(
(a,x)=>{
a[x.id]=x.checked
return a
},
{
//log:should_log_errors,
max_key_length: 60,
to_json:true,
value_hook:({value,key,key_comments,value_comments,type_wish})=>{
/*if(key==="Local"){
console.log({value,key,key_comments,value_comments,type_wish})
}*/
/*
if(key_comments?.length>0||value_comments?.length>0){
console.log(
`value hook received`,
{key,value,key_comments,value_comments}
)
}
*/
//example:
if(key==`swatches`&&typeof value=='string'){
return value.split(',').map(x=>x.trim())
}
if(should_strip_wiki_value_brackets){
return strip_wiki_value_brackets(value)
}
},
key_hook: ({key,comments}) => {
/*
if(comments.length>0){
console.log(`key hook received`,{key,comments})
}
*/
//EXAMPLE KEY HOOK
//EG of acting on a key that meets specific conditions
if(key.toLowerCase().includes('key hook demo')){
return key
.toLowerCase()
.replace(/\[\[([^\]]*)\]\]/g, '$1')
.replace(/\*\*([^\*]*)\*\*/g, '$1')
.trim()
.replaceAll(' ','_')
}
//example of processing keys more with user functions
let rv=key
if(should_strip_key_bold){
rv=strip_markdown_bold(rv)
}
if(should_strip_wiki_key_brackets){
rv=strip_wiki_key_brackets(rv)
}
return rv
}
}
))
)
//Log JavaScript Object
//console.log(rv.js)
function reset_logs(){
if(errors_box){
errors_box.value=''
}
if(log_count_el){
log_count_el.innerText=''
}
}
//log_errors_checkbox.onchange=()=>{}
let errors_box_holder=document.getElementById(`logs_textarea_holder`)
if(should_log_errors){
reset_logs()
if(errors_box){
if(rv.logs?.length>0){
errors_box.value=rv.logs.join('\n')
log_count_el.innerText=`(${rv.logs.length})`
//errors_box_holder.open=true
}
else {
// errors_box_holder.hidden=true
}
}
else{
//log normal
//remove previous
console.clear()
//log new
if(rv.logs?.length>0){
console.log(rv.logs.length==1?rv.logs[0]:rv.logs)
}
}
}
json_textarea.value=rv.json
}
glon_textarea.onchange=e=>{
convert_to_js()
}
glon_textarea.onkeyup=e=>{
const pressed_arrow=e&&arrow_key_codes[get_key_code(e)]
if(pressed_arrow)return
convert_to_js()
}
//external script:
enable_tab_key(json_textarea)
enable_tab_key(glon_textarea)
json_textarea.onkeyup=()=>{
glon_textarea.value = glon.from_json()(json_textarea.value)
}
to_js_settings_fields.forEach(el=>el.onchange=convert_to_js)
get_by_id('show_glon_text_area').checked=true
get_by_id('show_json_text_area').checked=true
function download_text_file({ name, data,ext }) {
const blob = new Blob([data], { type:
ext=='json'?'application/json': 'text/plain' });
const link = document.createElement('a')
link.href = URL.createObjectURL(blob)
link.download = `${name}.${ext}`
document.body.appendChild(link)
link.click()
document.body.removeChild(link)
}
function get_stamp() {
return new Date().toISOString().replace(/[^\d]/g, '').replace(/(\d{4})/g, "$1_").substring(0, 14)
}
let button_to_save_glon =
document.getElementById('button_to_save_glon')
let button_to_save_json =
document.getElementById('button_to_save_json')
button_to_save_glon.onclick = () => {
download_text_file({
name: get_stamp() + ` glon`,
ext:'txt',
data: document.getElementById('glon_textarea').value
})
}
button_to_save_json.onclick = () => {
download_text_file({
name: get_stamp() + ` json`,
ext:'json',
data: document.getElementById('json_textarea').value
})
}
function copy(string,el){
navigator.clipboard.writeText(string)
.then(() => {
button_notice('Copied!')
})
.catch(err => {
button_notice('Failed')
});
function button_notice(s){
let init = el.innerText
el.innerText=s
setTimeout(()=>{
el.innerText=init
},1000)
}
}
document.getElementById('button_to_copy_glon').onclick=e=>{copy(
document.getElementById('glon_textarea').value,
e.target)
}
document.getElementById('button_to_copy_json').onclick=e=>{copy(
document.getElementById('json_textarea').value,
e.target)
}