lib0 1.0.0-rc.29 → 1.0.0-rc.31

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -85,6 +85,10 @@ export const $deltaMapChangeJson: s.Schema<DeltaAttrOpJSON>;
85
85
  * fingerprint read (and any `diff` / equality check that relies on it) is
86
86
  * wrong. Fields covered: insert, delete, retain, format, attribution,
87
87
  * value, key.
88
+ * - **A `clone` copies `_fingerprint` when the copy is content-identical:** a full range and the
89
+ * same key. Freezing a nested value with `done()` is fingerprint-neutral (trailing plain retains
90
+ * are not fingerprinted), so nothing else can differ. The root delta returned by
91
+ * `clone`/`slice`/`cloneDeep` never inherits a fingerprint.
88
92
  *
89
93
  * @internal not part of the consumer API — a content op; build deltas via {@link create}/{@link DeltaBuilder}.
90
94
  */
@@ -433,9 +437,12 @@ export class SetAttrOp<V extends unknown = any, K extends string | number = any>
433
437
  * Full (frozen) clone. A `move` apply reuses the source op instead of cloning, so there is no
434
438
  * shared-mutable variant — see {@link DeltaBuilder#apply}.
435
439
  *
436
- * @return {SetAttrOp<V,K>}
440
+ * @template {string|number} [K2=K]
441
+ * @param {K2} [key] retarget the clone to another attribute key (a transformer projecting or
442
+ * renaming an attribute); defaults to this op's key.
443
+ * @return {SetAttrOp<V,K2>}
437
444
  */
438
- clone(): SetAttrOp<V, K>;
445
+ clone<K2 extends string | number = K>(key?: K2): SetAttrOp<V, K2>;
439
446
  $type: s.Schema<SetAttrOp<any, any>>;
440
447
  /**
441
448
  * @param {SetAttrOp<V>} other
@@ -475,9 +482,11 @@ export class DeleteAttrOp<V = any, K extends string | number = string | number>
475
482
  /**
476
483
  * Full (frozen) clone; a `move` apply reuses the source op instead — see {@link DeltaBuilder#apply}.
477
484
  *
478
- * @return {DeleteAttrOp<V,K>}
485
+ * @template {string|number} [K2=K]
486
+ * @param {K2} [key] retarget the clone to another attribute key; defaults to this op's key.
487
+ * @return {DeleteAttrOp<V,K2>}
479
488
  */
480
- clone(): DeleteAttrOp<V, K>;
489
+ clone<K2 extends string | number = K>(key?: K2): DeleteAttrOp<V, K2>;
481
490
  $type: s.Schema<DeleteAttrOp<any, string | number>>;
482
491
  /**
483
492
  * @param {DeleteAttrOp<V>} other
@@ -531,9 +540,11 @@ export class ModifyAttrOp<Modifier extends DeltaAny = DeltaAny, K extends string
531
540
  /**
532
541
  * Full (frozen) clone; a `move` apply reuses the source op instead — see {@link DeltaBuilder#apply}.
533
542
  *
534
- * @return {ModifyAttrOp<Modifier,K>}
543
+ * @template {string|number} [K2=K]
544
+ * @param {K2} [key] retarget the clone to another attribute key; defaults to this op's key.
545
+ * @return {ModifyAttrOp<Modifier,K2>}
535
546
  */
536
- clone(): ModifyAttrOp<Modifier, K>;
547
+ clone<K2 extends string | number = K>(key?: K2): ModifyAttrOp<Modifier, K2>;
537
548
  $type: s.Schema<ModifyAttrOp<any, string>>;
538
549
  /**
539
550
  * @param {ModifyAttrOp<Modifier>} other
@@ -545,7 +556,7 @@ export const $modifyOp: s.Schema<ModifyOp<Delta<any>>>;
545
556
  export const $textOp: s.Schema<TextOp>;
546
557
  export const $deleteOp: s.Schema<DeleteOp<any>>;
547
558
  export const $retainOp: s.Schema<RetainOp>;
548
- export const $anyOp: s.Schema<DeleteOp<any> | TextOp | InsertOp<any> | ModifyOp<Delta<any>>>;
559
+ export const $anyOp: s.Schema<TextOp | InsertOp<any> | DeleteOp<any> | ModifyOp<Delta<any>>>;
549
560
  export const $setAttrOp: s.Schema<SetAttrOp<any>>;
550
561
  export const $modifyAttrOp: s.Schema<ModifyAttrOp<any>>;
551
562
  export const $deleteAttrOp: s.Schema<DeleteAttrOp<any>>;
@@ -992,70 +1003,38 @@ export function create<Schema extends s.Schema<DeltaAny>>(schema: Schema): Schem
992
1003
  * @param {NodeName} nodeName
993
1004
  * @param {Attrs} attrs
994
1005
  * @param {Children} [children]
995
- * @return {DeltaBuilder<(NodeName extends string ? { name: NodeName } : {}) & {
996
- * attrs: Attrs extends null ? {} : Attrs,
997
- * children: Extract<Children,Array<any>> extends Array<infer Ac> ? (unknown extends Ac ? never : Ac) : never,
998
- * text: Extract<Children,string> extends never ? false : true
999
- * }>}
1006
+ * @return {DeltaBuilder<CondensedDeltaConf<NodeName,Attrs,Children>>}
1000
1007
  */
1001
1008
  export function create<NodeName extends string | null, Attrs extends {
1002
1009
  [k: string | number]: any;
1003
- } | null, Children extends Array<any> | string = never>(nodeName: NodeName, attrs: Attrs, children?: Children | undefined): DeltaBuilder<(NodeName extends string ? {
1004
- name: NodeName;
1005
- } : {}) & {
1006
- attrs: Attrs extends null ? {} : Attrs;
1007
- children: Extract<Children, Array<any>> extends Array<infer Ac> ? (unknown extends Ac ? never : Ac) : never;
1008
- text: Extract<Children, string> extends never ? false : true;
1009
- }>;
1010
+ } | null, Children extends Array<any> | string = never>(nodeName: NodeName, attrs: Attrs, children?: Children | undefined): DeltaBuilder<CondensedDeltaConf<NodeName, Attrs, Children>>;
1010
1011
  /**
1011
1012
  * @template {string|null} NodeName
1012
1013
  * @template {Array<any>|string} [Children=never]
1013
1014
  * @overload
1014
1015
  * @param {NodeName} nodeName
1015
1016
  * @param {...Array<Children>} children
1016
- * @return {DeltaBuilder<(NodeName extends string ? { name: NodeName } : {}) & {
1017
- * children: Extract<Children,Array<any>> extends Array<infer Ac> ? (unknown extends Ac ? never : Ac) : never,
1018
- * text: Extract<Children,string> extends never ? false : true
1019
- * }>}
1017
+ * @return {DeltaBuilder<CondensedDeltaConf<NodeName,null,Children>>}
1020
1018
  */
1021
- export function from<NodeName extends string | null, Children extends Array<any> | string = never>(nodeName: NodeName, ...children: Array<Children>[]): DeltaBuilder<(NodeName extends string ? {
1022
- name: NodeName;
1023
- } : {}) & {
1024
- children: Extract<Children, Array<any>> extends Array<infer Ac> ? (unknown extends Ac ? never : Ac) : never;
1025
- text: Extract<Children, string> extends never ? false : true;
1026
- }>;
1019
+ export function from<NodeName extends string | null, Children extends Array<any> | string = never>(nodeName: NodeName, ...children: Array<Children>[]): DeltaBuilder<CondensedDeltaConf<NodeName, null, Children>>;
1027
1020
  /**
1028
1021
  * @template {Array<any>|string} [Children=never]
1029
1022
  * @overload
1030
1023
  * @param {...Array<Children>} children
1031
- * @return {DeltaBuilder<{
1032
- * children: Extract<Children,Array<any>> extends Array<infer Ac> ? (unknown extends Ac ? never : Ac) : never,
1033
- * text: Extract<Children,string> extends never ? false : true
1034
- * }>}
1024
+ * @return {DeltaBuilder<CondensedDeltaConf<null,null,Children>>}
1035
1025
  */
1036
- export function from<Children extends Array<any> | string = never>(...children: Array<Children>[]): DeltaBuilder<{
1037
- children: Extract<Children, Array<any>> extends Array<infer Ac> ? (unknown extends Ac ? never : Ac) : never;
1038
- text: Extract<Children, string> extends never ? false : true;
1039
- }>;
1026
+ export function from<Children extends Array<any> | string = never>(...children: Array<Children>[]): DeltaBuilder<CondensedDeltaConf<null, null, Children>>;
1040
1027
  /**
1041
1028
  * @template {{[k:string|number]:any}|null} Attrs
1042
1029
  * @template {Array<any>|string} [Children=never]
1043
1030
  * @overload
1044
1031
  * @param {Attrs} attrs
1045
1032
  * @param {...Array<Children>} children
1046
- * @return {DeltaBuilder<{
1047
- * attrs: Attrs extends null ? {} : Attrs,
1048
- * children: Extract<Children,Array<any>> extends Array<infer Ac> ? (unknown extends Ac ? never : Ac) : never,
1049
- * text: Extract<Children,string> extends never ? false : true
1050
- * }>}
1033
+ * @return {DeltaBuilder<CondensedDeltaConf<null,Attrs,Children>>}
1051
1034
  */
1052
1035
  export function from<Attrs extends {
1053
1036
  [k: string | number]: any;
1054
- } | null, Children extends Array<any> | string = never>(attrs: Attrs, ...children: Array<Children>[]): DeltaBuilder<{
1055
- attrs: Attrs extends null ? {} : Attrs;
1056
- children: Extract<Children, Array<any>> extends Array<infer Ac> ? (unknown extends Ac ? never : Ac) : never;
1057
- text: Extract<Children, string> extends never ? false : true;
1058
- }>;
1037
+ } | null, Children extends Array<any> | string = never>(attrs: Attrs, ...children: Array<Children>[]): DeltaBuilder<CondensedDeltaConf<null, Attrs, Children>>;
1059
1038
  /**
1060
1039
  * @template {string|null} NodeName
1061
1040
  * @template {{[k:string|number]:any}|null} Attrs
@@ -1064,28 +1043,98 @@ export function from<Attrs extends {
1064
1043
  * @param {NodeName} nodeName
1065
1044
  * @param {Attrs} attrs
1066
1045
  * @param {...Array<Children>} children
1067
- * @return {DeltaBuilder<(NodeName extends string ? { name: NodeName } : {}) & {
1068
- * attrs: Attrs extends null ? {} : Attrs,
1069
- * children: Extract<Children,Array<any>> extends Array<infer Ac> ? (unknown extends Ac ? never : Ac) : never,
1070
- * text: Extract<Children,string> extends never ? false : true
1071
- * }>}
1046
+ * @return {DeltaBuilder<CondensedDeltaConf<NodeName,Attrs,Children>>}
1072
1047
  */
1073
1048
  export function from<NodeName extends string | null, Attrs extends {
1074
1049
  [k: string | number]: any;
1075
- } | null, Children extends Array<any> | string = never>(nodeName: NodeName, attrs: Attrs, ...children: Array<Children>[]): DeltaBuilder<(NodeName extends string ? {
1076
- name: NodeName;
1077
- } : {}) & {
1078
- attrs: Attrs extends null ? {} : Attrs;
1079
- children: Extract<Children, Array<any>> extends Array<infer Ac> ? (unknown extends Ac ? never : Ac) : never;
1080
- text: Extract<Children, string> extends never ? false : true;
1081
- }>;
1050
+ } | null, Children extends Array<any> | string = never>(nodeName: NodeName, attrs: Attrs, ...children: Array<Children>[]): DeltaBuilder<CondensedDeltaConf<NodeName, Attrs, Children>>;
1051
+ /**
1052
+ * Shorthand for `delta.create().insert(..)` — see {@link DeltaBuilder#insert}.
1053
+ *
1054
+ * ## Builder shorthands
1055
+ *
1056
+ * A changeset is written as a chain (see the readme's "condensed vs. builder chain"), and the
1057
+ * `create()` head of that chain carries no information. Every content/attr builder method is
1058
+ * therefore also exported standalone — {@link insert}, {@link modify}, {@link retain},
1059
+ * {@link delete_} (trailing underscore: `delete` is a reserved word),
1060
+ * {@link setAttr}, {@link setAttrs}, {@link deleteAttr}, {@link modifyAttr} — each returning the very
1061
+ * same {@link DeltaBuilder}, so the rest of the chain is unchanged:
1062
+ *
1063
+ * delta.retain(5).delete(6).insert('!') // ⇔ delta.create().retain(5).delete(6).insert('!')
1064
+ * delta.delete_(6) // ⇔ delta.create().delete(6)
1065
+ *
1066
+ * There is deliberately no `format` shorthand: formatting is the second argument of
1067
+ * `insert`/`retain`/`modify` (`delta.retain(5, { bold: true })`). The ambient-context setters
1068
+ * (`useFormats` & co), the mark methods, `apply`/`append`/`rebase` and the terminal methods (`done`,
1069
+ * `toJSON`, ..) are not mirrored — they either say nothing on an empty delta or don't build one.
1070
+ *
1071
+ * Each shorthand is typed by indexing the method off {@link DeltaBuilder}, pinned to `create()`'s
1072
+ * zero-arg overload — so a shorthand and its `create()`-headed equivalent infer the identical conf,
1073
+ * with no re-declared generics to drift. The type arguments must be written out
1074
+ * (`DeltaBuilder<{}, false>['insert']`, not `DeltaBuilder['insert']`): a bare reference to a generic
1075
+ * type in JSDoc resolves its parameters to `any` rather than to the declared defaults, and
1076
+ * `FixedConf = any` satisfies both arms of `insert`'s `FixedConf extends true ? ..` branch, which
1077
+ * collapses the accreted conf.
1078
+ *
1079
+ * @type {DeltaBuilder<{}, false>['insert']}
1080
+ */
1081
+ export const insert: DeltaBuilder<{}, false>["insert"];
1082
+ /**
1083
+ * Shorthand for `delta.create().modify(..)` — see {@link DeltaBuilder#modify} and {@link insert}.
1084
+ *
1085
+ * @type {DeltaBuilder<{}, false>['modify']}
1086
+ */
1087
+ export const modify: DeltaBuilder<{}, false>["modify"];
1088
+ /**
1089
+ * Shorthand for `delta.create().retain(..)` — see {@link DeltaBuilder#retain} and {@link insert}.
1090
+ *
1091
+ * @type {DeltaBuilder<{}, false>['retain']}
1092
+ */
1093
+ export const retain: DeltaBuilder<{}, false>["retain"];
1094
+ /**
1095
+ * Shorthand for `delta.create().delete(..)` — see {@link DeltaBuilder}'s `delete` and {@link insert}.
1096
+ *
1097
+ * Trailing underscore because `delete` is a reserved word: `export const delete` is a syntax error,
1098
+ * and the rename clause that would work at runtime (`export { _delete as delete }`) makes `tsc` 6
1099
+ * emit a duplicated specifier into the `.d.ts` (`export { _delete as delete, _delete as delete }`),
1100
+ * which then fails `npm run check-dist-types` with "Duplicate identifier". The bug is specific to
1101
+ * declaration emit from a `.js` file — the same clause emits correctly from a `.ts` — so it cannot
1102
+ * be worked around in source; re-test it when the TypeScript floor moves.
1103
+ *
1104
+ * @type {DeltaBuilder<{}, false>['delete']}
1105
+ */
1106
+ export const delete_: DeltaBuilder<{}, false>["delete"];
1107
+ /**
1108
+ * Shorthand for `delta.create().setAttr(..)` — see {@link DeltaBuilder#setAttr} and {@link insert}.
1109
+ *
1110
+ * @type {DeltaBuilder<{}, false>['setAttr']}
1111
+ */
1112
+ export const setAttr: DeltaBuilder<{}, false>["setAttr"];
1113
+ /**
1114
+ * Shorthand for `delta.create().setAttrs(..)` — see {@link DeltaBuilder#setAttrs} and {@link insert}.
1115
+ *
1116
+ * @type {DeltaBuilder<{}, false>['setAttrs']}
1117
+ */
1118
+ export const setAttrs: DeltaBuilder<{}, false>["setAttrs"];
1119
+ /**
1120
+ * Shorthand for `delta.create().deleteAttr(..)` — see {@link DeltaBuilder#deleteAttr} and {@link insert}.
1121
+ *
1122
+ * @type {DeltaBuilder<{}, false>['deleteAttr']}
1123
+ */
1124
+ export const deleteAttr: DeltaBuilder<{}, false>["deleteAttr"];
1125
+ /**
1126
+ * Shorthand for `delta.create().modifyAttr(..)` — see {@link DeltaBuilder#modifyAttr} and {@link insert}.
1127
+ *
1128
+ * @type {DeltaBuilder<{}, false>['modifyAttr']}
1129
+ */
1130
+ export const modifyAttr: DeltaBuilder<{}, false>["modifyAttr"];
1082
1131
  export function diff<Conf_1 extends DeltaConf>(d1: Delta<Conf_1>, d2: NoInfer<Delta<Conf_1>>, options?: DiffOptions): Delta<Conf_1>;
1083
1132
  export function inverse<Conf_1 extends DeltaConf>(d: DeltaAny, base: Delta<Conf_1>): Delta<Conf_1>;
1084
1133
  export function diffChangesetWithSeparator(changeset: Array<{
1085
1134
  index: number;
1086
1135
  remove: Array<any>;
1087
1136
  insert: Array<any>;
1088
- }>, separator: RegExp): any[];
1137
+ }>, separator: RegExp, leftmostIndex?: number): any[];
1089
1138
  /**
1090
1139
  * Provenance metadata on a content/attr op: *who/what* inserted, deleted, or formatted it. The canonical
1091
1140
  * shape below is a convention — apply/diff/equality treat attribution as an **opaque** object (never branching
@@ -1268,6 +1317,28 @@ export type ReadDeltaConf<DConfSpec extends ReadableDeltaConf> = [DConfSpec exte
1268
1317
  } ? {
1269
1318
  recursiveChildren: true;
1270
1319
  } : {}))>> : never;
1320
+ /**
1321
+ * The conf produced by the condensed factories ({@link create}'s 3-arg form and {@link from}) -
1322
+ * the whole node declared in one call instead of built up with chained `setAttr`/`insert`. Routed
1323
+ * through {@link DeltaConfOverwrite} - and hence `PrettifyDeltaConf`/`_SanifyDelta` - so a condensed
1324
+ * call yields exactly the conf its `create(name).setAttrs(attrs).insert(children)`
1325
+ * equivalent would: delta-valued attrs/children normalize to `Delta<..>` (not `DeltaBuilder<..>`),
1326
+ * and an unsupplied slot is omitted rather than declared as `children: never` / `text: false`.
1327
+ *
1328
+ * The `[X] extends [never]` bracketing is load-bearing: `Extract<never,Array<any>>` is `never`, and
1329
+ * a bare `never extends Array<infer Ac>` matches with `Ac = never`.
1330
+ */
1331
+ export type CondensedDeltaConf<NodeName_1 extends string | null, Attrs_1 extends {
1332
+ [k: string | number]: any;
1333
+ } | null, Children_1 extends Array<any> | string> = DeltaConfOverwrite<NodeName_1 extends string ? {
1334
+ name: NodeName_1;
1335
+ } : {}, (Attrs_1 extends null ? {} : {
1336
+ attrs: Attrs_1;
1337
+ }) & ([Extract<Children_1, Array<any>>] extends [never] ? {} : (Extract<Children_1, Array<any>> extends Array<infer Ac> ? (unknown extends Ac ? {} : {
1338
+ children: Ac;
1339
+ }) : {})) & ([Extract<Children_1, string>] extends [never] ? {} : {
1340
+ text: true;
1341
+ })>;
1271
1342
  export type DiffOptions = {
1272
1343
  /**
1273
1344
  * Predicate deciding when two nodes
@@ -1284,6 +1355,24 @@ export type DiffOptions = {
1284
1355
  * manipulates deltas in place, e.g. a transformer).
1285
1356
  */
1286
1357
  clone?: boolean | undefined;
1358
+ /**
1359
+ * Where the change starts, as a position into `d1`: the
1360
+ * `path`'s numbers are content offsets (the trailing number is the gap the change starts at, in pre-edit
1361
+ * coordinates — the `from` of the editor transaction), string steps descend into attributes; `assoc` and
1362
+ * `attrs` are ignored. Places the first edit there when the content allows it (a wrong hint only shifts
1363
+ * placement, never correctness) — see "The hint" above.
1364
+ */
1365
+ hint?: import("./position.js").Pos | undefined;
1366
+ };
1367
+ /**
1368
+ * Where a lockstep walk of two states stands: the current op of each side and the units of it already
1369
+ * consumed from the walked end.
1370
+ */
1371
+ export type Cursors = {
1372
+ a: ChildrenOpAny | null;
1373
+ ai: number;
1374
+ b: ChildrenOpAny | null;
1375
+ bi: number;
1287
1376
  };
1288
1377
  import * as s from '../schema.js';
1289
1378
  import * as list from '../list.js';
@@ -1,4 +1,4 @@
1
- export function diff(as: Array<string>, bs: Array<string>): Array<{
1
+ export function diff(as: Array<string>, bs: Array<string>, leftmost?: boolean): Array<{
2
2
  index: number;
3
3
  remove: Array<string>;
4
4
  insert: Array<string>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lib0",
3
- "version": "1.0.0-rc.29",
3
+ "version": "1.0.0-rc.31",
4
4
  "description": "isomorphic utility functions",
5
5
  "sideEffects": false,
6
6
  "type": "module",