Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
range: NodeRange,
) {
let list = range.parent;
// Merge the list items into a single big item
for (let pos = range.end, i = range.endIndex - 1, e = range.startIndex; i > e; i--) {
pos -= list.child(i).nodeSize;
tr = tr.delete(pos - 1, pos + 1);
}
let $start = tr.doc.resolve(range.start);
let item = $start.nodeAfter;
let atStart = range.startIndex == 0, atEnd = range.endIndex == list.childCount
let parent = $start.node(-1), indexBefore = $start.index(-1)
if (!parent.canReplace(indexBefore + (atStart ? 0 : 1), indexBefore + 1,
item.content.append(atEnd ? Fragment.empty : Fragment.from(list)))) {
return tr;
}
let start = $start.pos, end = start + item.nodeSize
// Strip off the surrounding list. At the sides where we're not at
// the end of the list, the existing list is closed. At sides where
// this is the end, it is overwritten to its end.
tr = tr.step(new ReplaceAroundStep(start - (atStart ? 1 : 0), end + (atEnd ? 1 : 0), start + 1, end - 1,
new Slice((atStart ? Fragment.empty : Fragment.from(list.copy(Fragment.empty)))
.append(atEnd ? Fragment.empty : Fragment.from(list.copy(Fragment.empty))),
atStart ? 0 : 1, atEnd ? 0 : 1), atStart ? 0 : 1))
return tr;
}
// Try each open node in turn, starting from the innermost
for (let d = this.open.length - 1; d >= 0; d--) {
let open = this.open[d], wrap
// If pass > 1, it is allowed to wrap the node to help find a
// fit, so if findWrapping returns something, we add open
// nodes to the frontier for that wrapping.
if (pass > 1 && (wrap = open.match.findWrapping(child.type)) &&
!(parent && wrap.length && wrap[wrap.length - 1] == parent.type)) {
while (this.open.length - 1 > d) this.closeNode()
for (let w = 0; w < wrap.length; w++) {
open.match = open.match.matchType(wrap[w])
d++
open = {parent: wrap[w].create(),
match: wrap[w].contentMatch,
content: Fragment.empty, wrapper: true, openEnd: 0, depth: d + w}
this.open.push(open)
}
}
// See if the child fits here
let match = open.match.matchType(child.type)
if (!match) {
let fill = open.match.fillBefore(Fragment.from(child))
if (fill) {
for (let j = 0; j < fill.childCount; j++) {
let ch = fill.child(j)
this.addNode(open, ch, 0)
match = open.match.matchFragment(ch)
}
} else if (parent && open.match.matchType(parent.type)) {
// Don't continue looking further up if the parent node
export function mergeCells(state, dispatch) {
let sel = state.selection
if (!(sel instanceof CellSelection) || sel.$anchorCell.pos == sel.$headCell.pos) return false
let rect = selectedRect(state), {map} = rect
if (cellsOverlapRectangle(map, rect)) return false
if (dispatch) {
let tr = state.tr, seen = {}, content = Fragment.empty, mergedPos, mergedCell
for (let row = rect.top; row < rect.bottom; row++) {
for (let col = rect.left; col < rect.right; col++) {
let cellPos = map.map[row * map.width + col], cell = rect.table.nodeAt(cellPos)
if (seen[cellPos]) continue
seen[cellPos] = true
if (mergedPos == null) {
mergedPos = cellPos
mergedCell = cell
} else {
if (!isEmpty(cell)) content = content.append(cell.content)
let mapped = tr.mapping.map(cellPos + rect.tableStart)
tr.delete(mapped, mapped + cell.nodeSize)
}
}
}
tr.setNodeMarkup(mergedPos + rect.tableStart, null,
function fitLeftInner($from, depth, placed, placedBelow) {
let content = Fragment.empty, openEnd = 0, placedHere = placed[depth]
if ($from.depth > depth) {
let inner = fitLeftInner($from, depth + 1, placed, placedBelow || placedHere)
openEnd = inner.openEnd + 1
content = Fragment.from($from.node(depth + 1).copy(inner.content))
}
if (placedHere) {
content = content.append(placedHere.content)
openEnd = placedHere.openEnd
}
if (placedBelow) {
content = content.append($from.node(depth).contentMatchAt($from.indexAfter(depth)).fillBefore(Fragment.empty, true))
openEnd = 0
}
return {content, openEnd}
}
Transform.prototype.lift = function(range, target) {
let {$from, $to, depth} = range
let gapStart = $from.before(depth + 1), gapEnd = $to.after(depth + 1)
let start = gapStart, end = gapEnd
let before = Fragment.empty, openStart = 0
for (let d = depth, splitting = false; d > target; d--)
if (splitting || $from.index(d) > 0) {
splitting = true
before = Fragment.from($from.node(d).copy(before))
openStart++
} else {
start--
}
let after = Fragment.empty, openEnd = 0
for (let d = depth, splitting = false; d > target; d--)
if (splitting || $to.after(d + 1) < $to.end(d)) {
splitting = true
after = Fragment.from($to.node(d).copy(after))
openEnd++
} else {
end++
const updateFragmentNodes = updater => prevFrag => {
let frag = Fragment.empty;
const appendNodeToFragment = node =>
(frag = frag.append(Fragment.from(node)));
prevFrag.forEach(node =>
appendNodeToFragment(
node.copy(updateFragmentNodes(updater)(updater(node).content))
)
);
return frag;
};
function doWrapInList(
tr: Transform,
range: NodeRange,
wrappers: any,
doJoin: boolean,
nodeType: NodeType,
): Transform {
if (!wrappers) {
return tr;
}
let content = Fragment.empty;
for (let i = wrappers.length - 1; i >= 0; i--) {
content = Fragment.from(
wrappers[i].type.create(wrappers[i].attrs, content),
);
}
tr = tr.step(
new ReplaceAroundStep(range.start - (doJoin ? 2 : 0),
range.end,
range.start,
range.end,
new Slice(content, 0, 0), wrappers.length, true),
);
let found = 0
for (let i = 0; i < wrappers.length; i++) {
function fitRightClosed(node, openEnd, $from, depth, openStart) {
let match, content = node.content, count = content.childCount
if (openStart >= 0)
match = $from.node(depth).contentMatchAt($from.indexAfter(depth))
.matchFragment(content, openStart > 0 ? 1 : 0, count)
else
match = node.contentMatchAt(count)
if (openEnd > 0) {
let closed = fitRightClosed(content.lastChild, openEnd - 1, $from, depth + 1,
count == 1 ? openStart - 1 : -1)
content = content.replaceChild(count - 1, closed)
}
return node.copy(content.append(match.fillBefore(Fragment.empty, true)))
}
Transform.prototype.split = function(pos, depth = 1, typesAfter) {
let $pos = this.doc.resolve(pos), before = Fragment.empty, after = Fragment.empty
for (let d = $pos.depth, e = $pos.depth - depth, i = depth - 1; d > e; d--, i--) {
before = Fragment.from($pos.node(d).copy(before))
let typeAfter = typesAfter && typesAfter[i]
after = Fragment.from(typeAfter ? typeAfter.type.create(typeAfter.attrs, after) : $pos.node(d).copy(after))
}
return this.step(new ReplaceStep(pos, pos, new Slice(before.append(after), depth, depth), true))
}