-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
197 lines (153 loc) · 5.03 KB
/
script.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
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
let gluaWorker = (function() {
let resolvers = []
let worker = new Worker(chrome.runtime.getURL("glua_worker.js"))
worker.onmessage = function(e) {
if (resolvers[e.data.id]) {
resolvers[e.data.id](e.data.result)
delete resolvers[e.data.id]
}
}
worker.lintString = function(code) {
return new Promise((resolve, reject) => {
resolvers.push(resolve)
worker.postMessage({
id: resolvers.length - 1,
action: "lint",
code: code
})
})
}
worker.prettyPrintString = function(code) {
return new Promise((resolve, reject) => {
resolvers.push(resolve)
worker.postMessage({
id: resolvers.length - 1,
action: "prettyPrint",
code: code
})
})
}
return worker
}())
let replacePreWithCodeMirror = function(pre) {
let container = document.createElement("div")
container.classList.add("fpcm-container")
container.innerHTML = `
<div class="fpcm-nav">
<a data-link-id="lua" href="javascript:;" class="fpcm-nav-link active">Lua</a>
<a data-link-id="repl" href="javascript:;" class="fpcm-nav-link">Run</a>
<a href="javascript:;" class="fpcm-prettify-button fpcm-nav-right">Pretty Print</a>
</div>
<div class="fpcm-output">
<div data-box-id="lua" class="fpcm-code-box active"></div>
<div data-box-id="repl" class="fpcm-code-box"></div>
</div>
<div class="fpcm-resize-grabber">⋯</div>
`
let activeLink = container.querySelector(".fpcm-nav-link.active")
let activeCodeBox = container.querySelector(".fpcm-code-box.active")
let navLinkClicked = function(e) {
if (activeLink) {
activeLink.classList.remove("active")
}
e.target.classList.add("active")
activeLink = e.target
let codeBox = container.querySelector(`[data-box-id=${activeLink.dataset.linkId}]`)
if (activeCodeBox) {
activeCodeBox.classList.remove("active")
}
if (codeBox) {
codeBox.classList.add("active")
let codeMirror = codeBox.querySelector(".CodeMirror")
if (codeMirror && codeMirror.CodeMirror) {
codeMirror.CodeMirror.refresh()
}
}
activeCodeBox = codeBox
}
for (let navLink of container.querySelectorAll(".fpcm-nav-link")) {
navLink.addEventListener("click", navLinkClicked)
}
pre.parentElement.replaceChild(container, pre)
let luaBox = container.querySelector(".fpcm-code-box[data-box-id='lua']")
let luaMirror = CodeMirror(luaBox, {
value: pre.innerText
})
// lint the view after we register the lint function
setTimeout(() => {
luaMirror.performLint()
}, 0)
container.querySelector(".fpcm-prettify-button").addEventListener("click", (e) => {
gluaWorker.prettyPrintString(luaMirror.getValue())
.then((prettyPrinted) => {
luaMirror.setValue(prettyPrinted)
})
})
let grabber = container.querySelector(".fpcm-resize-grabber")
let isPointerDown = false
grabber.addEventListener("mousedown", (mouseDownEvent) => {
if (isPointerDown) { return }
let initialOutputHeight
let outputContainer = container.querySelector(".fpcm-output")
isPointerDown = true
let isDragging = false
mouseDownEvent.preventDefault()
let mouseMoveHandler = (e) => {
let movedY = e.pageY - mouseDownEvent.pageY
if (!isDragging && (Math.abs(movedY) > 3)) {
isDragging = true
initialOutputHeight = outputContainer.offsetHeight
}
if (isDragging) {
e.preventDefault()
outputContainer.style.height = `${initialOutputHeight + movedY}px`
}
}
let mouseUpHandler = () => {
isPointerDown = false
isDragging = false
window.removeEventListener("mousemove", mouseMoveHandler)
window.removeEventListener("mouseup", mouseUpHandler)
luaMirror.refresh()
}
window.addEventListener("mousemove", mouseMoveHandler)
window.addEventListener("mouseup", mouseUpHandler)
})
CodeMirror(container.querySelector(".fpcm-code-box[data-box-id='repl']"), {
value: "-- TODO"
})
}
let lintFunc = (code, callback, options, editor) => {
gluaWorker.lintString(code)
.then((result) => {
if (result == "good") {
return callback([])
} else {
return callback(result.map((o) => {
return {
message: o.msg,
severity: o.type,
from: CodeMirror.Pos(o.startLine, o.startPos),
to: CodeMirror.Pos(o.endLine, o.endPos)
}
}))
}
})
}
lintFunc.async = true
let currentForumSection = document.querySelectorAll(".navbit")[1].innerText
if (currentForumSection === "> Garry's Mod") {
console.log("[FPCM] loaded")
CodeMirror.defaults.mode = "lua"
CodeMirror.defaults.lint = true
CodeMirror.defaults.lineNumbers = true
CodeMirror.defaults.theme = "monokai"
CodeMirror.defaults.gutters = ["CodeMirror-lint-markers"]
// gluaLintString is not registered for a frame
setTimeout(() => {
CodeMirror.registerHelper("lint", "lua", lintFunc)
}, 0)
for (let pre of document.querySelectorAll("pre.bbcode_code")) {
replacePreWithCodeMirror(pre)
}
}