Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve node input rule handling #4341

Merged
merged 2 commits into from
Aug 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
46 changes: 6 additions & 40 deletions packages/core/src/inputRules/nodeInputRule.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { NodeType } from '@tiptap/pm/model'
import { NodeSelection, TextSelection } from '@tiptap/pm/state'

import { InputRule, InputRuleFinder } from '../InputRule.js'
import { ExtendedRegExpMatchArray } from '../types.js'
Expand All @@ -20,18 +19,6 @@ export function nodeInputRule(config: {
*/
type: NodeType

/**
* Should the input rule replace the node or append to it
* If true, the node will be replaced
*/
blockReplace?: boolean

/**
* Insert empty paragraph after inserting the node
* Only works if blockReplace is true
*/
addExtraNewline?: boolean

/**
* A function that returns the attributes for the node
* can also be an object of attributes
Expand All @@ -47,13 +34,11 @@ export function nodeInputRule(config: {
handler: ({ state, range, match }) => {
const attributes = callOrReturn(config.getAttributes, undefined, match) || {}
const { tr } = state
const start = config.blockReplace ? range.from - 1 : range.from
const start = range.from
let end = range.to

const newNode = config.type.create(attributes)

const { $to } = tr.selection

if (match[1]) {
const offset = match[0].lastIndexOf(match[1])
let matchStart = start + offset
Expand All @@ -72,32 +57,13 @@ export function nodeInputRule(config: {
// insert node from input rule
tr.replaceWith(matchStart, end, newNode)
} else if (match[0]) {
tr.replaceWith(start, end, newNode)
tr.insert(start - 1, config.type.create(attributes)).delete(
tr.mapping.map(start),
tr.mapping.map(end),
)
}

if (config.blockReplace && config.addExtraNewline) {
if ($to.nodeAfter) {
if ($to.nodeAfter.isTextblock) {
tr.setSelection(TextSelection.create(tr.doc, $to.pos + 1))
} else if ($to.nodeAfter.isBlock) {
tr.setSelection(NodeSelection.create(tr.doc, $to.pos))
} else {
tr.setSelection(TextSelection.create(tr.doc, $to.pos - 1))
}
} else {
// add node after horizontal rule if it’s the end of the document
const defaultNode = $to.parent.type.contentMatch.defaultType?.create() || state.doc.type.contentMatch.defaultType?.create()

if (defaultNode) {
const newPos = start + newNode.nodeSize

tr.insert(newPos, defaultNode)
tr.setSelection(TextSelection.create(tr.doc, newPos))
}
}

tr.scrollIntoView()
}
tr.scrollIntoView()
},
})
}
2 changes: 0 additions & 2 deletions packages/extension-horizontal-rule/src/horizontal-rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,6 @@ export const HorizontalRule = Node.create<HorizontalRuleOptions>({
nodeInputRule({
find: /^(?:---|—-|___\s|\*\*\*\s)$/,
type: this.type,
blockReplace: true,
addExtraNewline: true,
}),
]
},
Expand Down