-
Notifications
You must be signed in to change notification settings - Fork 4
/
editor-codemirror.js
52 lines (49 loc) · 1.69 KB
/
editor-codemirror.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
'use strict'
define(function(require) {
if ((new URLSearchParams(window.location.search)).get('textarea') !== null) { return }
let system = require('play/system')
let players = require('player/players')
let {updateCode} = require('update-code')
let consoleOut = require('console')
let fxMixChain = require('play/effects/fxMixChain')
let editorDiv = document.getElementById('code-codemirror')
editorDiv.style.display = 'block'
let editor = CodeMirror(editorDiv, {
lineNumbers: true,
lineWrapping: true,
tabSize: 2,
inputStyle: "textarea",
smartIndent: false,
})
editor.setValue(localStorage.getItem('limut-code') || '')
editor.on('change', () => localStorage.setItem('limut-code', editor.getValue()))
let ctrlCode = (event, keys) => {
if (event.isComposing || event.keyCode === 229) { return false }
return ((event.ctrlKey || event.metaKey) && (keys.includes(event.keyCode) || keys.includes(event.key)))
}
document.addEventListener("keydown", event => {
if (ctrlCode(event, ['.', 190])) { window.stop() }
})
editorDiv.addEventListener("keydown", event => {
if (ctrlCode(event, [10, 13])) { window.go() }
})
editorDiv.addEventListener("keydown", event => {
if (ctrlCode(event, ['/', 191])) { window.comment() }
})
window.stop = () => {
system.resume()
fxMixChain.disconnectAll()
players.stopAll()
consoleOut('> Stop all players')
}
window.go = () => {
updateCode(editor.getValue())
if (editor.getValue().trim() !== '') {
editorDiv.style.backgroundColor = '#d9d9d980'
setTimeout(() => editorDiv.style.backgroundColor = 'transparent', 150)
}
}
window.comment = () => {
editor.toggleComment()
}
})