UNPKG

@atlaskit/editor-plugin-show-diff

Version:

ShowDiff plugin for @atlaskit/editor-core

81 lines (80 loc) 3.28 kB
import { type NodeViewConstructor } from '@atlaskit/editor-common/lazy-node-view'; import type { Node as PMNode, Fragment } from '@atlaskit/editor-prosemirror/model'; import type { EditorView } from '@atlaskit/editor-prosemirror/view'; /** * Utilities for working with ProseMirror node views and DOM serialization within the * Show Diff editor plugin. * * This module centralizes: * - Access to the editor's `nodeViews` registry (when available on `EditorView`) * - Safe attempts to instantiate a node view for a given node, with a blocklist to * avoid node types that are known to be problematic in this context (e.g. tables) * - Schema-driven serialization of nodes and fragments to DOM via `DOMSerializer` * * The Show Diff decorations leverage this to either render nodes using their * corresponding node view implementation, or fall back to DOM serialization. */ /** * Narrowed `EditorView` that exposes the internal `nodeViews` registry. * Many editor instances provide this, but it's not part of the base type. */ export interface EditorViewWithNodeViews extends EditorView { nodeViews: Record<string, NodeViewConstructor>; } /** * Type guard to detect whether an `EditorView` exposes a `nodeViews` map. */ export declare function isEditorViewWithNodeViews(view: EditorView): view is EditorViewWithNodeViews; /** * Encapsulates DOM serialization and node view access/creation. * * Responsible for: * - Creating a `DOMSerializer` from the provided schema * - Reading `nodeViews` from an `EditorView` (if present) or using an explicit mapping * - Preventing node view creation for blocklisted node types */ export declare class NodeViewSerializer { private editorView?; private serializer?; private nodeViews?; private nodeViewBlocklist; constructor(params: { blocklist?: string[]; editorView?: EditorView; }); /** * Initializes or reinitializes the NodeViewSerializer with a new EditorView. * This allows the same serializer instance to be reused across different editor states. */ init(params: { editorView: EditorView; }): void; /** * Appends serialized child nodes to the given contentDOM element. */ private appendChildNodes; /** * Attempts to create a node view for the given node. * * Returns `null` when there is no `EditorView`, no constructor for the node type, * or the node type is blocklisted. Otherwise returns the constructed node view instance. */ tryCreateNodeView(targetNode: PMNode): Node | null; /** * Serializes a node to a DOM `Node` using the schema's `DOMSerializer`. */ serializeNode(node: PMNode): Node | null; /** * Serializes a fragment to a `DocumentFragment` using the schema's `DOMSerializer`. */ serializeFragment(fragment: Fragment): DocumentFragment | HTMLElement | null; /** * Returns a copy of the current node view blocklist. */ getNodeViewBlocklist(): Set<string>; /** * Returns a filtered copy of the node view blocklist, excluding specified node types. * @param excludeTypes - Array of node type names to exclude from the blocklist */ getFilteredNodeViewBlocklist(excludeTypes: string[]): Set<string>; }