@coorax/react 0.0.0-canary.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (92) hide show
  1. package/.oxlintrc.json +8 -0
  2. package/package.json +52 -0
  3. package/src/components/commons/button.tsx +59 -0
  4. package/src/components/corax-context-provider.tsx +47 -0
  5. package/src/components/corax-flow.tsx +44 -0
  6. package/src/components/fallbacks/image.fallback.tsx +23 -0
  7. package/src/components/index.ts +3 -0
  8. package/src/components/slots/buttons/block-actions-button.tsx +85 -0
  9. package/src/components/slots/buttons/toolbar-button.tsx +40 -0
  10. package/src/components/slots/index.ts +1 -0
  11. package/src/components/slots/maps/block-actions.tsx +84 -0
  12. package/src/components/slots/maps/index.ts +4 -0
  13. package/src/components/slots/maps/overlays.tsx +9 -0
  14. package/src/components/slots/maps/text-inspector.tsx +5 -0
  15. package/src/components/slots/maps/toolbar.tsx +79 -0
  16. package/src/components/slots/overlays/focus-overlay.tsx +30 -0
  17. package/src/components/slots/overlays/hover-overlay.tsx +18 -0
  18. package/src/components/slots/overlays/selection-overlay.tsx +20 -0
  19. package/src/components/slots/slot-button.tsx +20 -0
  20. package/src/components/slots/slot-component.tsx +17 -0
  21. package/src/components/slots/slots.tsx +29 -0
  22. package/src/constants/field.constants.ts +3 -0
  23. package/src/hooks/custom/use-custom-ref.ts +11 -0
  24. package/src/hooks/index.ts +2 -0
  25. package/src/hooks/use-block-entry.ts +44 -0
  26. package/src/hooks/use-block.ts +11 -0
  27. package/src/hooks/use-flow.ts +83 -0
  28. package/src/hooks/use-keyboard.ts +66 -0
  29. package/src/hooks/use-registry.ts +18 -0
  30. package/src/hooks/use-renderer.ts +8 -0
  31. package/src/hooks/use-slice.ts +13 -0
  32. package/src/hooks/use-slots.ts +13 -0
  33. package/src/hooks/use-tasks.ts +33 -0
  34. package/src/hooks/use-text-input.ts +83 -0
  35. package/src/hooks/use-ui-context.ts +16 -0
  36. package/src/hooks/useSelection.ts +27 -0
  37. package/src/index.ts +11 -0
  38. package/src/lib/comp.utils.tsx +44 -0
  39. package/src/lib/func.utils.ts +15 -0
  40. package/src/lib/selection.utils.ts +33 -0
  41. package/src/lib/slots.utils.tsx +23 -0
  42. package/src/lifecycle/fields/focus.events/onbeforeinput.ts +21 -0
  43. package/src/lifecycle/fields/focus.events/onblur.ts +19 -0
  44. package/src/lifecycle/fields/focus.events/onfocus.ts +16 -0
  45. package/src/lifecycle/fields/keyboard.events/onkeydown.ts +128 -0
  46. package/src/lifecycle/fields/lifecycle.fields.ts +116 -0
  47. package/src/lifecycle/fields/lifecycle.fields.types.ts +33 -0
  48. package/src/lifecycle/fields/lifecycle.utils.ts +26 -0
  49. package/src/lifecycle/fields/mouse.events/onclick.ts +79 -0
  50. package/src/lifecycle/fields/mouse.events/ondoubleclick.ts +27 -0
  51. package/src/lifecycle/fields/mouse.events/onmouseenter.ts +27 -0
  52. package/src/lifecycle/fields/mouse.events/onmouseleave.ts +21 -0
  53. package/src/lifecycle/fields/synthetic.events/onload.ts +16 -0
  54. package/src/lifecycle/fields/synthetic.events/onselect.ts +57 -0
  55. package/src/lifecycle/index.ts +1 -0
  56. package/src/modules/autosave.module.ts +238 -0
  57. package/src/modules/fallbacks.module.tsx +19 -0
  58. package/src/modules/index.ts +3 -0
  59. package/src/modules/slots.module.tsx +28 -0
  60. package/src/modules/ui-primitives.module.tsx +20 -0
  61. package/src/primitives/fields/field.primitives.tsx +86 -0
  62. package/src/primitives/index.ts +8 -0
  63. package/src/registry/fallbacks.registry.tsx +9 -0
  64. package/src/registry/index.ts +5 -0
  65. package/src/registry/primitives.registry.tsx +57 -0
  66. package/src/registry/registry.ts +44 -0
  67. package/src/registry/slots.registry.tsx +6 -0
  68. package/src/registry/ui.primitives.registry.tsx +9 -0
  69. package/src/renderer/renderer.test.ts +34 -0
  70. package/src/renderer/renderer.tsx +164 -0
  71. package/src/renderer/renderer.types.ts +3 -0
  72. package/src/renderer/renderer.utils.tsx +139 -0
  73. package/src/renderer/utils/divider.renderer.utils.tsx +53 -0
  74. package/src/services/localdb/db/db.test.ts +351 -0
  75. package/src/services/localdb/db/db.ts +628 -0
  76. package/src/services/localdb/lexer/lexer.test.ts +123 -0
  77. package/src/services/localdb/lexer/lexer.ts +173 -0
  78. package/src/services/localdb/lexer/lexer.types.ts +34 -0
  79. package/src/services/localdb/operator/operator.ts +16 -0
  80. package/src/services/localdb/parser/parser.constants.ts +23 -0
  81. package/src/services/localdb/parser/parser.test.ts +172 -0
  82. package/src/services/localdb/parser/parser.ts +427 -0
  83. package/src/services/localdb/parser/parser.types.ts +56 -0
  84. package/src/services/localdb/sheet/sheet.constants.ts +32 -0
  85. package/src/services/localdb/sheet/sheet.ts +47 -0
  86. package/src/services/localdb/sheet/sheet.types.ts +38 -0
  87. package/styles.css +2 -0
  88. package/tsconfig.app.json +26 -0
  89. package/tsconfig.json +9 -0
  90. package/tsconfig.node.json +9 -0
  91. package/tsconfig.tsbuildinfo +1 -0
  92. package/vite.config.ts +9 -0
