Sha*_*oon 5 javascript closures reactjs quill
我有:
const formats = ['header', 'bold', 'italic', 'underline', 'list', 'prediction', 'accepted', 'mention'];
const modules = {
mention: {
allowedChars: /^[a-zA-Z0-9_]*$/,
mentionDenotationChars: ['@'],
showDenotationChar: false,
spaceAfterInsert: false,
onSelect: (item, insertItem) => {
clearDocumentState(0)
const cursorPos = quill.getSelection()
console.log({ cursorPos })
insertItem(item)
},
source: () => { }
},
toolbar: [
['bold', 'italic', 'underline'],
[{ list: 'ordered' }, { list: 'bullet' }],
[{ header: [1, 2, false] }],
['clean']
]
};
const { quill, quillRef, Quill } = useQuill({
formats,
modules,
placeholder: 'Start writing something amazing...'
});
Run Code Online (Sandbox Code Playgroud)
如果重要的话,我正在使用https://github.com/afry/quill-mention和https://github.com/gtgalone/react-quilljs。
当我尝试访问函数中的quill实例时onSelect,我得到:
TypeError: Cannot read property 'getSelection' of undefined
Run Code Online (Sandbox Code Playgroud)
我假设该函数以某种方式被缓存并且quill尚未定义。有什么办法可以得到那个实例吗?
当 quill 有值时,您可以onSelect在 block 中设置回调。useEffect使用getModule获取mention模块并设置相应的option:
React.useEffect(() => {
if (quill) {
quill.getModule('mention').options.onSelect = ((item, insertItem) => {
clearDocumentState(0);
const cursorPos = quill.getSelection();
console.log({ cursorPos });
insertItem(item);
});
}
}, [quill]);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
80 次 |
| 最近记录: |