aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/MenuItem.mjs
diff options
context:
space:
mode:
authorHsieh Chin Fan <pham@topo.tw>2024-09-24 20:07:19 +0800
committerHsieh Chin Fan <pham@topo.tw>2024-09-25 02:05:56 +0800
commit54f0c9381fce41c4ca46baebfd6281a8f9f9ff93 (patch)
tree4d81f07eeebf22a535d1e673d94856767910f724 /src/MenuItem.mjs
parent34587b0d17cddaf5d5a11ffda4019acda9bc0864 (diff)
refactor: general menu
* rename suggestionEle -> menu for general use cases * set Suggestion as module
Diffstat (limited to 'src/MenuItem.mjs')
-rw-r--r--src/MenuItem.mjs33
1 files changed, 33 insertions, 0 deletions
diff --git a/src/MenuItem.mjs b/src/MenuItem.mjs
new file mode 100644
index 0000000..50afdb5
--- /dev/null
+++ b/src/MenuItem.mjs
@@ -0,0 +1,33 @@
1export class Suggestion {
2 constructor({ text, replace }) {
3 this.text = text
4 this.replace = replace
5 }
6
7 createElement(codemirror) {
8 const option = document.createElement('div');
9 if (this.text.startsWith('<')) {
10 option.innerHTML = this.text;
11 } else {
12 option.innerText = this.text;
13 }
14 option.classList.add('container__suggestion');
15 option.onmouseover = () => {
16 Array.from(menu.children).forEach(s => s.classList.remove('focus'))
17 option.classList.add('focus')
18 }
19 option.onmouseout = () => {
20 option.classList.remove('focus')
21 }
22 option.onclick = () => {
23 const anchor = codemirror.getCursor()
24 codemirror.setSelection(anchor, { ...anchor, ch: 0 })
25 codemirror.replaceSelection(this.replace)
26 codemirror.focus();
27 const newAnchor = { ...anchor, ch: this.replace.length }
28 codemirror.setCursor(newAnchor);
29 };
30
31 return option
32 }
33}