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 = ''; 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); } }