Skip to content

Commit b7959eb

Browse files
refactor(tiptap): simplify first block selection check (#3842)
1 parent a8ec07c commit b7959eb

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

packages/tiptap/src/editor/index.tsx

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -93,17 +93,17 @@ const Editor = forwardRef<{ editor: TiptapEditor | null }, EditorProps>(
9393
const isAtStart = state.selection.$head.pos === 0;
9494

9595
const $head = state.selection.$head;
96-
const depth = $head.depth;
9796
let isInFirstBlock = false;
9897

99-
for (let d = depth; d > 0; d--) {
100-
const node = $head.node(d);
101-
if (node.type.name === "doc") continue;
102-
const parentNode = $head.node(d - 1);
103-
if (parentNode.type.name === "doc") {
104-
isInFirstBlock = parentNode.firstChild === node;
105-
break;
106-
}
98+
let node = state.doc.firstChild;
99+
let firstTextBlockPos = 0;
100+
while (node && !node.isTextblock) {
101+
firstTextBlockPos += 1;
102+
node = node.firstChild;
103+
}
104+
105+
if (node) {
106+
isInFirstBlock = $head.start($head.depth) === firstTextBlockPos + 1;
107107
}
108108

109109
if (

0 commit comments

Comments
 (0)