Skip to content

Commit 418f9ff

Browse files
committed
[schema|delta] $type global type check and use in delta
1 parent 8a0ac3a commit 418f9ff

File tree

6 files changed

+2205
-811
lines changed

6 files changed

+2205
-811
lines changed

delta/delta.js

Lines changed: 63 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,10 @@ export class TextOp extends list.ListNode {
131131
this._fingerprint = null
132132
}
133133

134+
get $type () {
135+
return $textOp
136+
}
137+
134138
/**
135139
* @param {string} newVal
136140
*/
@@ -227,6 +231,10 @@ export class InsertOp extends list.ListNode {
227231
this._fingerprint = null
228232
}
229233

234+
get $type () {
235+
return $insertOp
236+
}
237+
230238
/**
231239
* @param {ArrayContent} newVal
232240
*/
@@ -332,6 +340,10 @@ export class DeleteOp extends list.ListNode {
332340
this._fingerprint = null
333341
}
334342

343+
get $type () {
344+
return $deleteOp
345+
}
346+
335347
/**
336348
* @return {'delete'}
337349
*/
@@ -416,6 +428,10 @@ export class RetainOp extends list.ListNode {
416428
this._fingerprint = null
417429
}
418430

431+
get $type () {
432+
return $retainOp
433+
}
434+
419435
/**
420436
* @return {'retain'}
421437
*/
@@ -502,6 +518,10 @@ export class ModifyOp extends list.ListNode {
502518
this._fingerprint = null
503519
}
504520

521+
get $type () {
522+
return $modifyOp
523+
}
524+
505525
/**
506526
* @return {'modify'}
507527
*/
@@ -609,6 +629,10 @@ export class SetAttrOp {
609629
this._fingerprint = null
610630
}
611631

632+
get $type () {
633+
return $setAttrOp
634+
}
635+
612636
/**
613637
* @return {'insert'}
614638
*/
@@ -695,13 +719,17 @@ export class DeleteAttrOp {
695719
this._fingerprint = null
696720
}
697721

698-
get value () { return undefined }
722+
get $type () {
723+
return $deleteAttrOp
724+
}
699725

700726
/**
701727
* @type {'delete'}
702728
*/
703729
get type () { return 'delete' }
704730

