Skip to content

Commit

Permalink
Make join also clear incompatible inline content
Browse files Browse the repository at this point in the history
FIX: Allow `Transform.join` to clear incompatible inline content from the node
after the join.

Issue ProseMirror/prosemirror#1490
  • Loading branch information
marijnh committed Oct 11, 2024
1 parent d4607ed commit 0ffc0ba
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/structure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ export function canJoin(doc: Node, pos: number): boolean {
$pos.parent.canReplace(index, index + 1)
}

function canAppendWithSubstitutedLinebeaks(a: Node, b: Node) {
function canAppendWithSubstitutedLinebreaks(a: Node, b: Node) {
if (!b.content.size) a.type.compatibleContent(b.type)
let match: ContentMatch | null = a.contentMatchAt(a.childCount)
let {linebreakReplacement} = a.type.schema
Expand All @@ -241,7 +241,7 @@ function canAppendWithSubstitutedLinebeaks(a: Node, b: Node) {
}

function joinable(a: Node | null, b: Node | null) {
return !!(a && b && !a.isLeaf && canAppendWithSubstitutedLinebeaks(a, b))
return !!(a && b && !a.isLeaf && canAppendWithSubstitutedLinebreaks(a, b))
}

/// Find an ancestor of the given position that can be joined to the
Expand Down Expand Up @@ -272,10 +272,10 @@ export function joinPoint(doc: Node, pos: number, dir = -1) {
export function join(tr: Transform, pos: number, depth: number) {
let convertNewlines = null
let {linebreakReplacement} = tr.doc.type.schema
if (linebreakReplacement) {
let before = tr.doc.resolve(pos - depth).node().type
let pre = before.whitespace == "pre"
let supportLinebreak = !!before.contentMatch.matchType(linebreakReplacement)
let $before = tr.doc.resolve(pos - depth), beforeType = $before.node().type
if (linebreakReplacement && beforeType.inlineContent) {
let pre = beforeType.whitespace == "pre"
let supportLinebreak = !!beforeType.contentMatch.matchType(linebreakReplacement)
if (pre && !supportLinebreak) convertNewlines = false
else if (!pre && supportLinebreak) convertNewlines = true
}
Expand All @@ -284,6 +284,9 @@ export function join(tr: Transform, pos: number, depth: number) {
let $after = tr.doc.resolve(pos + depth)
replaceLinebreaks(tr, $after.node(), $after.before(), mapFrom)
}
if (beforeType.inlineContent)
clearIncompatible(tr, pos + depth - 1, beforeType,
$before.node().contentMatchAt($before.index()), convertNewlines == null)
let mapping = tr.mapping.slice(mapFrom), start = mapping.map(pos - depth)
tr.step(new ReplaceStep(start, mapping.map(pos + depth, - 1), Slice.empty, true))
if (convertNewlines === true) {
Expand Down

0 comments on commit 0ffc0ba

Please sign in to comment.