无法访问模块函数中的 quill 实例
Unable to access quill instance within a module function
我有:
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...'
});
如果重要的话,我正在使用 https://github.com/afry/quill-mention and https://github.com/gtgalone/react-quilljs。
当我尝试访问 onSelect
函数中的 quill
实例时,我得到:
TypeError: Cannot read property 'getSelection' of undefined
我假设函数以某种方式被缓存并且 quill
尚未定义。有什么方法可以得到那个实例吗?
当quill有值时,您可以在useEffect
块中设置onSelect
回调。使用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]);
我有:
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...'
});
如果重要的话,我正在使用 https://github.com/afry/quill-mention and https://github.com/gtgalone/react-quilljs。
当我尝试访问 onSelect
函数中的 quill
实例时,我得到:
TypeError: Cannot read property 'getSelection' of undefined
我假设函数以某种方式被缓存并且 quill
尚未定义。有什么方法可以得到那个实例吗?
当quill有值时,您可以在useEffect
块中设置onSelect
回调。使用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]);