@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,132 @@
|
|
|
1
|
+
import type { Engine } from "../engine/engine";
|
|
2
|
+
import { ModuleUtils } from "../utils";
|
|
3
|
+
import type { ModuleDisposer, TModule } from "./modules.types";
|
|
4
|
+
|
|
5
|
+
export interface ModuleMap {}
|
|
6
|
+
|
|
7
|
+
export type ModuleName = keyof ModuleMap;
|
|
8
|
+
|
|
9
|
+
export interface ModuleManagerInterface {
|
|
10
|
+
register(modules: TModule[]): void;
|
|
11
|
+
isEnabled(name: ModuleName): boolean;
|
|
12
|
+
|
|
13
|
+
enable(name: ModuleName): void;
|
|
14
|
+
enableAll(): void;
|
|
15
|
+
|
|
16
|
+
disable(name: ModuleName, opts?: { cascade?: boolean }): void;
|
|
17
|
+
disabledAll(): void;
|
|
18
|
+
unregister(name: ModuleName): void;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export class ModuleManager implements ModuleManagerInterface {
|
|
22
|
+
private registry = new Map<ModuleName, TModule>();
|
|
23
|
+
private active = new Map<ModuleName, ModuleDisposer>();
|
|
24
|
+
|
|
25
|
+
private engine: Engine;
|
|
26
|
+
|
|
27
|
+
constructor(engine: Engine) {
|
|
28
|
+
this.engine = engine;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
register(modules: TModule[]): void {
|
|
32
|
+
for (const module of modules) {
|
|
33
|
+
if (this.registry.has(module.name)) {
|
|
34
|
+
throw new Error(`Module "${module.name}" is already registered`);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
this.registry.set(module.name, module);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
isEnabled(name: ModuleName): boolean {
|
|
42
|
+
return this.active.has(name);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
enable(name: ModuleName): void {
|
|
46
|
+
this.enableRec(name, []);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
enableAll(): void {
|
|
50
|
+
for (const name of this.registry.keys()) this.enable(name);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
disabledAll(): void {
|
|
54
|
+
for (const name of this.registry.keys()) this.disable(name, { cascade: true });
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
disable(name: ModuleName, opts: { cascade?: boolean } = {}): void {
|
|
58
|
+
const cleanup = this.active.get(name);
|
|
59
|
+
if (!cleanup) return;
|
|
60
|
+
|
|
61
|
+
const dependents = this.activeDependentsOf(name);
|
|
62
|
+
if (dependents.length > 0) {
|
|
63
|
+
if (!opts.cascade) {
|
|
64
|
+
throw new Error(`Cannot disable "${name}": required by ${dependents.join(", ")}`);
|
|
65
|
+
}
|
|
66
|
+
dependents.forEach((d) => this.disable(d, { cascade: true }));
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
this.active.delete(name);
|
|
70
|
+
cleanup();
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
unregister(name: ModuleName): void {
|
|
74
|
+
this.disable(name, { cascade: true });
|
|
75
|
+
this.registry.delete(name);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Enable module recursively
|
|
80
|
+
*
|
|
81
|
+
* @param name string
|
|
82
|
+
* @param stack string[]
|
|
83
|
+
* @returns void
|
|
84
|
+
*/
|
|
85
|
+
private enableRec(name: ModuleName, stack: string[]): void {
|
|
86
|
+
if (this.active.has(name)) return;
|
|
87
|
+
|
|
88
|
+
if (stack.includes(name)) {
|
|
89
|
+
throw new Error(`Circular dependency: ${[...stack, name].join(" -> ")}`);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const module = this.registry.get(name);
|
|
93
|
+
if (!module) {
|
|
94
|
+
const needer = stack[stack.length - 1];
|
|
95
|
+
throw new Error(
|
|
96
|
+
`Module "${name}" is not registered` + (needer ? ` (required by "${needer}")` : ""),
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
//# enble deps
|
|
101
|
+
for (const dep of module.requires ?? []) {
|
|
102
|
+
this.enableRec(dep, [...stack, name]);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
//# enable active module
|
|
106
|
+
this.activate(module);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
private activate(module: TModule): void {
|
|
110
|
+
const { ctx, dispose } = ModuleUtils.createScopedTCtx(this.engine, module.name);
|
|
111
|
+
|
|
112
|
+
try {
|
|
113
|
+
const custom = module.install(ctx);
|
|
114
|
+
|
|
115
|
+
this.active.set(module.name, () => {
|
|
116
|
+
try {
|
|
117
|
+
if (!custom) return;
|
|
118
|
+
custom?.();
|
|
119
|
+
} finally {
|
|
120
|
+
dispose();
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
} catch (error) {
|
|
124
|
+
dispose();
|
|
125
|
+
throw error;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
private activeDependentsOf(name: ModuleName): ModuleName[] {
|
|
130
|
+
return [...this.active.keys()].filter((n) => this.registry.get(n)?.requires.includes(name));
|
|
131
|
+
}
|
|
132
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type { CommandsInterface } from "../services/commandsbus/commandsbus";
|
|
2
|
+
import type { DocumentInterface } from "../services/document/document";
|
|
3
|
+
import type { EventBusInterface } from "../services/eventbus/eventbus";
|
|
4
|
+
import type { MutationsInterface } from "../services/mutations/mutations";
|
|
5
|
+
import type { ShortcutsInterface } from "../services/shortcuts/shortcuts";
|
|
6
|
+
import type { StateInterface } from "../services/state/state";
|
|
7
|
+
import type { TextMarksInterface } from "../services/text-marks/text-marks";
|
|
8
|
+
import type { UiInterface } from "../services/ui/ui";
|
|
9
|
+
import type { TasksInterface } from "../services/tasks/tasks.service";
|
|
10
|
+
import type { ModuleName } from "./modules-manager";
|
|
11
|
+
|
|
12
|
+
export type TModuleCtx = {
|
|
13
|
+
readonly events: EventBusInterface;
|
|
14
|
+
readonly commands: CommandsInterface;
|
|
15
|
+
readonly mutations: MutationsInterface;
|
|
16
|
+
readonly shortcuts: ShortcutsInterface;
|
|
17
|
+
readonly state: StateInterface;
|
|
18
|
+
readonly document: DocumentInterface;
|
|
19
|
+
readonly ui: UiInterface;
|
|
20
|
+
readonly text: TextMarksInterface;
|
|
21
|
+
readonly tasks: TasksInterface;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export type ModuleDisposer = () => void;
|
|
25
|
+
|
|
26
|
+
export interface TModule {
|
|
27
|
+
readonly name: ModuleName;
|
|
28
|
+
readonly version: string;
|
|
29
|
+
readonly requires: ReadonlyArray<ModuleName>;
|
|
30
|
+
|
|
31
|
+
install(ctx: TModuleCtx): void | ModuleDisposer;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export type BlockRect = {
|
|
35
|
+
top: number;
|
|
36
|
+
left: number;
|
|
37
|
+
right: number;
|
|
38
|
+
width: number;
|
|
39
|
+
height: number;
|
|
40
|
+
bottom: number;
|
|
41
|
+
offsetTop: number;
|
|
42
|
+
offsetLeft: number;
|
|
43
|
+
offsetWidth: number;
|
|
44
|
+
offsetHeight: number;
|
|
45
|
+
parentScrollTop: number;
|
|
46
|
+
parentScrollLeft: number;
|
|
47
|
+
};
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
import type { BlockId, CoraxTree } from "../../base/types/tree/tree";
|
|
2
|
+
import type { CommandMap } from "../../services/commandsbus/commandsbus";
|
|
3
|
+
import type { StateMap } from "../../services/state";
|
|
4
|
+
import type { SlotComponentMap, UIContext } from "../../services/ui";
|
|
5
|
+
import { overlayPositionFromBlockRects, type EditorRect } from "../editor/editor.module";
|
|
6
|
+
import type { BlockRect, TModule } from "../modules.types";
|
|
7
|
+
|
|
8
|
+
export type SelectedBlocks = Map<BlockId, BlockRect>;
|
|
9
|
+
type SelectMode = "replace" | "add" | "toggle" | "remove";
|
|
10
|
+
|
|
11
|
+
declare module "../../services/commandsbus/commandsbus" {
|
|
12
|
+
interface CommandMap {
|
|
13
|
+
"blocks.select": {
|
|
14
|
+
payload: { blocks: StateMap["selection"]["blocks"]; mode?: SelectMode };
|
|
15
|
+
result: void;
|
|
16
|
+
};
|
|
17
|
+
"blocks.clearSelection": { payload: void; result: void };
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
declare module "../../services/eventbus/eventbus" {
|
|
22
|
+
interface TEventMap {
|
|
23
|
+
"blocks:select": CommandMap["blocks.select"]["payload"];
|
|
24
|
+
"selection:changed": {
|
|
25
|
+
blocks: StateMap["selection"]["blocks"];
|
|
26
|
+
previous: StateMap["selection"]["blocks"];
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
declare module "../../services/state/state" {
|
|
32
|
+
interface StateMap {
|
|
33
|
+
selection: { blocks: SelectedBlocks };
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
declare module "../modules-manager" {
|
|
38
|
+
interface ModuleMap {
|
|
39
|
+
selection: {};
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
declare module "../../services/ui/ui" {
|
|
44
|
+
interface SlotComponentMap {
|
|
45
|
+
SelectionOverlay: {
|
|
46
|
+
position: {
|
|
47
|
+
top: number;
|
|
48
|
+
left: number;
|
|
49
|
+
width: number;
|
|
50
|
+
height: number;
|
|
51
|
+
} | null;
|
|
52
|
+
hidden: boolean;
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
interface ComponentContributionIdentityMap {
|
|
57
|
+
"selection-overlay": {};
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function compute(prev: SelectedBlocks, incoming: SelectedBlocks, mode: SelectMode): SelectedBlocks {
|
|
62
|
+
switch (mode) {
|
|
63
|
+
case "replace":
|
|
64
|
+
return new Map(incoming);
|
|
65
|
+
|
|
66
|
+
case "add":
|
|
67
|
+
return new Map([...prev, ...incoming]);
|
|
68
|
+
|
|
69
|
+
case "remove": {
|
|
70
|
+
const result = new Map(prev);
|
|
71
|
+
for (const id of incoming.keys()) {
|
|
72
|
+
result.delete(id);
|
|
73
|
+
}
|
|
74
|
+
return result;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
case "toggle": {
|
|
78
|
+
const result = new Map(prev);
|
|
79
|
+
for (const [id, rect] of incoming) {
|
|
80
|
+
if (result.has(id)) {
|
|
81
|
+
result.delete(id);
|
|
82
|
+
} else {
|
|
83
|
+
result.set(id, rect);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return result;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function sameBlocks(a: SelectedBlocks, b: SelectedBlocks): boolean {
|
|
92
|
+
if (a.size !== b.size) return false;
|
|
93
|
+
|
|
94
|
+
for (const [id, rectA] of a.entries()) {
|
|
95
|
+
const rectB = b.get(id);
|
|
96
|
+
if (!rectB) return false;
|
|
97
|
+
|
|
98
|
+
// Compare all BlockRect properties (shallow equality)
|
|
99
|
+
for (const key in rectA) {
|
|
100
|
+
if (Object.prototype.hasOwnProperty.call(rectA, key)) {
|
|
101
|
+
if (rectA[key as keyof typeof rectA] !== rectB[key as keyof typeof rectB]) {
|
|
102
|
+
return false;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
return true;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function selectionOverlayPropsFromUICtx(
|
|
112
|
+
ui: UIContext,
|
|
113
|
+
editorRect: EditorRect | null,
|
|
114
|
+
order: CoraxTree["order"],
|
|
115
|
+
): SlotComponentMap["SelectionOverlay"]["position"] | null {
|
|
116
|
+
const rects = Array.from(ui?.selection?.blocks?.entries() ?? [])
|
|
117
|
+
.map(([id, rect]) => (order.has(id) ? rect : null))
|
|
118
|
+
.filter(Boolean) as BlockRect[];
|
|
119
|
+
|
|
120
|
+
return overlayPositionFromBlockRects(rects, editorRect);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export const SelectionModule: TModule = {
|
|
124
|
+
name: "selection",
|
|
125
|
+
version: "1.0.0",
|
|
126
|
+
requires: [],
|
|
127
|
+
install(ctx) {
|
|
128
|
+
ctx.state.defineSlice("selection", { blocks: new Map() });
|
|
129
|
+
ctx.ui.contribute("overlay", {
|
|
130
|
+
id: "selection-overlay",
|
|
131
|
+
slot: "overlay",
|
|
132
|
+
order: 1000,
|
|
133
|
+
kind: "component",
|
|
134
|
+
name: "SelectionOverlay",
|
|
135
|
+
when: (ui) => ui.selection!.blocks.size > 0,
|
|
136
|
+
getProps: (ui) => {
|
|
137
|
+
const editorState = ctx.state.get("editor") ?? { editorRect: null };
|
|
138
|
+
const position = selectionOverlayPropsFromUICtx(
|
|
139
|
+
ui,
|
|
140
|
+
editorState?.editorRect,
|
|
141
|
+
ctx.document.get().order,
|
|
142
|
+
);
|
|
143
|
+
|
|
144
|
+
return {
|
|
145
|
+
position,
|
|
146
|
+
hidden: !Array.from(ui.selection!.blocks.keys()).length || !position,
|
|
147
|
+
};
|
|
148
|
+
},
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
const apply = (next: SelectedBlocks) => {
|
|
152
|
+
const previous = ctx.state.get("selection")!.blocks;
|
|
153
|
+
if (sameBlocks(previous, next)) return;
|
|
154
|
+
|
|
155
|
+
ctx.state.set("selection", { blocks: next });
|
|
156
|
+
void ctx.events.emit("selection:changed", { blocks: next, previous });
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
ctx.commands.register("blocks.select", ({ blocks, mode = "replace" }) => {
|
|
160
|
+
// Filter only blocks that exist in the document
|
|
161
|
+
const validBlocks = new Map<BlockId, BlockRect>();
|
|
162
|
+
for (const [id, rect] of blocks) {
|
|
163
|
+
if (ctx.document.has(id)) {
|
|
164
|
+
validBlocks.set(id, rect);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
const prev = ctx.state.get("selection")!.blocks;
|
|
169
|
+
apply(compute(prev, validBlocks, mode));
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
ctx.commands.register("blocks.clearSelection", () => apply(new Map()));
|
|
173
|
+
},
|
|
174
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
console.log("Selection test");
|