Skip to content

Commit a91d1cd

Browse files
authored
feat(Placeholder): don't render placeholder when content is null (#72)
1 parent 8499232 commit a91d1cd

File tree

3 files changed

+14
-10
lines changed

3 files changed

+14
-10
lines changed

demo/Playground.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export const Playground = React.memo<PlaygroundProps>((props) => {
5757
paragraphPlaceholder(_node, parent) {
5858
return parent?.type.name === BaseNode.Doc && parent.childCount === 1
5959
? 'Now... start typing'
60-
: 'Empty paragraph';
60+
: null;
6161
},
6262
},
6363
})

src/extensions/behavior/Cursor/gapcursor.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ const toDOM: WidgetConstructor = (view, getPos) => {
5757
const node = pType(view.state.schema).create();
5858

5959
const element = DOMSerializer.fromSchema(view.state.schema).serializeNode(node);
60-
element.appendChild(createPlaceholder(node, null, true));
60+
const placeholderDOM = createPlaceholder(node, null, true);
61+
if (placeholderDOM) element.appendChild(placeholderDOM);
6162
(element as Element).classList.add('ye-gapcursor');
6263
(element as HTMLElement).addEventListener('mousedown', () => {
6364
const pos = getPos();

src/extensions/behavior/Placeholder/index.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,12 @@ const getPlaceholderPluginKeys = (schema: Schema) => {
3232
const b = cn('placeholder');
3333

3434
export const createPlaceholder = (node: Node, parent: Node | null, focus?: boolean) => {
35+
const content = getPlaceholderContent(node, parent);
36+
if (!content) return null;
37+
3538
const placeholder = document.createElement('div');
3639
placeholder.classList.add(...b({focus}).split(' '));
37-
placeholder.textContent = getPlaceholderContent(node, parent);
38-
40+
placeholder.textContent = content;
3941
return placeholder;
4042
};
4143

@@ -67,18 +69,19 @@ const addDecoration = (
6769

6870
if (placeholderSpec.customPlugin) {
6971
widgetsMap[decorationPosition] = placeholderSpec.customPlugin;
70-
7172
return;
7273
}
7374

7475
const focus = decorationPosition === cursorPos;
75-
if (focus) {
76-
globalState.hasFocus = true;
77-
}
76+
77+
const placeholderDOM = createPlaceholder(node, parent, focus);
78+
if (!placeholderDOM) return;
79+
80+
if (focus) globalState.hasFocus = true;
7881

7982
widgetsMap[decorationPosition] = {
8083
pos: decorationPosition,
81-
toDOM: createPlaceholder(node, parent, focus),
84+
toDOM: placeholderDOM,
8285
};
8386
};
8487

@@ -185,7 +188,7 @@ function applyState(state: EditorState): PlaceholderPluginState {
185188
declare module 'prosemirror-model' {
186189
interface NodeSpec {
187190
placeholder?: {
188-
content: string | ((node: Node, parent?: Node | null) => string);
191+
content: string | ((node: Node, parent?: Node | null) => string | null);
189192
customPlugin?: PluginKey<DecorationSet>;
190193
alwaysVisible?: boolean;
191194
};

0 commit comments

Comments
 (0)