如何使用 Quill.js 将样式应用于匹配的文本

问题描述 投票:0回答:0

我正在使用 Quill.js 并尝试将自定义格式应用于与特定模式匹配的文本。

例如,我想匹配大括号之间的任何内容

{ ... }

我正在侦听“文本更改”事件并匹配插入的文本:

quillRef.current.on('text-change', (delta, oldDelta, source) => {
          delta.forEach((op) => {
            if (!quillRef.current) return
            if (typeof op.insert === 'string') {
              const matches = op.insert.match(tagRegex)

              if (matches) {
                const matchIndex = matches.index
                const matchLength = matches[0].length

                matchIndex &&
                  quillRef.current.formatText(matchIndex, matchLength, ['bold'])
              }
            }
          })
        })

现在我尝试将所有内容加粗{像这样输入},稍后我想给它一个自定义格式(给它背景颜色)。

我尝试过的一些事情:

Modifying the insert op directly
Using attributes or split to insert as separate op
Composing a new delta

但是这些方法最终要么对所有文本进行样式化,要么导致重复。

在 Quill 中响应文本更改时,有选择地仅设置匹配文本样式的正确方法是什么?

quill
© www.soinside.com 2019 - 2024. All rights reserved.