forked from RustPython/RustPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheditor.js
More file actions
95 lines (88 loc) · 3.19 KB
/
editor.js
File metadata and controls
95 lines (88 loc) · 3.19 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
import CodeMirror from 'codemirror';
CodeMirror.defineMode('notebook', function (config, _parserConfig) {
const nullMode = CodeMirror.getMode(config, 'text/plain');
const python = CodeMirror.getMode(config, 'python');
const markdown = CodeMirror.getMode(config, 'markdown');
const latex = CodeMirror.getMode(config, 'text/x-latex');
const javascript = CodeMirror.getMode(config, 'javascript');
const modeMap = {
py: python,
md: markdown,
math: latex,
js: javascript,
};
return {
startState() {
return {
mode: python,
modeState: python.startState(),
chunkStart: false,
};
},
token(stream, state) {
if (stream.sol() && stream.match('%%')) {
stream.eatSpace();
state.chunkStart = true;
return 'keyword';
}
if (state.chunkStart) {
const m = stream.match(/[\w\-]+/);
const name = m && m[0];
const mode = (state.mode = modeMap[name] || nullMode);
state.modeState = mode.startState ? mode.startState() : null;
state.chunkStart = false;
return 'keyword';
}
const { mode, modeState } = state;
return mode.token(stream, modeState);
},
indent(state, textAfter, line) {
const { mode, modeState } = state;
if (mode.indent) return mode.indent(modeState, textAfter, line);
},
innerMode(state) {
const { mode, modeState } = state;
return { mode, state: modeState };
},
};
});
CodeMirror.defineMIME('text/x-notebook', 'notebook');
function selectBuffer(editor, buffers, name) {
var buf = buffers[name];
if (buf.getEditor()) buf = buf.linkedDoc({ sharedHist: true });
var old = editor.swapDoc(buf);
var linked = old.iterLinkedDocs(function (doc) {
linked = doc;
});
if (linked) {
// Make sure the document in buffers is the one the other view is looking at
for (var name in buffers)
if (buffers[name] == old) buffers[name] = linked;
old.unlinkDoc(linked);
}
editor.focus();
// console.log(editor.getValue());
}
function openBuffer(buffers, name, text, mode, buffersDropDown, buffersList) {
buffers[name] = CodeMirror.Doc(text, mode);
let opt = document.createElement('option');
opt.appendChild(document.createTextNode(name));
buffersDropDown.appendChild(opt);
let li = document.createElement('li');
li.appendChild(document.createTextNode(name));
li.dataset.language = name;
buffersList.appendChild(li);
}
function newBuf(buffers, buffersDropDown, buffersList, primaryEditor) {
let name = prompt('Name your tab', '*scratch*');
if (name == null) return;
if (buffers.hasOwnProperty(name)) {
alert("There's already a buffer by that name.");
return;
}
openBuffer(buffers, name, '', 'python', buffersDropDown, buffersList);
selectBuffer(primaryEditor, buffers, name);
let sel = buffersDropDown;
sel.value = name;
}
export { selectBuffer, openBuffer, newBuf };