forked from SciSharp/BotSharp-UI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeScript.svelte
More file actions
102 lines (86 loc) · 2.99 KB
/
CodeScript.svelte
File metadata and controls
102 lines (86 loc) · 2.99 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
96
97
98
99
100
101
102
<script>
import { onMount, createEventDispatcher } from "svelte";
import CodeMirror from "svelte-codemirror-editor";
import { keymap, lineNumbers } from "@codemirror/view";
import { indentUnit, indentOnInput, indentService } from "@codemirror/language";
import { defaultKeymap, history, indentWithTab, historyKeymap } from "@codemirror/commands";
import { EditorState } from "@codemirror/state";
import { python } from "@codemirror/lang-python";
import { javascript } from "@codemirror/lang-javascript";
import { oneDark } from "@codemirror/theme-one-dark";
const dispatch = createEventDispatcher();
/** @type {string} */
export let language = 'python';
/** @type {string} */
export let scriptText;
/** @type {string} */
export let containerClasses = '';
/** @type {boolean} */
export let useDarkTheme = true;
/** @type {boolean} */
export let hideLineNumber = false;
/** @type {boolean} */
export let editable = true;
/** @type {import("@codemirror/state").Extension[]} */
const baseExtensions = [
indentUnit.of(" "),
EditorState.tabSize.of(4),
indentOnInput(),
history(),
keymap.of([...defaultKeymap, ...historyKeymap, indentWithTab]),
];
/** @type {import("@codemirror/state").Extension[]} */
let extensions = [];
onMount(() => {
if (language === 'python') {
extensions = [
python(),
indentService.of((context, pos) => {
const prevLine = pos > 0 ? context.state.doc.lineAt(pos - 1) : null;
if (prevLine) {
const prevText = prevLine.text;
const match = prevText.match(/^(\s*)/);
const baseIndent = match ? match[1].length : 0;
// Check if previous line ends with : (control structure)
if (prevText.trimEnd().endsWith(':')) {
return baseIndent + 4;
}
return baseIndent;
}
return 0;
}),
...baseExtensions
];
} else if (language === 'javascript') {
extensions = [
javascript(),
...baseExtensions
];
} else {
extensions = [
...baseExtensions
];
}
if (hideLineNumber) {
extensions = [
lineNumbers({ formatNumber: () => "" }),
...extensions
];
}
});
/** @param {any} e */
function handleChange(e) {
dispatch('change', {
text: e.detail
});
}
</script>
<CodeMirror
class={`code-script-container ${containerClasses}`}
lineWrapping
theme={useDarkTheme ? oneDark : null}
editable={editable}
extensions={extensions}
value={scriptText}
on:change={e => handleChange(e)}
/>