@coorax/core 0.0.0-canary.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +40 -0
- package/src/base/classes/component.class.ts +68 -0
- package/src/base/classes/container.class.ts +57 -0
- package/src/base/classes/field.class.ts +42 -0
- package/src/base/classes/tnode.class.ts +38 -0
- package/src/base/index.ts +23 -0
- package/src/base/types/components/components.ts +21 -0
- package/src/base/types/container/container.ts +37 -0
- package/src/base/types/container/division.types.ts +3 -0
- package/src/base/types/container/section.types.ts +3 -0
- package/src/base/types/container/ul-list.types.ts +3 -0
- package/src/base/types/css.types.ts +33 -0
- package/src/base/types/field/divider.types.ts +3 -0
- package/src/base/types/field/field.media.types.ts +6 -0
- package/src/base/types/field/field.text.types.ts +9 -0
- package/src/base/types/field/field.ts +46 -0
- package/src/base/types/field/heading.types.ts +3 -0
- package/src/base/types/field/paragraph.types.ts +3 -0
- package/src/base/types/global.types.ts +45 -0
- package/src/base/types/node/link.types.ts +3 -0
- package/src/base/types/node/node.ts +28 -0
- package/src/base/types/node/span.types.ts +3 -0
- package/src/base/types/tree/tree.ts +33 -0
- package/src/engine/engine.test.ts +27 -0
- package/src/engine/engine.ts +38 -0
- package/src/engine/index.ts +1 -0
- package/src/index.ts +13 -0
- package/src/modules/editor/editor.module.ts +119 -0
- package/src/modules/focus/focus.module.ts +185 -0
- package/src/modules/history/history.module.ts +60 -0
- package/src/modules/hover/hover.module.ts +121 -0
- package/src/modules/index.ts +10 -0
- package/src/modules/insertion/insertion.module.ts +140 -0
- package/src/modules/modules-manager.ts +132 -0
- package/src/modules/modules.types.ts +47 -0
- package/src/modules/selection/selection.module.ts +174 -0
- package/src/modules/selection/selection.test.ts +1 -0
- package/src/modules/text-editing/text-editing.constants.ts +6 -0
- package/src/modules/text-editing/text-editing.module.ts +377 -0
- package/src/modules/text-editing/text-editing.utils.ts +27 -0
- package/src/services/commandsbus/commandsbus.test.ts +312 -0
- package/src/services/commandsbus/commandsbus.ts +360 -0
- package/src/services/commandsbus/index.ts +1 -0
- package/src/services/document/document.ts +139 -0
- package/src/services/document/index.ts +1 -0
- package/src/services/eventbus/eventbus.test.ts +144 -0
- package/src/services/eventbus/eventbus.ts +162 -0
- package/src/services/eventbus/index.ts +2 -0
- package/src/services/eventbus/subscriber.ts +46 -0
- package/src/services/mutations/index.ts +2 -0
- package/src/services/mutations/mutations.errors.ts +12 -0
- package/src/services/mutations/mutations.test.ts +177 -0
- package/src/services/mutations/mutations.ts +457 -0
- package/src/services/shortcuts/shortcuts.ts +3 -0
- package/src/services/state/index.ts +2 -0
- package/src/services/state/state.errors.ts +12 -0
- package/src/services/state/state.ts +64 -0
- package/src/services/tasks/index.ts +1 -0
- package/src/services/tasks/tasks.service.test.ts +193 -0
- package/src/services/tasks/tasks.service.ts +288 -0
- package/src/services/text-marks/index.ts +2 -0
- package/src/services/text-marks/marks.types.ts +38 -0
- package/src/services/text-marks/text-marks.ts +244 -0
- package/src/services/ui/default-styles.ts +93 -0
- package/src/services/ui/index.ts +3 -0
- package/src/services/ui/ui.errors.ts +6 -0
- package/src/services/ui/ui.ts +148 -0
- package/src/services/ui/ui.types.ts +197 -0
- package/src/utils/components.utils.ts +26 -0
- package/src/utils/container.utils.ts +59 -0
- package/src/utils/fields.utils.ts +118 -0
- package/src/utils/global.utils.ts +128 -0
- package/src/utils/index.ts +7 -0
- package/src/utils/modules.utils.ts +110 -0
- package/src/utils/tnode.utils.ts +48 -0
- package/src/utils/tree.utils.ts +120 -0
- package/styles.css +178 -0
- package/tsconfig.json +9 -0
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
import type { Mark, MarkBehavior, MarkSpec, MarkType, TextContent } from "./marks.types";
|
|
2
|
+
|
|
3
|
+
export type TextMarksDisposer = () => void;
|
|
4
|
+
|
|
5
|
+
export interface TextMarksInterface {
|
|
6
|
+
replaceRange(
|
|
7
|
+
content: TextContent,
|
|
8
|
+
start: number,
|
|
9
|
+
end: number,
|
|
10
|
+
text: string,
|
|
11
|
+
opts?: { marks?: readonly MarkSpec[] },
|
|
12
|
+
): TextContent;
|
|
13
|
+
|
|
14
|
+
toggleMark(
|
|
15
|
+
content: TextContent,
|
|
16
|
+
{ start, end, spec }: { start: number; end: number; spec: MarkSpec },
|
|
17
|
+
): TextContent;
|
|
18
|
+
|
|
19
|
+
deleteRange(content: TextContent, start: number, end: number): TextContent;
|
|
20
|
+
|
|
21
|
+
insertText(
|
|
22
|
+
content: TextContent,
|
|
23
|
+
at: number,
|
|
24
|
+
text: string,
|
|
25
|
+
opts?: { marks?: readonly MarkSpec[] },
|
|
26
|
+
): TextContent;
|
|
27
|
+
|
|
28
|
+
registerMark(type: MarkType, behavior?: MarkBehavior): TextMarksDisposer;
|
|
29
|
+
isInclusive(type: MarkType): boolean;
|
|
30
|
+
insertText(
|
|
31
|
+
content: TextContent,
|
|
32
|
+
at: number,
|
|
33
|
+
text: string,
|
|
34
|
+
opts?: { marks?: readonly MarkSpec[] },
|
|
35
|
+
): TextContent;
|
|
36
|
+
}
|
|
37
|
+
export class TextMarksService implements TextMarksInterface {
|
|
38
|
+
private behaviors = new Map<MarkType, MarkBehavior>([
|
|
39
|
+
["bold", { inclusive: true }],
|
|
40
|
+
["italic", { inclusive: true }],
|
|
41
|
+
["underline", { inclusive: true }],
|
|
42
|
+
["code", { inclusive: true }],
|
|
43
|
+
["link", { inclusive: false }],
|
|
44
|
+
["initial", { inclusive: false }],
|
|
45
|
+
]);
|
|
46
|
+
|
|
47
|
+
registerMark(type: MarkType, behavior: MarkBehavior = { inclusive: true }): TextMarksDisposer {
|
|
48
|
+
if (this.behaviors.has(type)) throw new Error(`Mark "${type}" is already registered`);
|
|
49
|
+
this.behaviors.set(type, behavior);
|
|
50
|
+
return () => {
|
|
51
|
+
if (this.behaviors.get(type) === behavior) this.behaviors.delete(type);
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
isInclusive(type: MarkType) {
|
|
56
|
+
return this.behaviors.get(type)?.inclusive ?? true;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
private attrsKey(attrs?: Record<string, unknown>): string {
|
|
60
|
+
if (!attrs) return "";
|
|
61
|
+
const keys = Object.keys(attrs).sort();
|
|
62
|
+
return keys.length ? JSON.stringify(keys.map((k) => [k, attrs[k]])) : "";
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
private sameMarks(a: readonly Mark[], b: readonly Mark[]): boolean {
|
|
66
|
+
return (
|
|
67
|
+
a.length === b.length &&
|
|
68
|
+
a.every(
|
|
69
|
+
(mark, i) =>
|
|
70
|
+
mark.type === b[i].type &&
|
|
71
|
+
mark.start === b[i].start &&
|
|
72
|
+
mark.end === b[i].end &&
|
|
73
|
+
this.attrsKey(mark.attrs) === this.attrsKey(b[i].attrs),
|
|
74
|
+
)
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
private normalizeMarks(marks: readonly Mark[], textLength: number): readonly Mark[] {
|
|
79
|
+
const groups = new Map<string, Mark[]>();
|
|
80
|
+
|
|
81
|
+
for (const mark of marks) {
|
|
82
|
+
const start = Math.max(0, Math.min(mark.start, textLength));
|
|
83
|
+
const end = Math.max(0, Math.min(mark.end, textLength));
|
|
84
|
+
|
|
85
|
+
if (!(start < end)) continue;
|
|
86
|
+
|
|
87
|
+
const key = `${mark.type}|${this.attrsKey(mark.attrs)}`;
|
|
88
|
+
const list = groups.get(key) ?? [];
|
|
89
|
+
|
|
90
|
+
list.push({ ...mark, start, end });
|
|
91
|
+
groups.set(key, list);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const merged: Mark[] = [];
|
|
95
|
+
for (const list of groups.values()) {
|
|
96
|
+
list.sort((a, b) => a.start - b.start);
|
|
97
|
+
let cur = list[0];
|
|
98
|
+
|
|
99
|
+
for (let i = 1; i < list.length; i++) {
|
|
100
|
+
const next = list[i];
|
|
101
|
+
|
|
102
|
+
if (next.start <= cur.end) {
|
|
103
|
+
if (next.end > cur.end) cur = { ...cur, end: next.end };
|
|
104
|
+
} else {
|
|
105
|
+
merged.push(cur);
|
|
106
|
+
cur = next;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
merged.push(cur);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
merged.sort((a, b) => a.start - b.start || a.type.localeCompare(b.type) || a.end - b.end);
|
|
113
|
+
|
|
114
|
+
return this.sameMarks(marks, merged) ? marks : merged;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// private matches(m: Mark, spec: MarkSpec){
|
|
118
|
+
// m.type === spec.type && attrsKey(m.attrs) === attrsKey(spec.attrs);
|
|
119
|
+
//}
|
|
120
|
+
|
|
121
|
+
/** Vrai si [start, end) est entièrement couvert par des marks de ce type. */
|
|
122
|
+
private isFullyMarked<T extends MarkType = MarkType>(
|
|
123
|
+
marks: readonly Mark[],
|
|
124
|
+
start: number,
|
|
125
|
+
end: number,
|
|
126
|
+
spec: MarkSpec<T>,
|
|
127
|
+
): boolean {
|
|
128
|
+
// Pour un `link`, on ignore les attrs : un lien reste un lien, quel que soit son href
|
|
129
|
+
const relevant = marks.filter((m) => m.type === spec.type).sort((a, b) => a.start - b.start);
|
|
130
|
+
|
|
131
|
+
let covered = start;
|
|
132
|
+
for (const mark of relevant) {
|
|
133
|
+
if (mark.end <= covered) continue;
|
|
134
|
+
if (mark.start > covered) return false; // trou
|
|
135
|
+
covered = mark.end;
|
|
136
|
+
if (covered >= end) return true;
|
|
137
|
+
}
|
|
138
|
+
return covered >= end;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** Retire le type de mark sur [start, end) en coupant les marks qui débordent. */
|
|
142
|
+
private removeMark(marks: readonly Mark[], start: number, end: number, type: MarkType): Mark[] {
|
|
143
|
+
const out: Mark[] = [];
|
|
144
|
+
for (const mark of marks) {
|
|
145
|
+
if (mark.type !== type || mark.end <= start || mark.start >= end) {
|
|
146
|
+
out.push(mark); // non concerné
|
|
147
|
+
continue;
|
|
148
|
+
}
|
|
149
|
+
if (mark.start < start) out.push({ ...mark, end: start }); // partie avant
|
|
150
|
+
if (mark.end > end) out.push({ ...mark, start: end }); // partie après
|
|
151
|
+
}
|
|
152
|
+
return out;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
toggleMark(
|
|
156
|
+
content: TextContent,
|
|
157
|
+
{ start, end, spec }: { start: number; end: number; spec: MarkSpec },
|
|
158
|
+
): TextContent {
|
|
159
|
+
const len = content.text.length;
|
|
160
|
+
const s = Math.max(0, Math.min(start, end, len));
|
|
161
|
+
const e = Math.min(len, Math.max(start, end));
|
|
162
|
+
if (s >= e) return content; // sélection vide
|
|
163
|
+
|
|
164
|
+
const next = this.isFullyMarked(content.marks, s, e, spec)
|
|
165
|
+
? this.removeMark(content.marks, s, e, spec.type)
|
|
166
|
+
: [
|
|
167
|
+
// on retire d'abord pour qu'un lien existant soit remplacé, pas dupliqué
|
|
168
|
+
...(spec.type === "link" ? this.removeMark(content.marks, s, e, "link") : content.marks),
|
|
169
|
+
{ type: spec.type, start: s, end: e, ...(spec.attrs && { attrs: spec.attrs }) },
|
|
170
|
+
];
|
|
171
|
+
|
|
172
|
+
const marks = this.normalizeMarks(next, len);
|
|
173
|
+
return marks === content.marks ? content : { text: content.text, marks };
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
deleteRange(content: TextContent, start: number, end: number): TextContent {
|
|
177
|
+
const len = content.text.length;
|
|
178
|
+
const _start = Math.max(0, Math.min(start, end, len));
|
|
179
|
+
const _end = Math.min(len, Math.max(start, end));
|
|
180
|
+
if (_start >= _end) return content;
|
|
181
|
+
|
|
182
|
+
const diff = _end - _start;
|
|
183
|
+
const map = (p: number) => (p <= _start ? p : p >= _end ? p - diff : _end);
|
|
184
|
+
|
|
185
|
+
const newText = content.text.slice(0, _start) + content.text.slice(_end);
|
|
186
|
+
const marks = content.marks.map((mark) => ({
|
|
187
|
+
...mark,
|
|
188
|
+
start: map(mark.start),
|
|
189
|
+
end: map(mark.end),
|
|
190
|
+
}));
|
|
191
|
+
|
|
192
|
+
return { text: newText, marks: this.normalizeMarks(marks, newText.length) };
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
insertText(
|
|
196
|
+
content: TextContent,
|
|
197
|
+
at: number,
|
|
198
|
+
text: string,
|
|
199
|
+
opts: { marks?: readonly MarkSpec[] } = {},
|
|
200
|
+
): TextContent {
|
|
201
|
+
if (text.length === 0) return content;
|
|
202
|
+
|
|
203
|
+
const len = content.text.length;
|
|
204
|
+
const pos = Math.max(0, Math.min(at, len));
|
|
205
|
+
const n = text.length;
|
|
206
|
+
|
|
207
|
+
let marks: Mark[] = content.marks.map((mark) => {
|
|
208
|
+
if (mark.start >= pos) return { ...mark, start: mark.start + n, end: mark.end + n };
|
|
209
|
+
if (mark.end > pos) return { ...mark, end: mark.end + n };
|
|
210
|
+
if (mark.end === pos && this.isInclusive(mark.type)) return { ...mark, end: mark.end + n };
|
|
211
|
+
return mark;
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
if (opts.marks) {
|
|
215
|
+
const types = new Set([
|
|
216
|
+
...marks.map((mark) => mark.type),
|
|
217
|
+
...opts.marks.map((spec) => spec.type),
|
|
218
|
+
]);
|
|
219
|
+
for (const type of types) marks = this.removeMark(marks, pos, pos + n, type);
|
|
220
|
+
for (const spec of opts.marks) {
|
|
221
|
+
marks.push({
|
|
222
|
+
type: spec.type,
|
|
223
|
+
start: pos,
|
|
224
|
+
end: pos + n,
|
|
225
|
+
...(spec.attrs && { attrs: spec.attrs }),
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
const newText = content.text.slice(0, pos) + text + content.text.slice(pos);
|
|
231
|
+
return { text: newText, marks: this.normalizeMarks(marks, newText.length) };
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
replaceRange(
|
|
235
|
+
content: TextContent,
|
|
236
|
+
start: number,
|
|
237
|
+
end: number,
|
|
238
|
+
text: string,
|
|
239
|
+
opts?: { marks?: readonly MarkSpec[] },
|
|
240
|
+
): TextContent {
|
|
241
|
+
const s = Math.min(start, end);
|
|
242
|
+
return this.insertText(this.deleteRange(content, start, end), s, text, opts);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import type { NodeCommonProps } from "../../base/types/global.types";
|
|
2
|
+
import type { StyleName } from "./ui.types";
|
|
3
|
+
|
|
4
|
+
const Text: NodeCommonProps = {
|
|
5
|
+
style: {
|
|
6
|
+
width: "100%",
|
|
7
|
+
color: "var(--text-foreground)",
|
|
8
|
+
fontSize: "18px",
|
|
9
|
+
minHeight: "32px",
|
|
10
|
+
whiteSpace: "pre",
|
|
11
|
+
fontWeight: 400,
|
|
12
|
+
display: "block",
|
|
13
|
+
alignItems: "center",
|
|
14
|
+
outline: "none",
|
|
15
|
+
border: "none",
|
|
16
|
+
fieldSizing: "content",
|
|
17
|
+
cursor: "default",
|
|
18
|
+
paddingInline: 4,
|
|
19
|
+
userSelect: "none",
|
|
20
|
+
},
|
|
21
|
+
className:
|
|
22
|
+
"[&:empty:before]:content-[attr(data-placeholder)] [&:empty:before]:text-muted-foreground [&:empty:before]:pointer-events-none",
|
|
23
|
+
}; // ring ring-transparent bg-amber-500/10 focus:ring-primary
|
|
24
|
+
|
|
25
|
+
const Heading: NodeCommonProps = {
|
|
26
|
+
style: {
|
|
27
|
+
width: "100%",
|
|
28
|
+
maxWidth: "100%",
|
|
29
|
+
color: "var(--text-foreground)",
|
|
30
|
+
fontSize: "40px",
|
|
31
|
+
minHeight: "48px",
|
|
32
|
+
fontWeight: 400,
|
|
33
|
+
whiteSpace: "pre",
|
|
34
|
+
display: "block",
|
|
35
|
+
alignItems: "center",
|
|
36
|
+
outline: "none",
|
|
37
|
+
border: "none",
|
|
38
|
+
fieldSizing: "content",
|
|
39
|
+
cursor: "default",
|
|
40
|
+
paddingInline: 4,
|
|
41
|
+
userSelect: "none",
|
|
42
|
+
},
|
|
43
|
+
className:
|
|
44
|
+
"[&:empty:before]:content-[attr(data-placeholder)] [&:empty:before]:text-muted-foreground [&:empty:before]:pointer-events-none",
|
|
45
|
+
}; // ring ring-transparent bg-amber-500/10 focus:ring-primary
|
|
46
|
+
const Image: NodeCommonProps = {
|
|
47
|
+
style: {
|
|
48
|
+
width: "100%",
|
|
49
|
+
// minHeight: "480px",
|
|
50
|
+
objectFit: "cover",
|
|
51
|
+
display: "block",
|
|
52
|
+
alignItems: "center",
|
|
53
|
+
outline: "none",
|
|
54
|
+
border: "none",
|
|
55
|
+
fieldSizing: "content",
|
|
56
|
+
cursor: "default",
|
|
57
|
+
userSelect: "none",
|
|
58
|
+
},
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
const Divider: NodeCommonProps = {
|
|
62
|
+
style: {
|
|
63
|
+
width: "100%",
|
|
64
|
+
borderTopWidth: 1,
|
|
65
|
+
borderTopStyle: "solid",
|
|
66
|
+
borderTopColor: "var(--color-muted-foreground)",
|
|
67
|
+
marginBlock: 8,
|
|
68
|
+
height: 1,
|
|
69
|
+
position: "relative",
|
|
70
|
+
overflow: "visible",
|
|
71
|
+
},
|
|
72
|
+
pseudoStyle: {
|
|
73
|
+
":before": {
|
|
74
|
+
content: '""',
|
|
75
|
+
position: "absolute",
|
|
76
|
+
top: "-8px",
|
|
77
|
+
bottom: "-8px",
|
|
78
|
+
height: 16,
|
|
79
|
+
left: 0,
|
|
80
|
+
right: 0,
|
|
81
|
+
pointerEvents: "auto",
|
|
82
|
+
zIndex: 5,
|
|
83
|
+
},
|
|
84
|
+
},
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
//#TODO complete styles
|
|
88
|
+
export const STYLES: Partial<Record<StyleName, NodeCommonProps>> = {
|
|
89
|
+
text: Text,
|
|
90
|
+
heading: Heading,
|
|
91
|
+
image: Image,
|
|
92
|
+
divider: Divider,
|
|
93
|
+
};
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import type { NodeCommonProps } from "../../base/types/global.types";
|
|
2
|
+
import { UTILS } from "../../utils/global.utils";
|
|
3
|
+
import type { CommandsInterface, CommandType } from "../commandsbus/commandsbus";
|
|
4
|
+
import { STYLES } from "./default-styles";
|
|
5
|
+
import { UiError } from "./ui.errors";
|
|
6
|
+
import { cn } from "cn";
|
|
7
|
+
import type {
|
|
8
|
+
UiInterface,
|
|
9
|
+
StyleName,
|
|
10
|
+
SlotName,
|
|
11
|
+
Contribution,
|
|
12
|
+
SlotKey,
|
|
13
|
+
ContributionKind,
|
|
14
|
+
StylesMap,
|
|
15
|
+
ContributionDisposer,
|
|
16
|
+
PayloadInterceptor,
|
|
17
|
+
UIContext,
|
|
18
|
+
ButtonContributionId,
|
|
19
|
+
} from "./ui.types";
|
|
20
|
+
|
|
21
|
+
export class UiService implements UiInterface {
|
|
22
|
+
private commands: CommandsInterface;
|
|
23
|
+
|
|
24
|
+
private styles: Map<StyleName, NodeCommonProps> = new Map();
|
|
25
|
+
private outdatedStyles: Map<StyleName, NodeCommonProps> = new Map();
|
|
26
|
+
private bySlot: Map<SlotName, Contribution[]> = new Map();
|
|
27
|
+
private listeners: Map<SlotName, Set<() => void>> = new Map();
|
|
28
|
+
private interceptors = new Map<ButtonContributionId, PayloadInterceptor<any>[]>();
|
|
29
|
+
|
|
30
|
+
constructor(commands: CommandsInterface) {
|
|
31
|
+
this.commands = commands;
|
|
32
|
+
|
|
33
|
+
for (const [key, value] of UTILS.ObjectEntries(STYLES)) {
|
|
34
|
+
this.styles.set(key, value!);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
interceptPayload<C extends CommandType>(
|
|
39
|
+
contributionId: ButtonContributionId,
|
|
40
|
+
fn: PayloadInterceptor<C>,
|
|
41
|
+
) {
|
|
42
|
+
const list = this.interceptors.get(contributionId) ?? [];
|
|
43
|
+
this.interceptors.set(contributionId, [...list, fn]);
|
|
44
|
+
|
|
45
|
+
return () =>
|
|
46
|
+
this.interceptors.set(
|
|
47
|
+
contributionId,
|
|
48
|
+
(this.interceptors.get(contributionId) ?? []).filter((i) => i !== fn),
|
|
49
|
+
); // unsubscribe
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
async runContribution<C extends CommandType>(
|
|
53
|
+
c: Contribution<SlotName, SlotKey, C, "button">,
|
|
54
|
+
ui: UIContext,
|
|
55
|
+
) {
|
|
56
|
+
let payload = c.getPayload(ui);
|
|
57
|
+
for (const intercept of this.interceptors.get(c.id as ButtonContributionId) ?? []) {
|
|
58
|
+
const next = await intercept(payload, ui);
|
|
59
|
+
if (next === null) return; // annulé
|
|
60
|
+
payload = next;
|
|
61
|
+
}
|
|
62
|
+
this.commands.execute(c.command, payload);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
contribute<
|
|
66
|
+
S extends SlotName = SlotName,
|
|
67
|
+
Arg2 extends SlotKey | CommandType = SlotKey,
|
|
68
|
+
Arg3 extends CommandType | ContributionKind = Arg2 extends SlotKey
|
|
69
|
+
? CommandType
|
|
70
|
+
: ContributionKind,
|
|
71
|
+
Arg4 extends ContributionKind = ContributionKind,
|
|
72
|
+
>(slotName: S, c: Contribution<S, Arg2, Arg3, Arg4>): ContributionDisposer {
|
|
73
|
+
const prevContribution = this.bySlot.get(slotName) ?? [];
|
|
74
|
+
if (prevContribution.some((x) => x.id === c.id)) {
|
|
75
|
+
throw new UiError(`Contribution with id ${c.id} already exists`);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
this.bySlot.set(slotName, [...prevContribution, c as Contribution]);
|
|
79
|
+
return () => {
|
|
80
|
+
const current = this.bySlot.get(slotName) ?? [];
|
|
81
|
+
this.bySlot.set(
|
|
82
|
+
slotName,
|
|
83
|
+
current.filter((x) => x.id !== c.id),
|
|
84
|
+
);
|
|
85
|
+
this.notify(c.slot);
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
getSlots(slot: SlotName) {
|
|
90
|
+
return this.bySlot.get(slot) ?? EMPTY;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
subscribe(slot: SlotName, fn: () => void): ContributionDisposer {
|
|
94
|
+
const set = this.listeners.get(slot) ?? new Set();
|
|
95
|
+
set.add(fn);
|
|
96
|
+
this.listeners.set(slot, set);
|
|
97
|
+
return () => set.delete(fn);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
private notify(slot: SlotName) {
|
|
101
|
+
this.listeners.get(slot)?.forEach((fn) => fn());
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
registerStyle(styleName: StyleName, style: NodeCommonProps): ContributionDisposer {
|
|
105
|
+
const prevStyle = this.styles.get(styleName);
|
|
106
|
+
if (prevStyle) {
|
|
107
|
+
console.log(`[WARN]: Overwritting ${name} styles`);
|
|
108
|
+
this.outdatedStyles.set(styleName, prevStyle);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
this.styles.set(styleName, style);
|
|
112
|
+
|
|
113
|
+
return () => {
|
|
114
|
+
this.styles.set(styleName, this.outdatedStyles.get(styleName) ?? STYLES[styleName]!);
|
|
115
|
+
this.outdatedStyles.delete(styleName);
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
patchStyle(styleName: StyleName, style: NodeCommonProps): ContributionDisposer {
|
|
120
|
+
const prevStyle = this.styles.get(styleName) ?? { style: {}, pseudoStyle: {} };
|
|
121
|
+
|
|
122
|
+
this.outdatedStyles.set(styleName, prevStyle);
|
|
123
|
+
|
|
124
|
+
this.styles.set(styleName, {
|
|
125
|
+
style: {
|
|
126
|
+
...prevStyle.style,
|
|
127
|
+
...style.style,
|
|
128
|
+
},
|
|
129
|
+
pseudoStyle: {
|
|
130
|
+
...prevStyle.pseudoStyle,
|
|
131
|
+
...style.pseudoStyle,
|
|
132
|
+
},
|
|
133
|
+
className: cn(prevStyle.className, style.className),
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
return () => {
|
|
137
|
+
this.styles.set(styleName, this.outdatedStyles.get(styleName) ?? STYLES[styleName]!);
|
|
138
|
+
this.outdatedStyles.delete(styleName);
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
getStyle<T extends StyleName = keyof StylesMap>(styleName: T): NodeCommonProps {
|
|
143
|
+
return this.styles.get(styleName)!;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
const EMPTY: readonly Contribution[] = []; // référence stable
|
|
148
|
+
export * from "./ui.types";
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
import type { BlockId, Block } from "../../base";
|
|
2
|
+
import type { NodeCommonProps } from "../../base/types/global.types";
|
|
3
|
+
import type { CommandPayload, CommandType } from "../commandsbus";
|
|
4
|
+
import type { StateMap } from "../state";
|
|
5
|
+
|
|
6
|
+
export type MaybePromise<T> = T | Promise<T>;
|
|
7
|
+
|
|
8
|
+
// null = annuler l'exécution
|
|
9
|
+
export type PayloadInterceptor<C extends CommandType> = (
|
|
10
|
+
payload: CommandPayload<C>,
|
|
11
|
+
ui: UIContext,
|
|
12
|
+
) => MaybePromise<CommandPayload<C> | null>;
|
|
13
|
+
|
|
14
|
+
export interface SlotMap {
|
|
15
|
+
toolbar: {};
|
|
16
|
+
"block-actions": {};
|
|
17
|
+
sidebar: {};
|
|
18
|
+
inspector: {};
|
|
19
|
+
overlay: {};
|
|
20
|
+
}
|
|
21
|
+
export type SlotName = keyof SlotMap;
|
|
22
|
+
|
|
23
|
+
export interface IconMap {
|
|
24
|
+
TextIcon: {};
|
|
25
|
+
CursorIcon: {};
|
|
26
|
+
HeadingIcon: {};
|
|
27
|
+
CameraIcon: {};
|
|
28
|
+
ImagePlus: {};
|
|
29
|
+
TvMinimalPlayIcon: {};
|
|
30
|
+
QuoteIcon: {};
|
|
31
|
+
BracesIcon: {};
|
|
32
|
+
MinusIcon: {};
|
|
33
|
+
PlusIcon: {};
|
|
34
|
+
BoldIcon: {};
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export type SlotIcon = keyof IconMap;
|
|
38
|
+
|
|
39
|
+
export interface SlotComponentMap {
|
|
40
|
+
TextInspector: { blockId: BlockId };
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export type SlotComponentKey = keyof SlotComponentMap;
|
|
44
|
+
|
|
45
|
+
export interface SlotButtonPropsMap {
|
|
46
|
+
BlockActionButton: {
|
|
47
|
+
variant: "default" | "outline" | "secondary";
|
|
48
|
+
size: "icon" | "default" | "icon-lg";
|
|
49
|
+
isBaseButton?: boolean;
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export type SlotButtonKey = keyof SlotButtonPropsMap;
|
|
54
|
+
|
|
55
|
+
export type SlotKey = SlotButtonKey | SlotComponentKey;
|
|
56
|
+
|
|
57
|
+
export interface ButtonContributionIdentityMap {}
|
|
58
|
+
export type ButtonContributionId = keyof ButtonContributionIdentityMap;
|
|
59
|
+
|
|
60
|
+
export interface ComponentContributionIdentityMap {}
|
|
61
|
+
export type ComponentContributionId = keyof ComponentContributionIdentityMap;
|
|
62
|
+
|
|
63
|
+
export type ContributionId = ComponentContributionId | ButtonContributionId;
|
|
64
|
+
|
|
65
|
+
type BaseContribution<T extends SlotName = SlotName> = {
|
|
66
|
+
id: ContributionId;
|
|
67
|
+
slot: T;
|
|
68
|
+
order?: number;
|
|
69
|
+
when?: (ui: UIContext) => boolean;
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
export type ButtonContribution<
|
|
73
|
+
S extends SlotName = SlotName,
|
|
74
|
+
C extends CommandType = CommandType,
|
|
75
|
+
K extends SlotButtonKey = SlotButtonKey,
|
|
76
|
+
> = {
|
|
77
|
+
[Key in K]: BaseContribution<S> & {
|
|
78
|
+
command: C;
|
|
79
|
+
label?: string;
|
|
80
|
+
name: Key;
|
|
81
|
+
icon: SlotIcon;
|
|
82
|
+
kind: "button";
|
|
83
|
+
getPayload: (ui: UIContext) => CommandPayload<C>;
|
|
84
|
+
getProps: (ui: UIContext) => SlotButtonPropsMap[Key] | null;
|
|
85
|
+
};
|
|
86
|
+
}[K];
|
|
87
|
+
|
|
88
|
+
export type ComponentContribution<
|
|
89
|
+
T extends SlotName = SlotName,
|
|
90
|
+
K extends SlotComponentKey = SlotComponentKey,
|
|
91
|
+
> = {
|
|
92
|
+
[Key in K]: BaseContribution<T> & {
|
|
93
|
+
kind: "component";
|
|
94
|
+
name: Key;
|
|
95
|
+
getProps: (ui: UIContext) => SlotComponentMap[Key] | null;
|
|
96
|
+
};
|
|
97
|
+
}[K];
|
|
98
|
+
|
|
99
|
+
export interface ContributionMap<
|
|
100
|
+
S extends SlotName = SlotName,
|
|
101
|
+
C extends CommandType = CommandType,
|
|
102
|
+
B extends SlotKey = SlotKey,
|
|
103
|
+
> {
|
|
104
|
+
button: ButtonContribution<S, C, Extract<B, SlotButtonKey>>;
|
|
105
|
+
component: ComponentContribution<S, Extract<B, SlotComponentKey>>;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export type ContributionKind<S extends SlotName = SlotName, Arg2 = any, Arg3 = any> = [
|
|
109
|
+
S,
|
|
110
|
+
Arg2,
|
|
111
|
+
Arg3,
|
|
112
|
+
] extends [any, any, any]
|
|
113
|
+
? "button" | "component"
|
|
114
|
+
: never;
|
|
115
|
+
|
|
116
|
+
export interface UIContext {
|
|
117
|
+
selection: StateMap["selection"];
|
|
118
|
+
focus: StateMap["focus"];
|
|
119
|
+
hover: StateMap["hover"];
|
|
120
|
+
textSelection: StateMap["textSelection"];
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export type Contribution<
|
|
124
|
+
S extends SlotName = SlotName,
|
|
125
|
+
Arg2 extends SlotKey | CommandType = SlotKey,
|
|
126
|
+
Arg3 extends CommandType | ContributionKind = Arg2 extends SlotKey
|
|
127
|
+
? CommandType
|
|
128
|
+
: ContributionKind,
|
|
129
|
+
Arg4 extends ContributionKind = ContributionKind,
|
|
130
|
+
> = [Arg2] extends [SlotKey]
|
|
131
|
+
? ContributionMap<S, Arg3 extends CommandType ? Arg3 : CommandType, Arg2>[Arg4]
|
|
132
|
+
: ContributionMap<
|
|
133
|
+
S,
|
|
134
|
+
Arg2 extends CommandType ? Arg2 : CommandType,
|
|
135
|
+
SlotKey
|
|
136
|
+
>[Arg3 extends ContributionKind ? Arg3 : ContributionKind];
|
|
137
|
+
|
|
138
|
+
export type SlotBaseProps<
|
|
139
|
+
S extends SlotName = SlotName,
|
|
140
|
+
Arg2 extends SlotKey | CommandType = SlotKey,
|
|
141
|
+
Arg3 extends CommandType | ContributionKind = Arg2 extends SlotKey
|
|
142
|
+
? CommandType
|
|
143
|
+
: ContributionKind,
|
|
144
|
+
Arg4 extends ContributionKind = ContributionKind,
|
|
145
|
+
> = { c: Contribution<S, Arg2, Arg3, Arg4>; ui: UIContext };
|
|
146
|
+
|
|
147
|
+
export type ContributionDisposer = () => void;
|
|
148
|
+
|
|
149
|
+
type BlockStyles = {
|
|
150
|
+
[K in Block["type"]]: NodeCommonProps;
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
export interface StylesMap extends BlockStyles {}
|
|
154
|
+
export type StyleName = keyof StylesMap;
|
|
155
|
+
|
|
156
|
+
export interface UiInterface {
|
|
157
|
+
contribute<
|
|
158
|
+
S extends SlotName = SlotName,
|
|
159
|
+
Arg2 extends SlotKey | CommandType = SlotKey,
|
|
160
|
+
Arg3 extends CommandType | ContributionKind = Arg2 extends SlotKey
|
|
161
|
+
? CommandType
|
|
162
|
+
: ContributionKind,
|
|
163
|
+
Arg4 extends ContributionKind = ContributionKind,
|
|
164
|
+
>(
|
|
165
|
+
slotName: S,
|
|
166
|
+
contribution: Contribution<S, Arg2, Arg3, Arg4>,
|
|
167
|
+
): ContributionDisposer;
|
|
168
|
+
|
|
169
|
+
getSlots(name: SlotName): readonly Contribution[];
|
|
170
|
+
subscribe(name: SlotName, fn: () => void): ContributionDisposer;
|
|
171
|
+
|
|
172
|
+
getStyle: <T extends StyleName = StyleName>(styleName: T) => NodeCommonProps;
|
|
173
|
+
registerStyle: (styleName: StyleName, style: NodeCommonProps) => ContributionDisposer;
|
|
174
|
+
patchStyle: (styleName: StyleName, style: NodeCommonProps) => ContributionDisposer;
|
|
175
|
+
|
|
176
|
+
interceptPayload<C extends CommandType>(
|
|
177
|
+
contributionId: ButtonContributionId,
|
|
178
|
+
fn: PayloadInterceptor<C>,
|
|
179
|
+
): ContributionDisposer;
|
|
180
|
+
|
|
181
|
+
runContribution<C extends CommandType>(
|
|
182
|
+
c: Contribution<SlotName, SlotKey, C, "button">,
|
|
183
|
+
ui: UIContext,
|
|
184
|
+
): Promise<void>;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// Helper function for type-safe contributions
|
|
188
|
+
export function createContribution<
|
|
189
|
+
S extends SlotName = SlotName,
|
|
190
|
+
Arg2 extends SlotKey | CommandType = SlotKey,
|
|
191
|
+
Arg3 extends CommandType | ContributionKind = Arg2 extends SlotKey
|
|
192
|
+
? CommandType
|
|
193
|
+
: ContributionKind,
|
|
194
|
+
Arg4 extends ContributionKind = ContributionKind,
|
|
195
|
+
>(contribution: Contribution<S, Arg2, Arg3, Arg4>): Contribution<S, Arg2, Arg3, Arg4> {
|
|
196
|
+
return contribution;
|
|
197
|
+
}
|