aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/MenuItem.mjs
blob: 50afdb529bb22a562c5cd907f5d531b1c74f5bb3 (plain)
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
export class Suggestion {
  constructor({ text, replace }) {
    this.text = text
    this.replace = replace
  }

  createElement(codemirror) {
    const option = document.createElement('div');
    if (this.text.startsWith('<')) {
      option.innerHTML = this.text;
    } else {
      option.innerText = this.text;
    }
    option.classList.add('container__suggestion');
    option.onmouseover = () => {
      Array.from(menu.children).forEach(s => s.classList.remove('focus'))
      option.classList.add('focus')
    }
    option.onmouseout = () => {
      option.classList.remove('focus')
    }
    option.onclick = () => {
      const anchor = codemirror.getCursor()
      codemirror.setSelection(anchor, { ...anchor, ch: 0 })
      codemirror.replaceSelection(this.replace)
      codemirror.focus();
      const newAnchor = { ...anchor, ch: this.replace.length }
      codemirror.setCursor(newAnchor);
    };

    return option
  }
}