/** * @public * Interface for Operational Transformation on Plain Text */ export declare interface IPlainTextOperation { /** * Checks if two Text Operations are equivalent and equal. * @param other - Another Text Operation instance. */ equals(other: IPlainTextOperation): boolean; /** * Skip over a given number of characters. * @param n - Number of Characters to Skip. * @param attributes - Additional Attributes for Retain. */ retain(n: number, attributes?: TTextOperationAttributes | null): IPlainTextOperation; /** * Insert a string at the current position. * @param str - String to be Inserted. * @param attributes - Additional Attributes for Insert. */ insert(str: string, attributes?: TTextOperationAttributes | null): IPlainTextOperation; /** * Delete a string at the current position. * @param n - Number of Characters or String to be Deleted. */ delete(n: number | string): IPlainTextOperation; /** * Tests whether this operation has no effect. */ isNoop(): boolean; /** * Returns Shallow copy of current operation. */ clone(): IPlainTextOperation; /** * Returns an Iterator with values from Individual Operation set. */ entries(): IterableIterator<[number, ITextOperation]>; /** * Apply an operation to a string, returning a new string. Throws an error if * there's a mismatch between the input string and the operation. * @param prevContent - Text Content of Editor before applying the operation * @param prevAttributes - Text Operation attributes before applying * @param attributes - Text Operation attributes after applying (propagated) */ apply(prevContent: string, prevAttributes?: TTextOperationAttributes[], attributes?: TTextOperationAttributes[]): string; /** * Computes the inverse of an operation. The inverse of an operation is the * operation that reverts the effects of the operation, e.g. when you have an * operation 'insert("hello "); skip(6);' then the inverse is 'delete("hello "); * skip(6);'. The inverse should be used for implementing undo. * @param content - Text Content of Editor before applying the operation */ invert(content: string): IPlainTextOperation; /** * When you use ctrl-z to undo your latest changes, you expect the program not * to undo every single keystroke but to undo your last sentence you wrote at * a stretch or the deletion you did by holding the backspace key down. This * This can be implemented by composing operations on the undo stack. This * method can help decide whether two operations should be composed. It * returns true if the operations are consecutive insert operations or both * operations delete text at the same position. You may want to include other * factors like the time since the last change in your decision. * @param other - Other Operation. */ shouldBeComposedWith(other: IPlainTextOperation): boolean; /** * Decides whether two operations should be composed with each other * if they were inverted, that is * `shouldBeComposedWith(A, B) = shouldBeComposedWithInverted(B', A')`. * @param other - Other Operation */ shouldBeComposedWithInverted(other: IPlainTextOperation): boolean; /** * Compose merges two consecutive operations into one operation, that * preserves the changes of both. Or, in other words, for each input string S * and a pair of consecutive operations A and B, * apply(apply(S, A), B) = apply(S, compose(A, B)) must hold. * @param operation - Consecutive Operation to be composed with. */ compose(operation: IPlainTextOperation): IPlainTextOperation; /** * Transform of `A` takes another concurrent operation `B` and * produces two operations `A'` and `B'` (in an array) such that * `apply(apply(S, A), B') = apply(apply(S, B), A')`. * @param operation - Concurrent Operation to be composed with. */ transform(operation: IPlainTextOperation): [IPlainTextOperation, IPlainTextOperation]; /** * Tests whether next operation can be chained with current one, i.e., * checks if the `targetLength` of current operation is equal with the `baseLength` * of next operation. * @param other - Subsequent Operation */ canMergeWith(other: IPlainTextOperation): boolean; /** * Checks if the `baseLength` of current operation is equal with the given parameter. * @param length - Length to compare with. */ isEqualBaseLength(length: number): boolean; /** * Checks if the `targetLength` of current operation is equal with the given parameter. * @param length - Length to compare with. */ isEqualTargetLength(length: number): boolean; /** * Returns String representation of a Text Operation */ toString(): string; /** * Returns JSON representation of a Text Operation */ toJSON(): TPlainTextOperation; /** * Returns Serializable set of Primitive value of a Text Operation */ valueOf(): TPlainTextOperation; } /** * @public * Text Operation are essentially lists of ops. There are three types of operations: * * **Retain ops:** Advance the cursor position by a given number of characters. * Represented by positive ints. * * **Insert ops:** Insert a given string at the current cursor position. * Represented by strings. * * **Delete ops:** Delete the next n characters. Represented by negative ints. */ export declare interface ITextOperation { /** * Tests if it's an Insert Operation. */ isInsert(): boolean; /** * Tests if it's a Delete Operation. */ isDelete(): boolean; /** * Tests if it's a Retain Operation. */ isRetain(): boolean; /** * Tests if two Individual Text Operation equal or not. * @param other - Another Text Operation. */ equals(other: ITextOperation): boolean; /** * Tests if two Individual Text Operation have same Attributes or not. * @param otherAttributes - Another Text Operation Attributes. */ attributesEqual(otherAttributes: TTextOperationAttributes | null | void): boolean; /** * Tests if two Individual Text Operation have same Character Count or not. * @param characterCount - Another Text Operation Character Count. */ characterCountEqual(characterCount: number): boolean; /** * Tests if two Individual Text Operation have same Text Content or not. * @param text - Another Text Operation Text Content. */ textContentEqual(text: string): boolean; /** * Adds Character Count to current operation. * @param characterCount - Character Count to add. */ addCharacterCount(characterCount: number): void; /** * Adds Text Content to current operation. * @param text - Text Content to add. */ addTextContent(text: string): void; /** * Sets Text Content to current operation. * @param text - Text Content to add. */ setTextContent(text: string): void; /** * Returns Character Count of current operation. * @param characterCount - Character Count to add. */ characterCount(): number; /** * Returns Text Content of current operation. */ textContent(): string; /** * Tests if this Individual Text Operation has additional Attributes. */ hasEmptyAttributes(): boolean; /** * Returns additional Metadata of a Text Operation. */ getAttributes(): TTextOperationAttributes | null; /** * Returns String representation of an Individual Text Operation */ toString(): string; /** * Returns Primitive value of an Individual Text Operation */ valueOf(): string | number; } /** * @public * Implementation Class of Operational Transformation of Plain Text */ export declare class PlainTextOperation implements IPlainTextOperation { protected readonly _ops: ITextOperation[]; protected _baseLength: number; protected _targetLength: number; constructor(); equals(other: PlainTextOperation): boolean; /** * Returns nth last op from the ops array. * @param n - Reverse Index of the item. */ protected _last(n?: number): ITextOperation | null; retain(n: number, attributes?: TTextOperationAttributes | null): IPlainTextOperation; insert(str: string, attributes?: TTextOperationAttributes | null): IPlainTextOperation; delete(n: string | number): IPlainTextOperation; isNoop(): boolean; clone(): IPlainTextOperation; entries(): IterableIterator<[number, ITextOperation]>; apply(prevContent: string, prevAttributes?: TTextOperationAttributes[], attributes?: TTextOperationAttributes[]): string; invert(content: string): IPlainTextOperation; /** * Returns first non-Retain Operation. * @param operation - A Plain Text Operation */ protected _getSimpleOp(operation: PlainTextOperation): ITextOperation | null; /** * Returns the index of the text content where first insert/delete occured. * @param operation - A Plain Text Operation */ protected _getStartIndex(operation: PlainTextOperation): number; shouldBeComposedWith(other: PlainTextOperation): boolean; shouldBeComposedWithInverted(other: PlainTextOperation): boolean; /** * Returns next Text Operation from Iterator * @param opIterator - Iterator Protocol Object containing Text Operation */ protected _nextTextOp(opIterator: IterableIterator<[number, ITextOperation]>): ITextOperation | null; /** * Merges additional attributes from two Plain Text Operation. * @param first - Additional Attributes from First Operation. * @param second - Additional Attributes from Second Operation. * @param firstOpIsInsert - Is first operation an Insert Operation. */ protected _composeAttributes(first: TTextOperationAttributes | null, second: TTextOperationAttributes | null, firstOpIsInsert: boolean): TTextOperationAttributes; compose(otherOperation: IPlainTextOperation): IPlainTextOperation; /** * Transforms additional attributes from two Plain Text Operation. * @param attributes1 - Additional Attributes from First Operation. * @param attributes2 - Additional Attributes from Second Operation. */ protected _transformAttributes(attributes1: TTextOperationAttributes | null, attributes2: TTextOperationAttributes | null): [TTextOperationAttributes, TTextOperationAttributes]; transform(operation: IPlainTextOperation): [IPlainTextOperation, IPlainTextOperation]; isEqualBaseLength(length: number): boolean; isEqualTargetLength(length: number): boolean; canMergeWith(other: IPlainTextOperation): boolean; toString(): string; valueOf(): TPlainTextOperation; toJSON(): TPlainTextOperation; /** * Converts JSON representation of TextOperation into TextOperation object. */ static fromJSON(ops: TPlainTextOperation): PlainTextOperation; } /** * @public * JSON Representation of a Text Operation */ export declare type TPlainTextOperation = TPlainTextOperationValue[]; /** * @public * Value Representation of Text Operation */ export declare type TPlainTextOperationValue = string | number | TTextOperationAttributes; /** * @public * Additional Metadata Set for Text Operation */ export declare type TTextOperationAttributes = Record; /** * Copyright © 2021 Progyan Bhattacharya * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. * * See LICENSE file in the root directory for more details. */ /** * @public * Additional Metadata Data Type for Text Operation */ export declare type TTextOperationAttributeValue = number | boolean | string | symbol; export { }