Skip to content

Commit 9f51dfb

Browse files
fix: Custom inline content placeholder bug and enter handling (#784)
* Fixed custom inline content placeholder bug and enter handling * Improved code clarity
1 parent 33048f4 commit 9f51dfb

File tree

5 files changed

+14
-16
lines changed

5 files changed

+14
-16
lines changed

packages/core/src/blocks/ListItemBlockContent/ListItemKeyboardShortcuts.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Editor } from "@tiptap/core";
22
import { getBlockInfoFromPos } from "../../api/getBlockInfoFromPos";
33

44
export const handleEnter = (editor: Editor) => {
5-
const { node, contentType } = getBlockInfoFromPos(
5+
const { contentNode, contentType } = getBlockInfoFromPos(
66
editor.state.doc,
77
editor.state.selection.from
88
)!;
@@ -23,9 +23,9 @@ export const handleEnter = (editor: Editor) => {
2323

2424
return editor.commands.first(({ state, chain, commands }) => [
2525
() =>
26-
// Changes list item block to a text block if the content is empty.
26+
// Changes list item block to a paragraph block if the content is empty.
2727
commands.command(() => {
28-
if (node.textContent.length === 0) {
28+
if (contentNode.childCount === 0) {
2929
return commands.BNUpdateBlock(state.selection.from, {
3030
type: "paragraph",
3131
props: {},
@@ -36,10 +36,10 @@ export const handleEnter = (editor: Editor) => {
3636
}),
3737

3838
() =>
39-
// Splits the current block, moving content inside that's after the cursor to a new block of the same type
40-
// below.
39+
// Splits the current block, moving content inside that's after the cursor
40+
// to a new block of the same type below.
4141
commands.command(() => {
42-
if (node.textContent.length > 0) {
42+
if (contentNode.childCount > 0) {
4343
chain()
4444
.deleteSelection()
4545
.BNSplitBlock(state.selection.from, true)

packages/core/src/blocks/ParagraphBlockContent/ParagraphBlockContent.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import {
44
} from "../../schema";
55
import { createDefaultBlockDOMOutputSpec } from "../defaultBlockHelpers";
66
import { defaultProps } from "../defaultProps";
7-
import { handleEnter } from "../ListItemBlockContent/ListItemKeyboardShortcuts";
87
import { getCurrentBlockContentType } from "../../api/getCurrentBlockContentType";
98

109
export const paragraphPropSchema = {
@@ -18,7 +17,6 @@ export const ParagraphBlockContent = createStronglyTypedTiptapNode({
1817

1918
addKeyboardShortcuts() {
2019
return {
21-
Enter: () => handleEnter(this.editor),
2220
"Mod-Alt-0": () => {
2321
if (getCurrentBlockContentType(this.editor) !== "inline*") {
2422
return true;

packages/core/src/editor/Block.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ NESTED BLOCKS
363363
}
364364

365365
/* PLACEHOLDERS*/
366-
.bn-inline-content:has(> .ProseMirror-trailingBreak):before {
366+
.bn-inline-content:has(> .ProseMirror-trailingBreak:only-child):before {
367367
/*float: left; */
368368
pointer-events: none;
369369
height: 0;

packages/core/src/extensions/Placeholder/PlaceholderPlugin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export const PlaceholderPlugin = (
1616
const styleSheet = styleEl.sheet!;
1717

1818
const getBaseSelector = (additionalSelectors = "") =>
19-
`.bn-block-content${additionalSelectors} .bn-inline-content:has(> .ProseMirror-trailingBreak):before`;
19+
`.bn-block-content${additionalSelectors} .bn-inline-content:has(> .ProseMirror-trailingBreak:only-child):before`;
2020

2121
const getSelector = (
2222
blockType: string | "default",

packages/core/src/pm-nodes/BlockContainer.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,7 @@ export const BlockContainer = Node.create<{
611611
// of the block.
612612
() =>
613613
commands.command(({ state }) => {
614-
const { node, depth } = getBlockInfoFromPos(
614+
const { contentNode, depth } = getBlockInfoFromPos(
615615
state.doc,
616616
state.selection.from
617617
)!;
@@ -620,7 +620,7 @@ export const BlockContainer = Node.create<{
620620
state.selection.$anchor.parentOffset === 0;
621621
const selectionEmpty =
622622
state.selection.anchor === state.selection.head;
623-
const blockEmpty = node.textContent.length === 0;
623+
const blockEmpty = contentNode.childCount === 0;
624624
const blockIndented = depth > 2;
625625

626626
if (
@@ -638,7 +638,7 @@ export const BlockContainer = Node.create<{
638638
// empty & at the start of the block.
639639
() =>
640640
commands.command(({ state, chain }) => {
641-
const { node, endPos } = getBlockInfoFromPos(
641+
const { contentNode, endPos } = getBlockInfoFromPos(
642642
state.doc,
643643
state.selection.from
644644
)!;
@@ -647,7 +647,7 @@ export const BlockContainer = Node.create<{
647647
state.selection.$anchor.parentOffset === 0;
648648
const selectionEmpty =
649649
state.selection.anchor === state.selection.head;
650-
const blockEmpty = node.textContent.length === 0;
650+
const blockEmpty = contentNode.childCount === 0;
651651

652652
if (selectionAtBlockStart && selectionEmpty && blockEmpty) {
653653
const newBlockInsertionPos = endPos + 1;
@@ -667,14 +667,14 @@ export const BlockContainer = Node.create<{
667667
// deletes the selection beforehand, if it's not empty.
668668
() =>
669669
commands.command(({ state, chain }) => {
670-
const { node } = getBlockInfoFromPos(
670+
const { contentNode } = getBlockInfoFromPos(
671671
state.doc,
672672
state.selection.from
673673
)!;
674674

675675
const selectionAtBlockStart =
676676
state.selection.$anchor.parentOffset === 0;
677-
const blockEmpty = node.textContent.length === 0;
677+
const blockEmpty = contentNode.childCount === 0;
678678

679679
if (!blockEmpty) {
680680
chain()

0 commit comments

Comments
 (0)