mirror of
https://github.com/outline/outline.git
synced 2026-11-20 21:23:06 +03:00
* feat: Alt-click toggle disclosure folds or unfolds all toggle blocks Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> * Alt toggles nested blocks, Mod toggles all blocks Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> --------- Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
180 lines
5.9 KiB
TypeScript
180 lines
5.9 KiB
TypeScript
import type { Node as ProsemirrorNode } from "prosemirror-model";
|
|
import type { EditorView, NodeView, DecorationSource } from "prosemirror-view";
|
|
import type { Decoration } from "prosemirror-view";
|
|
import { isModKey } from "../../utils/keyboard";
|
|
import { EditorStyleHelper } from "../styles/EditorStyleHelper";
|
|
import {
|
|
Action,
|
|
toggleEventPluginKey,
|
|
toggleFoldPluginKey,
|
|
toggleStorageKey,
|
|
} from "./ToggleBlock";
|
|
|
|
/**
|
|
* Custom NodeView for toggle blocks that handles fold/unfold UI interactions.
|
|
*/
|
|
export class ToggleBlockView implements NodeView {
|
|
dom: HTMLDivElement;
|
|
contentDOM: HTMLDivElement;
|
|
private button: HTMLButtonElement;
|
|
private node: ProsemirrorNode;
|
|
private view: EditorView;
|
|
private getPos: () => number | undefined;
|
|
private boundBroadcastFoldState: (event: StorageEvent) => void;
|
|
|
|
constructor(
|
|
node: ProsemirrorNode,
|
|
view: EditorView,
|
|
getPos: () => number | undefined,
|
|
decorations: readonly Decoration[],
|
|
_innerDecorations: DecorationSource
|
|
) {
|
|
this.node = node;
|
|
this.view = view;
|
|
this.getPos = getPos;
|
|
|
|
// Create DOM structure
|
|
this.dom = document.createElement("div");
|
|
this.dom.className = EditorStyleHelper.toggleBlock;
|
|
|
|
this.button = document.createElement("button");
|
|
this.button.className = EditorStyleHelper.toggleBlockButton;
|
|
this.button.contentEditable = "false";
|
|
this.button.innerHTML =
|
|
'<svg fill="currentColor" width="12" height="24" viewBox="6 0 12 24" xmlns="http://www.w3.org/2000/svg"><path d="M8.23823905,10.6097108 L11.207376,14.4695888 L11.207376,14.4695888 C11.54411,14.907343 12.1719566,14.989236 12.6097108,14.652502 C12.6783439,14.5997073 12.7398293,14.538222 12.792624,14.4695888 L15.761761,10.6097108 L15.761761,10.6097108 C16.0984949,10.1719566 16.0166019,9.54410997 15.5788477,9.20737601 C15.4040391,9.07290785 15.1896811,9 14.969137,9 L9.03086304,9 L9.03086304,9 C8.47857829,9 8.03086304,9.44771525 8.03086304,10 C8.03086304,10.2205442 8.10377089,10.4349022 8.23823905,10.6097108 Z" /></svg>';
|
|
this.button.addEventListener("mousedown", this.handleToggleButtonClick);
|
|
|
|
this.contentDOM = document.createElement("div");
|
|
this.contentDOM.className = EditorStyleHelper.toggleBlockContent;
|
|
this.contentDOM.addEventListener("click", this.handleToggleHeadClick);
|
|
|
|
this.dom.appendChild(this.button);
|
|
this.dom.appendChild(this.contentDOM);
|
|
|
|
// Set initial fold state from decorations
|
|
this.syncFoldState(decorations);
|
|
|
|
// Listen for cross-tab storage changes
|
|
this.boundBroadcastFoldState = this.broadcastFoldState.bind(this);
|
|
window.addEventListener("storage", this.boundBroadcastFoldState);
|
|
}
|
|
|
|
private handleToggleButtonClick = (event: MouseEvent) => {
|
|
event.preventDefault();
|
|
if (event.button !== 0) {
|
|
return;
|
|
}
|
|
|
|
this.handleToggle(event);
|
|
};
|
|
|
|
private handleToggleHeadClick = (event: MouseEvent) => {
|
|
// When editable, clicking the title places the caret rather than toggling.
|
|
if (this.view.editable || event.button !== 0) {
|
|
return;
|
|
}
|
|
|
|
const target = event.target as HTMLElement;
|
|
const head = this.contentDOM.querySelector(
|
|
`.${EditorStyleHelper.toggleBlockHead}`
|
|
);
|
|
if (!head || !head.contains(target)) {
|
|
return;
|
|
}
|
|
|
|
// Let interactive elements within the title handle their own clicks.
|
|
const interactive = target.closest("a, button, input, label");
|
|
if (interactive && head.contains(interactive)) {
|
|
return;
|
|
}
|
|
|
|
// Selecting text within the title should not fold or unfold it.
|
|
if (window.getSelection()?.isCollapsed === false) {
|
|
return;
|
|
}
|
|
|
|
event.preventDefault();
|
|
this.handleToggle(event);
|
|
};
|
|
|
|
/**
|
|
* Fold or unfold this block. With Alt held the block and every toggle block
|
|
* nested inside it are toggled; with the Mod key held every toggle block in
|
|
* the document is toggled.
|
|
*/
|
|
private handleToggle = (event: MouseEvent) => {
|
|
const pos = this.getPos();
|
|
if (pos === undefined) {
|
|
return;
|
|
}
|
|
|
|
const isFolded = this.dom.classList.contains(
|
|
EditorStyleHelper.toggleBlockFolded
|
|
);
|
|
|
|
const type = isFolded ? Action.UNFOLD : Action.FOLD;
|
|
// Omitting the position applies the action to every toggle block.
|
|
const action = isModKey(event)
|
|
? { type }
|
|
: { type, at: pos, nested: event.altKey };
|
|
|
|
this.view.dispatch(
|
|
this.view.state.tr
|
|
.setMeta(toggleFoldPluginKey, action)
|
|
.setMeta(toggleEventPluginKey, action)
|
|
);
|
|
};
|
|
|
|
private broadcastFoldState(event: StorageEvent) {
|
|
if (
|
|
event.key !== toggleStorageKey(this.node.attrs.id) ||
|
|
!event.newValue ||
|
|
!event.oldValue
|
|
) {
|
|
return;
|
|
}
|
|
|
|
const newFoldState = JSON.parse(event.newValue);
|
|
const oldFoldState = JSON.parse(event.oldValue);
|
|
|
|
if (newFoldState.fold !== oldFoldState.fold) {
|
|
const pos = this.getPos();
|
|
if (pos === undefined) {
|
|
return;
|
|
}
|
|
|
|
this.view.dispatch(
|
|
this.view.state.tr
|
|
.setMeta(toggleFoldPluginKey, {
|
|
type: newFoldState.fold ? Action.FOLD : Action.UNFOLD,
|
|
at: pos,
|
|
})
|
|
.setMeta(toggleEventPluginKey, {
|
|
type: newFoldState.fold ? Action.FOLD : Action.UNFOLD,
|
|
at: pos,
|
|
})
|
|
);
|
|
}
|
|
}
|
|
|
|
private syncFoldState(decorations: readonly Decoration[]) {
|
|
const isFolded = decorations.some((d) => d.spec.fold === true);
|
|
this.dom.classList.toggle(EditorStyleHelper.toggleBlockFolded, isFolded);
|
|
}
|
|
|
|
update(node: ProsemirrorNode, decorations: readonly Decoration[]) {
|
|
if (node.type !== this.node.type) {
|
|
return false;
|
|
}
|
|
this.node = node;
|
|
this.syncFoldState(decorations);
|
|
return true;
|
|
}
|
|
|
|
destroy() {
|
|
this.button.removeEventListener("mousedown", this.handleToggleButtonClick);
|
|
this.contentDOM.removeEventListener("click", this.handleToggleHeadClick);
|
|
window.removeEventListener("storage", this.boundBroadcastFoldState);
|
|
}
|
|
}
|