Skip to content

Commit

Permalink
feat: change to check void node
Browse files Browse the repository at this point in the history
  • Loading branch information
libra-co committed Jun 7, 2024
1 parent 3f579d3 commit bba63d2
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/editors/inline-editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ const InlineEditor = ({ enableEdit, value, editorApi, onSave, columns, onContent
}

// Focus at start of document, when document is empty
const isDoEmpty = isDocumentEmpty(editor.children);
const isDoEmpty = isDocumentEmpty(editor);
if (isDoEmpty) {
focusNode(editor);
}
Expand Down
2 changes: 1 addition & 1 deletion src/editors/simple-slate-editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ const SimpleSlateEditor = ({ value, editorApi, onSave, columns, onContentChanged

// Focus at start of document, when document is empty
const onEditorClick = useCallback(() => {
const isDocEmpty = isDocumentEmpty(editor.children);
const isDocEmpty = isDocumentEmpty(editor);
if (isDocEmpty) {
focusNode(editor);
}
Expand Down
2 changes: 1 addition & 1 deletion src/editors/slate-editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export default function SlateEditor({ value, editorApi, onSave, onContentChanged

// Focus at start of document, when document is empty
const onEditorClick = useCallback(() => {
const isDocEmpty = isDocumentEmpty(editor.children);
const isDocEmpty = isDocumentEmpty(editor);
if (isDocEmpty) {
focusFirstNode(editor);
}
Expand Down
15 changes: 8 additions & 7 deletions src/utils/common.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import checkIsUrl from 'is-url';
import { Node, Text } from 'slate';
import { Editor, Node, Text } from 'slate';

export const isMac = () => {
const platform = navigator.platform;
Expand Down Expand Up @@ -31,15 +31,16 @@ export const isUrl = (url) => {
return true;
};

// Check paragraph wrap only one text node with empty string
export const isDocumentEmpty = (document) => {
// Check document is empty or only contains void nodes
export const isDocumentEmpty = (editor) => {
const document = editor.children;
const [firstChildNode] = document;
// Check if document has only one block node
const isWrapperEmpty = document.length === 1 && Node.string(document[0]).length === 0;
const isWrapperEmpty = document.length === 1 && Node.string(firstChildNode).length === 0;
if (!isWrapperEmpty) return false;
// Check if the only one block node has only one text node with empty string
const children = document[0].children;
const isChildrenEmpty = children.length === 1 && Text.isText(children[0]);
if (!isChildrenEmpty) return false;
const hasVoidNode = firstChildNode.children.some(node => Editor.isVoid(editor, node));
if (hasVoidNode) return false;

return true;
};

0 comments on commit bba63d2

Please sign in to comment.