在基于文本的可编辑内容中显示div

时间:2018-09-10 15:35:08

标签: javascript html contenteditable

我需要基于contenteditable div文本在contenteditable div中添加div。示例案例:

const ceDiv = document.getElementById('foo')

//and when text in ceDiv ends with {{ i need to show another div in current caret position

类似于Facebook提到朋友的事情

我现在所做的是将div {{var}}替换为div:

export function findAndReplaceBrackets(string: string): string {
    const bracketReg = /\{{2}(.*?)\}{2}/ig
    const brackets = string.match(bracketReg)
    if (!brackets) return string
    string = string.replace(/<\/?[^>]+(>|$)/g, '') // reset variables in string
    brackets.forEach(variable => {
        string = string.replace(variable, `<span style="
            cursor: pointer;
            background-color: blue;
            color: #ffffff;
            text-transform: uppercase;
            font-size: 13px;
            padding: 1px;
            margin: 0 2px;
            border-radius: 4px
        ">${variable}</span>`)
    })

    return string
}

但这根本不能解决我的问题

0 个答案:

没有答案