@selfcommunity/react-ui
Version:
React UI Components to integrate a Community created with SelfCommunity Platform.
24 lines (23 loc) • 1.08 kB
JavaScript
import { $canShowPlaceholderCurry } from '@lexical/text';
import { mergeRegister } from '@lexical/utils';
import { useState } from 'react';
import { useIsomorphicLayoutEffect } from '@selfcommunity/react-core';
function canShowPlaceholderFromCurrentEditorState(editor) {
return editor.getEditorState().read($canShowPlaceholderCurry(editor.isComposing()));
}
export function useCanShowPlaceholder(editor) {
const [canShowPlaceholder, setCanShowPlaceholder] = useState(() => canShowPlaceholderFromCurrentEditorState(editor));
useIsomorphicLayoutEffect(() => {
function resetCanShowPlaceholder() {
const currentCanShowPlaceholder = canShowPlaceholderFromCurrentEditorState(editor);
setCanShowPlaceholder(currentCanShowPlaceholder);
}
resetCanShowPlaceholder();
return mergeRegister(editor.registerUpdateListener(() => {
resetCanShowPlaceholder();
}), editor.registerEditableListener(() => {
resetCanShowPlaceholder();
}));
}, [editor]);
return canShowPlaceholder;
}