Files
Tom Moor f4c4018872 fix: Unfold toggle blocks when jumping to a comment (#14100)
* fix: Unfold toggle blocks when jumping to a comment

* fix: Reveal pending draft comment anchors in folded toggles
2026-10-15 21:40:53 -04:00

575 lines
14 KiB
TypeScript

import {
chainCommands,
joinTextblockBackward,
splitBlock,
} from "prosemirror-commands";
import { Slice, Fragment } from "prosemirror-model";
import { isInTable } from "prosemirror-tables";
import type { Command } from "prosemirror-state";
import { NodeSelection, TextSelection } from "prosemirror-state";
import { liftTarget, ReplaceAroundStep } from "prosemirror-transform";
import { v4 } from "uuid";
import ToggleBlock, {
Action,
toggleEventPluginKey,
toggleFoldPluginKey,
} from "../nodes/ToggleBlock";
import {
isToggleBlock,
isToggleBlockFolded,
getToggleBlockDepth,
isSelectionInToggleBlock,
isSelectionInToggleBlockHead,
isSelectionInToggleBlockBody,
isSelectionAtStartOfToggleBlockHead,
isSelectionInMiddleOfToggleBlockHead,
isSelectionAtEndOfToggleBlockHead,
detachToggleBlockBody,
attachToggleBlockBody,
} from "../queries/toggleBlock";
import { isInList } from "../queries/isInList";
import {
ancestors,
atBlockEnd,
atBlockStart,
deleteSelectionTr,
findCutAfter,
findCutBefore,
joinBackwardTr,
joinForwardTr,
liftChildrenOfNodeAt,
nearest,
prevSibling,
selectNodeBackwardTr,
selectNodeForwardTr,
wrapNodeAt,
} from "../utils";
// Commands
export const deleteSelectionPreservingBody: Command = (state, dispatch) => {
if (state.selection.empty) {
return false;
}
const { $from } = state.selection;
if (!isSelectionInToggleBlockHead(state)) {
return false;
}
const toggleBlock = $from.node($from.depth - 1);
if (!isToggleBlockFolded(state, toggleBlock)) {
return false;
}
const pos = $from.before($from.depth - 1);
const { tr: tr1, body } = detachToggleBlockBody(pos, state.tr);
let tr = deleteSelectionTr(tr1);
tr = attachToggleBlockBody(pos, body, tr);
dispatch?.(tr.scrollIntoView());
return true;
};
export const joinForwardPreservingBody: Command = (state, dispatch) => {
const { $cursor } = state.selection as TextSelection;
if (!$cursor || !isSelectionAtEndOfToggleBlockHead(state)) {
return false;
}
const toggleBlock = $cursor.node($cursor.depth - 1);
if (!isToggleBlockFolded(state, toggleBlock)) {
return false;
}
const pos = $cursor.before($cursor.depth - 1);
const { tr: tr1, body } = detachToggleBlockBody(pos, state.tr);
let tr = liftChildrenOfNodeAt(pos, tr1);
tr = joinForwardTr(tr);
tr = wrapNodeAt(pos, toggleBlock.type, toggleBlock.attrs, tr);
tr = attachToggleBlockBody(pos, body, tr);
dispatch?.(tr);
return true;
};
export const joinBackwardWithHead: Command = (state, dispatch) => {
const $cursor = atBlockStart(state.selection);
if (!$cursor) {
return false;
}
const $cut = findCutBefore($cursor);
if (!$cut) {
return false;
}
if (!$cut.nodeBefore || $cut.nodeBefore.type.name !== "container_toggle") {
return false;
}
const toggleBlock = $cut.nodeBefore;
if (isToggleBlockFolded(state, toggleBlock)) {
const pos = $cut.pos - toggleBlock.nodeSize;
const { tr: tr1, body } = detachToggleBlockBody(pos, state.tr);
let tr = liftChildrenOfNodeAt(pos, tr1);
tr = joinBackwardTr(tr);
tr = wrapNodeAt(pos, toggleBlock.type, toggleBlock.attrs, tr);
tr = attachToggleBlockBody(pos, body, tr);
dispatch?.(tr);
return true;
}
return joinTextblockBackward(state, dispatch);
};
export const joinBackwardWithBody: Command = (state, dispatch) => {
const $cursor = atBlockStart(state.selection);
if (!$cursor) {
return false;
}
const $cut = findCutBefore($cursor);
if (!$cut) {
return false;
}
if (!$cut.nodeBefore || $cut.nodeBefore.type.name !== "container_toggle") {
return false;
}
const toggleBlock = $cut.nodeBefore;
if (isToggleBlockFolded(state, toggleBlock)) {
return false;
}
return joinTextblockBackward(state, dispatch);
};
export const joinBackwardWithToggleblock: Command = chainCommands(
joinBackwardWithHead,
joinBackwardWithBody
);
export const selectNodeForwardPreservingBody: Command = (state, dispatch) => {
const { $cursor } = state.selection as TextSelection;
if (!$cursor || !isSelectionAtEndOfToggleBlockHead(state)) {
return false;
}
const toggleBlock = $cursor.node($cursor.depth - 1);
if (!isToggleBlockFolded(state, toggleBlock)) {
return false;
}
const pos = $cursor.before($cursor.depth - 1);
const { tr: tr1, body } = detachToggleBlockBody(pos, state.tr);
let tr = selectNodeForwardTr(tr1);
tr = attachToggleBlockBody(pos, body, tr);
dispatch?.(tr);
return true;
};
export const selectNodeBackwardPreservingBody: Command = (state, dispatch) => {
const $cursor = atBlockStart(state.selection);
if (!$cursor) {
return false;
}
const $cut = findCutBefore($cursor);
if (!$cut) {
return false;
}
if (!$cut.nodeBefore || $cut.nodeBefore.type.name !== "container_toggle") {
return false;
}
const toggleBlock = $cut.nodeBefore;
if (!isToggleBlockFolded(state, toggleBlock)) {
return false;
}
const pos = $cursor.before() - toggleBlock.nodeSize;
const { tr: tr1, body } = detachToggleBlockBody(pos, state.tr);
let tr = selectNodeBackwardTr(tr1);
tr = attachToggleBlockBody(pos, body, tr);
dispatch?.(tr);
return true;
};
export const indentBlock: Command = (state, dispatch) => {
// If inside a list, allow the list's Tab handler to handle indentation instead.
if (isInList(state)) {
return false;
}
// If inside a table, allow the table's Tab handler to move cells instead.
if (isInTable(state)) {
return false;
}
const { $from } = state.selection;
let before = -1;
for (let d = $from.depth; d >= 0; d--) {
const nodeBefore = prevSibling($from, d);
if (nodeBefore && nodeBefore.type === state.schema.nodes.container_toggle) {
// before of nodeBefore
before = $from.posAtIndex($from.index(d) - 1, d);
break;
}
}
if (before === -1) {
return false;
}
const toggle = state.doc.nodeAt(before)!;
const from = before + toggle.nodeSize;
const node = state.doc.nodeAt(from)!;
const to = from + node.nodeSize;
if (
!toggle.canReplace(
toggle.childCount,
toggle.childCount,
Fragment.from(node)
)
) {
return false;
}
// Move the block to the end of the toggle body without requiring it to be
// a valid toggle head.
const slice = new Slice(Fragment.from(toggle), 0, 0);
const step = new ReplaceAroundStep(
before,
to,
from,
to,
slice,
toggle.nodeSize - 1,
false
);
const tr = state.tr.step(step).scrollIntoView();
if (dispatch) {
dispatch(tr);
}
return true;
};
export const toggleBlock: Command = (state, dispatch) => {
const { $from } = state.selection;
if (!isSelectionInToggleBlock(state)) {
return false;
}
// If inside a table, allow the table's handler to add a row instead.
if (isInTable(state)) {
return false;
}
const isToggle = isToggleBlock(state);
const toggle = nearest(ancestors($from).filter(isToggle));
if (!toggle) {
return false;
}
const d = getToggleBlockDepth($from, toggle);
const pos = $from.before(d);
const isFolded = isToggleBlockFolded(state, toggle);
dispatch?.(
state.tr
.setMeta(toggleFoldPluginKey, {
type: isFolded ? Action.UNFOLD : Action.FOLD,
at: pos,
})
.setMeta(toggleEventPluginKey, {
type: isFolded ? Action.UNFOLD : Action.FOLD,
at: pos,
})
);
return true;
};
/**
* Unfold every folded toggle block that hides the given position in its body.
*
* @param pos the document position to reveal.
* @returns a command that unfolds the toggle blocks, or false when none hide the position.
*/
export const unfoldToggleBlocksAt =
(pos: number): Command =>
(state, dispatch) => {
const pluginState = toggleFoldPluginKey.getState(state);
if (!pluginState) {
return false;
}
const $pos = state.doc.resolve(pos);
const isToggle = isToggleBlock(state);
const targets: number[] = [];
for (let d = $pos.depth; d > 0; d--) {
const node = $pos.node(d);
// The head is the first child and stays visible when folded.
if (
isToggle(node) &&
pluginState.foldedIds.has(node.attrs.id) &&
$pos.index(d) > 0
) {
targets.push($pos.before(d));
}
}
if (!targets.length) {
return false;
}
dispatch?.(
state.tr.setMeta(toggleFoldPluginKey, {
type: Action.UNFOLD,
at: targets,
})
);
return true;
};
export const createParagraphNearPreservingBody: Command = (state, dispatch) => {
const { $cursor } = state.selection as TextSelection;
if (!$cursor) {
return false;
}
const atStart = isSelectionAtStartOfToggleBlockHead(state);
const atEnd = isSelectionAtEndOfToggleBlockHead(state);
if (!atStart && !atEnd) {
return false;
}
const toggle = $cursor.node(-1);
if (ToggleBlock.isHeadEmpty(toggle)) {
return false;
}
if (!isToggleBlockFolded(state, toggle)) {
return false;
}
const pos = atBlockStart(state.selection)
? $cursor.before(-1)
: $cursor.after(-1);
let tr = state.tr;
tr = tr.insert(pos, state.schema.nodes.paragraph.create());
const $before = tr.doc.resolve(tr.selection.$from.before(-1));
const $after = tr.doc.resolve(tr.selection.$to.after(-1));
tr = atBlockStart(tr.selection)
? tr.setSelection(TextSelection.near($before))
: tr.setSelection(TextSelection.near($after));
dispatch?.(tr);
return true;
};
export const liftAllEmptyChildBlocks: Command = (state, dispatch) => {
const { $cursor } = state.selection as TextSelection;
if (!$cursor || !isSelectionAtStartOfToggleBlockHead(state)) {
return false;
}
const toggle = $cursor.node(-1);
if (!ToggleBlock.isEmpty(toggle)) {
return false;
}
dispatch?.(liftChildrenOfNodeAt($cursor.before(-1), state.tr));
return true;
};
export const liftAllChildBlocksOfNodeBefore: Command = (state, dispatch) => {
const { $cursor } = state.selection as TextSelection;
if (!$cursor || !isSelectionAtStartOfToggleBlockHead(state)) {
return false;
}
dispatch?.(liftChildrenOfNodeAt($cursor.before(-1), state.tr));
return true;
};
export const liftAllChildBlocksOfNodeAfter: Command = (state, dispatch) => {
const $cursor = atBlockEnd(state.selection);
if (!$cursor) {
return false;
}
const $cut = findCutAfter($cursor);
if (!$cut) {
return false;
}
if (!$cut.nodeAfter || $cut.nodeAfter.type.name !== "container_toggle") {
return false;
}
dispatch?.(liftChildrenOfNodeAt($cut.pos, state.tr));
return true;
};
export const dedentBlocks: Command = (state, dispatch) => {
// If inside a list, allow the list's Shift-Tab handler to handle outdentation instead.
if (isInList(state)) {
return false;
}
// If inside a table, allow the table's Shift-Tab handler to move cells instead.
if (isInTable(state)) {
return false;
}
const { $from } = state.selection;
const isToggle = isToggleBlock(state);
const ancestor = nearest(ancestors($from).filter(isToggle));
if (!ancestor) {
return false;
}
const d = getToggleBlockDepth($from, ancestor);
const $fr_ =
state.selection instanceof NodeSelection
? state.doc.resolve($from.pos + 1)
: $from;
const $to_ = state.doc.resolve($from.end(d) - 1);
const range = $fr_.blockRange($to_, (node) => node.eq(ancestor));
if (range === null) {
return false;
}
const target = liftTarget(range);
if (target === null) {
return false;
}
const tr = state.tr;
tr.lift(range, target);
if (dispatch) {
dispatch(tr);
}
return true;
};
/**
* Exit toggle block when pressing Enter in the last empty paragraph within the body.
*/
export const exitToggleBlockOnEmptyParagraph: Command = (state, dispatch) => {
const { $cursor } = state.selection as TextSelection;
if (!$cursor) {
return false;
}
if (!isSelectionInToggleBlockBody(state)) {
return false;
}
// Check if current node is an empty paragraph
const node = $cursor.parent;
if (node.type !== state.schema.nodes.paragraph || node.content.size > 0) {
return false;
}
// Check if this is the last node in the toggle block body
const parentOfParagraph = $cursor.node(-1);
if ($cursor.index(-1) !== parentOfParagraph.childCount - 1) {
return false;
}
// Check if the paragraph is a direct child of the toggle block
if (parentOfParagraph.type !== state.schema.nodes.container_toggle) {
return false;
}
// Find the toggle block ancestor
const isToggle = isToggleBlock(state);
const ancestor = nearest(ancestors($cursor).filter(isToggle));
if (!ancestor) {
return false;
}
// Create a range scoped to the toggle block ancestor
const d = getToggleBlockDepth($cursor, ancestor);
const $start = state.doc.resolve($cursor.start(d + 1));
const $end = state.doc.resolve($cursor.end(d + 1));
const range = $start.blockRange($end, (n) => n.eq(ancestor));
if (range === null) {
return false;
}
const target = liftTarget(range);
if (target === null) {
return false;
}
const tr = state.tr;
tr.lift(range, target);
dispatch?.(tr);
return true;
};
export const splitBlockPreservingBody: Command = (state, dispatch) => {
const { $cursor } = state.selection as TextSelection;
if (!$cursor || !isSelectionInMiddleOfToggleBlockHead(state)) {
return false;
}
const toggle = $cursor.node($cursor.depth - 1);
if (!isToggleBlockFolded(state, toggle)) {
return false;
}
let tr = state.tr;
tr = tr.insert(
$cursor.after(-1),
toggle.firstChild!.type.create(
undefined,
tr.doc.slice($cursor.pos, $cursor.end()).content
)
);
tr = wrapNodeAt($cursor.after(-1), toggle.type, { id: v4() }, tr);
tr = tr.setSelection(
TextSelection.near(tr.doc.resolve($cursor.after(-1)), 1)
);
tr = tr.delete($cursor.pos, $cursor.end());
dispatch?.(tr);
return true;
};
export const splitTopLevelBlockWithinBody: Command = (state, dispatch) => {
const { $from } = state.selection;
if (!isSelectionInToggleBlockBody(state)) {
return false;
}
const isToggle = isToggleBlock(state);
const ancestor = nearest(ancestors($from).filter(isToggle));
if (!ancestor) {
return false;
}
const d = getToggleBlockDepth($from, ancestor);
if (d === $from.depth - 1) {
// split if the block containing cursor is a direct child of a toggle block
return splitBlock(state, dispatch);
}
return false;
};