json-joy
Version:
Collection of libraries for building collaborative editing apps.
141 lines (140 loc) • 5.9 kB
TypeScript
import { Point } from '../rga/Point';
import { Range } from '../rga/Range';
import { SliceStacking } from './constants';
import { NestedType } from './NestedType';
import { type Model, type NodeApi, ObjApi } from '../../../json-crdt/model';
import type { ObjNode, VecNode } from '../../../json-crdt/nodes';
import type { ITimestampStruct } from '../../../json-crdt-patch/clock';
import type { ArrChunk, ArrNode, JsonNode } from '../../../json-crdt/nodes';
import type { SliceType, SliceUpdateParams, SliceTypeSteps } from './types';
import type { Stateful } from '../types';
import type { Printable } from 'tree-dump/lib/types';
import type { AbstractRga } from '../../../json-crdt/nodes/rga';
import type { Peritext } from '../Peritext';
import type { Slices } from './Slices';
/**
* A slice is stored in a {@link Model} as a "vec" node. It is used for
* rich-text formatting annotations and block splits.
*
* Slices represent Peritext's rich-text formatting/splits. The "slice"
* concept captures both: (1) range annotations; as well as, (2) *markers*,
* which are a single-point annotations. The markers are used as block splits,
* e.g. paragraph, heading, blockquote, etc. In markers, the start and end
* positions of the range are normally the same, but could also wrap around
* a single RGA chunk.
*/
export declare class Slice<T = string> extends Range<T> implements Stateful, Printable {
/** The `Model` where the slice is stored. */
protected readonly model: Model;
/** The Peritext context. */
protected readonly txt: Peritext<T>;
/** The "arr" node where the slice is stored. */
protected readonly arr: ArrNode;
/** The `arr` chunk of `arr` where the slice is stored. */
protected readonly chunk: ArrChunk;
/** The `vec` node which stores the serialized contents of this slice. */
readonly tuple: VecNode;
start: Point<T>;
end: Point<T>;
static deserialize<T>(model: Model, txt: Peritext<T>, arr: ArrNode, chunk: ArrChunk, tuple: VecNode): Slice<T>;
/** @todo Use API node here. */
protected readonly rga: AbstractRga<T>;
/**
* ID of the slice. ID is used for layer sorting.
*/
readonly id: ITimestampStruct;
/**
* The low-level stacking behavior of the slice. Specifies whether the
* slice is a split, i.e. a "marker" for a block split, in which case it
* represents a single place in the text where text is split into blocks.
* Otherwise, specifies the low-level behavior or the rich-text formatting
* of the slice.
*/
stacking: SliceStacking;
constructor(
/** The `Model` where the slice is stored. */
model: Model,
/** The Peritext context. */
txt: Peritext<T>,
/** The "arr" node where the slice is stored. */
arr: ArrNode,
/** The `arr` chunk of `arr` where the slice is stored. */
chunk: ArrChunk,
/** The `vec` node which stores the serialized contents of this slice. */
tuple: VecNode, stacking: SliceStacking, start: Point<T>, end: Point<T>);
/**
* Represents a block split in the text, i.e. it is a *marker* that shows
* where a block was split. Markers also insert one "\n" new line character.
* Both marker ends are attached to the "before" anchor fo the "\n" new line
* character, i.e. it is *collapsed* to the "before" anchor.
*/
isMarker(): boolean;
tupleApi(): import("../../../json-crdt/model").VecApi<VecNode<any>>;
pos(): number;
/**
* Returns the {@link Range} which exactly contains the block boundary of this
* marker.
*/
boundary(): Range<T>;
set(start: Point<T>, end?: Point<T>): void;
/**
* Expand range left and right to contain all invisible space: (1) tombstones,
* (2) anchors of non-deleted adjacent chunks.
*/
expand(): void;
update(params: SliceUpdateParams<T>): void;
isSaved(): boolean;
getStore(): Slices<T> | undefined;
/**
* Delete this slice from its backing store.
*/
del(): void;
/**
* Whether the slice is deleted.
*/
isDel(): boolean;
typeNode(): JsonNode<unknown> | undefined;
typeApi(): NodeApi<JsonNode<unknown>> | undefined;
nestedType(): NestedType<T>;
/**
* The high-level behavior identifier of the slice. Specifies the
* user-defined type of the slice, e.g. paragraph, heading, blockquote, etc.
*
* Usually the type is a number or string primitive, in which case it is
* referred to as *tag*.
*
* The type is a list only for nested blocks, e.g. `['ul', 'li']`, in which
* case the type is a list of tags. The last tag in the list is the
* "leaf" tag, which is the type of the leaf block element.
*/
type(): SliceType;
typeSteps(): SliceTypeSteps;
/**
* High-level user-defined metadata of the slice, which accompanies the slice
* type.
*/
data(): unknown | undefined;
dataNode(): NodeApi<JsonNode<unknown>> | undefined;
dataAsObj(): ObjApi<ObjNode>;
/**
* Overwrites the data of this slice with the given data.
*
* @param data Data to set for this slice. The data can be any JSON value, but
* it is recommended to use an object.
*/
setData(data: unknown): void;
/**
* Merges object data into the slice's data using JSON CRDT diffing.
*
* @param data Data to merge into the slice. If the data is an object, it will be
* merged with the existing data of the slice using JSON CRDT diffing.
*/
mergeData(data: unknown): void;
/** ------------------------------------------------------ {@link Stateful} */
hash: number;
refresh(): number;
/** ----------------------------------------------------- {@link Printable} */
toStringName(): string;
protected toStringHeaderName(): string;
toStringHeader(tab?: string): string;
}