Skip to content

Commit 086beeb

Browse files
committed
Added decoration to TrailingNodeExtension to hide trailing block when editor is read-only
1 parent 50b1328 commit 086beeb

2 files changed

Lines changed: 41 additions & 0 deletions

File tree

packages/core/src/editor/editor.css

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,3 +195,8 @@ For the ShowSelectionPlugin
195195
background-color: highlight;
196196
padding: 2px 0;
197197
}
198+
199+
/* Hide trailing block when editor is not editable. */
200+
.bn-editor[contenteditable="false"] .bn-trailing-block {
201+
display: none;
202+
}

packages/core/src/extensions/TrailingNode/TrailingNode.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Plugin, PluginKey } from "prosemirror-state";
2+
import { Decoration, DecorationSet } from "prosemirror-view";
23
import { createExtension } from "../../editor/BlockNoteExtension.js";
34

45
// based on https://github.com/ueberdosis/tiptap/blob/40a9404c94c7fef7900610c195536384781ae101/demos/src/Experiments/TrailingNode/Vue/trailing-node.ts
@@ -19,6 +20,41 @@ export const TrailingNodeExtension = createExtension(() => {
1920
prosemirrorPlugins: [
2021
new Plugin({
2122
key: plugin,
23+
props: {
24+
decorations: (state) => {
25+
const { doc } = state;
26+
27+
const lastBlockGroup = doc.lastChild;
28+
if (!lastBlockGroup || lastBlockGroup.type.name !== "blockGroup") {
29+
return;
30+
}
31+
32+
const lastBlockContainer = lastBlockGroup.lastChild;
33+
if (
34+
!lastBlockContainer ||
35+
lastBlockContainer.type.name !== "blockContainer"
36+
) {
37+
return;
38+
}
39+
40+
const lastBlockContent = lastBlockContainer.firstChild;
41+
if (
42+
!lastBlockContent ||
43+
lastBlockContent.type.spec.content !== "inline*" ||
44+
lastBlockContent.content.size > 0
45+
) {
46+
return;
47+
}
48+
49+
const from = doc.content.size - 1 - lastBlockContainer.nodeSize;
50+
51+
return DecorationSet.create(doc, [
52+
Decoration.node(from, from + lastBlockContainer.nodeSize, {
53+
class: "bn-trailing-block",
54+
}),
55+
]);
56+
},
57+
},
2258
appendTransaction: (_, __, state) => {
2359
const { doc, tr, schema } = state;
2460
const shouldInsertNodeAtEnd = plugin.getState(state);

0 commit comments

Comments
 (0)