diff options
Diffstat (limited to 'src/MenuItem.mjs')
-rw-r--r-- | src/MenuItem.mjs | 33 |
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 @@ | |||
1 | export 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 | } | ||