Skip to content

Commit

Permalink
fix: placeholder caret in firefox
Browse files Browse the repository at this point in the history
  • Loading branch information
sibiraj-s committed Mar 6, 2021
1 parent 4ae87b9 commit 451f275
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
13 changes: 8 additions & 5 deletions src/lib/editor.component.scss
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,14 @@ $menubar-text-padding: 0 $menu-item-spacing;
}

.NgxEditor__Placeholder {
color: #6c757d;
opacity: 1;
user-select: none;
position: absolute;
cursor: text;
&::before {
color: #6c757d;
opacity: 1;
user-select: none;
position: absolute;
cursor: text;
content: attr(data-placeholder);
}
}

.NgxEditor__Content {
Expand Down
18 changes: 16 additions & 2 deletions src/lib/plugins/placeholder.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Plugin, EditorState, PluginKey, Transaction } from 'prosemirror-state';
import { DecorationSet, Decoration } from 'prosemirror-view';
import { Node as ProseMirrorNode } from 'prosemirror-model';

const PLACEHOLDER_CLASSNAME = 'NgxEditor__Placeholder';

Expand All @@ -17,22 +18,35 @@ const placeholderPlugin = (text?: string): Plugin => {
},
props: {
decorations(state: EditorState): DecorationSet {
const doc = state.doc;
const { doc } = state;

const placeholder = this.getState(state);

if (!placeholder) {
return DecorationSet.empty;
}

const decorations: Decoration[] = [];

const decorate = (node: ProseMirrorNode, pos: number) => {
if (doc.childCount === 1 && doc?.firstChild?.isTextblock && doc.firstChild.content.size === 0) {
const placeHolderEl = document.createElement('span');
placeHolderEl.classList.add(PLACEHOLDER_CLASSNAME);
placeHolderEl.textContent = placeholder;
return DecorationSet.create(doc, [Decoration.widget(1, placeHolderEl)]);
}

return DecorationSet.empty;
const placeholderNode = Decoration.node(pos, (pos + node.nodeSize), {
class: PLACEHOLDER_CLASSNAME,
'data-placeholder': placeholder
});

decorations.push(placeholderNode);
}
};

doc.descendants(decorate);
return DecorationSet.create(doc, decorations);
}
}
});
Expand Down

0 comments on commit 451f275

Please sign in to comment.