UNPKG

lib0

Version:

isomorphic utility functions

84 lines • 4.77 kB
/** * Makes the projected (side-B) delta conform to `$schema`, **recursively**: drops attributes, attribute * values, child nodes, and text the schema does not recognize, and descends into kept delta-valued * attributes and kept child nodes with a nested `conform` (a fresh instance per kept child/attr, kept in * step with the content — modelled on `children` + `inline`). A child is kept iff its node-name matches * one of the schema's child `$Delta`s; a non-delta child iff it validates against `$children`; text iff * the schema allows text. Everything else is dropped, so the output is guaranteed to satisfy `$schema`. * * Content that needs no per-position handling (text, scalars, and nodes matched by a `$deltaAny`/`$any` * schema) is coalesced into pass-through runs, so a `$deltaAny` schema is a true zero-overhead identity * and an incremental edit costs O(change), not O(document). * * `applyA` (A → B) does the full recursive filtering. `applyB` (B → A) maps a change authored on the * conformed view back onto A through the same layout: B positions are remapped across the content A has * but B does not (see {@link ConformTransformer#applyB} for the rules), and the change is first validated * against `$schema` — any op that would break conformance throws, before any state is touched. * Marks are best-effort — marks on kept children/attrs ride through the position map; marks anchored to * dropped content are dropped. * * @template {delta.DeltaConf} SchemaConf * @template {delta.DeltaConf} [IN=any] * @extends {Template<IN, SchemaConf>} */ export class Conform<SchemaConf extends delta.DeltaConf, IN extends delta.DeltaConf = any> extends Template<IN, SchemaConf> { /** * @param {import('../../schema.js').Schema<delta.Delta<IN>>} $d input delta schema * @param {import('../../schema.js').Schema<delta.Delta<SchemaConf>>} $schema the schema the output must match */ constructor($d: import("../../schema.js").Schema<delta.Delta<IN>>, $schema: import("../../schema.js").Schema<delta.Delta<SchemaConf>>); $schema: s.Schema<delta.Delta<SchemaConf>>; config: Config; } /** * Stateful, recursive transformer produced by {@link Conform}. See {@link Conform} for semantics. * * The A↔B layout lives in {@link ConformTransformer#cmap} (the *child* map), a delta reused as a * coalesced positional map (like `children`'s `childTs`): a `retain(n)` run = pass-through positions, an * `insert([t])` = a child routed through nested conform `t`, a `delete(n)` run = dropped positions * (width 0 on B). Both directions walk it with a {@link Cursor} and edit it in place, carrying the nested * transformers by reference so their state survives; a change with only retains/modifies is routed * without touching the map. * * @extends {Transformer<any,any>} */ export class ConformTransformer extends Transformer<any, any> { /** * @param {import('../../schema.js').Schema<delta.Delta<any>>} $in * @param {import('../../schema.js').Schema<delta.Delta<any>>} $out * @param {Config} config the schema-derived read-only config (computed once, see {@link configOf}) */ constructor($in: import("../../schema.js").Schema<delta.Delta<any>>, $out: import("../../schema.js").Schema<delta.Delta<any>>, config: Config); config: Config; /** @type {delta.DeltaBuilderAny} the coalesced child layout map */ cmap: delta.DeltaBuilderAny; /** @type {Map<string|number, ConformTransformer>} nested conforms for delta-valued attributes */ transformAttrs: Map<string | number, ConformTransformer>; } export function conform<IN extends delta.DeltaConf, SchemaConf extends delta.DeltaConf>($d: import("../../schema.js").Schema<delta.Delta<IN>>, $schema: import("../../schema.js").Schema<delta.Delta<SchemaConf>>): Conform<SchemaConf, IN>; /** * The expanded matcher for a children / attr-value schema. */ export type Allowed = { byName: Map<string, delta.$Delta<any>>; wild: delta.$Delta<any> | null; passAny: boolean; $scalar: import("../../schema.js").Schema<any>; }; /** * The read-only, schema-derived configuration of a conform transformer. Computed once per schema (see * {@link configOf}) and shared by every transformer instance built from that schema — many instances are * created for the children of one schema, so this avoids re-expanding the schema each time. */ export type Config = { passthrough: boolean; hasText: boolean; childAllowed: Allowed; attrsLoose: boolean; attrAllowed: Map<string | number, Allowed>; }; import * as delta from '../delta.js'; import { Template } from './core.js'; import * as s from '../../schema.js'; import { Transformer } from './core.js'; //# sourceMappingURL=conform.d.ts.map