json-joy
Version:
Collection of libraries for building collaborative editing apps.
141 lines (140 loc) • 5.95 kB
TypeScript
import { SliceBehavior, type SliceTypeCon } from '../slice/constants';
import type { PeritextMlElement } from '../block/types';
import type { NodeBuilder } from '../../../json-crdt-patch';
import type { JsonMlElement } from 'very-small-parser/lib/html/json-ml/types';
import type { FromHtmlConverter, ToHtmlConverter } from './types';
import type { JsonNodeView } from '../../../json-crdt/nodes';
import type { SchemaToJsonNode } from '../../../json-crdt/schema/types';
export type TagType = SliceTypeCon | number | string;
export declare class SliceRegistryEntry<Behavior extends SliceBehavior = SliceBehavior, Tag extends TagType = TagType, Schema extends NodeBuilder = NodeBuilder> {
/**
* Specifies whether the slice is an inline or block element. And if it is
* an inline element, whether multiple instances of the same tag are allowed
* to be applied to a range of tex - "Many", or only one instance - "One".
*/
readonly behavior: Behavior;
/**
* The tag name of this slice. The tag is one step in the type path of the
* slice. For example, below is a type path composed of three steps:
*
* ```js
* ['ul', 'li', 'p']
* ```
*
* Tag types are normally numbers of type {@link SliceTypeCon}, however,
* they can also be any arbitrary strings or numbers.
*/
readonly tag: Tag;
/**
* Default expected schema of the slice data.
*/
readonly schema: Schema;
/**
* This property is relevant only for block split markers. It specifies
* whether the block split marker is a container for other block elements.
*
* For example, a `blockquote` is a container for `paragraph` elements,
* however, a `paragraph` is not a container (it can only contain inline
* elements).
*
* If the marker slice is of the container sort, they tag can appear in the
* path steps of the type:
*
* ```
*
* ```
*/
readonly container: boolean;
/**
* Converts a node of this type to HTML representation: returns the HTML tag
* and attributes. The method receives {@link PeritextMlElement} as an
* argument, which is a tuple of internal HTML-like representation of the
* node.
*/
readonly toHtml: ToHtmlConverter<PeritextMlElement<Tag, JsonNodeView<SchemaToJsonNode<Schema>>, Behavior extends SliceBehavior.Marker ? false : true>> | undefined;
/**
* Specifies a mapping of converters from HTML {@link JsonMlElement} to
* {@link PeritextMlElement}. This way a slice type can specify multiple
* HTML tags that are converted to the same slice type.
*
* For example, both, `<b>` and `<strong>` tags can be converted to the
* {@link SliceTypeCon.b} slice type.
*/
readonly fromHtml?: {
[htmlTag: string]: FromHtmlConverter<PeritextMlElement<Tag, JsonNodeView<SchemaToJsonNode<Schema>>, Behavior extends SliceBehavior.Marker ? false : true>>;
} | undefined;
isInline(): boolean;
constructor(
/**
* Specifies whether the slice is an inline or block element. And if it is
* an inline element, whether multiple instances of the same tag are allowed
* to be applied to a range of tex - "Many", or only one instance - "One".
*/
behavior: Behavior,
/**
* The tag name of this slice. The tag is one step in the type path of the
* slice. For example, below is a type path composed of three steps:
*
* ```js
* ['ul', 'li', 'p']
* ```
*
* Tag types are normally numbers of type {@link SliceTypeCon}, however,
* they can also be any arbitrary strings or numbers.
*/
tag: Tag,
/**
* Default expected schema of the slice data.
*/
schema: Schema,
/**
* This property is relevant only for block split markers. It specifies
* whether the block split marker is a container for other block elements.
*
* For example, a `blockquote` is a container for `paragraph` elements,
* however, a `paragraph` is not a container (it can only contain inline
* elements).
*
* If the marker slice is of the container sort, they tag can appear in the
* path steps of the type:
*
* ```
*
* ```
*/
container?: boolean,
/**
* Converts a node of this type to HTML representation: returns the HTML tag
* and attributes. The method receives {@link PeritextMlElement} as an
* argument, which is a tuple of internal HTML-like representation of the
* node.
*/
toHtml?: ToHtmlConverter<PeritextMlElement<Tag, JsonNodeView<SchemaToJsonNode<Schema>>, Behavior extends SliceBehavior.Marker ? false : true>> | undefined,
/**
* Specifies a mapping of converters from HTML {@link JsonMlElement} to
* {@link PeritextMlElement}. This way a slice type can specify multiple
* HTML tags that are converted to the same slice type.
*
* For example, both, `<b>` and `<strong>` tags can be converted to the
* {@link SliceTypeCon.b} slice type.
*/
fromHtml?: {
[htmlTag: string]: FromHtmlConverter<PeritextMlElement<Tag, JsonNodeView<SchemaToJsonNode<Schema>>, Behavior extends SliceBehavior.Marker ? false : true>>;
} | undefined);
}
/**
* Slice registry contains a record of possible inline an block formatting
* annotations. Each entry in the registry is a {@link SliceRegistryEntry} that
* specifies the behavior, tag, and other properties of the slice.
*
* @todo Consider moving the registry under the `/transfer` directory. Or maybe
* `/slices` directory.
*/
export declare class SliceRegistry {
private map;
private _fromHtml;
add(entry: SliceRegistryEntry<any, any, any>): void;
isContainer(tag: TagType): boolean;
toHtml(el: PeritextMlElement): ReturnType<ToHtmlConverter<any>> | undefined;
fromHtml(el: JsonMlElement): PeritextMlElement | undefined;
}