From 54f0c9381fce41c4ca46baebfd6281a8f9f9ff93 Mon Sep 17 00:00:00 2001 From: Hsieh Chin Fan Date: Tue, 24 Sep 2024 20:07:19 +0800 Subject: refactor: general menu * rename suggestionEle -> menu for general use cases * set Suggestion as module --- src/MenuItem.mjs | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 src/MenuItem.mjs (limited to 'src/MenuItem.mjs') 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 @@ +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 + } +} -- cgit v1.2.3-70-g09d2