@dou-ui/editor 0.1.0-alpha.13 → 0.1.0-alpha.15

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 (47) hide show
  1. package/dist/collaboration/binding.js +32 -10
  2. package/dist/collaboration/binding.js.map +1 -1
  3. package/dist/collaboration/index.js +3 -0
  4. package/dist/collaboration/index.js.map +1 -1
  5. package/dist/collaboration/merged-props.js +152 -0
  6. package/dist/collaboration/merged-props.js.map +1 -0
  7. package/dist/components/block-handle.js.map +1 -1
  8. package/dist/components/block-handle.vue_vue_type_script_setup_true_lang.js +55 -5
  9. package/dist/components/block-handle.vue_vue_type_script_setup_true_lang.js.map +1 -1
  10. package/dist/core/extension.js.map +1 -1
  11. package/dist/core/model.js +2 -1
  12. package/dist/core/model.js.map +1 -1
  13. package/dist/core/transaction.js +7 -1
  14. package/dist/core/transaction.js.map +1 -1
  15. package/dist/define-block.js +2 -1
  16. package/dist/define-block.js.map +1 -1
  17. package/dist/document.js +20 -1
  18. package/dist/document.js.map +1 -1
  19. package/dist/dom.js +12 -1
  20. package/dist/dom.js.map +1 -1
  21. package/dist/editor.vue_vue_type_script_setup_true_lang.js.map +1 -1
  22. package/dist/editor2.js.map +1 -1
  23. package/dist/flow/board-block.js +6 -0
  24. package/dist/flow/board-block.js.map +1 -0
  25. package/dist/flow/board-block.vue_vue_type_script_setup_true_lang.js +437 -0
  26. package/dist/flow/board-block.vue_vue_type_script_setup_true_lang.js.map +1 -0
  27. package/dist/flow/board.js +102 -0
  28. package/dist/flow/board.js.map +1 -0
  29. package/dist/flow/index.js +166 -0
  30. package/dist/flow/index.js.map +1 -0
  31. package/dist/locale.js +33 -6
  32. package/dist/locale.js.map +1 -1
  33. package/dist/style.css +1 -1
  34. package/dist/style.js +1 -0
  35. package/dist/types/collaboration/binding.d.ts +4 -0
  36. package/dist/types/collaboration/merged-props.d.ts +20 -0
  37. package/dist/types/core/extension.d.ts +5 -2
  38. package/dist/types/core/transaction.d.ts +6 -0
  39. package/dist/types/document.d.ts +3 -2
  40. package/dist/types/dom.d.ts +7 -0
  41. package/dist/types/editor.vue.d.ts +9 -0
  42. package/dist/types/flow/board-block.vue.d.ts +16 -0
  43. package/dist/types/flow/board.d.ts +38 -0
  44. package/dist/types/flow/index.d.ts +59 -0
  45. package/dist/types/index.d.ts +9 -0
  46. package/dist/types/locale.d.ts +10 -0
  47. package/package.json +14 -5
