Skip to content

Commit

Permalink
Revert "Editor placeholder cursor visibility" (#1132)
Browse files Browse the repository at this point in the history
  • Loading branch information
emmatown committed May 10, 2024
1 parent 6c0e59a commit aa13ea9
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 28 deletions.
5 changes: 5 additions & 0 deletions .changeset/four-masks-tap.md
@@ -0,0 +1,5 @@
---
"@keystatic/core": patch
---

Fix typing when the placeholder is visible in the new editors
33 changes: 5 additions & 28 deletions packages/keystatic/src/form/fields/markdoc/editor/placeholder.ts
Expand Up @@ -7,42 +7,19 @@ export function placeholderPlugin(text: string) {
return new Plugin({
props: {
decorations(state) {
let doc = state.doc;
if (
state.doc.childCount === 1 &&
state.doc.firstChild?.isTextblock &&
state.doc.firstChild.content.size === 0
doc.childCount === 1 &&
doc.firstChild?.isTextblock &&
doc.firstChild.content.size === 0
) {
let placeholder = document.createElement('span');
placeholder.className = classes.placeholder;
placeholder.contentEditable = 'false'; // 1
placeholder.textContent = text;

return DecorationSet.create(state.doc, [
Decoration.widget(1, bookendWithZeroWidthSpaces(placeholder), {
// undocumented prop on `WidgetViewDesc` that stops the decoration
// from being wrapped with a contenteditable=false span.
// note:
// 1. we must add the contenteditable=false attr ourselves
// 2. this is a private API and may break in future versions
// see:
// - https://github.com/ProseMirror/prosemirror-view/blob/master/src/viewdesc.ts
raw: true,
}),
]);
return DecorationSet.create(doc, [Decoration.widget(1, placeholder)]);
}
},
},
});
}

// firefox bug where the cursor is hidden when beside `contenteditable=false`
// - https://discuss.prosemirror.net/t/firefox-contenteditable-false-cursor-bug/5016
// - https://bugzilla.mozilla.org/show_bug.cgi?id=1612076
function bookendWithZeroWidthSpaces(element: HTMLElement) {
let wrapper = document.createElement('span');
wrapper.append(zeroWidthSpace(), element, zeroWidthSpace());
return wrapper;
}
function zeroWidthSpace() {
return document.createTextNode('\u200B');
}

0 comments on commit aa13ea9

Please sign in to comment.