用反应笔。我想要添加一个不可编辑的文本块,我可以创建污点,但是如果我尝试向它添加一个contenteditable=false属性,它就不能工作。我的代码如下
import ReactQuill from 'react-quill';
import './App.scss';
import 'react-quill/dist/quill.snow.css';
import { useState, useRef } from 'react';
const Quill = ReactQuill.Quill;
const BlockEmbed = Quill.import('blots/embed');
class Mention extends BlockEmbed {
  static create(value) {
    let node = super.create(value);
    node.innerText = value;
    // node.contenteditable = false;
    node.setAttribute("contenteditable", false);
    return node;
  }
  static value(node) {
    return node.childNodes[0].textContent;
  }
}
Mention.blotName = 'label';
Mention.tagName = 'SPAN';
Mention.className = 'ql-label';
Quill.register(Mention);
function App() {
  const [value, setValue] = useState('');
  const thisEditor = useRef(null);
  const inserMention = (thisEditor) => {
    const editor = thisEditor.getEditor();
    let range = editor.getSelection();
    let position = range ? range.index : 0;
    editor.insertEmbed(position, 'label');
  }
  return (
    <div className="container bg-crow-green bg-gradient px-0">
      <div className='mt-4 border rounded'>
        <ReactQuill ref={thisEditor} theme='snow' value={value} onChange={setValue} />
        <button type="button" className="btn mt-4 btn-danger w-25" onClick={() => inserMention(thisEditor.current)}>Insert</button>
      </div>
    </div >
  );
}
export default App;单击“插入”按钮,创建一个新的嵌入并将其添加到编辑器,但它是可编辑的,但我不想要。另一个问题是,我想引用新添加的嵌入,然后也更改它的值,我可以使用Parment.find()来完成这个任务,然后使用格式,但是我不知道如何在react中这样做。
发布于 2022-10-28 02:43:43
尝试使用camcelCase属性。
 node.setAttribute("contentEditable", false);https://stackoverflow.com/questions/65558682
复制相似问题