-
Notifications
You must be signed in to change notification settings - Fork 2
/
api.pyj
217 lines (203 loc) · 6.31 KB
/
api.pyj
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
213
214
215
216
217
widget_id = new URLSearchParams(location.search).get("id") or crypto.randomUUID()
def the(*args): return document.getElementById(*args)
def one(*args): return document.querySelector(*args)
def all(*args): return document.querySelectorAll(*args)
def log(*args): console.log(*args)
def readfile(f):
request = new XMLHttpRequest()
request.open("GET", f, False)
request.send()
if request.status == 200:
return request.responseText
CodeMirror.defaults.lineNumbers=True
CodeMirror.defaults.indentWithTabs=False
CodeMirror.defaults.viewportMargin=1000
_memory = {}
def memory(name):
key = f"__{widget_id}_{name}"
try:
if _memory[key]:
return _memory[key]
except KeyError:
pass
def fn(arg=None, clear=None):
if clear:
sessionStorage.removeItem(key)
if arg is None:
value = sessionStorage.getItem(key)
return JSON.parse(value) if value else value
else:
sessionStorage.setItem(key, JSON.stringify(arg))
_memory[key] = fn
return fn
the('cfg_errors').style.display = "block"
cfg = toml.parse(readfile("config.toml"))
the('cfg_errors').style.display = "none"
widget = cfg.widget
def changeEditor(el):
target = el.target
for tab in all(".tab"):
tab.style.background = "transparent"
target.style.background = "lightgreen"
for code in widget.codes:
if code.tab == target.id:
code.wrapper.style.display = "block"
else:
code.wrapper.style.display = "none"
def buildEditors(widget):
editors = the("editors")
tabs = the("tabs")
for code in widget.codes:
widget.codes[code.name] = code
if code.name == widget.entrypoint:
widget.entrypoint = code
textarea = document.createElement("textarea")
textarea.id = f"_{code.name}"
textarea.style.display = "none"
value = memory(code.name)()
if value:
textarea.value = value
elif code.file:
textarea.value = readfile(code.file)
editors.appendChild(textarea)
editor = CodeMirror.fromTextArea(textarea, {'mode': {'name': code.type}})
tab = document.createElement("div")
tab.id = f"tab_{code.name}"
tab.classList.add("tab")
tab.innerHTML = code.name
tab.onclick = changeEditor
tabs.appendChild(tab)
code.editor = editor.getDoc()
code.wrapper = editor.getWrapperElement()
code.wrapper.style.display = "none"
code.tab = tab.id
code = widget.codes[0]
code.wrapper.style.display = "block"
the(code.tab).style.background = "lightgreen"
def getcode(id):
return widget.codes[id].editor.getValue()
lastListener = None
def new_iframe(el_id, htmlcode):
nonlocal lastListener
el = the(el_id)
wFrame = document.createElement('iframe')
while el.firstChild:
el.removeChild(el.firstChild)
el.appendChild(wFrame)
widgetWindow = wFrame.contentWindow
# Rewire messages between this widget, and the preview
try:
window.removeEventListener('message', lastListener)
except KeyError:
pass
def listener(e):
if e.source == widgetWindow:
window.parent.postMessage(e.data, "*")
elif e.source == window.parent:
widgetWindow.postMessage(e.data, "*")
lastListener = listener
window.addEventListener('message', listener)
content = wFrame.contentWindow
content.document.open()
content.document.write(htmlcode)
content.document.close()
def showPreview(*_):
the("errors").style.display = "none"
code = widget.entrypoint
if code.type == "pug":
new_iframe('widget', pug.compile(code.editor.getValue())({
"grist": grist, "cfg": cfg, "getcode": getcode
}))
elif code.type == "html":
new_iframe('widget', code.editor.getValue())
elif code.type == "python":
pyscript = document.createElement("script")
pyscript.type = "pyj"
try:
eval(compiler.compile(code.editor.getValue()))
except Exception as e:
console.error(e)
errors = the("errors")
errors.innerHTML = str.format("""<code><pre>{}, line {}, col {}: {}</pre></code>""", code.name, e.line, e.col, e.message)
errors.style.display = "block"
elif code.type == "javascript":
jsscript = document.createElement("script")
jsscript.innerHTML = code.editor.getValue()
codes = widget.codes
document.body.appendChild(jsscript)
the("editors").style.display = 'none'
the('widget').style.display = 'block'
for cat in ('.tab', '.btn'):
for e in all(cat):
e.style.display = 'none'
the('install').style.display = 'inline-block'
the("editor").style.display = 'inline-block'
memory('preview')(True)
def showEditor(*_):
the('bar').style.display = 'flex'
the('widget').style.display = 'none'
the("editors").style.display = 'inline-block'
memory('preview')(False)
for e in all('.tab'):
e.style.display = 'inline-block'
for e in all('.btn'):
e.style.display = 'none'
the('install').style.display = 'inline-block'
the('preview').style.display = 'inline-block'
# Create cancellable onOptions version
def onOptions(clb):
listen = True
def callback(*data):
if listen:
clb(*data)
def cancel():
listen = False
grist.onOptions(callback)
return cancel
def callback(options, *_args):
global widget, cfg
try:
if widget.installed:
return
except TypeError: pass
if options and options.installed:
if options._cfg:
the('cfg_errors').style.display = "block"
cfg = toml.parse(options._cfg)
the('cfg_errors').style.display = "none"
widget = cfg.widget
widget.installed = True
widget.isEditor = False
the('bar').style.display = 'none'
for code in widget.codes:
memory(code.name)(options[f"_{code.name}"])
buildEditors(widget)
showPreview()
def cb(options, *_):
if not options:
memory('preview')(False)
location.reload()
onOptions(cb)
return
if not options:
for code in widget.codes:
memory(code.name)(clear=True)
if not widget.isEditor:
widget.isEditor = True
buildEditors(widget)
if memory('preview')():
showPreview()
else:
showEditor()
onOptions(callback)
def install(*_):
options = {"installed": True}
for code in widget.codes:
options[f"_{code.name}"] = code.editor.getValue()
grist.setOptions(options).then(window.location.reload())
the("install").onclick = install
the("preview").onclick = showPreview
the("editor").onclick = showEditor
the("editor").style.display = "inline-block"
cfg.grist.onEditOptions = showEditor
grist.ready(cfg.grist)