# @embedpdf/plugin-view-manager — ./internal DocumentMovedEvent [InterfaceDeclaration] (from packages/plugin/view-manager/src/contract.ts): export interface DocumentMovedEvent{ readonly documentId: string; readonly fromPaneId: PaneId | null; readonly toPaneId: PaneId | null; } Pane [InterfaceDeclaration] (from packages/plugin/view-manager/src/model.ts): export interface Pane{ readonly id: PaneId; readonly documentIds: readonly string[]; readonly activeDocumentId: string | null; } PaneEvent [InterfaceDeclaration] (from packages/plugin/view-manager/src/contract.ts): export interface PaneEvent{ readonly paneId: PaneId; } PaneFocusChangedEvent [InterfaceDeclaration] (from packages/plugin/view-manager/src/contract.ts): export interface PaneFocusChangedEvent{ readonly paneId: PaneId | null; } PaneId [TypeAliasDeclaration] (from packages/plugin/view-manager/src/contract.ts): export type PaneId = string; PaneInfo [InterfaceDeclaration] (from packages/plugin/view-manager/src/contract.ts): export interface PaneInfo{ readonly id: PaneId; readonly documentIds: readonly string[]; readonly activeDocumentId: string | null; } ViewManagerAction [TypeAliasDeclaration] (from packages/plugin/view-manager/src/model.ts): export type ViewManagerAction = |{ type: 'CREATE_PANE'; pane: Pane} |{ type: 'REMOVE_PANE'; id: PaneId} |{ type: 'REORDER_PANES'; order: readonly PaneId[]} |{ type: 'SET_FOCUSED'; id: PaneId | null} |{ type: 'SET_ACTIVE_DOC'; paneId: PaneId; documentId: string | null} |{ type: 'ADD_DOC'; paneId: PaneId; documentId: string; index?: number} |{ type: 'REMOVE_DOC'; paneId: PaneId; documentId: string} |{ type: 'MOVE_DOC_WITHIN'; paneId: PaneId; documentId: string; toIndex: number} |{ type: 'MOVE_DOC_BETWEEN'; fromPaneId: PaneId; toPaneId: PaneId; documentId: string; toIndex?: number; } |{ type: 'RECONCILE'; open: readonly string[]; preferPaneId: PaneId | null} ; ViewManagerCapability [InterfaceDeclaration] (from packages/plugin/view-manager/src/contract.ts): export interface ViewManagerCapability{ listPanes(): readonly PaneInfo[]; getPane(id: PaneId): PaneInfo | null; getPaneOrder(): readonly PaneId[]; getFocusedPaneId(): PaneId | null; getPaneOfDocument(documentId: string): PaneId | null; createPane(options?:{ documentIds?: readonly string[]} ): PaneId; removePane(id: PaneId, options?:{ moveDocumentsTo?: PaneId} ): void; movePane(id: PaneId, toIndex: number): void; setFocusedPane(id: PaneId | null): void; splitPane(documentId: string, options?:{ from?: PaneId} ): PaneId; setActiveDocument(paneId: PaneId, documentId: string | null): void; addDocument(paneId: PaneId, documentId: string, index?: number): void; removeDocument(paneId: PaneId, documentId: string): void; moveDocumentWithin(paneId: PaneId, documentId: string, toIndex: number): void; moveDocumentBetween( fromPaneId: PaneId, toPaneId: PaneId, documentId: string, toIndex?: number, ): void; readonly onPaneCreated: EventHook; readonly onPaneRemoved: EventHook; readonly onFocusChanged: EventHook; readonly onDocumentMoved: EventHook; } ViewManagerHostCapability [TypeAliasDeclaration] (from packages/plugin/view-manager/src/host-contract.ts): export type ViewManagerHostCapability = ViewManagerCapability; ViewManagerState [InterfaceDeclaration] (from packages/plugin/view-manager/src/model.ts): export interface ViewManagerState{ readonly panes: Record; readonly order: readonly PaneId[]; readonly focusedPaneId: PaneId | null; readonly seq: number; } ViewManagerToken [VariableDeclaration] (from packages/plugin/view-manager/src/token.ts): ViewManagerToken = createCapabilityToken('view-manager', { hint: `add viewManagerPlugin() from '@embedpdf/plugin-view-manager' to your plugins list`, } ) createViewManagerController [FunctionDeclaration] (from packages/plugin/view-manager/src/controller.ts): export function createViewManagerController( rawCtx: PluginContext, ):{ api: ViewManagerHostCapability; connect(): void} { const report = (error: unknown) => globalThis.console?.error('[view-manager] listener failed:', error); const paneCreated = createEventHook(report); const paneRemoved = createEventHook(report); const focusChanged = createEventHook(report); const documentMoved = createEventHook(report); rawCtx.cleanup(() =>{ for (const hook of [paneCreated, paneRemoved, focusChanged, documentMoved]) hook.dispose(); } ); const paneOfDocument = (state: ViewManagerState, documentId: string): PaneId | null => state.order.find((id) => state.panes[id].documentIds.includes(documentId)) ?? null; const emitDiffs = (before: ViewManagerState, after: ViewManagerState): void =>{ if (before === after) return; for (const id of after.order) if (!before.panes[id]) paneCreated.emit({ paneId: id} ); for (const id of before.order) if (!after.panes[id]) paneRemoved.emit({ paneId: id} ); if (before.focusedPaneId !== after.focusedPaneId){ focusChanged.emit({ paneId: after.focusedPaneId} ); } if (before.panes !== after.panes){ const documents = new Set(); for (const pane of Object.values(before.panes)) pane.documentIds.forEach((d) => documents.add(d)); for (const pane of Object.values(after.panes)) pane.documentIds.forEach((d) => documents.add(d)); for (const documentId of documents){ const from = paneOfDocument(before, documentId); const to = paneOfDocument(after, documentId); if (from !== to) documentMoved.emit({ documentId, fromPaneId: from, toPaneId: to} ); } } } ; const ctx: PluginContext ={ ...rawCtx, dispatch: (action) =>{ const before = rawCtx.getState(); rawCtx.dispatch(action); emitDiffs(before, rawCtx.getState()); } , } ; const state = () => ctx.getState(); const infos = new WeakMap(); const infoOf = (pane: Pane): PaneInfo =>{ let info = infos.get(pane); if (!info){ info ={ id: pane.id, documentIds: pane.documentIds, activeDocumentId: pane.activeDocumentId, } ; infos.set(pane, info); } return info; } ; let listedFor:{ panes: ViewManagerState['panes']; order: readonly PaneId[]} | null = null; let listed: readonly PaneInfo[] = []; const listPanes = (): readonly PaneInfo[] =>{ const{ panes, order} = state(); if (listedFor && listedFor.panes === panes && listedFor.order === order) return listed; listedFor ={ panes, order} ; listed = order.map((id) => infoOf(panes[id])); return listed; } ; const createPane = (options?:{ documentIds?: readonly string[]} ): PaneId =>{ const id = `pane-${ state().seq + 1} `; ctx.dispatch({ type: 'CREATE_PANE', pane:{ id, documentIds: [], activeDocumentId: null} } ); for (const documentId of options?.documentIds ?? []){ ctx.dispatch({ type: 'ADD_DOC', paneId: id, documentId} ); } return id; } ; const api ={ listPanes, getPane: (id) =>{ const pane = state().panes[id]; return pane ? infoOf(pane) : null; } , getPaneOrder: () => state().order, getFocusedPaneId: () => state().focusedPaneId, getPaneOfDocument: (documentId) => paneOfDocument(state(), documentId), createPane, removePane: (id, options) =>{ const pane = state().panes[id]; if (!pane) return; const remaining = state().order.filter((other) => other !== id); const focused = state().focusedPaneId; const target = options?.moveDocumentsTo ?? (focused !== id ? focused : null) ?? remaining[remaining.length - 1] ?? null; if (target && target !== id && state().panes[target]){ for (const documentId of pane.documentIds){ ctx.dispatch({ type: 'MOVE_DOC_BETWEEN', fromPaneId: id, toPaneId: target, documentId} ); } } ctx.dispatch({ type: 'REMOVE_PANE', id} ); } , movePane: (id, toIndex) =>{ const current = state().order; const without = current.filter((x) => x !== id); if (without.length === current.length) return; const at = Math.max(0, Math.min(toIndex, without.length)); ctx.dispatch({ type: 'REORDER_PANES', order: [...without.slice(0, at), id, ...without.slice(at)], } ); } , setFocusedPane: (id) => ctx.dispatch({ type: 'SET_FOCUSED', id} ), splitPane: (documentId) => createPane({ documentIds: [documentId]} ), setActiveDocument: (paneId, documentId) => ctx.dispatch({ type: 'SET_ACTIVE_DOC', paneId, documentId} ), addDocument: (paneId, documentId, index) => ctx.dispatch({ type: 'ADD_DOC', paneId, documentId, index} ), removeDocument: (paneId, documentId) => ctx.dispatch({ type: 'REMOVE_DOC', paneId, documentId} ), moveDocumentWithin: (paneId, documentId, toIndex) => ctx.dispatch({ type: 'MOVE_DOC_WITHIN', paneId, documentId, toIndex} ), moveDocumentBetween: (fromPaneId, toPaneId, documentId, toIndex) => ctx.dispatch({ type: 'MOVE_DOC_BETWEEN', fromPaneId, toPaneId, documentId, toIndex} ), onPaneCreated: paneCreated.on, onPaneRemoved: paneRemoved.on, onFocusChanged: focusChanged.on, onDocumentMoved: documentMoved.on, } satisfies ViewManagerCapability; return{ api, connect(){ const documents = ctx.get(DocumentsToken); let last: string | null = null; const reconcile = () =>{ const order = documents.getOrder(); const key = order.join('|'); if (key === last) return; last = key; ctx.dispatch({ type: 'RECONCILE', open: order, preferPaneId: state().focusedPaneId} ); } ; reconcile(); ctx.cleanup(ctx.subscribe(reconcile)); } , } ; } initialViewManagerState [VariableDeclaration] (from packages/plugin/view-manager/src/model.ts): initialViewManagerState = (): ViewManagerState => ({ panes:{ } , order: [], focusedPaneId: null, seq: 0, } ) viewManagerReducer [FunctionDeclaration] (from packages/plugin/view-manager/src/model.ts): export function viewManagerReducer( state: ViewManagerState, action: ViewManagerAction, ): ViewManagerState{ switch (action.type){ case 'CREATE_PANE': return{ ...state, panes:{ ...state.panes, [action.pane.id]: action.pane} , order: [...state.order, action.pane.id], focusedPaneId: action.pane.id, seq: state.seq + 1, } ; case 'REMOVE_PANE':{ if (!state.panes[action.id]) return state; const panes ={ ...state.panes} ; delete panes[action.id]; const order = state.order.filter((id) => id !== action.id); const focusedPaneId = state.focusedPaneId === action.id ? (order[order.length - 1] ?? null) : state.focusedPaneId; return{ ...state, panes, order, focusedPaneId} ; } case 'REORDER_PANES': return{ ...state, order: action.order} ; case 'SET_FOCUSED': return{ ...state, focusedPaneId: action.id} ; case 'SET_ACTIVE_DOC':{ const pane = state.panes[action.paneId]; if (!pane) return state; if (action.documentId !== null && !pane.documentIds.includes(action.documentId)) return state; return setPane(state, { ...pane, activeDocumentId: action.documentId} ); } case 'ADD_DOC':{ const pane = state.panes[action.paneId]; if (!pane || pane.documentIds.includes(action.documentId)) return state; const holder = Object.values(state.panes).find((v) => v.documentIds.includes(action.documentId), ); const base = holder ? setPane(state, dropDocument(holder, action.documentId)) : state; const target = base.panes[action.paneId]; const documentIds = insertAt(target.documentIds, action.documentId, action.index); const activeDocumentId = target.activeDocumentId ?? action.documentId; return setPane(base, { ...target, documentIds, activeDocumentId} ); } case 'REMOVE_DOC':{ const pane = state.panes[action.paneId]; if (!pane) return state; return setPane(state, dropDocument(pane, action.documentId)); } case 'MOVE_DOC_WITHIN':{ const pane = state.panes[action.paneId]; if (!pane || !pane.documentIds.includes(action.documentId)) return state; const without = pane.documentIds.filter((id) => id !== action.documentId); const documentIds = insertAt(without, action.documentId, action.toIndex); return setPane(state, { ...pane, documentIds} ); } case 'MOVE_DOC_BETWEEN':{ const from = state.panes[action.fromPaneId]; const to = state.panes[action.toPaneId]; if (!from || !to || !from.documentIds.includes(action.documentId)) return state; if (action.fromPaneId === action.toPaneId){ return viewManagerReducer(state, { type: 'MOVE_DOC_WITHIN', paneId: action.toPaneId, documentId: action.documentId, toIndex: action.toIndex ?? to.documentIds.length, } ); } const nextFrom = dropDocument(from, action.documentId); const documentIds = insertAt(to.documentIds, action.documentId, action.toIndex); const nextTo: Pane ={ ...to, documentIds, activeDocumentId: action.documentId} ; return{ ...state, panes:{ ...state.panes, [nextFrom.id]: nextFrom, [nextTo.id]: nextTo} , focusedPaneId: action.toPaneId, } ; } case 'RECONCILE': return reconcile(state, action.open, action.preferPaneId); default: return state; } }