/** * @license Copyright (c) 2003-2026, CKSource Holding sp. z o.o. All rights reserved. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-licensing-options */ /** * @module ai/toolkit/documentdiffer */ import { ContextPlugin, Editor, type PluginDependenciesOf } from "@ckeditor/ckeditor5-core"; import { ModelPosition, type ModelWriter, type ModelDocumentFragment, type ModelElement, type Operation } from "@ckeditor/ckeditor5-engine"; import { DocumentDifferUtils } from "./documentdifferutils.js"; import { type ModelElementCategory } from "./elementcategory.js"; export { DATA_ID_MODEL_ATTRIBUTE, DATA_ID_VIEW_ATTRIBUTE } from "./elementcategory.js"; /** * The `DocumentDiffer` plugin provides utilities to compare two documents or documents fragments, and provide set of operations * which need to be executed to transform one document into the other. */ export declare class DocumentDiffer extends ContextPlugin { /** * @inheritDoc */ static get pluginName(): "DocumentDiffer"; /** * @inheritDoc */ static override get isOfficialPlugin(): true; /** * @inheritDoc */ static override get isPremiumPlugin(): true; static get requires(): PluginDependenciesOf<[DocumentDifferUtils]>; init(): Promise; get editor(): Editor; /** * Compares two HTML strings and returns the operations that transform `dataBefore` into `dataAfter`. * * @param dataBefore The HTML to transform from. * @param dataAfter The HTML to transform into. * @param options Diffing options. * @returns Operations that need to be applied to transform `dataBefore` into `dataAfter`. */ getDiff(dataBefore: string, dataAfter: string, options?: DocumentDifferOptions): Array; /** * Creates the diff between two model document fragments. * * The same comparison {@link #getDiff} makes, for content that is already in the model. Serializing a fragment to * reach that method is not a detour but a loss: an element whose downcast depends on its parent - a caption, a table * row - has no serialized form on its own, and what the data processor makes of the result is not always what went in. * * The fragments are read and not kept: every node the operations carry is a copy. * * @param before The content to transform from. * @param after The content to transform into. * @param options Diffing options. * @returns Operations that need to be applied to transform `before` into `after`. */ getDiffForFragments(before: ModelDocumentFragment, after: ModelDocumentFragment, options?: DocumentDifferOptions): Array; /** * Returns `true` if given `operation` is an operation that marks a content to be removed. This kind of operations can be * returned by {@link #getDiff} and {@link #getDiffForFragments} if the `markDeletions` option is enabled. * * @param operation */ isMarkedRemoveOperation(operation: Operation): boolean; /** * Applies the given operations (for example, the result of comparing two documents) to the model. * * Beyond applying the operations, this method reproduces the marker bookkeeping that the editing pipeline normally * performs, so that existing markers behave the same as during regular editing: * * * markers affected by a move or merge are anchored, so they can be restored on undo; * * marker operations have their `oldRange` reconciled with the live marker, as operations coming from a comparison * have it set to `null`. * * Must be called inside a model change block; the operations are added to the given `writer`'s batch. * * @param writer Writer of the change block the operations are applied within. * @param operations Operations to apply, in order. */ applyOperations(writer: ModelWriter, operations: Array): void; } export interface DocumentDifferOptions { anchor?: string | ModelPosition; markDeletions: boolean; /** * The editor whose model/schema should be used for parsing and for resolving a string `anchor` (e.g. multi-root root names). */ targetEditor?: Editor; /** * When set, `data-id`s in the compared data are ignored and the elements of both documents are paired by content similarity. * Use it when compared documents do not have `data-id`s set. */ matchElements?: boolean; } export interface InsertOperationData { type: "insert"; path: Path; tokens: Array; } export interface RemoveOperationData { type: "remove"; path: Path; howMany: number; } export interface SplitOperationData { type: "split"; path: Path; } export interface MergeOperationData { type: "merge"; path: Path; } export interface RenameOperationData { type: "rename"; path: Path; oldName: string; newName: string; } export interface AttributeOperationData { type: "attribute"; path: Path; howMany: number; key: string; oldValue: string; newValue: string; } export interface MarkerOperationData { type: "marker"; name: string; range: null | { start: Path; end: Path; }; } export interface UnwrapOperationData { type: "unwrap"; path: Path; howMany: number; } export interface WrapOperationData { type: "wrap"; token: ModelElementToken; range: { start: Path; end: Path; }; } export interface MarkedRemoveData { type: "markedRemove"; range: { start: Path; end: Path; }; } export type OperationData = InsertOperationData | RemoveOperationData | SplitOperationData | MergeOperationData | RenameOperationData | AttributeOperationData | MarkerOperationData | UnwrapOperationData | WrapOperationData | MarkedRemoveData; export type ModelToken = ModelElementToken | ModelTextToken; export interface ModelElementToken { type: "element"; element: ModelElement; elementId: string; /** * Elements category. * * Describes how the element behaves and how it can change. It is used when comparing lists of model tokens to create a better diff. * See {@link module:ai/toolkit/elementcategory~ModelElementCategory}. */ category: ModelElementCategory; isStart: boolean; /** * The chain of IDs of the limit-like elements the element stands in. */ limitId: string; } export interface ModelTextToken { type: "text"; data: string; parent: ModelElement | ModelDocumentFragment; offset: number; /** * The chain of IDs of the limit-like elements the text stands in. */ limitId: string; } export type Path = Array;