@@ -0,0 +1,437 @@
1
+ import { labelOf } from "../core/extension.js";
2
+ import { findBlockById } from "../core/blocks.js";
3
+ import { useEditor } from "../editor.js";
4
+ import { historyCommands } from "../core/editor.js";
5
+ import { joinStepKey, ownStepKey } from "../core/transaction.js";
6
+ import { BOARD_HEIGHT, BOARD_OPEN_EVENT, boardOf, clampHeight, onlyBoardChanged } from "./board.js";
7
+ import { DouButton, DouDialog } from "dou-ui";
8
+ import { computed, createBlock, createCommentVNode, createElementBlock, createElementVNode, createTextVNode, createVNode, defineComponent, mergeProps, nextTick, normalizeClass, normalizeStyle, onBeforeUnmount, onMounted, openBlock, shallowRef, toDisplayString, unref, useTemplateRef, watch, withCtx, withModifiers } from "vue";
9
+ import { NodeSelection } from "prosemirror-state";
10
+ import { EyeIcon, PencilIcon } from "@dou-ui/icons";
11
+ import { DouFlowControls, DouFlowEditor, defineFlowExtension } from "@dou-ui/flow";
12
+ // src/flow/board-block.vue?vue&type=script&setup=true&lang.ts
13
+ var _hoisted_1 = ["aria-label"];
14
+ var _hoisted_2 = {
15
+ key: 1,
16
+ class: "dou-editor-board__empty"
17
+ };
18
+ var _hoisted_3 = { class: "dou-editor-board__empty-text" };
19
+ var _hoisted_4 = {
20
+ key: 2,
21
+ class: "dou-editor-board__actions"
22
+ };
23
+ var _hoisted_5 = [
24
+ "aria-label",
25
+ "aria-valuemin",
26
+ "aria-valuemax",
27
+ "aria-valuenow"
28
+ ];
29
+ /** The board with the groups this reader folded or unfolded. */
30
+ var GROUP_WINDOW = 1e3;
31
+ /** A drag going on: its group, and when its last frame came. */
32
+ var board_block_vue_vue_type_script_setup_true_lang_default = /*@__PURE__*/ defineComponent({
33
+ __name: "board-block",
34
+ props: {
35
+ block: {},
36
+ updateProps: { type: Function },
37
+ selected: { type: Boolean },
38
+ editable: { type: Boolean },
39
+ settings: {}
40
+ },
41
+ setup(__props) {
42
+ function withFolded(board, folded) {
43
+ if (!folded.size) return board;
44
+ const nodes = board.nodes.map((node) => {
45
+ if (!folded.has(node.id)) return node;
46
+ const { collapsed: _, ...data } = node.data ?? {};
47
+ return {
48
+ ...node,
49
+ data: folded.get(node.id) ? {
50
+ ...data,
51
+ collapsed: true
52
+ } : data
53
+ };
54
+ });
55
+ return {
56
+ ...board,
57
+ nodes
58
+ };
59
+ }
60
+ const props = __props;
61
+ const { view, messages } = useEditor();
62
+ const text = computed(() => messages.value.board);
63
+ const label = computed(() => labelOf(props.settings.label, messages.value));
64
+ const stored = computed(() => boardOf(props.block.props.board));
65
+ const folded = shallowRef(/* @__PURE__ */ new Map());
66
+ const board = computed(() => withFolded(stored.value, folded.value));
67
+ const empty = computed(() => !board.value.nodes.length);
68
+ const open = shallowRef(false);
69
+ watch(open, (value) => {
70
+ if (!value) shown.value = board.value;
71
+ });
72
+ const root = useTemplateRef("root");
73
+ const canvas = useTemplateRef("canvas");
74
+ const preview = useTemplateRef("preview");
75
+ const dragged = shallowRef(null);
76
+ const height = computed(() => dragged.value ?? clampHeight(Number(props.block.props.height) || props.settings.height));
77
+ const seen = shallowRef(typeof IntersectionObserver === "undefined");
78
+ let observer = null;
79
+ let resized = null;
80
+ let frame = 0;
81
+ /** Fits the preview to its box again, once the nodes have been laid out. */
82
+ function refit() {
83
+ cancelAnimationFrame(frame);
84
+ frame = requestAnimationFrame(() => preview.value?.fit());
85
+ }
86
+ onMounted(() => {
87
+ const element = root.value;
88
+ element.addEventListener(BOARD_OPEN_EVENT, openEditor);
89
+ if (!seen.value) {
90
+ observer = new IntersectionObserver((entries) => {
91
+ if (!entries.some((entry) => entry.isIntersecting)) return;
92
+ seen.value = true;
93
+ observer?.disconnect();
94
+ observer = null;
95
+ }, { rootMargin: "200px" });
96
+ observer.observe(element);
97
+ }
98
+ if (typeof ResizeObserver !== "undefined") {
99
+ let width = element.clientWidth;
100
+ resized = new ResizeObserver(() => {
101
+ if (element.clientWidth === width) return;
102
+ width = element.clientWidth;
103
+ refit();
104
+ });
105
+ resized.observe(element);
106
+ }
107
+ });
108
+ onBeforeUnmount(() => {
109
+ settle();
110
+ root.value?.removeEventListener(BOARD_OPEN_EVENT, openEditor);
111
+ observer?.disconnect();
112
+ resized?.disconnect();
113
+ cancelAnimationFrame(frame);
114
+ });
115
+ const shown = shallowRef(board.value);
116
+ watch(board, (value) => {
117
+ if (!open.value) shown.value = value;
118
+ });
119
+ watch(shown, () => void nextTick(refit));
120
+ watch(height, () => void nextTick(refit));
121
+ function openEditor() {
122
+ if (!props.editable && empty.value) return;
123
+ steps.undo = 0;
124
+ steps.redo = 0;
125
+ pending = null;
126
+ open.value = true;
127
+ }
128
+ const contentPos = () => {
129
+ const state = view.value?.state;
130
+ return state ? findBlockById(state.doc, props.block.id)?.contentPos ?? null : null;
131
+ };
132
+ /** The keys go to the canvas from the start: a tool's letter, `Esc` letting go of a selection. */
133
+ function onOpened() {
134
+ canvas.value?.querySelector(".dou-flow-editor")?.focus({ preventScroll: true });
135
+ }
136
+ /** Back in the document: the block selected, the editor focused. */
137
+ function onClosed() {
138
+ const editor = view.value;
139
+ const pos = contentPos();
140
+ if (!editor || pos === null) return;
141
+ if (props.editable) editor.dispatch(editor.state.tr.setSelection(NodeSelection.create(editor.state.doc, pos)));
142
+ editor.focus();
143
+ }
144
+ let pending = null;
145
+ /** The steps taken on the board since it was opened, and those undone. */
146
+ const steps = {
147
+ undo: 0,
148
+ redo: 0
149
+ };
150
+ /** The board as the document keeps it: groups folded as they are there; and what this reader folded. */
151
+ function toDocument(next) {
152
+ const before = new Map(stored.value.nodes.map((node) => [node.id, node]));
153
+ let changed = null;
154
+ const nodes = next.nodes.map((node) => {
155
+ const mine = !!node.data?.collapsed;
156
+ const theirs = before.get(node.id)?.data?.collapsed;
157
+ if (mine === !!theirs) {
158
+ if (folded.value.has(node.id)) (changed ??= new Map(folded.value)).delete(node.id);
159
+ return node;
160
+ }
161
+ if (folded.value.get(node.id) !== mine) (changed ??= new Map(folded.value)).set(node.id, mine);
162
+ const { collapsed: _, ...data } = node.data ?? {};
163
+ if (theirs !== void 0) return {
164
+ ...node,
165
+ data: {
166
+ ...data,
167
+ collapsed: theirs
168
+ }
169
+ };
170
+ if (Object.keys(data).length || before.get(node.id)?.data) return {
171
+ ...node,
172
+ data
173
+ };
174
+ const { data: __, ...rest } = node;
175
+ return rest;
176
+ });
177
+ if (changed) folded.value = changed;
178
+ warnDataImages(next, before);
179
+ return {
180
+ ...next,
181
+ nodes
182
+ };
183
+ }
184
+ let warned = false;
185
+ /** Pictures kept in the document as data URLs make it large: say so once, while developing. */
186
+ function warnDataImages(next, before) {
187
+ if (warned || process.env.NODE_ENV === "production") return;
188
+ if (!next.nodes.find((node) => typeof node.data?.src === "string" && node.data.src.startsWith("data:") && before.get(node.id)?.data?.src !== node.data.src)) return;
189
+ warned = true;
190
+ console.warn(`[@dou-ui/editor] a picture on the "${props.block.type}" block is kept in the document as a data URL; pass \`upload\` to whiteboardBlock() to keep only its address`);
191
+ }
192
+ const currentBoard = () => {
193
+ const pos = contentPos();
194
+ return pos === null ? null : view.value?.state.doc.nodeAt(pos)?.attrs.board ?? null;
195
+ };
196
+ /** Writes the board as one undo step of its own. */
197
+ function record(next) {
198
+ const editor = view.value;
199
+ const pos = contentPos();
200
+ if (!editor || pos === null) return;
201
+ editor.dispatch(editor.state.tr.setNodeAttribute(pos, "board", next).setMeta(ownStepKey, true));
202
+ steps.undo++;
203
+ steps.redo = 0;
204
+ }
205
+ /** Writes the board into the undo step before, the drag's. */
206
+ function join(next) {
207
+ const editor = view.value;
208
+ const pos = contentPos();
209
+ if (!editor || pos === null) return;
210
+ const tr = editor.state.tr.setNodeAttribute(pos, "board", next).setMeta(joinStepKey, true);
211
+ editor.dispatch(tr.setMeta("appendedTransaction", editor.state.tr));
212
+ }
213
+ /** Ends the drag going on: the next change is a step of its own. */
214
+ function settle() {
215
+ pending = null;
216
+ }
217
+ function forward(tr) {
218
+ const editor = view.value;
219
+ const pos = contentPos();
220
+ if (!editor || pos === null || !props.editable) return;
221
+ const group = tr.meta.group;
222
+ const next = toDocument(tr.doc);
223
+ if (JSON.stringify(next) === JSON.stringify(currentBoard())) return;
224
+ const now = Date.now();
225
+ if (pending && (pending.group !== group || now - pending.time >= GROUP_WINDOW)) {
226
+ if (tr.meta.history === false) return join(next);
227
+ settle();
228
+ }
229
+ if (tr.meta.history === false) {
230
+ editor.dispatch(editor.state.tr.setNodeAttribute(pos, "board", next).setMeta("addToHistory", false));
231
+ return;
232
+ }
233
+ if (!group) return record(next);
234
+ if (pending) {
235
+ pending.time = now;
236
+ return join(next);
237
+ }
238
+ pending = {
239
+ group,
240
+ time: now
241
+ };
242
+ record(next);
243
+ }
244
+ /** Undo or redo in the document; refused when it would change something besides this board. */
245
+ function travel(kind) {
246
+ settle();
247
+ const editor = view.value;
248
+ if (!editor || !steps[kind]) return false;
249
+ const before = editor.state.doc;
250
+ if (!historyCommands(editor.state)[kind](editor.state, editor.dispatch, editor)) return false;
251
+ if (!onlyBoardChanged(before, editor.state.doc, props.block.id)) {
252
+ historyCommands(editor.state)[kind === "undo" ? "redo" : "undo"](editor.state, editor.dispatch, editor);
253
+ steps[kind] = 0;
254
+ return false;
255
+ }
256
+ steps[kind]--;
257
+ steps[kind === "undo" ? "redo" : "undo"]++;
258
+ return true;
259
+ }
260
+ /** A cancelled drag: the board is back where it started, and its step is taken back. */
261
+ function drop(group) {
262
+ if (pending?.group !== group) return;
263
+ pending = null;
264
+ const editor = view.value;
265
+ if (!editor || !steps.undo) return;
266
+ if (historyCommands(editor.state).undo(editor.state, editor.dispatch, editor)) steps.undo--;
267
+ }
268
+ const history = {
269
+ undo: () => travel("undo"),
270
+ redo: () => travel("redo"),
271
+ canUndo: () => steps.undo > 0,
272
+ canRedo: () => steps.redo > 0 && !pending,
273
+ drop,
274
+ close: settle
275
+ };
276
+ const bridge = defineFlowExtension({
277
+ name: "editor-board-history",
278
+ history: () => history
279
+ });
280
+ const extensions = [...props.settings.extensions, bridge];
281
+ function onResizeStart(event) {
282
+ if (event.button !== 0) return;
283
+ event.preventDefault();
284
+ const bar = event.currentTarget;
285
+ bar.setPointerCapture(event.pointerId);
286
+ const startY = event.clientY;
287
+ const start = height.value;
288
+ const move = (e) => dragged.value = clampHeight(start + e.clientY - startY);
289
+ const finish = (commit) => {
290
+ bar.removeEventListener("pointermove", move);
291
+ bar.removeEventListener("pointerup", up);
292
+ bar.removeEventListener("pointercancel", cancel);
293
+ window.removeEventListener("keydown", escape, true);
294
+ const value = dragged.value;
295
+ dragged.value = null;
296
+ if (commit && value !== null && value !== start) props.updateProps({ height: value });
297
+ };
298
+ const up = () => finish(true);
299
+ const cancel = () => finish(false);
300
+ const escape = (e) => {
301
+ if (e.key !== "Escape") return;
302
+ e.preventDefault();
303
+ e.stopPropagation();
304
+ finish(false);
305
+ };
306
+ bar.addEventListener("pointermove", move);
307
+ bar.addEventListener("pointerup", up);
308
+ bar.addEventListener("pointercancel", cancel);
309
+ window.addEventListener("keydown", escape, true);
310
+ }
311
+ function onResizeKey(event) {
312
+ const by = {
313
+ ArrowUp: -BOARD_HEIGHT.step,
314
+ ArrowDown: BOARD_HEIGHT.step
315
+ }[event.key];
316
+ if (by) {
317
+ event.preventDefault();
318
+ props.updateProps({ height: clampHeight(height.value + by) });
319
+ } else if (event.key === "Escape") {
320
+ event.preventDefault();
321
+ onClosed();
322
+ }
323
+ }
324
+ function resetHeight() {
325
+ props.updateProps({ height: props.settings.height });
326
+ }
327
+ return (_ctx, _cache) => {
328
+ return openBlock(), createElementBlock("div", {
329
+ ref_key: "root",
330
+ ref: root,
331
+ class: normalizeClass(["dou-editor-board", {
332
+ "is-selected": __props.selected,
333
+ "is-empty": empty.value
334
+ }]),
335
+ style: normalizeStyle({ height: `${height.value}px` }),
336
+ onDblclick: openEditor
337
+ }, [
338
+ !empty.value ? (openBlock(), createElementBlock("div", {
339
+ key: 0,
340
+ class: "dou-editor-board__preview",
341
+ role: "img",
342
+ "aria-label": label.value
343
+ }, [seen.value ? (openBlock(), createBlock(unref(DouFlowEditor), mergeProps({
344
+ key: 0,
345
+ class: "dou-editor-board__canvas",
346
+ inert: "",
347
+ ref_key: "preview",
348
+ ref: preview,
349
+ "model-value": shown.value
350
+ }, __props.settings.editorProps, {
351
+ extensions: __props.settings.extensions,
352
+ readonly: "",
353
+ "fit-view": "",
354
+ "fit-min-zoom": .05,
355
+ "simple-zoom": 0,
356
+ "min-zoom": .05,
357
+ "max-zoom": 1,
358
+ bordered: false,
359
+ searchable: false,
360
+ commentable: false,
361
+ "pan-on-scroll": false
362
+ }), null, 16, ["model-value", "extensions"])) : createCommentVNode("", true)], 8, _hoisted_1)) : (openBlock(), createElementBlock("div", _hoisted_2, [createElementVNode("span", _hoisted_3, toDisplayString(text.value.empty), 1), __props.editable ? (openBlock(), createBlock(unref(DouButton), {
363
+ key: 0,
364
+ size: "sm",
365
+ variant: "outline",
366
+ onClick: openEditor
367
+ }, {
368
+ leading: withCtx(() => [createVNode(unref(PencilIcon))]),
369
+ default: withCtx(() => [createTextVNode(" " + toDisplayString(text.value.start), 1)]),
370
+ _: 1
371
+ })) : createCommentVNode("", true)])),
372
+ !empty.value ? (openBlock(), createElementBlock("div", _hoisted_4, [createVNode(unref(DouButton), {
373
+ size: "sm",
374
+ variant: "outline",
375
+ class: "dou-editor-board__open",
376
+ onClick: openEditor
377
+ }, {
378
+ leading: withCtx(() => [__props.editable ? (openBlock(), createBlock(unref(PencilIcon), { key: 0 })) : (openBlock(), createBlock(unref(EyeIcon), { key: 1 }))]),
379
+ default: withCtx(() => [createTextVNode(" " + toDisplayString(__props.editable ? text.value.edit : text.value.view), 1)]),
380
+ _: 1
381
+ })])) : createCommentVNode("", true),
382
+ __props.editable ? (openBlock(), createElementBlock("div", {
383
+ key: 3,
384
+ class: "dou-editor-board__resize",
385
+ role: "separator",
386
+ "aria-orientation": "horizontal",
387
+ tabindex: "0",
388
+ "aria-label": text.value.resize,
389
+ "aria-valuemin": unref(BOARD_HEIGHT).min,
390
+ "aria-valuemax": unref(BOARD_HEIGHT).max,
391
+ "aria-valuenow": height.value,
392
+ "data-editor-control": "",
393
+ onPointerdown: onResizeStart,
394
+ onKeydown: onResizeKey,
395
+ onDblclick: withModifiers(resetHeight, ["stop"])
396
+ }, null, 40, _hoisted_5)) : createCommentVNode("", true),
397
+ createVNode(unref(DouDialog), {
398
+ open: open.value,
399
+ "onUpdate:open": _cache[0] || (_cache[0] = ($event) => open.value = $event),
400
+ title: label.value,
401
+ fullscreen: "",
402
+ "destroy-on-close": "",
403
+ onOpened,
404
+ onClose: settle,
405
+ onClosed
406
+ }, {
407
+ default: withCtx(() => [createElementVNode("div", {
408
+ ref_key: "canvas",
409
+ ref: canvas,
410
+ class: "dou-editor-board__editor",
411
+ "data-editor-control": ""
412
+ }, [createVNode(unref(DouFlowEditor), mergeProps({ "model-value": board.value }, __props.settings.editorProps, {
413
+ extensions,
414
+ readonly: !__props.editable,
415
+ "fit-view": "",
416
+ "fit-min-zoom": .1,
417
+ "simple-zoom": .1,
418
+ commentable: false,
419
+ "aria-label": label.value,
420
+ onTransaction: forward
421
+ }), {
422
+ default: withCtx(() => [createVNode(unref(DouFlowControls))]),
423
+ _: 1
424
+ }, 16, [
425
+ "model-value",
426
+ "readonly",
427
+ "aria-label"
428
+ ])], 512)]),
429
+ _: 1
430
+ }, 8, ["open", "title"])
431
+ ], 38);
432
+ };
433
+ }
434
+ });
435
+ export { board_block_vue_vue_type_script_setup_true_lang_default as default };
436
+
437
+ //# sourceMappingURL=board-block.vue_vue_type_script_setup_true_lang.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"board-block.vue_vue_type_script_setup_true_lang.js","names":["_openBlock","_createElementBlock","_normalizeClass","_normalizeStyle","_createBlock","_unref","_mergeProps","_createCommentVNode","_createElementVNode","_toDisplayString","_withCtx","_createVNode","_createTextVNode","_withModifiers"],"sources":["../../src/flow/board-block.vue"],"sourcesContent":["<script setup lang=\"ts\">\nimport {\n DouFlowControls,\n DouFlowEditor,\n defineFlowExtension,\n type FlowDocument,\n type FlowExtension,\n type FlowHistoryHandler,\n type FlowNode,\n type FlowTransaction,\n} from '@dou-ui/flow'\nimport { EyeIcon, PencilIcon } from '@dou-ui/icons'\nimport { DouButton, DouDialog } from 'dou-ui'\nimport { NodeSelection } from 'prosemirror-state'\nimport {\n computed,\n nextTick,\n onBeforeUnmount,\n onMounted,\n shallowRef,\n useTemplateRef,\n watch,\n} from 'vue'\nimport { findBlockById } from '../core/blocks'\nimport { historyCommands } from '../core/editor'\nimport { labelOf } from '../core/extension'\nimport type { BlockProps } from '../core/model'\nimport { joinStepKey, ownStepKey } from '../core/transaction'\nimport { useEditor } from '../editor'\nimport {\n BOARD_HEIGHT,\n BOARD_OPEN_EVENT,\n boardOf,\n clampHeight,\n onlyBoardChanged,\n type BoardBlockSettings,\n} from './board'\n\n/** The board with the groups this reader folded or unfolded. */\nfunction withFolded(board: FlowDocument, folded: Map<string, boolean>): FlowDocument {\n if (!folded.size) return board\n const nodes = board.nodes.map((node) => {\n if (!folded.has(node.id)) return node\n const { collapsed: _, ...data } = node.data ?? {}\n return { ...node, data: folded.get(node.id) ? { ...data, collapsed: true } : data }\n })\n return { ...board, nodes }\n}\n\nconst props = defineProps<{\n block: { id: string; type: string; props: BlockProps }\n updateProps: (props: BlockProps) => void\n selected: boolean\n editable: boolean\n settings: BoardBlockSettings\n}>()\n\nconst { view, messages } = useEditor()\nconst text = computed(() => messages.value.board)\nconst label = computed(() => labelOf(props.settings.label, messages.value))\nconst stored = computed(() => boardOf(props.block.props.board))\n/*\n * Whether a group is folded is each reader's own view, as with toggles: it\n * stays out of the document (what it holds there is kept), and is shown\n * from here.\n */\nconst folded = shallowRef(new Map<string, boolean>())\nconst board = computed(() => withFolded(stored.value, folded.value))\nconst empty = computed(() => !board.value.nodes.length)\n\n/* the fullscreen editor */\nconst open = shallowRef(false)\nwatch(open, (value) => {\n if (!value) shown.value = board.value\n})\n\nconst root = useTemplateRef<HTMLElement>('root')\nconst canvas = useTemplateRef<HTMLElement>('canvas')\nconst preview = useTemplateRef<InstanceType<typeof DouFlowEditor>>('preview')\n\n/* the height, followed live while the bar is dragged */\nconst dragged = shallowRef<number | null>(null)\nconst height = computed(\n () => dragged.value ?? clampHeight(Number(props.block.props.height) || props.settings.height),\n)\n\n/* the preview is drawn once the block comes near the screen */\nconst seen = shallowRef(typeof IntersectionObserver === 'undefined')\nlet observer: IntersectionObserver | null = null\nlet resized: ResizeObserver | null = null\nlet frame = 0\n/** Fits the preview to its box again, once the nodes have been laid out. */\nfunction refit() {\n cancelAnimationFrame(frame)\n frame = requestAnimationFrame(() => preview.value?.fit())\n}\nonMounted(() => {\n const element = root.value!\n element.addEventListener(BOARD_OPEN_EVENT, openEditor)\n if (!seen.value) {\n observer = new IntersectionObserver(\n (entries) => {\n if (!entries.some((entry) => entry.isIntersecting)) return\n seen.value = true\n observer?.disconnect()\n observer = null\n },\n { rootMargin: '200px' },\n )\n observer.observe(element)\n }\n if (typeof ResizeObserver !== 'undefined') {\n let width = element.clientWidth\n resized = new ResizeObserver(() => {\n if (element.clientWidth === width) return\n width = element.clientWidth\n refit()\n })\n resized.observe(element)\n }\n})\nonBeforeUnmount(() => {\n settle()\n root.value?.removeEventListener(BOARD_OPEN_EVENT, openEditor)\n observer?.disconnect()\n resized?.disconnect()\n cancelAnimationFrame(frame)\n})\n/* what the preview shows: held while the fullscreen editor covers it, so it isn't redrawn each frame */\nconst shown = shallowRef(board.value)\nwatch(board, (value) => {\n if (!open.value) shown.value = value\n})\nwatch(shown, () => void nextTick(refit))\nwatch(height, () => void nextTick(refit))\n\nfunction openEditor() {\n if (!props.editable && empty.value) return\n steps.undo = 0\n steps.redo = 0\n pending = null\n open.value = true\n}\n\nconst contentPos = () => {\n const state = view.value?.state\n return state ? (findBlockById(state.doc, props.block.id)?.contentPos ?? null) : null\n}\n\n/** The keys go to the canvas from the start: a tool's letter, `Esc` letting go of a selection. */\nfunction onOpened() {\n canvas.value?.querySelector<HTMLElement>('.dou-flow-editor')?.focus({ preventScroll: true })\n}\n\n/** Back in the document: the block selected, the editor focused. */\nfunction onClosed() {\n const editor = view.value\n const pos = contentPos()\n if (!editor || pos === null) return\n if (props.editable)\n editor.dispatch(editor.state.tr.setSelection(NodeSelection.create(editor.state.doc, pos)))\n editor.focus()\n}\n\n/*\n * Changes on the board go into the document as they are made, so `v-model`,\n * collaboration and the other blocks see them, and into the editor's undo\n * history: undo in the fullscreen editor is the document's, one step per\n * change on the board. The frames of a drag (or nudges with the arrow keys,\n * or typing in one node) join the step of the first one; only the fields\n * they change are written, so what others change meanwhile stays theirs. A\n * cancelled drag takes its step back.\n */\nconst GROUP_WINDOW = 1000\n/** A drag going on: its group, and when its last frame came. */\nlet pending: { group: string; time: number } | null = null\n/** The steps taken on the board since it was opened, and those undone. */\nconst steps = { undo: 0, redo: 0 }\n\n/** The board as the document keeps it: groups folded as they are there; and what this reader folded. */\nfunction toDocument(next: FlowDocument): FlowDocument {\n const before = new Map(stored.value.nodes.map((node) => [node.id, node]))\n let changed: Map<string, boolean> | null = null\n const nodes = next.nodes.map((node) => {\n const mine = !!node.data?.collapsed\n const theirs = before.get(node.id)?.data?.collapsed\n if (mine === !!theirs) {\n if (folded.value.has(node.id)) (changed ??= new Map(folded.value)).delete(node.id)\n return node\n }\n if (folded.value.get(node.id) !== mine) (changed ??= new Map(folded.value)).set(node.id, mine)\n const { collapsed: _, ...data } = node.data ?? {}\n if (theirs !== undefined) return { ...node, data: { ...data, collapsed: theirs } }\n // a node with no data before has none after\n if (Object.keys(data).length || before.get(node.id)?.data) return { ...node, data }\n const { data: __, ...rest } = node\n return rest as FlowNode\n })\n if (changed) folded.value = changed\n warnDataImages(next, before)\n return { ...next, nodes }\n}\n\nlet warned = false\n/** Pictures kept in the document as data URLs make it large: say so once, while developing. */\nfunction warnDataImages(next: FlowDocument, before: Map<string, FlowNode>) {\n if (warned || process.env.NODE_ENV === 'production') return\n const added = next.nodes.find(\n (node) =>\n typeof node.data?.src === 'string' &&\n node.data.src.startsWith('data:') &&\n before.get(node.id)?.data?.src !== node.data.src,\n )\n if (!added) return\n warned = true\n console.warn(\n `[@dou-ui/editor] a picture on the \"${props.block.type}\" block is kept in the document as a data URL; pass \\`upload\\` to whiteboardBlock() to keep only its address`,\n )\n}\n\nconst currentBoard = () => {\n const pos = contentPos()\n return pos === null ? null : (view.value?.state.doc.nodeAt(pos)?.attrs.board ?? null)\n}\n\n/** Writes the board as one undo step of its own. */\nfunction record(next: unknown) {\n const editor = view.value\n const pos = contentPos()\n if (!editor || pos === null) return\n editor.dispatch(editor.state.tr.setNodeAttribute(pos, 'board', next).setMeta(ownStepKey, true))\n steps.undo++\n steps.redo = 0\n}\n\n/** Writes the board into the undo step before, the drag's. */\nfunction join(next: unknown) {\n const editor = view.value\n const pos = contentPos()\n if (!editor || pos === null) return\n const tr = editor.state.tr.setNodeAttribute(pos, 'board', next).setMeta(joinStepKey, true)\n // for the built-in history: part of the step before, however long after\n editor.dispatch(tr.setMeta('appendedTransaction', editor.state.tr))\n}\n\n/** Ends the drag going on: the next change is a step of its own. */\nfunction settle() {\n pending = null\n}\n\nfunction forward(tr: FlowTransaction) {\n const editor = view.value\n const pos = contentPos()\n if (!editor || pos === null || !props.editable) return\n const group = tr.meta.group\n const next = toDocument(tr.doc)\n // only folded or unfolded: nothing for the document\n if (JSON.stringify(next) === JSON.stringify(currentBoard())) return\n const now = Date.now()\n if (pending && (pending.group !== group || now - pending.time >= GROUP_WINDOW)) {\n // a cancelled drag putting the board back: part of its step\n if (tr.meta.history === false) return join(next)\n settle()\n }\n if (tr.meta.history === false) {\n editor.dispatch(\n editor.state.tr.setNodeAttribute(pos, 'board', next).setMeta('addToHistory', false),\n )\n return\n }\n if (!group) return record(next)\n if (pending) {\n pending.time = now\n return join(next)\n }\n pending = { group, time: now }\n record(next)\n}\n\n/** Undo or redo in the document; refused when it would change something besides this board. */\nfunction travel(kind: 'undo' | 'redo') {\n settle()\n const editor = view.value\n if (!editor || !steps[kind]) return false\n const before = editor.state.doc\n const commands = historyCommands(editor.state)\n if (!commands[kind](editor.state, editor.dispatch, editor)) return false\n if (!onlyBoardChanged(before, editor.state.doc, props.block.id)) {\n // the board's own steps are used up: put back what was taken\n historyCommands(editor.state)[kind === 'undo' ? 'redo' : 'undo'](\n editor.state,\n editor.dispatch,\n editor,\n )\n steps[kind] = 0\n return false\n }\n steps[kind]--\n steps[kind === 'undo' ? 'redo' : 'undo']++\n return true\n}\n\n/** A cancelled drag: the board is back where it started, and its step is taken back. */\nfunction drop(group: string) {\n if (pending?.group !== group) return\n pending = null\n const editor = view.value\n if (!editor || !steps.undo) return\n if (historyCommands(editor.state).undo(editor.state, editor.dispatch, editor)) steps.undo--\n}\n\nconst history: FlowHistoryHandler = {\n undo: () => travel('undo'),\n redo: () => travel('redo'),\n canUndo: () => steps.undo > 0,\n canRedo: () => steps.redo > 0 && !pending,\n drop,\n close: settle,\n}\nconst bridge: FlowExtension = defineFlowExtension({\n name: 'editor-board-history',\n history: () => history,\n})\nconst extensions = [...props.settings.extensions, bridge]\n\n/* changing the height: drag the bar under the block, or the arrow keys on it */\nfunction onResizeStart(event: PointerEvent) {\n if (event.button !== 0) return\n event.preventDefault()\n const bar = event.currentTarget as HTMLElement\n bar.setPointerCapture(event.pointerId)\n const startY = event.clientY\n const start = height.value\n const move = (e: PointerEvent) => (dragged.value = clampHeight(start + e.clientY - startY))\n const finish = (commit: boolean) => {\n bar.removeEventListener('pointermove', move)\n bar.removeEventListener('pointerup', up)\n bar.removeEventListener('pointercancel', cancel)\n window.removeEventListener('keydown', escape, true)\n const value = dragged.value\n dragged.value = null\n if (commit && value !== null && value !== start) props.updateProps({ height: value })\n }\n const up = () => finish(true)\n const cancel = () => finish(false)\n const escape = (e: KeyboardEvent) => {\n if (e.key !== 'Escape') return\n e.preventDefault()\n e.stopPropagation()\n finish(false)\n }\n bar.addEventListener('pointermove', move)\n bar.addEventListener('pointerup', up)\n bar.addEventListener('pointercancel', cancel)\n window.addEventListener('keydown', escape, true)\n}\n\nfunction onResizeKey(event: KeyboardEvent) {\n const by = { ArrowUp: -BOARD_HEIGHT.step, ArrowDown: BOARD_HEIGHT.step }[event.key]\n if (by) {\n event.preventDefault()\n props.updateProps({ height: clampHeight(height.value + by) })\n } else if (event.key === 'Escape') {\n event.preventDefault()\n onClosed()\n }\n}\n\nfunction resetHeight() {\n props.updateProps({ height: props.settings.height })\n}\n</script>\n\n<template>\n <div\n ref=\"root\"\n class=\"dou-editor-board\"\n :class=\"{ 'is-selected': selected, 'is-empty': empty }\"\n :style=\"{ height: `${height}px` }\"\n @dblclick=\"openEditor\"\n >\n <div v-if=\"!empty\" class=\"dou-editor-board__preview\" role=\"img\" :aria-label=\"label\">\n <DouFlowEditor\n class=\"dou-editor-board__canvas\"\n inert\n v-if=\"seen\"\n ref=\"preview\"\n :model-value=\"shown\"\n v-bind=\"settings.editorProps\"\n :extensions=\"settings.extensions\"\n readonly\n fit-view\n :fit-min-zoom=\"0.05\"\n :simple-zoom=\"0\"\n :min-zoom=\"0.05\"\n :max-zoom=\"1\"\n :bordered=\"false\"\n :searchable=\"false\"\n :commentable=\"false\"\n :pan-on-scroll=\"false\"\n />\n </div>\n <div v-else class=\"dou-editor-board__empty\">\n <span class=\"dou-editor-board__empty-text\">{{ text.empty }}</span>\n <DouButton v-if=\"editable\" size=\"sm\" variant=\"outline\" @click=\"openEditor\">\n <template #leading><PencilIcon /></template>\n {{ text.start }}\n </DouButton>\n </div>\n\n <div v-if=\"!empty\" class=\"dou-editor-board__actions\">\n <DouButton size=\"sm\" variant=\"outline\" class=\"dou-editor-board__open\" @click=\"openEditor\">\n <template #leading><PencilIcon v-if=\"editable\" /><EyeIcon v-else /></template>\n {{ editable ? text.edit : text.view }}\n </DouButton>\n </div>\n\n <div\n v-if=\"editable\"\n class=\"dou-editor-board__resize\"\n role=\"separator\"\n aria-orientation=\"horizontal\"\n tabindex=\"0\"\n :aria-label=\"text.resize\"\n :aria-valuemin=\"BOARD_HEIGHT.min\"\n :aria-valuemax=\"BOARD_HEIGHT.max\"\n :aria-valuenow=\"height\"\n data-editor-control\n @pointerdown=\"onResizeStart\"\n @keydown=\"onResizeKey\"\n @dblclick.stop=\"resetHeight\"\n />\n\n <DouDialog\n v-model:open=\"open\"\n :title=\"label\"\n fullscreen\n destroy-on-close\n @opened=\"onOpened\"\n @close=\"settle\"\n @closed=\"onClosed\"\n >\n <div ref=\"canvas\" class=\"dou-editor-board__editor\" data-editor-control>\n <DouFlowEditor\n :model-value=\"board\"\n v-bind=\"settings.editorProps\"\n :extensions=\"extensions\"\n :readonly=\"!editable\"\n fit-view\n :fit-min-zoom=\"0.1\"\n :simple-zoom=\"0.1\"\n :commentable=\"false\"\n :aria-label=\"label\"\n @transaction=\"forward\"\n >\n <DouFlowControls />\n </DouFlowEditor>\n </div>\n </DouDialog>\n </div>\n</template>\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA6KA,IAAM,eAAe;;;;;;;;;;;;EAtIrB,SAAS,WAAW,OAAqB,QAA4C;GACnF,IAAI,CAAC,OAAO,MAAM,OAAO;GACzB,MAAM,QAAQ,MAAM,MAAM,KAAK,SAAS;IACtC,IAAI,CAAC,OAAO,IAAI,KAAK,EAAE,GAAG,OAAO;IACjC,MAAM,EAAE,WAAW,GAAG,GAAG,SAAS,KAAK,QAAQ,CAAC;IAChD,OAAO;KAAE,GAAG;KAAM,MAAM,OAAO,IAAI,KAAK,EAAE,IAAI;MAAE,GAAG;MAAM,WAAW;KAAK,IAAI;IAAK;GACpF,CAAC;GACD,OAAO;IAAE,GAAG;IAAO;GAAM;EAC3B;EAEA,MAAM,Q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 var BOARD_OPEN_EVENT = "dou-editor-board-open";
4
+ /** Heights a block may have in the document, in px. */
5
+ var BOARD_HEIGHT = {
6
+ min: 160,
7
+ max: 1200,
8
+ step: 40
9
+ };
10
+ /** The items of a list that are objects. */
11
+ var objects = (list) => Array.isArray(list) ? list.filter((item) => item && typeof item === "object" && !Array.isArray(item)) : [];
12
+ /**
13
+ * A block's `board` prop as a flow document; one that is missing or not
14
+ * shaped like one is read as an empty flow (nodes and edges that aren't
15
+ * objects are left out).
16
+ */
17
+ function boardOf(value) {
18
+ const source = value && typeof value === "object" && !Array.isArray(value) ? value : {};
19
+ return {
20
+ ...source,
21
+ version: 1,
22
+ nodes: objects(source.nodes),
23
+ edges: objects(source.edges)
24
+ };
25
+ }
26
+ var clampHeight = (height) => Math.round(Math.min(BOARD_HEIGHT.max, Math.max(BOARD_HEIGHT.min, height)));
27
+ /** A block's `height` prop in px: the default when it isn't a positive number. */
28
+ var heightOf = (value, fallback) => typeof value === "number" && value > 0 ? clampHeight(value) : fallback;
29
+ var isObject = (value) => !!value && typeof value === "object" && !Array.isArray(value);
30
+ /** What `checkDocument` reports of a block's props: a board or height it would read differently. */
31
+ function checkBoardProps(props) {
32
+ const problems = [];
33
+ const { board, height } = props;
34
+ if (board != null) {
35
+ if (!isObject(board)) problems.push({
36
+ prop: "board",
37
+ message: "The board is not a flow document; it would be shown empty"
38
+ });
39
+ else for (const list of ["nodes", "edges"]) {
40
+ const items = board[list];
41
+ if (items === void 0) continue;
42
+ if (!Array.isArray(items)) problems.push({
43
+ prop: `board.${list}`,
44
+ message: `"${list}" is not a list; it would be read as empty`
45
+ });
46
+ else if (items.some((item) => !isObject(item))) problems.push({
47
+ prop: `board.${list}`,
48
+ message: `Some ${list} are not objects; they would be left out`
49
+ });
50
+ }
51
+ }
52
+ if (height != null && !(typeof height === "number" && height > 0)) problems.push({
53
+ prop: "height",
54
+ message: `The height ${JSON.stringify(height)} is not a positive number; the default would be used`
55
+ });
56
+ else if (typeof height === "number" && (height < BOARD_HEIGHT.min || height > BOARD_HEIGHT.max)) problems.push({
57
+ prop: "height",
58
+ message: `The height ${height} is outside ${BOARD_HEIGHT.min}–${BOARD_HEIGHT.max}; it would be clamped`
59
+ });
60
+ return problems;
61
+ }
62
+ var endOf = (at) => at.pos + at.node.nodeSize;
63
+ /** A content node's attributes besides the board. */
64
+ var restOf = (node) => JSON.stringify({
65
+ ...node.attrs,
66
+ board: null
67
+ });
68
+ /**
69
+ * Whether `a` and `b` differ only in the `board` prop of the block `id`:
70
+ * an undo taken from the fullscreen editor must not change anything the
71
+ * person can't see there.
72
+ */
73
+ function onlyBoardChanged(a, b, id) {
74
+ const find = (doc) => {
75
+ let found = null;
76
+ doc.descendants((node, pos) => {
77
+ if (found) return false;
78
+ if (node.type.name === "block" && node.attrs.id === id) {
79
+ found = {
80
+ pos,
81
+ node
82
+ };
83
+ return false;
84
+ }
85
+ return true;
86
+ });
87
+ return found;
88
+ };
89
+ const before = find(a);
90
+ const after = find(b);
91
+ if (!before || !after) return false;
92
+ if (!a.slice(0, before.pos).eq(b.slice(0, after.pos))) return false;
93
+ if (!a.slice(endOf(before)).eq(b.slice(endOf(after)))) return false;
94
+ const [x, y] = [before.node.firstChild, after.node.firstChild];
95
+ if (x.type !== y.type) return false;
96
+ if (restOf(x) !== restOf(y)) return false;
97
+ if (before.node.childCount !== after.node.childCount) return false;
98
+ return before.node.childCount < 2 || before.node.child(1).eq(after.node.child(1));
99
+ }
100
+ export { BOARD_HEIGHT, BOARD_OPEN_EVENT, boardOf, checkBoardProps, clampHeight, heightOf, onlyBoardChanged };
101
+
102
+ //# sourceMappingURL=board.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"board.js","names":[],"sources":["../../src/flow/board.ts"],"sourcesContent":["import type { FlowDocument, FlowExtension } from '@dou-ui/flow'\nimport type { Node as PMNode } from 'prosemirror-model'\nimport type { Label } from '../core/extension'\nimport type { FlowBlockEditorProps } from './index'\n\n/** Sent to a block's element to open its fullscreen editor (`Enter` on the selected block). */\nexport const BOARD_OPEN_EVENT = 'dou-editor-board-open'\n\n/** Heights a block may have in the document, in px. */\nexport const BOARD_HEIGHT = { min: 160, max: 1200, step: 40 }\n\nexport interface BoardBlockSettings {\n label: Label\n extensions: (FlowExtension | FlowExtension[])[]\n editorProps: FlowBlockEditorProps\n height: number\n}\n\n/** The items of a list that are objects. */\nconst objects = <T>(list: unknown) =>\n Array.isArray(list)\n ? (list.filter((item) => item && typeof item === 'object' && !Array.isArray(item)) as T[])\n : []\n\n/**\n * A block's `board` prop as a flow document; one that is missing or not\n * shaped like one is read as an empty flow (nodes and edges that aren't\n * objects are left out).\n */\nexport function boardOf(value: unknown): FlowDocument {\n const source =\n value && typeof value === 'object' && !Array.isArray(value)\n ? (value as Partial<FlowDocument>)\n : {}\n return {\n ...source,\n version: 1,\n nodes: objects(source.nodes),\n edges: objects(source.edges),\n } as FlowDocument\n}\n\nexport const clampHeight = (height: number) =>\n Math.round(Math.min(BOARD_HEIGHT.max, Math.max(BOARD_HEIGHT.min, height)))\n\n/** A block's `height` prop in px: the default when it isn't a positive number. */\nexport const heightOf = (value: unknown, fallback: number) =>\n typeof value === 'number' && value > 0 ? clampHeight(value) : fallback\n\nconst isObject = (value: unknown): value is Record<string, unknown> =>\n !!value && typeof value === 'object' && !Array.isArray(value)\n\n/** What `checkDocument` reports of a block's props: a board or height it would read differently. */\nexport function checkBoardProps(props: Record<string, unknown>) {\n const problems: { prop: string; message: string }[] = []\n const { board, height } = props\n if (board != null) {\n if (!isObject(board))\n problems.push({\n prop: 'board',\n message: 'The board is not a flow document; it would be shown empty',\n })\n else\n for (const list of ['nodes', 'edges'] as const) {\n const items = board[list]\n if (items === undefined) continue\n if (!Array.isArray(items))\n problems.push({\n prop: `board.${list}`,\n message: `\"${list}\" is not a list; it would be read as empty`,\n })\n else if (items.some((item) => !isObject(item)))\n problems.push({\n prop: `board.${list}`,\n message: `Some ${list} are not objects; they would be left out`,\n })\n }\n }\n if (height != null && !(typeof height === 'number' && height > 0))\n problems.push({\n prop: 'height',\n message: `The height ${JSON.stringify(height)} is not a positive number; the default would be used`,\n })\n else if (typeof height === 'number' && (height < BOARD_HEIGHT.min || height > BOARD_HEIGHT.max))\n problems.push({\n prop: 'height',\n message: `The height ${height} is outside ${BOARD_HEIGHT.min}–${BOARD_HEIGHT.max}; it would be clamped`,\n })\n return problems\n}\n\ninterface Found {\n pos: number\n node: PMNode\n}\n\nconst endOf = (at: Found) => at.pos + at.node.nodeSize\n/** A content node's attributes besides the board. */\nconst restOf = (node: PMNode) => JSON.stringify({ ...node.attrs, board: null })\n\n/**\n * Whether `a` and `b` differ only in the `board` prop of the block `id`:\n * an undo taken from the fullscreen editor must not change anything the\n * person can't see there.\n */\nexport function onlyBoardChanged(a: PMNode, b: PMNode, id: string): boolean {\n const find = (doc: PMNode) => {\n let found: Found | null = null\n doc.descendants((node, pos) => {\n if (found) return false\n if (node.type.name === 'block' && node.attrs.id === id) {\n found = { pos, node }\n return false\n }\n return true\n })\n return found as Found | null\n }\n const before = find(a)\n const after = find(b)\n if (!before || !after) return false\n if (!a.slice(0, before.pos).eq(b.slice(0, after.pos))) return false\n if (!a.slice(endOf(before)).eq(b.slice(endOf(after)))) return false\n const [x, y] = [before.node.firstChild!, after.node.firstChild!]\n if (x.type !== y.type) return false\n if (restOf(x) !== restOf(y)) return false\n // the children of the block, if any\n if (before.node.childCount !== after.node.childCount) return false\n return before.node.childCount < 2 || before.node.child(1).eq(after.node.child(1))\n}\n"],"mappings":";;AAMA,IAAa,mBAAmB;;AAGhC,IAAa,eAAe;CAAE,KAAK;CAAK,KAAK;CAAM,MAAM;AAAG;;AAU5D,IAAM,WAAc,SAClB,MAAM,QAAQ,IAAI,IACb,KAAK,QAAQ,SAAS,QAAQ,OAAO,SAAS,YAAY,CAAC,MAAM,QAAQ,IAAI,CAAC,IAC/E,CAAC;;;;;;AAOP,SAAgB,QAAQ,OAA8B;CACpD,MAAM,SACJ,SAAS,OAAO,UAAU,YAAY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