ContentEditableの中でダブルブラケットに囲まれた文字列を取得する
テキストノードの中身をカーソル位置で前後に分割する
前半部分を[[で分けて、最後の要素を取得する
そこに]]が含まれていないかをチェック
後半部分を]]で分けて、最初の要素を取得する
そこに[[が含まれていないかをチェック
それぞれの文字列を結合すればダブルブラケットの中身になる
Textboxで使っている。
code:script.js
function pageSuggest(e,obj){
selection= window.getSelection()
if (selection.focusNode.nodeName != "#text"){return;}
if (selection.anchorOffset < 2){return;}
const pops = document.querySelectorAll(".popupmenu")
pops.forEach(elm=>{
elm.remove();
})
txtNode = selection.focusNode
currentCaretPoint = selection.anchorOffset
const frontTextBody = txtNode.textContent.substring(0,currentCaretPoint)
const backTextBody = txtNode.textContent.substring(currentCaretPoint)
frontline = frontTextBody.split("[[").length > 1 ? frontTextBody.split("[[").slice(-1)0: undefined
backline = backTextBody.split("]]").length > 1 ? backTextBody.split("]]")0: undefined
if (frontline != undefined && backline != undefined ){
if (frontline.indexOf(']]') == -1 && backline.indexOf('[[') == -1){
console.log(frontline + backline)
const sgDiv = document.createElement("div")
sgDiv.innerText = frontline + backline
sgDiv.classList.add("popupmenu")
var clientRect = txtNode.parentElement.getBoundingClientRect() ;
sgDiv.style.top = clientRect.top - 30 + "px"
txtNode.parentElement.after(sgDiv)
//これをキーワードにしてサジェストを起こす
}}
}