@coorax/core 0.0.0-canary.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (78) hide show
  1. package/package.json +40 -0
  2. package/src/base/classes/component.class.ts +68 -0
  3. package/src/base/classes/container.class.ts +57 -0
  4. package/src/base/classes/field.class.ts +42 -0
  5. package/src/base/classes/tnode.class.ts +38 -0
  6. package/src/base/index.ts +23 -0
  7. package/src/base/types/components/components.ts +21 -0
  8. package/src/base/types/container/container.ts +37 -0
  9. package/src/base/types/container/division.types.ts +3 -0
  10. package/src/base/types/container/section.types.ts +3 -0
  11. package/src/base/types/container/ul-list.types.ts +3 -0
  12. package/src/base/types/css.types.ts +33 -0
  13. package/src/base/types/field/divider.types.ts +3 -0
  14. package/src/base/types/field/field.media.types.ts +6 -0
  15. package/src/base/types/field/field.text.types.ts +9 -0
  16. package/src/base/types/field/field.ts +46 -0
  17. package/src/base/types/field/heading.types.ts +3 -0
  18. package/src/base/types/field/paragraph.types.ts +3 -0
  19. package/src/base/types/global.types.ts +45 -0
  20. package/src/base/types/node/link.types.ts +3 -0
  21. package/src/base/types/node/node.ts +28 -0
  22. package/src/base/types/node/span.types.ts +3 -0
  23. package/src/base/types/tree/tree.ts +33 -0
  24. package/src/engine/engine.test.ts +27 -0
  25. package/src/engine/engine.ts +38 -0
  26. package/src/engine/index.ts +1 -0
  27. package/src/index.ts +13 -0
  28. package/src/modules/editor/editor.module.ts +119 -0
  29. package/src/modules/focus/focus.module.ts +185 -0
  30. package/src/modules/history/history.module.ts +60 -0
  31. package/src/modules/hover/hover.module.ts +121 -0
  32. package/src/modules/index.ts +10 -0
  33. package/src/modules/insertion/insertion.module.ts +140 -0
  34. package/src/modules/modules-manager.ts +132 -0
  35. package/src/modules/modules.types.ts +47 -0
  36. package/src/modules/selection/selection.module.ts +174 -0
  37. package/src/modules/selection/selection.test.ts +1 -0
  38. package/src/modules/text-editing/text-editing.constants.ts +6 -0
  39. package/src/modules/text-editing/text-editing.module.ts +377 -0
  40. package/src/modules/text-editing/text-editing.utils.ts +27 -0
  41. package/src/services/commandsbus/commandsbus.test.ts +312 -0
  42. package/src/services/commandsbus/commandsbus.ts +360 -0
  43. package/src/services/commandsbus/index.ts +1 -0
  44. package/src/services/document/document.ts +139 -0
  45. package/src/services/document/index.ts +1 -0
  46. package/src/services/eventbus/eventbus.test.ts +144 -0
  47. package/src/services/eventbus/eventbus.ts +162 -0
  48. package/src/services/eventbus/index.ts +2 -0
  49. package/src/services/eventbus/subscriber.ts +46 -0
  50. package/src/services/mutations/index.ts +2 -0
  51. package/src/services/mutations/mutations.errors.ts +12 -0
  52. package/src/services/mutations/mutations.test.ts +177 -0
  53. package/src/services/mutations/mutations.ts +457 -0
  54. package/src/services/shortcuts/shortcuts.ts +3 -0
  55. package/src/services/state/index.ts +2 -0
  56. package/src/services/state/state.errors.ts +12 -0
  57. package/src/services/state/state.ts +64 -0
  58. package/src/services/tasks/index.ts +1 -0
  59. package/src/services/tasks/tasks.service.test.ts +193 -0
  60. package/src/services/tasks/tasks.service.ts +288 -0
  61. package/src/services/text-marks/index.ts +2 -0
  62. package/src/services/text-marks/marks.types.ts +38 -0
  63. package/src/services/text-marks/text-marks.ts +244 -0
  64. package/src/services/ui/default-styles.ts +93 -0
  65. package/src/services/ui/index.ts +3 -0
  66. package/src/services/ui/ui.errors.ts +6 -0
  67. package/src/services/ui/ui.ts +148 -0
  68. package/src/services/ui/ui.types.ts +197 -0
  69. package/src/utils/components.utils.ts +26 -0
  70. package/src/utils/container.utils.ts +59 -0
  71. package/src/utils/fields.utils.ts +118 -0
  72. package/src/utils/global.utils.ts +128 -0
  73. package/src/utils/index.ts +7 -0
  74. package/src/utils/modules.utils.ts +110 -0
  75. package/src/utils/tnode.utils.ts +48 -0
  76. package/src/utils/tree.utils.ts +120 -0
  77. package/styles.css +178 -0
  78. package/tsconfig.json +9 -0