@@ -0,0 +1,128 @@
1
+ import {
2
+ FieldsUtils,
3
+ isEditableField,
4
+ type Block,
5
+ type BlockId,
6
+ type BlockRect,
7
+ type BlockTag,
8
+ type Engine,
9
+ type TextField,
10
+ } from "@coorax/core";
11
+ import { getFieldTypeAndTargetId } from "../lifecycle.fields";
12
+ import type { FnParams, NativeCallback, LifeCycleCallback } from "../lifecycle.fields.types";
13
+ import { blockRectFromBlockId } from "../lifecycle.utils";
14
+
15
+ export const createOnKeyDown = (async <K extends BlockTag = BlockTag>(
16
+ engine: Engine,
17
+ block: Block,
18
+ ev: FnParams<NativeCallback<K, "onKeyDown">>,
19
+ ) => {
20
+ const fieldInfo = getFieldTypeAndTargetId(engine, ev, block);
21
+ if (!fieldInfo) return;
22
+ if (ev.ctrlKey || ev.metaKey) return;
23
+
24
+ const tree = engine.document.get();
25
+
26
+ const normalizedTarget = fieldInfo.target;
27
+ const hasCurrentElement = engine.document.has(normalizedTarget);
28
+
29
+ if (!hasCurrentElement) {
30
+ console.warn(`Ignoring stale field target: ${normalizedTarget}`);
31
+ return;
32
+ }
33
+
34
+ const targetIndex = [...tree.order].indexOf(normalizedTarget);
35
+ const index = targetIndex + 1;
36
+
37
+ const currentTarget = ev.currentTarget;
38
+
39
+ //Handle new block insertion
40
+ if (ev.key === "Enter" && !ev.shiftKey) {
41
+ ev.preventDefault();
42
+ currentTarget.blur();
43
+
44
+ const newField = FieldsUtils.NewField("text");
45
+
46
+ engine.commands.tryExecute("blocks.insert", { block: newField, pos: index });
47
+ await fieldInfo.emit();
48
+
49
+ // Apres emit pour etre sur que the element integrate the DOM
50
+ const blockRect = blockRectFromBlockId(newField.id);
51
+ if (!blockRect) return;
52
+
53
+ engine.commands.tryExecute("blocks.focus", { id: newField.id, rect: blockRect });
54
+ }
55
+
56
+ // Handle sibling block focus
57
+ if (ev.key === "ArrowUp" || ev.key === "ArrowDown" || ev.key === "Tab") {
58
+ ev.preventDefault();
59
+
60
+ const mustBeNegativeDelta = ev.key === "ArrowUp" || (ev.key === "Tab" && ev.shiftKey);
61
+
62
+ const delta = mustBeNegativeDelta ? -1 : 1;
63
+
64
+ const currentTree = engine.document.get();
65
+ const blockIndex = Array.from(currentTree.order).indexOf(block.id);
66
+
67
+ if (blockIndex === -1) return;
68
+ if (blockIndex === 0 && delta < 0) return;
69
+
70
+ if (blockIndex === currentTree.order.size - 1 && delta > 0) return;
71
+ const rectId = [...currentTree.order][blockIndex + delta];
72
+
73
+ const rect = blockRectFromBlockId(rectId);
74
+ if (!rect) {
75
+ return;
76
+ }
77
+
78
+ currentTarget.blur();
79
+ engine.commands.tryExecute("blocks.focus", { id: rectId, rect });
80
+ }
81
+
82
+ if (ev.key === "Escape") {
83
+ ev.preventDefault();
84
+ //#The blur is neccessary because onBlur use it to compute save operation
85
+ currentTarget.blur();
86
+
87
+ if (!isEditableField(block)) {
88
+ engine.commands.tryExecute("blocks.select", { blocks: new Map(), mode: "replace" });
89
+ }
90
+ // engine.commands.tryExecute("blocks.focus", { focusedId: null, blockRect: null })
91
+ }
92
+
93
+ if (ev.key === "Backspace") {
94
+ if (isEditableField(block)) {
95
+ const textContent = (block as TextField).props.children.text;
96
+
97
+ if (textContent.length === 0) {
98
+ const order = [...engine.document.get().order];
99
+ const curBlockIndex = order.findIndex((id) => id === block.id);
100
+
101
+ const prevSiblingBlock = engine.document.getBlock(order[curBlockIndex - 1]);
102
+ if (prevSiblingBlock) {
103
+ const blockRect = blockRectFromBlockId(prevSiblingBlock.id);
104
+ if (!blockRect) {
105
+ engine.commands.tryExecute("blocks.focus", null);
106
+ return;
107
+ }
108
+
109
+ if (isEditableField(prevSiblingBlock)) {
110
+ engine.commands.tryExecute("blocks.focus", {
111
+ id: prevSiblingBlock.id,
112
+ rect: blockRect,
113
+ });
114
+ } else {
115
+ engine.commands.tryExecute("blocks.select", {
116
+ blocks: new Map<BlockId, BlockRect>().set(prevSiblingBlock.id, blockRect),
117
+ mode: "replace",
118
+ });
119
+ }
120
+ } else {
121
+ engine.commands.tryExecute("blocks.focus", null);
122
+ }
123
+
124
+ engine.commands.tryExecute("blocks.delete", { blockId: block.id });
125
+ }
126
+ }
127
+ }
128
+ }) satisfies LifeCycleCallback<any, any, "onKeyDown">;
@@ -0,0 +1,116 @@
1
+ import {
2
+ UTILS,
3
+ type Block,
4
+ type BlockId,
5
+ type Engine,
6
+ type BlockTag,
7
+ type FieldType,
8
+ } from "@coorax/core";
9
+ import type { FieldLifeCycleEvents, FnParams, NativeCallback } from "./lifecycle.fields.types";
10
+
11
+ import { createOnClick } from "./mouse.events/onclick";
12
+ import { createOnDoubleClick } from "./mouse.events/ondoubleclick";
13
+ import { createOnFocus } from "./focus.events/onfocus";
14
+ import { createOnBlur } from "./focus.events/onblur";
15
+ import { createOnMouseLeave } from "./mouse.events/onmouseleave";
16
+ import { createOnMouseEnter } from "./mouse.events/onmouseenter";
17
+ import { createOnKeyDown } from "./keyboard.events/onkeydown";
18
+ import { createOnSelect } from "./synthetic.events/onselect";
19
+ import { createOnLoad } from "./synthetic.events/onload";
20
+
21
+ export function getFieldTypeAndTargetId(
22
+ engine: Engine,
23
+ ev: FnParams<NativeCallback>,
24
+ block: Block,
25
+ ) {
26
+ const target = (block?.id ?? ev.currentTarget?.getAttribute("id") ?? null) as BlockId | null;
27
+ const fieldType = (block?.type ??
28
+ ev.currentTarget?.getAttribute("data-block-type") ??
29
+ null) as FieldType | null;
30
+
31
+ if (!fieldType || !target) {
32
+ console.warn(`Failed to continue event at ${ev.type}`);
33
+ ev.preventDefault();
34
+ ev.stopPropagation();
35
+ return null;
36
+ }
37
+
38
+ const safeTarget = target as BlockId;
39
+ const safeFieldType = fieldType as FieldType;
40
+
41
+ function emit() {
42
+ // console.log("LF: ", ev.type);
43
+
44
+ return engine.events.emit("field:lifecycle", {
45
+ type: (ev as any)?._reactName || `on${UTILS.Capitalize(ev.type)}`,
46
+ target: safeTarget,
47
+ fieldType: safeFieldType,
48
+ cycleEvent: ev,
49
+ });
50
+ }
51
+
52
+ return { fieldType: safeFieldType, target: safeTarget, emit };
53
+ }
54
+
55
+ type AllLCEvents = {
56
+ [T in BlockTag]?: FieldLifeCycleEvents<T>;
57
+ };
58
+
59
+ function getAllLifeCycleEvents(engine: Engine, block: Block): AllLCEvents {
60
+ function getCommonHandlers<TagName extends BlockTag = BlockTag>(
61
+ tag: TagName,
62
+ ): AllLCEvents[TagName] {
63
+ switch (tag) {
64
+ case "h1":
65
+ case "p":
66
+ return {
67
+ onClick: createOnClick.bind(null, engine, block),
68
+ onKeyDown: createOnKeyDown.bind(null, engine, block),
69
+ onFocus: createOnFocus.bind(null, engine, block),
70
+ onBlur: createOnBlur.bind(null, engine, block),
71
+ onDoubleClick: createOnDoubleClick.bind(null, engine, block),
72
+ onMouseEnter: createOnMouseEnter.bind(null, engine, block),
73
+ onMouseLeave: createOnMouseLeave.bind(null, engine, block),
74
+ onSelect: createOnSelect.bind(null, engine, block),
75
+ // onBeforeInput: createOnBeforeInput.bind(null, engine, block),
76
+ };
77
+ case "img":
78
+ case "video":
79
+ return {
80
+ onClick: createOnClick.bind(null, engine, block),
81
+ onKeyDown: createOnKeyDown.bind(null, engine, block),
82
+ onFocus: createOnFocus.bind(null, engine, block),
83
+ // onDoubleClick: createOnDoubleClick.bind(null, engine, block),
84
+ onMouseEnter: createOnMouseEnter.bind(null, engine, block),
85
+ onMouseLeave: createOnMouseLeave.bind(null, engine, block),
86
+ onLoad: createOnLoad.bind(null, engine, block),
87
+ };
88
+ case "hr":
89
+ return {
90
+ onClick: createOnClick.bind(null, engine, block),
91
+ onMouseEnter: createOnMouseEnter.bind(null, engine, block),
92
+ onMouseLeave: createOnMouseLeave.bind(null, engine, block),
93
+ };
94
+ default:
95
+ return {};
96
+ }
97
+ }
98
+
99
+ return {
100
+ h1: { ...getCommonHandlers<"h1">("h1") },
101
+ p: { ...getCommonHandlers<"p">("p") },
102
+ img: { ...getCommonHandlers<"img">("img") },
103
+ video: { ...getCommonHandlers<"video">("video") },
104
+ hr: { ...getCommonHandlers<"hr">("hr") },
105
+ };
106
+ }
107
+
108
+ function getFieldLifeCycleEvents<K extends BlockTag = BlockTag>(
109
+ engine: Engine,
110
+ tag: K,
111
+ block: Block,
112
+ ): FieldLifeCycleEvents<K> {
113
+ return getAllLifeCycleEvents(engine, block)[tag] as FieldLifeCycleEvents<K>;
114
+ }
115
+
116
+ export const FLC = { getFieldLifeCycleEvents };
@@ -0,0 +1,33 @@
1
+ import type { BlockId, BlockTag, BlockType, Engine, Field, FieldType } from "@coorax/core";
2
+ import type { DOMAttributes } from "react";
3
+
4
+ export type FieldElementOf<T extends BlockTag> = HTMLElementTagNameMap[T];
5
+
6
+ export interface FieldLifeCycleEvents<T extends BlockTag = BlockTag> extends Omit<
7
+ DOMAttributes<FieldElementOf<T>>,
8
+ "dangerouslySetInnerHTML" | "children"
9
+ > {}
10
+ export type FieldLifeCycleLiterals<T extends BlockTag = BlockTag> = keyof FieldLifeCycleEvents<T>;
11
+ export type NativeCallback<
12
+ T extends BlockTag = BlockTag,
13
+ K extends FieldLifeCycleLiterals<T> = FieldLifeCycleLiterals<T>,
14
+ > = FieldLifeCycleEvents<T>[K];
15
+
16
+ export type LifeCycleCallback<
17
+ T extends BlockTag = BlockTag,
18
+ A extends FieldType = FieldType,
19
+ K extends FieldLifeCycleLiterals<T> = FieldLifeCycleLiterals<T>,
20
+ > = (engine: Engine, field: Field<A>, event: FnParams<NativeCallback<T, K>>) => void;
21
+
22
+ export type FnParams<T extends ((event: never) => void) | undefined> = T extends (
23
+ event: infer R,
24
+ ) => void
25
+ ? R
26
+ : never;
27
+
28
+ export type FieldLifeCycleEvProps<T extends BlockTag = BlockTag> = {
29
+ target: BlockId;
30
+ fieldType: BlockType;
31
+ type: FieldLifeCycleLiterals<T>;
32
+ cycleEvent: FnParams<NativeCallback<T, FieldLifeCycleLiterals<T>>>;
33
+ };
@@ -0,0 +1,26 @@
1
+ import type { BlockId, BlockRect } from "@coorax/core";
2
+
3
+ export function blockRectFromBlockId(id: BlockId): BlockRect | null {
4
+ const domElement = document.getElementById(id);
5
+ if (!domElement) return null;
6
+
7
+ const parentElement = domElement.closest("#corax_flow_blocks_container");
8
+ if (!parentElement) return null;
9
+
10
+ const rect = domElement.getBoundingClientRect();
11
+
12
+ return {
13
+ top: rect.top,
14
+ left: rect.left,
15
+ width: rect.width,
16
+ right: rect.right,
17
+ height: rect.height,
18
+ bottom: rect.bottom,
19
+ offsetTop: domElement.offsetTop,
20
+ offsetLeft: domElement.offsetLeft,
21
+ offsetHeight: domElement.offsetHeight,
22
+ offsetWidth: domElement.offsetWidth,
23
+ parentScrollLeft: parentElement.scrollLeft,
24
+ parentScrollTop: parentElement.scrollTop,
25
+ };
26
+ }
@@ -0,0 +1,79 @@
1
+ import type { Block, BlockId, BlockRect, BlockTag, Engine } from "@coorax/core";
2
+ import type { FnParams, LifeCycleCallback, NativeCallback } from "../lifecycle.fields.types";
3
+ import { getFieldTypeAndTargetId } from "../lifecycle.fields";
4
+ import { blockRectFromBlockId } from "../lifecycle.utils";
5
+
6
+ export const createOnClick = (async <K extends BlockTag = BlockTag>(
7
+ engine: Engine,
8
+ block: Block,
9
+ ev: FnParams<NativeCallback<K, "onClick">>,
10
+ ) => {
11
+ ev.stopPropagation();
12
+
13
+ const fieldInfo = getFieldTypeAndTargetId(engine, ev, block);
14
+ if (!fieldInfo) return;
15
+
16
+ const isOnControl = ev.ctrlKey || ev.metaKey;
17
+
18
+ const selection = engine.state.get("selection");
19
+ if (!selection) return;
20
+
21
+ const keys = Array.from(selection.blocks.keys());
22
+
23
+ const blockRect = blockRectFromBlockId(block.id);
24
+ if (!blockRect) return;
25
+
26
+ if (!ev.shiftKey || keys.includes(block.id)) {
27
+ const blocks = new Map<BlockId, BlockRect>();
28
+ blocks.set(block.id, blockRect);
29
+
30
+ engine.commands.tryExecute("blocks.select", {
31
+ blocks,
32
+ mode: isOnControl ? "toggle" : "replace",
33
+ });
34
+ } else {
35
+ const treeOrder = [...engine.document.get().order];
36
+ const currentBlockIndex = treeOrder.indexOf(block.id);
37
+
38
+ const orderMap = new Map(treeOrder.map((id, index) => [id, index]));
39
+
40
+ const { minIndexKey, maxIndexKey } = keys.reduce(
41
+ (acc, key) => {
42
+ const currPos = orderMap.get(key) ?? Infinity;
43
+ const minPos = orderMap.get(acc.minIndexKey) ?? Infinity;
44
+ const maxPos = orderMap.get(acc.maxIndexKey) ?? -Infinity;
45
+ return {
46
+ minIndexKey: currPos < minPos ? key : acc.minIndexKey,
47
+ maxIndexKey: currPos > maxPos ? key : acc.maxIndexKey,
48
+ };
49
+ },
50
+ { minIndexKey: keys[0], maxIndexKey: keys[0] },
51
+ );
52
+
53
+ const lowestIndex = treeOrder.indexOf(minIndexKey);
54
+ const greatestIndex = treeOrder.indexOf(maxIndexKey);
55
+
56
+ const toSelect = (() => {
57
+ if (currentBlockIndex > lowestIndex) {
58
+ return treeOrder.slice(lowestIndex, currentBlockIndex + 1);
59
+ }
60
+
61
+ return treeOrder.slice(currentBlockIndex, greatestIndex + 1);
62
+ })();
63
+
64
+ const blocks = new Map<BlockId, BlockRect>();
65
+ for (const id of toSelect) {
66
+ const rect = blockRectFromBlockId(id);
67
+ if (rect) {
68
+ blocks.set(id, rect);
69
+ }
70
+ }
71
+
72
+ engine.commands.tryExecute("blocks.select", {
73
+ blocks,
74
+ mode: "replace",
75
+ });
76
+ }
77
+
78
+ await fieldInfo.emit();
79
+ }) satisfies LifeCycleCallback<any, any, "onClick">;
@@ -0,0 +1,27 @@
1
+ import type { BlockTag, Block } from "@coorax/core";
2
+ import { Engine } from "@coorax/core";
3
+ import { getFieldTypeAndTargetId } from "../lifecycle.fields";
4
+ import type { FnParams, NativeCallback, LifeCycleCallback } from "../lifecycle.fields.types";
5
+ import { blockRectFromBlockId } from "../lifecycle.utils";
6
+
7
+ export const createOnDoubleClick = (async <K extends BlockTag = BlockTag>(
8
+ engine: Engine,
9
+ block: Block,
10
+ ev: FnParams<NativeCallback<K, "onDoubleClick">>,
11
+ ) => {
12
+ ev.stopPropagation();
13
+ const fieldInfo = getFieldTypeAndTargetId(engine, ev, block);
14
+ if (!fieldInfo) return;
15
+
16
+ const rect = blockRectFromBlockId(block.id);
17
+ if (!rect) return;
18
+
19
+ const focus = engine.state.get("focus");
20
+ if (focus?.id === block.id) {
21
+ return;
22
+ }
23
+ engine.commands.tryExecute("blocks.focus", { id: block.id, rect });
24
+ // focusDomElement(block.id)
25
+
26
+ await fieldInfo.emit();
27
+ }) satisfies LifeCycleCallback<any, any, "onDoubleClick">;
@@ -0,0 +1,27 @@
1
+ import type { Block, BlockTag, Engine } from "@coorax/core";
2
+ import { getFieldTypeAndTargetId } from "../lifecycle.fields";
3
+ import type { FnParams, NativeCallback, LifeCycleCallback } from "../lifecycle.fields.types";
4
+ import { blockRectFromBlockId } from "../lifecycle.utils";
5
+
6
+ export const createOnMouseEnter = (async <K extends BlockTag = BlockTag>(
7
+ engine: Engine,
8
+ block: Block,
9
+ ev: FnParams<NativeCallback<K, "onMouseEnter">>,
10
+ ) => {
11
+ const fieldInfo = getFieldTypeAndTargetId(engine, ev, block);
12
+ if (!fieldInfo) return;
13
+
14
+ const selection = engine.state.get("selection");
15
+ if (selection && selection.blocks.has(block.id)) {
16
+ return;
17
+ }
18
+
19
+ const rect = blockRectFromBlockId(block.id);
20
+ if (!rect) return;
21
+
22
+ // console.log("hovered");
23
+
24
+ engine.commands.tryExecute("blocks.hover", { id: block.id, rect });
25
+
26
+ await fieldInfo.emit();
27
+ }) satisfies LifeCycleCallback<any, any, "onMouseEnter">;
@@ -0,0 +1,21 @@
1
+ import type { Block, BlockTag, Engine } from "@coorax/core";
2
+ import { getFieldTypeAndTargetId } from "../lifecycle.fields";
3
+ import type { FnParams, NativeCallback, LifeCycleCallback } from "../lifecycle.fields.types";
4
+
5
+ export const createOnMouseLeave = (async <K extends BlockTag = BlockTag>(
6
+ engine: Engine,
7
+ block: Block,
8
+ ev: FnParams<NativeCallback<K, "onMouseLeave">>,
9
+ ) => {
10
+ const fieldInfo = getFieldTypeAndTargetId(engine, ev, block);
11
+ if (!fieldInfo) return;
12
+
13
+ const state = engine.state.get("hover");
14
+ if (!state) return;
15
+
16
+ if (state === null) return;
17
+
18
+ engine.commands.tryExecute("blocks.hover", null);
19
+
20
+ await fieldInfo.emit();
21
+ }) satisfies LifeCycleCallback<any, any, "onMouseLeave">;
@@ -0,0 +1,16 @@
1
+ import type { Block, BlockTag, Engine } from "@coorax/core";
2
+ import { getFieldTypeAndTargetId } from "../lifecycle.fields";
3
+ import type { FnParams, NativeCallback, LifeCycleCallback } from "../lifecycle.fields.types";
4
+
5
+ export const createOnLoad = (async <K extends BlockTag = BlockTag>(
6
+ engine: Engine,
7
+ block: Block,
8
+ ev: FnParams<NativeCallback<K, "onLoad">>,
9
+ ) => {
10
+ const fieldInfo = getFieldTypeAndTargetId(engine, ev, block);
11
+ if (!fieldInfo) return;
12
+
13
+ console.log("Loaded");
14
+
15
+ await fieldInfo.emit();
16
+ }) satisfies LifeCycleCallback<any, any, "onLoad">;
@@ -0,0 +1,57 @@
1
+ import type {
2
+ Block,
3
+ BlockTag,
4
+ Engine,
5
+ FieldId,
6
+ FieldTextContentPath,
7
+ // SelectionRect,
8
+ } from "@coorax/core";
9
+ import { getFieldTypeAndTargetId } from "../lifecycle.fields";
10
+ import type { FnParams, NativeCallback, LifeCycleCallback } from "../lifecycle.fields.types";
11
+ import { rangeToOffsets } from "../../../lib/selection.utils";
12
+
13
+ export const createOnSelect = (async <K extends BlockTag = BlockTag>(
14
+ engine: Engine,
15
+ block: Block,
16
+ ev: FnParams<NativeCallback<K, "onSelect">>,
17
+ ) => {
18
+ const fieldInfo = getFieldTypeAndTargetId(engine, ev, block);
19
+ if (!fieldInfo) return;
20
+
21
+ const target = ev.currentTarget as HTMLTextAreaElement;
22
+ if (!target) return;
23
+
24
+ const documentSel = document.getSelection();
25
+ if (!documentSel) return;
26
+
27
+ const range = documentSel.getRangeAt(0);
28
+ const el = range.commonAncestorContainer;
29
+ const root = (el instanceof Element ? el : el.parentElement)?.closest<HTMLElement>(
30
+ "[data-text-path]",
31
+ );
32
+ if (!root) return; // hors d'une zone de texte riche
33
+
34
+ const blockId = root.getAttribute("id") as FieldId;
35
+ const path = root.dataset.textPath! as FieldTextContentPath; // "props.children", "props.label"...
36
+ const { start, end } = rangeToOffsets(root, range);
37
+
38
+ // if (start === end) {
39
+ // const curSel = engine.state.get("textSelection");
40
+ // if (curSel) {
41
+ // engine.commands.tryExecute("text.setSelection", null);
42
+ // }
43
+ // return;
44
+ // }
45
+ engine.commands.tryExecute("text.setSelection", { blockId, path, start, end });
46
+
47
+ const sel = {
48
+ blockId: block.id as FieldId,
49
+ path: "props.children" as FieldTextContentPath,
50
+ start: Math.min(start, end),
51
+ end: Math.max(start, end),
52
+ };
53
+
54
+ engine.commands.tryExecute("text.setSelection", sel);
55
+
56
+ await fieldInfo.emit();
57
+ }) satisfies LifeCycleCallback<any, any, "onSelect">;
@@ -0,0 +1 @@
1
+ console.log("Hello");