Skip to content

Instantly share code, notes, and snippets.

@danlucraft
Last active January 17, 2020 09:04
Show Gist options
  • Save danlucraft/d55456c6eb28d2f3249e06b94a0e35eb to your computer and use it in GitHub Desktop.
Save danlucraft/d55456c6eb28d2f3249e06b94a0e35eb to your computer and use it in GitHub Desktop.
Make sure there is always some text after a link inline, so typing after a link is not impossible
inlines: {
link: {
// No link should be present but empty (default normalize should remove
// it if this returns false).
text: function(string) {
return string.length !== 0
},
// If the text node after a link inline is empty...
next: function(node) {
return node.text.length !== 0
},
// ... then insert a space at the start of that text node.
normalize: function(editor, {code, node}) {
if (code === "next_sibling_invalid") {
node.nodes.find((el, ix) => {
const isLink = el.object === "inline" && el.type === "link"
const next = node.nodes.get(ix + 1)
const nextIsEmptyText = next && next.object === "text"
&& next.text === ""
if (isLink && nextIsEmptyText) {
editor.insertTextByKey(next.key, 0, " ")
// if the user pressed backspace we want to move the cursor
// backwards, but if there was a selection never mind.
const isExpanded = editor.value.selection.isExpanded
if (!isExpanded)
editor.moveBackward(1)
}
})
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment