Skip to content

Instantly share code, notes, and snippets.

@skogsmaskin
Last active October 27, 2018 23:54
Show Gist options
  • Save skogsmaskin/0452149937e7ddb1448936ab79a6c6c4 to your computer and use it in GitHub Desktop.
Save skogsmaskin/0452149937e7ddb1448936ab79a6c6c4 to your computer and use it in GitHub Desktop.
export default function SplitNodeSelectionPlugin() {
return {
onCommand(command, editor, next) {
const splitOperationsInsertingNewBlock = editor.operations.filter(
op => op.type === 'split_node' && op.path.size === 1
)
if (splitOperationsInsertingNewBlock.size === 0) {
return next()
}
return splitOperationsInsertingNewBlock
.map(op => {
const path = List.of(op.path.get(0) + 1)
let node = editor.value.document.nodes.get(path.get(0))
if (!node) {
// New block not ready yet
return false
}
// Make it collapse to the end instead of the start
const customSelection = {
anchor: {path: [op.path.get(0) + 1, 0], offset: node.text.length},
focus: {path: [op.path.get(0) + 1, 0], offset: node.text.length}
}
editor.operations = editor.operations
.push(
Operation.create({
type: 'set_selection',
properties: customSelection,
selection: Selection.create(customSelection)
})
)
return true
})
.filter(Boolean).size > 0
? true
: next()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment