Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
let $from = tr.doc.resolve(from), depth = $from.sharedDepth(to), parent = $from.node(depth)
for (let index = $from.indexAfter(depth), pos = $from.after(depth + 1); pos <= to; ++index) {
let after = parent.maybeChild(index)
if (!after) break
if (index && joinable.indexOf(pos) == -1) {
let before = parent.child(index - 1)
if (before.type == after.type && isJoinable(before, after))
joinable.push(pos)
}
pos += after.nodeSize
}
}
// Join the joinable points
joinable.sort((a, b) => a - b)
for (let i = joinable.length - 1; i >= 0; i--) {
if (canJoin(tr.doc, joinable[i])) tr.join(joinable[i])
}
dispatch(tr)
}
}
export function joinUp(state, dispatch) {
let sel = state.selection, nodeSel = sel instanceof NodeSelection, point
if (nodeSel) {
if (sel.node.isTextblock || !canJoin(state.doc, sel.from)) return false
point = sel.from
} else {
point = joinPoint(state.doc, sel.from, -1)
if (point == null) return false
}
if (dispatch) {
let tr = state.tr.join(point)
if (nodeSel) tr.setSelection(NodeSelection.create(tr.doc, point - state.doc.resolve(point).nodeBefore.nodeSize))
dispatch(tr.scrollIntoView())
}
return true
}
export function joinDown(state, dispatch) {
let sel = state.selection, point
if (sel instanceof NodeSelection) {
if (sel.node.isTextblock || !canJoin(state.doc, sel.to)) return false
point = sel.to
} else {
point = joinPoint(state.doc, sel.to, 1)
if (point == null) return false
}
if (dispatch)
dispatch(state.tr.join(point).scrollIntoView())
return true
}
function deleteBarrier(state, $cut, dispatch) {
let before = $cut.nodeBefore, after = $cut.nodeAfter, conn, match
if (before.type.spec.isolating || after.type.spec.isolating) return false
if (joinMaybeClear(state, $cut, dispatch)) return true
if ($cut.parent.canReplace($cut.index(), $cut.index() + 1) &&
(conn = (match = before.contentMatchAt(before.childCount)).findWrapping(after.type)) &&
match.matchType(conn[0] || after.type).validEnd) {
if (dispatch) {
let end = $cut.pos + after.nodeSize, wrap = Fragment.empty
for (let i = conn.length - 1; i >= 0; i--)
wrap = Fragment.from(conn[i].create(null, wrap))
wrap = Fragment.from(before.copy(wrap))
let tr = state.tr.step(new ReplaceAroundStep($cut.pos - 1, end, $cut.pos, end, new Slice(wrap, 1, 0), conn.length, true))
let joinAt = end + 2 * conn.length
if (canJoin(tr.doc, joinAt)) tr.join(joinAt)
dispatch(tr.scrollIntoView())
}
return true
}
let selAfter = Selection.findFrom($cut, 1)
let range = selAfter && selAfter.$from.blockRange(selAfter.$to), target = range && liftTarget(range)
if (target != null && target >= $cut.depth) {
if (dispatch) dispatch(state.tr.lift(range, target).scrollIntoView())
return true
}
return false
}
function joinMaybeClear(state, $pos, dispatch) {
let before = $pos.nodeBefore, after = $pos.nodeAfter, index = $pos.index()
if (!before || !after || !before.type.compatibleContent(after.type)) return false
if (!before.content.size && $pos.parent.canReplace(index - 1, index)) {
if (dispatch) dispatch(state.tr.delete($pos.pos - before.nodeSize, $pos.pos).scrollIntoView())
return true
}
if (!$pos.parent.canReplace(index, index + 1) || !(after.isTextblock || canJoin(state.doc, $pos.pos)))
return false
if (dispatch)
dispatch(state.tr
.clearIncompatible($pos.pos, before.type, before.contentMatchAt(before.childCount))
.join($pos.pos)
.scrollIntoView())
return true
}
export default function joinDown(tr: Transform): Transform {
const sel = tr.selection;
let point;
if (sel instanceof NodeSelection) {
if (sel.node.isTextblock || !canJoin(tr.doc, sel.to)) {
return tr;
}
point = sel.to;
} else {
point = joinPoint(tr.doc, sel.to, 1);
if (point === null || point === undefined) {
return tr;
}
}
tr = tr.join(point);
return tr;
}