@coorax/core 0.0.0-canary.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/package.json +40 -0
- package/src/base/classes/component.class.ts +68 -0
- package/src/base/classes/container.class.ts +57 -0
- package/src/base/classes/field.class.ts +42 -0
- package/src/base/classes/tnode.class.ts +38 -0
- package/src/base/index.ts +23 -0
- package/src/base/types/components/components.ts +21 -0
- package/src/base/types/container/container.ts +37 -0
- package/src/base/types/container/division.types.ts +3 -0
- package/src/base/types/container/section.types.ts +3 -0
- package/src/base/types/container/ul-list.types.ts +3 -0
- package/src/base/types/css.types.ts +33 -0
- package/src/base/types/field/divider.types.ts +3 -0
- package/src/base/types/field/field.media.types.ts +6 -0
- package/src/base/types/field/field.text.types.ts +9 -0
- package/src/base/types/field/field.ts +46 -0
- package/src/base/types/field/heading.types.ts +3 -0
- package/src/base/types/field/paragraph.types.ts +3 -0
- package/src/base/types/global.types.ts +45 -0
- package/src/base/types/node/link.types.ts +3 -0
- package/src/base/types/node/node.ts +28 -0
- package/src/base/types/node/span.types.ts +3 -0
- package/src/base/types/tree/tree.ts +33 -0
- package/src/engine/engine.test.ts +27 -0
- package/src/engine/engine.ts +38 -0
- package/src/engine/index.ts +1 -0
- package/src/index.ts +13 -0
- package/src/modules/editor/editor.module.ts +119 -0
- package/src/modules/focus/focus.module.ts +185 -0
- package/src/modules/history/history.module.ts +60 -0
- package/src/modules/hover/hover.module.ts +121 -0
- package/src/modules/index.ts +10 -0
- package/src/modules/insertion/insertion.module.ts +140 -0
- package/src/modules/modules-manager.ts +132 -0
- package/src/modules/modules.types.ts +47 -0
- package/src/modules/selection/selection.module.ts +174 -0
- package/src/modules/selection/selection.test.ts +1 -0
- package/src/modules/text-editing/text-editing.constants.ts +6 -0
- package/src/modules/text-editing/text-editing.module.ts +377 -0
- package/src/modules/text-editing/text-editing.utils.ts +27 -0
- package/src/services/commandsbus/commandsbus.test.ts +312 -0
- package/src/services/commandsbus/commandsbus.ts +360 -0
- package/src/services/commandsbus/index.ts +1 -0
- package/src/services/document/document.ts +139 -0
- package/src/services/document/index.ts +1 -0
- package/src/services/eventbus/eventbus.test.ts +144 -0
- package/src/services/eventbus/eventbus.ts +162 -0
- package/src/services/eventbus/index.ts +2 -0
- package/src/services/eventbus/subscriber.ts +46 -0
- package/src/services/mutations/index.ts +2 -0
- package/src/services/mutations/mutations.errors.ts +12 -0
- package/src/services/mutations/mutations.test.ts +177 -0
- package/src/services/mutations/mutations.ts +457 -0
- package/src/services/shortcuts/shortcuts.ts +3 -0
- package/src/services/state/index.ts +2 -0
- package/src/services/state/state.errors.ts +12 -0
- package/src/services/state/state.ts +64 -0
- package/src/services/tasks/index.ts +1 -0
- package/src/services/tasks/tasks.service.test.ts +193 -0
- package/src/services/tasks/tasks.service.ts +288 -0
- package/src/services/text-marks/index.ts +2 -0
- package/src/services/text-marks/marks.types.ts +38 -0
- package/src/services/text-marks/text-marks.ts +244 -0
- package/src/services/ui/default-styles.ts +93 -0
- package/src/services/ui/index.ts +3 -0
- package/src/services/ui/ui.errors.ts +6 -0
- package/src/services/ui/ui.ts +148 -0
- package/src/services/ui/ui.types.ts +197 -0
- package/src/utils/components.utils.ts +26 -0
- package/src/utils/container.utils.ts +59 -0
- package/src/utils/fields.utils.ts +118 -0
- package/src/utils/global.utils.ts +128 -0
- package/src/utils/index.ts +7 -0
- package/src/utils/modules.utils.ts +110 -0
- package/src/utils/tnode.utils.ts +48 -0
- package/src/utils/tree.utils.ts +120 -0
- package/styles.css +178 -0
- package/tsconfig.json +9 -0
|
@@ -0,0 +1,457 @@
|
|
|
1
|
+
import { ContainerClass } from "../../base/classes/container.class";
|
|
2
|
+
import { FieldClass } from "../../base/classes/field.class";
|
|
3
|
+
import type { Container } from "../../base/types/container/container";
|
|
4
|
+
import type { Field, FieldId } from "../../base/types/field/field";
|
|
5
|
+
import type { OmitContainersMethods, OmitFieldsMethods } from "../../base/types/global.types";
|
|
6
|
+
import type { BlockId, CoraxTree, OmittedMethodsBlock } from "../../base/types/tree/tree";
|
|
7
|
+
import { UTILS } from "../../utils/global.utils";
|
|
8
|
+
import { MutationError, MutationPrototypeError } from "./mutations.errors";
|
|
9
|
+
|
|
10
|
+
type MaxDepth = [1, 2, 3];
|
|
11
|
+
|
|
12
|
+
type DataKeys<T> = {
|
|
13
|
+
[K in keyof T]: T[K] extends (...args: any[]) => any ? never : K;
|
|
14
|
+
}[keyof T] &
|
|
15
|
+
string;
|
|
16
|
+
|
|
17
|
+
type IsPlainObject<T> = T extends object
|
|
18
|
+
? T extends (...args: any[]) => any
|
|
19
|
+
? false
|
|
20
|
+
: T extends any[]
|
|
21
|
+
? false
|
|
22
|
+
: T extends Set<any>
|
|
23
|
+
? false
|
|
24
|
+
: T extends Map<any, any>
|
|
25
|
+
? false
|
|
26
|
+
: T extends Date
|
|
27
|
+
? false
|
|
28
|
+
: T extends RegExp
|
|
29
|
+
? false
|
|
30
|
+
: true
|
|
31
|
+
: false;
|
|
32
|
+
|
|
33
|
+
type DeepKey<T, Depth extends any[] = MaxDepth> = Depth["length"] extends 0
|
|
34
|
+
? never
|
|
35
|
+
: {
|
|
36
|
+
[K in DataKeys<T>]: IsPlainObject<T[K]> extends true
|
|
37
|
+
? K | `${K}.${DeepKey<T[K], Tail<Depth>>}`
|
|
38
|
+
: K;
|
|
39
|
+
}[DataKeys<T>];
|
|
40
|
+
|
|
41
|
+
type Tail<T extends any[]> = ((...args: T) => any) extends (_: any, ...tail: infer TT) => any
|
|
42
|
+
? TT
|
|
43
|
+
: [];
|
|
44
|
+
|
|
45
|
+
type DeepValue<T, K extends string> = K extends keyof T
|
|
46
|
+
? T[K]
|
|
47
|
+
: K extends `${infer First}.${infer Rest}`
|
|
48
|
+
? First extends keyof T
|
|
49
|
+
? DeepValue<T[First], Rest>
|
|
50
|
+
: any
|
|
51
|
+
: any;
|
|
52
|
+
|
|
53
|
+
type MutationOptions<T extends object, K extends DeepKey<T> = DeepKey<T>> = {
|
|
54
|
+
id: "id" extends keyof T ? T["id"] : never;
|
|
55
|
+
keyValuePairs: Omit<
|
|
56
|
+
{
|
|
57
|
+
[Key in K]?: DeepValue<T, Key>;
|
|
58
|
+
},
|
|
59
|
+
"id" | "variant"
|
|
60
|
+
>;
|
|
61
|
+
autoMerge?: boolean;
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
export interface MutationsInterface {
|
|
65
|
+
insertField(tree: CoraxTree, field: Field, index?: number): CoraxTree;
|
|
66
|
+
deleteField(tree: CoraxTree, fieldId: FieldId): CoraxTree;
|
|
67
|
+
// patchField(tree: CoraxTree, field: Partial<OmitFieldsMethods<Field>> & { id: FieldId }): CoraxTree
|
|
68
|
+
mutateField<T extends Field = Field>(
|
|
69
|
+
tree: CoraxTree,
|
|
70
|
+
options: MutationOptions<OmitFieldsMethods<T>>,
|
|
71
|
+
): CoraxTree;
|
|
72
|
+
|
|
73
|
+
// group(tree: CoraxTree, toBlockIds: BlockId[]): CoraxTree
|
|
74
|
+
// unGroup(tree: CoraxTree, toUnBlockIds: BlockId[]): CoraxTree
|
|
75
|
+
// updateGroup(tree: CoraxTree, containerId: ContainerId, BlockIds: BlockId[]): CoraxTree
|
|
76
|
+
// mergeGroups(tree: CoraxTree, ...containerIds: ContainerId[]): CoraxTree
|
|
77
|
+
|
|
78
|
+
// patchContainer(tree: CoraxTree, container: Partial<OmitContainersMethods<Container>> & { id: ContainerId }): CoraxTree
|
|
79
|
+
mutateContainer(
|
|
80
|
+
tree: CoraxTree,
|
|
81
|
+
options: MutationOptions<OmitContainersMethods<Container>>,
|
|
82
|
+
): CoraxTree;
|
|
83
|
+
|
|
84
|
+
// createComponent(tree: CoraxTree, container: Container): CoraxTree
|
|
85
|
+
// insertComponent(tree: CoraxTree, component: Component, index?: number): CoraxTree
|
|
86
|
+
// deleteComponent(tree: CoraxTree, componentId: ComponentId): CoraxTree
|
|
87
|
+
|
|
88
|
+
// patchComponent(tree: CoraxTree, component: Partial<OmitComponentsMethods<Component>> & { id: ComponentId }): CoraxTree
|
|
89
|
+
// mutateComponent(tree: CoraxTree, options: MutationOptions<OmitComponentsMethods<Component>>): CoraxTree
|
|
90
|
+
|
|
91
|
+
// insertNode(tree: CoraxTree, fieldId: FieldId, node: TNode, indexes: [number, number]): CoraxTree
|
|
92
|
+
// patchNode(tree: CoraxTree, node: Partial<OmitTNodesMethods<TNode>> & { id: TNodeId }): CoraxTree
|
|
93
|
+
// mutateNode(tree: CoraxTree, options: MutationOptions<OmitTNodesMethods<TNode>>): CoraxTree
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const FORBIDDEN = new Set(["__proto__", "constructor", "prototype"]);
|
|
97
|
+
|
|
98
|
+
export class MutationsService implements MutationsInterface {
|
|
99
|
+
insertField(tree: CoraxTree, _field: Field, index?: number): CoraxTree {
|
|
100
|
+
//Check if tree already has the fieldId
|
|
101
|
+
const hasField = tree.order.has(_field.id);
|
|
102
|
+
|
|
103
|
+
if (hasField) {
|
|
104
|
+
throw new MutationError(`Field ${_field.name} with ID (${_field.id}) already exists`);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const field = _field instanceof FieldClass ? _field : new FieldClass(_field);
|
|
108
|
+
if (!(field instanceof FieldClass)) {
|
|
109
|
+
throw new MutationPrototypeError(field.name);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
if (index === 0) {
|
|
113
|
+
throw new MutationError(`Cannot delete the first field!`);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
if (field.containerId) {
|
|
117
|
+
const hasContainer = tree.containers.has(field.containerId);
|
|
118
|
+
|
|
119
|
+
const hasContainerInOrder = tree.order.has(field.containerId);
|
|
120
|
+
|
|
121
|
+
if (!hasContainer || !hasContainerInOrder) {
|
|
122
|
+
throw new MutationError(
|
|
123
|
+
`Insert of Field ${field.name} with ID (${field.id}) failed. Container ${field.containerId} does not exist`,
|
|
124
|
+
);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
tree = this.mutateContainer(tree, {
|
|
128
|
+
id: field.containerId,
|
|
129
|
+
keyValuePairs: {
|
|
130
|
+
order: new Set([field.id]),
|
|
131
|
+
},
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
let order: Set<BlockId>;
|
|
136
|
+
if (index !== undefined) {
|
|
137
|
+
if (index < 0) {
|
|
138
|
+
console.warn(`Mutation: negative index detected '${index}'`);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
const orderArray = Array.from(tree.order);
|
|
142
|
+
orderArray.splice(index, 0, field.id);
|
|
143
|
+
order = new Set(orderArray);
|
|
144
|
+
} else {
|
|
145
|
+
order = new Set(tree.order);
|
|
146
|
+
order.add(field.id);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
const fields = new Map(tree.fields);
|
|
150
|
+
fields.set(field.id, field);
|
|
151
|
+
|
|
152
|
+
return {
|
|
153
|
+
...tree,
|
|
154
|
+
order,
|
|
155
|
+
fields,
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
deleteField(tree: CoraxTree, fieldId: FieldId): CoraxTree {
|
|
160
|
+
//Check if tree already has the fieldId
|
|
161
|
+
const hasField = tree.order.has(fieldId);
|
|
162
|
+
if (!hasField) {
|
|
163
|
+
throw new MutationError(`Field with ID (${fieldId}) not found`);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
const field = tree.fields.get(fieldId);
|
|
167
|
+
|
|
168
|
+
const order = new Set(tree.order);
|
|
169
|
+
order.delete(fieldId);
|
|
170
|
+
|
|
171
|
+
const fields = new Map(tree.fields);
|
|
172
|
+
fields.delete(fieldId);
|
|
173
|
+
|
|
174
|
+
let updatedTree: CoraxTree = {
|
|
175
|
+
...tree,
|
|
176
|
+
order,
|
|
177
|
+
fields,
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
const hasContainer = field?.containerId && updatedTree.containers.has(field.containerId);
|
|
181
|
+
|
|
182
|
+
if (hasContainer) {
|
|
183
|
+
const container = updatedTree.containers.get(field.containerId!);
|
|
184
|
+
|
|
185
|
+
const containerHasField = container?.order.has(fieldId);
|
|
186
|
+
|
|
187
|
+
if (container && containerHasField) {
|
|
188
|
+
const containerOrder = new Set(container.order);
|
|
189
|
+
containerOrder.delete(fieldId);
|
|
190
|
+
|
|
191
|
+
updatedTree = this.mutateContainer(updatedTree, {
|
|
192
|
+
id: field.containerId!,
|
|
193
|
+
keyValuePairs: {
|
|
194
|
+
order: containerOrder,
|
|
195
|
+
},
|
|
196
|
+
autoMerge: false,
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
return updatedTree;
|
|
202
|
+
}
|
|
203
|
+
// patchField(tree: CoraxTree, field: Partial<OmitFieldsMethods<Field>> & { id: FieldId; }): CoraxTree {
|
|
204
|
+
// throw new Error("Method not implemented.");
|
|
205
|
+
// }
|
|
206
|
+
|
|
207
|
+
mutateField<T extends Field = Field>(
|
|
208
|
+
tree: CoraxTree,
|
|
209
|
+
{ id, keyValuePairs, autoMerge }: MutationOptions<OmitFieldsMethods<T>>,
|
|
210
|
+
): CoraxTree {
|
|
211
|
+
const hasFieldInOrder = tree.order.has(id);
|
|
212
|
+
if (!hasFieldInOrder) {
|
|
213
|
+
throw new MutationError(`Field ${id} doesn't exist on the tree`);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
const existingField = tree.fields.get(id);
|
|
217
|
+
|
|
218
|
+
if (!existingField || !(existingField instanceof FieldClass)) {
|
|
219
|
+
throw new MutationPrototypeError(existingField?.name ?? id);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
const currentField = existingField.getCurrentFieldObject();
|
|
223
|
+
const updatedField = this.deepObjectUpdate(currentField, {
|
|
224
|
+
keyValuePairs,
|
|
225
|
+
autoMerge,
|
|
226
|
+
id,
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
if (updatedField === currentField) return tree;
|
|
230
|
+
|
|
231
|
+
const fields = new Map(tree.fields);
|
|
232
|
+
fields.set(id, new FieldClass(updatedField));
|
|
233
|
+
|
|
234
|
+
return {
|
|
235
|
+
...tree,
|
|
236
|
+
fields,
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
// group(tree: CoraxTree, toBlockIds: BlockId[]): CoraxTree {
|
|
241
|
+
// throw new Error("Method not implemented.");
|
|
242
|
+
// }
|
|
243
|
+
// unGroup(tree: CoraxTree, toUnBlockIds: BlockId[]): CoraxTree {
|
|
244
|
+
// throw new Error("Method not implemented.");
|
|
245
|
+
// }
|
|
246
|
+
// updateGroup(tree: CoraxTree, containerId: ContainerId, BlockIds: BlockId[]): CoraxTree {
|
|
247
|
+
// throw new Error("Method not implemented.");
|
|
248
|
+
// }
|
|
249
|
+
// mergeGroups(tree: CoraxTree, ...containerIds: ContainerId[]): CoraxTree {
|
|
250
|
+
// throw new Error("Method not implemented.");
|
|
251
|
+
// }
|
|
252
|
+
// patchContainer(tree: CoraxTree, container: Partial<OmitContainersMethods<Container>> & { id: ContainerId; }): CoraxTree {
|
|
253
|
+
// throw new Error("Method not implemented.");
|
|
254
|
+
// }
|
|
255
|
+
|
|
256
|
+
private canMerge(autoMerge: boolean, value: unknown, current: unknown): boolean {
|
|
257
|
+
if (!autoMerge) return false;
|
|
258
|
+
if (current === undefined || current === null) return false; // rien à fusionner : on pose la valeur
|
|
259
|
+
if (Array.isArray(value)) return Array.isArray(current);
|
|
260
|
+
if (UTILS.isPlainObject(value)) return UTILS.isPlainObject(current);
|
|
261
|
+
return false; // primitives, null, classes : remplacement
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
private mergeValues<T extends unknown[] | Record<string, unknown>>(current: T, value: T): T {
|
|
265
|
+
if (Array.isArray(current)) {
|
|
266
|
+
const incoming = value as unknown[];
|
|
267
|
+
return incoming.length === 0 ? current : ([...current, ...incoming] as T);
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
const cur = current as Record<string, unknown>;
|
|
271
|
+
const inc = value as Record<string, unknown>;
|
|
272
|
+
const unchanged = Object.keys(inc).every((k) => Object.is(cur[k], inc[k]));
|
|
273
|
+
return unchanged ? current : ({ ...cur, ...inc } as T);
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
// private mergeValues<T = Array<any> | object>(...values: T[]): T {
|
|
277
|
+
// let result = Array.isArray(values[0]) ? [] : {}
|
|
278
|
+
|
|
279
|
+
// for (const value of values) {
|
|
280
|
+
// if (Array.isArray(value)) {
|
|
281
|
+
// if (!Array.isArray(result)) throw new MutationError("Cannot merge Array into Object")
|
|
282
|
+
// result = [...result, ...value]
|
|
283
|
+
// } else {
|
|
284
|
+
// if (Array.isArray(result)) throw new MutationError("Cannot merge Object into Array")
|
|
285
|
+
// result = { ...result, ...value }
|
|
286
|
+
// }
|
|
287
|
+
// }
|
|
288
|
+
|
|
289
|
+
// return result as T
|
|
290
|
+
// }
|
|
291
|
+
|
|
292
|
+
// private canMerge(autoMerge = false, value: any, ...rest: any[]) {
|
|
293
|
+
// if (!autoMerge || !rest.length) return false
|
|
294
|
+
|
|
295
|
+
// let lastType = typeof value
|
|
296
|
+
// if (typeof value !== "object" && !Array.isArray(value)) {
|
|
297
|
+
// return false
|
|
298
|
+
// }
|
|
299
|
+
|
|
300
|
+
// for (const v of rest) {
|
|
301
|
+
// const _value = v ?? (Array.isArray(value) ? [] : {})
|
|
302
|
+
|
|
303
|
+
// if (typeof _value !== "object" && !Array.isArray(_value)) {
|
|
304
|
+
// return false
|
|
305
|
+
// }
|
|
306
|
+
|
|
307
|
+
// if (typeof _value !== lastType) {
|
|
308
|
+
// return false
|
|
309
|
+
// }
|
|
310
|
+
|
|
311
|
+
// lastType = typeof _value
|
|
312
|
+
// }
|
|
313
|
+
|
|
314
|
+
// return true
|
|
315
|
+
// }
|
|
316
|
+
|
|
317
|
+
// private deepObjectUpdate<T extends OmittedMethodsBlock = OmittedMethodsBlock>(obj: T, { keyValuePairs, autoMerge = true }: MutationOptions<T>) {
|
|
318
|
+
// const FORBIDDEN = new Set(["__proto__", "constructor", "prototype"])
|
|
319
|
+
|
|
320
|
+
// for (const [key, value] of UTILS.ObjectEntries(keyValuePairs)) {
|
|
321
|
+
|
|
322
|
+
// //#TODO verify the key
|
|
323
|
+
|
|
324
|
+
// let currentObject = obj //.getCurrentContainerObject()
|
|
325
|
+
// const keyPaths = key.split(".")
|
|
326
|
+
|
|
327
|
+
// if (keyPaths.some(p => FORBIDDEN.has(p))) {
|
|
328
|
+
// throw new MutationError(`Forbidden key "${key}"`)
|
|
329
|
+
// }
|
|
330
|
+
|
|
331
|
+
// for (const path of keyPaths) {
|
|
332
|
+
|
|
333
|
+
// if (keyPaths.indexOf(path) === keyPaths.length - 1) {
|
|
334
|
+
// // console.log("this.canMerge(autoMerge, currentObject[path], value): ", this.canMerge(autoMerge, currentObject[path], value))
|
|
335
|
+
|
|
336
|
+
// if (this.canMerge(autoMerge, value, currentObject[path])) {
|
|
337
|
+
// currentObject[path] = this.mergeValues(currentObject[path], value)
|
|
338
|
+
// } else {
|
|
339
|
+
// // console.log("currentObject[path]: ", currentObject[path])
|
|
340
|
+
// // console.log("value: ", value)
|
|
341
|
+
|
|
342
|
+
// //#Review limit
|
|
343
|
+
// if (!autoMerge) {
|
|
344
|
+
// for (const [key] of Object.entries(currentObject)) {
|
|
345
|
+
// delete currentObject[key]
|
|
346
|
+
// }
|
|
347
|
+
// }
|
|
348
|
+
|
|
349
|
+
// currentObject[path] = value
|
|
350
|
+
|
|
351
|
+
// }
|
|
352
|
+
|
|
353
|
+
// } else {
|
|
354
|
+
|
|
355
|
+
// // console.log("Path: ", [...key.split(path).splice(0, 1), path].join(""))
|
|
356
|
+
// currentObject = currentObject[path] ?? Object.assign(currentObject, {
|
|
357
|
+
// [path]: {}
|
|
358
|
+
// })[path]
|
|
359
|
+
// }
|
|
360
|
+
|
|
361
|
+
// }
|
|
362
|
+
|
|
363
|
+
// }
|
|
364
|
+
// }
|
|
365
|
+
|
|
366
|
+
private setIn(obj: any, path: string[], value: unknown, autoMerge: boolean): any {
|
|
367
|
+
const [head, ...rest] = path;
|
|
368
|
+
const current = obj?.[head];
|
|
369
|
+
|
|
370
|
+
let next: unknown;
|
|
371
|
+
if (rest.length === 0) {
|
|
372
|
+
next = this.canMerge(autoMerge, value, current) ? this.mergeValues(current, value) : value;
|
|
373
|
+
} else {
|
|
374
|
+
// crée le niveau manquant, ou si la valeur existante n'est pas un objet
|
|
375
|
+
next = this.setIn(UTILS.isPlainObject(current) ? current : {}, rest, value, autoMerge);
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
if (Object.is(next, current)) return obj; // rien n'a changé : même référence
|
|
379
|
+
|
|
380
|
+
return Array.isArray(obj)
|
|
381
|
+
? Object.assign([...obj], { [head]: next })
|
|
382
|
+
: { ...obj, [head]: next };
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
private deepObjectUpdate<T extends OmittedMethodsBlock>(
|
|
386
|
+
obj: T,
|
|
387
|
+
{ keyValuePairs, autoMerge = true }: MutationOptions<T>,
|
|
388
|
+
): T {
|
|
389
|
+
let result: any = obj;
|
|
390
|
+
for (const [key, value] of UTILS.ObjectEntries(keyValuePairs)) {
|
|
391
|
+
const path = key.split(".");
|
|
392
|
+
if (path.some((p) => FORBIDDEN.has(p))) {
|
|
393
|
+
throw new MutationError(`Forbidden key "${key}"`);
|
|
394
|
+
}
|
|
395
|
+
result = this.setIn(result, path, value, autoMerge);
|
|
396
|
+
}
|
|
397
|
+
return result;
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
mutateContainer(
|
|
401
|
+
tree: CoraxTree,
|
|
402
|
+
{ keyValuePairs, id, autoMerge = true }: MutationOptions<OmitContainersMethods<Container>>,
|
|
403
|
+
): CoraxTree {
|
|
404
|
+
if (!tree.order.has(id)) {
|
|
405
|
+
throw new MutationError(`Container ${id} doesn't exist on the tree`);
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
const existingContainer = tree.containers.get(id);
|
|
409
|
+
|
|
410
|
+
// Get plain object - handle both class instances and plain objects
|
|
411
|
+
if (!existingContainer || !(existingContainer instanceof ContainerClass)) {
|
|
412
|
+
throw new MutationPrototypeError(existingContainer?.name ?? "ContainerNotFound");
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
const currentContainer = existingContainer.getCurrentContainerObject();
|
|
416
|
+
const updatedContainer = this.deepObjectUpdate(currentContainer, {
|
|
417
|
+
keyValuePairs,
|
|
418
|
+
autoMerge,
|
|
419
|
+
id,
|
|
420
|
+
});
|
|
421
|
+
|
|
422
|
+
if (currentContainer === updatedContainer) return tree;
|
|
423
|
+
|
|
424
|
+
const containers = new Map(tree.containers);
|
|
425
|
+
containers.set(id, new ContainerClass(updatedContainer) as unknown as Container);
|
|
426
|
+
|
|
427
|
+
return {
|
|
428
|
+
...tree,
|
|
429
|
+
containers,
|
|
430
|
+
};
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
// createComponent(tree: CoraxTree, container: Container): CoraxTree {
|
|
434
|
+
// throw new Error("Method not implemented.");
|
|
435
|
+
// }
|
|
436
|
+
// insertComponent(tree: CoraxTree, component: Component, index?: number): CoraxTree {
|
|
437
|
+
// throw new Error("Method not implemented.");
|
|
438
|
+
// }
|
|
439
|
+
// deleteComponent(tree: CoraxTree, componentId: ComponentId): CoraxTree {
|
|
440
|
+
// throw new Error("Method not implemented.");
|
|
441
|
+
// }
|
|
442
|
+
// patchComponent(tree: CoraxTree, component: Partial<OmitComponentsMethods<Component>> & { id: ComponentId; }): CoraxTree {
|
|
443
|
+
// throw new Error("Method not implemented.");
|
|
444
|
+
// }
|
|
445
|
+
// mutateComponent(tree: CoraxTree, options: MutationOptions<OmitComponentsMethods<Component>>): CoraxTree {
|
|
446
|
+
// throw new Error("Method not implemented.");
|
|
447
|
+
// }
|
|
448
|
+
// insertNode(tree: CoraxTree, fieldId: FieldId, node: TNode, indexes: [number, number]): CoraxTree {
|
|
449
|
+
// throw new Error("Method not implemented.");
|
|
450
|
+
// }
|
|
451
|
+
// patchNode(tree: CoraxTree, node: Partial<OmitTNodesMethods<TNode>> & { id: TNodeId; }): CoraxTree {
|
|
452
|
+
// throw new Error("Method not implemented.");
|
|
453
|
+
// }
|
|
454
|
+
// mutateNode(tree: CoraxTree, options: MutationOptions<OmitTNodesMethods<TNode>>): CoraxTree {
|
|
455
|
+
// throw new Error("Method not implemented.");
|
|
456
|
+
// }
|
|
457
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export class StateError extends Error {
|
|
2
|
+
constructor(msg: string) {
|
|
3
|
+
super(`StateError: ${msg}`);
|
|
4
|
+
this.name = "StateError";
|
|
5
|
+
}
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export class StateNotFoundError extends StateError {
|
|
9
|
+
constructor(name: string) {
|
|
10
|
+
super(`State ${name} not found`);
|
|
11
|
+
}
|
|
12
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { StateError } from "./state.errors";
|
|
2
|
+
|
|
3
|
+
export interface StateMap {}
|
|
4
|
+
|
|
5
|
+
export type SliceName = keyof StateMap;
|
|
6
|
+
export type SliceValue<T extends SliceName = SliceName> = StateMap[T];
|
|
7
|
+
|
|
8
|
+
type StateDisposer = () => void;
|
|
9
|
+
|
|
10
|
+
export interface StateInterface {
|
|
11
|
+
defineSlice<T extends SliceName = SliceName>(name: T, initialValue: SliceValue<T>): StateDisposer;
|
|
12
|
+
get<T extends SliceName = SliceName>(name: T): SliceValue<T> | undefined;
|
|
13
|
+
set<T extends SliceName = SliceName>(name: T, value: SliceValue<T>): void;
|
|
14
|
+
subscribe<T extends SliceName = SliceName>(
|
|
15
|
+
name: T,
|
|
16
|
+
fn: (value: SliceValue<T>) => void,
|
|
17
|
+
): StateDisposer;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export class StateService implements StateInterface {
|
|
21
|
+
private slices = new Map<SliceName, StateMap[SliceName]>();
|
|
22
|
+
private listeners = new Map<SliceName, Set<(v: StateMap[SliceName]) => void>>();
|
|
23
|
+
|
|
24
|
+
defineSlice<T extends SliceName = SliceName>(
|
|
25
|
+
name: T,
|
|
26
|
+
initialValue: SliceValue<T>,
|
|
27
|
+
): StateDisposer {
|
|
28
|
+
if (this.slices.has(name)) {
|
|
29
|
+
throw new StateError(`${name} slice already defined`);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
this.slices.set(name, initialValue);
|
|
33
|
+
|
|
34
|
+
return () => {
|
|
35
|
+
this.slices.delete(name);
|
|
36
|
+
this.listeners.delete(name);
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
get<T extends SliceName = SliceName>(name: T): SliceValue<T> | undefined {
|
|
41
|
+
const state = this.slices.get(name);
|
|
42
|
+
if (!state) return undefined;
|
|
43
|
+
|
|
44
|
+
return state as SliceValue<T>;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
set<T extends SliceName = SliceName>(name: T, value: SliceValue<T>): void {
|
|
48
|
+
if (!this.slices.has(name)) throw new StateError(`Slice "${String(name)}" is not defined`);
|
|
49
|
+
|
|
50
|
+
if (value === this.slices.get(name)) return;
|
|
51
|
+
this.slices.set(name, value);
|
|
52
|
+
this.listeners.get(name)?.forEach((fn) => fn(value));
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
subscribe<T extends SliceName = SliceName>(
|
|
56
|
+
name: T,
|
|
57
|
+
fn: (value: SliceValue<T>) => void,
|
|
58
|
+
): StateDisposer {
|
|
59
|
+
const set = this.listeners.get(name) ?? new Set();
|
|
60
|
+
set.add(fn as (v: StateMap[SliceName]) => void);
|
|
61
|
+
this.listeners.set(name, set);
|
|
62
|
+
return () => set.delete(fn as (v: StateMap[SliceName]) => void);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./tasks.service";
|