@@ -0,0 +1,119 @@
1
+ import type { BlockRect, TModule } from "..";
2
+ import type { CommandMap } from "../../services/commandsbus/commandsbus";
3
+ import { areObjectsTheSame } from "../../utils";
4
+
5
+ export type EditorRect = Omit<BlockRect, "parentScrollTop" | "parentScrollLeft"> & {
6
+ clientTop: number;
7
+ clientLeft: number;
8
+ scrollTop: number;
9
+ scrollLeft: number;
10
+ };
11
+
12
+ export type OverlayPosition = {
13
+ top: number;
14
+ left: number;
15
+ width: number;
16
+ height: number;
17
+ };
18
+
19
+ const MIN_SIZE = 16;
20
+
21
+ /** Viewport block rects → `position: absolute` inside the scroll container. */
22
+ export function overlayPositionFromBlockRects(
23
+ rects: readonly BlockRect[],
24
+ editorRect: EditorRect | null,
25
+ ): OverlayPosition | null {
26
+ if (!editorRect || rects.length === 0) return null;
27
+
28
+ const top = Math.min(...rects.map((rect) => rect.top));
29
+ const left = Math.min(...rects.map((rect) => rect.left));
30
+ const bottom = Math.max(...rects.map((rect) => rect.bottom));
31
+ const right = Math.max(...rects.map((rect) => rect.right));
32
+
33
+ const scrollTop = rects[0]!.parentScrollTop;
34
+ const scrollLeft = rects[0]!.parentScrollLeft;
35
+
36
+ const width = right - left;
37
+ const height = bottom - top;
38
+
39
+ return {
40
+ top:
41
+ top -
42
+ editorRect.top -
43
+ editorRect.clientTop +
44
+ scrollTop -
45
+ (height < MIN_SIZE ? MIN_SIZE / 2 : 0),
46
+ left:
47
+ left -
48
+ editorRect.left -
49
+ editorRect.clientLeft +
50
+ scrollLeft -
51
+ (width < MIN_SIZE ? MIN_SIZE / 2 : 0),
52
+ width: width < MIN_SIZE ? MIN_SIZE : width,
53
+ height: height < MIN_SIZE ? MIN_SIZE : height,
54
+ };
55
+ }
56
+
57
+ declare module "../modules-manager" {
58
+ interface ModuleMap {
59
+ editor: {};
60
+ }
61
+ }
62
+
63
+ declare module "../../services/state/state" {
64
+ interface StateMap {
65
+ editor: { editorRect: EditorRect | null };
66
+ }
67
+ }
68
+
69
+ declare module "../../services/commandsbus/commandsbus" {
70
+ interface CommandMap {
71
+ "editor.rect": {
72
+ payload: { editorRect: EditorRect };
73
+ result: void;
74
+ };
75
+ }
76
+ }
77
+
78
+ declare module "../../services/eventbus/eventbus" {
79
+ interface TEventMap {
80
+ "editor:rect": CommandMap["editor.rect"]["payload"];
81
+ }
82
+ }
83
+
84
+ export const EditorModule: TModule = {
85
+ name: "editor",
86
+ version: "1.0.0",
87
+ requires: [],
88
+ install(ctx) {
89
+ ctx.state.defineSlice("editor", { editorRect: null });
90
+
91
+ ctx.commands.register("editor.rect", ({ editorRect }) => {
92
+ const prevState = ctx.state.get("editor");
93
+
94
+ if (editorRect === null) {
95
+ if (prevState?.editorRect === null) return;
96
+
97
+ ctx.state.set("editor", { editorRect });
98
+ ctx.events.emit("editor:rect", { editorRect });
99
+ return;
100
+ }
101
+
102
+ if (!prevState) {
103
+ return;
104
+ }
105
+
106
+ if (prevState.editorRect === null) {
107
+ ctx.state.set("editor", { editorRect });
108
+ ctx.events.emit("editor:rect", { editorRect });
109
+ return;
110
+ }
111
+
112
+ const areSame = areObjectsTheSame(prevState.editorRect, editorRect);
113
+ if (!areSame) {
114
+ ctx.state.set("editor", { editorRect });
115
+ ctx.events.emit("editor:rect", { editorRect });
116
+ }
117
+ });
118
+ },
119
+ };
@@ -0,0 +1,185 @@
1
+ import type { BlockId, FieldId, TextField } from "../../base";
2
+ import type { CommandMap } from "../../services/commandsbus";
3
+ import type { StateMap } from "../../services/state";
4
+ import type { BlockRect, TModule } from "../modules.types";
5
+ import type { UIContext } from "../../services/ui";
6
+ import type { SlotComponentMap } from "../../services/ui";
7
+ import { overlayPositionFromBlockRects, type EditorRect } from "../editor/editor.module";
8
+ import _ from "lodash";
9
+ import { isEditableField } from "../../utils";
10
+
11
+ declare module "../modules-manager" {
12
+ interface ModuleMap {
13
+ focus: {};
14
+ }
15
+ }
16
+
17
+ declare module "../../services/state/state" {
18
+ interface StateMap {
19
+ focus: { id: BlockId; rect: BlockRect } | null;
20
+ focus_memory: {
21
+ blocks: { id: BlockId; rect: BlockRect }[];
22
+ count: number;
23
+ };
24
+ }
25
+ }
26
+
27
+ declare module "../../services/commandsbus/commandsbus" {
28
+ interface CommandMap {
29
+ "blocks.focus": { payload: StateMap["focus"]; result: void };
30
+ }
31
+ }
32
+
33
+ declare module "../../services/eventbus/eventbus" {
34
+ interface TEventMap {
35
+ "blocks:focus": CommandMap["blocks.focus"]["payload"];
36
+ }
37
+ }
38
+
39
+ declare module "../../services/ui/ui" {
40
+ interface SlotComponentMap {
41
+ FocusOverlay: {
42
+ position: {
43
+ top: number;
44
+ left: number;
45
+ width: number;
46
+ height: number;
47
+ } | null;
48
+ hidden: boolean;
49
+ };
50
+ }
51
+
52
+ interface ComponentContributionIdentityMap {
53
+ "focus-overlay": {};
54
+ }
55
+ }
56
+
57
+ function focusOverlayPropsFromUICtx(
58
+ ui: UIContext,
59
+ editorRect: EditorRect | null,
60
+ ): SlotComponentMap["FocusOverlay"]["position"] | null {
61
+ const rect = ui.focus?.rect;
62
+ if (!rect) return null;
63
+
64
+ return overlayPositionFromBlockRects([rect], editorRect);
65
+ }
66
+
67
+ export const FocusModule: TModule = {
68
+ name: "focus",
69
+ version: "1.0.0",
70
+ requires: ["selection"],
71
+ install(ctx) {
72
+ ctx.state.defineSlice("focus", null);
73
+ ctx.state.defineSlice("focus_memory", { blocks: [], count: 3 });
74
+
75
+ ctx.ui.contribute("overlay", {
76
+ id: "focus-overlay",
77
+ slot: "overlay",
78
+ order: 1001,
79
+ kind: "component",
80
+ name: "FocusOverlay",
81
+ when: (ui) => !!ui.focus?.id,
82
+ getProps: (ui) => {
83
+ const editorState = ctx.state.get("editor") ?? { editorRect: null };
84
+ const position = focusOverlayPropsFromUICtx(ui, editorState?.editorRect);
85
+ const document = ctx.state.get("document");
86
+
87
+ return {
88
+ position,
89
+ hidden: !ui.focus?.id || !position || !document?.order.has(ui?.focus?.id),
90
+ };
91
+ },
92
+ });
93
+
94
+ ctx.commands.register("blocks.focus", (payload) => {
95
+ // if (!payload) {
96
+ // void ctx.state.set("focus", null);
97
+ // void ctx.commands.execute("blocks.select", { blocks: new Map(), mode: "replace" });
98
+ // return;
99
+ // }
100
+
101
+ if (payload && !isEditableField(ctx.document.getBlock(payload.id as BlockId)!)) {
102
+ void ctx.commands.execute("blocks.select", {
103
+ blocks: new Map().set(payload.id, payload.rect),
104
+ mode: "replace",
105
+ });
106
+ return;
107
+ }
108
+
109
+ const { id: focusedId = null, rect = null } = payload ?? {};
110
+ const prev = ctx.state.get("focus");
111
+
112
+ if (focusedId !== null && !ctx.document.has(focusedId)) return;
113
+ if (focusedId === prev?.id && _.isEqual(rect, prev.rect)) return;
114
+
115
+ const prevFocusedId = prev?.id;
116
+ const shouldUnfocusPrev = Boolean(
117
+ prevFocusedId &&
118
+ prevFocusedId !== focusedId &&
119
+ (() => {
120
+ const b = ctx.document.getBlock(prevFocusedId);
121
+ return b && isEditableField(b);
122
+ })(),
123
+ );
124
+
125
+ const shouldFocusCurrent = Boolean(
126
+ focusedId &&
127
+ (() => {
128
+ const b = ctx.document.getBlock(focusedId);
129
+ return b && isEditableField(b);
130
+ })(),
131
+ );
132
+
133
+ if (shouldUnfocusPrev || shouldFocusCurrent) {
134
+ ctx.document.apply(
135
+ (tree) => {
136
+ let nextTree = tree;
137
+ if (shouldUnfocusPrev && prevFocusedId) {
138
+ nextTree = ctx.mutations.mutateField<TextField>(nextTree, {
139
+ keyValuePairs: {
140
+ "props.contentEditable": false,
141
+ },
142
+ id: prevFocusedId as FieldId,
143
+ });
144
+ }
145
+ if (shouldFocusCurrent && focusedId) {
146
+ nextTree = ctx.mutations.mutateField<TextField>(nextTree, {
147
+ keyValuePairs: {
148
+ "props.contentEditable": true,
149
+ },
150
+ id: focusedId as FieldId,
151
+ });
152
+ }
153
+ return nextTree;
154
+ },
155
+ { shouldEmitEvent: false },
156
+ );
157
+ }
158
+
159
+ void ctx.state.set("focus", payload);
160
+ const selectedBlocks = ctx.state.get("selection");
161
+
162
+ if (payload && !selectedBlocks?.blocks?.has(payload.id)) {
163
+ void ctx.commands.execute("blocks.select", {
164
+ blocks: new Map().set(payload.id, payload.rect),
165
+ mode: "replace",
166
+ });
167
+ } else if (!payload) {
168
+ void ctx.commands.execute("blocks.select", {
169
+ blocks: new Map(),
170
+ mode: "replace",
171
+ });
172
+ }
173
+
174
+ const { blocks: _memoBlocks, count } = ctx.state.get("focus_memory")!;
175
+ if (focusedId && rect) {
176
+ void ctx.state.set("focus_memory", {
177
+ blocks: [..._memoBlocks, { id: focusedId, rect }].slice(-count),
178
+ count,
179
+ });
180
+ }
181
+
182
+ void ctx.events.emit("blocks:focus", payload);
183
+ });
184
+ },
185
+ };
@@ -0,0 +1,60 @@
1
+ import type { CoraxTree } from "../../base";
2
+ import type { TModule } from "../modules.types";
3
+
4
+ declare module "../modules-manager" {
5
+ interface ModuleMap {
6
+ history: {};
7
+ }
8
+ }
9
+
10
+ declare module "../../services/state/state" {
11
+ interface StateMap {
12
+ history: { canUndo: boolean; canRedo: boolean };
13
+ }
14
+ }
15
+
16
+ declare module "../../services/commandsbus/commandsbus" {
17
+ interface CommandMap {
18
+ "history.undo": { payload: {}; result: void };
19
+ "history.redo": { payload: {}; result: void };
20
+ }
21
+ }
22
+
23
+ export const HistoryModule: TModule = {
24
+ name: "history",
25
+ version: "1.0.0",
26
+ requires: ["focus"],
27
+ install(ctx) {
28
+ const MAX = 100;
29
+ //#Use Set to store the stack
30
+ let stack: CoraxTree[] = [ctx.document.get()];
31
+ let cursor = 0;
32
+
33
+ ctx.state.defineSlice("history", { canUndo: false, canRedo: false });
34
+ const sync = () =>
35
+ ctx.state.set("history", {
36
+ canUndo: cursor > 0,
37
+ canRedo: cursor < stack.length - 1,
38
+ });
39
+
40
+ ctx.events.on("document:changed", ({ value }) => {
41
+ if (value.origin === "history") return;
42
+ stack = [...stack.slice(0, cursor + 1), value.next].slice(-MAX); // coupe la branche redo
43
+ cursor = stack.length - 1;
44
+ sync();
45
+ // console.log("Snapshot: ", value.next.version);
46
+ });
47
+
48
+ const go = (to: number) => {
49
+ if (to < 0 || to >= stack.length) return;
50
+ cursor = to;
51
+ ctx.document.apply(() => stack[cursor], { origin: "history" });
52
+ sync();
53
+
54
+ if (!ctx.state.get("focus")) void ctx.commands.tryExecute("blocks.focus", null);
55
+ };
56
+
57
+ ctx.commands.register("history.undo", () => go(cursor - 1));
58
+ ctx.commands.register("history.redo", () => go(cursor + 1));
59
+ },
60
+ };
@@ -0,0 +1,121 @@
1
+ import type { BlockId } from "../../base";
2
+ import type { SliceValue, StateMap } from "../../services/state";
3
+ import type { BlockRect, TModule } from "../modules.types";
4
+ import type { UIContext } from "../../services/ui";
5
+ import type { SlotComponentMap } from "../../services/ui";
6
+ import { overlayPositionFromBlockRects, type EditorRect } from "../editor/editor.module";
7
+
8
+ declare module "../modules-manager" {
9
+ interface ModuleMap {
10
+ hover: {};
11
+ }
12
+ }
13
+
14
+ declare module "../../services/state/state" {
15
+ interface StateMap {
16
+ hover: { id: BlockId; rect: BlockRect } | null;
17
+ hover_memory: { blocks: { id: BlockId; rect: BlockRect }[]; count: number };
18
+ }
19
+ }
20
+
21
+ declare module "../../services/commandsbus/commandsbus" {
22
+ interface CommandMap {
23
+ "blocks.hover": { payload: StateMap["hover"]; result: void };
24
+ }
25
+ }
26
+
27
+ declare module "../../services/eventbus/eventbus" {
28
+ interface TEventMap {
29
+ "blocks:hover": StateMap["hover"];
30
+ }
31
+ }
32
+
33
+ declare module "../../services/ui/ui" {
34
+ interface SlotComponentMap {
35
+ HoverOverlay: {
36
+ position: {
37
+ top: number;
38
+ left: number;
39
+ width: number;
40
+ height: number;
41
+ } | null;
42
+ hidden: boolean;
43
+ };
44
+ }
45
+
46
+ interface ComponentContributionIdentityMap {
47
+ "hover-overlay": {};
48
+ }
49
+ }
50
+
51
+ function hoverOverlayPropsFromUICtx(
52
+ ui: UIContext,
53
+ editorRect: EditorRect | null,
54
+ ): SlotComponentMap["HoverOverlay"]["position"] | null {
55
+ const rect = ui.hover?.rect;
56
+ if (!rect) return null;
57
+
58
+ return overlayPositionFromBlockRects([rect], editorRect);
59
+ }
60
+
61
+ export const HoverModule: TModule = {
62
+ name: "hover",
63
+ version: "1.0.0",
64
+ requires: [],
65
+ install(ctx) {
66
+ ctx.state.defineSlice("hover", null);
67
+ ctx.state.defineSlice("hover_memory", { blocks: [], count: 3 });
68
+
69
+ ctx.ui.contribute("overlay", {
70
+ id: "hover-overlay",
71
+ slot: "overlay",
72
+ order: 999,
73
+ kind: "component",
74
+ name: "HoverOverlay",
75
+ when: (ui) => !!ui.hover?.id,
76
+ getProps: (ui) => {
77
+ const editorState = ctx.state.get("editor") ?? { editorRect: null };
78
+ const selection = ctx.state.get("selection");
79
+ const document = ctx.state.get("document");
80
+
81
+ const position = hoverOverlayPropsFromUICtx(ui, editorState?.editorRect);
82
+
83
+ return {
84
+ position,
85
+ hidden:
86
+ !ui.hover?.id ||
87
+ !position ||
88
+ (!!selection && selection.blocks.has(ui.hover?.id)) ||
89
+ !document?.order.has(ui.hover?.id),
90
+ };
91
+ },
92
+ });
93
+
94
+ ctx.commands.register("blocks.hover", (payload) => {
95
+ if (!payload) {
96
+ void ctx.state.set("hover", null);
97
+ void ctx.events.emit("blocks:hover", null);
98
+ return;
99
+ }
100
+
101
+ const { id, rect } = payload;
102
+ const prev = ctx.state.get("hover");
103
+
104
+ if (id !== null && !ctx.document.has(id)) return;
105
+ if (id === prev?.id) return;
106
+
107
+ void ctx.state.set("hover", payload);
108
+ const { blocks: _memoBlocks, count } = ctx.state.get(
109
+ "hover_memory",
110
+ ) as SliceValue<"hover_memory">;
111
+ if (id && rect) {
112
+ void ctx.state.set("hover_memory", {
113
+ blocks: [..._memoBlocks, { id, rect }].slice(-count),
114
+ count,
115
+ });
116
+ }
117
+
118
+ void ctx.events.emit("blocks:hover", payload);
119
+ });
120
+ },
121
+ };
@@ -0,0 +1,10 @@
1
+ export * from "./insertion/insertion.module";
2
+ export * from "./selection/selection.module";
3
+ export * from "./focus/focus.module";
4
+ export * from "./modules.types";
5
+ export * from "./modules-manager";
6
+ export * from "./hover/hover.module";
7
+ export * from "./history/history.module";
8
+ export * from "./text-editing/text-editing.module";
9
+ export * from "./text-editing/text-editing.utils";
10
+ export * from "./editor/editor.module";
@@ -0,0 +1,140 @@
1
+ import type { FieldId } from "../../base";
2
+ import type { Block, BlockId } from "../../base/types/tree/tree";
3
+ import type { CommandMap } from "../../services/commandsbus/commandsbus";
4
+ import type { Contribution, ContributionId, SlotIcon } from "../../services/ui/ui.types";
5
+ import { FieldsUtils } from "../../utils/fields.utils";
6
+ import type { TModule } from "../modules.types";
7
+
8
+ declare module "../../services/commandsbus/commandsbus" {
9
+ interface CommandMap {
10
+ "blocks.insert": { payload: { block: Block; pos?: number }; result: void };
11
+ "blocks.delete": { payload: { blockId: BlockId }; result: void };
12
+ "actions.toggle": { payload: boolean; result: void };
13
+ }
14
+ }
15
+
16
+ declare module "../../services/eventbus/eventbus" {
17
+ interface TEventMap {
18
+ "blocks:insert": CommandMap["blocks.insert"]["payload"];
19
+ "blocks:delete": CommandMap["blocks.delete"]["payload"];
20
+ "actions:toggle": CommandMap["actions.toggle"]["payload"];
21
+ }
22
+ }
23
+
24
+ declare module "../../services/state/state" {
25
+ interface StateMap {
26
+ insertion: boolean;
27
+ }
28
+ }
29
+
30
+ declare module "../modules-manager" {
31
+ interface ModuleMap {
32
+ insertion: {};
33
+ }
34
+ }
35
+
36
+ declare module "../../services/ui/ui" {
37
+ interface ButtonContributionIdentityMap {
38
+ "insert-field": {};
39
+ "insert-heading": {};
40
+ "insert-text": {};
41
+ "insert-image": {};
42
+ "insert-divider": {};
43
+ }
44
+
45
+ interface IconMap {
46
+ DividerIcon: {};
47
+ }
48
+ }
49
+
50
+ const ActionsOpenerContribution: Contribution<"block-actions", "actions.toggle", "button"> = {
51
+ id: "insert-field",
52
+ slot: "block-actions",
53
+ order: 0,
54
+ kind: "button",
55
+ command: "actions.toggle",
56
+ icon: "PlusIcon",
57
+ getPayload: () => true,
58
+ name: "BlockActionButton",
59
+ getProps() {
60
+ // ui.translate("Insert block")
61
+ return { variant: "secondary", size: "icon-lg", isBaseButton: true };
62
+ },
63
+ when() {
64
+ return true;
65
+ },
66
+ };
67
+
68
+ export const InsertionModule: TModule = {
69
+ name: "insertion",
70
+ version: "1.0.0",
71
+ requires: [],
72
+ install(ctx) {
73
+ ctx.state.defineSlice("insertion", false);
74
+
75
+ ctx.commands.register("actions.toggle", (isOpen) => {
76
+ ctx.state.set("insertion", isOpen);
77
+ ctx.events.emit("actions:toggle", isOpen);
78
+ });
79
+
80
+ ctx.commands.register("blocks.insert", ({ block, pos }) => {
81
+ const isNotMedia = block.type !== "image" && block.type !== "video";
82
+ const hasSrcInProps = "src" in block.props && !!block.props.src.trim();
83
+
84
+ const shouldEmitEvent = isNotMedia || hasSrcInProps;
85
+
86
+ ctx.document.apply(
87
+ (tree) => {
88
+ // #TODO handle others block
89
+ if (block.variant === "field") {
90
+ return ctx.mutations.insertField(tree, block, pos);
91
+ }
92
+ return tree;
93
+ },
94
+ { shouldEmitEvent },
95
+ );
96
+
97
+ ctx.state.set("insertion", false);
98
+ ctx.events.emit("blocks:insert", { block, pos });
99
+ });
100
+
101
+ ctx.commands.register("blocks.delete", ({ blockId }) => {
102
+ const isFieldBlock = blockId.startsWith("field:");
103
+ if (!isFieldBlock) return;
104
+
105
+ ctx.document.apply((tree) => ctx.mutations.deleteField(tree, blockId as FieldId));
106
+
107
+ ctx.events.emit("blocks:delete", { blockId });
108
+ });
109
+
110
+ function actionButton(id: ContributionId, block: Block, icon: SlotIcon, order: number) {
111
+ ctx.ui.contribute("block-actions", {
112
+ id,
113
+ order,
114
+ icon,
115
+ kind: "button",
116
+ slot: "block-actions",
117
+ command: "blocks.insert",
118
+ name: "BlockActionButton",
119
+ getProps() {
120
+ // ui.translate("Insert block")
121
+ return { variant: "outline", size: "icon" };
122
+ },
123
+ getPayload: () => ({ block }),
124
+ when() {
125
+ return true;
126
+ },
127
+ });
128
+ }
129
+
130
+ ctx.ui.contribute("block-actions", {
131
+ ...ActionsOpenerContribution,
132
+ getPayload: () => !ctx.state.get("insertion"),
133
+ });
134
+
135
+ actionButton("insert-heading", FieldsUtils.NewHeadingField(), "HeadingIcon", 1);
136
+ actionButton("insert-text", FieldsUtils.NewTextField(), "TextIcon", 2);
137
+ actionButton("insert-image", FieldsUtils.NewImageField(), "ImagePlus", 3);
138
+ actionButton("insert-divider", FieldsUtils.NewDividerField(), "DividerIcon", 4);
139
+ },
140
+ };