mirror of https://github.com/fantasticit/think.git
tiptap: fix get editor element clientWidth
parent
aac6238263
commit
bf913734ea
|
@ -3,13 +3,26 @@ import { Editor } from '@tiptap/core';
|
|||
const cache = new Map();
|
||||
|
||||
export function getEditorContainerDOMSize(editor: Editor): { width: number } {
|
||||
const targetNode = editor.options.element as HTMLElement;
|
||||
|
||||
if (!cache.has('width')) {
|
||||
cache.set('width', (editor.options.element as HTMLElement).offsetWidth);
|
||||
cache.set('width', targetNode.clientWidth);
|
||||
}
|
||||
|
||||
if (cache.has('width') && cache.get('width') <= 0) {
|
||||
cache.set('width', (editor.options.element as HTMLElement).offsetWidth);
|
||||
cache.set('width', targetNode.clientWidth);
|
||||
}
|
||||
|
||||
const config = { attributes: true, childList: true, subtree: true };
|
||||
const callback = function (mutationsList, observer) {
|
||||
cache.set('width', targetNode.clientWidth);
|
||||
};
|
||||
const observer = new MutationObserver(callback);
|
||||
observer.observe(targetNode, config);
|
||||
|
||||
editor.on('destroy', () => {
|
||||
observer.disconnect();
|
||||
});
|
||||
|
||||
return { width: cache.get('width') };
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue