lib0 0.2.112 → 0.2.115-0
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.
- package/README.md +1 -1
- package/broadcastchannel.js +1 -1
- package/buffer.d.ts +3 -3
- package/buffer.d.ts.map +1 -1
- package/buffer.js +1 -1
- package/coverage/tmp/coverage-27667-1761218530660-0.json +1 -0
- package/coverage/tmp/{coverage-20055-1752683207886-0.json → coverage-27668-1761218485882-0.json} +1 -1
- package/crypto/aes-gcm.d.ts +4 -4
- package/crypto/aes-gcm.d.ts.map +1 -1
- package/crypto/aes-gcm.js +6 -6
- package/crypto/common.d.ts +1 -1
- package/crypto/common.d.ts.map +1 -1
- package/crypto/common.js +1 -1
- package/crypto/ecdsa.d.ts +2 -2
- package/crypto/ecdsa.d.ts.map +1 -1
- package/crypto/ecdsa.js +4 -4
- package/crypto/rsa-oaep.d.ts +2 -2
- package/crypto/rsa-oaep.d.ts.map +1 -1
- package/crypto/rsa-oaep.js +3 -3
- package/decoding.d.ts +27 -14
- package/decoding.d.ts.map +1 -1
- package/decoding.js +12 -8
- package/delta/abstract-array.d.ts +166 -0
- package/delta/abstract-array.d.ts.map +1 -0
- package/delta/abstract-array.js +421 -0
- package/delta/abstract.d.ts +69 -0
- package/delta/abstract.d.ts.map +1 -0
- package/delta/abstract.js +102 -0
- package/delta/array.d.ts +23 -0
- package/delta/array.d.ts.map +1 -0
- package/delta/array.js +45 -0
- package/delta/array.test.d.ts +2 -0
- package/delta/array.test.d.ts.map +1 -0
- package/delta/binding.d.ts +105 -0
- package/delta/binding.d.ts.map +1 -0
- package/delta/binding.js +369 -0
- package/delta/binding.test.d.ts +5 -0
- package/delta/binding.test.d.ts.map +1 -0
- package/delta/d2.d.ts +705 -0
- package/delta/d2.d.ts.map +1 -0
- package/delta/d2.js +1309 -0
- package/delta/d2.test.d.ts +15 -0
- package/delta/d2.test.d.ts.map +1 -0
- package/delta/index.d.ts +14 -0
- package/delta/index.d.ts.map +1 -0
- package/delta/index.js +79 -0
- package/delta/map.d.ts +230 -0
- package/delta/map.d.ts.map +1 -0
- package/delta/map.js +304 -0
- package/delta/node.d.ts +119 -0
- package/delta/node.d.ts.map +1 -0
- package/delta/node.js +183 -0
- package/delta/node.test.d.ts +4 -0
- package/delta/node.test.d.ts.map +1 -0
- package/delta/ops.d.ts +466 -0
- package/delta/ops.d.ts.map +1 -0
- package/delta/ops.js +544 -0
- package/delta/readme.md +129 -0
- package/delta/text.d.ts +43 -0
- package/delta/text.d.ts.map +1 -0
- package/delta/text.js +54 -0
- package/delta/text.test.d.ts +6 -0
- package/delta/text.test.d.ts.map +1 -0
- package/delta/transformer.d.ts +164 -0
- package/delta/transformer.d.ts.map +1 -0
- package/delta/transformer.js +888 -0
- package/delta/transformer.test.d.ts +13 -0
- package/delta/transformer.test.d.ts.map +1 -0
- package/delta/value.d.ts +84 -0
- package/delta/value.d.ts.map +1 -0
- package/delta/value.js +168 -0
- package/dist/abstract-array.cjs +433 -0
- package/dist/abstract-array.cjs.map +1 -0
- package/dist/abstract.cjs +122 -0
- package/dist/abstract.cjs.map +1 -0
- package/dist/aes-gcm.cjs +12 -12
- package/dist/aes-gcm.cjs.map +1 -1
- package/dist/array.cjs +60 -17
- package/dist/array.cjs.map +1 -1
- package/dist/array2.cjs +26 -0
- package/dist/array2.cjs.map +1 -0
- package/dist/binding.cjs +398 -0
- package/dist/binding.cjs.map +1 -0
- package/dist/{broadcastchannel-2c4b0a1c.cjs → broadcastchannel-b4eaea6e.cjs} +4 -4
- package/dist/broadcastchannel-b4eaea6e.cjs.map +1 -0
- package/dist/broadcastchannel.cjs +12 -12
- package/dist/{buffer-a74f7330.cjs → buffer-adc4e6ea.cjs} +7 -7
- package/dist/buffer-adc4e6ea.cjs.map +1 -0
- package/dist/buffer.cjs +11 -11
- package/dist/buffer.d.ts +3 -3
- package/dist/buffer.d.ts.map +1 -1
- package/dist/cache.cjs +6 -6
- package/dist/common.cjs +1 -1
- package/dist/common.cjs.map +1 -1
- package/dist/component.cjs +14 -9
- package/dist/component.cjs.map +1 -1
- package/dist/crypto/aes-gcm.d.ts +4 -4
- package/dist/crypto/aes-gcm.d.ts.map +1 -1
- package/dist/crypto/common.d.ts +1 -1
- package/dist/crypto/common.d.ts.map +1 -1
- package/dist/crypto/ecdsa.d.ts +2 -2
- package/dist/crypto/ecdsa.d.ts.map +1 -1
- package/dist/crypto/rsa-oaep.d.ts +2 -2
- package/dist/crypto/rsa-oaep.d.ts.map +1 -1
- package/dist/d2.cjs +1347 -0
- package/dist/d2.cjs.map +1 -0
- package/dist/{decoding-2b136346.cjs → decoding-50b9ce38.cjs} +18 -14
- package/dist/decoding-50b9ce38.cjs.map +1 -0
- package/dist/decoding.cjs +6 -6
- package/dist/decoding.d.ts +27 -14
- package/dist/decoding.d.ts.map +1 -1
- package/dist/delta/abstract-array.d.ts +166 -0
- package/dist/delta/abstract-array.d.ts.map +1 -0
- package/dist/delta/abstract.d.ts +69 -0
- package/dist/delta/abstract.d.ts.map +1 -0
- package/dist/delta/array.d.ts +23 -0
- package/dist/delta/array.d.ts.map +1 -0
- package/dist/delta/array.test.d.ts +2 -0
- package/dist/delta/array.test.d.ts.map +1 -0
- package/dist/delta/binding.d.ts +105 -0
- package/dist/delta/binding.d.ts.map +1 -0
- package/dist/delta/binding.test.d.ts +5 -0
- package/dist/delta/binding.test.d.ts.map +1 -0
- package/dist/delta/d2.d.ts +705 -0
- package/dist/delta/d2.d.ts.map +1 -0
- package/dist/delta/d2.test.d.ts +15 -0
- package/dist/delta/d2.test.d.ts.map +1 -0
- package/dist/delta/index.d.ts +14 -0
- package/dist/delta/index.d.ts.map +1 -0
- package/dist/delta/map.d.ts +230 -0
- package/dist/delta/map.d.ts.map +1 -0
- package/dist/delta/node.d.ts +119 -0
- package/dist/delta/node.d.ts.map +1 -0
- package/dist/delta/node.test.d.ts +4 -0
- package/dist/delta/node.test.d.ts.map +1 -0
- package/dist/delta/ops.d.ts +466 -0
- package/dist/delta/ops.d.ts.map +1 -0
- package/dist/delta/text.d.ts +43 -0
- package/dist/delta/text.d.ts.map +1 -0
- package/dist/delta/text.test.d.ts +6 -0
- package/dist/delta/text.test.d.ts.map +1 -0
- package/dist/delta/transformer.d.ts +164 -0
- package/dist/delta/transformer.d.ts.map +1 -0
- package/dist/delta/transformer.test.d.ts +13 -0
- package/dist/delta/transformer.test.d.ts.map +1 -0
- package/dist/delta/value.d.ts +84 -0
- package/dist/delta/value.d.ts.map +1 -0
- package/dist/{diff-77c4cf8e.cjs → diff-f0776c15.cjs} +2 -2
- package/dist/{diff-77c4cf8e.cjs.map → diff-f0776c15.cjs.map} +1 -1
- package/dist/diff.cjs +3 -3
- package/dist/{dom-16daf1a0.cjs → dom-2b123630.cjs} +31 -2
- package/dist/dom-2b123630.cjs.map +1 -0
- package/dist/dom.cjs +17 -2
- package/dist/dom.cjs.map +1 -1
- package/dist/dom.d.ts +17 -0
- package/dist/dom.d.ts.map +1 -1
- package/dist/ecdsa.cjs +4 -4
- package/dist/ecdsa.cjs.map +1 -1
- package/dist/{encoding-1acb59c4.cjs → encoding-7f85922c.cjs} +5 -5
- package/dist/encoding-7f85922c.cjs.map +1 -0
- package/dist/encoding.cjs +4 -4
- package/dist/encoding.d.ts +6 -6
- package/dist/encoding.d.ts.map +1 -1
- package/dist/{environment-2de08e0e.cjs → environment-90227ead.cjs} +4 -4
- package/dist/{environment-2de08e0e.cjs.map → environment-90227ead.cjs.map} +1 -1
- package/dist/environment.cjs +5 -5
- package/dist/{error-8582d695.cjs → error-0c1f634f.cjs} +10 -2
- package/dist/error-0c1f634f.cjs.map +1 -0
- package/dist/error.cjs +2 -1
- package/dist/error.cjs.map +1 -1
- package/dist/error.d.ts +1 -0
- package/dist/error.d.ts.map +1 -1
- package/dist/{eventloop-b299a889.cjs → eventloop-a0168106.cjs} +2 -2
- package/dist/{eventloop-b299a889.cjs.map → eventloop-a0168106.cjs.map} +1 -1
- package/dist/eventloop.cjs +3 -3
- package/dist/{function-09b8292c.cjs → function-e7d18feb.cjs} +2 -2
- package/dist/{function-09b8292c.cjs.map → function-e7d18feb.cjs.map} +1 -1
- package/dist/function.cjs +2 -2
- package/dist/index.cjs +23 -22
- package/dist/index.cjs.map +1 -1
- package/dist/index2.cjs +71 -0
- package/dist/index2.cjs.map +1 -0
- package/dist/{indexeddb-0cbb4d6f.cjs → indexeddb-46d1e737.cjs} +3 -3
- package/dist/{indexeddb-0cbb4d6f.cjs.map → indexeddb-46d1e737.cjs.map} +1 -1
- package/dist/indexeddb.cjs +5 -5
- package/dist/indexeddbV2.cjs +2 -1
- package/dist/indexeddbV2.cjs.map +1 -1
- package/dist/jwt.cjs +12 -12
- package/dist/list.cjs +39 -12
- package/dist/list.cjs.map +1 -1
- package/dist/list.d.ts +13 -3
- package/dist/list.d.ts.map +1 -1
- package/dist/logging.cjs +11 -9
- package/dist/logging.cjs.map +1 -1
- package/dist/logging.common.cjs +7 -7
- package/dist/logging.node.cjs +7 -7
- package/dist/{map-0dabcc55.cjs → map-24d263c0.cjs} +7 -1
- package/dist/map-24d263c0.cjs.map +1 -0
- package/dist/map.cjs +314 -7
- package/dist/map.cjs.map +1 -1
- package/dist/map.d.ts +1 -0
- package/dist/map.d.ts.map +1 -1
- package/dist/map2.cjs +15 -0
- package/dist/map2.cjs.map +1 -0
- package/dist/{math-08e068f9.cjs → math-96d5e8c4.cjs} +4 -2
- package/dist/math-96d5e8c4.cjs.map +1 -0
- package/dist/math.cjs +1 -1
- package/dist/math.d.ts.map +1 -1
- package/dist/metric.cjs +1 -1
- package/dist/node.cjs +206 -0
- package/dist/node.cjs.map +1 -0
- package/dist/{number-466d8922.cjs → number-1fb57bba.cjs} +2 -2
- package/dist/{number-466d8922.cjs.map → number-1fb57bba.cjs.map} +1 -1
- package/dist/number.cjs +2 -2
- package/dist/{object-491858d1.cjs → object-18980796.cjs} +12 -2
- package/dist/object-18980796.cjs.map +1 -0
- package/dist/object.cjs +3 -1
- package/dist/object.cjs.map +1 -1
- package/dist/object.d.ts +3 -0
- package/dist/object.d.ts.map +1 -1
- package/dist/observable.cjs +1 -1
- package/dist/ops.cjs +575 -0
- package/dist/ops.cjs.map +1 -0
- package/dist/patience.cjs +2 -2
- package/dist/performance.node.cjs +4 -4
- package/dist/pledge.cjs +2 -1
- package/dist/pledge.cjs.map +1 -1
- package/dist/{prng-24dfe0bf.cjs → prng-004c76e8.cjs} +5 -5
- package/dist/{prng-24dfe0bf.cjs.map → prng-004c76e8.cjs.map} +1 -1
- package/dist/prng.cjs +12 -12
- package/dist/prng.d.ts +1 -1
- package/dist/prng.d.ts.map +1 -1
- package/dist/{promise-7d13a97c.cjs → promise-cda7b9bb.cjs} +2 -2
- package/dist/{promise-7d13a97c.cjs.map → promise-cda7b9bb.cjs.map} +1 -1
- package/dist/promise.cjs +3 -3
- package/dist/rabin-gf2-polynomial.cjs +11 -11
- package/dist/rabin-uncached.cjs +11 -11
- package/dist/rabin.cjs +11 -11
- package/dist/random.cjs +1 -1
- package/dist/rsa-oaep.cjs +3 -3
- package/dist/rsa-oaep.cjs.map +1 -1
- package/dist/schema.cjs +572 -167
- package/dist/schema.cjs.map +1 -1
- package/dist/schema.d.ts +326 -122
- package/dist/schema.d.ts.map +1 -1
- package/dist/schema.test.d.ts +5 -0
- package/dist/schema.test.d.ts.map +1 -1
- package/dist/{sort-b8702761.cjs → sort-812cc211.cjs} +2 -2
- package/dist/{sort-b8702761.cjs.map → sort-812cc211.cjs.map} +1 -1
- package/dist/sort.cjs +2 -2
- package/dist/{statistics-c2316dca.cjs → statistics-65f6114b.cjs} +2 -2
- package/dist/{statistics-c2316dca.cjs.map → statistics-65f6114b.cjs.map} +1 -1
- package/dist/statistics.cjs +2 -2
- package/dist/{string-b2827a90.cjs → string-fddc5f8b.cjs} +3 -3
- package/dist/string-fddc5f8b.cjs.map +1 -0
- package/dist/string.cjs +1 -1
- package/dist/string.d.ts +3 -3
- package/dist/string.d.ts.map +1 -1
- package/dist/testing.cjs +16 -16
- package/dist/text.cjs +79 -0
- package/dist/text.cjs.map +1 -0
- package/dist/{time-bc2081b9.cjs → time-d8438852.cjs} +2 -2
- package/dist/{time-bc2081b9.cjs.map → time-d8438852.cjs.map} +1 -1
- package/dist/time.cjs +2 -2
- package/dist/traits.cjs +22 -0
- package/dist/traits.cjs.map +1 -1
- package/dist/traits.d.ts +1 -0
- package/dist/traits.d.ts.map +1 -1
- package/dist/traits.test.d.ts.map +1 -1
- package/dist/transformer.cjs +930 -0
- package/dist/transformer.cjs.map +1 -0
- package/dist/url.cjs +2 -1
- package/dist/url.cjs.map +1 -1
- package/dist/value.cjs +187 -0
- package/dist/value.cjs.map +1 -0
- package/dist/webcrypto.d.ts +1 -1
- package/dist/webcrypto.d.ts.map +1 -1
- package/dist/{websocket-40a601d4.cjs → websocket-b073d0fc.cjs} +3 -3
- package/dist/{websocket-40a601d4.cjs.map → websocket-b073d0fc.cjs.map} +1 -1
- package/dist/websocket.cjs +4 -4
- package/dom.d.ts +17 -0
- package/dom.d.ts.map +1 -1
- package/dom.js +21 -0
- package/encoding.d.ts +6 -6
- package/encoding.d.ts.map +1 -1
- package/encoding.js +1 -1
- package/error.d.ts +1 -0
- package/error.d.ts.map +1 -1
- package/error.js +6 -0
- package/list.d.ts +13 -3
- package/list.d.ts.map +1 -1
- package/list.js +36 -8
- package/map.d.ts +1 -0
- package/map.d.ts.map +1 -1
- package/map.js +6 -0
- package/math.d.ts.map +1 -1
- package/math.js +3 -1
- package/object.d.ts +3 -0
- package/object.d.ts.map +1 -1
- package/object.js +9 -1
- package/package.json +9 -3
- package/prng.d.ts +1 -1
- package/prng.d.ts.map +1 -1
- package/prng.js +1 -1
- package/schema.d.ts +326 -122
- package/schema.d.ts.map +1 -1
- package/schema.js +513 -141
- package/schema.test.d.ts +5 -0
- package/schema.test.d.ts.map +1 -1
- package/string.d.ts +3 -3
- package/string.d.ts.map +1 -1
- package/string.js +2 -2
- package/test.html +1 -0
- package/test.js +13 -1
- package/traits.d.ts +1 -0
- package/traits.d.ts.map +1 -1
- package/traits.js +21 -0
- package/traits.test.d.ts.map +1 -1
- package/webcrypto.d.ts +1 -1
- package/webcrypto.d.ts.map +1 -1
- package/coverage/tmp/coverage-20054-1752683240888-0.json +0 -1
- package/dist/broadcastchannel-2c4b0a1c.cjs.map +0 -1
- package/dist/buffer-a74f7330.cjs.map +0 -1
- package/dist/decoding-2b136346.cjs.map +0 -1
- package/dist/dom-16daf1a0.cjs.map +0 -1
- package/dist/encoding-1acb59c4.cjs.map +0 -1
- package/dist/error-8582d695.cjs.map +0 -1
- package/dist/map-0dabcc55.cjs.map +0 -1
- package/dist/math-08e068f9.cjs.map +0 -1
- package/dist/object-491858d1.cjs.map +0 -1
- package/dist/string-b2827a90.cjs.map +0 -1
package/delta/d2.js
ADDED
|
@@ -0,0 +1,1309 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @beta this API is about to change
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import * as list from '../list.js'
|
|
6
|
+
import * as map_ from '../map.js'
|
|
7
|
+
import * as object from '../object.js'
|
|
8
|
+
import * as traits from '../traits.js'
|
|
9
|
+
import * as arr from '../array.js'
|
|
10
|
+
import * as fun from '../function.js'
|
|
11
|
+
import * as s from '../schema.js'
|
|
12
|
+
import * as error from '../error.js'
|
|
13
|
+
import * as math from '../math.js'
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* @typedef {{
|
|
17
|
+
* insert?: string[]
|
|
18
|
+
* insertAt?: number
|
|
19
|
+
* delete?: string[]
|
|
20
|
+
* deleteAt?: number
|
|
21
|
+
* format?: Record<string,string[]>
|
|
22
|
+
* formatAt?: number
|
|
23
|
+
* }} Attribution
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* @type {s.Schema<Attribution>}
|
|
28
|
+
*/
|
|
29
|
+
export const $attribution = s.$object({
|
|
30
|
+
insert: s.$array(s.$string).optional,
|
|
31
|
+
insertAt: s.$number.optional,
|
|
32
|
+
delete: s.$array(s.$string).optional,
|
|
33
|
+
deleteAt: s.$number.optional,
|
|
34
|
+
format: s.$record(s.$string, s.$array(s.$string)).optional,
|
|
35
|
+
formatAt: s.$number.optional
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* @typedef {s.Unwrap<$anyOp>} DeltaOps
|
|
40
|
+
*/
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* @typedef {{ [key: string]: any }} FormattingAttributes
|
|
44
|
+
*/
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* @typedef {{
|
|
48
|
+
* name?: string
|
|
49
|
+
* attrs?: { [Key in string|number|symbol]: DeltaAttrOpJSON },
|
|
50
|
+
* children?: Array<DeltaListOpJSON>
|
|
51
|
+
* }} DeltaJSON
|
|
52
|
+
*/
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* @typedef {{ insert: string|Array<any>, format?: { [key: string]: any }, attribution?: Attribution } | { delete: number } | { retain: number, format?: { [key:string]: any }, attribution?: Attribution } | { modify: object }} DeltaListOpJSON
|
|
56
|
+
*/
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* @typedef {{ type: 'insert', value: any, prevValue?: any, attribution?: Attribution } | { type: 'delete', prevValue?: any, attribution?: Attribution } | { type: 'modify', modify: DeltaJSON }} DeltaAttrOpJSON
|
|
60
|
+
*/
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* @type {s.Schema<DeltaAttrOpJSON>}
|
|
64
|
+
*/
|
|
65
|
+
export const $deltaMapChangeJson = s.$union(
|
|
66
|
+
s.$object({ type: s.$literal('insert'), value: s.$any, prevValue: s.$any.optional, attribution: $attribution.optional }),
|
|
67
|
+
s.$object({ type: s.$literal('modify'), modify: s.$any }),
|
|
68
|
+
s.$object({ type: s.$literal('delete'), prevValue: s.$any.optional, attribution: $attribution.optional })
|
|
69
|
+
)
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* @template {{[key:string]: any} | null} Attrs
|
|
73
|
+
* @param {Attrs} attrs
|
|
74
|
+
* @return {Attrs}
|
|
75
|
+
*/
|
|
76
|
+
const _cloneAttrs = attrs => attrs == null ? attrs : { ...attrs }
|
|
77
|
+
/**
|
|
78
|
+
* @template {any} MaybeDelta
|
|
79
|
+
* @param {MaybeDelta} maybeDelta
|
|
80
|
+
* @return {MaybeDelta}
|
|
81
|
+
*/
|
|
82
|
+
const _cloneMaybeDelta = maybeDelta => $deltaAny.check(maybeDelta) ? maybeDelta.clone() : maybeDelta
|
|
83
|
+
|
|
84
|
+
export class TextOp extends list.ListNode {
|
|
85
|
+
/**
|
|
86
|
+
* @param {string} insert
|
|
87
|
+
* @param {FormattingAttributes|null} format
|
|
88
|
+
* @param {Attribution?} attribution
|
|
89
|
+
*/
|
|
90
|
+
constructor (insert, format, attribution) {
|
|
91
|
+
super()
|
|
92
|
+
this.insert = insert
|
|
93
|
+
this.format = format
|
|
94
|
+
this.attribution = attribution
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* @return {'insert'}
|
|
99
|
+
*/
|
|
100
|
+
get type () {
|
|
101
|
+
return 'insert'
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
get length () {
|
|
105
|
+
return this.insert.length
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Remove a part of the operation (similar to Array.splice)
|
|
110
|
+
*
|
|
111
|
+
* @param {number} offset
|
|
112
|
+
* @param {number} len
|
|
113
|
+
*/
|
|
114
|
+
_splice (offset, len) {
|
|
115
|
+
this.insert = this.insert.slice(0, offset) + this.insert.slice(offset + len)
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* @return {DeltaListOpJSON}
|
|
120
|
+
*/
|
|
121
|
+
toJSON () {
|
|
122
|
+
const { insert, format, attribution } = this
|
|
123
|
+
return object.assign({ insert }, format != null ? { format } : ({}), attribution != null ? { attribution } : ({}))
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* @param {TextOp} other
|
|
128
|
+
*/
|
|
129
|
+
[traits.EqualityTraitSymbol] (other) {
|
|
130
|
+
return fun.equalityDeep(this.insert, other.insert) && fun.equalityDeep(this.format, other.format) && fun.equalityDeep(this.attribution, other.attribution)
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* @return {TextOp}
|
|
135
|
+
*/
|
|
136
|
+
clone () {
|
|
137
|
+
return new TextOp(this.insert, _cloneAttrs(this.format), _cloneAttrs(this.attribution))
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* @template ArrayContent
|
|
143
|
+
*/
|
|
144
|
+
export class InsertOp extends list.ListNode {
|
|
145
|
+
/**
|
|
146
|
+
* @param {Array<ArrayContent>} insert
|
|
147
|
+
* @param {FormattingAttributes|null} format
|
|
148
|
+
* @param {Attribution?} attribution
|
|
149
|
+
*/
|
|
150
|
+
constructor (insert, format, attribution) {
|
|
151
|
+
super()
|
|
152
|
+
this.insert = insert
|
|
153
|
+
this.format = format
|
|
154
|
+
this.attribution = attribution
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* @return {'insert'}
|
|
159
|
+
*/
|
|
160
|
+
get type () {
|
|
161
|
+
return 'insert'
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
get length () {
|
|
165
|
+
return this.insert.length
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Remove a part of the operation (similar to Array.splice)
|
|
170
|
+
*
|
|
171
|
+
* @param {number} offset
|
|
172
|
+
* @param {number} len
|
|
173
|
+
*/
|
|
174
|
+
_splice (offset, len) {
|
|
175
|
+
this.insert.splice(offset, len)
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* @return {DeltaListOpJSON}
|
|
180
|
+
*/
|
|
181
|
+
toJSON () {
|
|
182
|
+
const { insert, format, attribution } = this
|
|
183
|
+
return object.assign({ insert: insert.map(ins => $deltaAny.check(ins) ? ins.toJSON() : ins) }, format ? { format } : ({}), attribution != null ? { attribution } : ({}))
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* @param {InsertOp<ArrayContent>} other
|
|
188
|
+
*/
|
|
189
|
+
[traits.EqualityTraitSymbol] (other) {
|
|
190
|
+
return fun.equalityDeep(this.insert, other.insert) && fun.equalityDeep(this.format, other.format) && fun.equalityDeep(this.attribution, other.attribution)
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* @return {InsertOp<ArrayContent>}
|
|
195
|
+
*/
|
|
196
|
+
clone () {
|
|
197
|
+
return new InsertOp(this.insert.slice(), _cloneAttrs(this.format), _cloneAttrs(this.attribution))
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
export class DeleteOp extends list.ListNode {
|
|
202
|
+
/**
|
|
203
|
+
* @param {number} len
|
|
204
|
+
*/
|
|
205
|
+
constructor (len) {
|
|
206
|
+
super()
|
|
207
|
+
this.delete = len
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* @return {'delete'}
|
|
212
|
+
*/
|
|
213
|
+
get type () {
|
|
214
|
+
return 'delete'
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
get length () {
|
|
218
|
+
return 0
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* Remove a part of the operation (similar to Array.splice)
|
|
223
|
+
*
|
|
224
|
+
* @param {number} _offset
|
|
225
|
+
* @param {number} len
|
|
226
|
+
*/
|
|
227
|
+
_splice (_offset, len) {
|
|
228
|
+
this.delete -= len
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* @return {DeltaListOpJSON}
|
|
233
|
+
*/
|
|
234
|
+
toJSON () {
|
|
235
|
+
return { delete: this.delete }
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* @param {DeleteOp} other
|
|
240
|
+
*/
|
|
241
|
+
[traits.EqualityTraitSymbol] (other) {
|
|
242
|
+
return this.delete === other.delete
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
clone () {
|
|
246
|
+
return new DeleteOp(this.delete)
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
export class RetainOp extends list.ListNode {
|
|
251
|
+
/**
|
|
252
|
+
* @param {number} retain
|
|
253
|
+
* @param {FormattingAttributes|null} format
|
|
254
|
+
* @param {Attribution?} attribution
|
|
255
|
+
*/
|
|
256
|
+
constructor (retain, format, attribution) {
|
|
257
|
+
super()
|
|
258
|
+
this.retain = retain
|
|
259
|
+
this.format = format
|
|
260
|
+
this.attribution = attribution
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* @return {'retain'}
|
|
265
|
+
*/
|
|
266
|
+
get type () {
|
|
267
|
+
return 'retain'
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
get length () {
|
|
271
|
+
return this.retain
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* Remove a part of the operation (similar to Array.splice)
|
|
276
|
+
*
|
|
277
|
+
* @param {number} _offset
|
|
278
|
+
* @param {number} len
|
|
279
|
+
*/
|
|
280
|
+
_splice (_offset, len) {
|
|
281
|
+
this.retain -= len
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/**
|
|
285
|
+
* @return {DeltaListOpJSON}
|
|
286
|
+
*/
|
|
287
|
+
toJSON () {
|
|
288
|
+
const { retain, format, attribution } = this
|
|
289
|
+
return object.assign({ retain }, format ? { format } : {}, attribution != null ? { attribution } : {})
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
/**
|
|
293
|
+
* @param {RetainOp} other
|
|
294
|
+
*/
|
|
295
|
+
[traits.EqualityTraitSymbol] (other) {
|
|
296
|
+
return this.retain === other.retain && fun.equalityDeep(this.format, other.format) && fun.equalityDeep(this.attribution, other.attribution)
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
clone () {
|
|
300
|
+
return new RetainOp(this.retain, _cloneAttrs(this.format), _cloneAttrs(this.attribution))
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/**
|
|
305
|
+
* Delta that can be applied on a YType Embed
|
|
306
|
+
*
|
|
307
|
+
* @template {Delta<any,any,any,any,any>} DTypes
|
|
308
|
+
*/
|
|
309
|
+
export class ModifyOp extends list.ListNode {
|
|
310
|
+
/**
|
|
311
|
+
* @param {DTypes} delta
|
|
312
|
+
* @param {FormattingAttributes|null} format
|
|
313
|
+
* @param {Attribution?} attribution
|
|
314
|
+
*/
|
|
315
|
+
constructor (delta, format, attribution) {
|
|
316
|
+
super()
|
|
317
|
+
this.modify = delta
|
|
318
|
+
this.format = format
|
|
319
|
+
this.attribution = attribution
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
/**
|
|
323
|
+
* @return {'modify'}
|
|
324
|
+
*/
|
|
325
|
+
get type () {
|
|
326
|
+
return 'modify'
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
get length () {
|
|
330
|
+
return 1
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
/**
|
|
334
|
+
* Remove a part of the operation (similar to Array.splice)
|
|
335
|
+
*
|
|
336
|
+
* @param {number} _offset
|
|
337
|
+
* @param {number} _len
|
|
338
|
+
*/
|
|
339
|
+
_splice (_offset, _len) {
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
/**
|
|
343
|
+
* @return {DeltaListOpJSON}
|
|
344
|
+
*/
|
|
345
|
+
toJSON () {
|
|
346
|
+
const { modify, attribution, format } = this
|
|
347
|
+
return object.assign({ modify: modify.toJSON() }, format ? { format } : {}, attribution != null ? { attribution } : {})
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
/**
|
|
351
|
+
* @param {ModifyOp<any>} other
|
|
352
|
+
*/
|
|
353
|
+
[traits.EqualityTraitSymbol] (other) {
|
|
354
|
+
return this.modify[traits.EqualityTraitSymbol](other.modify) && fun.equalityDeep(this.format, other.format) && fun.equalityDeep(this.attribution, other.attribution)
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
/**
|
|
358
|
+
* @return {ModifyOp<DTypes>}
|
|
359
|
+
*/
|
|
360
|
+
clone () {
|
|
361
|
+
return new ModifyOp(this.modify.clone(), _cloneAttrs(this.format), _cloneAttrs(this.attribution))
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
/**
|
|
366
|
+
* @template V
|
|
367
|
+
* @template [K=string]
|
|
368
|
+
*/
|
|
369
|
+
export class MapInsertOp {
|
|
370
|
+
/**
|
|
371
|
+
* @param {K} key
|
|
372
|
+
* @param {V} value
|
|
373
|
+
* @param {V|undefined} prevValue
|
|
374
|
+
* @param {Attribution?} attribution
|
|
375
|
+
*/
|
|
376
|
+
constructor (key, value, prevValue, attribution) {
|
|
377
|
+
/**
|
|
378
|
+
* @type {K}
|
|
379
|
+
*/
|
|
380
|
+
this.key = key
|
|
381
|
+
/**
|
|
382
|
+
* @type {V}
|
|
383
|
+
*/
|
|
384
|
+
this.value = value
|
|
385
|
+
/**
|
|
386
|
+
* @type {V|undefined}
|
|
387
|
+
*/
|
|
388
|
+
this.prevValue = prevValue
|
|
389
|
+
this.attribution = attribution
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
/**
|
|
393
|
+
* @return {'insert'}
|
|
394
|
+
*/
|
|
395
|
+
get type () { return 'insert' }
|
|
396
|
+
|
|
397
|
+
toJSON () {
|
|
398
|
+
const v = this.value
|
|
399
|
+
const prevValue = this.prevValue
|
|
400
|
+
const attribution = this.attribution
|
|
401
|
+
return object.assign({
|
|
402
|
+
type: this.type,
|
|
403
|
+
value: $deltaAny.check(v) ? v.toJSON() : v
|
|
404
|
+
}, attribution != null ? { attribution } : {}, prevValue !== undefined ? { prevValue } : {})
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
/**
|
|
408
|
+
* @param {MapInsertOp<V>} other
|
|
409
|
+
*/
|
|
410
|
+
[traits.EqualityTraitSymbol] (other) {
|
|
411
|
+
return this.key === other.key && fun.equalityDeep(this.value, other.value) && fun.equalityDeep(this.attribution, other.attribution)
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
/**
|
|
415
|
+
* @return {MapInsertOp<V,K>}
|
|
416
|
+
*/
|
|
417
|
+
clone () {
|
|
418
|
+
return new MapInsertOp(this.key, _cloneMaybeDelta(this.value), _cloneMaybeDelta(this.prevValue), _cloneAttrs(this.attribution))
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
/**
|
|
423
|
+
* @template V
|
|
424
|
+
* @template [K=string]
|
|
425
|
+
*/
|
|
426
|
+
export class MapDeleteOp {
|
|
427
|
+
/**
|
|
428
|
+
* @param {K} key
|
|
429
|
+
* @param {V|undefined} prevValue
|
|
430
|
+
* @param {Attribution?} attribution
|
|
431
|
+
*/
|
|
432
|
+
constructor (key, prevValue, attribution) {
|
|
433
|
+
/**
|
|
434
|
+
* @type {K}
|
|
435
|
+
*/
|
|
436
|
+
this.key = key
|
|
437
|
+
/**
|
|
438
|
+
* @type {V|undefined}
|
|
439
|
+
*/
|
|
440
|
+
this.prevValue = prevValue
|
|
441
|
+
this.attribution = attribution
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
get value () { return undefined }
|
|
445
|
+
|
|
446
|
+
/**
|
|
447
|
+
* @type {'delete'}
|
|
448
|
+
*/
|
|
449
|
+
get type () { return 'delete' }
|
|
450
|
+
|
|
451
|
+
/**
|
|
452
|
+
* @return {DeltaAttrOpJSON}
|
|
453
|
+
*/
|
|
454
|
+
toJSON () {
|
|
455
|
+
const {
|
|
456
|
+
type, attribution, prevValue
|
|
457
|
+
} = this
|
|
458
|
+
return object.assign({ type }, attribution != null ? { attribution } : {}, prevValue !== undefined ? { prevValue } : {})
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
/**
|
|
462
|
+
* @param {MapDeleteOp<V>} other
|
|
463
|
+
*/
|
|
464
|
+
[traits.EqualityTraitSymbol] (other) {
|
|
465
|
+
return this.key === other.key && fun.equalityDeep(this.attribution, other.attribution)
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
clone () {
|
|
469
|
+
return new MapDeleteOp(this.key, _cloneMaybeDelta(this.prevValue), _cloneAttrs(this.attribution))
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
/**
|
|
474
|
+
* @template {Delta} Modifier
|
|
475
|
+
* @template [K=string]
|
|
476
|
+
*/
|
|
477
|
+
export class MapModifyOp {
|
|
478
|
+
/**
|
|
479
|
+
* @param {K} key
|
|
480
|
+
* @param {Modifier} delta
|
|
481
|
+
*/
|
|
482
|
+
constructor (key, delta) {
|
|
483
|
+
/**
|
|
484
|
+
* @type {K}
|
|
485
|
+
*/
|
|
486
|
+
this.key = key
|
|
487
|
+
/**
|
|
488
|
+
* @type {Modifier}
|
|
489
|
+
*/
|
|
490
|
+
this.value = delta
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
/**
|
|
494
|
+
* @type {'modify'}
|
|
495
|
+
*/
|
|
496
|
+
get type () { return 'modify' }
|
|
497
|
+
|
|
498
|
+
/**
|
|
499
|
+
* @return {DeltaAttrOpJSON}
|
|
500
|
+
*/
|
|
501
|
+
toJSON () {
|
|
502
|
+
return {
|
|
503
|
+
type: this.type,
|
|
504
|
+
modify: this.value.toJSON()
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
/**
|
|
509
|
+
* @param {MapModifyOp<Modifier>} other
|
|
510
|
+
*/
|
|
511
|
+
[traits.EqualityTraitSymbol] (other) {
|
|
512
|
+
return this.key === other.key && this.value[traits.EqualityTraitSymbol](other.value)
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
/**
|
|
516
|
+
* @return {MapModifyOp<Modifier,K>}
|
|
517
|
+
*/
|
|
518
|
+
clone () {
|
|
519
|
+
return new MapModifyOp(this.key, this.value.clone())
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
/**
|
|
524
|
+
* @type {s.Schema<MapDeleteOp<any> | DeleteOp>}
|
|
525
|
+
*/
|
|
526
|
+
export const $deleteOp = s.$custom(o => o != null && (o.constructor === DeleteOp || o.constructor === MapDeleteOp))
|
|
527
|
+
|
|
528
|
+
/**
|
|
529
|
+
* @type {s.Schema<MapInsertOp<any> | InsertOp<any>>}
|
|
530
|
+
*/
|
|
531
|
+
export const $insertOp = s.$custom(o => o != null && (o.constructor === MapInsertOp || o.constructor === InsertOp))
|
|
532
|
+
|
|
533
|
+
/**
|
|
534
|
+
* @template Content
|
|
535
|
+
* @param {s.Schema<Content>} $content
|
|
536
|
+
* @return {s.Schema<MapInsertOp<Content> | InsertOp<Content>>}
|
|
537
|
+
*/
|
|
538
|
+
export const $insertOpWith = $content => s.$custom(o =>
|
|
539
|
+
o != null && (
|
|
540
|
+
(o.constructor === MapInsertOp && $content.check(/** @type {MapInsertOp<Content>} */ (o).value)) ||
|
|
541
|
+
(o.constructor === InsertOp && /** @type {InsertOp<Content>} */ (o).insert.every(ins => $content.check(ins)))
|
|
542
|
+
)
|
|
543
|
+
)
|
|
544
|
+
|
|
545
|
+
/**
|
|
546
|
+
* @type {s.Schema<TextOp>}
|
|
547
|
+
*/
|
|
548
|
+
export const $textOp = s.$constructedBy(TextOp)
|
|
549
|
+
|
|
550
|
+
/**
|
|
551
|
+
* @type {s.Schema<RetainOp>}
|
|
552
|
+
*/
|
|
553
|
+
export const $retainOp = s.$constructedBy(RetainOp)
|
|
554
|
+
|
|
555
|
+
/**
|
|
556
|
+
* @type {s.Schema<MapModifyOp<any> | ModifyOp<any>>}
|
|
557
|
+
*/
|
|
558
|
+
export const $modifyOp = s.$custom(o => o != null && (o.constructor === MapModifyOp || o.constructor === ModifyOp))
|
|
559
|
+
|
|
560
|
+
/**
|
|
561
|
+
* @template {Delta} Modify
|
|
562
|
+
* @param {s.Schema<Modify>} $content
|
|
563
|
+
* @return {s.Schema<MapModifyOp<Modify> | ModifyOp<Modify>>}
|
|
564
|
+
*/
|
|
565
|
+
export const $modifyOpWith = $content => s.$custom(o =>
|
|
566
|
+
o != null && (
|
|
567
|
+
(o.constructor === MapModifyOp && $content.check(/** @type {MapModifyOp<Modify>} */ (o).value)) ||
|
|
568
|
+
(o.constructor === ModifyOp && $content.check(/** @type {ModifyOp<Modify>} */ (o).modify))
|
|
569
|
+
)
|
|
570
|
+
)
|
|
571
|
+
|
|
572
|
+
export const $anyOp = s.$union($insertOp, $deleteOp, $textOp, $modifyOp)
|
|
573
|
+
|
|
574
|
+
/**
|
|
575
|
+
* @template {Array<any>|string} C1
|
|
576
|
+
* @template {Array<any>|string} C2
|
|
577
|
+
* @typedef {Extract<C1 | C2, Array<any>> extends never
|
|
578
|
+
* ? never
|
|
579
|
+
* : (Array<(Extract<C1 | C2,Array<any>> extends Array<infer AC1> ? (unknown extends AC1 ? never : AC1) : never)>)} MergeListArrays
|
|
580
|
+
*/
|
|
581
|
+
|
|
582
|
+
/**
|
|
583
|
+
* @template {{[Key in string|number|symbol]: any}} Attrs
|
|
584
|
+
* @template {string|number|symbol} Key
|
|
585
|
+
* @template {any} Val
|
|
586
|
+
* @typedef {{ [K in (Key | keyof Attrs)]: (unknown extends Attrs[K] ? never : Attrs[K]) | (Key extends K ? Val : never) }} AddToAttrs
|
|
587
|
+
*/
|
|
588
|
+
|
|
589
|
+
/**
|
|
590
|
+
* @template {{[Key in string|number|symbol]: any}} Attrs
|
|
591
|
+
* @template {{[Key in string|number|symbol]: any}} NewAttrs
|
|
592
|
+
* @typedef {{ [K in (keyof NewAttrs | keyof Attrs)]: (unknown extends Attrs[K] ? never : Attrs[K]) | (unknown extends NewAttrs[K] ? never : NewAttrs[K]) }} MergeAttrs
|
|
593
|
+
*/
|
|
594
|
+
|
|
595
|
+
/**
|
|
596
|
+
* @template X
|
|
597
|
+
* @typedef {0 extends (1 & X) ? null : X} _AnyToNull
|
|
598
|
+
*/
|
|
599
|
+
|
|
600
|
+
/**
|
|
601
|
+
* @template {s.Schema<Delta<any,any,any,any,any>>|null} Schema
|
|
602
|
+
* @typedef {_AnyToNull<Schema> extends null ? Delta<any,{[key:string|number|symbol]:any},any,string> : (Schema extends s.Schema<infer D> ? D : never)} AllowedDeltaFromSchema
|
|
603
|
+
*/
|
|
604
|
+
|
|
605
|
+
/**
|
|
606
|
+
* @template {string} [NodeName=any]
|
|
607
|
+
* @template {{[key:string|number|symbol]:any}} [out Attrs={}]
|
|
608
|
+
* @template {any} [out Children=never]
|
|
609
|
+
* @template {string|never} [out Text=never]
|
|
610
|
+
* @template {s.Schema<Delta<any,any,any,any,any>>|null} [Schema=any]
|
|
611
|
+
*/
|
|
612
|
+
export class Delta {
|
|
613
|
+
/**
|
|
614
|
+
* @param {NodeName} [name]
|
|
615
|
+
* @param {Schema} [$schema]
|
|
616
|
+
*/
|
|
617
|
+
constructor (name, $schema) {
|
|
618
|
+
this.name = name || null
|
|
619
|
+
this.$schema = $schema || null
|
|
620
|
+
/**
|
|
621
|
+
* @type {Map<keyof Attrs, { [K in keyof Attrs]: MapInsertOp<Attrs[K],K>|MapDeleteOp<Attrs[K],K>|(Delta extends Attrs[K] ? MapModifyOp<Extract<Attrs[K],Delta>,K> : never) }[keyof Attrs]>}
|
|
622
|
+
*/
|
|
623
|
+
this.attrs = map_.create()
|
|
624
|
+
/**
|
|
625
|
+
* @type {list.List<
|
|
626
|
+
* RetainOp
|
|
627
|
+
* | DeleteOp
|
|
628
|
+
* | (Text extends never ? never : TextOp)
|
|
629
|
+
* | (Children extends never ? never : InsertOp<Children>)
|
|
630
|
+
* | (Delta extends Children ? ModifyOp<Extract<Children,Delta>> : never)
|
|
631
|
+
* >}
|
|
632
|
+
*/
|
|
633
|
+
this.children = /** @type {any} */ (list.create())
|
|
634
|
+
/**
|
|
635
|
+
* @type {FormattingAttributes?}
|
|
636
|
+
*/
|
|
637
|
+
this.usedAttributes = null
|
|
638
|
+
/**
|
|
639
|
+
* @type {Attribution?}
|
|
640
|
+
*/
|
|
641
|
+
this.usedAttribution = null
|
|
642
|
+
/**
|
|
643
|
+
* @type {any}
|
|
644
|
+
*/
|
|
645
|
+
this.origin = null
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
isEmpty () {
|
|
649
|
+
return this.attrs.size === 0 && list.isEmpty(this.children)
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
/**
|
|
653
|
+
* @return {DeltaJSON}
|
|
654
|
+
*/
|
|
655
|
+
toJSON () {
|
|
656
|
+
/**
|
|
657
|
+
* @type {any}
|
|
658
|
+
*/
|
|
659
|
+
const attrs = {}
|
|
660
|
+
/**
|
|
661
|
+
* @type {any}
|
|
662
|
+
*/
|
|
663
|
+
const children = []
|
|
664
|
+
this.attrs.forEach(attr => {
|
|
665
|
+
attrs[attr.key] = attr.toJSON()
|
|
666
|
+
})
|
|
667
|
+
this.children.forEach(val => {
|
|
668
|
+
children.push(val.toJSON())
|
|
669
|
+
})
|
|
670
|
+
return object.assign(
|
|
671
|
+
(this.name != null ? { name: this.name } : {}),
|
|
672
|
+
(object.isEmpty(attrs) ? {} : { attrs }),
|
|
673
|
+
(children.length > 0 ? { children } : {})
|
|
674
|
+
)
|
|
675
|
+
}
|
|
676
|
+
|
|
677
|
+
/**
|
|
678
|
+
* @param {Delta<any,any,any,any,any>} other
|
|
679
|
+
* @return {boolean}
|
|
680
|
+
*/
|
|
681
|
+
equals (other) {
|
|
682
|
+
return this[traits.EqualityTraitSymbol](other)
|
|
683
|
+
}
|
|
684
|
+
|
|
685
|
+
/**
|
|
686
|
+
* @return {this}
|
|
687
|
+
*/
|
|
688
|
+
clone () {
|
|
689
|
+
/**
|
|
690
|
+
* @type {Delta<any,{[k:string|number|symbol]:any},any,any>}
|
|
691
|
+
*/
|
|
692
|
+
const d = new Delta(/** @type {any} */ (this.name), this.$schema)
|
|
693
|
+
d.origin = this.origin
|
|
694
|
+
this.attrs.forEach(op => {
|
|
695
|
+
d.attrs.set(op.key, /** @type {any} */ (op))
|
|
696
|
+
})
|
|
697
|
+
this.children.forEach(op => {
|
|
698
|
+
list.pushEnd(d.children, op.clone())
|
|
699
|
+
})
|
|
700
|
+
return /** @type {any} */ (d)
|
|
701
|
+
}
|
|
702
|
+
|
|
703
|
+
/**
|
|
704
|
+
* @param {any} other
|
|
705
|
+
* @return {boolean}
|
|
706
|
+
*/
|
|
707
|
+
[traits.EqualityTraitSymbol] (other) {
|
|
708
|
+
return this.name === other.name && fun.equalityDeep(this.attrs, other.attrs) && fun.equalityDeep(this.children, other.children)
|
|
709
|
+
}
|
|
710
|
+
|
|
711
|
+
/**
|
|
712
|
+
* @param {Attribution?} attribution
|
|
713
|
+
*/
|
|
714
|
+
useAttribution (attribution) {
|
|
715
|
+
this.usedAttribution = attribution
|
|
716
|
+
return this
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
/**
|
|
720
|
+
* @param {FormattingAttributes?} attributes
|
|
721
|
+
* @return {this}
|
|
722
|
+
*/
|
|
723
|
+
useAttributes (attributes) {
|
|
724
|
+
this.usedAttributes = attributes
|
|
725
|
+
return this
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
/**
|
|
729
|
+
* @param {string} name
|
|
730
|
+
* @param {any} value
|
|
731
|
+
*/
|
|
732
|
+
updateUsedAttributes (name, value) {
|
|
733
|
+
if (value == null) {
|
|
734
|
+
this.usedAttributes = object.assign({}, this.usedAttributes)
|
|
735
|
+
delete this.usedAttributes?.[name]
|
|
736
|
+
if (object.isEmpty(this.usedAttributes)) {
|
|
737
|
+
this.usedAttributes = null
|
|
738
|
+
}
|
|
739
|
+
} else if (!fun.equalityDeep(this.usedAttributes?.[name], value)) {
|
|
740
|
+
this.usedAttributes = object.assign({}, this.usedAttributes)
|
|
741
|
+
this.usedAttributes[name] = value
|
|
742
|
+
}
|
|
743
|
+
return this
|
|
744
|
+
}
|
|
745
|
+
|
|
746
|
+
/**
|
|
747
|
+
* @template {keyof Attribution} NAME
|
|
748
|
+
* @param {NAME} name
|
|
749
|
+
* @param {Attribution[NAME]?} value
|
|
750
|
+
*/
|
|
751
|
+
updateUsedAttribution (name, value) {
|
|
752
|
+
if (value == null) {
|
|
753
|
+
this.usedAttribution = object.assign({}, this.usedAttribution)
|
|
754
|
+
delete this.usedAttribution?.[name]
|
|
755
|
+
if (object.isEmpty(this.usedAttribution)) {
|
|
756
|
+
this.usedAttribution = null
|
|
757
|
+
}
|
|
758
|
+
} else if (!fun.equalityDeep(this.usedAttribution?.[name], value)) {
|
|
759
|
+
this.usedAttribution = object.assign({}, this.usedAttribution)
|
|
760
|
+
this.usedAttribution[name] = value
|
|
761
|
+
}
|
|
762
|
+
return this
|
|
763
|
+
}
|
|
764
|
+
|
|
765
|
+
/**
|
|
766
|
+
* @template {AllowedDeltaFromSchema<Schema> extends Delta<any,any,infer Children,infer Text,infer Schema> ? ((Children extends never ? never : Array<Children>) | Text) : never} NewContent
|
|
767
|
+
* @param {NewContent} insert
|
|
768
|
+
* @param {FormattingAttributes?} [formatting]
|
|
769
|
+
* @param {Attribution?} [attribution]
|
|
770
|
+
* @return {Delta<
|
|
771
|
+
* NodeName,
|
|
772
|
+
* Attrs,
|
|
773
|
+
* Exclude<NewContent,string>[number]|Children,
|
|
774
|
+
* (Extract<NewContent,string>|Text) extends string ? string : never,
|
|
775
|
+
* Schema
|
|
776
|
+
* >}
|
|
777
|
+
*/
|
|
778
|
+
insert (insert, formatting = null, attribution = null) {
|
|
779
|
+
const mergedAttributes = mergeAttrs(this.usedAttributes, formatting)
|
|
780
|
+
const mergedAttribution = mergeAttrs(this.usedAttribution, attribution)
|
|
781
|
+
/**
|
|
782
|
+
* @param {TextOp | InsertOp<any>} lastOp
|
|
783
|
+
*/
|
|
784
|
+
const checkMergedEquals = lastOp => (mergedAttributes === lastOp.format || fun.equalityDeep(mergedAttributes, lastOp.format)) && (mergedAttribution === lastOp.attribution || fun.equalityDeep(mergedAttribution, lastOp.attribution))
|
|
785
|
+
const end = this.children.end
|
|
786
|
+
if (s.$string.check(insert)) {
|
|
787
|
+
if ($textOp.check(end) && checkMergedEquals(end)) {
|
|
788
|
+
end.insert += insert
|
|
789
|
+
} else if (insert.length > 0) {
|
|
790
|
+
list.pushEnd(this.children, new TextOp(insert, object.isEmpty(mergedAttributes) ? null : mergedAttributes, object.isEmpty(mergedAttribution) ? null : mergedAttribution))
|
|
791
|
+
}
|
|
792
|
+
} else if (arr.isArray(insert)) {
|
|
793
|
+
if ($insertOp.check(end) && checkMergedEquals(end)) {
|
|
794
|
+
end.insert.push(...insert)
|
|
795
|
+
} else if (insert.length > 0) {
|
|
796
|
+
list.pushEnd(this.children, new InsertOp(insert, object.isEmpty(mergedAttributes) ? null : mergedAttributes, object.isEmpty(mergedAttribution) ? null : mergedAttribution))
|
|
797
|
+
}
|
|
798
|
+
}
|
|
799
|
+
return /** @type {any} */ (this)
|
|
800
|
+
}
|
|
801
|
+
|
|
802
|
+
/**
|
|
803
|
+
* @template {AllowedDeltaFromSchema<Schema> extends Delta<any,any,infer Children,any,any> ? Extract<Children,Delta<any,any,any,any,any>> : never} NewContent
|
|
804
|
+
* @param {NewContent} modify
|
|
805
|
+
* @param {FormattingAttributes?} formatting
|
|
806
|
+
* @param {Attribution?} attribution
|
|
807
|
+
* @return {Delta<
|
|
808
|
+
* NodeName,
|
|
809
|
+
* Attrs,
|
|
810
|
+
* Exclude<NewContent,string>[number]|Children,
|
|
811
|
+
* (Extract<NewContent,string>|Text) extends string ? string : never,
|
|
812
|
+
* Schema
|
|
813
|
+
* >}
|
|
814
|
+
*/
|
|
815
|
+
modify (modify, formatting = null, attribution = null) {
|
|
816
|
+
const mergedAttributes = mergeAttrs(this.usedAttributes, formatting)
|
|
817
|
+
const mergedAttribution = mergeAttrs(this.usedAttribution, attribution)
|
|
818
|
+
list.pushEnd(this.children, new ModifyOp(modify, object.isEmpty(mergedAttributes) ? null : mergedAttributes, object.isEmpty(mergedAttribution) ? null : mergedAttribution))
|
|
819
|
+
return /** @type {any} */ (this)
|
|
820
|
+
}
|
|
821
|
+
|
|
822
|
+
/**
|
|
823
|
+
* @param {number} len
|
|
824
|
+
* @param {FormattingAttributes?} [format]
|
|
825
|
+
* @param {Attribution?} [attribution]
|
|
826
|
+
*/
|
|
827
|
+
retain (len, format = null, attribution = null) {
|
|
828
|
+
const mergedFormats = mergeAttrs(this.usedAttributes, format)
|
|
829
|
+
const mergedAttribution = mergeAttrs(this.usedAttribution, attribution)
|
|
830
|
+
const lastOp = /** @type {RetainOp|InsertOp<any>} */ (this.children.end)
|
|
831
|
+
if (lastOp instanceof RetainOp && fun.equalityDeep(mergedFormats, lastOp.format) && fun.equalityDeep(mergedAttribution, lastOp.attribution)) {
|
|
832
|
+
lastOp.retain += len
|
|
833
|
+
} else if (len > 0) {
|
|
834
|
+
list.pushEnd(this.children, new RetainOp(len, mergedFormats, mergedAttribution))
|
|
835
|
+
}
|
|
836
|
+
return this
|
|
837
|
+
}
|
|
838
|
+
|
|
839
|
+
/**
|
|
840
|
+
* @param {number} len
|
|
841
|
+
* @return {this}
|
|
842
|
+
*/
|
|
843
|
+
delete (len) {
|
|
844
|
+
const lastOp = /** @type {DeleteOp|InsertOp<any>} */ (this.children.end)
|
|
845
|
+
if (lastOp instanceof DeleteOp) {
|
|
846
|
+
lastOp.delete += len
|
|
847
|
+
} else if (len > 0) {
|
|
848
|
+
list.pushEnd(this.children, new DeleteOp(len))
|
|
849
|
+
}
|
|
850
|
+
return this
|
|
851
|
+
}
|
|
852
|
+
|
|
853
|
+
/**
|
|
854
|
+
* @template {AllowedDeltaFromSchema<Schema> extends Delta<any,infer Attrs,any,any,any> ? (keyof Attrs) : never} Key
|
|
855
|
+
* @template {AllowedDeltaFromSchema<Schema> extends Delta<any,infer Attrs,any,any,any> ? (Attrs[Key]) : never} Val
|
|
856
|
+
* @param {Key} key
|
|
857
|
+
* @param {Val} val
|
|
858
|
+
* @param {Attribution?} attribution
|
|
859
|
+
* @param {Val|undefined} [prevValue]
|
|
860
|
+
* @return {Delta<
|
|
861
|
+
* NodeName,
|
|
862
|
+
* { [K in keyof AddToAttrs<Attrs,Key,Val>]: AddToAttrs<Attrs,Key,Val>[K] },
|
|
863
|
+
* Children,
|
|
864
|
+
* Text,
|
|
865
|
+
* Schema
|
|
866
|
+
* >}
|
|
867
|
+
*/
|
|
868
|
+
set (key, val, attribution = null, prevValue) {
|
|
869
|
+
this.attrs.set(key, /** @type {any} */ (new MapInsertOp(key, val, prevValue, mergeAttrs(this.usedAttribution, attribution))))
|
|
870
|
+
return /** @type {any} */ (this)
|
|
871
|
+
}
|
|
872
|
+
|
|
873
|
+
/**
|
|
874
|
+
* @template {AllowedDeltaFromSchema<Schema> extends Delta<any,infer Attrs,any,any,any> ? Attrs : never} NewAttrs
|
|
875
|
+
* @param {NewAttrs} attrs
|
|
876
|
+
* @param {Attribution?} attribution
|
|
877
|
+
* @return {Delta<
|
|
878
|
+
* NodeName,
|
|
879
|
+
* { [K in keyof MergeAttrs<Attrs,NewAttrs>]: MergeAttrs<Attrs,NewAttrs>[K] },
|
|
880
|
+
* Children,
|
|
881
|
+
* Text,
|
|
882
|
+
* Schema
|
|
883
|
+
* >}
|
|
884
|
+
*/
|
|
885
|
+
setMany (attrs, attribution = null) {
|
|
886
|
+
for (const k in attrs) {
|
|
887
|
+
this.set(/** @type {any} */ (k), attrs[k], attribution)
|
|
888
|
+
}
|
|
889
|
+
return /** @type {any} */ (this)
|
|
890
|
+
}
|
|
891
|
+
|
|
892
|
+
/**
|
|
893
|
+
* @template {AllowedDeltaFromSchema<Schema> extends Delta<any,infer As,any,any,any> ? keyof As : never} Key
|
|
894
|
+
* @param {Key} key
|
|
895
|
+
* @param {Attribution?} attribution
|
|
896
|
+
* @param {any} [prevValue]
|
|
897
|
+
* @return {Delta<
|
|
898
|
+
* NodeName,
|
|
899
|
+
* { [K in keyof AddToAttrs<Attrs,Key,never>]: AddToAttrs<Attrs,Key,never>[K] },
|
|
900
|
+
* Children,
|
|
901
|
+
* Text,
|
|
902
|
+
* Schema
|
|
903
|
+
* >}
|
|
904
|
+
*/
|
|
905
|
+
unset (key, attribution = null, prevValue) {
|
|
906
|
+
this.attrs.set(key, /** @type {any} */ (new MapDeleteOp(key, prevValue, mergeAttrs(this.usedAttribution, attribution))))
|
|
907
|
+
return this
|
|
908
|
+
}
|
|
909
|
+
|
|
910
|
+
/**
|
|
911
|
+
* @template {AllowedDeltaFromSchema<Schema> extends Delta<any,infer As,any,any,any> ? { [K in keyof As]: Extract<As[K],Delta<any,any,any,any,any>> extends never ? never : K }[keyof As] : never} Key
|
|
912
|
+
* @template {AllowedDeltaFromSchema<Schema> extends Delta<any,infer As,any,any,any> ? Extract<As[Key],Delta<any,any,any,any,any>> : never} D
|
|
913
|
+
* @param {Key} key
|
|
914
|
+
* @param {D} modify
|
|
915
|
+
* @return {Delta<
|
|
916
|
+
* NodeName,
|
|
917
|
+
* { [K in keyof AddToAttrs<Attrs,Key,D>]: AddToAttrs<Attrs,Key,D>[K] },
|
|
918
|
+
* Children,
|
|
919
|
+
* Text,
|
|
920
|
+
* Schema
|
|
921
|
+
* >}
|
|
922
|
+
*/
|
|
923
|
+
update (key, modify) {
|
|
924
|
+
this.attrs.set(key, /** @type {any} */ (new MapModifyOp(key, modify)))
|
|
925
|
+
return /** @type {any} */ (this)
|
|
926
|
+
}
|
|
927
|
+
|
|
928
|
+
/**
|
|
929
|
+
* @param {Delta<NodeName,Partial<Attrs>,Children,Text,any>} other
|
|
930
|
+
* @return {this}
|
|
931
|
+
*/
|
|
932
|
+
apply (other) {
|
|
933
|
+
this.$schema?.expect(other)
|
|
934
|
+
// apply attrs
|
|
935
|
+
;/** @type {Delta<NodeName,Attrs,Children,Text,any>} */ (/** @type {any} */ (other)).attrs.forEach(op => {
|
|
936
|
+
const c = this.attrs.get(op.key)
|
|
937
|
+
if ($modifyOp.check(op)) {
|
|
938
|
+
if ($deltaAny.check(c?.value)) {
|
|
939
|
+
/** @type {Delta} */ (c.value).apply(op.value)
|
|
940
|
+
} else {
|
|
941
|
+
// then this is a simple modify
|
|
942
|
+
this.attrs.set(op.key, /** @type {any} */ (op))
|
|
943
|
+
}
|
|
944
|
+
} else {
|
|
945
|
+
/** @type {MapInsertOp<any>} */ (op).prevValue = c?.value
|
|
946
|
+
this.attrs.set(op.key, /** @type {any} */ (op))
|
|
947
|
+
}
|
|
948
|
+
})
|
|
949
|
+
// apply children
|
|
950
|
+
let opsI = this.children.start
|
|
951
|
+
let offset = 0
|
|
952
|
+
other.children.forEach(op => {
|
|
953
|
+
if ($textOp.check(op) || $insertOp.check(op)) {
|
|
954
|
+
if (offset === 0) {
|
|
955
|
+
list.insertBetween(this.children, opsI == null ? this.children.end : opsI.prev, opsI, op.clone())
|
|
956
|
+
} else {
|
|
957
|
+
if (opsI == null) error.unexpectedCase()
|
|
958
|
+
const cpy = opsI.clone()
|
|
959
|
+
cpy._splice(0, offset)
|
|
960
|
+
opsI._splice(offset, opsI.length - offset)
|
|
961
|
+
list.insertBetween(this.children, opsI, opsI.next || null, cpy)
|
|
962
|
+
list.insertBetween(this.children, opsI, cpy || null, op)
|
|
963
|
+
offset = 0
|
|
964
|
+
}
|
|
965
|
+
} else if ($retainOp.check(op)) {
|
|
966
|
+
let skipLen = op.length
|
|
967
|
+
while (opsI != null && opsI.length - offset <= skipLen) {
|
|
968
|
+
skipLen -= opsI.length - offset
|
|
969
|
+
opsI = opsI?.next || null
|
|
970
|
+
offset = 0
|
|
971
|
+
}
|
|
972
|
+
if (opsI != null) {
|
|
973
|
+
offset += skipLen
|
|
974
|
+
} else {
|
|
975
|
+
list.pushEnd(this.children, new RetainOp(skipLen, op.format, op.attribution))
|
|
976
|
+
}
|
|
977
|
+
} else if ($deleteOp.check(op)) {
|
|
978
|
+
let remainingLen = op.delete
|
|
979
|
+
while (remainingLen > 0) {
|
|
980
|
+
if (opsI == null) {
|
|
981
|
+
list.pushEnd(this.children, new DeleteOp(remainingLen))
|
|
982
|
+
break
|
|
983
|
+
} else if (opsI instanceof DeleteOp) {
|
|
984
|
+
const delLen = opsI.length - offset
|
|
985
|
+
// the same content can't be deleted twice, remove duplicated deletes
|
|
986
|
+
if (delLen >= remainingLen) {
|
|
987
|
+
offset = 0
|
|
988
|
+
opsI = opsI.next
|
|
989
|
+
} else {
|
|
990
|
+
offset += remainingLen
|
|
991
|
+
}
|
|
992
|
+
remainingLen -= delLen
|
|
993
|
+
} else { // insert / embed / retain / modify ⇒ replace
|
|
994
|
+
// case1: delete o fully
|
|
995
|
+
// case2: delete some part of beginning
|
|
996
|
+
// case3: delete some part of end
|
|
997
|
+
// case4: delete some part of center
|
|
998
|
+
const delLen = math.min(opsI.length - offset, remainingLen)
|
|
999
|
+
if (opsI.length === delLen) {
|
|
1000
|
+
// case 1
|
|
1001
|
+
offset = 0
|
|
1002
|
+
list.remove(this.children, opsI)
|
|
1003
|
+
} else if (offset === 0) {
|
|
1004
|
+
// case 2
|
|
1005
|
+
offset = 0
|
|
1006
|
+
opsI._splice(0, delLen)
|
|
1007
|
+
} else if (offset + delLen === opsI.length) {
|
|
1008
|
+
// case 3
|
|
1009
|
+
opsI._splice(offset, delLen)
|
|
1010
|
+
offset = 0
|
|
1011
|
+
opsI = opsI.next
|
|
1012
|
+
} else {
|
|
1013
|
+
// case 4
|
|
1014
|
+
opsI._splice(offset, delLen)
|
|
1015
|
+
}
|
|
1016
|
+
remainingLen -= delLen
|
|
1017
|
+
}
|
|
1018
|
+
}
|
|
1019
|
+
} else if ($modifyOp.check(op)) {
|
|
1020
|
+
if (opsI == null) {
|
|
1021
|
+
list.pushEnd(this.children, op.clone())
|
|
1022
|
+
return
|
|
1023
|
+
}
|
|
1024
|
+
if ($modifyOp.check(opsI)) {
|
|
1025
|
+
opsI.modify.apply(op.modify)
|
|
1026
|
+
} else if ($textOp.check(opsI) || $insertOp.check(opsI)) {
|
|
1027
|
+
const d = opsI.insert[offset]
|
|
1028
|
+
if (!$deltaAny.check(d)) {
|
|
1029
|
+
// probably incompatible delta. can only modify deltas
|
|
1030
|
+
error.unexpectedCase()
|
|
1031
|
+
}
|
|
1032
|
+
d.apply(op.modify)
|
|
1033
|
+
} else if ($retainOp.check(opsI)) {
|
|
1034
|
+
if (offset > 0) {
|
|
1035
|
+
const cpy = opsI.clone()
|
|
1036
|
+
cpy._splice(offset, opsI.length - offset) // skipped len
|
|
1037
|
+
opsI._splice(0, offset) // new remainder
|
|
1038
|
+
list.insertBetween(this.children, opsI.prev, opsI, cpy) // insert skipped len
|
|
1039
|
+
offset = 0
|
|
1040
|
+
}
|
|
1041
|
+
// not deleting opsI, because current idea is that modify should not
|
|
1042
|
+
// advance content
|
|
1043
|
+
list.insertBetween(this.children, opsI.prev, opsI, op.clone()) // insert skipped len
|
|
1044
|
+
if (opsI.length === 1) {
|
|
1045
|
+
list.remove(this.children, opsI)
|
|
1046
|
+
} else {
|
|
1047
|
+
opsI._splice(0, 1)
|
|
1048
|
+
}
|
|
1049
|
+
} else if ($deleteOp.check(opsI)) {
|
|
1050
|
+
// nop
|
|
1051
|
+
} else {
|
|
1052
|
+
error.unexpectedCase()
|
|
1053
|
+
}
|
|
1054
|
+
} else {
|
|
1055
|
+
error.unexpectedCase()
|
|
1056
|
+
}
|
|
1057
|
+
})
|
|
1058
|
+
return this
|
|
1059
|
+
}
|
|
1060
|
+
|
|
1061
|
+
/**
|
|
1062
|
+
* @param {Delta<any,any,any,any,any>} other
|
|
1063
|
+
* @param {boolean} priority
|
|
1064
|
+
*/
|
|
1065
|
+
rebase (other, priority) {
|
|
1066
|
+
// @todo rebase children
|
|
1067
|
+
/**
|
|
1068
|
+
* Rebase attributes
|
|
1069
|
+
*
|
|
1070
|
+
* - insert vs delete ⇒ insert takes precedence
|
|
1071
|
+
* - insert vs modify ⇒ insert takes precedence
|
|
1072
|
+
* - insert vs insert ⇒ priority decides
|
|
1073
|
+
* - delete vs modify ⇒ delete takes precedence
|
|
1074
|
+
* - delete vs delete ⇒ current delete op is removed because item has already been deleted
|
|
1075
|
+
* - modify vs modify ⇒ rebase using priority
|
|
1076
|
+
*/
|
|
1077
|
+
this.attrs.forEach(op => {
|
|
1078
|
+
if ($insertOp.check(op)) {
|
|
1079
|
+
if ($insertOp.check(other.attrs.get(op.key)) && !priority) {
|
|
1080
|
+
this.attrs.delete(op.key)
|
|
1081
|
+
}
|
|
1082
|
+
} else if ($deleteOp.check(op)) {
|
|
1083
|
+
const otherOp = other.attrs.get(op.key)
|
|
1084
|
+
if ($insertOp.check(otherOp)) {
|
|
1085
|
+
this.attrs.delete(otherOp.key)
|
|
1086
|
+
}
|
|
1087
|
+
} else if ($modifyOp.check(op)) {
|
|
1088
|
+
const otherOp = other.attrs.get(op.key)
|
|
1089
|
+
if (otherOp == null) {
|
|
1090
|
+
// nop
|
|
1091
|
+
} else if ($modifyOp.check(otherOp)) {
|
|
1092
|
+
op.value.rebase(otherOp.value, priority)
|
|
1093
|
+
} else {
|
|
1094
|
+
this.attrs.delete(otherOp.key)
|
|
1095
|
+
}
|
|
1096
|
+
}
|
|
1097
|
+
})
|
|
1098
|
+
return this
|
|
1099
|
+
}
|
|
1100
|
+
|
|
1101
|
+
done () {
|
|
1102
|
+
const cs = this.children
|
|
1103
|
+
for (let end = cs.end; end !== null && $retainOp.check(end) && end.format == null; end = cs.end) {
|
|
1104
|
+
list.popEnd(cs)
|
|
1105
|
+
}
|
|
1106
|
+
return this
|
|
1107
|
+
}
|
|
1108
|
+
}
|
|
1109
|
+
|
|
1110
|
+
/**
|
|
1111
|
+
* @template {string} NodeName
|
|
1112
|
+
* @template {{ [key: string|number|symbol]: any }} [Attrs={}]
|
|
1113
|
+
* @template {any} [Children=never]
|
|
1114
|
+
* @template {string|never} [Text=never]
|
|
1115
|
+
* @typedef {Delta<NodeName,Attrs,Children|RecursiveDelta<NodeName,Attrs,Children>,Text>} RecursiveDelta
|
|
1116
|
+
*/
|
|
1117
|
+
|
|
1118
|
+
/**
|
|
1119
|
+
* @template {string} [NodeName=any]
|
|
1120
|
+
* @template {{ [key: string|number|symbol]: any }} [Attrs={}]
|
|
1121
|
+
* @template {any} [Children=never]
|
|
1122
|
+
* @template {boolean} [HasText=false]
|
|
1123
|
+
* @template {boolean} [Recursive=false]
|
|
1124
|
+
* @param {object} opts
|
|
1125
|
+
* @param {s.Schema<NodeName>?} [opts.name]
|
|
1126
|
+
* @param {s.Schema<Attrs>?} [opts.attrs]
|
|
1127
|
+
* @param {s.Schema<Children>?} [opts.children]
|
|
1128
|
+
* @param {HasText} [opts.hasText]
|
|
1129
|
+
* @param {Recursive} [opts.recursive]
|
|
1130
|
+
* @return {s.Schema<Delta<
|
|
1131
|
+
* NodeName,
|
|
1132
|
+
* Attrs,
|
|
1133
|
+
* Children|(Recursive extends true ? RecursiveDelta<NodeName,Attrs,Children,HasText extends true ? string : never> : never),
|
|
1134
|
+
* HasText extends true ? string : never
|
|
1135
|
+
* >>}
|
|
1136
|
+
*/
|
|
1137
|
+
export const $delta = ({ name, attrs, children, hasText, recursive }) => {
|
|
1138
|
+
name = name == null ? s.$any : name
|
|
1139
|
+
/**
|
|
1140
|
+
* @type {s.Schema<Array<any>>}
|
|
1141
|
+
*/
|
|
1142
|
+
let $arrContent = children == null ? s.$never : s.$array(children)
|
|
1143
|
+
const $attrsPartial = attrs == null ? s.$object({}) : (s.$$object.check(attrs) ? attrs.partial : attrs)
|
|
1144
|
+
const $d = s.$instanceOf(Delta, /** @param {Delta<any,any,any,any,any>} d */ d => {
|
|
1145
|
+
if (
|
|
1146
|
+
!name.check(d.name) ||
|
|
1147
|
+
Array.from(d.attrs.entries()).some(
|
|
1148
|
+
([k, op]) => $insertOp.check(op) && !$attrsPartial.check({ [k]: op.value })
|
|
1149
|
+
)
|
|
1150
|
+
) return false
|
|
1151
|
+
for (const op of d.children) {
|
|
1152
|
+
if ((!hasText && $textOp.check(op)) || ($insertOp.check(op) && !$arrContent.check(op.insert))) {
|
|
1153
|
+
return false
|
|
1154
|
+
}
|
|
1155
|
+
}
|
|
1156
|
+
return true
|
|
1157
|
+
})
|
|
1158
|
+
if (recursive) {
|
|
1159
|
+
$arrContent = children == null ? s.$array($d) : s.$array(children, $d)
|
|
1160
|
+
}
|
|
1161
|
+
return /** @type {any} */ ($d)
|
|
1162
|
+
}
|
|
1163
|
+
|
|
1164
|
+
export const $deltaAny = s.$instanceOf(Delta)
|
|
1165
|
+
|
|
1166
|
+
/**
|
|
1167
|
+
* Helper function to merge attribution and attributes. The latter input "wins".
|
|
1168
|
+
*
|
|
1169
|
+
* @template {{ [key: string]: any }} T
|
|
1170
|
+
* @param {T | null} a
|
|
1171
|
+
* @param {T | null} b
|
|
1172
|
+
*/
|
|
1173
|
+
export const mergeAttrs = (a, b) => object.isEmpty(a) ? b : (object.isEmpty(b) ? a : object.assign({}, a, b))
|
|
1174
|
+
|
|
1175
|
+
/**
|
|
1176
|
+
* @template {Delta?} D
|
|
1177
|
+
* @param {D} a
|
|
1178
|
+
* @param {D} b
|
|
1179
|
+
* @return {D}
|
|
1180
|
+
*/
|
|
1181
|
+
export const mergeDeltas = (a, b) => {
|
|
1182
|
+
if (a != null && b != null) {
|
|
1183
|
+
const c = /** @type {Exclude<D,null>} */ (a.clone())
|
|
1184
|
+
c.apply(b)
|
|
1185
|
+
return c
|
|
1186
|
+
}
|
|
1187
|
+
return a == null ? b : (a || null)
|
|
1188
|
+
}
|
|
1189
|
+
|
|
1190
|
+
/**
|
|
1191
|
+
* @overload
|
|
1192
|
+
* @return {Delta<any,{},never,never,null>}
|
|
1193
|
+
*/
|
|
1194
|
+
/**
|
|
1195
|
+
* @template {string} NodeName
|
|
1196
|
+
* @overload
|
|
1197
|
+
* @param {NodeName} nodeName
|
|
1198
|
+
* @return {Delta<NodeName,{},never,never,null>}
|
|
1199
|
+
*/
|
|
1200
|
+
/**
|
|
1201
|
+
* @template {string} NodeName
|
|
1202
|
+
* @template {s.Schema<Delta<any,any,any,any,any>>} Schema
|
|
1203
|
+
* @overload
|
|
1204
|
+
* @param {NodeName} nodeName
|
|
1205
|
+
* @param {Schema} schema
|
|
1206
|
+
* @return {Schema extends s.Schema<Delta<infer N,infer Attrs,infer Children,infer Text,any>> ? Delta<NodeName,Attrs,Children,Text,Schema> : never}
|
|
1207
|
+
*/
|
|
1208
|
+
/**
|
|
1209
|
+
* @template {s.Schema<Delta<any,any,any,any,any>>} Schema
|
|
1210
|
+
* @overload
|
|
1211
|
+
* @param {Schema} schema
|
|
1212
|
+
* @return {Schema extends s.Schema<Delta<infer N,infer Attrs,infer Children,infer Text,any>> ? Delta<N,Attrs,Children,Text,Schema> : never}
|
|
1213
|
+
*/
|
|
1214
|
+
/**
|
|
1215
|
+
* @template {string|null} NodeName
|
|
1216
|
+
* @template {{[k:string|number|symbol]:any}|null} Attrs
|
|
1217
|
+
* @template {Array<Array<any>|string>} Children
|
|
1218
|
+
* @overload
|
|
1219
|
+
* @param {NodeName} nodeName
|
|
1220
|
+
* @param {Attrs} attrs
|
|
1221
|
+
* @param {...Children} children
|
|
1222
|
+
* @return {Delta<
|
|
1223
|
+
* NodeName extends null ? any : NodeName,
|
|
1224
|
+
* Attrs extends null ? {} : Attrs,
|
|
1225
|
+
* Extract<Children[number],Array<any>> extends Array<infer Ac> ? (unknown extends Ac ? never : Ac) : never,
|
|
1226
|
+
* Extract<Children[number],string>,
|
|
1227
|
+
* null
|
|
1228
|
+
* >}
|
|
1229
|
+
*/
|
|
1230
|
+
/**
|
|
1231
|
+
* @param {string|s.Schema<Delta<any,any,any,any,any>>} [nodeNameOrSchema]
|
|
1232
|
+
* @param {{[K:string|number|symbol]:any}|s.Schema<Delta<any,any,any,any,any>>} [attrsOrSchema]
|
|
1233
|
+
* @param {Array<Array<any>|string>} children
|
|
1234
|
+
* @return {Delta<any,any,any,any,any>}
|
|
1235
|
+
*/
|
|
1236
|
+
export const create = (nodeNameOrSchema, attrsOrSchema, ...children) => {
|
|
1237
|
+
const nodeName = /** @type {any} */ (s.$string.check(nodeNameOrSchema) ? nodeNameOrSchema : null)
|
|
1238
|
+
const schema = /** @type {any} */ (s.$$schema.check(nodeNameOrSchema) ? nodeNameOrSchema : (s.$$schema.check(attrsOrSchema) ? attrsOrSchema : null))
|
|
1239
|
+
const d = /** @type {Delta<any,any,any,string,null>} */ (new Delta(nodeName, schema))
|
|
1240
|
+
if (s.$objectAny.check(attrsOrSchema)) {
|
|
1241
|
+
d.setMany(attrsOrSchema)
|
|
1242
|
+
}
|
|
1243
|
+
if (s.$arrayAny.check(children)) {
|
|
1244
|
+
children.forEach(v => {
|
|
1245
|
+
d.insert(v)
|
|
1246
|
+
})
|
|
1247
|
+
}
|
|
1248
|
+
return d
|
|
1249
|
+
}
|
|
1250
|
+
|
|
1251
|
+
// DELTA TEXT
|
|
1252
|
+
|
|
1253
|
+
/**
|
|
1254
|
+
* @template [Embeds=never]
|
|
1255
|
+
* @typedef {Delta<any,{},Embeds,string>} TextDelta
|
|
1256
|
+
*/
|
|
1257
|
+
|
|
1258
|
+
/**
|
|
1259
|
+
* @template {Array<s.Schema<any>>} [$Embeds=any]
|
|
1260
|
+
* @param {$Embeds} $embeds
|
|
1261
|
+
* @return {s.Schema<TextDelta<_AnyToNull<$Embeds> extends null ? never : ($Embeds extends Array<s.Schema<infer $C>> ? $C : never)>>}
|
|
1262
|
+
*/
|
|
1263
|
+
export const $text = (...$embeds) => /** @type {any} */ ($delta({ children: s.$union(...$embeds), hasText: true }))
|
|
1264
|
+
export const $textOnly = $text()
|
|
1265
|
+
|
|
1266
|
+
/**
|
|
1267
|
+
* @template {s.Schema<Delta<any,{},any,any,null>>} [Schema=s.Schema<Delta<any,{},never,string,null>>]
|
|
1268
|
+
* @param {Schema} [$schema]
|
|
1269
|
+
* @return {Schema extends s.Schema<Delta<infer N,infer Attrs,infer Children,infer Text,any>> ? Delta<N,Attrs,Children,Text,Schema> : never}
|
|
1270
|
+
*/
|
|
1271
|
+
export const text = $schema => /** @type {any} */ (create($schema || $textOnly))
|
|
1272
|
+
|
|
1273
|
+
/**
|
|
1274
|
+
* @template {any} Children
|
|
1275
|
+
* @typedef {Delta<any,{},Children,never>} ArrayDelta
|
|
1276
|
+
*/
|
|
1277
|
+
|
|
1278
|
+
/**
|
|
1279
|
+
* @template {s.Schema<any>} $Children
|
|
1280
|
+
* @param {$Children} [$children]
|
|
1281
|
+
* @return {s.Schema<ArrayDelta<$Children>>}
|
|
1282
|
+
*/
|
|
1283
|
+
export const $array = $children => $delta({ children: $children })
|
|
1284
|
+
|
|
1285
|
+
/**
|
|
1286
|
+
* @template {s.Schema<ArrayDelta<any>>} [$Schema=never]
|
|
1287
|
+
* @param {$Schema} $schema
|
|
1288
|
+
* @return {$Schema extends never ? ArrayDelta<never> : Delta<any,{},never,never,$Schema>}
|
|
1289
|
+
*/
|
|
1290
|
+
export const array = $schema => /** @type {any} */ ($schema ? create($schema) : create())
|
|
1291
|
+
|
|
1292
|
+
/**
|
|
1293
|
+
* @template {{ [K: string|number|symbol]: any }} Attrs
|
|
1294
|
+
* @typedef {Delta<any,Attrs,never,never>} MapDelta
|
|
1295
|
+
*/
|
|
1296
|
+
|
|
1297
|
+
/**
|
|
1298
|
+
* @template {{ [K: string|number|symbol]: any }} $Attrs
|
|
1299
|
+
* @param {s.Schema<$Attrs>} $attrs
|
|
1300
|
+
* @return {s.Schema<MapDelta<$Attrs>>}
|
|
1301
|
+
*/
|
|
1302
|
+
export const $map = $attrs => /** @type {any} */ ($delta({ attrs: $attrs }))
|
|
1303
|
+
|
|
1304
|
+
/**
|
|
1305
|
+
* @template {s.Schema<MapDelta<any>>|undefined} [$Schema=undefined]
|
|
1306
|
+
* @param {$Schema} [$schema]
|
|
1307
|
+
* @return {$Schema extends s.Schema<MapDelta<infer Attrs>> ? Delta<any,Attrs,never,never,$Schema> : MapDelta<{}>}
|
|
1308
|
+
*/
|
|
1309
|
+
export const map = $schema => /** @type {any} */ (create(/** @type {any} */ ($schema)))
|