-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
162 lines (151 loc) · 4.14 KB
/
main.js
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
// basicSetup imports
import {keymap, highlightSpecialChars, drawSelection, highlightActiveLine, dropCursor,
rectangularSelection, crosshairCursor,
lineNumbers, highlightActiveLineGutter} from "@codemirror/view"
import {EditorState} from "@codemirror/state"
import {defaultHighlightStyle, syntaxHighlighting, indentOnInput, bracketMatching,
foldGutter, foldKeymap} from "@codemirror/language"
import {defaultKeymap, history, historyKeymap} from "@codemirror/commands"
import {searchKeymap, highlightSelectionMatches} from "@codemirror/search"
import {autocompletion, completionKeymap} from "@codemirror/autocomplete"
import {lintKeymap} from "@codemirror/lint"
// Actual imports
import {EditorView} from "@codemirror/view"
import {indentWithTab} from "@codemirror/commands"
import {javascript} from "@codemirror/lang-javascript"
import { nord } from 'cm6-theme-nord'
import { vim, Vim, getCM } from "@replit/codemirror-vim"
import { linter } from "@codemirror/lint";
import { flame, transform } from "./bl4st.mjs"
import { Engine } from "./ablaze.mjs"
function setup() {
const engine = new Engine(document.getElementById("canvas"))
engine.init()
let errors = [];
function errorLint(view) {
return errors.map(error => {
return {
from: 0,
to: view.state.doc.length,
severity: "error",
message: error.toString(),
}
});
}
function evalCode(code, view) {
try {
const config = eval(code);
engine.setConfig(config)
} catch (err) {
errors = [err];
console.error(err);
// trigger review of the error
view?.setState(view?.state);
return true;
}
params.set("c", code);
window.history.pushState({}, '', `${location.pathname}?${params.toString()}`);
errors = [];
// trigger review to clear errors
view?.setState(view?.state);
return true;
}
const params = new URLSearchParams(location.search);
let defaultCode = `
// More info at https://github.com/emptyflash/bl4st
flame()
.initScale(.2)
.initVal(.8)
.colorful(0.4)
.exposure(1.6)
.addTransform(
transform()
.fisheye()
.x(({time}) => [.2,1.5*Math.sin(time/5)])
)
.addTransform(
transform()
.fisheye()
.y(({time}) => [.2,1.5*Math.cos(time/3)])
)`
if (params.get("c")) {
defaultCode = params.get("c");
}
let vimExtension = [];
if (params.get("v") !== null) {
vimExtension = [vim()];
}
const basicSetup = [
lineNumbers(),
highlightActiveLineGutter(),
highlightSpecialChars(),
history(),
foldGutter(),
drawSelection(),
dropCursor(),
EditorState.allowMultipleSelections.of(true),
indentOnInput(),
syntaxHighlighting(defaultHighlightStyle, {fallback: true}),
bracketMatching(),
autocompletion(),
rectangularSelection(),
crosshairCursor(),
highlightActiveLine(),
highlightSelectionMatches(),
keymap.of([
...defaultKeymap,
...searchKeymap,
...historyKeymap,
...foldKeymap,
...completionKeymap,
...lintKeymap
])
]
const editorDiv = document.getElementById("editor")
const runCode = (view) => evalCode(view.state.doc.toString(), view)
const editor = new EditorView({
doc: defaultCode,
extensions: [
keymap.of([{
key: "Ctrl-Enter",
run: runCode,
},{
key: "Cmd-Enter",
run: runCode
},{
key: "Ctrl-e",
run: (view) => {
let cm = getCM(view);
Vim.exitInsertMode(cm);
return true;
}
}]),
keymap.of(defaultKeymap),
keymap.of([indentWithTab]),
javascript(),
basicSetup,
EditorView.lineWrapping,
nord,
linter(errorLint),
].concat(vimExtension),
parent: editorDiv,
});
setTimeout(() => {
evalCode(defaultCode, editor)
engine.render()
}, 10);
function runButtonFn() {
evalCode(editor.state.doc.toString(), editor);
}
const runButton = document.getElementById("runButton");
runButton.onclick = runButtonFn
Vim.defineEx('write', 'w', function() {
evalCode(editor.state.doc.toString(), editor);
});
}
export {
flame,
transform,
Engine,
setup,
}