@voidhash/mimic 0.0.2 → 0.0.3
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/.turbo/turbo-build.log +257 -42
- package/dist/Document.cjs +152 -0
- package/dist/Document.d.cts +67 -0
- package/dist/Document.d.cts.map +1 -0
- package/dist/Document.d.mts +68 -0
- package/dist/Document.d.mts.map +1 -0
- package/dist/Document.mjs +147 -0
- package/dist/Document.mjs.map +1 -0
- package/dist/EffectSchema.cjs +180 -0
- package/dist/EffectSchema.d.cts +84 -0
- package/dist/EffectSchema.d.cts.map +1 -0
- package/dist/EffectSchema.d.mts +84 -0
- package/dist/EffectSchema.d.mts.map +1 -0
- package/dist/EffectSchema.mjs +176 -0
- package/dist/EffectSchema.mjs.map +1 -0
- package/dist/FractionalIndex.cjs +365 -0
- package/dist/FractionalIndex.mjs +364 -0
- package/dist/FractionalIndex.mjs.map +1 -0
- package/dist/Operation.cjs +53 -0
- package/dist/Operation.d.cts +39 -0
- package/dist/Operation.d.cts.map +1 -0
- package/dist/Operation.d.mts +39 -0
- package/dist/Operation.d.mts.map +1 -0
- package/dist/Operation.mjs +46 -0
- package/dist/Operation.mjs.map +1 -0
- package/dist/OperationDefinition.cjs +13 -0
- package/dist/OperationDefinition.d.cts +12 -0
- package/dist/OperationDefinition.d.cts.map +1 -0
- package/dist/OperationDefinition.d.mts +12 -0
- package/dist/OperationDefinition.d.mts.map +1 -0
- package/dist/OperationDefinition.mjs +13 -0
- package/dist/OperationDefinition.mjs.map +1 -0
- package/dist/OperationPath.cjs +148 -0
- package/dist/OperationPath.d.cts +60 -0
- package/dist/OperationPath.d.cts.map +1 -0
- package/dist/OperationPath.d.mts +60 -0
- package/dist/OperationPath.d.mts.map +1 -0
- package/dist/OperationPath.mjs +138 -0
- package/dist/OperationPath.mjs.map +1 -0
- package/dist/{Presence-gWrmGBeu.cjs → Presence.cjs} +4 -39
- package/dist/{Presence-N8u7Eppr.d.mts → Presence.d.cts} +2 -2
- package/dist/Presence.d.cts.map +1 -0
- package/dist/{Presence-DKKP4v5X.d.cts → Presence.d.mts} +2 -2
- package/dist/Presence.d.mts.map +1 -0
- package/dist/{Presence-DdMVKcOv.mjs → Presence.mjs} +3 -28
- package/dist/Presence.mjs.map +1 -0
- package/dist/Primitive.cjs +52 -0
- package/dist/Primitive.d.cts +20 -0
- package/dist/Primitive.d.cts.map +1 -0
- package/dist/Primitive.d.mts +20 -0
- package/dist/Primitive.d.mts.map +1 -0
- package/dist/Primitive.mjs +48 -0
- package/dist/Primitive.mjs.map +1 -0
- package/dist/ProxyEnvironment.cjs +34 -0
- package/dist/ProxyEnvironment.d.cts +31 -0
- package/dist/ProxyEnvironment.d.cts.map +1 -0
- package/dist/ProxyEnvironment.d.mts +31 -0
- package/dist/ProxyEnvironment.d.mts.map +1 -0
- package/dist/ProxyEnvironment.mjs +29 -0
- package/dist/ProxyEnvironment.mjs.map +1 -0
- package/dist/Transaction.cjs +66 -0
- package/dist/Transaction.d.cts +56 -0
- package/dist/Transaction.d.cts.map +1 -0
- package/dist/Transaction.d.mts +56 -0
- package/dist/Transaction.d.mts.map +1 -0
- package/dist/Transaction.mjs +58 -0
- package/dist/Transaction.mjs.map +1 -0
- package/dist/Transform.cjs +11 -0
- package/dist/Transform.d.cts +21 -0
- package/dist/Transform.d.cts.map +1 -0
- package/dist/Transform.d.mts +21 -0
- package/dist/Transform.d.mts.map +1 -0
- package/dist/Transform.mjs +6 -0
- package/dist/Transform.mjs.map +1 -0
- package/dist/_virtual/_@oxc-project_runtime@0.103.0/helpers/defineProperty.cjs +14 -0
- package/dist/_virtual/_@oxc-project_runtime@0.103.0/helpers/defineProperty.mjs +14 -0
- package/dist/_virtual/_@oxc-project_runtime@0.103.0/helpers/objectSpread2.cjs +27 -0
- package/dist/_virtual/_@oxc-project_runtime@0.103.0/helpers/objectSpread2.mjs +27 -0
- package/dist/_virtual/_@oxc-project_runtime@0.103.0/helpers/toPrimitive.cjs +16 -0
- package/dist/_virtual/_@oxc-project_runtime@0.103.0/helpers/toPrimitive.mjs +16 -0
- package/dist/_virtual/_@oxc-project_runtime@0.103.0/helpers/toPropertyKey.cjs +11 -0
- package/dist/_virtual/_@oxc-project_runtime@0.103.0/helpers/toPropertyKey.mjs +11 -0
- package/dist/_virtual/_@oxc-project_runtime@0.103.0/helpers/typeof.cjs +18 -0
- package/dist/_virtual/_@oxc-project_runtime@0.103.0/helpers/typeof.mjs +12 -0
- package/dist/_virtual/rolldown_runtime.cjs +43 -0
- package/dist/{chunk-CLMFDpHK.mjs → _virtual/rolldown_runtime.mjs} +1 -1
- package/dist/client/ClientDocument.cjs +590 -0
- package/dist/client/ClientDocument.d.cts +161 -0
- package/dist/client/ClientDocument.d.cts.map +1 -0
- package/dist/client/ClientDocument.d.mts +162 -0
- package/dist/client/ClientDocument.d.mts.map +1 -0
- package/dist/client/ClientDocument.mjs +586 -0
- package/dist/client/ClientDocument.mjs.map +1 -0
- package/dist/client/Rebase.cjs +204 -0
- package/dist/client/Rebase.d.cts +87 -0
- package/dist/client/Rebase.d.cts.map +1 -0
- package/dist/client/Rebase.d.mts +88 -0
- package/dist/client/Rebase.d.mts.map +1 -0
- package/dist/client/Rebase.mjs +198 -0
- package/dist/client/Rebase.mjs.map +1 -0
- package/dist/client/StateMonitor.cjs +133 -0
- package/dist/client/StateMonitor.d.cts +122 -0
- package/dist/client/StateMonitor.d.cts.map +1 -0
- package/dist/client/StateMonitor.d.mts +122 -0
- package/dist/client/StateMonitor.d.mts.map +1 -0
- package/dist/client/StateMonitor.mjs +129 -0
- package/dist/client/StateMonitor.mjs.map +1 -0
- package/dist/client/Transport.cjs +11 -0
- package/dist/client/Transport.d.cts +237 -0
- package/dist/client/Transport.d.cts.map +1 -0
- package/dist/client/Transport.d.mts +237 -0
- package/dist/client/Transport.d.mts.map +1 -0
- package/dist/client/Transport.mjs +6 -0
- package/dist/client/Transport.mjs.map +1 -0
- package/dist/client/WebSocketTransport.cjs +396 -0
- package/dist/client/WebSocketTransport.d.cts +29 -0
- package/dist/client/WebSocketTransport.d.cts.map +1 -0
- package/dist/client/WebSocketTransport.d.mts +29 -0
- package/dist/client/WebSocketTransport.d.mts.map +1 -0
- package/dist/client/WebSocketTransport.mjs +392 -0
- package/dist/client/WebSocketTransport.mjs.map +1 -0
- package/dist/client/errors.cjs +135 -0
- package/dist/client/errors.d.cts +87 -0
- package/dist/client/errors.d.cts.map +1 -0
- package/dist/client/errors.d.mts +87 -0
- package/dist/client/errors.d.mts.map +1 -0
- package/dist/client/errors.mjs +127 -0
- package/dist/client/errors.mjs.map +1 -0
- package/dist/client/index.cjs +22 -1424
- package/dist/client/index.d.cts +8 -692
- package/dist/client/index.d.mts +8 -692
- package/dist/client/index.mjs +9 -1413
- package/dist/index.cjs +20 -2973
- package/dist/index.d.cts +12 -419
- package/dist/index.d.mts +12 -419
- package/dist/index.mjs +13 -2968
- package/dist/primitives/Array.cjs +302 -0
- package/dist/primitives/Array.d.cts +95 -0
- package/dist/primitives/Array.d.cts.map +1 -0
- package/dist/primitives/Array.d.mts +95 -0
- package/dist/primitives/Array.d.mts.map +1 -0
- package/dist/primitives/Array.mjs +301 -0
- package/dist/primitives/Array.mjs.map +1 -0
- package/dist/primitives/Boolean.cjs +95 -0
- package/dist/primitives/Boolean.d.cts +44 -0
- package/dist/primitives/Boolean.d.cts.map +1 -0
- package/dist/primitives/Boolean.d.mts +44 -0
- package/dist/primitives/Boolean.d.mts.map +1 -0
- package/dist/primitives/Boolean.mjs +94 -0
- package/dist/primitives/Boolean.mjs.map +1 -0
- package/dist/primitives/Either.cjs +200 -0
- package/dist/primitives/Either.d.cts +113 -0
- package/dist/primitives/Either.d.cts.map +1 -0
- package/dist/primitives/Either.d.mts +113 -0
- package/dist/primitives/Either.d.mts.map +1 -0
- package/dist/primitives/Either.mjs +199 -0
- package/dist/primitives/Either.mjs.map +1 -0
- package/dist/primitives/Lazy.cjs +46 -0
- package/dist/primitives/Lazy.d.cts +46 -0
- package/dist/primitives/Lazy.d.cts.map +1 -0
- package/dist/primitives/Lazy.d.mts +46 -0
- package/dist/primitives/Lazy.d.mts.map +1 -0
- package/dist/primitives/Lazy.mjs +46 -0
- package/dist/primitives/Lazy.mjs.map +1 -0
- package/dist/primitives/Literal.cjs +91 -0
- package/dist/primitives/Literal.d.cts +46 -0
- package/dist/primitives/Literal.d.cts.map +1 -0
- package/dist/primitives/Literal.d.mts +46 -0
- package/dist/primitives/Literal.d.mts.map +1 -0
- package/dist/primitives/Literal.mjs +90 -0
- package/dist/primitives/Literal.mjs.map +1 -0
- package/dist/primitives/Number.cjs +115 -0
- package/dist/primitives/Number.d.cts +54 -0
- package/dist/primitives/Number.d.cts.map +1 -0
- package/dist/primitives/Number.d.mts +54 -0
- package/dist/primitives/Number.d.mts.map +1 -0
- package/dist/primitives/Number.mjs +114 -0
- package/dist/primitives/Number.mjs.map +1 -0
- package/dist/primitives/String.cjs +127 -0
- package/dist/primitives/String.d.cts +56 -0
- package/dist/primitives/String.d.cts.map +1 -0
- package/dist/primitives/String.d.mts +56 -0
- package/dist/primitives/String.d.mts.map +1 -0
- package/dist/primitives/String.mjs +126 -0
- package/dist/primitives/String.mjs.map +1 -0
- package/dist/primitives/Struct.cjs +207 -0
- package/dist/primitives/Struct.d.cts +96 -0
- package/dist/primitives/Struct.d.cts.map +1 -0
- package/dist/primitives/Struct.d.mts +97 -0
- package/dist/primitives/Struct.d.mts.map +1 -0
- package/dist/primitives/Struct.mjs +206 -0
- package/dist/primitives/Struct.mjs.map +1 -0
- package/dist/primitives/Tree.cjs +575 -0
- package/dist/primitives/Tree.d.cts +185 -0
- package/dist/primitives/Tree.d.cts.map +1 -0
- package/dist/primitives/Tree.d.mts +185 -0
- package/dist/primitives/Tree.d.mts.map +1 -0
- package/dist/primitives/Tree.mjs +574 -0
- package/dist/primitives/Tree.mjs.map +1 -0
- package/dist/primitives/TreeNode.cjs +73 -0
- package/dist/primitives/TreeNode.d.cts +92 -0
- package/dist/primitives/TreeNode.d.cts.map +1 -0
- package/dist/primitives/TreeNode.d.mts +93 -0
- package/dist/primitives/TreeNode.d.mts.map +1 -0
- package/dist/primitives/TreeNode.mjs +72 -0
- package/dist/primitives/TreeNode.mjs.map +1 -0
- package/dist/primitives/Union.cjs +170 -0
- package/dist/primitives/Union.d.cts +81 -0
- package/dist/primitives/Union.d.cts.map +1 -0
- package/dist/primitives/Union.d.mts +81 -0
- package/dist/primitives/Union.d.mts.map +1 -0
- package/dist/primitives/Union.mjs +169 -0
- package/dist/primitives/Union.mjs.map +1 -0
- package/dist/primitives/shared.cjs +60 -0
- package/dist/primitives/shared.d.cts +147 -0
- package/dist/primitives/shared.d.cts.map +1 -0
- package/dist/primitives/shared.d.mts +147 -0
- package/dist/primitives/shared.d.mts.map +1 -0
- package/dist/primitives/shared.mjs +58 -0
- package/dist/primitives/shared.mjs.map +1 -0
- package/dist/server/ServerDocument.cjs +110 -0
- package/dist/server/ServerDocument.d.cts +98 -0
- package/dist/server/ServerDocument.d.cts.map +1 -0
- package/dist/server/ServerDocument.d.mts +99 -0
- package/dist/server/ServerDocument.d.mts.map +1 -0
- package/dist/server/ServerDocument.mjs +106 -0
- package/dist/server/ServerDocument.mjs.map +1 -0
- package/dist/server/errors.cjs +85 -0
- package/dist/server/errors.d.cts +53 -0
- package/dist/server/errors.d.cts.map +1 -0
- package/dist/server/errors.d.mts +53 -0
- package/dist/server/errors.d.mts.map +1 -0
- package/dist/server/errors.mjs +81 -0
- package/dist/server/errors.mjs.map +1 -0
- package/dist/server/index.cjs +9 -185
- package/dist/server/index.d.cts +3 -148
- package/dist/server/index.d.mts +3 -148
- package/dist/server/index.mjs +3 -181
- package/dist/types/index.cjs +16 -0
- package/dist/types/index.d.cts +16 -0
- package/dist/types/index.d.cts.map +1 -0
- package/dist/types/index.d.mts +16 -0
- package/dist/types/index.d.mts.map +1 -0
- package/dist/types/index.mjs +12 -0
- package/dist/types/index.mjs.map +1 -0
- package/dist/utils/tree-helpers.cjs +443 -0
- package/dist/utils/tree-helpers.d.cts +280 -0
- package/dist/utils/tree-helpers.d.cts.map +1 -0
- package/dist/utils/tree-helpers.d.mts +280 -0
- package/dist/utils/tree-helpers.d.mts.map +1 -0
- package/dist/utils/tree-helpers.mjs +439 -0
- package/dist/utils/tree-helpers.mjs.map +1 -0
- package/package.json +2 -2
- package/tsdown.config.ts +1 -1
- package/dist/Document-ChuFrTk1.cjs +0 -571
- package/dist/Document-CwiAFTIq.mjs +0 -438
- package/dist/Document-CwiAFTIq.mjs.map +0 -1
- package/dist/Presence-DKKP4v5X.d.cts.map +0 -1
- package/dist/Presence-DdMVKcOv.mjs.map +0 -1
- package/dist/Presence-N8u7Eppr.d.mts.map +0 -1
- package/dist/Primitive-DqQFc3Gu.d.mts +0 -1180
- package/dist/Primitive-DqQFc3Gu.d.mts.map +0 -1
- package/dist/Primitive-awpEjnKL.d.cts +0 -1180
- package/dist/Primitive-awpEjnKL.d.cts.map +0 -1
- package/dist/client/index.d.cts.map +0 -1
- package/dist/client/index.d.mts.map +0 -1
- package/dist/client/index.mjs.map +0 -1
- package/dist/index.d.cts.map +0 -1
- package/dist/index.d.mts.map +0 -1
- package/dist/index.mjs.map +0 -1
- package/dist/server/index.d.cts.map +0 -1
- package/dist/server/index.d.mts.map +0 -1
- package/dist/server/index.mjs.map +0 -1
|
@@ -0,0 +1,575 @@
|
|
|
1
|
+
const require_rolldown_runtime = require('../_virtual/rolldown_runtime.cjs');
|
|
2
|
+
const require_defineProperty = require('../_virtual/_@oxc-project_runtime@0.103.0/helpers/defineProperty.cjs');
|
|
3
|
+
const require_objectSpread2 = require('../_virtual/_@oxc-project_runtime@0.103.0/helpers/objectSpread2.cjs');
|
|
4
|
+
const require_shared = require('./shared.cjs');
|
|
5
|
+
const require_OperationDefinition = require('../OperationDefinition.cjs');
|
|
6
|
+
const require_OperationPath = require('../OperationPath.cjs');
|
|
7
|
+
const require_Operation = require('../Operation.cjs');
|
|
8
|
+
const require_FractionalIndex = require('../FractionalIndex.cjs');
|
|
9
|
+
let effect = require("effect");
|
|
10
|
+
|
|
11
|
+
//#region src/primitives/Tree.ts
|
|
12
|
+
/**
|
|
13
|
+
* Helper to get children sorted by position
|
|
14
|
+
*/
|
|
15
|
+
const getOrderedChildren = (nodes, parentId) => {
|
|
16
|
+
return [...nodes].filter((n) => n.parentId === parentId).sort((a, b) => a.pos < b.pos ? -1 : a.pos > b.pos ? 1 : 0);
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* Get all descendant IDs of a node (recursive)
|
|
20
|
+
*/
|
|
21
|
+
const getDescendantIds = (nodes, nodeId) => {
|
|
22
|
+
const children = nodes.filter((n) => n.parentId === nodeId);
|
|
23
|
+
const descendantIds = [];
|
|
24
|
+
for (const child of children) {
|
|
25
|
+
descendantIds.push(child.id);
|
|
26
|
+
descendantIds.push(...getDescendantIds(nodes, child.id));
|
|
27
|
+
}
|
|
28
|
+
return descendantIds;
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* Check if moving a node to a new parent would create a cycle
|
|
32
|
+
*/
|
|
33
|
+
const wouldCreateCycle = (nodes, nodeId, newParentId) => {
|
|
34
|
+
if (newParentId === null) return false;
|
|
35
|
+
if (newParentId === nodeId) return true;
|
|
36
|
+
return getDescendantIds(nodes, nodeId).includes(newParentId);
|
|
37
|
+
};
|
|
38
|
+
/**
|
|
39
|
+
* Generate a fractional position between two positions
|
|
40
|
+
*/
|
|
41
|
+
const generateTreePosBetween = (left, right) => {
|
|
42
|
+
const charSet = require_FractionalIndex.base62CharSet();
|
|
43
|
+
return effect.Effect.runSync(require_FractionalIndex.generateKeyBetween(left, right, charSet));
|
|
44
|
+
};
|
|
45
|
+
var TreePrimitive = class TreePrimitive {
|
|
46
|
+
constructor(schema) {
|
|
47
|
+
require_defineProperty._defineProperty(this, "_tag", "TreePrimitive");
|
|
48
|
+
require_defineProperty._defineProperty(this, "_State", void 0);
|
|
49
|
+
require_defineProperty._defineProperty(this, "_Proxy", void 0);
|
|
50
|
+
require_defineProperty._defineProperty(this, "_TRequired", void 0);
|
|
51
|
+
require_defineProperty._defineProperty(this, "_THasDefault", void 0);
|
|
52
|
+
require_defineProperty._defineProperty(this, "TSetInput", void 0);
|
|
53
|
+
require_defineProperty._defineProperty(this, "TUpdateInput", void 0);
|
|
54
|
+
require_defineProperty._defineProperty(this, "_schema", void 0);
|
|
55
|
+
require_defineProperty._defineProperty(this, "_nodeTypeRegistry", void 0);
|
|
56
|
+
require_defineProperty._defineProperty(this, "_opDefinitions", {
|
|
57
|
+
set: require_OperationDefinition.make({
|
|
58
|
+
kind: "tree.set",
|
|
59
|
+
payload: effect.Schema.Unknown,
|
|
60
|
+
target: effect.Schema.Unknown,
|
|
61
|
+
apply: (payload) => payload
|
|
62
|
+
}),
|
|
63
|
+
insert: require_OperationDefinition.make({
|
|
64
|
+
kind: "tree.insert",
|
|
65
|
+
payload: effect.Schema.Unknown,
|
|
66
|
+
target: effect.Schema.Unknown,
|
|
67
|
+
apply: (payload) => payload
|
|
68
|
+
}),
|
|
69
|
+
remove: require_OperationDefinition.make({
|
|
70
|
+
kind: "tree.remove",
|
|
71
|
+
payload: effect.Schema.Unknown,
|
|
72
|
+
target: effect.Schema.Unknown,
|
|
73
|
+
apply: (payload) => payload
|
|
74
|
+
}),
|
|
75
|
+
move: require_OperationDefinition.make({
|
|
76
|
+
kind: "tree.move",
|
|
77
|
+
payload: effect.Schema.Unknown,
|
|
78
|
+
target: effect.Schema.Unknown,
|
|
79
|
+
apply: (payload) => payload
|
|
80
|
+
})
|
|
81
|
+
});
|
|
82
|
+
require_defineProperty._defineProperty(this, "_internal", {
|
|
83
|
+
createProxy: (env, operationPath) => {
|
|
84
|
+
const getCurrentState = () => {
|
|
85
|
+
const state = env.getState(operationPath);
|
|
86
|
+
return state !== null && state !== void 0 ? state : [];
|
|
87
|
+
};
|
|
88
|
+
const getParentType = (parentId) => {
|
|
89
|
+
var _parent$type;
|
|
90
|
+
if (parentId === null) return null;
|
|
91
|
+
const parent = getCurrentState().find((n) => n.id === parentId);
|
|
92
|
+
return (_parent$type = parent === null || parent === void 0 ? void 0 : parent.type) !== null && _parent$type !== void 0 ? _parent$type : null;
|
|
93
|
+
};
|
|
94
|
+
const createNodeProxy = (nodeState) => {
|
|
95
|
+
return {
|
|
96
|
+
id: nodeState.id,
|
|
97
|
+
type: nodeState.type,
|
|
98
|
+
is: (nodeType) => {
|
|
99
|
+
return nodeState.type === nodeType.type;
|
|
100
|
+
},
|
|
101
|
+
as: (nodeType) => {
|
|
102
|
+
if (nodeState.type !== nodeType.type) throw new require_shared.ValidationError(`Node is of type "${nodeState.type}", not "${nodeType.type}"`);
|
|
103
|
+
const nodePath = operationPath.append(nodeState.id);
|
|
104
|
+
const dataProxy = nodeType.data._internal.createProxy(env, nodePath);
|
|
105
|
+
return {
|
|
106
|
+
id: nodeState.id,
|
|
107
|
+
type: nodeType.type,
|
|
108
|
+
data: dataProxy,
|
|
109
|
+
get: () => nodeState,
|
|
110
|
+
update: (value) => {
|
|
111
|
+
dataProxy.update(value);
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
},
|
|
115
|
+
get: () => nodeState
|
|
116
|
+
};
|
|
117
|
+
};
|
|
118
|
+
const buildSnapshot = (nodeId, nodes) => {
|
|
119
|
+
const node = nodes.find((n) => n.id === nodeId);
|
|
120
|
+
if (!node) return void 0;
|
|
121
|
+
const childNodes = getOrderedChildren(nodes, nodeId);
|
|
122
|
+
const children = [];
|
|
123
|
+
for (const child of childNodes) {
|
|
124
|
+
const childSnapshot = buildSnapshot(child.id, nodes);
|
|
125
|
+
if (childSnapshot) children.push(childSnapshot);
|
|
126
|
+
}
|
|
127
|
+
return require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({
|
|
128
|
+
id: node.id,
|
|
129
|
+
type: node.type
|
|
130
|
+
}, node.data), {}, { children });
|
|
131
|
+
};
|
|
132
|
+
return {
|
|
133
|
+
get: () => {
|
|
134
|
+
return getCurrentState();
|
|
135
|
+
},
|
|
136
|
+
set: (nodes) => {
|
|
137
|
+
env.addOperation(require_Operation.fromDefinition(operationPath, this._opDefinitions.set, nodes));
|
|
138
|
+
},
|
|
139
|
+
root: () => {
|
|
140
|
+
return getCurrentState().find((n) => n.parentId === null);
|
|
141
|
+
},
|
|
142
|
+
children: (parentId) => {
|
|
143
|
+
return getOrderedChildren(getCurrentState(), parentId);
|
|
144
|
+
},
|
|
145
|
+
node: (id) => {
|
|
146
|
+
const nodeState = getCurrentState().find((n) => n.id === id);
|
|
147
|
+
if (!nodeState) return void 0;
|
|
148
|
+
return createNodeProxy(nodeState);
|
|
149
|
+
},
|
|
150
|
+
insertFirst: (parentId, nodeType, data) => {
|
|
151
|
+
const state = getCurrentState();
|
|
152
|
+
const siblings = getOrderedChildren(state, parentId);
|
|
153
|
+
const pos = generateTreePosBetween(null, siblings.length > 0 ? siblings[0].pos : null);
|
|
154
|
+
const id = env.generateId();
|
|
155
|
+
if (parentId !== null && !state.find((n) => n.id === parentId)) throw new require_shared.ValidationError(`Parent node not found: ${parentId}`);
|
|
156
|
+
const parentType = getParentType(parentId);
|
|
157
|
+
this._validateChildType(parentType, nodeType.type);
|
|
158
|
+
if (parentId === null && state.some((n) => n.parentId === null)) throw new require_shared.ValidationError("Tree already has a root node");
|
|
159
|
+
const mergedData = require_shared.applyDefaults(nodeType.data, data);
|
|
160
|
+
env.addOperation(require_Operation.fromDefinition(operationPath, this._opDefinitions.insert, {
|
|
161
|
+
id,
|
|
162
|
+
type: nodeType.type,
|
|
163
|
+
parentId,
|
|
164
|
+
pos,
|
|
165
|
+
data: mergedData
|
|
166
|
+
}));
|
|
167
|
+
return id;
|
|
168
|
+
},
|
|
169
|
+
insertLast: (parentId, nodeType, data) => {
|
|
170
|
+
const state = getCurrentState();
|
|
171
|
+
const siblings = getOrderedChildren(state, parentId);
|
|
172
|
+
const pos = generateTreePosBetween(siblings.length > 0 ? siblings[siblings.length - 1].pos : null, null);
|
|
173
|
+
const id = env.generateId();
|
|
174
|
+
if (parentId !== null && !state.find((n) => n.id === parentId)) throw new require_shared.ValidationError(`Parent node not found: ${parentId}`);
|
|
175
|
+
const parentType = getParentType(parentId);
|
|
176
|
+
this._validateChildType(parentType, nodeType.type);
|
|
177
|
+
if (parentId === null && state.some((n) => n.parentId === null)) throw new require_shared.ValidationError("Tree already has a root node");
|
|
178
|
+
const mergedData = require_shared.applyDefaults(nodeType.data, data);
|
|
179
|
+
env.addOperation(require_Operation.fromDefinition(operationPath, this._opDefinitions.insert, {
|
|
180
|
+
id,
|
|
181
|
+
type: nodeType.type,
|
|
182
|
+
parentId,
|
|
183
|
+
pos,
|
|
184
|
+
data: mergedData
|
|
185
|
+
}));
|
|
186
|
+
return id;
|
|
187
|
+
},
|
|
188
|
+
insertAt: (parentId, index, nodeType, data) => {
|
|
189
|
+
const state = getCurrentState();
|
|
190
|
+
const siblings = getOrderedChildren(state, parentId);
|
|
191
|
+
const clampedIndex = Math.max(0, Math.min(index, siblings.length));
|
|
192
|
+
const pos = generateTreePosBetween(clampedIndex > 0 && siblings[clampedIndex - 1] ? siblings[clampedIndex - 1].pos : null, clampedIndex < siblings.length && siblings[clampedIndex] ? siblings[clampedIndex].pos : null);
|
|
193
|
+
const id = env.generateId();
|
|
194
|
+
if (parentId !== null && !state.find((n) => n.id === parentId)) throw new require_shared.ValidationError(`Parent node not found: ${parentId}`);
|
|
195
|
+
const parentType = getParentType(parentId);
|
|
196
|
+
this._validateChildType(parentType, nodeType.type);
|
|
197
|
+
if (parentId === null && state.some((n) => n.parentId === null)) throw new require_shared.ValidationError("Tree already has a root node");
|
|
198
|
+
const mergedData = require_shared.applyDefaults(nodeType.data, data);
|
|
199
|
+
env.addOperation(require_Operation.fromDefinition(operationPath, this._opDefinitions.insert, {
|
|
200
|
+
id,
|
|
201
|
+
type: nodeType.type,
|
|
202
|
+
parentId,
|
|
203
|
+
pos,
|
|
204
|
+
data: mergedData
|
|
205
|
+
}));
|
|
206
|
+
return id;
|
|
207
|
+
},
|
|
208
|
+
insertAfter: (siblingId, nodeType, data) => {
|
|
209
|
+
var _nextSibling$pos;
|
|
210
|
+
const state = getCurrentState();
|
|
211
|
+
const sibling = state.find((n) => n.id === siblingId);
|
|
212
|
+
if (!sibling) throw new require_shared.ValidationError(`Sibling node not found: ${siblingId}`);
|
|
213
|
+
const parentId = sibling.parentId;
|
|
214
|
+
const siblings = getOrderedChildren(state, parentId);
|
|
215
|
+
const nextSibling = siblings[siblings.findIndex((n) => n.id === siblingId) + 1];
|
|
216
|
+
const pos = generateTreePosBetween(sibling.pos, (_nextSibling$pos = nextSibling === null || nextSibling === void 0 ? void 0 : nextSibling.pos) !== null && _nextSibling$pos !== void 0 ? _nextSibling$pos : null);
|
|
217
|
+
const id = env.generateId();
|
|
218
|
+
const parentType = getParentType(parentId);
|
|
219
|
+
this._validateChildType(parentType, nodeType.type);
|
|
220
|
+
const mergedData = require_shared.applyDefaults(nodeType.data, data);
|
|
221
|
+
env.addOperation(require_Operation.fromDefinition(operationPath, this._opDefinitions.insert, {
|
|
222
|
+
id,
|
|
223
|
+
type: nodeType.type,
|
|
224
|
+
parentId,
|
|
225
|
+
pos,
|
|
226
|
+
data: mergedData
|
|
227
|
+
}));
|
|
228
|
+
return id;
|
|
229
|
+
},
|
|
230
|
+
insertBefore: (siblingId, nodeType, data) => {
|
|
231
|
+
var _prevSibling$pos;
|
|
232
|
+
const state = getCurrentState();
|
|
233
|
+
const sibling = state.find((n) => n.id === siblingId);
|
|
234
|
+
if (!sibling) throw new require_shared.ValidationError(`Sibling node not found: ${siblingId}`);
|
|
235
|
+
const parentId = sibling.parentId;
|
|
236
|
+
const siblings = getOrderedChildren(state, parentId);
|
|
237
|
+
const prevSibling = siblings[siblings.findIndex((n) => n.id === siblingId) - 1];
|
|
238
|
+
const pos = generateTreePosBetween((_prevSibling$pos = prevSibling === null || prevSibling === void 0 ? void 0 : prevSibling.pos) !== null && _prevSibling$pos !== void 0 ? _prevSibling$pos : null, sibling.pos);
|
|
239
|
+
const id = env.generateId();
|
|
240
|
+
const parentType = getParentType(parentId);
|
|
241
|
+
this._validateChildType(parentType, nodeType.type);
|
|
242
|
+
const mergedData = require_shared.applyDefaults(nodeType.data, data);
|
|
243
|
+
env.addOperation(require_Operation.fromDefinition(operationPath, this._opDefinitions.insert, {
|
|
244
|
+
id,
|
|
245
|
+
type: nodeType.type,
|
|
246
|
+
parentId,
|
|
247
|
+
pos,
|
|
248
|
+
data: mergedData
|
|
249
|
+
}));
|
|
250
|
+
return id;
|
|
251
|
+
},
|
|
252
|
+
remove: (id) => {
|
|
253
|
+
env.addOperation(require_Operation.fromDefinition(operationPath, this._opDefinitions.remove, { id }));
|
|
254
|
+
},
|
|
255
|
+
move: (nodeId, newParentId, toIndex) => {
|
|
256
|
+
var _state$find$type, _state$find;
|
|
257
|
+
const state = getCurrentState();
|
|
258
|
+
const node = state.find((n) => n.id === nodeId);
|
|
259
|
+
if (!node) throw new require_shared.ValidationError(`Node not found: ${nodeId}`);
|
|
260
|
+
if (newParentId !== null && !state.find((n) => n.id === newParentId)) throw new require_shared.ValidationError(`Parent node not found: ${newParentId}`);
|
|
261
|
+
if (wouldCreateCycle(state, nodeId, newParentId)) throw new require_shared.ValidationError("Move would create a cycle in the tree");
|
|
262
|
+
const newParentType = newParentId === null ? null : (_state$find$type = (_state$find = state.find((n) => n.id === newParentId)) === null || _state$find === void 0 ? void 0 : _state$find.type) !== null && _state$find$type !== void 0 ? _state$find$type : null;
|
|
263
|
+
this._validateChildType(newParentType, node.type);
|
|
264
|
+
if (node.parentId === null && newParentId !== null) throw new require_shared.ValidationError("Cannot move root node to have a parent");
|
|
265
|
+
const siblings = getOrderedChildren(state, newParentId).filter((n) => n.id !== nodeId);
|
|
266
|
+
const clampedIndex = Math.max(0, Math.min(toIndex, siblings.length));
|
|
267
|
+
const pos = generateTreePosBetween(clampedIndex > 0 && siblings[clampedIndex - 1] ? siblings[clampedIndex - 1].pos : null, clampedIndex < siblings.length && siblings[clampedIndex] ? siblings[clampedIndex].pos : null);
|
|
268
|
+
env.addOperation(require_Operation.fromDefinition(operationPath, this._opDefinitions.move, {
|
|
269
|
+
id: nodeId,
|
|
270
|
+
parentId: newParentId,
|
|
271
|
+
pos
|
|
272
|
+
}));
|
|
273
|
+
},
|
|
274
|
+
moveAfter: (nodeId, siblingId) => {
|
|
275
|
+
var _state$find$type2, _state$find2, _nextSibling$pos2;
|
|
276
|
+
const state = getCurrentState();
|
|
277
|
+
const node = state.find((n) => n.id === nodeId);
|
|
278
|
+
const sibling = state.find((n) => n.id === siblingId);
|
|
279
|
+
if (!node) throw new require_shared.ValidationError(`Node not found: ${nodeId}`);
|
|
280
|
+
if (!sibling) throw new require_shared.ValidationError(`Sibling node not found: ${siblingId}`);
|
|
281
|
+
const newParentId = sibling.parentId;
|
|
282
|
+
if (wouldCreateCycle(state, nodeId, newParentId)) throw new require_shared.ValidationError("Move would create a cycle in the tree");
|
|
283
|
+
const newParentType = newParentId === null ? null : (_state$find$type2 = (_state$find2 = state.find((n) => n.id === newParentId)) === null || _state$find2 === void 0 ? void 0 : _state$find2.type) !== null && _state$find$type2 !== void 0 ? _state$find$type2 : null;
|
|
284
|
+
this._validateChildType(newParentType, node.type);
|
|
285
|
+
if (node.parentId === null && newParentId !== null) throw new require_shared.ValidationError("Cannot move root node to have a parent");
|
|
286
|
+
const siblings = getOrderedChildren(state, newParentId).filter((n) => n.id !== nodeId);
|
|
287
|
+
const nextSibling = siblings[siblings.findIndex((n) => n.id === siblingId) + 1];
|
|
288
|
+
const pos = generateTreePosBetween(sibling.pos, (_nextSibling$pos2 = nextSibling === null || nextSibling === void 0 ? void 0 : nextSibling.pos) !== null && _nextSibling$pos2 !== void 0 ? _nextSibling$pos2 : null);
|
|
289
|
+
env.addOperation(require_Operation.fromDefinition(operationPath, this._opDefinitions.move, {
|
|
290
|
+
id: nodeId,
|
|
291
|
+
parentId: newParentId,
|
|
292
|
+
pos
|
|
293
|
+
}));
|
|
294
|
+
},
|
|
295
|
+
moveBefore: (nodeId, siblingId) => {
|
|
296
|
+
var _state$find$type3, _state$find3, _prevSibling$pos2;
|
|
297
|
+
const state = getCurrentState();
|
|
298
|
+
const node = state.find((n) => n.id === nodeId);
|
|
299
|
+
const sibling = state.find((n) => n.id === siblingId);
|
|
300
|
+
if (!node) throw new require_shared.ValidationError(`Node not found: ${nodeId}`);
|
|
301
|
+
if (!sibling) throw new require_shared.ValidationError(`Sibling node not found: ${siblingId}`);
|
|
302
|
+
const newParentId = sibling.parentId;
|
|
303
|
+
if (wouldCreateCycle(state, nodeId, newParentId)) throw new require_shared.ValidationError("Move would create a cycle in the tree");
|
|
304
|
+
const newParentType = newParentId === null ? null : (_state$find$type3 = (_state$find3 = state.find((n) => n.id === newParentId)) === null || _state$find3 === void 0 ? void 0 : _state$find3.type) !== null && _state$find$type3 !== void 0 ? _state$find$type3 : null;
|
|
305
|
+
this._validateChildType(newParentType, node.type);
|
|
306
|
+
if (node.parentId === null && newParentId !== null) throw new require_shared.ValidationError("Cannot move root node to have a parent");
|
|
307
|
+
const siblings = getOrderedChildren(state, newParentId).filter((n) => n.id !== nodeId);
|
|
308
|
+
const prevSibling = siblings[siblings.findIndex((n) => n.id === siblingId) - 1];
|
|
309
|
+
const pos = generateTreePosBetween((_prevSibling$pos2 = prevSibling === null || prevSibling === void 0 ? void 0 : prevSibling.pos) !== null && _prevSibling$pos2 !== void 0 ? _prevSibling$pos2 : null, sibling.pos);
|
|
310
|
+
env.addOperation(require_Operation.fromDefinition(operationPath, this._opDefinitions.move, {
|
|
311
|
+
id: nodeId,
|
|
312
|
+
parentId: newParentId,
|
|
313
|
+
pos
|
|
314
|
+
}));
|
|
315
|
+
},
|
|
316
|
+
moveToFirst: (nodeId, newParentId) => {
|
|
317
|
+
var _state$find$type4, _state$find4;
|
|
318
|
+
const state = getCurrentState();
|
|
319
|
+
const node = state.find((n) => n.id === nodeId);
|
|
320
|
+
if (!node) throw new require_shared.ValidationError(`Node not found: ${nodeId}`);
|
|
321
|
+
if (newParentId !== null && !state.find((n) => n.id === newParentId)) throw new require_shared.ValidationError(`Parent node not found: ${newParentId}`);
|
|
322
|
+
if (wouldCreateCycle(state, nodeId, newParentId)) throw new require_shared.ValidationError("Move would create a cycle in the tree");
|
|
323
|
+
const newParentType = newParentId === null ? null : (_state$find$type4 = (_state$find4 = state.find((n) => n.id === newParentId)) === null || _state$find4 === void 0 ? void 0 : _state$find4.type) !== null && _state$find$type4 !== void 0 ? _state$find$type4 : null;
|
|
324
|
+
this._validateChildType(newParentType, node.type);
|
|
325
|
+
if (node.parentId === null && newParentId !== null) throw new require_shared.ValidationError("Cannot move root node to have a parent");
|
|
326
|
+
const siblings = getOrderedChildren(state, newParentId).filter((n) => n.id !== nodeId);
|
|
327
|
+
const pos = generateTreePosBetween(null, siblings.length > 0 ? siblings[0].pos : null);
|
|
328
|
+
env.addOperation(require_Operation.fromDefinition(operationPath, this._opDefinitions.move, {
|
|
329
|
+
id: nodeId,
|
|
330
|
+
parentId: newParentId,
|
|
331
|
+
pos
|
|
332
|
+
}));
|
|
333
|
+
},
|
|
334
|
+
moveToLast: (nodeId, newParentId) => {
|
|
335
|
+
var _state$find$type5, _state$find5;
|
|
336
|
+
const state = getCurrentState();
|
|
337
|
+
const node = state.find((n) => n.id === nodeId);
|
|
338
|
+
if (!node) throw new require_shared.ValidationError(`Node not found: ${nodeId}`);
|
|
339
|
+
if (newParentId !== null && !state.find((n) => n.id === newParentId)) throw new require_shared.ValidationError(`Parent node not found: ${newParentId}`);
|
|
340
|
+
if (wouldCreateCycle(state, nodeId, newParentId)) throw new require_shared.ValidationError("Move would create a cycle in the tree");
|
|
341
|
+
const newParentType = newParentId === null ? null : (_state$find$type5 = (_state$find5 = state.find((n) => n.id === newParentId)) === null || _state$find5 === void 0 ? void 0 : _state$find5.type) !== null && _state$find$type5 !== void 0 ? _state$find$type5 : null;
|
|
342
|
+
this._validateChildType(newParentType, node.type);
|
|
343
|
+
if (node.parentId === null && newParentId !== null) throw new require_shared.ValidationError("Cannot move root node to have a parent");
|
|
344
|
+
const siblings = getOrderedChildren(state, newParentId).filter((n) => n.id !== nodeId);
|
|
345
|
+
const pos = generateTreePosBetween(siblings.length > 0 ? siblings[siblings.length - 1].pos : null, null);
|
|
346
|
+
env.addOperation(require_Operation.fromDefinition(operationPath, this._opDefinitions.move, {
|
|
347
|
+
id: nodeId,
|
|
348
|
+
parentId: newParentId,
|
|
349
|
+
pos
|
|
350
|
+
}));
|
|
351
|
+
},
|
|
352
|
+
at: (id, nodeType) => {
|
|
353
|
+
const node = getCurrentState().find((n) => n.id === id);
|
|
354
|
+
if (!node) throw new require_shared.ValidationError(`Node not found: ${id}`);
|
|
355
|
+
if (node.type !== nodeType.type) throw new require_shared.ValidationError(`Node is of type "${node.type}", not "${nodeType.type}"`);
|
|
356
|
+
const nodePath = operationPath.append(id);
|
|
357
|
+
return nodeType.data._internal.createProxy(env, nodePath);
|
|
358
|
+
},
|
|
359
|
+
updateAt: (id, nodeType, value) => {
|
|
360
|
+
const node = getCurrentState().find((n) => n.id === id);
|
|
361
|
+
if (!node) throw new require_shared.ValidationError(`Node not found: ${id}`);
|
|
362
|
+
if (node.type !== nodeType.type) throw new require_shared.ValidationError(`Node is of type "${node.type}", not "${nodeType.type}"`);
|
|
363
|
+
const nodePath = operationPath.append(id);
|
|
364
|
+
nodeType.data._internal.createProxy(env, nodePath).update(value);
|
|
365
|
+
},
|
|
366
|
+
toSnapshot: () => {
|
|
367
|
+
const state = getCurrentState();
|
|
368
|
+
const rootNode = state.find((n) => n.parentId === null);
|
|
369
|
+
if (!rootNode) return void 0;
|
|
370
|
+
return buildSnapshot(rootNode.id, state);
|
|
371
|
+
}
|
|
372
|
+
};
|
|
373
|
+
},
|
|
374
|
+
applyOperation: (state, operation) => {
|
|
375
|
+
const path = operation.path;
|
|
376
|
+
const tokens = path.toTokens().filter((t) => t !== "");
|
|
377
|
+
const currentState = state !== null && state !== void 0 ? state : [];
|
|
378
|
+
let newState;
|
|
379
|
+
if (tokens.length === 0) switch (operation.kind) {
|
|
380
|
+
case "tree.set": {
|
|
381
|
+
const payload = operation.payload;
|
|
382
|
+
if (!globalThis.Array.isArray(payload)) throw new require_shared.ValidationError(`TreePrimitive.set requires an array payload`);
|
|
383
|
+
newState = payload;
|
|
384
|
+
break;
|
|
385
|
+
}
|
|
386
|
+
case "tree.insert": {
|
|
387
|
+
const { id, type, parentId, pos, data } = operation.payload;
|
|
388
|
+
newState = [...currentState, {
|
|
389
|
+
id,
|
|
390
|
+
type,
|
|
391
|
+
parentId,
|
|
392
|
+
pos,
|
|
393
|
+
data
|
|
394
|
+
}];
|
|
395
|
+
break;
|
|
396
|
+
}
|
|
397
|
+
case "tree.remove": {
|
|
398
|
+
const { id } = operation.payload;
|
|
399
|
+
const descendantIds = getDescendantIds(currentState, id);
|
|
400
|
+
const idsToRemove = new Set([id, ...descendantIds]);
|
|
401
|
+
newState = currentState.filter((node) => !idsToRemove.has(node.id));
|
|
402
|
+
break;
|
|
403
|
+
}
|
|
404
|
+
case "tree.move": {
|
|
405
|
+
const { id, parentId, pos } = operation.payload;
|
|
406
|
+
newState = currentState.map((node) => node.id === id ? require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, node), {}, {
|
|
407
|
+
parentId,
|
|
408
|
+
pos
|
|
409
|
+
}) : node);
|
|
410
|
+
break;
|
|
411
|
+
}
|
|
412
|
+
default: throw new require_shared.ValidationError(`TreePrimitive cannot apply operation of kind: ${operation.kind}`);
|
|
413
|
+
}
|
|
414
|
+
else {
|
|
415
|
+
const nodeId = tokens[0];
|
|
416
|
+
const nodeIndex = currentState.findIndex((node$1) => node$1.id === nodeId);
|
|
417
|
+
if (nodeIndex === -1) throw new require_shared.ValidationError(`Tree node not found with ID: ${nodeId}`);
|
|
418
|
+
const node = currentState[nodeIndex];
|
|
419
|
+
const nodeTypePrimitive = this._getNodeTypePrimitive(node.type);
|
|
420
|
+
const remainingPath = path.shift();
|
|
421
|
+
const nodeOperation = require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, operation), {}, { path: remainingPath });
|
|
422
|
+
const newData = nodeTypePrimitive.data._internal.applyOperation(node.data, nodeOperation);
|
|
423
|
+
const mutableState = [...currentState];
|
|
424
|
+
mutableState[nodeIndex] = require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, node), {}, { data: newData });
|
|
425
|
+
newState = mutableState;
|
|
426
|
+
}
|
|
427
|
+
require_shared.runValidators(newState, this._schema.validators);
|
|
428
|
+
return newState;
|
|
429
|
+
},
|
|
430
|
+
getInitialState: () => {
|
|
431
|
+
var _rootNodeType$data$_i;
|
|
432
|
+
if (this._schema.defaultValue !== void 0) return this._schema.defaultValue;
|
|
433
|
+
const rootNodeType = this._schema.root;
|
|
434
|
+
const rootData = (_rootNodeType$data$_i = rootNodeType.data._internal.getInitialState()) !== null && _rootNodeType$data$_i !== void 0 ? _rootNodeType$data$_i : {};
|
|
435
|
+
const rootId = crypto.randomUUID();
|
|
436
|
+
const rootPos = generateTreePosBetween(null, null);
|
|
437
|
+
return [{
|
|
438
|
+
id: rootId,
|
|
439
|
+
type: rootNodeType.type,
|
|
440
|
+
parentId: null,
|
|
441
|
+
pos: rootPos,
|
|
442
|
+
data: rootData
|
|
443
|
+
}];
|
|
444
|
+
},
|
|
445
|
+
transformOperation: (clientOp, serverOp) => {
|
|
446
|
+
const clientPath = clientOp.path;
|
|
447
|
+
const serverPath = serverOp.path;
|
|
448
|
+
if (!require_OperationPath.pathsOverlap(clientPath, serverPath)) return {
|
|
449
|
+
type: "transformed",
|
|
450
|
+
operation: clientOp
|
|
451
|
+
};
|
|
452
|
+
if (serverOp.kind === "tree.remove") {
|
|
453
|
+
const removedId = serverOp.payload.id;
|
|
454
|
+
const clientTokens$1 = clientPath.toTokens().filter((t) => t !== "");
|
|
455
|
+
const serverTokens$1 = serverPath.toTokens().filter((t) => t !== "");
|
|
456
|
+
if (clientOp.kind === "tree.move") {
|
|
457
|
+
const movePayload = clientOp.payload;
|
|
458
|
+
if (movePayload.id === removedId || movePayload.parentId === removedId) return { type: "noop" };
|
|
459
|
+
}
|
|
460
|
+
if (clientOp.kind === "tree.insert") {
|
|
461
|
+
if (clientOp.payload.parentId === removedId) return { type: "noop" };
|
|
462
|
+
}
|
|
463
|
+
if (clientTokens$1.length > serverTokens$1.length) {
|
|
464
|
+
if (clientTokens$1[serverTokens$1.length] === removedId) return { type: "noop" };
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
if (serverOp.kind === "tree.insert" && clientOp.kind === "tree.insert") return {
|
|
468
|
+
type: "transformed",
|
|
469
|
+
operation: clientOp
|
|
470
|
+
};
|
|
471
|
+
if (serverOp.kind === "tree.move" && clientOp.kind === "tree.move") {
|
|
472
|
+
if (serverOp.payload.id === clientOp.payload.id) return {
|
|
473
|
+
type: "transformed",
|
|
474
|
+
operation: clientOp
|
|
475
|
+
};
|
|
476
|
+
return {
|
|
477
|
+
type: "transformed",
|
|
478
|
+
operation: clientOp
|
|
479
|
+
};
|
|
480
|
+
}
|
|
481
|
+
if (require_OperationPath.pathsEqual(clientPath, serverPath)) return {
|
|
482
|
+
type: "transformed",
|
|
483
|
+
operation: clientOp
|
|
484
|
+
};
|
|
485
|
+
if (serverOp.kind === "tree.set" && require_OperationPath.isPrefix(serverPath, clientPath)) return {
|
|
486
|
+
type: "transformed",
|
|
487
|
+
operation: clientOp
|
|
488
|
+
};
|
|
489
|
+
const clientTokens = clientPath.toTokens().filter((t) => t !== "");
|
|
490
|
+
const serverTokens = serverPath.toTokens().filter((t) => t !== "");
|
|
491
|
+
if (clientTokens.length > 0 && serverTokens.length > 0) {
|
|
492
|
+
if (clientTokens[0] !== serverTokens[0]) return {
|
|
493
|
+
type: "transformed",
|
|
494
|
+
operation: clientOp
|
|
495
|
+
};
|
|
496
|
+
return {
|
|
497
|
+
type: "transformed",
|
|
498
|
+
operation: clientOp
|
|
499
|
+
};
|
|
500
|
+
}
|
|
501
|
+
return {
|
|
502
|
+
type: "transformed",
|
|
503
|
+
operation: clientOp
|
|
504
|
+
};
|
|
505
|
+
}
|
|
506
|
+
});
|
|
507
|
+
this._schema = schema;
|
|
508
|
+
}
|
|
509
|
+
/** Mark this tree as required */
|
|
510
|
+
required() {
|
|
511
|
+
return new TreePrimitive(require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, this._schema), {}, { required: true }));
|
|
512
|
+
}
|
|
513
|
+
/** Set a default value for this tree */
|
|
514
|
+
default(defaultValue) {
|
|
515
|
+
return new TreePrimitive(require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, this._schema), {}, { defaultValue }));
|
|
516
|
+
}
|
|
517
|
+
/** Get the root node type */
|
|
518
|
+
get root() {
|
|
519
|
+
return this._schema.root;
|
|
520
|
+
}
|
|
521
|
+
/** Add a custom validation rule */
|
|
522
|
+
refine(fn, message) {
|
|
523
|
+
return new TreePrimitive(require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, this._schema), {}, { validators: [...this._schema.validators, {
|
|
524
|
+
validate: fn,
|
|
525
|
+
message
|
|
526
|
+
}] }));
|
|
527
|
+
}
|
|
528
|
+
/**
|
|
529
|
+
* Build a registry of all node types reachable from root
|
|
530
|
+
*/
|
|
531
|
+
_buildNodeTypeRegistry() {
|
|
532
|
+
if (this._nodeTypeRegistry !== void 0) return this._nodeTypeRegistry;
|
|
533
|
+
const registry = /* @__PURE__ */ new Map();
|
|
534
|
+
const visited = /* @__PURE__ */ new Set();
|
|
535
|
+
const visit = (node) => {
|
|
536
|
+
if (visited.has(node.type)) return;
|
|
537
|
+
visited.add(node.type);
|
|
538
|
+
registry.set(node.type, node);
|
|
539
|
+
for (const child of node.children) visit(child);
|
|
540
|
+
};
|
|
541
|
+
visit(this._schema.root);
|
|
542
|
+
this._nodeTypeRegistry = registry;
|
|
543
|
+
return registry;
|
|
544
|
+
}
|
|
545
|
+
/**
|
|
546
|
+
* Get a node type primitive by its type string
|
|
547
|
+
*/
|
|
548
|
+
_getNodeTypePrimitive(type) {
|
|
549
|
+
const nodeType = this._buildNodeTypeRegistry().get(type);
|
|
550
|
+
if (!nodeType) throw new require_shared.ValidationError(`Unknown node type: ${type}`);
|
|
551
|
+
return nodeType;
|
|
552
|
+
}
|
|
553
|
+
/**
|
|
554
|
+
* Validate that a node type can be a child of a parent node type
|
|
555
|
+
*/
|
|
556
|
+
_validateChildType(parentType, childType) {
|
|
557
|
+
if (parentType === null) {
|
|
558
|
+
if (childType !== this._schema.root.type) throw new require_shared.ValidationError(`Root node must be of type "${this._schema.root.type}", got "${childType}"`);
|
|
559
|
+
return;
|
|
560
|
+
}
|
|
561
|
+
const parentNodePrimitive = this._getNodeTypePrimitive(parentType);
|
|
562
|
+
if (!parentNodePrimitive.isChildAllowed(childType)) throw new require_shared.ValidationError(`Node type "${childType}" is not allowed as a child of "${parentType}". Allowed types: ${parentNodePrimitive.children.map((c) => c.type).join(", ") || "none"}`);
|
|
563
|
+
}
|
|
564
|
+
};
|
|
565
|
+
/** Creates a new TreePrimitive with the given root node type */
|
|
566
|
+
const Tree = (options) => new TreePrimitive({
|
|
567
|
+
required: false,
|
|
568
|
+
defaultValue: void 0,
|
|
569
|
+
root: options.root,
|
|
570
|
+
validators: []
|
|
571
|
+
});
|
|
572
|
+
|
|
573
|
+
//#endregion
|
|
574
|
+
exports.Tree = Tree;
|
|
575
|
+
exports.TreePrimitive = TreePrimitive;
|