@atlaskit/adf-schema
Version:
Shared package that contains the ADF-schema (json) and ProseMirror node/mark specs
75 lines (74 loc) • 2.81 kB
TypeScript
import { type Schema } from '@atlaskit/editor-prosemirror/model';
import type { Node as PMNode } from '@atlaskit/editor-prosemirror/model';
import { Step, StepResult } from '@atlaskit/editor-prosemirror/transform';
import type { Mappable } from '@atlaskit/editor-prosemirror/transform';
export type BatchAttrsStepData = {
position: number;
nodeType: string;
attrs: Record<string, unknown>;
};
/**
* 📢 Public API: Editor FE Platform
*
* Represents a step that applies a batch of attribute changes to nodes in a ProseMirror document.
*
* This step is particularly useful when you need to update the attributes of multiple nodes in a document
* in a single operation. For example, you might want to change the color of several panels or update metadata
* for various sections without needing to perform multiple separate operations.
*
* **Use Cases:**
* - **Efficiency**: Apply multiple attribute changes in a single step to reduce the number of operations.
* - **Atomicity**: Ensure that a group of attribute changes are applied together, maintaining document consistency.
* - **Consistency**: Use when changes are logically related, such as updating theme attributes for a document section.
*
* **When Not to Use:**
* - **Single Changes**: If you only need to change attributes on a single node, a more straightforward step might be suitable like `AttrsStep` from prosemirror.
* - **Complex Node Transformations**: This step is designed for attribute changes rather than structural changes.
* - **Performance Concerns**: While efficient for batch updates, unnecessary use for single updates may add overhead.
*
* @example
* ```typescript
* import { BatchAttrsStep } from '@atlaskit/adf-schema/steps';
*
* // Define the attribute changes
* const changes = [
* {
* position: 0, // Position of the first panel
* nodeType: 'panel',
* attrs: { panelType: 'error' }
* },
* {
* position: 7, // Position of the second panel
* nodeType: 'panel',
* attrs: { panelType: 'success' }
* }
* ];
*
* // Create the step and apply it to the document
* const step = new BatchAttrsStep(changes);
*
* const tr = editorState.tr;
*
* tr.step(step);
* ```
*
* @class
* @extends {Step}
*/
export declare class BatchAttrsStep extends Step {
data: Array<BatchAttrsStepData>;
private inverted;
constructor(data: Array<BatchAttrsStepData>, inverted?: boolean);
apply(doc: PMNode): StepResult;
invert(doc: PMNode): BatchAttrsStep;
map(mapping: Mappable): BatchAttrsStep;
toJSON(): {
stepType: string;
data: BatchAttrsStepData[];
inverted: boolean;
};
static fromJSON(_schema: Schema, json: {
data: Array<Record<string, unknown>>;
inverted?: boolean;
}): BatchAttrsStep;
}