@coorax/react 0.0.0-canary.2 → 0.0.0-canary.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/dist/components/commons/button.d.ts +8 -0
- package/dist/components/corax-context-provider.d.ts +6 -0
- package/dist/components/corax-flow.d.ts +4 -0
- package/dist/components/fallbacks/image.fallback.d.ts +3 -0
- package/dist/components/slots/buttons/block-actions-button.d.ts +3 -0
- package/dist/components/slots/buttons/toolbar-button.d.ts +3 -0
- package/dist/components/slots/maps/block-actions.d.ts +1 -0
- package/dist/components/slots/maps/overlays.d.ts +1 -0
- package/dist/components/slots/maps/text-inspector.d.ts +2 -0
- package/dist/components/slots/maps/toolbar.d.ts +1 -0
- package/dist/components/slots/overlays/focus-overlay.d.ts +3 -0
- package/dist/components/slots/overlays/hover-overlay.d.ts +3 -0
- package/dist/components/slots/overlays/selection-overlay.d.ts +3 -0
- package/dist/components/slots/slot-button.d.ts +6 -0
- package/dist/components/slots/slot-component.d.ts +5 -0
- package/dist/components/slots/slots.d.ts +4 -0
- package/dist/constants/field.constants.d.ts +2 -0
- package/dist/hooks/custom/use-custom-ref.d.ts +1 -0
- package/dist/hooks/custom/use-isomorphic-layout-effect.d.ts +2 -0
- package/{src/hooks/index.ts → dist/hooks/index.d.ts} +1 -0
- package/dist/hooks/use-block-entry.d.ts +2 -0
- package/dist/hooks/use-block.d.ts +2 -0
- package/dist/hooks/use-flow.d.ts +17 -0
- package/dist/hooks/use-keyboard.d.ts +10 -0
- package/dist/hooks/use-registry.d.ts +3 -0
- package/dist/hooks/use-renderer.d.ts +2 -0
- package/dist/hooks/use-slice.d.ts +2 -0
- package/dist/hooks/use-slots.d.ts +2 -0
- package/dist/hooks/use-tasks.d.ts +7 -0
- package/dist/hooks/use-text-input.d.ts +2 -0
- package/dist/hooks/use-ui-context.d.ts +2 -0
- package/dist/hooks/useSelection.d.ts +2 -0
- package/{src/index.ts → dist/index.d.ts} +3 -4
- package/dist/index.js +2751 -0
- package/dist/lib/comp.utils.d.ts +4 -0
- package/dist/lib/func.utils.d.ts +3 -0
- package/dist/lib/selection.utils.d.ts +5 -0
- package/dist/lib/slots.utils.d.ts +4 -0
- package/dist/lifecycle/fields/focus.events/onbeforeinput.d.ts +3 -0
- package/dist/lifecycle/fields/focus.events/onblur.d.ts +3 -0
- package/dist/lifecycle/fields/focus.events/onfocus.d.ts +3 -0
- package/dist/lifecycle/fields/keyboard.events/onkeydown.d.ts +3 -0
- package/dist/lifecycle/fields/lifecycle.fields.d.ts +12 -0
- package/dist/lifecycle/fields/lifecycle.fields.types.d.ts +15 -0
- package/dist/lifecycle/fields/lifecycle.utils.d.ts +2 -0
- package/dist/lifecycle/fields/mouse.events/onclick.d.ts +3 -0
- package/dist/lifecycle/fields/mouse.events/ondoubleclick.d.ts +4 -0
- package/dist/lifecycle/fields/mouse.events/onmouseenter.d.ts +3 -0
- package/dist/lifecycle/fields/mouse.events/onmouseleave.d.ts +3 -0
- package/dist/lifecycle/fields/synthetic.events/onload.d.ts +3 -0
- package/dist/lifecycle/fields/synthetic.events/onselect.d.ts +3 -0
- package/dist/lifecycle/index.d.ts +1 -0
- package/dist/modules/autosave.module.d.ts +31 -0
- package/dist/modules/fallbacks.module.d.ts +7 -0
- package/dist/modules/slots.module.d.ts +7 -0
- package/dist/modules/ui-primitives.module.d.ts +7 -0
- package/dist/primitives/fields/field.primitives.d.ts +2 -0
- package/dist/primitives/index.d.ts +3 -0
- package/{src/registry/fallbacks.registry.tsx → dist/registry/fallbacks.registry.d.ts} +3 -4
- package/dist/registry/primitives.registry.d.ts +22 -0
- package/dist/registry/registry.d.ts +15 -0
- package/dist/registry/slots.registry.d.ts +4 -0
- package/{src/registry/ui.primitives.registry.tsx → dist/registry/ui.primitives.registry.d.ts} +3 -4
- package/dist/renderer/renderer.d.ts +23 -0
- package/dist/renderer/renderer.test.d.ts +1 -0
- package/{src/renderer/renderer.types.ts → dist/renderer/renderer.types.d.ts} +0 -1
- package/dist/renderer/renderer.utils.d.ts +14 -0
- package/dist/renderer/utils/divider.renderer.utils.d.ts +6 -0
- package/dist/services/localdb/db/db.d.ts +74 -0
- package/dist/services/localdb/db/db.test.d.ts +1 -0
- package/dist/services/localdb/lexer/lexer.d.ts +14 -0
- package/dist/services/localdb/lexer/lexer.test.d.ts +1 -0
- package/dist/services/localdb/lexer/lexer.types.d.ts +5 -0
- package/dist/services/localdb/operator/operator.d.ts +10 -0
- package/dist/services/localdb/parser/parser.constants.d.ts +3 -0
- package/dist/services/localdb/parser/parser.d.ts +14 -0
- package/dist/services/localdb/parser/parser.test.d.ts +1 -0
- package/dist/services/localdb/parser/parser.types.d.ts +43 -0
- package/dist/services/localdb/sheet/sheet.constants.d.ts +3 -0
- package/dist/services/localdb/sheet/sheet.d.ts +14 -0
- package/dist/services/localdb/sheet/sheet.types.d.ts +13 -0
- package/dist/styles.css +1094 -0
- package/package.json +29 -12
- package/.oxlintrc.json +0 -8
- package/src/components/commons/button.tsx +0 -59
- package/src/components/corax-context-provider.tsx +0 -47
- package/src/components/corax-flow.tsx +0 -44
- package/src/components/fallbacks/image.fallback.tsx +0 -23
- package/src/components/slots/buttons/block-actions-button.tsx +0 -85
- package/src/components/slots/buttons/toolbar-button.tsx +0 -40
- package/src/components/slots/maps/block-actions.tsx +0 -84
- package/src/components/slots/maps/overlays.tsx +0 -9
- package/src/components/slots/maps/text-inspector.tsx +0 -5
- package/src/components/slots/maps/toolbar.tsx +0 -79
- package/src/components/slots/overlays/focus-overlay.tsx +0 -30
- package/src/components/slots/overlays/hover-overlay.tsx +0 -18
- package/src/components/slots/overlays/selection-overlay.tsx +0 -20
- package/src/components/slots/slot-button.tsx +0 -20
- package/src/components/slots/slot-component.tsx +0 -17
- package/src/components/slots/slots.tsx +0 -29
- package/src/constants/field.constants.ts +0 -3
- package/src/hooks/custom/use-custom-ref.ts +0 -11
- package/src/hooks/use-block-entry.ts +0 -44
- package/src/hooks/use-block.ts +0 -11
- package/src/hooks/use-flow.ts +0 -83
- package/src/hooks/use-keyboard.ts +0 -66
- package/src/hooks/use-registry.ts +0 -18
- package/src/hooks/use-renderer.ts +0 -8
- package/src/hooks/use-slice.ts +0 -13
- package/src/hooks/use-slots.ts +0 -13
- package/src/hooks/use-tasks.ts +0 -33
- package/src/hooks/use-text-input.ts +0 -83
- package/src/hooks/use-ui-context.ts +0 -16
- package/src/hooks/useSelection.ts +0 -27
- package/src/lib/comp.utils.tsx +0 -44
- package/src/lib/func.utils.ts +0 -15
- package/src/lib/selection.utils.ts +0 -33
- package/src/lib/slots.utils.tsx +0 -23
- package/src/lifecycle/fields/focus.events/onbeforeinput.ts +0 -21
- package/src/lifecycle/fields/focus.events/onblur.ts +0 -19
- package/src/lifecycle/fields/focus.events/onfocus.ts +0 -16
- package/src/lifecycle/fields/keyboard.events/onkeydown.ts +0 -128
- package/src/lifecycle/fields/lifecycle.fields.ts +0 -116
- package/src/lifecycle/fields/lifecycle.fields.types.ts +0 -33
- package/src/lifecycle/fields/lifecycle.utils.ts +0 -26
- package/src/lifecycle/fields/mouse.events/onclick.ts +0 -79
- package/src/lifecycle/fields/mouse.events/ondoubleclick.ts +0 -27
- package/src/lifecycle/fields/mouse.events/onmouseenter.ts +0 -27
- package/src/lifecycle/fields/mouse.events/onmouseleave.ts +0 -21
- package/src/lifecycle/fields/synthetic.events/onload.ts +0 -16
- package/src/lifecycle/fields/synthetic.events/onselect.ts +0 -57
- package/src/lifecycle/index.ts +0 -1
- package/src/modules/autosave.module.ts +0 -238
- package/src/modules/fallbacks.module.tsx +0 -19
- package/src/modules/slots.module.tsx +0 -28
- package/src/modules/ui-primitives.module.tsx +0 -20
- package/src/primitives/fields/field.primitives.tsx +0 -86
- package/src/primitives/index.ts +0 -8
- package/src/registry/primitives.registry.tsx +0 -57
- package/src/registry/registry.ts +0 -44
- package/src/registry/slots.registry.tsx +0 -6
- package/src/renderer/renderer.test.ts +0 -34
- package/src/renderer/renderer.tsx +0 -164
- package/src/renderer/renderer.utils.tsx +0 -139
- package/src/renderer/utils/divider.renderer.utils.tsx +0 -53
- package/src/services/localdb/db/db.test.ts +0 -351
- package/src/services/localdb/db/db.ts +0 -628
- package/src/services/localdb/lexer/lexer.test.ts +0 -123
- package/src/services/localdb/lexer/lexer.ts +0 -173
- package/src/services/localdb/lexer/lexer.types.ts +0 -34
- package/src/services/localdb/operator/operator.ts +0 -16
- package/src/services/localdb/parser/parser.constants.ts +0 -23
- package/src/services/localdb/parser/parser.test.ts +0 -172
- package/src/services/localdb/parser/parser.ts +0 -427
- package/src/services/localdb/parser/parser.types.ts +0 -56
- package/src/services/localdb/sheet/sheet.constants.ts +0 -32
- package/src/services/localdb/sheet/sheet.ts +0 -47
- package/src/services/localdb/sheet/sheet.types.ts +0 -38
- package/tsconfig.app.json +0 -26
- package/tsconfig.json +0 -9
- package/tsconfig.node.json +0 -9
- package/tsconfig.tsbuildinfo +0 -1
- package/vite.config.ts +0 -9
- /package/{src/components/index.ts → dist/components/index.d.ts} +0 -0
- /package/{src/components/slots/index.ts → dist/components/slots/index.d.ts} +0 -0
- /package/{src/components/slots/maps/index.ts → dist/components/slots/maps/index.d.ts} +0 -0
- /package/{src/modules/index.ts → dist/modules/index.d.ts} +0 -0
- /package/{src/registry/index.ts → dist/registry/index.d.ts} +0 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,2751 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
// src/registry/registry.ts
|
|
4
|
+
class GenericRegistry {
|
|
5
|
+
items = new Map;
|
|
6
|
+
listeners = new Map;
|
|
7
|
+
notify(key) {
|
|
8
|
+
this.listeners.get(key)?.forEach((l) => l());
|
|
9
|
+
}
|
|
10
|
+
get(key) {
|
|
11
|
+
return this.items.get(key);
|
|
12
|
+
}
|
|
13
|
+
register(key, value) {
|
|
14
|
+
this.items.set(key, value);
|
|
15
|
+
this.notify(key);
|
|
16
|
+
return () => {
|
|
17
|
+
this.items.delete(key);
|
|
18
|
+
this.notify(key);
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
subscribe(key, fn) {
|
|
22
|
+
const set = this.listeners.get(key) ?? new Set;
|
|
23
|
+
set.add(fn);
|
|
24
|
+
this.listeners.set(key, set);
|
|
25
|
+
return () => {
|
|
26
|
+
set.delete(fn);
|
|
27
|
+
if (set.size === 0) {
|
|
28
|
+
this.listeners.delete(key);
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// src/registry/slots.registry.tsx
|
|
35
|
+
var SlotsComponentRegistry = new GenericRegistry;
|
|
36
|
+
|
|
37
|
+
// src/components/commons/button.tsx
|
|
38
|
+
import { cva } from "class-variance-authority";
|
|
39
|
+
import { cn } from "cn";
|
|
40
|
+
import { jsxDEV } from "react/jsx-dev-runtime";
|
|
41
|
+
var buttonVariants = cva("group/button inline-flex shrink-0 items-center justify-center rounded-lg border border-transparent bg-clip-padding text-sm font-medium whitespace-nowrap transition-all outline-none select-none focus-visible:border-ring focus-visible:ring-3 focus-visible:ring-ring/50 active:not-aria-[haspopup]:translate-y-px disabled:pointer-events-none disabled:opacity-50 aria-invalid:border-destructive aria-invalid:ring-3 aria-invalid:ring-destructive/20 dark:aria-invalid:border-destructive/50 dark:aria-invalid:ring-destructive/40 [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4", {
|
|
42
|
+
variants: {
|
|
43
|
+
variant: {
|
|
44
|
+
default: "bg-primary text-primary-foreground hover:bg-primary/80",
|
|
45
|
+
outline: "border-border bg-background hover:bg-muted hover:text-foreground aria-expanded:bg-muted aria-expanded:text-foreground dark:border-input dark:bg-input/30 dark:hover:bg-input/50",
|
|
46
|
+
secondary: "bg-secondary text-secondary-foreground hover:bg-[color-mix(in_oklch,var(--secondary),var(--foreground)_5%)] aria-expanded:bg-secondary aria-expanded:text-secondary-foreground",
|
|
47
|
+
ghost: "hover:bg-muted hover:text-foreground aria-expanded:bg-muted aria-expanded:text-foreground dark:hover:bg-muted/50",
|
|
48
|
+
destructive: "bg-destructive/10 text-destructive hover:bg-destructive/20 focus-visible:border-destructive/40 focus-visible:ring-destructive/20 dark:bg-destructive/20 dark:hover:bg-destructive/30 dark:focus-visible:ring-destructive/40",
|
|
49
|
+
link: "text-primary underline-offset-4 hover:underline"
|
|
50
|
+
},
|
|
51
|
+
size: {
|
|
52
|
+
default: "h-8 gap-1.5 px-2.5 has-data-[icon=inline-end]:pr-2 has-data-[icon=inline-start]:pl-2",
|
|
53
|
+
xs: "h-6 gap-1 rounded-[min(var(--radius-md),10px)] px-2 text-xs in-data-[slot=button-group]:rounded-lg has-data-[icon=inline-end]:pr-1.5 has-data-[icon=inline-start]:pl-1.5 [&_svg:not([class*='size-'])]:size-3",
|
|
54
|
+
sm: "h-7 gap-1 rounded-[min(var(--radius-md),12px)] px-2.5 text-[0.8rem] in-data-[slot=button-group]:rounded-lg has-data-[icon=inline-end]:pr-1.5 has-data-[icon=inline-start]:pl-1.5 [&_svg:not([class*='size-'])]:size-3.5",
|
|
55
|
+
lg: "h-9 gap-1.5 px-2.5 has-data-[icon=inline-end]:pr-2 has-data-[icon=inline-start]:pl-2",
|
|
56
|
+
icon: "size-8",
|
|
57
|
+
"icon-xs": "size-6 rounded-[min(var(--radius-md),10px)] in-data-[slot=button-group]:rounded-lg [&_svg:not([class*='size-'])]:size-3",
|
|
58
|
+
"icon-sm": "size-7 rounded-[min(var(--radius-md),12px)] in-data-[slot=button-group]:rounded-lg",
|
|
59
|
+
"icon-lg": "size-9"
|
|
60
|
+
}
|
|
61
|
+
},
|
|
62
|
+
defaultVariants: {
|
|
63
|
+
variant: "default",
|
|
64
|
+
size: "default"
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
function Button({
|
|
68
|
+
className,
|
|
69
|
+
variant = "default",
|
|
70
|
+
size = "default",
|
|
71
|
+
...props
|
|
72
|
+
}) {
|
|
73
|
+
return /* @__PURE__ */ jsxDEV("button", {
|
|
74
|
+
"data-slot": "button",
|
|
75
|
+
"data-variant": variant,
|
|
76
|
+
"data-size": size,
|
|
77
|
+
className: cn(buttonVariants({ variant, size, className })),
|
|
78
|
+
...props
|
|
79
|
+
}, undefined, false, undefined, this);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// src/components/corax-context-provider.tsx
|
|
83
|
+
import {
|
|
84
|
+
EditorModule,
|
|
85
|
+
Engine,
|
|
86
|
+
FocusModule,
|
|
87
|
+
HistoryModule,
|
|
88
|
+
HoverModule,
|
|
89
|
+
InsertionModule,
|
|
90
|
+
SelectionModule,
|
|
91
|
+
TextEditingModule
|
|
92
|
+
} from "@coorax/core";
|
|
93
|
+
import { createContext, useContext, useMemo } from "react";
|
|
94
|
+
|
|
95
|
+
// src/registry/fallbacks.registry.tsx
|
|
96
|
+
var FallbackComponentsRegistry = new GenericRegistry;
|
|
97
|
+
|
|
98
|
+
// src/components/fallbacks/image.fallback.tsx
|
|
99
|
+
import { RefreshCw } from "lucide-react";
|
|
100
|
+
import { cn as cn2 } from "cn";
|
|
101
|
+
import { jsxDEV as jsxDEV2 } from "react/jsx-dev-runtime";
|
|
102
|
+
function ImageFallback({
|
|
103
|
+
className,
|
|
104
|
+
style,
|
|
105
|
+
...props
|
|
106
|
+
}) {
|
|
107
|
+
return /* @__PURE__ */ jsxDEV2("div", {
|
|
108
|
+
className: cn2("w-full h-[360px] flex gap-2 items-center justify-center bg-amber-500/10", className),
|
|
109
|
+
style,
|
|
110
|
+
children: [
|
|
111
|
+
/* @__PURE__ */ jsxDEV2(RefreshCw, {
|
|
112
|
+
className: "animate-spin size-5"
|
|
113
|
+
}, undefined, false, undefined, this),
|
|
114
|
+
/* @__PURE__ */ jsxDEV2("span", {
|
|
115
|
+
className: "font-semibold",
|
|
116
|
+
children: props.name
|
|
117
|
+
}, undefined, false, undefined, this)
|
|
118
|
+
]
|
|
119
|
+
}, undefined, true, undefined, this);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// src/modules/fallbacks.module.tsx
|
|
123
|
+
var FallbacksModule = {
|
|
124
|
+
name: "fallbacks",
|
|
125
|
+
version: "1.0.0",
|
|
126
|
+
requires: [],
|
|
127
|
+
install() {
|
|
128
|
+
FallbackComponentsRegistry.register("image", ImageFallback);
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
// src/components/corax-context-provider.tsx
|
|
133
|
+
import { jsxDEV as jsxDEV3 } from "react/jsx-dev-runtime";
|
|
134
|
+
"use client";
|
|
135
|
+
var CoraxEngineContext = createContext(null);
|
|
136
|
+
function CoraxEngineProvider({ children }) {
|
|
137
|
+
const engine = useMemo(() => {
|
|
138
|
+
const engine2 = new Engine;
|
|
139
|
+
engine2.modules.register([
|
|
140
|
+
HistoryModule,
|
|
141
|
+
AutoSaveModule,
|
|
142
|
+
EditorModule,
|
|
143
|
+
HoverModule,
|
|
144
|
+
InsertionModule,
|
|
145
|
+
FocusModule,
|
|
146
|
+
SelectionModule,
|
|
147
|
+
TextEditingModule,
|
|
148
|
+
SlotsModule,
|
|
149
|
+
FallbacksModule
|
|
150
|
+
]);
|
|
151
|
+
engine2.modules.enableAll();
|
|
152
|
+
return engine2;
|
|
153
|
+
}, []);
|
|
154
|
+
return /* @__PURE__ */ jsxDEV3(CoraxEngineContext.Provider, {
|
|
155
|
+
value: engine,
|
|
156
|
+
children
|
|
157
|
+
}, undefined, false, undefined, this);
|
|
158
|
+
}
|
|
159
|
+
function useEngine() {
|
|
160
|
+
const engine = useContext(CoraxEngineContext);
|
|
161
|
+
if (!engine) {
|
|
162
|
+
throw new Error("useBlogEngine must be used within BlogEngineProvider");
|
|
163
|
+
}
|
|
164
|
+
return engine;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// src/hooks/use-slice.ts
|
|
168
|
+
import { useSyncExternalStore } from "react";
|
|
169
|
+
function useSlice(sliceName) {
|
|
170
|
+
const engine = useEngine();
|
|
171
|
+
const slice = useSyncExternalStore((fn) => engine.state.subscribe(sliceName, fn), () => engine.state.get(sliceName), () => engine.state.get(sliceName));
|
|
172
|
+
return slice;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// src/lib/slots.utils.tsx
|
|
176
|
+
import { cn as cn3 } from "cn";
|
|
177
|
+
function getBlockActionsProps(isOpen, index) {
|
|
178
|
+
return {
|
|
179
|
+
className: cn3("absolute cursor-pointer transition-all disabled:pointer-events-none disabled:opacity-50"),
|
|
180
|
+
style: {
|
|
181
|
+
"--index": isOpen ? 0 : index,
|
|
182
|
+
"--step": isOpen ? "0px" : "38px",
|
|
183
|
+
transform: "translateX(calc((36px*var(--index)) + var(--step))"
|
|
184
|
+
}
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
// src/components/slots/buttons/block-actions-button.tsx
|
|
189
|
+
import { cn as cn4 } from "cn";
|
|
190
|
+
|
|
191
|
+
// src/lib/comp.utils.tsx
|
|
192
|
+
import {
|
|
193
|
+
BoldIcon,
|
|
194
|
+
HeadingIcon,
|
|
195
|
+
ImagePlus,
|
|
196
|
+
ItalicIcon,
|
|
197
|
+
LinkIcon,
|
|
198
|
+
Minus,
|
|
199
|
+
PlusIcon,
|
|
200
|
+
Pointer,
|
|
201
|
+
PointerIcon,
|
|
202
|
+
TextCursor,
|
|
203
|
+
TextInitialIcon,
|
|
204
|
+
UnderlineIcon
|
|
205
|
+
} from "lucide-react";
|
|
206
|
+
import { jsxDEV as jsxDEV4 } from "react/jsx-dev-runtime";
|
|
207
|
+
function CoraxIcon({ icon }) {
|
|
208
|
+
switch (icon) {
|
|
209
|
+
case "TextIcon":
|
|
210
|
+
return /* @__PURE__ */ jsxDEV4(TextCursor, {}, undefined, false, undefined, this);
|
|
211
|
+
case "CursorIcon":
|
|
212
|
+
return /* @__PURE__ */ jsxDEV4(PointerIcon, {}, undefined, false, undefined, this);
|
|
213
|
+
case "HeadingIcon":
|
|
214
|
+
return /* @__PURE__ */ jsxDEV4(HeadingIcon, {}, undefined, false, undefined, this);
|
|
215
|
+
case "PlusIcon":
|
|
216
|
+
return /* @__PURE__ */ jsxDEV4(PlusIcon, {}, undefined, false, undefined, this);
|
|
217
|
+
case "BoldIcon":
|
|
218
|
+
return /* @__PURE__ */ jsxDEV4(BoldIcon, {}, undefined, false, undefined, this);
|
|
219
|
+
case "ItalicIcon":
|
|
220
|
+
return /* @__PURE__ */ jsxDEV4(ItalicIcon, {}, undefined, false, undefined, this);
|
|
221
|
+
case "UnderlineIcon":
|
|
222
|
+
return /* @__PURE__ */ jsxDEV4(UnderlineIcon, {}, undefined, false, undefined, this);
|
|
223
|
+
case "LinkIcon":
|
|
224
|
+
return /* @__PURE__ */ jsxDEV4(LinkIcon, {}, undefined, false, undefined, this);
|
|
225
|
+
case "ImagePlus":
|
|
226
|
+
return /* @__PURE__ */ jsxDEV4(ImagePlus, {}, undefined, false, undefined, this);
|
|
227
|
+
case "TextInitial":
|
|
228
|
+
return /* @__PURE__ */ jsxDEV4(TextInitialIcon, {}, undefined, false, undefined, this);
|
|
229
|
+
case "DividerIcon":
|
|
230
|
+
return /* @__PURE__ */ jsxDEV4(Minus, {}, undefined, false, undefined, this);
|
|
231
|
+
default:
|
|
232
|
+
return /* @__PURE__ */ jsxDEV4(Pointer, {}, undefined, false, undefined, this);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
// src/lifecycle/fields/lifecycle.utils.ts
|
|
237
|
+
function blockRectFromBlockId(id) {
|
|
238
|
+
const domElement = document.getElementById(id);
|
|
239
|
+
if (!domElement)
|
|
240
|
+
return null;
|
|
241
|
+
const parentElement = domElement.closest("#corax_flow_blocks_container");
|
|
242
|
+
if (!parentElement)
|
|
243
|
+
return null;
|
|
244
|
+
const rect = domElement.getBoundingClientRect();
|
|
245
|
+
return {
|
|
246
|
+
top: rect.top,
|
|
247
|
+
left: rect.left,
|
|
248
|
+
width: rect.width,
|
|
249
|
+
right: rect.right,
|
|
250
|
+
height: rect.height,
|
|
251
|
+
bottom: rect.bottom,
|
|
252
|
+
offsetTop: domElement.offsetTop,
|
|
253
|
+
offsetLeft: domElement.offsetLeft,
|
|
254
|
+
offsetHeight: domElement.offsetHeight,
|
|
255
|
+
offsetWidth: domElement.offsetWidth,
|
|
256
|
+
parentScrollLeft: parentElement.scrollLeft,
|
|
257
|
+
parentScrollTop: parentElement.scrollTop
|
|
258
|
+
};
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
// src/components/slots/buttons/block-actions-button.tsx
|
|
262
|
+
import React2 from "react";
|
|
263
|
+
import { jsxDEV as jsxDEV5 } from "react/jsx-dev-runtime";
|
|
264
|
+
var BlockActionButton = React2.memo(function BlockActionButton2({
|
|
265
|
+
c,
|
|
266
|
+
ui,
|
|
267
|
+
...rest
|
|
268
|
+
}) {
|
|
269
|
+
const engine = useEngine();
|
|
270
|
+
const isOpen = useSlice("insertion");
|
|
271
|
+
const focusMemory = useSlice("focus_memory");
|
|
272
|
+
const selection = useSlice("selection");
|
|
273
|
+
const { blocks = [] } = focusMemory ?? {};
|
|
274
|
+
const { blocks: _selectionBLocks = new Map } = selection ?? {};
|
|
275
|
+
const selectedBlocksIds = Array.from(_selectionBLocks?.keys() ?? []);
|
|
276
|
+
const focusedId = selectedBlocksIds[0] ?? blocks?.[blocks.length - 1]?.id;
|
|
277
|
+
if (c.name !== "BlockActionButton")
|
|
278
|
+
return null;
|
|
279
|
+
if (!c.when?.(ui) || c.kind !== "button")
|
|
280
|
+
return null;
|
|
281
|
+
const { variant, size, isBaseButton = false } = c.getProps(ui);
|
|
282
|
+
const enabled = engine.commands.has(c.command);
|
|
283
|
+
return /* @__PURE__ */ jsxDEV5(Button, {
|
|
284
|
+
...rest,
|
|
285
|
+
variant,
|
|
286
|
+
size,
|
|
287
|
+
disabled: !enabled,
|
|
288
|
+
...!isBaseButton ? getBlockActionsProps(!isOpen, c.order ? c.order - 1 : 0) : {
|
|
289
|
+
className: cn4("z-99 cursor-pointer transition-all", {
|
|
290
|
+
"rotate-45": isOpen
|
|
291
|
+
})
|
|
292
|
+
},
|
|
293
|
+
onClick: () => {
|
|
294
|
+
if (c.command === "blocks.insert") {
|
|
295
|
+
const payload2 = c.getPayload(ui);
|
|
296
|
+
const pos = [...engine.document.get().order].indexOf(focusedId);
|
|
297
|
+
engine.commands.tryExecute(c.command, {
|
|
298
|
+
...payload2,
|
|
299
|
+
pos: pos === -1 ? undefined : pos + 1
|
|
300
|
+
});
|
|
301
|
+
setTimeout(() => {
|
|
302
|
+
const rect = blockRectFromBlockId(payload2.block.id);
|
|
303
|
+
if (!rect)
|
|
304
|
+
return;
|
|
305
|
+
engine.commands.tryExecute("blocks.focus", {
|
|
306
|
+
id: payload2.block.id,
|
|
307
|
+
rect
|
|
308
|
+
});
|
|
309
|
+
}, 0);
|
|
310
|
+
return;
|
|
311
|
+
}
|
|
312
|
+
const payload = c.getPayload(ui);
|
|
313
|
+
engine.commands.tryExecute(c.command, payload);
|
|
314
|
+
},
|
|
315
|
+
children: [
|
|
316
|
+
/* @__PURE__ */ jsxDEV5(CoraxIcon, {
|
|
317
|
+
icon: c.icon
|
|
318
|
+
}, undefined, false, undefined, this),
|
|
319
|
+
c.label
|
|
320
|
+
]
|
|
321
|
+
}, undefined, true, undefined, this);
|
|
322
|
+
}, () => true);
|
|
323
|
+
|
|
324
|
+
// src/components/slots/buttons/toolbar-button.tsx
|
|
325
|
+
import React3 from "react";
|
|
326
|
+
import { jsxDEV as jsxDEV6 } from "react/jsx-dev-runtime";
|
|
327
|
+
var ToolbarButton = React3.memo(function ToolbarButton2({
|
|
328
|
+
c,
|
|
329
|
+
ui
|
|
330
|
+
}) {
|
|
331
|
+
const engine = useEngine();
|
|
332
|
+
const marks = useSlice("currentMarks");
|
|
333
|
+
const isActive = !!c.getProps(ui)?.isActive || marks?.find((mark) => mark.type === c.getPayload(ui).format);
|
|
334
|
+
return /* @__PURE__ */ jsxDEV6(Button, {
|
|
335
|
+
onClick: () => {
|
|
336
|
+
engine.commands.tryExecute(c.command, c.getPayload(ui));
|
|
337
|
+
},
|
|
338
|
+
title: c.label,
|
|
339
|
+
size: "icon-xs",
|
|
340
|
+
className: "rounded-[8px] cursor-pointer",
|
|
341
|
+
variant: isActive ? "default" : "outline",
|
|
342
|
+
children: /* @__PURE__ */ jsxDEV6(CoraxIcon, {
|
|
343
|
+
icon: c.icon
|
|
344
|
+
}, undefined, false, undefined, this)
|
|
345
|
+
}, c.id, false, undefined, this);
|
|
346
|
+
}, (prev, next) => {
|
|
347
|
+
const prevActive = prev.c.getProps(prev.ui);
|
|
348
|
+
const nextActive = next.c.getProps(next.ui);
|
|
349
|
+
return prevActive === nextActive;
|
|
350
|
+
});
|
|
351
|
+
|
|
352
|
+
// src/components/slots/overlays/focus-overlay.tsx
|
|
353
|
+
import { useEffect } from "react";
|
|
354
|
+
|
|
355
|
+
// src/lib/func.utils.ts
|
|
356
|
+
function focusDomElement(id) {
|
|
357
|
+
const domElement = document.getElementById(id);
|
|
358
|
+
if (!domElement)
|
|
359
|
+
return;
|
|
360
|
+
domElement.focus();
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
// src/components/slots/overlays/focus-overlay.tsx
|
|
364
|
+
import { jsxDEV as jsxDEV7 } from "react/jsx-dev-runtime";
|
|
365
|
+
function FocusOverlay({ c, ui }) {
|
|
366
|
+
const { position, hidden } = c.getProps(ui);
|
|
367
|
+
const focusedId = ui.focus?.id;
|
|
368
|
+
useEffect(() => {
|
|
369
|
+
if (!focusedId)
|
|
370
|
+
return;
|
|
371
|
+
focusDomElement(focusedId);
|
|
372
|
+
}, [focusedId]);
|
|
373
|
+
if (hidden || !position)
|
|
374
|
+
return null;
|
|
375
|
+
return /* @__PURE__ */ jsxDEV7("div", {
|
|
376
|
+
style: position,
|
|
377
|
+
className: "absolute border border-blue-700 pointer-events-none flex justify-end"
|
|
378
|
+
}, undefined, false, undefined, this);
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
// src/components/slots/overlays/hover-overlay.tsx
|
|
382
|
+
import { jsxDEV as jsxDEV8 } from "react/jsx-dev-runtime";
|
|
383
|
+
function HoverOverlay({ c, ui }) {
|
|
384
|
+
const { position, hidden } = c.getProps(ui);
|
|
385
|
+
if (hidden || !position)
|
|
386
|
+
return null;
|
|
387
|
+
return /* @__PURE__ */ jsxDEV8("div", {
|
|
388
|
+
style: position,
|
|
389
|
+
className: "absolute border-[0.5px] border-blue-400 pointer-events-none flex justify-end"
|
|
390
|
+
}, undefined, false, undefined, this);
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
// src/components/slots/overlays/selection-overlay.tsx
|
|
394
|
+
import { jsxDEV as jsxDEV9 } from "react/jsx-dev-runtime";
|
|
395
|
+
function SelectionOverlay({ c, ui }) {
|
|
396
|
+
const { position, hidden } = c.getProps(ui);
|
|
397
|
+
if (hidden || !position)
|
|
398
|
+
return null;
|
|
399
|
+
return /* @__PURE__ */ jsxDEV9("div", {
|
|
400
|
+
style: position,
|
|
401
|
+
className: "absolute border border-blue-500 pointer-events-none flex justify-end"
|
|
402
|
+
}, undefined, false, undefined, this);
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
// src/modules/slots.module.tsx
|
|
406
|
+
var SlotsModule = {
|
|
407
|
+
name: "slots",
|
|
408
|
+
version: "1.0.0",
|
|
409
|
+
requires: [],
|
|
410
|
+
install() {
|
|
411
|
+
SlotsComponentRegistry.register("BlockActionButton", BlockActionButton);
|
|
412
|
+
SlotsComponentRegistry.register("ToolBarButton", ToolbarButton);
|
|
413
|
+
SlotsComponentRegistry.register("SelectionOverlay", SelectionOverlay);
|
|
414
|
+
SlotsComponentRegistry.register("FocusOverlay", FocusOverlay);
|
|
415
|
+
SlotsComponentRegistry.register("HoverOverlay", HoverOverlay);
|
|
416
|
+
}
|
|
417
|
+
};
|
|
418
|
+
// src/modules/autosave.module.ts
|
|
419
|
+
import { SerializeTree } from "@coorax/core";
|
|
420
|
+
|
|
421
|
+
// src/services/localdb/lexer/lexer.ts
|
|
422
|
+
class Lexer {
|
|
423
|
+
pos = 0;
|
|
424
|
+
word = [];
|
|
425
|
+
tokensTypes = [
|
|
426
|
+
"SELECT",
|
|
427
|
+
"FROM",
|
|
428
|
+
"WHERE",
|
|
429
|
+
"JOIN",
|
|
430
|
+
"INNER",
|
|
431
|
+
"LEFT",
|
|
432
|
+
"RIGHT",
|
|
433
|
+
"FULL",
|
|
434
|
+
"CROSS",
|
|
435
|
+
"ON",
|
|
436
|
+
"AND",
|
|
437
|
+
"OR",
|
|
438
|
+
"AS",
|
|
439
|
+
"INSERT",
|
|
440
|
+
"INTO",
|
|
441
|
+
"VALUES",
|
|
442
|
+
"CREATE",
|
|
443
|
+
"TABLE"
|
|
444
|
+
];
|
|
445
|
+
cleanWord() {
|
|
446
|
+
this.word = [];
|
|
447
|
+
}
|
|
448
|
+
bumpPos() {
|
|
449
|
+
this.pos++;
|
|
450
|
+
}
|
|
451
|
+
flushWord(tokens) {
|
|
452
|
+
if (this.word.length === 0)
|
|
453
|
+
return;
|
|
454
|
+
const currentWord = this.word.join("");
|
|
455
|
+
const upper = currentWord.toUpperCase();
|
|
456
|
+
if (this.tokensTypes.includes(upper)) {
|
|
457
|
+
tokens.push({
|
|
458
|
+
type: upper,
|
|
459
|
+
value: upper
|
|
460
|
+
});
|
|
461
|
+
} else {
|
|
462
|
+
tokens.push({
|
|
463
|
+
type: "IDENTIFIER",
|
|
464
|
+
value: currentWord
|
|
465
|
+
});
|
|
466
|
+
}
|
|
467
|
+
this.cleanWord();
|
|
468
|
+
}
|
|
469
|
+
lex(sql) {
|
|
470
|
+
const sqlString = sql.replace(/[\r\n]+/g, " ").trim();
|
|
471
|
+
const tokens = [];
|
|
472
|
+
if (!sqlString.endsWith(";")) {
|
|
473
|
+
throw new Error(`Failed to execute sql. ';' missing at the end`);
|
|
474
|
+
}
|
|
475
|
+
this.pos = 0;
|
|
476
|
+
this.cleanWord();
|
|
477
|
+
while (this.pos < sqlString.length) {
|
|
478
|
+
const currentChar = sqlString[this.pos];
|
|
479
|
+
if (currentChar === "'" || currentChar === '"') {
|
|
480
|
+
this.flushWord(tokens);
|
|
481
|
+
const quoteChar = currentChar;
|
|
482
|
+
this.bumpPos();
|
|
483
|
+
const strChars = [];
|
|
484
|
+
let closed = false;
|
|
485
|
+
while (this.pos < sqlString.length) {
|
|
486
|
+
const char = sqlString[this.pos];
|
|
487
|
+
if (char === "\\") {
|
|
488
|
+
this.bumpPos();
|
|
489
|
+
if (this.pos < sqlString.length) {
|
|
490
|
+
strChars.push(sqlString[this.pos]);
|
|
491
|
+
this.bumpPos();
|
|
492
|
+
}
|
|
493
|
+
continue;
|
|
494
|
+
}
|
|
495
|
+
if (char === quoteChar) {
|
|
496
|
+
this.bumpPos();
|
|
497
|
+
closed = true;
|
|
498
|
+
break;
|
|
499
|
+
}
|
|
500
|
+
strChars.push(char);
|
|
501
|
+
this.bumpPos();
|
|
502
|
+
}
|
|
503
|
+
if (!closed) {
|
|
504
|
+
throw new Error(`Unterminated string literal at ${this.pos}`);
|
|
505
|
+
}
|
|
506
|
+
tokens.push({
|
|
507
|
+
type: "STRING",
|
|
508
|
+
value: strChars.join("")
|
|
509
|
+
});
|
|
510
|
+
continue;
|
|
511
|
+
}
|
|
512
|
+
if (currentChar === " " || currentChar === "\t") {
|
|
513
|
+
this.flushWord(tokens);
|
|
514
|
+
this.bumpPos();
|
|
515
|
+
continue;
|
|
516
|
+
}
|
|
517
|
+
switch (currentChar) {
|
|
518
|
+
case ",":
|
|
519
|
+
this.flushWord(tokens);
|
|
520
|
+
tokens.push({ type: "COMMA", value: "," });
|
|
521
|
+
this.bumpPos();
|
|
522
|
+
continue;
|
|
523
|
+
case ";":
|
|
524
|
+
this.flushWord(tokens);
|
|
525
|
+
tokens.push({ type: "SEMICOLON", value: ";" });
|
|
526
|
+
this.bumpPos();
|
|
527
|
+
continue;
|
|
528
|
+
case "(":
|
|
529
|
+
this.flushWord(tokens);
|
|
530
|
+
tokens.push({ type: "BRACKETS", value: "(" });
|
|
531
|
+
this.bumpPos();
|
|
532
|
+
continue;
|
|
533
|
+
case ")":
|
|
534
|
+
this.flushWord(tokens);
|
|
535
|
+
tokens.push({ type: "BRACKETS", value: ")" });
|
|
536
|
+
this.bumpPos();
|
|
537
|
+
continue;
|
|
538
|
+
case "*":
|
|
539
|
+
this.flushWord(tokens);
|
|
540
|
+
tokens.push({ type: "STAR", value: "*" });
|
|
541
|
+
this.bumpPos();
|
|
542
|
+
continue;
|
|
543
|
+
case "=":
|
|
544
|
+
this.flushWord(tokens);
|
|
545
|
+
tokens.push({ type: "EQUALS", value: "=" });
|
|
546
|
+
this.bumpPos();
|
|
547
|
+
continue;
|
|
548
|
+
case "?":
|
|
549
|
+
this.flushWord(tokens);
|
|
550
|
+
tokens.push({ type: "QUESTION_MARK", value: "?" });
|
|
551
|
+
this.bumpPos();
|
|
552
|
+
continue;
|
|
553
|
+
default:
|
|
554
|
+
this.word.push(currentChar);
|
|
555
|
+
this.bumpPos();
|
|
556
|
+
continue;
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
this.flushWord(tokens);
|
|
560
|
+
this.pos = 0;
|
|
561
|
+
this.cleanWord();
|
|
562
|
+
return tokens;
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
// src/services/localdb/parser/parser.constants.ts
|
|
567
|
+
var TYPES_DEFS = [
|
|
568
|
+
"TEXT",
|
|
569
|
+
"DATETIME",
|
|
570
|
+
"INTEGER",
|
|
571
|
+
"UUID",
|
|
572
|
+
"OBJECT",
|
|
573
|
+
"ARRAY[STRING]",
|
|
574
|
+
"FILE",
|
|
575
|
+
"BLOB"
|
|
576
|
+
];
|
|
577
|
+
var TYPES_DEFS_MAPPING = {
|
|
578
|
+
TEXT: "string",
|
|
579
|
+
INTEGER: "number",
|
|
580
|
+
DATETIME: "number",
|
|
581
|
+
UUID: "uuid",
|
|
582
|
+
OBJECT: "object",
|
|
583
|
+
"ARRAY[STRING]": "array.string",
|
|
584
|
+
FILE: "file",
|
|
585
|
+
BLOB: "blob"
|
|
586
|
+
};
|
|
587
|
+
|
|
588
|
+
// src/services/localdb/parser/parser.ts
|
|
589
|
+
class Parser {
|
|
590
|
+
statementTypes = ["SELECT", "INSERT", "CREATE"];
|
|
591
|
+
foundTable(tokens) {
|
|
592
|
+
return tokens.find((token, idx) => {
|
|
593
|
+
return token.type === "IDENTIFIER" && ["FROM", "INTO", "TABLE"].includes(tokens[idx - 1]?.type);
|
|
594
|
+
});
|
|
595
|
+
}
|
|
596
|
+
parseSelect(tokens) {
|
|
597
|
+
const fromIndex = tokens.findIndex((t) => t.type === "FROM");
|
|
598
|
+
if (fromIndex === -1) {
|
|
599
|
+
throw new Error("Invalid SELECT statement: missing FROM clause");
|
|
600
|
+
}
|
|
601
|
+
const isAllFieldsWanted = tokens.slice(0, fromIndex).some((t) => t.type === "STAR");
|
|
602
|
+
const columns = isAllFieldsWanted ? ["*"] : tokens.slice(1, fromIndex).filter((t) => t.type === "IDENTIFIER" || t.type === "STRING").map((t) => t.value);
|
|
603
|
+
const tableToken = tokens[fromIndex + 1];
|
|
604
|
+
if (!tableToken || tableToken.type !== "IDENTIFIER") {
|
|
605
|
+
throw new Error("Invalid SELECT statement: missing table name after FROM");
|
|
606
|
+
}
|
|
607
|
+
const mainTable = tableToken.value;
|
|
608
|
+
let cursor = fromIndex + 2;
|
|
609
|
+
let mainAlias;
|
|
610
|
+
if (cursor < tokens.length) {
|
|
611
|
+
if (tokens[cursor]?.type === "AS" && tokens[cursor + 1]?.type === "IDENTIFIER") {
|
|
612
|
+
mainAlias = tokens[cursor + 1].value;
|
|
613
|
+
cursor += 2;
|
|
614
|
+
} else if (tokens[cursor]?.type === "IDENTIFIER" && !["JOIN", "INNER", "LEFT", "RIGHT", "FULL", "CROSS", "WHERE", "SEMICOLON"].includes(tokens[cursor].type)) {
|
|
615
|
+
mainAlias = tokens[cursor].value;
|
|
616
|
+
cursor++;
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
const joins = [];
|
|
620
|
+
while (cursor < tokens.length) {
|
|
621
|
+
const currentToken = tokens[cursor];
|
|
622
|
+
if (!currentToken || currentToken.type === "SEMICOLON" || currentToken.type === "WHERE") {
|
|
623
|
+
break;
|
|
624
|
+
}
|
|
625
|
+
let joinType = "JOIN";
|
|
626
|
+
let hasJoin = false;
|
|
627
|
+
if (currentToken.type === "JOIN") {
|
|
628
|
+
joinType = "JOIN";
|
|
629
|
+
cursor++;
|
|
630
|
+
hasJoin = true;
|
|
631
|
+
} else if (["INNER", "LEFT", "RIGHT", "FULL", "CROSS"].includes(currentToken.type) && tokens[cursor + 1]?.type === "JOIN") {
|
|
632
|
+
joinType = currentToken.type;
|
|
633
|
+
cursor += 2;
|
|
634
|
+
hasJoin = true;
|
|
635
|
+
}
|
|
636
|
+
if (!hasJoin) {
|
|
637
|
+
break;
|
|
638
|
+
}
|
|
639
|
+
const joinTableToken = tokens[cursor];
|
|
640
|
+
if (!joinTableToken || joinTableToken.type !== "IDENTIFIER") {
|
|
641
|
+
throw new Error(`Expected table name after ${joinType} JOIN`);
|
|
642
|
+
}
|
|
643
|
+
const joinTable = joinTableToken.value;
|
|
644
|
+
cursor++;
|
|
645
|
+
let joinAlias;
|
|
646
|
+
if (cursor < tokens.length) {
|
|
647
|
+
if (tokens[cursor]?.type === "AS" && tokens[cursor + 1]?.type === "IDENTIFIER") {
|
|
648
|
+
joinAlias = tokens[cursor + 1].value;
|
|
649
|
+
cursor += 2;
|
|
650
|
+
} else if (tokens[cursor]?.type === "IDENTIFIER" && tokens[cursor].value.toUpperCase() !== "ON" && !["JOIN", "INNER", "LEFT", "RIGHT", "FULL", "CROSS", "WHERE", "SEMICOLON"].includes(tokens[cursor].type)) {
|
|
651
|
+
joinAlias = tokens[cursor].value;
|
|
652
|
+
cursor++;
|
|
653
|
+
}
|
|
654
|
+
}
|
|
655
|
+
let joinCondition;
|
|
656
|
+
if (tokens[cursor]?.type === "ON") {
|
|
657
|
+
cursor++;
|
|
658
|
+
const leftToken = tokens[cursor];
|
|
659
|
+
cursor++;
|
|
660
|
+
const opToken = tokens[cursor];
|
|
661
|
+
cursor++;
|
|
662
|
+
const rightToken = tokens[cursor];
|
|
663
|
+
cursor++;
|
|
664
|
+
if (!leftToken || !opToken || !rightToken || opToken.type !== "EQUALS" && opToken.value !== "=") {
|
|
665
|
+
throw new Error(`Invalid ON clause syntax in JOIN ${joinTable}`);
|
|
666
|
+
}
|
|
667
|
+
const leftParts = leftToken.value.split(".");
|
|
668
|
+
const rightParts = rightToken.value.split(".");
|
|
669
|
+
const leftTable = leftParts.length > 1 ? leftParts[0] : undefined;
|
|
670
|
+
const leftColumn = leftParts.length > 1 ? leftParts[1] : leftParts[0];
|
|
671
|
+
const rightTable = rightParts.length > 1 ? rightParts[0] : undefined;
|
|
672
|
+
const rightColumn = rightParts.length > 1 ? rightParts[1] : rightParts[0];
|
|
673
|
+
joinCondition = {
|
|
674
|
+
leftTable,
|
|
675
|
+
leftColumn,
|
|
676
|
+
operator: "=",
|
|
677
|
+
rightTable,
|
|
678
|
+
rightColumn
|
|
679
|
+
};
|
|
680
|
+
}
|
|
681
|
+
joins.push({
|
|
682
|
+
type: joinType,
|
|
683
|
+
table: joinTable,
|
|
684
|
+
alias: joinAlias,
|
|
685
|
+
on: joinCondition
|
|
686
|
+
});
|
|
687
|
+
}
|
|
688
|
+
const where = [];
|
|
689
|
+
if (tokens[cursor]?.type === "WHERE") {
|
|
690
|
+
cursor++;
|
|
691
|
+
while (cursor < tokens.length) {
|
|
692
|
+
const colToken = tokens[cursor];
|
|
693
|
+
if (!colToken || colToken.type === "SEMICOLON")
|
|
694
|
+
break;
|
|
695
|
+
cursor++;
|
|
696
|
+
const opToken = tokens[cursor];
|
|
697
|
+
cursor++;
|
|
698
|
+
const valToken = tokens[cursor];
|
|
699
|
+
cursor++;
|
|
700
|
+
if (colToken && opToken && valToken) {
|
|
701
|
+
const colParts = colToken.value.split(".");
|
|
702
|
+
const table = colParts.length > 1 ? colParts[0] : undefined;
|
|
703
|
+
const column = colParts.length > 1 ? colParts[1] : colParts[0];
|
|
704
|
+
where.push({
|
|
705
|
+
table,
|
|
706
|
+
column,
|
|
707
|
+
operator: opToken.value || "=",
|
|
708
|
+
value: valToken.value
|
|
709
|
+
});
|
|
710
|
+
}
|
|
711
|
+
if (tokens[cursor]?.type === "AND" || tokens[cursor]?.type === "OR") {
|
|
712
|
+
cursor++;
|
|
713
|
+
} else {
|
|
714
|
+
break;
|
|
715
|
+
}
|
|
716
|
+
}
|
|
717
|
+
}
|
|
718
|
+
return {
|
|
719
|
+
type: "SELECT",
|
|
720
|
+
columns,
|
|
721
|
+
table: mainTable,
|
|
722
|
+
alias: mainAlias,
|
|
723
|
+
joins: joins.length > 0 ? joins : undefined,
|
|
724
|
+
where: where.length > 0 ? where : undefined
|
|
725
|
+
};
|
|
726
|
+
}
|
|
727
|
+
parseInsert(tokens, params, paramState = { index: 0 }) {
|
|
728
|
+
const intoIndex = tokens.findIndex((t) => t.type === "INTO");
|
|
729
|
+
if (intoIndex === -1) {
|
|
730
|
+
throw new Error("Invalid INSERT statement: missing INTO keyword");
|
|
731
|
+
}
|
|
732
|
+
const tableToken = tokens[intoIndex + 1];
|
|
733
|
+
if (!tableToken || tableToken.type !== "IDENTIFIER") {
|
|
734
|
+
throw new Error("Invalid INSERT statement: missing table name");
|
|
735
|
+
}
|
|
736
|
+
const table = tableToken.value;
|
|
737
|
+
let cursor = intoIndex + 2;
|
|
738
|
+
let fields;
|
|
739
|
+
if (tokens[cursor]?.type === "BRACKETS" && tokens[cursor]?.value === "(") {
|
|
740
|
+
cursor++;
|
|
741
|
+
fields = [];
|
|
742
|
+
while (cursor < tokens.length) {
|
|
743
|
+
const t = tokens[cursor];
|
|
744
|
+
if (t.type === "BRACKETS" && t.value === ")") {
|
|
745
|
+
cursor++;
|
|
746
|
+
break;
|
|
747
|
+
}
|
|
748
|
+
if (t.type === "IDENTIFIER") {
|
|
749
|
+
fields.push(t.value);
|
|
750
|
+
}
|
|
751
|
+
cursor++;
|
|
752
|
+
}
|
|
753
|
+
}
|
|
754
|
+
const valuesIndex = tokens.slice(cursor).findIndex((t) => t.type === "VALUES");
|
|
755
|
+
if (valuesIndex === -1) {
|
|
756
|
+
throw new Error("Invalid INSERT statement: missing VALUES keyword");
|
|
757
|
+
}
|
|
758
|
+
cursor += valuesIndex + 1;
|
|
759
|
+
const statements = [];
|
|
760
|
+
while (cursor < tokens.length) {
|
|
761
|
+
if (tokens[cursor]?.type === "SEMICOLON")
|
|
762
|
+
break;
|
|
763
|
+
if (tokens[cursor]?.type === "BRACKETS" && tokens[cursor]?.value === "(") {
|
|
764
|
+
cursor++;
|
|
765
|
+
const values = [];
|
|
766
|
+
while (cursor < tokens.length) {
|
|
767
|
+
const t = tokens[cursor];
|
|
768
|
+
if (t.type === "BRACKETS" && t.value === ")") {
|
|
769
|
+
cursor++;
|
|
770
|
+
break;
|
|
771
|
+
}
|
|
772
|
+
if (t.type === "COMMA") {
|
|
773
|
+
cursor++;
|
|
774
|
+
continue;
|
|
775
|
+
}
|
|
776
|
+
if (t.type === "QUESTION_MARK") {
|
|
777
|
+
if (params && paramState.index < params.length) {
|
|
778
|
+
values.push(params[paramState.index++]);
|
|
779
|
+
} else {
|
|
780
|
+
values.push(null);
|
|
781
|
+
}
|
|
782
|
+
} else if (t.type === "STRING") {
|
|
783
|
+
values.push(t.value);
|
|
784
|
+
} else if (t.type === "NUMBER" || /^-?\d+(\.\d+)?$/.test(t.value)) {
|
|
785
|
+
values.push(Number(t.value));
|
|
786
|
+
} else {
|
|
787
|
+
values.push(t.value);
|
|
788
|
+
}
|
|
789
|
+
cursor++;
|
|
790
|
+
}
|
|
791
|
+
statements.push({
|
|
792
|
+
type: "INSERT",
|
|
793
|
+
table,
|
|
794
|
+
fields,
|
|
795
|
+
columns: values.map((v) => typeof v === "object" ? JSON.stringify(v) : String(v ?? "")),
|
|
796
|
+
values
|
|
797
|
+
});
|
|
798
|
+
} else {
|
|
799
|
+
cursor++;
|
|
800
|
+
}
|
|
801
|
+
}
|
|
802
|
+
return statements;
|
|
803
|
+
}
|
|
804
|
+
parseCreate(tokens) {
|
|
805
|
+
const statements = [];
|
|
806
|
+
let cursor = 0;
|
|
807
|
+
while (cursor < tokens.length) {
|
|
808
|
+
const createIdx = tokens.slice(cursor).findIndex((t) => t.type === "CREATE");
|
|
809
|
+
if (createIdx === -1)
|
|
810
|
+
break;
|
|
811
|
+
const startIdx = cursor + createIdx;
|
|
812
|
+
const semiIdx = tokens.slice(startIdx).findIndex((t) => t.type === "SEMICOLON");
|
|
813
|
+
if (semiIdx === -1) {
|
|
814
|
+
throw new Error("Invalid CREATE statement: missing ';' at end");
|
|
815
|
+
}
|
|
816
|
+
const currentTokens = tokens.slice(startIdx, startIdx + semiIdx + 1);
|
|
817
|
+
cursor = startIdx + semiIdx + 1;
|
|
818
|
+
const nextToken = currentTokens[1];
|
|
819
|
+
if (!nextToken || !["TABLE", "INDEX"].includes(nextToken.type)) {
|
|
820
|
+
throw new Error(`Invalid statement at near ${nextToken?.value}. Should be TABLE or INDEX!`);
|
|
821
|
+
}
|
|
822
|
+
const currentTable = this.foundTable(currentTokens);
|
|
823
|
+
if (!currentTable) {
|
|
824
|
+
throw new Error("Cannot find table name in CREATE TABLE statement");
|
|
825
|
+
}
|
|
826
|
+
const openBracketIndex = currentTokens.findIndex((t) => t.type === "BRACKETS" && t.value === "(");
|
|
827
|
+
const closeBracketIndex = currentTokens.findIndex((t) => t.type === "BRACKETS" && t.value === ")");
|
|
828
|
+
if (openBracketIndex === -1 || closeBracketIndex === -1) {
|
|
829
|
+
throw new Error("Invalid statement. '(' or ')' is missing in CREATE TABLE!");
|
|
830
|
+
}
|
|
831
|
+
const creationColumns = [];
|
|
832
|
+
const innerTokens = currentTokens.slice(openBracketIndex + 1, closeBracketIndex);
|
|
833
|
+
const isTypeDefOrDecorators = (token) => TYPES_DEFS.includes(token.value.toUpperCase()) || token.type !== "IDENTIFIER";
|
|
834
|
+
for (let i = 0;i < innerTokens.length; i++) {
|
|
835
|
+
const token = innerTokens[i];
|
|
836
|
+
if (isTypeDefOrDecorators(token))
|
|
837
|
+
continue;
|
|
838
|
+
const tokenTypeDef = innerTokens[i + 1];
|
|
839
|
+
if (!tokenTypeDef)
|
|
840
|
+
continue;
|
|
841
|
+
const mappedType = TYPES_DEFS_MAPPING[tokenTypeDef.value.toUpperCase()];
|
|
842
|
+
if (!mappedType) {
|
|
843
|
+
throw new Error(`Unknown type at near ${tokenTypeDef.value}`);
|
|
844
|
+
}
|
|
845
|
+
creationColumns.push(`${token.value}:${mappedType}`);
|
|
846
|
+
}
|
|
847
|
+
statements.push({
|
|
848
|
+
type: "CREATE",
|
|
849
|
+
table: currentTable.value,
|
|
850
|
+
columns: creationColumns
|
|
851
|
+
});
|
|
852
|
+
}
|
|
853
|
+
return statements;
|
|
854
|
+
}
|
|
855
|
+
parse(tokens, params) {
|
|
856
|
+
if (tokens.length < 5) {
|
|
857
|
+
throw new Error(`Invalid tokens array with missing elements!`);
|
|
858
|
+
}
|
|
859
|
+
const firstStatement = tokens[0];
|
|
860
|
+
if (!this.statementTypes.includes(firstStatement.type)) {
|
|
861
|
+
throw new Error(`Invalid statement at near ${firstStatement.value}`);
|
|
862
|
+
}
|
|
863
|
+
const paramState = { index: 0 };
|
|
864
|
+
if (firstStatement.type === "CREATE") {
|
|
865
|
+
return this.parseCreate(tokens);
|
|
866
|
+
}
|
|
867
|
+
if (firstStatement.type === "SELECT") {
|
|
868
|
+
return [this.parseSelect(tokens)];
|
|
869
|
+
}
|
|
870
|
+
if (firstStatement.type === "INSERT") {
|
|
871
|
+
const stmtChunks = [];
|
|
872
|
+
let currentChunk = [];
|
|
873
|
+
for (const t of tokens) {
|
|
874
|
+
currentChunk.push(t);
|
|
875
|
+
if (t.type === "SEMICOLON") {
|
|
876
|
+
stmtChunks.push(currentChunk);
|
|
877
|
+
currentChunk = [];
|
|
878
|
+
}
|
|
879
|
+
}
|
|
880
|
+
if (currentChunk.length > 0)
|
|
881
|
+
stmtChunks.push(currentChunk);
|
|
882
|
+
const results = [];
|
|
883
|
+
for (const chunk of stmtChunks) {
|
|
884
|
+
if (chunk.some((t) => t.type === "INSERT")) {
|
|
885
|
+
results.push(...this.parseInsert(chunk, params, paramState));
|
|
886
|
+
}
|
|
887
|
+
}
|
|
888
|
+
return results;
|
|
889
|
+
}
|
|
890
|
+
throw new Error(`Unsupported statement type: ${firstStatement.type}`);
|
|
891
|
+
}
|
|
892
|
+
}
|
|
893
|
+
|
|
894
|
+
// src/services/localdb/operator/operator.ts
|
|
895
|
+
class DBClient {
|
|
896
|
+
lexer = new Lexer;
|
|
897
|
+
parser = new Parser;
|
|
898
|
+
NewRequest(query, params) {
|
|
899
|
+
return this.parser.parse(this.lexer.lex(query), params);
|
|
900
|
+
}
|
|
901
|
+
}
|
|
902
|
+
|
|
903
|
+
// src/services/localdb/sheet/sheet.ts
|
|
904
|
+
class TablesSheet {
|
|
905
|
+
tables = [];
|
|
906
|
+
constructor() {}
|
|
907
|
+
statementToTable(stm) {
|
|
908
|
+
const fields = {};
|
|
909
|
+
const order = [];
|
|
910
|
+
for (const [key, value] of stm.columns.map((cl) => cl.split(":"))) {
|
|
911
|
+
const fieldType = value;
|
|
912
|
+
const newField = fieldType;
|
|
913
|
+
fields[key] = newField;
|
|
914
|
+
if (order.includes(key)) {
|
|
915
|
+
throw new Error(`Duplicate field at statement near ${key}`);
|
|
916
|
+
}
|
|
917
|
+
order.push(key);
|
|
918
|
+
}
|
|
919
|
+
return {
|
|
920
|
+
name: stm.table,
|
|
921
|
+
fields,
|
|
922
|
+
order
|
|
923
|
+
};
|
|
924
|
+
}
|
|
925
|
+
async RegisterSheet(statements) {
|
|
926
|
+
const tables = statements.map((stm) => this.statementToTable(stm));
|
|
927
|
+
this.tables = tables;
|
|
928
|
+
return tables;
|
|
929
|
+
}
|
|
930
|
+
GetTables() {
|
|
931
|
+
return this.tables;
|
|
932
|
+
}
|
|
933
|
+
}
|
|
934
|
+
|
|
935
|
+
// src/services/localdb/sheet/sheet.constants.ts
|
|
936
|
+
import z from "zod";
|
|
937
|
+
var DEFAULT_FIELDS_SCHEMA = new Map([
|
|
938
|
+
["string", z.string()],
|
|
939
|
+
["number", z.number()],
|
|
940
|
+
["bigint", z.bigint()],
|
|
941
|
+
["symbol", z.symbol()],
|
|
942
|
+
["undefined", z.undefined()],
|
|
943
|
+
["boolean", z.boolean()],
|
|
944
|
+
["object", z.record(z.string(), z.any()).or(z.object({}))],
|
|
945
|
+
["uuid", z.string()],
|
|
946
|
+
["array.string", z.array(z.string())],
|
|
947
|
+
[
|
|
948
|
+
"file",
|
|
949
|
+
z.custom((val) => {
|
|
950
|
+
if (typeof Blob !== "undefined" && val instanceof Blob)
|
|
951
|
+
return true;
|
|
952
|
+
if (typeof File !== "undefined" && val instanceof File)
|
|
953
|
+
return true;
|
|
954
|
+
if (typeof val === "object" && val !== null)
|
|
955
|
+
return true;
|
|
956
|
+
return false;
|
|
957
|
+
})
|
|
958
|
+
],
|
|
959
|
+
[
|
|
960
|
+
"blob",
|
|
961
|
+
z.custom((val) => {
|
|
962
|
+
if (typeof Blob !== "undefined" && val instanceof Blob)
|
|
963
|
+
return true;
|
|
964
|
+
if (typeof File !== "undefined" && val instanceof File)
|
|
965
|
+
return true;
|
|
966
|
+
if (typeof val === "object" && val !== null)
|
|
967
|
+
return true;
|
|
968
|
+
return false;
|
|
969
|
+
})
|
|
970
|
+
]
|
|
971
|
+
]);
|
|
972
|
+
|
|
973
|
+
// src/services/localdb/db/db.ts
|
|
974
|
+
class LocalDB {
|
|
975
|
+
DBName = "MYDB";
|
|
976
|
+
DBVersion = 1;
|
|
977
|
+
defaultStores = [];
|
|
978
|
+
DBTypesStoreName = "__db.types";
|
|
979
|
+
isAvailable;
|
|
980
|
+
isError = null;
|
|
981
|
+
db = null;
|
|
982
|
+
client = new DBClient;
|
|
983
|
+
sheet = new TablesSheet;
|
|
984
|
+
constructor(DbName) {
|
|
985
|
+
if (DbName)
|
|
986
|
+
this.DBName = DbName;
|
|
987
|
+
this.isAvailable = typeof window !== "undefined" && !!window?.indexedDB;
|
|
988
|
+
}
|
|
989
|
+
onDBError(cb, ev) {
|
|
990
|
+
console.error("[ERROR] IndexedDB error:", ev);
|
|
991
|
+
this.isError = new Error(ev.target?.error?.name ?? ev.type);
|
|
992
|
+
cb(new Error(ev.target?.error?.name ?? ev.type));
|
|
993
|
+
}
|
|
994
|
+
onDBSuccess(cb, ev) {
|
|
995
|
+
this.db = ev.target.result;
|
|
996
|
+
this.isError = null;
|
|
997
|
+
cb(null);
|
|
998
|
+
}
|
|
999
|
+
closeConnection() {
|
|
1000
|
+
if (this.db) {
|
|
1001
|
+
this.db.close();
|
|
1002
|
+
this.db = null;
|
|
1003
|
+
}
|
|
1004
|
+
}
|
|
1005
|
+
ensureObjectStores(db, storeNames = this.defaultStores) {
|
|
1006
|
+
for (const name of storeNames) {
|
|
1007
|
+
if (db.objectStoreNames.contains(name))
|
|
1008
|
+
continue;
|
|
1009
|
+
db.createObjectStore(name, { keyPath: "id" });
|
|
1010
|
+
}
|
|
1011
|
+
}
|
|
1012
|
+
bumpVersion(value) {
|
|
1013
|
+
this.DBVersion = value ?? this.DBVersion + 1;
|
|
1014
|
+
}
|
|
1015
|
+
async DeleteDB() {
|
|
1016
|
+
if (!this.isAvailable) {
|
|
1017
|
+
console.warn("[WARN] IndexDB not available in this browser!");
|
|
1018
|
+
return new Error("[ERROR] IndexDB not available in this browser!");
|
|
1019
|
+
}
|
|
1020
|
+
this.closeConnection();
|
|
1021
|
+
return new Promise((resolve) => {
|
|
1022
|
+
const request = window.indexedDB.deleteDatabase(this.DBName);
|
|
1023
|
+
request.onsuccess = () => {
|
|
1024
|
+
this.db = null;
|
|
1025
|
+
resolve(null);
|
|
1026
|
+
};
|
|
1027
|
+
request.onerror = () => {
|
|
1028
|
+
console.error("[ERROR] Failed to delete database", request.error);
|
|
1029
|
+
resolve(request.error ?? new Error("Failed to delete database"));
|
|
1030
|
+
};
|
|
1031
|
+
request.onblocked = () => {
|
|
1032
|
+
console.warn("[WARN] Database deletion blocked. Close all open connections first.");
|
|
1033
|
+
resolve(new Error("[WARN] Database deletion blocked. Close all open connections first."));
|
|
1034
|
+
};
|
|
1035
|
+
});
|
|
1036
|
+
}
|
|
1037
|
+
async init() {
|
|
1038
|
+
return new Promise((resolve, reject) => {
|
|
1039
|
+
if (!this.isAvailable) {
|
|
1040
|
+
console.warn("[WARN] IndexDB not available in this browser!");
|
|
1041
|
+
return;
|
|
1042
|
+
}
|
|
1043
|
+
if (this.db)
|
|
1044
|
+
resolve(true);
|
|
1045
|
+
const request = window.indexedDB.open(this.DBName, this.DBVersion !== 1 ? this.DBVersion : undefined);
|
|
1046
|
+
request.onupgradeneeded = () => {
|
|
1047
|
+
this.ensureObjectStores(request.result, this.defaultStores);
|
|
1048
|
+
};
|
|
1049
|
+
request.onerror = this.onDBError.bind(this, reject);
|
|
1050
|
+
request.onsuccess = (ev) => {
|
|
1051
|
+
this.bumpVersion(request.result.version);
|
|
1052
|
+
this.onDBSuccess(() => resolve(true), ev);
|
|
1053
|
+
};
|
|
1054
|
+
});
|
|
1055
|
+
}
|
|
1056
|
+
async retrieveData(objectStore, columns) {
|
|
1057
|
+
return new Promise((resolve) => {
|
|
1058
|
+
const data = [];
|
|
1059
|
+
const cursorReq = objectStore.openCursor();
|
|
1060
|
+
cursorReq.onsuccess = (ev) => {
|
|
1061
|
+
const cursor = ev.target.result;
|
|
1062
|
+
if (cursor) {
|
|
1063
|
+
const value = cursor.value;
|
|
1064
|
+
if (typeof value !== "object" || value === null) {
|
|
1065
|
+
cursor.continue();
|
|
1066
|
+
return;
|
|
1067
|
+
}
|
|
1068
|
+
const row = { ...value };
|
|
1069
|
+
if (!columns.includes("*")) {
|
|
1070
|
+
for (const key of Object.keys(row)) {
|
|
1071
|
+
if (!columns.includes(key)) {
|
|
1072
|
+
delete row[key];
|
|
1073
|
+
}
|
|
1074
|
+
}
|
|
1075
|
+
}
|
|
1076
|
+
data.push(row);
|
|
1077
|
+
cursor.continue();
|
|
1078
|
+
} else {
|
|
1079
|
+
resolve(data);
|
|
1080
|
+
}
|
|
1081
|
+
};
|
|
1082
|
+
cursorReq.onerror = (ev) => {
|
|
1083
|
+
console.error(`Cursor error: ${ev.target.error?.message}`);
|
|
1084
|
+
resolve([]);
|
|
1085
|
+
};
|
|
1086
|
+
});
|
|
1087
|
+
}
|
|
1088
|
+
async retrieveById(objectStore, id, columns) {
|
|
1089
|
+
return new Promise((resolve) => {
|
|
1090
|
+
const request = objectStore.get(id);
|
|
1091
|
+
request.onsuccess = () => {
|
|
1092
|
+
const value = request.result;
|
|
1093
|
+
if (!value) {
|
|
1094
|
+
resolve(null);
|
|
1095
|
+
return;
|
|
1096
|
+
}
|
|
1097
|
+
if (typeof value !== "object") {
|
|
1098
|
+
console.error(`Expected an object, received ${typeof value}`);
|
|
1099
|
+
resolve(null);
|
|
1100
|
+
return;
|
|
1101
|
+
}
|
|
1102
|
+
const row = { ...value };
|
|
1103
|
+
if (!columns.includes("*")) {
|
|
1104
|
+
for (const key of Object.keys(row)) {
|
|
1105
|
+
if (!columns.includes(key)) {
|
|
1106
|
+
delete row[key];
|
|
1107
|
+
}
|
|
1108
|
+
}
|
|
1109
|
+
}
|
|
1110
|
+
resolve(row);
|
|
1111
|
+
};
|
|
1112
|
+
request.onerror = (ev) => {
|
|
1113
|
+
console.error(`Retrieve by ID error: ${ev.target.error?.message}`);
|
|
1114
|
+
resolve(null);
|
|
1115
|
+
};
|
|
1116
|
+
});
|
|
1117
|
+
}
|
|
1118
|
+
async insertData(objectStore, data) {
|
|
1119
|
+
return new Promise((resolve) => {
|
|
1120
|
+
if (Array.isArray(data)) {
|
|
1121
|
+
throw new Error("Oops! you provide an array...");
|
|
1122
|
+
}
|
|
1123
|
+
const request = objectStore.put(data);
|
|
1124
|
+
request.onsuccess = () => {
|
|
1125
|
+
resolve(null);
|
|
1126
|
+
};
|
|
1127
|
+
request.onerror = (ev) => {
|
|
1128
|
+
resolve(new Error(`Insert error: ${ev.target.error?.message}`));
|
|
1129
|
+
};
|
|
1130
|
+
});
|
|
1131
|
+
}
|
|
1132
|
+
openTx(tableNames, reject, mode = "readwrite") {
|
|
1133
|
+
const rawNames = Array.isArray(tableNames) ? tableNames : [tableNames];
|
|
1134
|
+
const uniqueNames = [...new Set(rawNames.filter(Boolean))];
|
|
1135
|
+
for (const name of uniqueNames) {
|
|
1136
|
+
if (!this.db?.objectStoreNames.contains(name)) {
|
|
1137
|
+
throw new Error(`Table ${name} does not exist!`);
|
|
1138
|
+
}
|
|
1139
|
+
}
|
|
1140
|
+
const tx = this.db.transaction([this.DBTypesStoreName, ...uniqueNames], mode);
|
|
1141
|
+
tx.onerror = this.onDBError.bind(this, reject);
|
|
1142
|
+
return [tx, tx.objectStore(uniqueNames[0])];
|
|
1143
|
+
}
|
|
1144
|
+
convertFieldValueType(value, type) {
|
|
1145
|
+
switch (type) {
|
|
1146
|
+
case "number":
|
|
1147
|
+
if (typeof value === "number")
|
|
1148
|
+
return value;
|
|
1149
|
+
if (!/^-?\d+$/.test(String(value).trim())) {
|
|
1150
|
+
throw new Error(`Invalid number expr. Got ${value}`);
|
|
1151
|
+
}
|
|
1152
|
+
return parseInt(String(value).trim(), 10);
|
|
1153
|
+
case "boolean":
|
|
1154
|
+
if (typeof value === "boolean")
|
|
1155
|
+
return value;
|
|
1156
|
+
if (!["true", "false"].includes(String(value).trim())) {
|
|
1157
|
+
throw new Error(`Invalid boolean expr. Got ${value}`);
|
|
1158
|
+
}
|
|
1159
|
+
return String(value).trim() === "true";
|
|
1160
|
+
case "bigint":
|
|
1161
|
+
return BigInt(value);
|
|
1162
|
+
case "symbol":
|
|
1163
|
+
return typeof value === "symbol" ? value : Symbol.for(String(value));
|
|
1164
|
+
case "object":
|
|
1165
|
+
if (typeof value === "object" && value !== null)
|
|
1166
|
+
return value;
|
|
1167
|
+
if (typeof value === "string") {
|
|
1168
|
+
try {
|
|
1169
|
+
return JSON.parse(value);
|
|
1170
|
+
} catch {
|
|
1171
|
+
return {};
|
|
1172
|
+
}
|
|
1173
|
+
}
|
|
1174
|
+
return {};
|
|
1175
|
+
case "array.string":
|
|
1176
|
+
if (Array.isArray(value))
|
|
1177
|
+
return value.map(String);
|
|
1178
|
+
if (value instanceof Set)
|
|
1179
|
+
return Array.from(value).map(String);
|
|
1180
|
+
if (typeof value === "string") {
|
|
1181
|
+
try {
|
|
1182
|
+
const parsed = JSON.parse(value);
|
|
1183
|
+
if (Array.isArray(parsed))
|
|
1184
|
+
return parsed.map(String);
|
|
1185
|
+
} catch {}
|
|
1186
|
+
return value.split(",").map((s) => s.trim()).filter(Boolean);
|
|
1187
|
+
}
|
|
1188
|
+
return [];
|
|
1189
|
+
case "file":
|
|
1190
|
+
case "blob":
|
|
1191
|
+
return value;
|
|
1192
|
+
default:
|
|
1193
|
+
return typeof value === "string" ? value.trim() : String(value ?? "");
|
|
1194
|
+
}
|
|
1195
|
+
}
|
|
1196
|
+
async executeQuery(requests) {
|
|
1197
|
+
this.isError = null;
|
|
1198
|
+
let lastResult = [];
|
|
1199
|
+
for (const request of requests) {
|
|
1200
|
+
try {
|
|
1201
|
+
switch (request.type) {
|
|
1202
|
+
case "SELECT": {
|
|
1203
|
+
const selectReq = request;
|
|
1204
|
+
const joinTables = (selectReq.joins ?? []).map((j) => j.table);
|
|
1205
|
+
const allTables = [selectReq.table, ...joinTables];
|
|
1206
|
+
const [tx, primaryStore] = this.openTx(allTables, (err) => {
|
|
1207
|
+
this.isError = err;
|
|
1208
|
+
}, "readonly");
|
|
1209
|
+
let mainRows = await this.retrieveData(primaryStore, ["*"]);
|
|
1210
|
+
if (selectReq.where && selectReq.where.length > 0) {
|
|
1211
|
+
mainRows = mainRows.filter((row) => {
|
|
1212
|
+
return selectReq.where.every((cond) => {
|
|
1213
|
+
if (cond.table && cond.table !== selectReq.table && cond.table !== selectReq.alias) {
|
|
1214
|
+
return true;
|
|
1215
|
+
}
|
|
1216
|
+
const val = row[cond.column];
|
|
1217
|
+
if (cond.operator === "=") {
|
|
1218
|
+
return String(val) === String(cond.value);
|
|
1219
|
+
}
|
|
1220
|
+
if (cond.operator === "!=") {
|
|
1221
|
+
return String(val) !== String(cond.value);
|
|
1222
|
+
}
|
|
1223
|
+
return true;
|
|
1224
|
+
});
|
|
1225
|
+
});
|
|
1226
|
+
}
|
|
1227
|
+
if (selectReq.joins && selectReq.joins.length > 0) {
|
|
1228
|
+
for (const join of selectReq.joins) {
|
|
1229
|
+
const joinStore = tx.objectStore(join.table);
|
|
1230
|
+
let joinRows = await this.retrieveData(joinStore, ["*"]);
|
|
1231
|
+
if (selectReq.where && selectReq.where.length > 0) {
|
|
1232
|
+
joinRows = joinRows.filter((row) => {
|
|
1233
|
+
return selectReq.where.every((cond) => {
|
|
1234
|
+
if (cond.table && cond.table !== join.table && cond.table !== join.alias) {
|
|
1235
|
+
return true;
|
|
1236
|
+
}
|
|
1237
|
+
const val = row[cond.column];
|
|
1238
|
+
if (cond.operator === "=") {
|
|
1239
|
+
return String(val) === String(cond.value);
|
|
1240
|
+
}
|
|
1241
|
+
if (cond.operator === "!=") {
|
|
1242
|
+
return String(val) !== String(cond.value);
|
|
1243
|
+
}
|
|
1244
|
+
return true;
|
|
1245
|
+
});
|
|
1246
|
+
});
|
|
1247
|
+
}
|
|
1248
|
+
let leftCol = "id";
|
|
1249
|
+
let rightCol = `${selectReq.table.replace(/s$/, "")}_id`;
|
|
1250
|
+
if (join.on) {
|
|
1251
|
+
leftCol = join.on.leftColumn;
|
|
1252
|
+
rightCol = join.on.rightColumn;
|
|
1253
|
+
}
|
|
1254
|
+
const survivingRows = [];
|
|
1255
|
+
for (const mainRow of mainRows) {
|
|
1256
|
+
const mainVal = mainRow[leftCol];
|
|
1257
|
+
const matched = joinRows.filter((child) => String(child[rightCol]) === String(mainVal));
|
|
1258
|
+
if (join.type === "INNER" && matched.length === 0) {
|
|
1259
|
+
continue;
|
|
1260
|
+
}
|
|
1261
|
+
const collection = {};
|
|
1262
|
+
let allHaveId = matched.length > 0;
|
|
1263
|
+
for (const child of matched) {
|
|
1264
|
+
if (!child.id)
|
|
1265
|
+
allHaveId = false;
|
|
1266
|
+
const cleanChild = { ...child };
|
|
1267
|
+
delete cleanChild[rightCol];
|
|
1268
|
+
collection[child.id] = cleanChild;
|
|
1269
|
+
}
|
|
1270
|
+
if (allHaveId || matched.length === 0) {
|
|
1271
|
+
mainRow[join.table] = collection;
|
|
1272
|
+
} else {
|
|
1273
|
+
mainRow[join.table] = matched;
|
|
1274
|
+
}
|
|
1275
|
+
survivingRows.push(mainRow);
|
|
1276
|
+
}
|
|
1277
|
+
mainRows = survivingRows;
|
|
1278
|
+
}
|
|
1279
|
+
}
|
|
1280
|
+
if (!selectReq.columns.includes("*")) {
|
|
1281
|
+
mainRows = mainRows.map((row) => {
|
|
1282
|
+
const filtered = {};
|
|
1283
|
+
for (const col of selectReq.columns) {
|
|
1284
|
+
if (col in row) {
|
|
1285
|
+
filtered[col] = row[col];
|
|
1286
|
+
}
|
|
1287
|
+
}
|
|
1288
|
+
return filtered;
|
|
1289
|
+
});
|
|
1290
|
+
}
|
|
1291
|
+
lastResult = mainRows;
|
|
1292
|
+
break;
|
|
1293
|
+
}
|
|
1294
|
+
case "INSERT": {
|
|
1295
|
+
const insertReq = request;
|
|
1296
|
+
const [tx, objectStore] = this.openTx(insertReq.table, (err) => {
|
|
1297
|
+
this.isError = err;
|
|
1298
|
+
}, "readwrite");
|
|
1299
|
+
const table = await this.retrieveById(tx.objectStore(this.DBTypesStoreName), insertReq.table, ["*"]);
|
|
1300
|
+
if (!table) {
|
|
1301
|
+
return new Error(`Table ${insertReq.table} types not found. Please try Migrate your DB`);
|
|
1302
|
+
}
|
|
1303
|
+
const newData = {};
|
|
1304
|
+
const fieldsToUse = insertReq.fields?.length ? insertReq.fields : table.order;
|
|
1305
|
+
const valuesToUse = insertReq.values?.length ? insertReq.values : insertReq.columns;
|
|
1306
|
+
for (let idx = 0;idx < fieldsToUse.length; idx++) {
|
|
1307
|
+
const fieldId = fieldsToUse[idx];
|
|
1308
|
+
const LocalDBDataType = table.fields[fieldId];
|
|
1309
|
+
const typeSchema = DEFAULT_FIELDS_SCHEMA.get(LocalDBDataType);
|
|
1310
|
+
if (!typeSchema) {
|
|
1311
|
+
return new Error(`Unknown table type ${LocalDBDataType}`);
|
|
1312
|
+
}
|
|
1313
|
+
const correspondingValue = valuesToUse[idx];
|
|
1314
|
+
if (correspondingValue === undefined) {
|
|
1315
|
+
continue;
|
|
1316
|
+
}
|
|
1317
|
+
const convertedValue = this.convertFieldValueType(correspondingValue, LocalDBDataType);
|
|
1318
|
+
const safeValue = typeSchema.safeParse(convertedValue);
|
|
1319
|
+
if (!safeValue.success) {
|
|
1320
|
+
return new Error(`Mismatch field type at ${fieldId}. Must be ${LocalDBDataType}. Stack: ${safeValue.error.message}`);
|
|
1321
|
+
}
|
|
1322
|
+
newData[fieldId] = convertedValue;
|
|
1323
|
+
}
|
|
1324
|
+
await this.insertData(objectStore, newData);
|
|
1325
|
+
break;
|
|
1326
|
+
}
|
|
1327
|
+
default:
|
|
1328
|
+
break;
|
|
1329
|
+
}
|
|
1330
|
+
} catch (error) {
|
|
1331
|
+
return error instanceof Error ? error : new Error(JSON.stringify(error));
|
|
1332
|
+
}
|
|
1333
|
+
}
|
|
1334
|
+
return lastResult;
|
|
1335
|
+
}
|
|
1336
|
+
async QueryDB(query, params) {
|
|
1337
|
+
if (!this.db)
|
|
1338
|
+
await this.init();
|
|
1339
|
+
const req = this.client.NewRequest(query, params);
|
|
1340
|
+
return this.executeQuery(req);
|
|
1341
|
+
}
|
|
1342
|
+
async MigrateDB(query) {
|
|
1343
|
+
const statements = this.client.NewRequest(query);
|
|
1344
|
+
const isBadReq = statements.some((s) => s.type !== "CREATE");
|
|
1345
|
+
if (isBadReq) {
|
|
1346
|
+
return new Error(`Bad request at migration. All must be CREATE statement!`);
|
|
1347
|
+
}
|
|
1348
|
+
const tables = await this.sheet.RegisterSheet(statements);
|
|
1349
|
+
const uniqueNames = [...new Set(tables.map((table) => table.name))];
|
|
1350
|
+
if (uniqueNames.includes(this.DBTypesStoreName)) {
|
|
1351
|
+
return new Error(`Reserved store name ${this.DBTypesStoreName}!`);
|
|
1352
|
+
}
|
|
1353
|
+
if (!this.isAvailable) {
|
|
1354
|
+
return new Error("[WARN] IndexDB not available in this browser!");
|
|
1355
|
+
}
|
|
1356
|
+
if (!this.db)
|
|
1357
|
+
await this.init();
|
|
1358
|
+
const namesToCreate = uniqueNames.filter((name) => !this.db?.objectStoreNames.contains(name));
|
|
1359
|
+
if (!namesToCreate.length) {
|
|
1360
|
+
return null;
|
|
1361
|
+
}
|
|
1362
|
+
this.closeConnection();
|
|
1363
|
+
this.bumpVersion();
|
|
1364
|
+
return new Promise((resolve) => {
|
|
1365
|
+
const request = window.indexedDB.open(this.DBName, this.DBVersion);
|
|
1366
|
+
request.onupgradeneeded = () => {
|
|
1367
|
+
const db = request.result;
|
|
1368
|
+
this.ensureObjectStores(db, [this.DBTypesStoreName, ...namesToCreate]);
|
|
1369
|
+
};
|
|
1370
|
+
request.onsuccess = async (ev) => {
|
|
1371
|
+
const db = request.result;
|
|
1372
|
+
const objectStore = db.transaction([this.DBTypesStoreName], "readwrite").objectStore(this.DBTypesStoreName);
|
|
1373
|
+
await Promise.all(tables.map((table) => this.insertData(objectStore, { ...table, id: table.name })));
|
|
1374
|
+
this.onDBSuccess.call(this, resolve, ev);
|
|
1375
|
+
};
|
|
1376
|
+
request.onerror = this.onDBError.bind(this, resolve);
|
|
1377
|
+
request.onblocked = () => {
|
|
1378
|
+
console.warn("[WARN] Migration blocked because another IndexedDB connection is still open.");
|
|
1379
|
+
resolve(new Error("IndexedDB migration blocked"));
|
|
1380
|
+
};
|
|
1381
|
+
});
|
|
1382
|
+
}
|
|
1383
|
+
}
|
|
1384
|
+
|
|
1385
|
+
// src/modules/autosave.module.ts
|
|
1386
|
+
async function loadCompleteDocument(db, documentId) {
|
|
1387
|
+
const result = await db.QueryDB(`
|
|
1388
|
+
SELECT * FROM documents
|
|
1389
|
+
JOIN fields ON documents.id = fields.document_id
|
|
1390
|
+
JOIN containers ON documents.id = containers.document_id
|
|
1391
|
+
JOIN components ON documents.id = components.document_id
|
|
1392
|
+
LEFT JOIN medias ON documents.id = medias.document_id
|
|
1393
|
+
WHERE documents.id = '${documentId}';
|
|
1394
|
+
`);
|
|
1395
|
+
if (result instanceof Error) {
|
|
1396
|
+
throw result;
|
|
1397
|
+
}
|
|
1398
|
+
if (Array.isArray(result) && result.length > 0) {
|
|
1399
|
+
return result[0];
|
|
1400
|
+
}
|
|
1401
|
+
return null;
|
|
1402
|
+
}
|
|
1403
|
+
var AutoSaveModule = {
|
|
1404
|
+
name: "autosave",
|
|
1405
|
+
version: "1.0.0",
|
|
1406
|
+
requires: [],
|
|
1407
|
+
install(ctx) {
|
|
1408
|
+
ctx.state.defineSlice("localdbState", { isDBReady: false });
|
|
1409
|
+
if (typeof window === "undefined" || !("indexedDB" in window)) {
|
|
1410
|
+
console.error("[WARN] indexedDB not supported or SSR");
|
|
1411
|
+
return;
|
|
1412
|
+
}
|
|
1413
|
+
const db = new LocalDB("coraxdb");
|
|
1414
|
+
db.DeleteDB().then((err) => {
|
|
1415
|
+
if (err) {
|
|
1416
|
+
console.error(err);
|
|
1417
|
+
return;
|
|
1418
|
+
}
|
|
1419
|
+
db.MigrateDB(`
|
|
1420
|
+
CREATE TABLE documents (
|
|
1421
|
+
id TEXT,
|
|
1422
|
+
version TEXT,
|
|
1423
|
+
order ARRAY[STRING],
|
|
1424
|
+
created_at DATETIME
|
|
1425
|
+
);
|
|
1426
|
+
CREATE TABLE fields (
|
|
1427
|
+
id TEXT,
|
|
1428
|
+
document_id TEXT,
|
|
1429
|
+
name TEXT,
|
|
1430
|
+
tag TEXT,
|
|
1431
|
+
type TEXT,
|
|
1432
|
+
variant TEXT,
|
|
1433
|
+
containerId TEXT,
|
|
1434
|
+
props OBJECT
|
|
1435
|
+
);
|
|
1436
|
+
CREATE TABLE containers (
|
|
1437
|
+
id TEXT,
|
|
1438
|
+
document_id TEXT,
|
|
1439
|
+
name TEXT,
|
|
1440
|
+
tag TEXT,
|
|
1441
|
+
type TEXT,
|
|
1442
|
+
variant TEXT,
|
|
1443
|
+
order ARRAY[STRING],
|
|
1444
|
+
props OBJECT
|
|
1445
|
+
);
|
|
1446
|
+
CREATE TABLE components (
|
|
1447
|
+
id TEXT,
|
|
1448
|
+
document_id TEXT,
|
|
1449
|
+
name TEXT,
|
|
1450
|
+
tag TEXT,
|
|
1451
|
+
type TEXT,
|
|
1452
|
+
variant TEXT,
|
|
1453
|
+
order ARRAY[STRING],
|
|
1454
|
+
count INTEGER,
|
|
1455
|
+
updatedAt DATETIME,
|
|
1456
|
+
createdAt DATETIME,
|
|
1457
|
+
props OBJECT
|
|
1458
|
+
);
|
|
1459
|
+
CREATE TABLE medias (
|
|
1460
|
+
id TEXT,
|
|
1461
|
+
document_id TEXT,
|
|
1462
|
+
name TEXT,
|
|
1463
|
+
type TEXT,
|
|
1464
|
+
size INTEGER,
|
|
1465
|
+
data FILE,
|
|
1466
|
+
created_at DATETIME
|
|
1467
|
+
);
|
|
1468
|
+
`).then((error) => {
|
|
1469
|
+
if (error) {
|
|
1470
|
+
console.error("Failed to migrate database:", error);
|
|
1471
|
+
return;
|
|
1472
|
+
}
|
|
1473
|
+
ctx.state.set("localdbState", { isDBReady: true });
|
|
1474
|
+
}).catch((r) => {
|
|
1475
|
+
console.error(r);
|
|
1476
|
+
});
|
|
1477
|
+
}).catch((e) => {
|
|
1478
|
+
console.error(e);
|
|
1479
|
+
});
|
|
1480
|
+
ctx.commands.before("blocks.insert", (p) => {
|
|
1481
|
+
const dbState = ctx.state.get("localdbState");
|
|
1482
|
+
if (!dbState || !dbState.isDBReady)
|
|
1483
|
+
return null;
|
|
1484
|
+
return p;
|
|
1485
|
+
});
|
|
1486
|
+
ctx.events.on("document:changed", (sbc) => {
|
|
1487
|
+
const dbState = ctx.state.get("localdbState");
|
|
1488
|
+
if (!dbState || !dbState.isDBReady)
|
|
1489
|
+
return;
|
|
1490
|
+
const jsonDocument = SerializeTree(sbc.value.next);
|
|
1491
|
+
const saveDocument = async () => {
|
|
1492
|
+
const documentId = jsonDocument.id;
|
|
1493
|
+
const now = Date.now();
|
|
1494
|
+
await db.QueryDB(`INSERT INTO documents (id, version, order, created_at) VALUES (?, ?, ?, ?);`, [documentId, jsonDocument.version, Array.from(jsonDocument.order ?? []), now]);
|
|
1495
|
+
for (const [fieldId, field] of Object.entries(jsonDocument.fields ?? {})) {
|
|
1496
|
+
await db.QueryDB(`INSERT INTO fields (id, document_id, name, tag, type, variant, containerId, props) VALUES (?, ?, ?, ?, ?, ?, ?, ?);`, [
|
|
1497
|
+
field.id || fieldId,
|
|
1498
|
+
documentId,
|
|
1499
|
+
field.name ?? "",
|
|
1500
|
+
field.tag ?? "p",
|
|
1501
|
+
field.type ?? "text",
|
|
1502
|
+
field.variant ?? "field",
|
|
1503
|
+
field.containerId ?? "",
|
|
1504
|
+
field.props ?? {}
|
|
1505
|
+
]);
|
|
1506
|
+
const fieldProps = field.props ?? {};
|
|
1507
|
+
if (fieldProps.file && (fieldProps.file instanceof Blob || typeof File !== "undefined" && fieldProps.file instanceof File)) {
|
|
1508
|
+
const file = fieldProps.file;
|
|
1509
|
+
await db.QueryDB(`INSERT INTO medias (id, document_id, name, type, size, data, created_at) VALUES (?, ?, ?, ?, ?, ?, ?);`, [
|
|
1510
|
+
`media:${field.id || fieldId}`,
|
|
1511
|
+
documentId,
|
|
1512
|
+
file.name ?? "unnamed",
|
|
1513
|
+
file.type ?? "application/octet-stream",
|
|
1514
|
+
file.size ?? 0,
|
|
1515
|
+
file,
|
|
1516
|
+
now
|
|
1517
|
+
]);
|
|
1518
|
+
}
|
|
1519
|
+
}
|
|
1520
|
+
for (const [containerId, container] of Object.entries(jsonDocument.containers ?? {})) {
|
|
1521
|
+
await db.QueryDB(`INSERT INTO containers (id, document_id, name, tag, type, variant, order, props) VALUES (?, ?, ?, ?, ?, ?, ?, ?);`, [
|
|
1522
|
+
container.id || containerId,
|
|
1523
|
+
documentId,
|
|
1524
|
+
container.name ?? "",
|
|
1525
|
+
container.tag ?? "div",
|
|
1526
|
+
container.type ?? "division",
|
|
1527
|
+
container.variant ?? "container",
|
|
1528
|
+
Array.from(container.order ?? []),
|
|
1529
|
+
container.props ?? {}
|
|
1530
|
+
]);
|
|
1531
|
+
}
|
|
1532
|
+
for (const [componentId, component] of Object.entries(jsonDocument.components ?? {})) {
|
|
1533
|
+
await db.QueryDB(`INSERT INTO components (id, document_id, name, tag, type, variant, order, count, updatedAt, createdAt, props) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);`, [
|
|
1534
|
+
component.id || componentId,
|
|
1535
|
+
documentId,
|
|
1536
|
+
component.name ?? "",
|
|
1537
|
+
component.tag ?? "div",
|
|
1538
|
+
component.type ?? "division",
|
|
1539
|
+
component.variant ?? "component",
|
|
1540
|
+
Array.from(component.order ?? []),
|
|
1541
|
+
component.count ?? 0,
|
|
1542
|
+
component.updatedAt ?? now,
|
|
1543
|
+
component.createdAt ?? now,
|
|
1544
|
+
component.props ?? {}
|
|
1545
|
+
]);
|
|
1546
|
+
}
|
|
1547
|
+
};
|
|
1548
|
+
ctx.tasks.schedule("autosave:persist", 1000, saveDocument);
|
|
1549
|
+
});
|
|
1550
|
+
}
|
|
1551
|
+
};
|
|
1552
|
+
// src/components/slots/maps/block-actions.tsx
|
|
1553
|
+
import { useEffect as useEffect2, useMemo as useMemo2, useRef } from "react";
|
|
1554
|
+
|
|
1555
|
+
// src/hooks/use-slots.ts
|
|
1556
|
+
import { useSyncExternalStore as useSyncExternalStore2 } from "react";
|
|
1557
|
+
function useSlots(slotName) {
|
|
1558
|
+
const engine = useEngine();
|
|
1559
|
+
const items = useSyncExternalStore2((fn) => engine.ui.subscribe(slotName, fn), () => engine.ui.getSlots(slotName), () => engine.ui.getSlots(slotName));
|
|
1560
|
+
return items;
|
|
1561
|
+
}
|
|
1562
|
+
|
|
1563
|
+
// src/hooks/use-registry.ts
|
|
1564
|
+
import { useSyncExternalStore as useSyncExternalStore3 } from "react";
|
|
1565
|
+
|
|
1566
|
+
// src/registry/ui.primitives.registry.tsx
|
|
1567
|
+
var UIPrimitivesRegistry = new GenericRegistry;
|
|
1568
|
+
|
|
1569
|
+
// src/hooks/use-registry.ts
|
|
1570
|
+
function useRegistryComponent(name) {
|
|
1571
|
+
return useSyncExternalStore3((fn) => SlotsComponentRegistry.subscribe(name, fn), () => SlotsComponentRegistry.get(name), () => SlotsComponentRegistry.get(name));
|
|
1572
|
+
}
|
|
1573
|
+
|
|
1574
|
+
// src/components/slots/slot-button.tsx
|
|
1575
|
+
import React4 from "react";
|
|
1576
|
+
function SlotButton({ c, ui }) {
|
|
1577
|
+
const Comp = useRegistryComponent(c.name);
|
|
1578
|
+
if (!Comp) {
|
|
1579
|
+
console.warn(`No component registered for slot key "${c.name}"`);
|
|
1580
|
+
return null;
|
|
1581
|
+
}
|
|
1582
|
+
return React4.createElement(Comp, {
|
|
1583
|
+
c,
|
|
1584
|
+
ui,
|
|
1585
|
+
...{
|
|
1586
|
+
"data-slot-type": "slot-button"
|
|
1587
|
+
}
|
|
1588
|
+
});
|
|
1589
|
+
}
|
|
1590
|
+
|
|
1591
|
+
// src/hooks/use-ui-context.ts
|
|
1592
|
+
function useUIContext() {
|
|
1593
|
+
const selection = useSlice("selection") ?? { blocks: new Map };
|
|
1594
|
+
const focus = useSlice("focus") ?? null;
|
|
1595
|
+
const hover = useSlice("hover") ?? null;
|
|
1596
|
+
const textSelection = useSlice("textSelection") ?? null;
|
|
1597
|
+
return {
|
|
1598
|
+
selection,
|
|
1599
|
+
focus,
|
|
1600
|
+
hover,
|
|
1601
|
+
textSelection
|
|
1602
|
+
};
|
|
1603
|
+
}
|
|
1604
|
+
|
|
1605
|
+
// src/components/slots/slot-component.tsx
|
|
1606
|
+
import { createElement } from "react";
|
|
1607
|
+
function SlotComponent({ c, ui }) {
|
|
1608
|
+
const Comp = useRegistryComponent(c.name);
|
|
1609
|
+
if (!Comp) {
|
|
1610
|
+
console.warn(`No component registered for slot key "${c.name}"`);
|
|
1611
|
+
return null;
|
|
1612
|
+
}
|
|
1613
|
+
const props = c.getProps(ui);
|
|
1614
|
+
if (!props)
|
|
1615
|
+
return null;
|
|
1616
|
+
return createElement(Comp, { ui, c });
|
|
1617
|
+
}
|
|
1618
|
+
|
|
1619
|
+
// src/components/slots/slots.tsx
|
|
1620
|
+
import { jsxDEV as jsxDEV10, Fragment } from "react/jsx-dev-runtime";
|
|
1621
|
+
function Slots({ slotName }) {
|
|
1622
|
+
const items = useSlots(slotName);
|
|
1623
|
+
const ui = useUIContext();
|
|
1624
|
+
return /* @__PURE__ */ jsxDEV10(Fragment, {
|
|
1625
|
+
children: items.map((c) => /* @__PURE__ */ jsxDEV10(SlotItem, {
|
|
1626
|
+
c,
|
|
1627
|
+
ui
|
|
1628
|
+
}, c.id, false, undefined, this))
|
|
1629
|
+
}, undefined, false, undefined, this);
|
|
1630
|
+
}
|
|
1631
|
+
function SlotItem({ c, ui }) {
|
|
1632
|
+
if (c.when && !c.when(ui))
|
|
1633
|
+
return null;
|
|
1634
|
+
switch (c.kind) {
|
|
1635
|
+
case "button":
|
|
1636
|
+
return /* @__PURE__ */ jsxDEV10(SlotButton, {
|
|
1637
|
+
c,
|
|
1638
|
+
ui
|
|
1639
|
+
}, undefined, false, undefined, this);
|
|
1640
|
+
case "component":
|
|
1641
|
+
return /* @__PURE__ */ jsxDEV10(SlotComponent, {
|
|
1642
|
+
c,
|
|
1643
|
+
ui
|
|
1644
|
+
}, undefined, false, undefined, this);
|
|
1645
|
+
}
|
|
1646
|
+
}
|
|
1647
|
+
|
|
1648
|
+
// src/components/slots/maps/block-actions.tsx
|
|
1649
|
+
import { cn as cn5 } from "cn";
|
|
1650
|
+
import { jsxDEV as jsxDEV11 } from "react/jsx-dev-runtime";
|
|
1651
|
+
function BlockActions({ className, children, ...rest }) {
|
|
1652
|
+
const selection = useSlice("selection");
|
|
1653
|
+
const documentSlice = useSlice("document");
|
|
1654
|
+
const { blocks = new Map } = selection ?? {};
|
|
1655
|
+
const { order = new Set } = documentSlice ?? {};
|
|
1656
|
+
const keys = [...blocks.keys()];
|
|
1657
|
+
const firstSelectionId = keys[0];
|
|
1658
|
+
const engine = useEngine();
|
|
1659
|
+
const lastPositionRef = useRef({ active: false, top: 0, left: 0 });
|
|
1660
|
+
const blockRect = blocks.get(firstSelectionId);
|
|
1661
|
+
const position = useMemo2(() => {
|
|
1662
|
+
if (!firstSelectionId || !blockRect) {
|
|
1663
|
+
return { active: false, top: 0, left: 0 };
|
|
1664
|
+
}
|
|
1665
|
+
const container = document.getElementById("corax_flow_blocks_container");
|
|
1666
|
+
if (!container)
|
|
1667
|
+
return { active: false, top: 0, left: 0 };
|
|
1668
|
+
const containerRect = container.getBoundingClientRect();
|
|
1669
|
+
const block = engine.document.getBlock(firstSelectionId);
|
|
1670
|
+
if (!block || block.variant !== "field")
|
|
1671
|
+
return { active: false, top: 0, left: 0 };
|
|
1672
|
+
const domElement = document.getElementById(firstSelectionId);
|
|
1673
|
+
if (!domElement)
|
|
1674
|
+
return { active: false, top: 0, left: 0 };
|
|
1675
|
+
const heightDiff = 36 - blockRect.height;
|
|
1676
|
+
const toSubstract = heightDiff / 2;
|
|
1677
|
+
const top = blockRect.top - (-toSubstract > 32 ? -8 : toSubstract) - containerRect.top - container.clientTop + blockRect.parentScrollTop;
|
|
1678
|
+
return {
|
|
1679
|
+
active: true,
|
|
1680
|
+
top,
|
|
1681
|
+
left: 0
|
|
1682
|
+
};
|
|
1683
|
+
}, [firstSelectionId, engine, blockRect]);
|
|
1684
|
+
useEffect2(() => {
|
|
1685
|
+
if (position.active) {
|
|
1686
|
+
lastPositionRef.current = position;
|
|
1687
|
+
}
|
|
1688
|
+
}, [position]);
|
|
1689
|
+
const _position = position.active ? position : lastPositionRef.current;
|
|
1690
|
+
return /* @__PURE__ */ jsxDEV11("div", {
|
|
1691
|
+
style: {
|
|
1692
|
+
top: _position.top,
|
|
1693
|
+
left: _position.left
|
|
1694
|
+
},
|
|
1695
|
+
className: cn5("absolute top-0 left-0 gap-1 flex items-center transition-opacity", className, {
|
|
1696
|
+
"opacity-0 pointer-events-none": !_position.active || keys.length > 1 || firstSelectionId && !order?.has(firstSelectionId)
|
|
1697
|
+
}),
|
|
1698
|
+
...rest,
|
|
1699
|
+
children: [
|
|
1700
|
+
/* @__PURE__ */ jsxDEV11(Slots, {
|
|
1701
|
+
slotName: "block-actions"
|
|
1702
|
+
}, undefined, false, undefined, this),
|
|
1703
|
+
children
|
|
1704
|
+
]
|
|
1705
|
+
}, undefined, true, undefined, this);
|
|
1706
|
+
}
|
|
1707
|
+
// src/components/slots/maps/overlays.tsx
|
|
1708
|
+
import { jsxDEV as jsxDEV12, Fragment as Fragment2 } from "react/jsx-dev-runtime";
|
|
1709
|
+
function Overlays() {
|
|
1710
|
+
return /* @__PURE__ */ jsxDEV12(Fragment2, {
|
|
1711
|
+
children: /* @__PURE__ */ jsxDEV12(Slots, {
|
|
1712
|
+
slotName: "overlay"
|
|
1713
|
+
}, undefined, false, undefined, this)
|
|
1714
|
+
}, undefined, false, undefined, this);
|
|
1715
|
+
}
|
|
1716
|
+
// src/components/slots/maps/text-inspector.tsx
|
|
1717
|
+
import { jsxDEV as jsxDEV13 } from "react/jsx-dev-runtime";
|
|
1718
|
+
function TextInspector({ blockId }) {
|
|
1719
|
+
return /* @__PURE__ */ jsxDEV13("div", {
|
|
1720
|
+
children: blockId
|
|
1721
|
+
}, undefined, false, undefined, this);
|
|
1722
|
+
}
|
|
1723
|
+
// src/components/slots/maps/toolbar.tsx
|
|
1724
|
+
import { cn as cn6 } from "cn";
|
|
1725
|
+
import { useRef as useRef2, useMemo as useMemo3, useEffect as useEffect3 } from "react";
|
|
1726
|
+
|
|
1727
|
+
// src/lib/selection.utils.ts
|
|
1728
|
+
function rangeToOffsets(root, range) {
|
|
1729
|
+
const measure = (node, offset) => {
|
|
1730
|
+
const r = document.createRange();
|
|
1731
|
+
r.selectNodeContents(root);
|
|
1732
|
+
r.setEnd(node, offset);
|
|
1733
|
+
return r.toString().length;
|
|
1734
|
+
};
|
|
1735
|
+
return {
|
|
1736
|
+
start: measure(range.startContainer, range.startOffset),
|
|
1737
|
+
end: measure(range.endContainer, range.endOffset)
|
|
1738
|
+
};
|
|
1739
|
+
}
|
|
1740
|
+
function offsetsToRange(root, start, end) {
|
|
1741
|
+
const range = document.createRange();
|
|
1742
|
+
const walker = document.createTreeWalker(root, NodeFilter.SHOW_TEXT);
|
|
1743
|
+
let pos = 0, startSet = false;
|
|
1744
|
+
for (let n = walker.nextNode();n; n = walker.nextNode()) {
|
|
1745
|
+
const next = pos + n.length;
|
|
1746
|
+
if (!startSet && start <= next) {
|
|
1747
|
+
range.setStart(n, start - pos);
|
|
1748
|
+
startSet = true;
|
|
1749
|
+
}
|
|
1750
|
+
if (startSet && end <= next) {
|
|
1751
|
+
range.setEnd(n, end - pos);
|
|
1752
|
+
return range;
|
|
1753
|
+
}
|
|
1754
|
+
pos = next;
|
|
1755
|
+
}
|
|
1756
|
+
return null;
|
|
1757
|
+
}
|
|
1758
|
+
|
|
1759
|
+
// src/components/slots/maps/toolbar.tsx
|
|
1760
|
+
import { jsxDEV as jsxDEV14 } from "react/jsx-dev-runtime";
|
|
1761
|
+
var TOOLBAR_OFFSET = 8;
|
|
1762
|
+
var DEFAULT_STATE = { active: false, top: 0, left: 0 };
|
|
1763
|
+
function Toolbar({ className, children, ...rest }) {
|
|
1764
|
+
const textSel = useSlice("textSelection");
|
|
1765
|
+
const currentBlockId = textSel?.blockId;
|
|
1766
|
+
const documentSlice = useSlice("document");
|
|
1767
|
+
const { order = new Set } = documentSlice ?? {};
|
|
1768
|
+
const engine = useEngine();
|
|
1769
|
+
const lastPositionRef = useRef2(DEFAULT_STATE);
|
|
1770
|
+
const toolbarRef = useRef2(null);
|
|
1771
|
+
const position = useMemo3(() => {
|
|
1772
|
+
const toolbarElement = toolbarRef.current;
|
|
1773
|
+
if (!currentBlockId || !toolbarElement) {
|
|
1774
|
+
return DEFAULT_STATE;
|
|
1775
|
+
}
|
|
1776
|
+
const el = document.getElementById(textSel.blockId);
|
|
1777
|
+
if (!el)
|
|
1778
|
+
return DEFAULT_STATE;
|
|
1779
|
+
const range = offsetsToRange(el, textSel.start, textSel.end);
|
|
1780
|
+
if (!range)
|
|
1781
|
+
return DEFAULT_STATE;
|
|
1782
|
+
const selRect = range.getBoundingClientRect();
|
|
1783
|
+
const block = engine.document.getBlock(currentBlockId);
|
|
1784
|
+
if (!block)
|
|
1785
|
+
return { active: false, top: 0, left: 0 };
|
|
1786
|
+
const blockRect = blockRectFromBlockId(currentBlockId);
|
|
1787
|
+
if (!blockRect)
|
|
1788
|
+
return { active: false, top: 0, left: 0 };
|
|
1789
|
+
return {
|
|
1790
|
+
active: true,
|
|
1791
|
+
top: blockRect.top - toolbarElement.offsetHeight - TOOLBAR_OFFSET,
|
|
1792
|
+
left: selRect.left - (toolbarElement.offsetWidth - selRect.width) / 2
|
|
1793
|
+
};
|
|
1794
|
+
}, [currentBlockId, engine, textSel]);
|
|
1795
|
+
useEffect3(() => {
|
|
1796
|
+
if (position.active) {
|
|
1797
|
+
lastPositionRef.current = position;
|
|
1798
|
+
}
|
|
1799
|
+
}, [position]);
|
|
1800
|
+
const _position = position.active ? position : lastPositionRef.current;
|
|
1801
|
+
return /* @__PURE__ */ jsxDEV14("div", {
|
|
1802
|
+
ref: toolbarRef,
|
|
1803
|
+
style: {
|
|
1804
|
+
top: _position.top,
|
|
1805
|
+
left: _position.left
|
|
1806
|
+
},
|
|
1807
|
+
className: cn6("fixed top-0 left-0 gap-1 flex items-center transition-all", className, {
|
|
1808
|
+
"opacity-0 pointer-events-none": !_position.active || currentBlockId && !order?.has(currentBlockId)
|
|
1809
|
+
}),
|
|
1810
|
+
...rest,
|
|
1811
|
+
children: [
|
|
1812
|
+
/* @__PURE__ */ jsxDEV14(Slots, {
|
|
1813
|
+
slotName: "toolbar"
|
|
1814
|
+
}, undefined, false, undefined, this),
|
|
1815
|
+
children
|
|
1816
|
+
]
|
|
1817
|
+
}, undefined, true, undefined, this);
|
|
1818
|
+
}
|
|
1819
|
+
|
|
1820
|
+
// src/components/slots/maps/index.ts
|
|
1821
|
+
"use client";
|
|
1822
|
+
// src/components/corax-flow.tsx
|
|
1823
|
+
import { useEffect as useEffect9, useReducer } from "react";
|
|
1824
|
+
|
|
1825
|
+
// src/hooks/use-renderer.ts
|
|
1826
|
+
import { useMemo as useMemo4 } from "react";
|
|
1827
|
+
|
|
1828
|
+
// src/renderer/renderer.tsx
|
|
1829
|
+
import React5 from "react";
|
|
1830
|
+
import {
|
|
1831
|
+
UTILS as UTILS3,
|
|
1832
|
+
segments
|
|
1833
|
+
} from "@coorax/core";
|
|
1834
|
+
|
|
1835
|
+
// src/registry/primitives.registry.tsx
|
|
1836
|
+
class PrimitivesComponentsRegistry {
|
|
1837
|
+
components = new Map;
|
|
1838
|
+
get(name) {
|
|
1839
|
+
const sid = Symbol.for(name);
|
|
1840
|
+
const foundCOmp = this.components.get(sid);
|
|
1841
|
+
if (foundCOmp) {
|
|
1842
|
+
return foundCOmp;
|
|
1843
|
+
}
|
|
1844
|
+
return null;
|
|
1845
|
+
}
|
|
1846
|
+
getAll(names) {
|
|
1847
|
+
return names.map((name) => this.get(name));
|
|
1848
|
+
}
|
|
1849
|
+
post(name, comp) {
|
|
1850
|
+
if (comp.length < 1) {
|
|
1851
|
+
throw new Error("Components arguments must be greater than 1");
|
|
1852
|
+
}
|
|
1853
|
+
const sid = Symbol.for(name);
|
|
1854
|
+
this.components.set(sid, comp);
|
|
1855
|
+
}
|
|
1856
|
+
delete(name) {
|
|
1857
|
+
const sid = Symbol.for(name);
|
|
1858
|
+
this.components.delete(sid);
|
|
1859
|
+
}
|
|
1860
|
+
batch(comps) {
|
|
1861
|
+
for (const [key, value] of Object.entries(comps)) {
|
|
1862
|
+
const sid = Symbol.for(key);
|
|
1863
|
+
this.components.set(sid, value);
|
|
1864
|
+
}
|
|
1865
|
+
}
|
|
1866
|
+
}
|
|
1867
|
+
|
|
1868
|
+
// src/hooks/use-block.ts
|
|
1869
|
+
import { useSyncExternalStore as useSyncExternalStore4 } from "react";
|
|
1870
|
+
function useBlock(id) {
|
|
1871
|
+
const engine = useEngine();
|
|
1872
|
+
return useSyncExternalStore4((fn) => engine.document.subscribeBlock(id, fn), () => engine.document.getBlock(id), () => engine.document.getBlock(id));
|
|
1873
|
+
}
|
|
1874
|
+
|
|
1875
|
+
// src/primitives/fields/field.primitives.tsx
|
|
1876
|
+
import { useCallback, useEffect as useEffect5, useRef as useRef3 } from "react";
|
|
1877
|
+
|
|
1878
|
+
// src/hooks/custom/use-isomorphic-layout-effect.ts
|
|
1879
|
+
import { useEffect as useEffect4, useLayoutEffect } from "react";
|
|
1880
|
+
var useIsomorphicLayoutEffect = typeof window !== "undefined" ? useLayoutEffect : useEffect4;
|
|
1881
|
+
|
|
1882
|
+
// src/primitives/fields/field.primitives.tsx
|
|
1883
|
+
import { jsxDEV as jsxDEV15 } from "react/jsx-dev-runtime";
|
|
1884
|
+
function FieldPrimitive({ blockId }) {
|
|
1885
|
+
const blockRef = useRef3(null);
|
|
1886
|
+
const engine = useEngine();
|
|
1887
|
+
const renderer = useRenderer();
|
|
1888
|
+
const block = useBlock(blockId);
|
|
1889
|
+
const sel = useSlice("textSelection");
|
|
1890
|
+
const updateRange = useCallback(() => {
|
|
1891
|
+
if (!sel)
|
|
1892
|
+
return;
|
|
1893
|
+
const root = document.querySelector(`[id="${CSS.escape(sel.blockId)}"][data-text-path="${CSS.escape(sel.path)}"]`);
|
|
1894
|
+
if (!root || !root.contains(document.activeElement))
|
|
1895
|
+
return;
|
|
1896
|
+
const domSel = document.getSelection();
|
|
1897
|
+
if (!domSel)
|
|
1898
|
+
return;
|
|
1899
|
+
if (domSel.rangeCount) {
|
|
1900
|
+
const cur = rangeToOffsets(root, domSel.getRangeAt(0));
|
|
1901
|
+
if (cur.start === sel.start && cur.end === sel.end)
|
|
1902
|
+
return;
|
|
1903
|
+
}
|
|
1904
|
+
const range = offsetsToRange(root, sel.start, sel.end);
|
|
1905
|
+
if (!range)
|
|
1906
|
+
return;
|
|
1907
|
+
domSel.removeAllRanges();
|
|
1908
|
+
domSel.addRange(range);
|
|
1909
|
+
}, [sel]);
|
|
1910
|
+
useIsomorphicLayoutEffect(() => {
|
|
1911
|
+
updateRange();
|
|
1912
|
+
}, [updateRange, engine]);
|
|
1913
|
+
useIsomorphicLayoutEffect(() => {
|
|
1914
|
+
if (!sel)
|
|
1915
|
+
return;
|
|
1916
|
+
const root = document.querySelector(`[id="${CSS.escape(sel.blockId)}"][data-text-path="${CSS.escape(sel.path)}"]`);
|
|
1917
|
+
if (!root)
|
|
1918
|
+
return;
|
|
1919
|
+
const range = offsetsToRange(root, sel.start, sel.end);
|
|
1920
|
+
if (!range)
|
|
1921
|
+
return;
|
|
1922
|
+
root.focus({ preventScroll: true });
|
|
1923
|
+
const domSel = document.getSelection();
|
|
1924
|
+
domSel.removeAllRanges();
|
|
1925
|
+
domSel.addRange(range);
|
|
1926
|
+
});
|
|
1927
|
+
useEffect5(() => {
|
|
1928
|
+
const domEl = blockRef.current;
|
|
1929
|
+
if (!domEl)
|
|
1930
|
+
return;
|
|
1931
|
+
const observer = new ResizeObserver(() => {
|
|
1932
|
+
const rect = blockRectFromBlockId(blockId);
|
|
1933
|
+
if (!rect || !!engine.document.get().order.size)
|
|
1934
|
+
return;
|
|
1935
|
+
engine.commands.tryExecute("blocks.focus", { id: blockId, rect });
|
|
1936
|
+
});
|
|
1937
|
+
observer.observe(domEl);
|
|
1938
|
+
return () => {
|
|
1939
|
+
observer.unobserve(domEl);
|
|
1940
|
+
observer.disconnect();
|
|
1941
|
+
};
|
|
1942
|
+
}, [engine, blockId]);
|
|
1943
|
+
if (!blockId.startsWith("field") || !block)
|
|
1944
|
+
return null;
|
|
1945
|
+
return /* @__PURE__ */ jsxDEV15("div", {
|
|
1946
|
+
ref: blockRef,
|
|
1947
|
+
children: renderer.renderField(block)
|
|
1948
|
+
}, undefined, false, undefined, this);
|
|
1949
|
+
}
|
|
1950
|
+
|
|
1951
|
+
// src/primitives/index.ts
|
|
1952
|
+
var FieldsPrimitives = {
|
|
1953
|
+
field: FieldPrimitive
|
|
1954
|
+
};
|
|
1955
|
+
|
|
1956
|
+
// src/renderer/renderer.utils.tsx
|
|
1957
|
+
import {
|
|
1958
|
+
UTILS as UTILS2
|
|
1959
|
+
} from "@coorax/core";
|
|
1960
|
+
|
|
1961
|
+
// src/lifecycle/fields/lifecycle.fields.ts
|
|
1962
|
+
import {
|
|
1963
|
+
UTILS
|
|
1964
|
+
} from "@coorax/core";
|
|
1965
|
+
|
|
1966
|
+
// src/lifecycle/fields/mouse.events/onclick.ts
|
|
1967
|
+
var createOnClick = async (engine, block, ev) => {
|
|
1968
|
+
ev.stopPropagation();
|
|
1969
|
+
const fieldInfo = getFieldTypeAndTargetId(engine, ev, block);
|
|
1970
|
+
if (!fieldInfo)
|
|
1971
|
+
return;
|
|
1972
|
+
const isOnControl = ev.ctrlKey || ev.metaKey;
|
|
1973
|
+
const selection = engine.state.get("selection");
|
|
1974
|
+
if (!selection)
|
|
1975
|
+
return;
|
|
1976
|
+
const keys = Array.from(selection.blocks.keys());
|
|
1977
|
+
const blockRect = blockRectFromBlockId(block.id);
|
|
1978
|
+
if (!blockRect)
|
|
1979
|
+
return;
|
|
1980
|
+
if (!ev.shiftKey || keys.includes(block.id)) {
|
|
1981
|
+
const blocks = new Map;
|
|
1982
|
+
blocks.set(block.id, blockRect);
|
|
1983
|
+
engine.commands.tryExecute("blocks.select", {
|
|
1984
|
+
blocks,
|
|
1985
|
+
mode: isOnControl ? "toggle" : "replace"
|
|
1986
|
+
});
|
|
1987
|
+
} else {
|
|
1988
|
+
const treeOrder = [...engine.document.get().order];
|
|
1989
|
+
const currentBlockIndex = treeOrder.indexOf(block.id);
|
|
1990
|
+
const orderMap = new Map(treeOrder.map((id, index) => [id, index]));
|
|
1991
|
+
const { minIndexKey, maxIndexKey } = keys.reduce((acc, key) => {
|
|
1992
|
+
const currPos = orderMap.get(key) ?? Infinity;
|
|
1993
|
+
const minPos = orderMap.get(acc.minIndexKey) ?? Infinity;
|
|
1994
|
+
const maxPos = orderMap.get(acc.maxIndexKey) ?? -Infinity;
|
|
1995
|
+
return {
|
|
1996
|
+
minIndexKey: currPos < minPos ? key : acc.minIndexKey,
|
|
1997
|
+
maxIndexKey: currPos > maxPos ? key : acc.maxIndexKey
|
|
1998
|
+
};
|
|
1999
|
+
}, { minIndexKey: keys[0], maxIndexKey: keys[0] });
|
|
2000
|
+
const lowestIndex = treeOrder.indexOf(minIndexKey);
|
|
2001
|
+
const greatestIndex = treeOrder.indexOf(maxIndexKey);
|
|
2002
|
+
const toSelect = (() => {
|
|
2003
|
+
if (currentBlockIndex > lowestIndex) {
|
|
2004
|
+
return treeOrder.slice(lowestIndex, currentBlockIndex + 1);
|
|
2005
|
+
}
|
|
2006
|
+
return treeOrder.slice(currentBlockIndex, greatestIndex + 1);
|
|
2007
|
+
})();
|
|
2008
|
+
const blocks = new Map;
|
|
2009
|
+
for (const id of toSelect) {
|
|
2010
|
+
const rect = blockRectFromBlockId(id);
|
|
2011
|
+
if (rect) {
|
|
2012
|
+
blocks.set(id, rect);
|
|
2013
|
+
}
|
|
2014
|
+
}
|
|
2015
|
+
engine.commands.tryExecute("blocks.select", {
|
|
2016
|
+
blocks,
|
|
2017
|
+
mode: "replace"
|
|
2018
|
+
});
|
|
2019
|
+
}
|
|
2020
|
+
await fieldInfo.emit();
|
|
2021
|
+
};
|
|
2022
|
+
|
|
2023
|
+
// src/lifecycle/fields/mouse.events/ondoubleclick.ts
|
|
2024
|
+
var createOnDoubleClick = async (engine, block, ev) => {
|
|
2025
|
+
ev.stopPropagation();
|
|
2026
|
+
const fieldInfo = getFieldTypeAndTargetId(engine, ev, block);
|
|
2027
|
+
if (!fieldInfo)
|
|
2028
|
+
return;
|
|
2029
|
+
const rect = blockRectFromBlockId(block.id);
|
|
2030
|
+
if (!rect)
|
|
2031
|
+
return;
|
|
2032
|
+
const focus = engine.state.get("focus");
|
|
2033
|
+
if (focus?.id === block.id) {
|
|
2034
|
+
return;
|
|
2035
|
+
}
|
|
2036
|
+
engine.commands.tryExecute("blocks.focus", { id: block.id, rect });
|
|
2037
|
+
await fieldInfo.emit();
|
|
2038
|
+
};
|
|
2039
|
+
|
|
2040
|
+
// src/lifecycle/fields/focus.events/onfocus.ts
|
|
2041
|
+
var createOnFocus = async (engine, block, ev) => {
|
|
2042
|
+
const fieldInfo = getFieldTypeAndTargetId(engine, ev, block);
|
|
2043
|
+
if (!fieldInfo)
|
|
2044
|
+
return;
|
|
2045
|
+
await fieldInfo.emit();
|
|
2046
|
+
};
|
|
2047
|
+
|
|
2048
|
+
// src/lifecycle/fields/focus.events/onblur.ts
|
|
2049
|
+
var createOnBlur = async (engine, block, ev) => {
|
|
2050
|
+
const fieldInfo = getFieldTypeAndTargetId(engine, ev, block);
|
|
2051
|
+
if (!fieldInfo)
|
|
2052
|
+
return;
|
|
2053
|
+
const textSel = engine.state.get("textSelection");
|
|
2054
|
+
if (textSel && textSel.start !== textSel.end)
|
|
2055
|
+
return;
|
|
2056
|
+
engine.commands.tryExecute("blocks.focus", null);
|
|
2057
|
+
await fieldInfo.emit();
|
|
2058
|
+
};
|
|
2059
|
+
|
|
2060
|
+
// src/lifecycle/fields/mouse.events/onmouseleave.ts
|
|
2061
|
+
var createOnMouseLeave = async (engine, block, ev) => {
|
|
2062
|
+
const fieldInfo = getFieldTypeAndTargetId(engine, ev, block);
|
|
2063
|
+
if (!fieldInfo)
|
|
2064
|
+
return;
|
|
2065
|
+
const state = engine.state.get("hover");
|
|
2066
|
+
if (!state)
|
|
2067
|
+
return;
|
|
2068
|
+
if (state === null)
|
|
2069
|
+
return;
|
|
2070
|
+
engine.commands.tryExecute("blocks.hover", null);
|
|
2071
|
+
await fieldInfo.emit();
|
|
2072
|
+
};
|
|
2073
|
+
|
|
2074
|
+
// src/lifecycle/fields/mouse.events/onmouseenter.ts
|
|
2075
|
+
var createOnMouseEnter = async (engine, block, ev) => {
|
|
2076
|
+
const fieldInfo = getFieldTypeAndTargetId(engine, ev, block);
|
|
2077
|
+
if (!fieldInfo)
|
|
2078
|
+
return;
|
|
2079
|
+
const selection = engine.state.get("selection");
|
|
2080
|
+
if (selection && selection.blocks.has(block.id)) {
|
|
2081
|
+
return;
|
|
2082
|
+
}
|
|
2083
|
+
const rect = blockRectFromBlockId(block.id);
|
|
2084
|
+
if (!rect)
|
|
2085
|
+
return;
|
|
2086
|
+
engine.commands.tryExecute("blocks.hover", { id: block.id, rect });
|
|
2087
|
+
await fieldInfo.emit();
|
|
2088
|
+
};
|
|
2089
|
+
|
|
2090
|
+
// src/lifecycle/fields/keyboard.events/onkeydown.ts
|
|
2091
|
+
import {
|
|
2092
|
+
FieldsUtils,
|
|
2093
|
+
isEditableField
|
|
2094
|
+
} from "@coorax/core";
|
|
2095
|
+
var createOnKeyDown = async (engine, block, ev) => {
|
|
2096
|
+
const fieldInfo = getFieldTypeAndTargetId(engine, ev, block);
|
|
2097
|
+
if (!fieldInfo)
|
|
2098
|
+
return;
|
|
2099
|
+
if (ev.ctrlKey || ev.metaKey)
|
|
2100
|
+
return;
|
|
2101
|
+
const tree = engine.document.get();
|
|
2102
|
+
const normalizedTarget = fieldInfo.target;
|
|
2103
|
+
const hasCurrentElement = engine.document.has(normalizedTarget);
|
|
2104
|
+
if (!hasCurrentElement) {
|
|
2105
|
+
console.warn(`Ignoring stale field target: ${normalizedTarget}`);
|
|
2106
|
+
return;
|
|
2107
|
+
}
|
|
2108
|
+
const targetIndex = [...tree.order].indexOf(normalizedTarget);
|
|
2109
|
+
const index = targetIndex + 1;
|
|
2110
|
+
const currentTarget = ev.currentTarget;
|
|
2111
|
+
if (ev.key === "Enter" && !ev.shiftKey) {
|
|
2112
|
+
ev.preventDefault();
|
|
2113
|
+
currentTarget.blur();
|
|
2114
|
+
const newField = FieldsUtils.NewField("text");
|
|
2115
|
+
engine.commands.tryExecute("blocks.insert", { block: newField, pos: index });
|
|
2116
|
+
await fieldInfo.emit();
|
|
2117
|
+
const blockRect = blockRectFromBlockId(newField.id);
|
|
2118
|
+
if (!blockRect)
|
|
2119
|
+
return;
|
|
2120
|
+
engine.commands.tryExecute("blocks.focus", { id: newField.id, rect: blockRect });
|
|
2121
|
+
}
|
|
2122
|
+
if (ev.key === "ArrowUp" || ev.key === "ArrowDown" || ev.key === "Tab") {
|
|
2123
|
+
ev.preventDefault();
|
|
2124
|
+
const mustBeNegativeDelta = ev.key === "ArrowUp" || ev.key === "Tab" && ev.shiftKey;
|
|
2125
|
+
const delta = mustBeNegativeDelta ? -1 : 1;
|
|
2126
|
+
const currentTree = engine.document.get();
|
|
2127
|
+
const blockIndex = Array.from(currentTree.order).indexOf(block.id);
|
|
2128
|
+
if (blockIndex === -1)
|
|
2129
|
+
return;
|
|
2130
|
+
if (blockIndex === 0 && delta < 0)
|
|
2131
|
+
return;
|
|
2132
|
+
if (blockIndex === currentTree.order.size - 1 && delta > 0)
|
|
2133
|
+
return;
|
|
2134
|
+
const rectId = [...currentTree.order][blockIndex + delta];
|
|
2135
|
+
const rect = blockRectFromBlockId(rectId);
|
|
2136
|
+
if (!rect) {
|
|
2137
|
+
return;
|
|
2138
|
+
}
|
|
2139
|
+
currentTarget.blur();
|
|
2140
|
+
engine.commands.tryExecute("blocks.focus", { id: rectId, rect });
|
|
2141
|
+
}
|
|
2142
|
+
if (ev.key === "Escape") {
|
|
2143
|
+
ev.preventDefault();
|
|
2144
|
+
currentTarget.blur();
|
|
2145
|
+
if (!isEditableField(block)) {
|
|
2146
|
+
engine.commands.tryExecute("blocks.select", { blocks: new Map, mode: "replace" });
|
|
2147
|
+
}
|
|
2148
|
+
}
|
|
2149
|
+
if (ev.key === "Backspace") {
|
|
2150
|
+
if (isEditableField(block)) {
|
|
2151
|
+
const textContent = block.props.children.text;
|
|
2152
|
+
if (textContent.length === 0) {
|
|
2153
|
+
const order = [...engine.document.get().order];
|
|
2154
|
+
const curBlockIndex = order.findIndex((id) => id === block.id);
|
|
2155
|
+
const prevSiblingBlock = engine.document.getBlock(order[curBlockIndex - 1]);
|
|
2156
|
+
if (prevSiblingBlock) {
|
|
2157
|
+
const blockRect = blockRectFromBlockId(prevSiblingBlock.id);
|
|
2158
|
+
if (!blockRect) {
|
|
2159
|
+
engine.commands.tryExecute("blocks.focus", null);
|
|
2160
|
+
return;
|
|
2161
|
+
}
|
|
2162
|
+
if (isEditableField(prevSiblingBlock)) {
|
|
2163
|
+
engine.commands.tryExecute("blocks.focus", {
|
|
2164
|
+
id: prevSiblingBlock.id,
|
|
2165
|
+
rect: blockRect
|
|
2166
|
+
});
|
|
2167
|
+
} else {
|
|
2168
|
+
engine.commands.tryExecute("blocks.select", {
|
|
2169
|
+
blocks: new Map().set(prevSiblingBlock.id, blockRect),
|
|
2170
|
+
mode: "replace"
|
|
2171
|
+
});
|
|
2172
|
+
}
|
|
2173
|
+
} else {
|
|
2174
|
+
engine.commands.tryExecute("blocks.focus", null);
|
|
2175
|
+
}
|
|
2176
|
+
engine.commands.tryExecute("blocks.delete", { blockId: block.id });
|
|
2177
|
+
}
|
|
2178
|
+
}
|
|
2179
|
+
}
|
|
2180
|
+
};
|
|
2181
|
+
|
|
2182
|
+
// src/lifecycle/fields/synthetic.events/onselect.ts
|
|
2183
|
+
var createOnSelect = async (engine, block, ev) => {
|
|
2184
|
+
const fieldInfo = getFieldTypeAndTargetId(engine, ev, block);
|
|
2185
|
+
if (!fieldInfo)
|
|
2186
|
+
return;
|
|
2187
|
+
const target = ev.currentTarget;
|
|
2188
|
+
if (!target)
|
|
2189
|
+
return;
|
|
2190
|
+
const documentSel = document.getSelection();
|
|
2191
|
+
if (!documentSel)
|
|
2192
|
+
return;
|
|
2193
|
+
const range = documentSel.getRangeAt(0);
|
|
2194
|
+
const el = range.commonAncestorContainer;
|
|
2195
|
+
const root = (el instanceof Element ? el : el.parentElement)?.closest("[data-text-path]");
|
|
2196
|
+
if (!root)
|
|
2197
|
+
return;
|
|
2198
|
+
const blockId = root.getAttribute("id");
|
|
2199
|
+
const path = root.dataset.textPath;
|
|
2200
|
+
const { start, end } = rangeToOffsets(root, range);
|
|
2201
|
+
engine.commands.tryExecute("text.setSelection", { blockId, path, start, end });
|
|
2202
|
+
const sel = {
|
|
2203
|
+
blockId: block.id,
|
|
2204
|
+
path: "props.children",
|
|
2205
|
+
start: Math.min(start, end),
|
|
2206
|
+
end: Math.max(start, end)
|
|
2207
|
+
};
|
|
2208
|
+
engine.commands.tryExecute("text.setSelection", sel);
|
|
2209
|
+
await fieldInfo.emit();
|
|
2210
|
+
};
|
|
2211
|
+
|
|
2212
|
+
// src/lifecycle/fields/synthetic.events/onload.ts
|
|
2213
|
+
var createOnLoad = async (engine, block, ev) => {
|
|
2214
|
+
const fieldInfo = getFieldTypeAndTargetId(engine, ev, block);
|
|
2215
|
+
if (!fieldInfo)
|
|
2216
|
+
return;
|
|
2217
|
+
console.log("Loaded");
|
|
2218
|
+
await fieldInfo.emit();
|
|
2219
|
+
};
|
|
2220
|
+
|
|
2221
|
+
// src/lifecycle/fields/lifecycle.fields.ts
|
|
2222
|
+
function getFieldTypeAndTargetId(engine, ev, block) {
|
|
2223
|
+
const target = block?.id ?? ev.currentTarget?.getAttribute("id") ?? null;
|
|
2224
|
+
const fieldType = block?.type ?? ev.currentTarget?.getAttribute("data-block-type") ?? null;
|
|
2225
|
+
if (!fieldType || !target) {
|
|
2226
|
+
console.warn(`Failed to continue event at ${ev.type}`);
|
|
2227
|
+
ev.preventDefault();
|
|
2228
|
+
ev.stopPropagation();
|
|
2229
|
+
return null;
|
|
2230
|
+
}
|
|
2231
|
+
const safeTarget = target;
|
|
2232
|
+
const safeFieldType = fieldType;
|
|
2233
|
+
function emit() {
|
|
2234
|
+
return engine.events.emit("field:lifecycle", {
|
|
2235
|
+
type: ev?._reactName || `on${UTILS.Capitalize(ev.type)}`,
|
|
2236
|
+
target: safeTarget,
|
|
2237
|
+
fieldType: safeFieldType,
|
|
2238
|
+
cycleEvent: ev
|
|
2239
|
+
});
|
|
2240
|
+
}
|
|
2241
|
+
return { fieldType: safeFieldType, target: safeTarget, emit };
|
|
2242
|
+
}
|
|
2243
|
+
function getAllLifeCycleEvents(engine, block) {
|
|
2244
|
+
function getCommonHandlers(tag) {
|
|
2245
|
+
switch (tag) {
|
|
2246
|
+
case "h1":
|
|
2247
|
+
case "p":
|
|
2248
|
+
return {
|
|
2249
|
+
onClick: createOnClick.bind(null, engine, block),
|
|
2250
|
+
onKeyDown: createOnKeyDown.bind(null, engine, block),
|
|
2251
|
+
onFocus: createOnFocus.bind(null, engine, block),
|
|
2252
|
+
onBlur: createOnBlur.bind(null, engine, block),
|
|
2253
|
+
onDoubleClick: createOnDoubleClick.bind(null, engine, block),
|
|
2254
|
+
onMouseEnter: createOnMouseEnter.bind(null, engine, block),
|
|
2255
|
+
onMouseLeave: createOnMouseLeave.bind(null, engine, block),
|
|
2256
|
+
onSelect: createOnSelect.bind(null, engine, block)
|
|
2257
|
+
};
|
|
2258
|
+
case "img":
|
|
2259
|
+
case "video":
|
|
2260
|
+
return {
|
|
2261
|
+
onClick: createOnClick.bind(null, engine, block),
|
|
2262
|
+
onKeyDown: createOnKeyDown.bind(null, engine, block),
|
|
2263
|
+
onFocus: createOnFocus.bind(null, engine, block),
|
|
2264
|
+
onMouseEnter: createOnMouseEnter.bind(null, engine, block),
|
|
2265
|
+
onMouseLeave: createOnMouseLeave.bind(null, engine, block),
|
|
2266
|
+
onLoad: createOnLoad.bind(null, engine, block)
|
|
2267
|
+
};
|
|
2268
|
+
case "hr":
|
|
2269
|
+
return {
|
|
2270
|
+
onClick: createOnClick.bind(null, engine, block),
|
|
2271
|
+
onMouseEnter: createOnMouseEnter.bind(null, engine, block),
|
|
2272
|
+
onMouseLeave: createOnMouseLeave.bind(null, engine, block)
|
|
2273
|
+
};
|
|
2274
|
+
default:
|
|
2275
|
+
return {};
|
|
2276
|
+
}
|
|
2277
|
+
}
|
|
2278
|
+
return {
|
|
2279
|
+
h1: { ...getCommonHandlers("h1") },
|
|
2280
|
+
p: { ...getCommonHandlers("p") },
|
|
2281
|
+
img: { ...getCommonHandlers("img") },
|
|
2282
|
+
video: { ...getCommonHandlers("video") },
|
|
2283
|
+
hr: { ...getCommonHandlers("hr") }
|
|
2284
|
+
};
|
|
2285
|
+
}
|
|
2286
|
+
function getFieldLifeCycleEvents(engine, tag, block) {
|
|
2287
|
+
return getAllLifeCycleEvents(engine, block)[tag];
|
|
2288
|
+
}
|
|
2289
|
+
var FLC = { getFieldLifeCycleEvents };
|
|
2290
|
+
|
|
2291
|
+
// src/renderer/renderer.utils.tsx
|
|
2292
|
+
import { cn as cn7 } from "cn";
|
|
2293
|
+
import { css } from "@emotion/css";
|
|
2294
|
+
function getTextFieldProps(engine, block) {
|
|
2295
|
+
const ui = engine.ui;
|
|
2296
|
+
const { style, pseudoStyle, className, children: _, placeholder, ...rest } = block.props;
|
|
2297
|
+
const uiStyles = ui.getStyle(block.type);
|
|
2298
|
+
const _pseudoStyle = {
|
|
2299
|
+
...uiStyles.pseudoStyle,
|
|
2300
|
+
...pseudoStyle
|
|
2301
|
+
};
|
|
2302
|
+
const pseudoClassName = resolveCoraxPseudoStyle(_pseudoStyle);
|
|
2303
|
+
const baseProps = {
|
|
2304
|
+
id: block.id,
|
|
2305
|
+
key: block.id,
|
|
2306
|
+
"data-block-type": block.type,
|
|
2307
|
+
style: {
|
|
2308
|
+
...uiStyles.style,
|
|
2309
|
+
...style,
|
|
2310
|
+
...block.props?.contentEditable ? { cursor: "auto", userSelect: "auto" } : null
|
|
2311
|
+
},
|
|
2312
|
+
className: cn7(uiStyles.className, className, pseudoClassName)
|
|
2313
|
+
};
|
|
2314
|
+
const containerProps = block.isContainerized() ? { "data-containerId": block.containerId } : {};
|
|
2315
|
+
const editableProps = {
|
|
2316
|
+
spellCheck: false,
|
|
2317
|
+
suppressContentEditableWarning: true,
|
|
2318
|
+
"data-placeholder": placeholder || "Tell the news here",
|
|
2319
|
+
...rest,
|
|
2320
|
+
contentEditable: !!rest?.contentEditable,
|
|
2321
|
+
autoFocus: true
|
|
2322
|
+
};
|
|
2323
|
+
return {
|
|
2324
|
+
...baseProps,
|
|
2325
|
+
...containerProps,
|
|
2326
|
+
...editableProps,
|
|
2327
|
+
...FLC.getFieldLifeCycleEvents(engine, block.tag, block)
|
|
2328
|
+
};
|
|
2329
|
+
}
|
|
2330
|
+
function getMediaFieldProps(engine, block) {
|
|
2331
|
+
const ui = engine.ui;
|
|
2332
|
+
const { style, pseudoStyle, className, ...rest } = block.props;
|
|
2333
|
+
const uiStyles = ui.getStyle(block.type);
|
|
2334
|
+
const _pseudoStyle = {
|
|
2335
|
+
...uiStyles.pseudoStyle,
|
|
2336
|
+
...pseudoStyle
|
|
2337
|
+
};
|
|
2338
|
+
const pseudoClassName = resolveCoraxPseudoStyle(_pseudoStyle);
|
|
2339
|
+
const baseProps = {
|
|
2340
|
+
id: block.id,
|
|
2341
|
+
key: block.id,
|
|
2342
|
+
"data-block-type": block.type,
|
|
2343
|
+
style: {
|
|
2344
|
+
...uiStyles.style,
|
|
2345
|
+
...style
|
|
2346
|
+
},
|
|
2347
|
+
className: cn7(uiStyles.className, className, pseudoClassName)
|
|
2348
|
+
};
|
|
2349
|
+
const containerProps = block.isContainerized() ? { "data-containerId": block.containerId } : {};
|
|
2350
|
+
return {
|
|
2351
|
+
...baseProps,
|
|
2352
|
+
...containerProps,
|
|
2353
|
+
...rest,
|
|
2354
|
+
...FLC.getFieldLifeCycleEvents(engine, block.tag, block)
|
|
2355
|
+
};
|
|
2356
|
+
}
|
|
2357
|
+
function resolveCoraxPseudoStyle(style) {
|
|
2358
|
+
if (!style || Object.keys(style).length === 0)
|
|
2359
|
+
return;
|
|
2360
|
+
return css(style);
|
|
2361
|
+
}
|
|
2362
|
+
|
|
2363
|
+
// src/renderer/utils/divider.renderer.utils.tsx
|
|
2364
|
+
import { cn as cn8 } from "cn";
|
|
2365
|
+
function getDividerFieldProps(engine, block) {
|
|
2366
|
+
const ui = engine.ui;
|
|
2367
|
+
const { style, pseudoStyle, className, ...rest } = block.props;
|
|
2368
|
+
const uiStyles = ui.getStyle(block.type);
|
|
2369
|
+
const _pseudoStyle = {
|
|
2370
|
+
...uiStyles.pseudoStyle,
|
|
2371
|
+
...pseudoStyle
|
|
2372
|
+
};
|
|
2373
|
+
const pseudoClassName = resolveCoraxPseudoStyle(_pseudoStyle);
|
|
2374
|
+
const baseProps = {
|
|
2375
|
+
id: block.id,
|
|
2376
|
+
key: block.id,
|
|
2377
|
+
"data-block-type": block.type,
|
|
2378
|
+
style: {
|
|
2379
|
+
...uiStyles.style,
|
|
2380
|
+
...style
|
|
2381
|
+
},
|
|
2382
|
+
className: cn8(uiStyles.className, className, pseudoClassName)
|
|
2383
|
+
};
|
|
2384
|
+
const containerProps = block.isContainerized() ? { "data-containerId": block.containerId } : {};
|
|
2385
|
+
const handlers = FLC.getFieldLifeCycleEvents(engine, block.tag, block);
|
|
2386
|
+
return {
|
|
2387
|
+
...baseProps,
|
|
2388
|
+
...containerProps,
|
|
2389
|
+
...rest,
|
|
2390
|
+
...handlers
|
|
2391
|
+
};
|
|
2392
|
+
}
|
|
2393
|
+
|
|
2394
|
+
// src/renderer/renderer.tsx
|
|
2395
|
+
import { jsxDEV as jsxDEV16, Fragment as Fragment3 } from "react/jsx-dev-runtime";
|
|
2396
|
+
var defaults = {
|
|
2397
|
+
bold: "strong",
|
|
2398
|
+
italic: "em",
|
|
2399
|
+
underline: "u",
|
|
2400
|
+
code: "code",
|
|
2401
|
+
link: "span",
|
|
2402
|
+
initial: "span"
|
|
2403
|
+
};
|
|
2404
|
+
var RANK = {
|
|
2405
|
+
link: 0,
|
|
2406
|
+
bold: 1,
|
|
2407
|
+
italic: 2,
|
|
2408
|
+
underline: 3,
|
|
2409
|
+
code: 4,
|
|
2410
|
+
initial: 5
|
|
2411
|
+
};
|
|
2412
|
+
function formatText(text) {
|
|
2413
|
+
return text;
|
|
2414
|
+
}
|
|
2415
|
+
|
|
2416
|
+
class ReactRenderer {
|
|
2417
|
+
engine;
|
|
2418
|
+
primitives = new PrimitivesComponentsRegistry;
|
|
2419
|
+
constructor(engine) {
|
|
2420
|
+
this.engine = engine;
|
|
2421
|
+
for (const [type, comp] of UTILS3.ObjectEntries(FieldsPrimitives)) {
|
|
2422
|
+
this.primitives.post(type, comp);
|
|
2423
|
+
}
|
|
2424
|
+
}
|
|
2425
|
+
renderContainer(contanier) {
|
|
2426
|
+
throw new Error("Method not implemented." + contanier.name);
|
|
2427
|
+
}
|
|
2428
|
+
renderTree(key = 0) {
|
|
2429
|
+
const children = Array.from(this.engine.document.get().order).map((id) => {
|
|
2430
|
+
const block = this.engine.document.getBlock(id);
|
|
2431
|
+
if (!block)
|
|
2432
|
+
return /* @__PURE__ */ jsxDEV16(Fragment3, {}, undefined, false, undefined, this);
|
|
2433
|
+
const Comp = this.primitives.get(block.variant);
|
|
2434
|
+
if (!Comp)
|
|
2435
|
+
return /* @__PURE__ */ jsxDEV16(Fragment3, {}, undefined, false, undefined, this);
|
|
2436
|
+
return /* @__PURE__ */ jsxDEV16(Comp, {
|
|
2437
|
+
blockId: id
|
|
2438
|
+
}, id, false, undefined, this);
|
|
2439
|
+
});
|
|
2440
|
+
return React5.createElement(React5.Fragment, { key }, ...children);
|
|
2441
|
+
}
|
|
2442
|
+
renderTextField(field, key = null) {
|
|
2443
|
+
const children = field.props.children;
|
|
2444
|
+
return React5.createElement(field.tag, {
|
|
2445
|
+
...getTextFieldProps(this.engine, field),
|
|
2446
|
+
key: key !== null ? key : field.id
|
|
2447
|
+
}, this.renderTextContent(children));
|
|
2448
|
+
}
|
|
2449
|
+
renderMediaField(field, key = null) {
|
|
2450
|
+
const isMediaReady = !!field.props.src.trim();
|
|
2451
|
+
if (!isMediaReady) {
|
|
2452
|
+
const Fallback = FallbackComponentsRegistry.get(field.type);
|
|
2453
|
+
if (Fallback)
|
|
2454
|
+
return /* @__PURE__ */ jsxDEV16(Fallback, {
|
|
2455
|
+
...field
|
|
2456
|
+
}, undefined, false, undefined, this);
|
|
2457
|
+
}
|
|
2458
|
+
return React5.createElement(field.tag, {
|
|
2459
|
+
...getMediaFieldProps(this.engine, field),
|
|
2460
|
+
key: key !== null ? key : field.id
|
|
2461
|
+
});
|
|
2462
|
+
}
|
|
2463
|
+
renderDividerField(field, key = null) {
|
|
2464
|
+
return React5.createElement(field.tag, {
|
|
2465
|
+
...getDividerFieldProps(this.engine, field),
|
|
2466
|
+
key: key !== null ? key : field.id
|
|
2467
|
+
});
|
|
2468
|
+
}
|
|
2469
|
+
renderField(_field, key = null) {
|
|
2470
|
+
const field = typeof _field === "string" ? this.engine.document.getBlock(_field) : _field;
|
|
2471
|
+
if (!field || field.variant !== "field")
|
|
2472
|
+
return /* @__PURE__ */ jsxDEV16(Fragment3, {}, undefined, false, undefined, this);
|
|
2473
|
+
switch (field.type) {
|
|
2474
|
+
case "text":
|
|
2475
|
+
case "heading":
|
|
2476
|
+
return this.renderTextField(field, key);
|
|
2477
|
+
case "video":
|
|
2478
|
+
case "image":
|
|
2479
|
+
return this.renderMediaField(field, key);
|
|
2480
|
+
case "divider":
|
|
2481
|
+
return this.renderDividerField(field);
|
|
2482
|
+
default:
|
|
2483
|
+
return /* @__PURE__ */ jsxDEV16(Fragment3, {}, undefined, false, undefined, this);
|
|
2484
|
+
}
|
|
2485
|
+
}
|
|
2486
|
+
renderSegment(seg, key) {
|
|
2487
|
+
const marks = [...seg.marks].sort((a, b) => (RANK[a.type] ?? 9) - (RANK[b.type] ?? 9));
|
|
2488
|
+
const node = marks.reduceRight((child, mark) => {
|
|
2489
|
+
const Comp = defaults[mark.type];
|
|
2490
|
+
return Comp ? /* @__PURE__ */ jsxDEV16(Comp, {
|
|
2491
|
+
...mark.attrs,
|
|
2492
|
+
children: child
|
|
2493
|
+
}, undefined, false, undefined, this) : child;
|
|
2494
|
+
}, formatText(seg.text));
|
|
2495
|
+
return /* @__PURE__ */ jsxDEV16(React5.Fragment, {
|
|
2496
|
+
children: node
|
|
2497
|
+
}, key, false, undefined, this);
|
|
2498
|
+
}
|
|
2499
|
+
renderTextContent(text) {
|
|
2500
|
+
if (!text?.text)
|
|
2501
|
+
return "";
|
|
2502
|
+
return segments(text).map(this.renderSegment);
|
|
2503
|
+
}
|
|
2504
|
+
renderNode(node, key = null) {
|
|
2505
|
+
return React5.createElement(node.tag, {
|
|
2506
|
+
style: node.props.style,
|
|
2507
|
+
className: node.props.className,
|
|
2508
|
+
key: key ?? node.id
|
|
2509
|
+
}, node.props.text);
|
|
2510
|
+
}
|
|
2511
|
+
}
|
|
2512
|
+
|
|
2513
|
+
// src/hooks/use-renderer.ts
|
|
2514
|
+
function useRenderer() {
|
|
2515
|
+
const engine = useEngine();
|
|
2516
|
+
return useMemo4(() => new ReactRenderer(engine), [engine]);
|
|
2517
|
+
}
|
|
2518
|
+
|
|
2519
|
+
// src/hooks/use-flow.ts
|
|
2520
|
+
import { useEffect as useEffect7 } from "react";
|
|
2521
|
+
|
|
2522
|
+
// src/hooks/custom/use-custom-ref.ts
|
|
2523
|
+
import { useEffect as useEffect6, useRef as useRef4 } from "react";
|
|
2524
|
+
function useCustomRef(factory) {
|
|
2525
|
+
const isServer = typeof window === "undefined";
|
|
2526
|
+
const getInitialValue = () => {
|
|
2527
|
+
if (isServer)
|
|
2528
|
+
return null;
|
|
2529
|
+
try {
|
|
2530
|
+
return typeof factory === "function" ? factory() : factory;
|
|
2531
|
+
} catch {
|
|
2532
|
+
return null;
|
|
2533
|
+
}
|
|
2534
|
+
};
|
|
2535
|
+
const ref = useRef4(getInitialValue());
|
|
2536
|
+
useEffect6(() => {
|
|
2537
|
+
try {
|
|
2538
|
+
ref.current = typeof factory === "function" ? factory() : factory;
|
|
2539
|
+
} catch {}
|
|
2540
|
+
}, [factory]);
|
|
2541
|
+
return ref;
|
|
2542
|
+
}
|
|
2543
|
+
|
|
2544
|
+
// src/hooks/use-flow.ts
|
|
2545
|
+
function editorRectFromDomElement(domElement) {
|
|
2546
|
+
const containerRect = domElement.getBoundingClientRect();
|
|
2547
|
+
return {
|
|
2548
|
+
scrollTop: domElement.scrollTop,
|
|
2549
|
+
scrollLeft: domElement.scrollLeft,
|
|
2550
|
+
clientLeft: domElement.clientLeft,
|
|
2551
|
+
clientTop: domElement.clientTop,
|
|
2552
|
+
top: containerRect.top,
|
|
2553
|
+
left: containerRect.left,
|
|
2554
|
+
bottom: containerRect.bottom,
|
|
2555
|
+
right: containerRect.right,
|
|
2556
|
+
width: containerRect.width,
|
|
2557
|
+
height: containerRect.height,
|
|
2558
|
+
offsetTop: domElement.offsetTop,
|
|
2559
|
+
offsetLeft: domElement.offsetLeft,
|
|
2560
|
+
offsetWidth: domElement.offsetWidth,
|
|
2561
|
+
offsetHeight: domElement.offsetHeight
|
|
2562
|
+
};
|
|
2563
|
+
}
|
|
2564
|
+
function useFlow() {
|
|
2565
|
+
const engine = useEngine();
|
|
2566
|
+
const coraxFlowEditorRef = useCustomRef(() => {
|
|
2567
|
+
if (typeof document === "undefined")
|
|
2568
|
+
return null;
|
|
2569
|
+
const flowEditor = document.getElementById("corax_flow_blocks_container");
|
|
2570
|
+
if (!flowEditor)
|
|
2571
|
+
return null;
|
|
2572
|
+
engine.commands.tryExecute("editor.rect", { editorRect: editorRectFromDomElement(flowEditor) });
|
|
2573
|
+
return flowEditor;
|
|
2574
|
+
});
|
|
2575
|
+
useEffect7(() => {
|
|
2576
|
+
function handleKeyDown(ev) {
|
|
2577
|
+
if (!ev.ctrlKey || ev.key.toLocaleLowerCase() !== "z")
|
|
2578
|
+
return;
|
|
2579
|
+
engine.commands.tryExecute(ev.shiftKey ? "history.redo" : "history.undo", {});
|
|
2580
|
+
}
|
|
2581
|
+
const flowEditor = coraxFlowEditorRef.current;
|
|
2582
|
+
function handleClick(ev) {
|
|
2583
|
+
const target = ev.target;
|
|
2584
|
+
if (!(target instanceof Element))
|
|
2585
|
+
return;
|
|
2586
|
+
if (target.closest("[data-block-type]") || target.closest("[data-slot-type]"))
|
|
2587
|
+
return;
|
|
2588
|
+
engine.commands.tryExecute("blocks.select", { blocks: new Map, mode: "replace" });
|
|
2589
|
+
}
|
|
2590
|
+
if (flowEditor) {
|
|
2591
|
+
flowEditor.addEventListener("click", handleClick);
|
|
2592
|
+
}
|
|
2593
|
+
document.addEventListener("keydown", handleKeyDown);
|
|
2594
|
+
return () => {
|
|
2595
|
+
document.removeEventListener("keydown", handleKeyDown);
|
|
2596
|
+
if (flowEditor) {
|
|
2597
|
+
flowEditor.removeEventListener("click", handleClick);
|
|
2598
|
+
}
|
|
2599
|
+
};
|
|
2600
|
+
}, [engine, coraxFlowEditorRef]);
|
|
2601
|
+
useEffect7(() => {
|
|
2602
|
+
const element = coraxFlowEditorRef.current;
|
|
2603
|
+
if (!element)
|
|
2604
|
+
return;
|
|
2605
|
+
const publishEditorRect = () => {
|
|
2606
|
+
engine.commands.tryExecute("editor.rect", { editorRect: editorRectFromDomElement(element) });
|
|
2607
|
+
};
|
|
2608
|
+
const observer = new ResizeObserver(publishEditorRect);
|
|
2609
|
+
observer.observe(element);
|
|
2610
|
+
element.addEventListener("scroll", publishEditorRect, { passive: true });
|
|
2611
|
+
return () => {
|
|
2612
|
+
observer.disconnect();
|
|
2613
|
+
element.removeEventListener("scroll", publishEditorRect);
|
|
2614
|
+
};
|
|
2615
|
+
}, [engine, coraxFlowEditorRef]);
|
|
2616
|
+
}
|
|
2617
|
+
|
|
2618
|
+
// src/hooks/use-text-input.ts
|
|
2619
|
+
import { useEffect as useEffect8 } from "react";
|
|
2620
|
+
function useTextInput(engine) {
|
|
2621
|
+
useEffect8(() => {
|
|
2622
|
+
let composing = false;
|
|
2623
|
+
const rootOf = (t) => ((t instanceof Element ? t : t?.parentElement) ?? null)?.closest("[data-text-path]") ?? null;
|
|
2624
|
+
const onBeforeInput = (e) => {
|
|
2625
|
+
const root = rootOf(e.target);
|
|
2626
|
+
if (!root || composing || e.isComposing)
|
|
2627
|
+
return;
|
|
2628
|
+
const tr = e.getTargetRanges()[0];
|
|
2629
|
+
if (tr) {
|
|
2630
|
+
const r = document.createRange();
|
|
2631
|
+
r.setStart(tr.startContainer, tr.startOffset);
|
|
2632
|
+
r.setEnd(tr.endContainer, tr.endOffset);
|
|
2633
|
+
engine.commands.tryExecute("text.setSelection", {
|
|
2634
|
+
blockId: root.dataset.blockId,
|
|
2635
|
+
path: root.dataset.textPath,
|
|
2636
|
+
...rangeToOffsets(root, r)
|
|
2637
|
+
});
|
|
2638
|
+
}
|
|
2639
|
+
switch (e.inputType) {
|
|
2640
|
+
case "insertText":
|
|
2641
|
+
if (e.data)
|
|
2642
|
+
engine.commands.tryExecute("text.insert", { text: e.data });
|
|
2643
|
+
break;
|
|
2644
|
+
case "insertParagraph":
|
|
2645
|
+
case "insertLineBreak":
|
|
2646
|
+
engine.commands.tryExecute("text.insert", { text: `
|
|
2647
|
+
` });
|
|
2648
|
+
break;
|
|
2649
|
+
case "insertFromPaste": {
|
|
2650
|
+
const text = e.dataTransfer?.getData("text/plain");
|
|
2651
|
+
if (text)
|
|
2652
|
+
engine.commands.tryExecute("text.insert", { text });
|
|
2653
|
+
break;
|
|
2654
|
+
}
|
|
2655
|
+
case "deleteContentBackward":
|
|
2656
|
+
engine.commands.tryExecute("text.delete", { direction: "backward" });
|
|
2657
|
+
break;
|
|
2658
|
+
case "deleteWordBackward":
|
|
2659
|
+
engine.commands.tryExecute("text.delete", { direction: "backward", type: "word" });
|
|
2660
|
+
break;
|
|
2661
|
+
case "deleteContentForward":
|
|
2662
|
+
engine.commands.tryExecute("text.delete", { direction: "forward" });
|
|
2663
|
+
break;
|
|
2664
|
+
case "deleteWordForward":
|
|
2665
|
+
engine.commands.tryExecute("text.delete", { direction: "forward", type: "word" });
|
|
2666
|
+
break;
|
|
2667
|
+
case "historyUndo":
|
|
2668
|
+
engine.commands.tryExecute("history.undo", undefined);
|
|
2669
|
+
break;
|
|
2670
|
+
case "historyRedo":
|
|
2671
|
+
engine.commands.tryExecute("history.redo", undefined);
|
|
2672
|
+
break;
|
|
2673
|
+
default:
|
|
2674
|
+
break;
|
|
2675
|
+
}
|
|
2676
|
+
e.preventDefault();
|
|
2677
|
+
};
|
|
2678
|
+
const start = () => {
|
|
2679
|
+
composing = true;
|
|
2680
|
+
};
|
|
2681
|
+
const end = () => {
|
|
2682
|
+
composing = false;
|
|
2683
|
+
};
|
|
2684
|
+
document.addEventListener("beforeinput", onBeforeInput);
|
|
2685
|
+
document.addEventListener("compositionstart", start);
|
|
2686
|
+
document.addEventListener("compositionend", end);
|
|
2687
|
+
return () => {
|
|
2688
|
+
document.removeEventListener("beforeinput", onBeforeInput);
|
|
2689
|
+
document.removeEventListener("compositionstart", start);
|
|
2690
|
+
document.removeEventListener("compositionend", end);
|
|
2691
|
+
};
|
|
2692
|
+
}, [engine]);
|
|
2693
|
+
}
|
|
2694
|
+
|
|
2695
|
+
// src/components/corax-flow.tsx
|
|
2696
|
+
import { cn as cn9 } from "cn";
|
|
2697
|
+
import { jsxDEV as jsxDEV17 } from "react/jsx-dev-runtime";
|
|
2698
|
+
"use client";
|
|
2699
|
+
function CoraxFlow({
|
|
2700
|
+
children,
|
|
2701
|
+
className
|
|
2702
|
+
}) {
|
|
2703
|
+
const [count, forceUpdate] = useReducer((x) => x + 1, 0);
|
|
2704
|
+
const engine = useEngine();
|
|
2705
|
+
const renderer = useRenderer();
|
|
2706
|
+
useFlow();
|
|
2707
|
+
useTextInput(engine);
|
|
2708
|
+
useEffect9(() => {
|
|
2709
|
+
const disposer = engine.events.on("blocks:insert", () => {
|
|
2710
|
+
forceUpdate();
|
|
2711
|
+
});
|
|
2712
|
+
return () => disposer();
|
|
2713
|
+
}, [engine]);
|
|
2714
|
+
return /* @__PURE__ */ jsxDEV17("div", {
|
|
2715
|
+
id: "corax_flow_editor",
|
|
2716
|
+
className: "relative flex flex-col w-full max-w-200 h-full",
|
|
2717
|
+
children: [
|
|
2718
|
+
"CoraxFlow",
|
|
2719
|
+
/* @__PURE__ */ jsxDEV17("div", {
|
|
2720
|
+
id: "corax_flow_blocks_container",
|
|
2721
|
+
className: cn9("relative w-full h-full flex flex-col overflow-auto scrollbar-none pl-10 gap-1", className),
|
|
2722
|
+
children: [
|
|
2723
|
+
renderer.renderTree(count),
|
|
2724
|
+
children
|
|
2725
|
+
]
|
|
2726
|
+
}, undefined, true, undefined, this)
|
|
2727
|
+
]
|
|
2728
|
+
}, undefined, true, undefined, this);
|
|
2729
|
+
}
|
|
2730
|
+
|
|
2731
|
+
// src/components/index.ts
|
|
2732
|
+
"use client";
|
|
2733
|
+
// src/index.ts
|
|
2734
|
+
export * from "@coorax/core";
|
|
2735
|
+
"use client";
|
|
2736
|
+
export {
|
|
2737
|
+
useUIContext,
|
|
2738
|
+
useSlice,
|
|
2739
|
+
useIsomorphicLayoutEffect,
|
|
2740
|
+
useEngine,
|
|
2741
|
+
loadCompleteDocument,
|
|
2742
|
+
Toolbar,
|
|
2743
|
+
TextInspector,
|
|
2744
|
+
SlotsModule,
|
|
2745
|
+
Overlays,
|
|
2746
|
+
FallbacksModule,
|
|
2747
|
+
CoraxFlow,
|
|
2748
|
+
CoraxEngineProvider,
|
|
2749
|
+
BlockActions,
|
|
2750
|
+
AutoSaveModule
|
|
2751
|
+
};
|