Skip to content

Commit 4d31d5b

Browse files
committed
chore: Update playground demo
1 parent 10cab8b commit 4d31d5b

File tree

2 files changed

+109
-37
lines changed

2 files changed

+109
-37
lines changed

playground/demo/javascript.vue

Lines changed: 94 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
<template>
22
<div>
33
<Codemirror
4+
id="cm-component"
45
v-model:value="code"
56
:options="cmOptions"
67
border
7-
:height="200"
8+
@ready="onEditorReady"
89
:width="400"
10+
:height="300"
911
/>
1012
</div>
1113
</template>
12-
<script lang="ts">
14+
<script lang="ts" setup>
1315
import { ref } from "vue";
1416
import type { EditorConfiguration } from "codemirror";
1517
import Codemirror from "codemirror-editor-vue3";
@@ -20,34 +22,97 @@ import Codemirror from "codemirror-editor-vue3";
2022
2123
// language
2224
import "codemirror/mode/javascript/javascript.js";
25+
import "codemirror/lib/codemirror.css";
26+
import "codemirror/addon/hint/show-hint.css";
27+
import "codemirror/mode/javascript/javascript";
28+
import "codemirror/addon/hint/show-hint";
29+
import "codemirror/addon/hint/javascript-hint";
30+
const code = ref(`function getCompletions(token, context) {
31+
var found = [], start = token.string;
32+
function maybeAdd(str) {
33+
if (str.indexOf(start) == 0) found.push(str);
34+
}
35+
function gatherCompletions(obj) {
36+
if (typeof obj == "string") forEach(stringProps, maybeAdd);
37+
else if (obj instanceof Array) forEach(arrayProps, maybeAdd);
38+
else if (obj instanceof Function) forEach(funcProps, maybeAdd);
39+
for (var name in obj) maybeAdd(name);
40+
}
2341
24-
export default defineComponent({
25-
components: {
26-
Codemirror,
27-
},
28-
setup() {
29-
const code = ref(`function findSequence(goal) {
30-
function find(start, history) {
31-
if (start == goal)
32-
return history;
33-
else if (start > goal)
34-
return null;
35-
else
36-
return find(start + 5, "(" + history + " + 5)") ||
37-
find(start * 3, "(" + history + " * 3)");
38-
}
39-
return find(1, "1");
40-
}`);
41-
42-
const cmOptions: EditorConfiguration = {
43-
mode: "application/typescript",
44-
lineWiseCopyCut: true,
45-
};
42+
if (context) {
43+
var obj = context.pop(), base;
44+
if (obj.className == "js-variable")
45+
base = window[obj.string];
46+
else if (obj.className == "js-string")
47+
base = "";
48+
else if (obj.className == "js-atom")
49+
base = 1;
50+
while (base != null && context.length)
51+
base = base[context.pop().string];
52+
if (base != null) gatherCompletions(base);
53+
}
54+
else {
55+
for (var v = token.state.localVars; v; v = v.next) maybeAdd(v.name);
56+
gatherCompletions(window);
57+
forEach(keywords, maybeAdd);
58+
}
59+
return found;
60+
}`);
4661
47-
return {
48-
code,
49-
cmOptions,
50-
};
62+
const cmOptions: EditorConfiguration = {
63+
lineNumbers: true,
64+
mode: { name: "javascript" },
65+
extraKeys: {
66+
// "Ctrl-Space": "autocomplete",
67+
"Ctrl-Space": () => {
68+
alert("Ctrl-Space");
69+
},
5170
},
52-
});
71+
};
72+
73+
function synonyms(cm: any, option: any) {
74+
const comp = [
75+
["here", "hither"],
76+
["asynchronous", "nonsynchronous"],
77+
["completion", "achievement", "conclusion", "culmination", "expirations"],
78+
["hinting", "advise", "broach", "imply"],
79+
["function", "action"],
80+
["provide", "add", "bring", "give"],
81+
["synonyms", "equivalents"],
82+
["words", "token"],
83+
["each", "every"],
84+
];
85+
return new Promise((accept) => {
86+
setTimeout(() => {
87+
const cursor = cm.getCursor(),
88+
line = cm.getLine(cursor.line);
89+
let start = cursor.ch,
90+
end = cursor.ch;
91+
while (start && /\w/.test(line.charAt(start - 1))) --start;
92+
while (end < line.length && /\w/.test(line.charAt(end))) ++end;
93+
const word = line.slice(start, end).toLowerCase();
94+
for (let i = 0; i < comp.length; i++) {
95+
if (comp[i].indexOf(word) !== -1) {
96+
return accept({
97+
list: comp[i],
98+
from: cm.pos(cursor.line, start),
99+
to: cm.pos(cursor.line, end),
100+
});
101+
}
102+
}
103+
return accept(null);
104+
}, 100);
105+
});
106+
}
107+
const onEditorReady = (cm: any) => {
108+
if (typeof Promise !== "undefined") {
109+
cm.setOption("hintOptions", { hint: synonyms });
110+
}
111+
};
53112
</script>
113+
114+
<style>
115+
/* #cm-component .CodeMirror {
116+
font-family: "Fira Code", monospace;
117+
} */
118+
</style>

playground/demo/merge.vue

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
<template>
2-
<Codemirror merge :options="cmOptions" :height="300" class="cm-component" original-style @change="onChange"
3-
@focus="onFocus" @ready="onReady" />
2+
<Codemirror
3+
merge
4+
:options="cmOptions"
5+
:height="300"
6+
class="cm-component"
7+
@change="onChange"
8+
@focus="onFocus"
9+
@ready="onReady"
10+
/>
411
</template>
512
<script lang="ts">
613
import { ref, defineComponent } from "vue";
@@ -48,15 +55,15 @@ export default defineComponent({
4855
// console.log("beforeChange", instance, obj);
4956
// });
5057
},
51-
onCursorActivity(
52-
instance: Editor,
53-
line: number,
54-
gutter: string,
55-
contextMenuEvent: MouseEvent,
56-
) {
58+
onCursorActivity(instance: Editor, line: number, gutter: string, contextMenuEvent: MouseEvent) {
5759
console.log("onGutterContextMenu", instance, line, gutter, contextMenuEvent);
5860
},
5961
};
6062
},
6163
});
6264
</script>
65+
<style>
66+
/* .cm-component > {
67+
font-family: "Fira Code", monospace !important;
68+
} */
69+
</style>

0 commit comments

Comments
 (0)