@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,139 @@
|
|
|
1
|
+
import type { ComponentId, ContainerId, FieldId } from "../../base";
|
|
2
|
+
import type { Block, BlockId, CoraxTree } from "../../base/types/tree/tree";
|
|
3
|
+
import { coraxTreeDiffing, GetDefaultTree, type TreeDiff } from "../../utils/tree.utils";
|
|
4
|
+
import type { EventBusInterface } from "../eventbus/eventbus";
|
|
5
|
+
import type { StateInterface } from "../state/state";
|
|
6
|
+
|
|
7
|
+
declare module "../state/state" {
|
|
8
|
+
interface StateMap {
|
|
9
|
+
document: CoraxTree;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
declare module "../eventbus/eventbus" {
|
|
14
|
+
interface TEventMap {
|
|
15
|
+
"document:changed": {
|
|
16
|
+
prev: CoraxTree;
|
|
17
|
+
next: CoraxTree;
|
|
18
|
+
diff: TreeDiff;
|
|
19
|
+
origin: "user" | "history";
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export type DocumentDisposer = () => void;
|
|
25
|
+
|
|
26
|
+
export interface DocumentInterface {
|
|
27
|
+
get(): CoraxTree;
|
|
28
|
+
subscribeBlock(id: BlockId, fn: () => void): DocumentDisposer;
|
|
29
|
+
apply(
|
|
30
|
+
fn: (tree: CoraxTree) => CoraxTree,
|
|
31
|
+
options?: { shouldEmitEvent?: boolean; origin?: "user" | "history" },
|
|
32
|
+
): void;
|
|
33
|
+
getBlock(id: BlockId): Block | undefined;
|
|
34
|
+
has(id: BlockId): boolean;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export class DocumentService implements DocumentInterface {
|
|
38
|
+
private state: StateInterface;
|
|
39
|
+
private events: EventBusInterface;
|
|
40
|
+
|
|
41
|
+
private blockListeners = new Map<BlockId, Set<() => void>>();
|
|
42
|
+
|
|
43
|
+
constructor(state: StateInterface, events: EventBusInterface) {
|
|
44
|
+
this.state = state;
|
|
45
|
+
this.events = events;
|
|
46
|
+
|
|
47
|
+
this.state.defineSlice("document", GetDefaultTree());
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
subscribeBlock(id: BlockId, fn: () => void): DocumentDisposer {
|
|
51
|
+
const set = this.blockListeners.get(id) ?? new Set();
|
|
52
|
+
set.add(fn);
|
|
53
|
+
this.blockListeners.set(id, set);
|
|
54
|
+
return () => {
|
|
55
|
+
set.delete(fn);
|
|
56
|
+
if (!set.size) this.blockListeners.delete(id);
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
private bumpDocumentVersion(version: CoraxTree["version"]): CoraxTree["version"] {
|
|
61
|
+
const parts = version.split(".").map((n) => parseInt(n, 10));
|
|
62
|
+
|
|
63
|
+
if (parts.length !== 3 || parts.some((n) => isNaN(n))) {
|
|
64
|
+
throw new Error(`Invalid document version format: ${version}`);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
let [major, minor, patch] = parts;
|
|
68
|
+
|
|
69
|
+
// Increment from right to left (patch -> minor -> major)
|
|
70
|
+
patch++;
|
|
71
|
+
if (patch >= 100) {
|
|
72
|
+
patch = 0;
|
|
73
|
+
minor++;
|
|
74
|
+
}
|
|
75
|
+
if (minor >= 100) {
|
|
76
|
+
minor = 0;
|
|
77
|
+
major++;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const newVersion = `${major}.${minor}.${patch}` as CoraxTree["version"];
|
|
81
|
+
// console.log("Versioning: ", version, "->", newVersion);
|
|
82
|
+
|
|
83
|
+
return newVersion;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
get() {
|
|
87
|
+
return this.state.get("document")!;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
apply(
|
|
91
|
+
predicate: (tree: CoraxTree) => CoraxTree,
|
|
92
|
+
{ shouldEmitEvent, origin }: { shouldEmitEvent?: boolean; origin?: "user" | "history" } = {
|
|
93
|
+
shouldEmitEvent: true,
|
|
94
|
+
origin: "user",
|
|
95
|
+
},
|
|
96
|
+
) {
|
|
97
|
+
const prev = this.get();
|
|
98
|
+
const next = predicate(prev);
|
|
99
|
+
|
|
100
|
+
if (next === prev) return;
|
|
101
|
+
|
|
102
|
+
const diff = coraxTreeDiffing(prev, next);
|
|
103
|
+
|
|
104
|
+
if (!shouldEmitEvent) {
|
|
105
|
+
this.state.set("document", next);
|
|
106
|
+
|
|
107
|
+
for (const id of [...diff.changed, ...diff.added, ...diff.removed]) {
|
|
108
|
+
this.blockListeners.get(id)?.forEach((fn) => fn());
|
|
109
|
+
}
|
|
110
|
+
} else {
|
|
111
|
+
const versionnedTree = { ...next, version: this.bumpDocumentVersion(next.version) };
|
|
112
|
+
|
|
113
|
+
this.state.set("document", versionnedTree);
|
|
114
|
+
for (const id of [...diff.changed, ...diff.added, ...diff.removed]) {
|
|
115
|
+
this.blockListeners.get(id)?.forEach((fn) => fn());
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
void this.events.emit("document:changed", {
|
|
119
|
+
prev,
|
|
120
|
+
next: versionnedTree,
|
|
121
|
+
origin: origin ?? "user",
|
|
122
|
+
diff,
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
getBlock(id: BlockId): Block | undefined {
|
|
128
|
+
const t = this.get();
|
|
129
|
+
return (
|
|
130
|
+
t.fields.get(id as FieldId) ??
|
|
131
|
+
t.containers.get(id as ContainerId) ??
|
|
132
|
+
t.components.get(id as ComponentId)
|
|
133
|
+
);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
has(id: BlockId) {
|
|
137
|
+
return this.getBlock(id) !== undefined;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./document";
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import { describe, expect, test } from "vitest";
|
|
2
|
+
import { Subscriber } from "./subscriber";
|
|
3
|
+
import type { Field } from "../../base/types/field/field";
|
|
4
|
+
import { EventBusService, type TEvent } from "./eventbus";
|
|
5
|
+
|
|
6
|
+
declare module "./eventbus" {
|
|
7
|
+
interface TEventMap {
|
|
8
|
+
"field:test": { field: Field };
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
describe("Subscriber", () => {
|
|
13
|
+
test("creates subscriber with target and type", () => {
|
|
14
|
+
const subscriber = new Subscriber("field-1", "field:test");
|
|
15
|
+
|
|
16
|
+
expect(subscriber.target).toBe("field-1");
|
|
17
|
+
expect(subscriber.event).toBe("field:test");
|
|
18
|
+
expect(subscriber.value).toEqual({});
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
test("merges custom props onto a new subscriber", () => {
|
|
22
|
+
const subscriber = new Subscriber("field-1", "field:test").with({ reason: "patch" });
|
|
23
|
+
|
|
24
|
+
expect(subscriber.target).toBe("field-1");
|
|
25
|
+
expect(subscriber.event).toBe("field:test");
|
|
26
|
+
expect(subscriber.value).toEqual({ reason: "patch" });
|
|
27
|
+
});
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
describe("EventBus", () => {
|
|
31
|
+
test("passes custom emit props to the listener", () => {
|
|
32
|
+
const bus = new EventBusService();
|
|
33
|
+
const seen: Array<{ target: string; reason?: string; type: string }> = [];
|
|
34
|
+
|
|
35
|
+
const mockField: Field = { id: "field-1", type: "text" } as unknown as Field;
|
|
36
|
+
|
|
37
|
+
bus.on<"field:test", { reason: string }>("field:test", (scb) => {
|
|
38
|
+
seen.push({
|
|
39
|
+
target: scb.target,
|
|
40
|
+
reason: scb.value.reason,
|
|
41
|
+
type: scb.value.field.type,
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
bus.emit("field:test", { field: mockField, reason: "insert" });
|
|
46
|
+
|
|
47
|
+
expect(seen).toEqual([{ target: expect.any(String), reason: "insert", type: "text" }]);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
test("passes event value array to the listener", async () => {
|
|
51
|
+
const bus = new EventBusService();
|
|
52
|
+
const seen: Array<{ target: string; value: unknown }> = [];
|
|
53
|
+
|
|
54
|
+
const mockField: Field = { id: "field-1", type: "text" } as unknown as Field;
|
|
55
|
+
|
|
56
|
+
bus.on("field:test", (scb) => {
|
|
57
|
+
seen.push({ target: scb.target, value: scb.value.field });
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
await bus.emit("field:test", { field: mockField });
|
|
61
|
+
|
|
62
|
+
expect(seen).toHaveLength(1);
|
|
63
|
+
expect(typeof seen[0].target).toBe("string");
|
|
64
|
+
expect(seen[0].value).toEqual(mockField);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
test("emits changes event with type when emitting other events", async () => {
|
|
68
|
+
const bus = new EventBusService();
|
|
69
|
+
const seen: Array<{ originalType?: Omit<TEvent, "changes"> }> = [];
|
|
70
|
+
|
|
71
|
+
bus.on("changes", (scb) => {
|
|
72
|
+
seen.push({ originalType: scb.event });
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
const mockField: Field = { id: "field-1", type: "text" } as unknown as Field;
|
|
76
|
+
await bus.emit("field:test", { field: mockField });
|
|
77
|
+
|
|
78
|
+
expect(seen).toHaveLength(1);
|
|
79
|
+
expect(seen[0].originalType).toBe("field:test");
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
test("cleanup removes event handler", () => {
|
|
83
|
+
const bus = new EventBusService();
|
|
84
|
+
const seen: Array<{ target: string }> = [];
|
|
85
|
+
|
|
86
|
+
const cleanup = bus.on("field:test", (scb) => {
|
|
87
|
+
seen.push({ target: scb.target });
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
cleanup();
|
|
91
|
+
|
|
92
|
+
const mockField: Field = { id: "field-1", type: "text" } as unknown as Field;
|
|
93
|
+
bus.emit("field:test", { field: mockField });
|
|
94
|
+
|
|
95
|
+
expect(seen).toHaveLength(0);
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
test("cleanup removes specific handler when multiple handlers exist", () => {
|
|
99
|
+
const bus = new EventBusService();
|
|
100
|
+
const seen: Array<{ target: string; label: string }> = [];
|
|
101
|
+
|
|
102
|
+
const cleanup1 = bus.on("field:test", (scb) => {
|
|
103
|
+
seen.push({ target: scb.target, label: "handler1" });
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
const cleanup2 = bus.on("field:test", (scb) => {
|
|
107
|
+
seen.push({ target: scb.target, label: "handler2" });
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
cleanup1();
|
|
111
|
+
const mockField: Field = { id: "field-1", type: "text" } as unknown as Field;
|
|
112
|
+
bus.emit("field:test", { field: mockField });
|
|
113
|
+
|
|
114
|
+
expect(seen).toHaveLength(1);
|
|
115
|
+
expect(seen[0].label).toBe("handler2");
|
|
116
|
+
|
|
117
|
+
cleanup2();
|
|
118
|
+
bus.emit("field:test", { field: mockField });
|
|
119
|
+
|
|
120
|
+
expect(seen).toHaveLength(1);
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
test("batch events", async () => {
|
|
124
|
+
const bus = new EventBusService();
|
|
125
|
+
const mockField: Field = { id: "field-1", type: "text" } as unknown as Field;
|
|
126
|
+
|
|
127
|
+
bus.on("re-render", async () => {
|
|
128
|
+
await new Promise((r) => setTimeout(r, 0));
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
let cbs = Promise.all(
|
|
132
|
+
bus.batch([
|
|
133
|
+
["field:test", { field: mockField }],
|
|
134
|
+
["re-render", { args: [mockField] }],
|
|
135
|
+
]),
|
|
136
|
+
);
|
|
137
|
+
expect(cbs).toBeInstanceOf(Promise);
|
|
138
|
+
expect(bus.isRerenderEventActive).toBe(true);
|
|
139
|
+
|
|
140
|
+
cbs = (await cbs) as any;
|
|
141
|
+
expect(cbs).toHaveLength(2);
|
|
142
|
+
expect(bus.isRerenderEventActive).toBe(false);
|
|
143
|
+
});
|
|
144
|
+
});
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import type { FieldId } from "../../base";
|
|
2
|
+
import { UTILS } from "../../utils/global.utils";
|
|
3
|
+
import { type SubscriberProps, Subscriber } from "./subscriber";
|
|
4
|
+
|
|
5
|
+
export type CustomProps<T extends any = any> = T extends object ? Omit<T, "event"> : T;
|
|
6
|
+
|
|
7
|
+
export interface TEventMap {
|
|
8
|
+
changes: CustomProps & { event: Omit<TEvent, "changes"> };
|
|
9
|
+
"re-render": { args: any[] };
|
|
10
|
+
"field:update": { id: FieldId };
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export type TEvent = keyof TEventMap;
|
|
14
|
+
|
|
15
|
+
export type EventPropsValue<E extends TEvent = TEvent> = TEventMap[E];
|
|
16
|
+
|
|
17
|
+
type EventCallback<E extends TEvent = TEvent, T extends CustomProps = CustomProps> = (
|
|
18
|
+
scb: SubscriberProps<E, T>,
|
|
19
|
+
) => void | Promise<void>;
|
|
20
|
+
|
|
21
|
+
type TEventHandler = {
|
|
22
|
+
id: string;
|
|
23
|
+
callback: EventCallback;
|
|
24
|
+
subscriber: Subscriber;
|
|
25
|
+
};
|
|
26
|
+
type EventDisposer = () => void;
|
|
27
|
+
|
|
28
|
+
export interface EventBusInterface {
|
|
29
|
+
on<E extends TEvent = TEvent, T extends CustomProps = CustomProps>(
|
|
30
|
+
ev: E,
|
|
31
|
+
handler: EventCallback<E, T>,
|
|
32
|
+
): EventDisposer;
|
|
33
|
+
|
|
34
|
+
emit<E extends TEvent = TEvent, T extends EventPropsValue<E> = EventPropsValue<E>>(
|
|
35
|
+
ev: E,
|
|
36
|
+
props: T,
|
|
37
|
+
toAwaited?: boolean,
|
|
38
|
+
): Promise<void>;
|
|
39
|
+
|
|
40
|
+
batch(evs: ReadonlyArray<readonly [TEvent, any]>): Promise<void>[];
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export class EventBusService implements EventBusInterface {
|
|
44
|
+
private handlers: Map<TEvent, TEventHandler[]> = new Map();
|
|
45
|
+
isRerenderEventActive = false;
|
|
46
|
+
|
|
47
|
+
private cleanUpCallback(ev: TEvent, blogHandler: TEventHandler) {
|
|
48
|
+
// console.info(`Cleaning up cb: `, ev, blogHandler.id)
|
|
49
|
+
|
|
50
|
+
const newHandlers = (this.handlers.get(ev) ?? []).filter(
|
|
51
|
+
(handler) => handler.id !== blogHandler.id,
|
|
52
|
+
);
|
|
53
|
+
this.handlers.set(ev, newHandlers);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// #TODO typesafe global event with the corresponding object type in sbc
|
|
57
|
+
on<E extends TEvent = TEvent, T extends CustomProps = CustomProps>(
|
|
58
|
+
ev: E,
|
|
59
|
+
callback: EventCallback<E, T>,
|
|
60
|
+
): EventDisposer {
|
|
61
|
+
const prevHandlers = this.handlers.get(ev) ?? [];
|
|
62
|
+
const newId = UTILS.generateSubscriberId();
|
|
63
|
+
|
|
64
|
+
const newTEvent: TEventHandler = {
|
|
65
|
+
id: newId,
|
|
66
|
+
callback: callback as EventCallback,
|
|
67
|
+
subscriber: new Subscriber(newId, ev),
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
this.handlers.set(ev, [...prevHandlers, newTEvent]);
|
|
71
|
+
|
|
72
|
+
return this.cleanUpCallback.bind(this, ev, newTEvent);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* If passed, props will overwrite all custom properties
|
|
77
|
+
* on each event handler
|
|
78
|
+
*/
|
|
79
|
+
async emit<E extends TEvent = TEvent, T extends any = CustomProps>(
|
|
80
|
+
ev: E,
|
|
81
|
+
props: T & EventPropsValue<E>,
|
|
82
|
+
toAwaited = true,
|
|
83
|
+
): Promise<void> {
|
|
84
|
+
if (ev === "re-render") {
|
|
85
|
+
this.isRerenderEventActive = true;
|
|
86
|
+
}
|
|
87
|
+
//#DONE emit changes ev for global & passed the correct props
|
|
88
|
+
// console.log("Event: ", ev);
|
|
89
|
+
|
|
90
|
+
const eventHandlers = this.handlers.get(ev) ?? [];
|
|
91
|
+
// console.log(eventHandlers);
|
|
92
|
+
|
|
93
|
+
const eventsPromises = Promise.all(
|
|
94
|
+
eventHandlers.map((handler) => {
|
|
95
|
+
const subscriber = props
|
|
96
|
+
? handler.subscriber.with<T & EventPropsValue<E>>(props as T & EventPropsValue<E>)
|
|
97
|
+
: (handler.subscriber as unknown as SubscriberProps<E, T>);
|
|
98
|
+
return handler.callback(subscriber);
|
|
99
|
+
}),
|
|
100
|
+
);
|
|
101
|
+
|
|
102
|
+
if (toAwaited) {
|
|
103
|
+
await eventsPromises.catch((err) => console.error("Error in event handlers:", err));
|
|
104
|
+
} else {
|
|
105
|
+
eventsPromises
|
|
106
|
+
.then(() => console.log("All handlers resolved"))
|
|
107
|
+
.catch((err) => console.error("Error in event handlers:", err));
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
if (this.isRerenderEventActive) {
|
|
111
|
+
this.isRerenderEventActive = false;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
if (ev !== "changes") {
|
|
115
|
+
await this.emit("changes", { ...props, event: ev });
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// this.cleanUpEvent.call(this, ev)
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Emit multiple events in batch with type-safe event/props pairing
|
|
123
|
+
* @example
|
|
124
|
+
* eventBus.batch([
|
|
125
|
+
* ["field:insert", { fieldOrNode: myField, index: 0 }],
|
|
126
|
+
* ["plugin:mount", { name: "myPlugin" }]
|
|
127
|
+
* ])
|
|
128
|
+
*/
|
|
129
|
+
batch<E1 extends TEvent>(evs: [[E1, EventPropsValue<E1>]]): Promise<void>[];
|
|
130
|
+
batch<E1 extends TEvent, E2 extends TEvent>(
|
|
131
|
+
evs: [[E1, EventPropsValue<E1>], [E2, EventPropsValue<E2>]],
|
|
132
|
+
): Promise<void>[];
|
|
133
|
+
batch<E1 extends TEvent, E2 extends TEvent, E3 extends TEvent>(
|
|
134
|
+
evs: [[E1, EventPropsValue<E1>], [E2, EventPropsValue<E2>], [E3, EventPropsValue<E3>]],
|
|
135
|
+
): Promise<void>[];
|
|
136
|
+
batch<E1 extends TEvent, E2 extends TEvent, E3 extends TEvent, E4 extends TEvent>(
|
|
137
|
+
evs: [
|
|
138
|
+
[E1, EventPropsValue<E1>],
|
|
139
|
+
[E2, EventPropsValue<E2>],
|
|
140
|
+
[E3, EventPropsValue<E3>],
|
|
141
|
+
[E4, EventPropsValue<E4>],
|
|
142
|
+
],
|
|
143
|
+
): Promise<void>[];
|
|
144
|
+
batch<
|
|
145
|
+
E1 extends TEvent,
|
|
146
|
+
E2 extends TEvent,
|
|
147
|
+
E3 extends TEvent,
|
|
148
|
+
E4 extends TEvent,
|
|
149
|
+
E5 extends TEvent,
|
|
150
|
+
>(
|
|
151
|
+
evs: [
|
|
152
|
+
[E1, EventPropsValue<E1>],
|
|
153
|
+
[E2, EventPropsValue<E2>],
|
|
154
|
+
[E3, EventPropsValue<E3>],
|
|
155
|
+
[E4, EventPropsValue<E4>],
|
|
156
|
+
[E5, EventPropsValue<E5>],
|
|
157
|
+
],
|
|
158
|
+
): Promise<void>[];
|
|
159
|
+
batch(evs: ReadonlyArray<readonly [TEvent, any]>): Promise<void>[] {
|
|
160
|
+
return evs.map(([ev, props]) => this.emit(ev, props as any));
|
|
161
|
+
}
|
|
162
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
// import type {
|
|
2
|
+
// FieldLifeCycleLiterals,
|
|
3
|
+
// FnParams,
|
|
4
|
+
// NativeCallback,
|
|
5
|
+
// } from "~/src/engine/life-cycle/fields/fields.lc.types";
|
|
6
|
+
|
|
7
|
+
import type { TEvent, CustomProps, EventPropsValue } from "./eventbus";
|
|
8
|
+
|
|
9
|
+
export interface SCB<E extends TEvent = TEvent> extends EventProps<E> {
|
|
10
|
+
readonly target: string;
|
|
11
|
+
readonly event: Omit<TEvent, "changes">;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export type EventProps<E extends TEvent = TEvent> = {
|
|
15
|
+
value: EventPropsValue<E>;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export type SubscriberProps<
|
|
19
|
+
E extends TEvent = TEvent,
|
|
20
|
+
T extends CustomProps = CustomProps,
|
|
21
|
+
> = SCB & {
|
|
22
|
+
value: Partial<T>;
|
|
23
|
+
} & EventProps<E>;
|
|
24
|
+
|
|
25
|
+
export class Subscriber<E extends TEvent = TEvent> implements SCB<E> {
|
|
26
|
+
readonly target: string;
|
|
27
|
+
readonly event: E;
|
|
28
|
+
value: EventPropsValue<E> = {} as any;
|
|
29
|
+
|
|
30
|
+
constructor(target: string, event: E) {
|
|
31
|
+
this.target = target;
|
|
32
|
+
this.event = event;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
with<T extends CustomProps = CustomProps>(props: T): SubscriberProps<E, T> {
|
|
36
|
+
const { event = "", ...restProps } = props as any;
|
|
37
|
+
|
|
38
|
+
return Object.assign(Object.create(Object.getPrototypeOf(this)), this, {
|
|
39
|
+
// @ts-ignore
|
|
40
|
+
...(event.trim() ? { event } : null),
|
|
41
|
+
// This is only for `re-render` event, I can just update its type
|
|
42
|
+
// but, flem....!!!
|
|
43
|
+
...("value" in restProps ? restProps : { value: restProps }),
|
|
44
|
+
}) as SubscriberProps<E, T>;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export class MutationError extends Error {
|
|
2
|
+
constructor(message: string) {
|
|
3
|
+
super(`MutationError: ${message}`);
|
|
4
|
+
this.name = "MutationError";
|
|
5
|
+
}
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export class MutationPrototypeError extends MutationError {
|
|
9
|
+
constructor(name: string) {
|
|
10
|
+
super(`Trying to mutate '${name}' who has lost his prototype`);
|
|
11
|
+
}
|
|
12
|
+
}
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
// import { beforeEach, describe, expect, it } from "vitest";
|
|
2
|
+
// import type { CoraxTree } from "../../base/types/tree/tree";
|
|
3
|
+
// import { GetDefaultTree } from "../../utils/tree.utils";
|
|
4
|
+
// import { MutationsService } from "./mutations";
|
|
5
|
+
// import { ContainersUtils } from "../../utils/container.utils";
|
|
6
|
+
// import { UTILS } from "../../utils/global.utils";
|
|
7
|
+
// import { MutationError, MutationPrototypeError } from "./mutations.errors";
|
|
8
|
+
// import { FieldsUtils } from "../../utils/fields.utils";
|
|
9
|
+
// import { ContainerClass } from "../../base/classes/container.class";
|
|
10
|
+
// import type { FieldId } from "../../base/types/field/field";
|
|
11
|
+
|
|
12
|
+
// describe("MutationsService", () => {
|
|
13
|
+
// const mutations = new MutationsService();
|
|
14
|
+
// let tree: CoraxTree;
|
|
15
|
+
|
|
16
|
+
// function cleanUp() {
|
|
17
|
+
// tree = GetDefaultTree();
|
|
18
|
+
// }
|
|
19
|
+
|
|
20
|
+
// beforeEach(() => {
|
|
21
|
+
// cleanUp();
|
|
22
|
+
// });
|
|
23
|
+
|
|
24
|
+
// describe("Mutate Container", () => {
|
|
25
|
+
// const container = ContainersUtils.NewContainer("section");
|
|
26
|
+
|
|
27
|
+
// it("should merge values on the correct path", () => {
|
|
28
|
+
// const currentTree = mutations.mutateContainer(
|
|
29
|
+
// {
|
|
30
|
+
// ...tree,
|
|
31
|
+
// order: [container.id],
|
|
32
|
+
// containers: {
|
|
33
|
+
// [container.id]: container,
|
|
34
|
+
// },
|
|
35
|
+
// },
|
|
36
|
+
// {
|
|
37
|
+
// id: container.id,
|
|
38
|
+
// keyValuePairs: {
|
|
39
|
+
// name: "Updated container name",
|
|
40
|
+
// "props.style.background": "chocolate",
|
|
41
|
+
// "props.style.color": "white",
|
|
42
|
+
// } as any,
|
|
43
|
+
// },
|
|
44
|
+
// );
|
|
45
|
+
|
|
46
|
+
// expect(currentTree.containers[container.id].name).toBe("Updated container name");
|
|
47
|
+
// expect(currentTree.containers[container.id].props?.style?.background).toBe("chocolate");
|
|
48
|
+
// expect(currentTree.containers[container.id].props?.style?.color).toBe("white");
|
|
49
|
+
// });
|
|
50
|
+
|
|
51
|
+
// it("should insert string values on the correct path", () => {
|
|
52
|
+
// const currentTree = mutations.mutateContainer(
|
|
53
|
+
// {
|
|
54
|
+
// ...tree,
|
|
55
|
+
// order: [container.id],
|
|
56
|
+
// containers: {
|
|
57
|
+
// [container.id]: ContainersUtils.NewContainer("section", {
|
|
58
|
+
// ...container.getCurrentContainerObject(),
|
|
59
|
+
// props: { style: { color: "whitesmoke" } },
|
|
60
|
+
// }),
|
|
61
|
+
// },
|
|
62
|
+
// },
|
|
63
|
+
// {
|
|
64
|
+
// id: container.id,
|
|
65
|
+
// keyValuePairs: {
|
|
66
|
+
// "props.style.background": "chocolate",
|
|
67
|
+
// } as any,
|
|
68
|
+
// autoMerge: false,
|
|
69
|
+
// },
|
|
70
|
+
// );
|
|
71
|
+
|
|
72
|
+
// expect(currentTree.containers[container.id].props?.style?.background).toBe("chocolate");
|
|
73
|
+
// expect(currentTree.containers[container.id].props?.style?.color).toBeUndefined();
|
|
74
|
+
// });
|
|
75
|
+
|
|
76
|
+
// it("should merge correctly arrays and objects", () => {
|
|
77
|
+
// const firstFieldId = UTILS.generateFieldId();
|
|
78
|
+
// const secondFieldId = UTILS.generateFieldId();
|
|
79
|
+
|
|
80
|
+
// const currentTree = mutations.mutateContainer(
|
|
81
|
+
// {
|
|
82
|
+
// ...tree,
|
|
83
|
+
// order: [container.id],
|
|
84
|
+
// containers: {
|
|
85
|
+
// [container.id]: ContainersUtils.NewContainer("section", {
|
|
86
|
+
// ...container,
|
|
87
|
+
// order: [firstFieldId],
|
|
88
|
+
// props: { style: { color: "whitesmoke" } },
|
|
89
|
+
// }),
|
|
90
|
+
// },
|
|
91
|
+
// },
|
|
92
|
+
// {
|
|
93
|
+
// id: container.id,
|
|
94
|
+
// keyValuePairs: {
|
|
95
|
+
// "props.style": {
|
|
96
|
+
// fontSize: "24px",
|
|
97
|
+
// },
|
|
98
|
+
// order: [secondFieldId],
|
|
99
|
+
// } as any,
|
|
100
|
+
// },
|
|
101
|
+
// );
|
|
102
|
+
|
|
103
|
+
// expect(currentTree.containers[container.id].props?.style?.color).toBe("whitesmoke");
|
|
104
|
+
// expect(currentTree.containers[container.id].props?.style?.fontSize).toBe("24px");
|
|
105
|
+
// expect(currentTree.containers[container.id].order).toEqual([firstFieldId, secondFieldId]);
|
|
106
|
+
// });
|
|
107
|
+
|
|
108
|
+
// it("shouldn't merge array and object", () => {
|
|
109
|
+
// const invokeMutation = mutations.mutateContainer.bind(
|
|
110
|
+
// mutations,
|
|
111
|
+
// {
|
|
112
|
+
// ...tree,
|
|
113
|
+
// order: [container.id],
|
|
114
|
+
// containers: {
|
|
115
|
+
// [container.id]: ContainersUtils.NewContainer("section", {
|
|
116
|
+
// ...container,
|
|
117
|
+
// props: { style: { color: "whitesmoke" } },
|
|
118
|
+
// }),
|
|
119
|
+
// },
|
|
120
|
+
// },
|
|
121
|
+
// {
|
|
122
|
+
// id: container.id,
|
|
123
|
+
// keyValuePairs: {
|
|
124
|
+
// "props.style": ["hello"],
|
|
125
|
+
// } as any,
|
|
126
|
+
// },
|
|
127
|
+
// );
|
|
128
|
+
|
|
129
|
+
// expect(invokeMutation).toThrow(new MutationError("Cannot merge Array into Object"));
|
|
130
|
+
// });
|
|
131
|
+
// });
|
|
132
|
+
|
|
133
|
+
// describe("Insert field", () => {
|
|
134
|
+
// it("should insert field", () => {
|
|
135
|
+
// const container = ContainersUtils.NewContainer("section");
|
|
136
|
+
// const field = FieldsUtils.NewField("text", { containerId: container.id });
|
|
137
|
+
|
|
138
|
+
// const newTree = mutations.insertField(
|
|
139
|
+
// {
|
|
140
|
+
// ...tree,
|
|
141
|
+
// order: [container.id],
|
|
142
|
+
// containers: {
|
|
143
|
+
// [container.id]: container,
|
|
144
|
+
// },
|
|
145
|
+
// },
|
|
146
|
+
// field,
|
|
147
|
+
// );
|
|
148
|
+
|
|
149
|
+
// expect(newTree.order).toEqual([container.id, field.id]);
|
|
150
|
+
// expect(newTree.fields[field.id]).toBe(field);
|
|
151
|
+
// expect(newTree.containers[container.id]).toBeInstanceOf(ContainerClass);
|
|
152
|
+
// expect(newTree.containers[container.id].order).toEqual([field.id]);
|
|
153
|
+
// });
|
|
154
|
+
|
|
155
|
+
// it("shouldn't insert field without prototype", () => {
|
|
156
|
+
// const field = FieldsUtils.NewField("text");
|
|
157
|
+
|
|
158
|
+
// const invokeMutation = mutations.insertField.bind(mutations, tree, { ...field });
|
|
159
|
+
// expect(invokeMutation).toThrow(new MutationPrototypeError(field.name));
|
|
160
|
+
// });
|
|
161
|
+
|
|
162
|
+
// it("sholud insert field at the correct index", () => {
|
|
163
|
+
// const field = FieldsUtils.NewField("text");
|
|
164
|
+
// const field2 = FieldsUtils.NewField("heading");
|
|
165
|
+
// const field3 = FieldsUtils.NewField("text");
|
|
166
|
+
|
|
167
|
+
// let newTree = mutations.insertField(tree, field);
|
|
168
|
+
// newTree = mutations.insertField(newTree, field2);
|
|
169
|
+
// newTree = mutations.insertField(newTree, field3, 1);
|
|
170
|
+
|
|
171
|
+
// expect(newTree.order).toEqual([field.id, field3.id, field2.id]);
|
|
172
|
+
// expect(newTree.fields[newTree.order[1] as FieldId]).toBe(field3);
|
|
173
|
+
// });
|
|
174
|
+
// });
|
|
175
|
+
// });
|
|
176
|
+
|
|
177
|
+
console.log("Mutation test");
|