731+
get value () { return undefined }
732+
705733
get fingerprint () {
706734
return this._fingerprint || (this._fingerprint = buffer.toBase64(encoding.encode(encoder => {
707735
encoding.writeVarUint(encoder, 6) // map delete type: 6
@@ -757,6 +785,10 @@ export class ModifyAttrOp {
757785
this._fingerprint = null
758786
}
759787

788+
get $type () {
789+
return $modifyAttrOp
790+
}
791+
760792
/**
761793
* @type {'modify'}
762794
*/
@@ -808,78 +840,45 @@ export class ModifyAttrOp {
808840
}
809841
}
810842

811-
/**
812-
* @type {s.Schema<DeleteOp>}
813-
*/
814-
export const $deleteOp = s.$constructedBy(DeleteOp)
815-
export const $deleteAttrOp = /** @type {s.Schema<DeleteAttrOp<{},string|number>>} */ (s.$constructedBy(DeleteAttrOp))
816-
817-
/**
818-
* @type {s.Schema<InsertOp<any>>}
819-
*/
820-
export const $insertOp = s.$constructedBy(InsertOp)
843+
export const $insertOp = /** @type {s.Schema<InsertOp<any>>} */ (s.$type('insertOp'))
844+
export const $modifyOp = /** @type {s.Schema<ModifyOp>} */ (s.$type('modifyOp'))
845+
export const $textOp = /** @type {s.Schema<TextOp>} */ (s.$type('textOp'))
846+
export const $deleteOp = /** @type {s.Schema<DeleteOp<any>>} */ (s.$type('deleteOp'))
847+
export const $retainOp = /** @type {s.Schema<RetainOp>} */ (s.$type('retainOp'))
848+
export const $anyOp = s.$union($insertOp, $deleteOp, $textOp, $modifyOp)
821849

822-
/**
823-
* @type {s.Schema<SetAttrOp<any>>}
824-
*/
825-
export const $setAttrOp = s.$constructedBy(SetAttrOp)
850+
export const $setAttrOp = /** @type {s.Schema<SetAttrOp<any>>} */ (s.$type('setAttrOp'))
851+
export const $modifyAttrOp = /** @type {s.Schema<ModifyAttrOp<any,string|number>>} */ (s.$type('modifyAttrOp'))
852+
export const $deleteAttrOp = /** @type {s.Schema<DeleteAttrOp<{},string|number>>} */ (s.$type('deleteAttrOp'))
853+
export const $anyAttrOp = s.$union($setAttrOp, $deleteAttrOp, $modifyAttrOp)
826854

827855
/**
828856
* @template {fingerprintTrait.Fingerprintable} Content
829857
* @param {s.Schema<Content>} $content
830858
* @return {s.Schema<SetAttrOp<Content>>}
831859
*/
832-
export const $setAttrOpWith = $content => /** @type {any} */ (s.$constructedBy(SetAttrOp, o => $content.check(o.value)))
860+
export const $setAttrOpWith = $content => s.$custom(o => $setAttrOp.check(o) && $content.check(o.value))
833861

834862
/**
835863
* @template {fingerprintTrait.Fingerprintable} Content
836864
* @param {s.Schema<Content>} $content
837865
* @return {s.Schema<InsertOp<Content>>}
838866
*/
839-
export const $insertOpWith = $content => /** @type {any} */ (s.$constructedBy(InsertOp, o => $content.check(o.insert.every(ins => $content.check(ins)))))
840-
841-
/**
842-
* @type {s.Schema<TextOp>}
843-
*/
844-
export const $textOp = s.$constructedBy(TextOp)
845-
846-
/**
847-
* @type {s.Schema<RetainOp>}
848-
*/
849-
export const $retainOp = s.$constructedBy(RetainOp)
850-
851-
/**
852-
* @type {s.Schema<ModifyAttrOp<any,string|number>>}
853-
*/
854-
export const $modifyAttrOp = s.$constructedBy(ModifyAttrOp)
855-
856-
/**
857-
* @type {s.Schema<ModifyOp>}
858-
*/
859-
export const $modifyOp = s.$constructedBy(ModifyOp)
867+
export const $insertOpWith = $content => s.$custom(o => $insertOp.check(o) && $content.check(o.insert.every(ins => $content.check(ins))))
860868

861869
/**
862870
* @template {DeltaAny} Modify
863871
* @param {s.Schema<Modify>} $content
864-
* @return {s.Schema<ModifyAttrOp<Modify> | ModifyOp<Modify>>}
872+
* @return {s.Schema<ModifyOp<Modify>>}
865873
*/
866-
export const $modifyOpWith = $content => s.$custom(o =>
867-
o != null && (
868-
(o.constructor === ModifyAttrOp && $content.check(/** @type {ModifyAttrOp<Modify>} */ (o).value)) ||
869-
(o.constructor === ModifyOp && $content.check(/** @type {ModifyOp<Modify>} */ (o).value))
870-
)
871-
)
872-
873-
export const $anyOp = s.$union($insertOp, $deleteOp, $textOp, $modifyOp)
874-
export const $anyAttrOp = s.$union($setAttrOp, $deleteAttrOp, $modifyAttrOp)
874+
export const $modifyOpWith = $content => s.$custom(o => $modifyOp.check(o) && $content.check(o.value))
875875

876876
/**
877-
* @template {Array<any>|string} C1
878-
* @template {Array<any>|string} C2
879-
* @typedef {Extract<C1 | C2, Array<any>> extends never
880-
* ? never
881-
* : (Array<(Extract<C1 | C2,Array<any>> extends Array<infer AC1> ? (unknown extends AC1 ? never : AC1) : never)>)} MergeListArrays
877+
* @template {DeltaAny} Modify
878+
* @param {s.Schema<Modify>} $content
879+
* @return {s.Schema<ModifyAttrOp<Modify>>}
882880
*/
881+
export const $modifyAttrOpWith = $content => s.$custom(o => $modifyAttrOp.check(o) && $content.check(/** @type {ModifyAttrOp<Modify>} */ (o).value))
883882

884883
/**
885884
* @template {{[Key in string|number]: any}} Attrs
@@ -894,19 +893,10 @@ export const $anyAttrOp = s.$union($setAttrOp, $deleteAttrOp, $modifyAttrOp)
894893
* @typedef {{ [K in (keyof NewAttrs | keyof Attrs)]: (unknown extends Attrs[K] ? never : Attrs[K]) | (unknown extends NewAttrs[K] ? never : NewAttrs[K]) }} MergeAttrs
895894
*/
896895

897-
/**
898-
* @template X
899-
* @typedef {0 extends (1 & X) ? null : X} _AnyToNull
900-
*/
901-
902-
/**
903-
* @template {s.Schema<Delta<any>>|null} Schema
904-
* @typedef {_AnyToNull<Schema> extends null ? Delta<{ name:any,attrs:{[key:string|number]:any},children:any,text:any }> : (Schema extends s.Schema<infer D> ? D : never)} AllowedDeltaFromSchema
905-
*/
906-
907896
/**
908897
* @typedef {Delta<any>} DeltaAny
909898
*/
899+
910900
/**
911901
* @typedef {DeltaBuilder<any>} DeltaBuilderAny
912902
*/
@@ -942,10 +932,6 @@ export const $anyAttrOp = s.$union($setAttrOp, $deleteAttrOp, $modifyAttrOp)
942932
* @typedef {0 extends (1 & DConf) ? string : (DConf extends {text:true} ? string : never)} DeltaConfGetText
943933
*/
944934

945-
/**
946-
* @typedef {DeltaConfGetAttrs<any>} QQ
947-
*/
948-
949935
/**
950936
* @template {DeltaConf} DConf
951937
* @typedef {import('../ts.js').TypeIsAny<DConf, {[K:string|number]:any}, (DConf extends {attrs:infer Attrs} ? (Attrs extends undefined ? {} : Attrs) : {})>} DeltaConfGetAttrs
@@ -972,6 +958,8 @@ export const $anyAttrOp = s.$union($setAttrOp, $deleteAttrOp, $modifyAttrOp)
972958
*/
973959

974960
/**
961+
* Transform Delta(Builder) to a normal delta.
962+
*
975963
* @template V
976964
* @typedef {V extends never ? never : (import('../ts.js').TypeIsAny<V,any,V extends Delta<infer DConf> ? Delta<DConf> : V>)} _SanifyDelta
977965
*/
@@ -1044,6 +1032,7 @@ class DeltaData {
10441032
* >}
10451033
*/
10461034
export class Delta extends DeltaData {
1035+
get $type () { return $deltaAny }
10471036
/**
10481037
* @type {string}
10491038
*/
@@ -1400,7 +1389,7 @@ export class DeltaBuilder extends Delta {
14001389
const mergedFormats = mergeAttrs(this.usedAttributes, format)
14011390
const mergedAttribution = mergeAttrs(this.usedAttribution, attribution)
14021391
const lastOp = /** @type {RetainOp|InsertOp<any>} */ (this.children.end)
1403-
if (lastOp instanceof RetainOp && fun.equalityDeep(mergedFormats, lastOp.format) && fun.equalityDeep(mergedAttribution, lastOp.attribution)) {
1392+
if ($retainOp.check(lastOp) && fun.equalityDeep(mergedFormats, lastOp.format) && fun.equalityDeep(mergedAttribution, lastOp.attribution)) {
14041393
// @ts-ignore
14051394
lastOp.retain += len
14061395
} else if (len > 0) {
@@ -1416,7 +1405,7 @@ export class DeltaBuilder extends Delta {
14161405
delete (len) {
14171406
modDeltaCheck(this)
14181407
const lastOp = /** @type {DeleteOp<any>|InsertOp<any>} */ (this.children.end)
1419-
if (lastOp instanceof DeleteOp) {
1408+
if ($deleteOp.check(lastOp)) {
14201409
lastOp.delete += len
14211410
} else if (len > 0) {
14221411
list.pushEnd(this.children, new DeleteOp(len))
@@ -1600,7 +1589,7 @@ export class DeltaBuilder extends Delta {
16001589
list.pushEnd(this.children, scheduleForMerge(new DeleteOp(remainingLen)))
16011590
this.childCnt += remainingLen
16021591
break
1603-
} else if (opsI instanceof DeleteOp) {
1592+
} else if ($deleteOp.check(opsI)) {
16041593
const delLen = opsI.length - offset
16051594
// the same content can't be deleted twice, remove duplicated deletes
16061595
if (delLen >= remainingLen) {
@@ -1934,7 +1923,7 @@ export class $Delta extends s.Schema {
19341923
*/
19351924
check (o, err = undefined) {
19361925
const { $name, $attrs, $children, hasText, $formats } = this.shape
1937-
if (!(o instanceof Delta)) {
1926+
if (!$deltaAny.check(o, err)) {
19381927
err?.extend(null, 'Delta', o?.constructor.name, 'Constructor match failed')
19391928
} else if (o.name != null && !$name.check(o.name, err)) {
19401929
err?.extend('Delta.name', $name.toString(), o.name, 'Unexpected node name')
@@ -2010,8 +1999,9 @@ export const _$delta = ({ name, attrs, children, text, recursive }) => {
20101999
let $arrContent = children == null ? s.$never : s.$array(s.$(children))
20112000
const $name = name == null ? s.$any : s.$(name)
20122001
const $attrsPartial = attrs == null ? s.$object({}) : (s.$$record.check(attrs) ? attrs : /** @type {any} */ (s.$(attrs)).partial)
2013-
const $d = s.$instanceOf(Delta, /** @param {Delta<any>} d */ d => {
2002+
const $d = s.$custom(d => {
20142003
if (
2004+
!$deltaAny.check(d) ||
20152005
!$name.check(d.name) ||
20162006
object.some(d.attrs,
20172007
(op, k) => $setAttrOp.check(op) && !$attrsPartial.check({ [k]: op.value })
@@ -2030,15 +2020,8 @@ export const _$delta = ({ name, attrs, children, text, recursive }) => {
20302020
return /** @type {any} */ ($d)
20312021
}
20322022

2033-
/**
2034-
* @type {s.Schema<DeltaAny>}
2035-
*/
2036-
export const $deltaAny = /** @type {any} */ (s.$instanceOf(Delta))
2037-
2038-
/**
2039-
* @type {s.Schema<DeltaBuilderAny>}
2040-
*/
2041-
export const $deltaBuilderAny = /** @type {any} */ (s.$instanceOf(DeltaBuilder))
2023+
export const $deltaAny = /** @type {s.Schema<DeltaAny>} */ (s.$type('delta'))
2024+
export const $deltaBuilderAny = /** @type {s.Schema<DeltaBuilderAny>} */ (s.$custom(o => $deltaAny.check(o) && !o.isDone))
20422025

20432026
/**
20442027
* Helper function to merge attribution and attributes. The latter input "wins".

environment.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,3 +150,12 @@ export const supportsColor = forceColor || (
150150
)
151151
)
152152
/* c8 ignore stop */
153+
154+
const globalScope = /** @type {any} */ (typeof globalThis !== 'undefined'
155+
? globalThis
156+
: typeof window !== 'undefined'
157+
? window
158+
// @ts-ignore
159+
: typeof global !== 'undefined' ? global : {})
160+
161+
export { globalScope as global }

0 commit comments

Comments
 (0)