import type { Node as ProsemirrorNode } from "prosemirror-model"; import type { EditorView, NodeView, DecorationSource } from "prosemirror-view"; import type { Decoration } from "prosemirror-view"; import { EditorStyleHelper } from "../styles/EditorStyleHelper"; import { Action, toggleEventPluginKey, toggleFoldPluginKey, } 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 editorProps: Record; private boundBroadcastFoldState: (event: StorageEvent) => void; constructor( node: ProsemirrorNode, view: EditorView, getPos: () => number | undefined, decorations: readonly Decoration[], _innerDecorations: DecorationSource, editorProps: Record ) { this.node = node; this.view = view; this.getPos = getPos; this.editorProps = editorProps; // 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.handleToggle); this.contentDOM = document.createElement("div"); this.contentDOM.className = EditorStyleHelper.toggleBlockContent; 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 handleToggle = (event: MouseEvent) => { event.preventDefault(); if (event.button !== 0) { return; } const pos = this.getPos(); if (pos === undefined) { return; } const isFolded = this.dom.classList.contains( EditorStyleHelper.toggleBlockFolded ); this.view.dispatch( this.view.state.tr .setMeta(toggleFoldPluginKey, { type: isFolded ? Action.UNFOLD : Action.FOLD, at: pos, }) .setMeta(toggleEventPluginKey, { type: isFolded ? Action.UNFOLD : Action.FOLD, at: pos, }) ); }; private broadcastFoldState(event: StorageEvent) { const key = `${this.node.attrs.id}:${this.editorProps.userId}`; if (event.key !== key || !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.handleToggle); window.removeEventListener("storage", this.boundBroadcastFoldState); } }