@dou-ui/dashboard 0.1.0-alpha.11 → 0.1.0-alpha.13
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.
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { sameValue } from "../core/transaction.js";
|
|
2
|
+
import { keptIndexes, orderKeys } from "./order.js";
|
|
2
3
|
import { collaborationColors } from "dou-ui";
|
|
3
4
|
import { shallowRef } from "vue";
|
|
4
5
|
import * as Y from "yjs";
|
|
@@ -7,7 +8,8 @@ import * as Y from "yjs";
|
|
|
7
8
|
var ORDER = "$order";
|
|
8
9
|
/** The places by breakpoint, a map of their own, so places on two breakpoints don't overwrite each other. */
|
|
9
10
|
var PLACE = "place";
|
|
10
|
-
|
|
11
|
+
/** Whether two JSON values are the same, whatever the order of their keys. */
|
|
12
|
+
var same = sameValue;
|
|
11
13
|
/** Where in `value` something JSON can't carry sits, e.g. a component or a function. */
|
|
12
14
|
function unshareable(value, path) {
|
|
13
15
|
if (value === null || [
|
|
@@ -87,19 +89,159 @@ function writeOrder(map, widgets) {
|
|
|
87
89
|
if (fields && fields.get(ORDER) !== keys[index]) fields.set(ORDER, keys[index]);
|
|
88
90
|
});
|
|
89
91
|
}
|
|
92
|
+
/** Runs `write` as one transaction of the map's document, from `origin`. */
|
|
93
|
+
function transact(map, write, origin) {
|
|
94
|
+
if (map.doc) map.doc.transact(write, origin ?? null);
|
|
95
|
+
else write();
|
|
96
|
+
}
|
|
90
97
|
/**
|
|
91
98
|
* Writes a dashboard into an empty map, e.g. when a dashboard so far saved
|
|
92
99
|
* as JSON starts being edited together. Do it once, where the shared
|
|
93
100
|
* document is created; otherwise the first person to open the dashboard
|
|
94
101
|
* brings theirs.
|
|
95
102
|
*/
|
|
96
|
-
function fillMap(map, document) {
|
|
103
|
+
function fillMap(map, document, options = {}) {
|
|
97
104
|
const fill = () => {
|
|
98
105
|
for (const widget of document.widgets) writeWidget(map, widget);
|
|
99
106
|
writeOrder(map, document.widgets);
|
|
100
107
|
};
|
|
101
|
-
|
|
102
|
-
|
|
108
|
+
transact(map, fill, options.origin);
|
|
109
|
+
}
|
|
110
|
+
/** Writes the keys and places that differ between `before` and `after` into a widget's map. */
|
|
111
|
+
function writeDifference(fields, before, after) {
|
|
112
|
+
const a = before;
|
|
113
|
+
const b = after;
|
|
114
|
+
for (const key of /* @__PURE__ */ new Set([...Object.keys(a), ...Object.keys(b)])) {
|
|
115
|
+
if (key === "id" || key[0] === "$") continue;
|
|
116
|
+
if (key === PLACE) {
|
|
117
|
+
const from = before.place ?? {};
|
|
118
|
+
const to = after.place ?? {};
|
|
119
|
+
const found = fields.get(PLACE);
|
|
120
|
+
let places = found instanceof Y.Map ? found : null;
|
|
121
|
+
for (const name of /* @__PURE__ */ new Set([...Object.keys(from), ...Object.keys(to)])) {
|
|
122
|
+
if (same(from[name], to[name])) continue;
|
|
123
|
+
if (!places) fields.set(PLACE, places = new Y.Map());
|
|
124
|
+
const place = to[name];
|
|
125
|
+
if (place) places.set(name, { ...place });
|
|
126
|
+
else places.delete(name);
|
|
127
|
+
}
|
|
128
|
+
} else if (!same(a[key], b[key])) {
|
|
129
|
+
if (b[key] === void 0) fields.delete(key);
|
|
130
|
+
else {
|
|
131
|
+
warnUnshareable(after.id, key, b[key]);
|
|
132
|
+
fields.set(key, b[key]);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Gives `placing` (widgets added or moved in `next`) order keys among the
|
|
139
|
+
* widgets as they are ordered in the map now: right after the widget before
|
|
140
|
+
* them in `next`; those before the first widget that stays go right before
|
|
141
|
+
* it, and those after the last one go after all the others. Everyone else
|
|
142
|
+
* keeps their key.
|
|
143
|
+
*/
|
|
144
|
+
function placeOrder(map, next, placing) {
|
|
145
|
+
if (!placing.size) return;
|
|
146
|
+
const list = readMap(map).widgets.map((widget) => widget.id).filter((id) => !placing.has(id));
|
|
147
|
+
const listed = new Set(list);
|
|
148
|
+
const stays = (id) => listed.has(id);
|
|
149
|
+
let last = -1;
|
|
150
|
+
next.forEach((widget, index) => {
|
|
151
|
+
if (stays(widget.id)) last = index;
|
|
152
|
+
});
|
|
153
|
+
const leading = [];
|
|
154
|
+
const trailing = [];
|
|
155
|
+
const after = /* @__PURE__ */ new Map();
|
|
156
|
+
let first = null;
|
|
157
|
+
let previous = null;
|
|
158
|
+
next.forEach((widget, index) => {
|
|
159
|
+
if (stays(widget.id)) {
|
|
160
|
+
first ??= widget.id;
|
|
161
|
+
previous = widget.id;
|
|
162
|
+
} else if (placing.has(widget.id)) {
|
|
163
|
+
if (index > last) trailing.push(widget.id);
|
|
164
|
+
else if (previous === null) leading.push(widget.id);
|
|
165
|
+
else {
|
|
166
|
+
const attached = after.get(previous);
|
|
167
|
+
if (attached) attached.push(widget.id);
|
|
168
|
+
else after.set(previous, [widget.id]);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
});
|
|
172
|
+
const ordered = [];
|
|
173
|
+
for (const id of list) {
|
|
174
|
+
if (id === first) ordered.push(...leading);
|
|
175
|
+
ordered.push(id, ...after.get(id) ?? []);
|
|
176
|
+
}
|
|
177
|
+
ordered.push(...trailing);
|
|
178
|
+
const shared = ordered.map((id) => map.get(id));
|
|
179
|
+
const keys = orderKeys(shared.map((fields, index) => placing.has(ordered[index]) ? null : fields.get(ORDER) ?? null));
|
|
180
|
+
shared.forEach((fields, index) => {
|
|
181
|
+
if (fields.get(ORDER) !== keys[index]) fields.set(ORDER, keys[index]);
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* While developing: each widget `readMap` gave, as it was then, to tell a
|
|
186
|
+
* widget of `base` changed in place from one others changed meanwhile.
|
|
187
|
+
*/
|
|
188
|
+
var asRead = /* @__PURE__ */ new WeakMap();
|
|
189
|
+
/** The widgets of `next` that can be written: each id once, each with a type. */
|
|
190
|
+
function writable(next, base) {
|
|
191
|
+
const dev = process.env.NODE_ENV !== "production";
|
|
192
|
+
if (dev && base && (next === base || next.widgets === base.widgets)) console.warn("[@dou-ui/dashboard] updateMap got the same object as `next` and `base`, so nothing is written: change a copy of `base`, e.g. structuredClone(base).");
|
|
193
|
+
else if (dev && base) {
|
|
194
|
+
const kept = new Set(base.widgets);
|
|
195
|
+
for (const widget of next.widgets) {
|
|
196
|
+
const snapshot = kept.has(widget) ? asRead.get(widget) : void 0;
|
|
197
|
+
if (!snapshot || same(snapshot, widget)) continue;
|
|
198
|
+
console.warn(`[@dou-ui/dashboard] updateMap got the widget "${widget.id}" of \`base\` itself in \`next\`, changed in place: the change isn't written. Change a copy of \`base\`, e.g. structuredClone(base).`);
|
|
199
|
+
break;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
const seen = /* @__PURE__ */ new Set();
|
|
203
|
+
return next.widgets.filter((widget) => {
|
|
204
|
+
if (typeof widget.type !== "string") {
|
|
205
|
+
if (dev) console.warn(`[@dou-ui/dashboard] updateMap skips "${widget.id}": a widget needs a \`type\`.`);
|
|
206
|
+
return false;
|
|
207
|
+
}
|
|
208
|
+
if (seen.has(widget.id)) {
|
|
209
|
+
if (dev) console.warn(`[@dou-ui/dashboard] updateMap skips the second widget "${widget.id}": ids must be unique.`);
|
|
210
|
+
return false;
|
|
211
|
+
}
|
|
212
|
+
seen.add(widget.id);
|
|
213
|
+
return true;
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* Change a shared map from `options.base` to `next`, without a dashboard,
|
|
218
|
+
* e.g. on the server when a program wrote a new version while people edit
|
|
219
|
+
* the dashboard. Only what differs is written.
|
|
220
|
+
*/
|
|
221
|
+
function updateMap(map, next, options = {}) {
|
|
222
|
+
if (!map.doc) throw new Error("[@dou-ui/dashboard] updateMap needs a map of a Y.Doc");
|
|
223
|
+
const update = () => {
|
|
224
|
+
const base = options.base ?? readMap(map);
|
|
225
|
+
const widgets = writable(next, options.base);
|
|
226
|
+
const before = new Map(base.widgets.map((widget) => [widget.id, widget]));
|
|
227
|
+
const ids = new Set(next.widgets.map((widget) => widget.id));
|
|
228
|
+
for (const id of before.keys()) if (!ids.has(id)) map.delete(id);
|
|
229
|
+
const index = new Map(base.widgets.map((widget, i) => [widget.id, i]));
|
|
230
|
+
const common = widgets.filter((widget) => index.has(widget.id));
|
|
231
|
+
const kept = keptIndexes(common.map((widget) => String(index.get(widget.id)).padStart(10, "0")));
|
|
232
|
+
const placing = new Set(common.filter((_, i) => !kept.has(i)).map((widget) => widget.id));
|
|
233
|
+
for (const widget of widgets) {
|
|
234
|
+
const old = before.get(widget.id);
|
|
235
|
+
const fields = map.get(widget.id);
|
|
236
|
+
if (!old) {
|
|
237
|
+
writeWidget(map, widget);
|
|
238
|
+
placing.add(widget.id);
|
|
239
|
+
} else if (fields instanceof Y.Map) writeDifference(fields, old, widget);
|
|
240
|
+
else placing.delete(widget.id);
|
|
241
|
+
}
|
|
242
|
+
placeOrder(map, widgets, placing);
|
|
243
|
+
};
|
|
244
|
+
transact(map, update, options.origin);
|
|
103
245
|
}
|
|
104
246
|
/** The dashboard the map holds: its widgets by their order keys (then ids, the same for everyone). */
|
|
105
247
|
function readMap(map) {
|
|
@@ -119,9 +261,11 @@ function readMap(map) {
|
|
|
119
261
|
});
|
|
120
262
|
});
|
|
121
263
|
list.sort((a, b) => a.order < b.order ? -1 : a.order > b.order ? 1 : a.widget.id < b.widget.id ? -1 : 1);
|
|
264
|
+
const widgets = list.map((entry) => entry.widget);
|
|
265
|
+
if (process.env.NODE_ENV !== "production") for (const widget of widgets) asRead.set(widget, structuredClone(widget));
|
|
122
266
|
return {
|
|
123
267
|
version: 1,
|
|
124
|
-
widgets
|
|
268
|
+
widgets
|
|
125
269
|
};
|
|
126
270
|
}
|
|
127
271
|
/** The widgets and keys a transaction changed, written to the map. */
|
|
@@ -276,6 +420,6 @@ function collaboration(options) {
|
|
|
276
420
|
peers: awareness ? (id) => peersByWidget.value.get(id) ?? NO_PEERS : void 0
|
|
277
421
|
};
|
|
278
422
|
}
|
|
279
|
-
export { collaboration, collaborationColors, fillMap, readMap };
|
|
423
|
+
export { collaboration, collaborationColors, fillMap, readMap, updateMap };
|
|
280
424
|
|
|
281
425
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":[],"sources":["../../src/collaboration/index.ts"],"sourcesContent":["import { collaborationColors } from 'dou-ui'\nimport { shallowRef } from 'vue'\nimport * as Y from 'yjs'\nimport type {\n DashboardCollaboration,\n DashboardCollaborationHost,\n DashboardPeer,\n DashboardPresence,\n} from '../core/collaboration'\nimport type { DashboardTransaction } from '../core/transaction'\nimport type { DashboardDocument, DashboardWidget } from '../core/types'\nimport { orderKeys } from './order'\n\n/** The part of an `Awareness` (`y-protocols/awareness`) the dashboard uses. */\nexport interface AwarenessLike {\n clientID: number\n getLocalState(): Record<string, unknown> | null\n setLocalStateField(field: string, value: unknown): void\n getStates(): Map<number, Record<string, unknown>>\n on(event: 'change', listener: (...args: unknown[]) => void): void\n off(event: 'change', listener: (...args: unknown[]) => void): void\n}\n\n/** How the people editing together see each other. */\nexport interface CollaborationUser {\n name: string\n /** The color of this user's outlines, any CSS color. Picked from the client id when left out. */\n color?: string\n}\n\nexport interface CollaborationOptions {\n /** Where the widgets live, e.g. `ydoc.getMap('widgets')`. */\n widgets: Y.Map<unknown>\n /** Shares which widget each person is at, and what they do there (`y-protocols/awareness`). */\n awareness?: AwarenessLike\n /** This user, shown to the others. Needs `awareness`. */\n user?: CollaborationUser\n}\n\nexport { collaborationColors }\nexport type { DashboardPeer, DashboardPresence }\n\ntype Shared = Y.Map<unknown>\ntype Item = Record<string, unknown>\n\n/** A widget's place among the others, kept beside its keys. */\nconst ORDER = '$order'\n/** The places by breakpoint, a map of their own, so places on two breakpoints don't overwrite each other. */\nconst PLACE = 'place'\n\nconst same = (a: unknown, b: unknown) =>\n a === b ||\n (typeof a === 'object' && typeof b === 'object' && JSON.stringify(a) === JSON.stringify(b))\n\n/** Where in `value` something JSON can't carry sits, e.g. a component or a function. */\nfunction unshareable(value: unknown, path: string): string | null {\n if (value === null || ['string', 'number', 'boolean', 'undefined'].includes(typeof value))\n return null\n if (typeof value !== 'object') return path\n const proto = Object.getPrototypeOf(value)\n if (!Array.isArray(value) && proto !== Object.prototype && proto !== null) return path\n for (const [key, item] of Object.entries(value)) {\n const found = unshareable(item, Array.isArray(value) ? `${path}[${key}]` : `${path}.${key}`)\n if (found) return found\n }\n return null\n}\n\nconst warned = new Set<string>()\n/** Others would get a broken copy of such a value while this dashboard shows it fine. */\nfunction warnUnshareable(id: string, key: string, value: unknown) {\n // the app's bundler replaces this, as for Vue's own warnings\n if (process.env.NODE_ENV === 'production') return\n const path = unshareable(value, key)\n if (!path || warned.has(`${id}:${path}`)) return\n warned.add(`${id}:${path}`)\n console.warn(\n `[@dou-ui/dashboard] \"${id}\" has ${path}, which can't be shared: only JSON values reach the others.`,\n )\n}\n\n/** Writes the places that changed into a widget's place map. */\nfunction writePlace(fields: Shared, place: DashboardWidget['place']) {\n const shared = fields.get(PLACE)\n if (!(shared instanceof Y.Map)) {\n const map = new Y.Map<unknown>()\n for (const [name, value] of Object.entries(place)) if (value) map.set(name, { ...value })\n fields.set(PLACE, map)\n return\n }\n for (const [name, value] of Object.entries(place))\n if (value && !same(shared.get(name), value)) shared.set(name, { ...value })\n for (const name of Array.from(shared.keys())) if (!place[name]) shared.delete(name)\n}\n\n/** Writes a widget's keys besides its order, only those that changed; `keys` limits which. */\nfunction writeWidget(map: Shared, widget: DashboardWidget, keys?: Set<string>) {\n const existing = map.get(widget.id)\n const item = widget as unknown as Item\n if (!(existing instanceof Y.Map)) {\n // a new map can't be read before it is in the document: fill it, then put it in\n const fields = new Y.Map<unknown>()\n for (const [key, value] of Object.entries(item)) {\n if (key === 'id' || value === undefined) continue\n if (key === PLACE) {\n const places = new Y.Map<unknown>()\n for (const [name, place] of Object.entries(value as DashboardWidget['place']))\n if (place) places.set(name, { ...place })\n fields.set(PLACE, places)\n continue\n }\n warnUnshareable(widget.id, key, value)\n fields.set(key, value)\n }\n map.set(widget.id, fields)\n return\n }\n for (const [key, value] of Object.entries(item)) {\n if (key === 'id' || value === undefined || (keys && !keys.has(key))) continue\n if (key === PLACE) writePlace(existing, value as DashboardWidget['place'])\n else if (!same(existing.get(key), value)) {\n warnUnshareable(widget.id, key, value)\n existing.set(key, value)\n }\n }\n for (const key of Array.from(existing.keys()))\n if (key[0] !== '$' && item[key] === undefined && (!keys || keys.has(key))) existing.delete(key)\n}\n\n/** Writes each widget's order key, giving new keys only where it must. */\nfunction writeOrder(map: Shared, widgets: readonly DashboardWidget[]) {\n const shared = widgets.map((widget) => {\n const fields = map.get(widget.id)\n return fields instanceof Y.Map ? fields : null\n })\n const keys = orderKeys(shared.map((fields) => (fields?.get(ORDER) as string | undefined) ?? null))\n shared.forEach((fields, index) => {\n if (fields && fields.get(ORDER) !== keys[index]) fields.set(ORDER, keys[index])\n })\n}\n\n/**\n * Writes a dashboard into an empty map, e.g. when a dashboard so far saved\n * as JSON starts being edited together. Do it once, where the shared\n * document is created; otherwise the first person to open the dashboard\n * brings theirs.\n */\nexport function fillMap(map: Y.Map<unknown>, document: DashboardDocument) {\n const fill = () => {\n for (const widget of document.widgets) writeWidget(map, widget)\n writeOrder(map, document.widgets)\n }\n if (map.doc) map.doc.transact(fill)\n else fill()\n}\n\n/** The dashboard the map holds: its widgets by their order keys (then ids, the same for everyone). */\nexport function readMap(map: Y.Map<unknown>): DashboardDocument {\n const list: { widget: DashboardWidget; order: string }[] = []\n map.forEach((value, id) => {\n if (!(value instanceof Y.Map)) return\n const { [ORDER]: order, ...rest } = value.toJSON() as Item\n if (typeof rest.type !== 'string') return\n const place = rest[PLACE]\n list.push({\n widget: {\n ...rest,\n id,\n place: place && typeof place === 'object' ? place : {},\n } as DashboardWidget,\n order: typeof order === 'string' ? order : '',\n })\n })\n // oxlint-disable-next-line unicorn/no-array-sort -- sorts the list built here\n list.sort((a, b) =>\n a.order < b.order ? -1 : a.order > b.order ? 1 : a.widget.id < b.widget.id ? -1 : 1,\n )\n return { version: 1, widgets: list.map((entry) => entry.widget) }\n}\n\n/** The widgets and keys a transaction changed, written to the map. */\nfunction writeChanges(map: Shared, tr: DashboardTransaction) {\n const after = new Map(tr.doc.widgets.map((widget) => [widget.id, widget]))\n const touched = new Map<string, Set<string> | true>()\n let added = false\n let removed = false\n for (const step of tr.steps) {\n if (step.type === 'add') {\n touched.set(step.widget.id, true)\n added = true\n } else if (step.type === 'remove') {\n removed = true\n touched.delete(step.id)\n } else {\n const current = touched.get(step.id)\n if (current === true) continue\n const keys = current ?? new Set<string>()\n for (const key of Object.keys(step.patch)) keys.add(key)\n touched.set(step.id, keys)\n }\n }\n if (removed)\n for (const widget of tr.before.widgets) if (!after.has(widget.id)) map.delete(widget.id)\n for (const [id, keys] of touched) {\n const widget = after.get(id)\n if (!widget) continue\n // a widget removed and added again in one change is written anew\n if (keys === true && map.get(id) instanceof Y.Map && removed) map.delete(id)\n writeWidget(map, widget, keys === true ? undefined : keys)\n }\n if (added) writeOrder(map, tr.doc.widgets)\n}\n\nconst NO_PEERS: readonly DashboardPeer[] = []\n\n/**\n * Edit a dashboard together through a Yjs document: pass the result to\n * `DouDashboard` as `collaboration`. Each widget lives in `widgets` as its\n * own `Y.Map`, each key on its own and each breakpoint's place on its own,\n * so two people changing different widgets, or different settings of one,\n * both keep their change. When two people move widgets at once, everyone\n * lays the places out the same way, so the dashboards agree. While a widget\n * is dragged, changes from others wait until it is let go. The dashboard's\n * `v-model` only mirrors the shared document: a document set through\n * `v-model` from outside is not written back. Undo only takes back this\n * user's own changes. With `awareness`, each person sees which widget the\n * others are at, outlined in their colors, and whether they are moving,\n * resizing or changing its settings.\n */\nexport function collaboration(options: CollaborationOptions): DashboardCollaboration {\n const { widgets: map, awareness, user } = options\n const ydoc = map.doc\n if (!ydoc) throw new Error('[@dou-ui/dashboard] widgets must be a map of a Y.Doc')\n if (awareness && user) {\n const color = user.color ?? collaborationColors[ydoc.clientID % collaborationColors.length]\n awareness.setLocalStateField('user', { name: user.name, color })\n }\n\n /** This dashboard's own changes, which undo can take back. */\n const tracked = { dashboard: 'tracked' }\n /** Changes undo leaves alone, e.g. filling the map. */\n const untracked = { dashboard: 'untracked' }\n const undoManager = new Y.UndoManager([map], {\n trackedOrigins: new Set([tracked]),\n captureTimeout: 500,\n })\n /** Bumped when the undo stacks change, so `canUndo` and `canRedo` update. */\n const stacks = shallowRef(0)\n const bump = () => stacks.value++\n undoManager.on('stack-item-added', bump)\n undoManager.on('stack-item-popped', bump)\n undoManager.on('stack-cleared', bump)\n\n let host: DashboardCollaborationHost | undefined\n /** What undo or redo is running, to name the change. */\n let travelling: 'undo' | 'redo' | null = null\n let lastGroup: string | undefined\n\n /** Brings the dashboard in line with the map. */\n function pull() {\n host?.apply(readMap(map), travelling ?? 'remote')\n }\n\n function run(action: 'undo' | 'redo') {\n const stack = action === 'undo' ? undoManager.undoStack : undoManager.redoStack\n if (!stack.length) return false\n travelling = action\n try {\n undoManager[action]()\n } finally {\n travelling = null\n }\n return true\n }\n\n /** The others at each widget. */\n const peersByWidget = shallowRef(new Map<string, DashboardPeer[]>())\n function readPeers() {\n if (!awareness) return\n const next = new Map<string, DashboardPeer[]>()\n awareness.getStates().forEach((state, client) => {\n if (client === awareness.clientID) return\n const who = state.user as { name?: string; color?: string } | undefined\n const presence = state.dashboard as DashboardPresence | null | undefined\n if (!who || !presence?.widget) return\n const peer: DashboardPeer = {\n client,\n name: who.name ?? '',\n color: who.color ?? 'currentColor',\n action: presence.action,\n }\n const list = next.get(presence.widget)\n if (list) list.push(peer)\n else next.set(presence.widget, [peer])\n })\n peersByWidget.value = next\n }\n\n return {\n setup(dashboard) {\n host = dashboard\n // the first one to open the dashboard brings its widgets; everyone else reads the map\n if (!map.size) ydoc.transact(() => fillMap(map, dashboard.document()), untracked)\n pull()\n\n // changes from others, and from undo: applied once per Yjs transaction\n let dirty = false\n const observe = (_events: unknown, transaction: Y.Transaction) => {\n if (transaction.origin !== tracked && transaction.origin !== untracked) dirty = true\n }\n const flush = () => {\n if (!dirty) return\n dirty = false\n pull()\n }\n map.observeDeep(observe)\n ydoc.on('afterTransaction', flush)\n if (awareness) {\n readPeers()\n awareness.on('change', readPeers)\n }\n return () => {\n map.unobserveDeep(observe)\n ydoc.off('afterTransaction', flush)\n if (awareness) awareness.off('change', readPeers)\n host = undefined\n }\n },\n transaction(tr) {\n const tracking = tr.meta.history !== false\n // like the dashboard's own history: only changes of one group undo together\n if (tracking && (!tr.meta.group || tr.meta.group !== lastGroup)) undoManager.stopCapturing()\n if (tracking) lastGroup = tr.meta.group\n ydoc.transact(() => writeChanges(map, tr), tracking ? tracked : untracked)\n },\n sync: pull,\n undo: () => run('undo'),\n redo: () => run('redo'),\n canUndo: () => (stacks.value, undoManager.undoStack.length > 0),\n canRedo: () => (stacks.value, undoManager.redoStack.length > 0),\n presence: awareness\n ? (presence) => awareness.setLocalStateField('dashboard', presence)\n : undefined,\n peers: awareness ? (id) => peersByWidget.value.get(id) ?? NO_PEERS : undefined,\n }\n}\n"],"mappings":";;;;;;AA8CA,IAAM,QAAQ;;AAEd,IAAM,QAAQ;AAEd,IAAM,QAAQ,GAAY,MACxB,MAAM,KACL,OAAO,MAAM,YAAY,OAAO,MAAM,YAAY,KAAK,UAAU,CAAC,MAAM,KAAK,UAAU,CAAC;;AAG3F,SAAS,YAAY,OAAgB,MAA6B;CAChE,IAAI,UAAU,QAAQ;EAAC;EAAU;EAAU;EAAW;CAAW,CAAC,CAAC,SAAS,OAAO,KAAK,GACtF,OAAO;CACT,IAAI,OAAO,UAAU,UAAU,OAAO;CACtC,MAAM,QAAQ,OAAO,eAAe,KAAK;CACzC,IAAI,CAAC,MAAM,QAAQ,KAAK,KAAK,UAAU,OAAO,aAAa,UAAU,MAAM,OAAO;CAClF,KAAK,MAAM,CAAC,KAAK,SAAS,OAAO,QAAQ,KAAK,GAAG;EA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;GAGL,IAAI,QAAQ;GACZ,MAAM,WAAW,SAAkB,gBAA+B;IAChE,IAAI,YAAY,WAAW,WAAW,YAAY,WAAW,WAAW,QAAQ;GAClF;GACA,MAAM,cAAc;IAClB,IAAI,CAAC,OAAO;IACZ,QAAQ;IACR,KAAK;GACP;GACA,IAAI,YAAY,OAAO;GACvB,KAAK,GAAG,oBAAoB,KAAK;GACjC,IAAI,WAAW;IACb,UAAU;IACV,UAAU,GAAG,UAAU,SAAS;GAClC;GACA,aAAa;IACX,IAAI,cAAc,OAAO;IACzB,KAAK,IAAI,oBAAoB,KAAK;IAClC,IAAI,WAAW,UAAU,IAAI,UAAU,SAAS;IAChD,OAAO,KAAA;GACT;EACF;EACA,YAAY,IAAI;GACd,MAAM,WAAW,GAAG,KAAK,YAAY;GAErC,IAAI,aAAa,CAAC,GAAG,KAAK,SAAS,GAAG,KAAK,UAAU,YAAY,YAAY,cAAc;GAC3F,IAAI,UAAU,YAAY,GAAG,KAAK;GAClC,KAAK,eAAe,aAAa,KAAK,EAAE,GAAG,WAAW,UAAU,SAAS;EAC3E;EACA,MAAM;EACN,YAAY,IAAI,MAAM;EACtB,YAAY,IAAI,MAAM;EACtB,gBAAgB,OAAO,OAAO,YAAY,UAAU,SAAS;EAC7D,gBAAgB,OAAO,OAAO,YAAY,UAAU,SAAS;EAC7D,UAAU,aACL,aAAa,UAAU,mBAAmB,aAAa,QAAQ,IAChE,KAAA;EACJ,OAAO,aAAa,OAAO,cAAc,MAAM,IAAI,EAAE,KAAK,WAAW,KAAA;CACvE;AACF"}
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../../src/collaboration/index.ts"],"sourcesContent":["import { collaborationColors } from 'dou-ui'\nimport { shallowRef } from 'vue'\nimport * as Y from 'yjs'\nimport type {\n DashboardCollaboration,\n DashboardCollaborationHost,\n DashboardPeer,\n DashboardPresence,\n} from '../core/collaboration'\nimport { sameValue, type DashboardTransaction } from '../core/transaction'\nimport type { DashboardDocument, DashboardWidget } from '../core/types'\nimport { keptIndexes, orderKeys } from './order'\n\n/** The part of an `Awareness` (`y-protocols/awareness`) the dashboard uses. */\nexport interface AwarenessLike {\n clientID: number\n getLocalState(): Record<string, unknown> | null\n setLocalStateField(field: string, value: unknown): void\n getStates(): Map<number, Record<string, unknown>>\n on(event: 'change', listener: (...args: unknown[]) => void): void\n off(event: 'change', listener: (...args: unknown[]) => void): void\n}\n\n/** How the people editing together see each other. */\nexport interface CollaborationUser {\n name: string\n /** The color of this user's outlines, any CSS color. Picked from the client id when left out. */\n color?: string\n}\n\nexport interface CollaborationOptions {\n /** Where the widgets live, e.g. `ydoc.getMap('widgets')`. */\n widgets: Y.Map<unknown>\n /** Shares which widget each person is at, and what they do there (`y-protocols/awareness`). */\n awareness?: AwarenessLike\n /** This user, shown to the others. Needs `awareness`. */\n user?: CollaborationUser\n}\n\nexport { collaborationColors }\nexport type { DashboardPeer, DashboardPresence }\n\ntype Shared = Y.Map<unknown>\ntype Item = Record<string, unknown>\n\n/** A widget's place among the others, kept beside its keys. */\nconst ORDER = '$order'\n/** The places by breakpoint, a map of their own, so places on two breakpoints don't overwrite each other. */\nconst PLACE = 'place'\n\n/** Whether two JSON values are the same, whatever the order of their keys. */\nconst same = sameValue\n\n/** Where in `value` something JSON can't carry sits, e.g. a component or a function. */\nfunction unshareable(value: unknown, path: string): string | null {\n if (value === null || ['string', 'number', 'boolean', 'undefined'].includes(typeof value))\n return null\n if (typeof value !== 'object') return path\n const proto = Object.getPrototypeOf(value)\n if (!Array.isArray(value) && proto !== Object.prototype && proto !== null) return path\n for (const [key, item] of Object.entries(value)) {\n const found = unshareable(item, Array.isArray(value) ? `${path}[${key}]` : `${path}.${key}`)\n if (found) return found\n }\n return null\n}\n\nconst warned = new Set<string>()\n/** Others would get a broken copy of such a value while this dashboard shows it fine. */\nfunction warnUnshareable(id: string, key: string, value: unknown) {\n // the app's bundler replaces this, as for Vue's own warnings\n if (process.env.NODE_ENV === 'production') return\n const path = unshareable(value, key)\n if (!path || warned.has(`${id}:${path}`)) return\n warned.add(`${id}:${path}`)\n console.warn(\n `[@dou-ui/dashboard] \"${id}\" has ${path}, which can't be shared: only JSON values reach the others.`,\n )\n}\n\n/** Writes the places that changed into a widget's place map. */\nfunction writePlace(fields: Shared, place: DashboardWidget['place']) {\n const shared = fields.get(PLACE)\n if (!(shared instanceof Y.Map)) {\n const map = new Y.Map<unknown>()\n for (const [name, value] of Object.entries(place)) if (value) map.set(name, { ...value })\n fields.set(PLACE, map)\n return\n }\n for (const [name, value] of Object.entries(place))\n if (value && !same(shared.get(name), value)) shared.set(name, { ...value })\n for (const name of Array.from(shared.keys())) if (!place[name]) shared.delete(name)\n}\n\n/** Writes a widget's keys besides its order, only those that changed; `keys` limits which. */\nfunction writeWidget(map: Shared, widget: DashboardWidget, keys?: Set<string>) {\n const existing = map.get(widget.id)\n const item = widget as unknown as Item\n if (!(existing instanceof Y.Map)) {\n // a new map can't be read before it is in the document: fill it, then put it in\n const fields = new Y.Map<unknown>()\n for (const [key, value] of Object.entries(item)) {\n if (key === 'id' || value === undefined) continue\n if (key === PLACE) {\n const places = new Y.Map<unknown>()\n for (const [name, place] of Object.entries(value as DashboardWidget['place']))\n if (place) places.set(name, { ...place })\n fields.set(PLACE, places)\n continue\n }\n warnUnshareable(widget.id, key, value)\n fields.set(key, value)\n }\n map.set(widget.id, fields)\n return\n }\n for (const [key, value] of Object.entries(item)) {\n if (key === 'id' || value === undefined || (keys && !keys.has(key))) continue\n if (key === PLACE) writePlace(existing, value as DashboardWidget['place'])\n else if (!same(existing.get(key), value)) {\n warnUnshareable(widget.id, key, value)\n existing.set(key, value)\n }\n }\n for (const key of Array.from(existing.keys()))\n if (key[0] !== '$' && item[key] === undefined && (!keys || keys.has(key))) existing.delete(key)\n}\n\n/** Writes each widget's order key, giving new keys only where it must. */\nfunction writeOrder(map: Shared, widgets: readonly DashboardWidget[]) {\n const shared = widgets.map((widget) => {\n const fields = map.get(widget.id)\n return fields instanceof Y.Map ? fields : null\n })\n const keys = orderKeys(shared.map((fields) => (fields?.get(ORDER) as string | undefined) ?? null))\n shared.forEach((fields, index) => {\n if (fields && fields.get(ORDER) !== keys[index]) fields.set(ORDER, keys[index])\n })\n}\n\n/** Runs `write` as one transaction of the map's document, from `origin`. */\nfunction transact(map: Shared, write: () => void, origin: unknown) {\n if (map.doc) map.doc.transact(write, origin ?? null)\n else write()\n}\n\nexport interface FillMapOptions {\n /** The origin of the Yjs transaction, so the others can tell who wrote it. */\n origin?: unknown\n}\n\n/**\n * Writes a dashboard into an empty map, e.g. when a dashboard so far saved\n * as JSON starts being edited together. Do it once, where the shared\n * document is created; otherwise the first person to open the dashboard\n * brings theirs.\n */\nexport function fillMap(\n map: Y.Map<unknown>,\n document: DashboardDocument,\n options: FillMapOptions = {},\n) {\n const fill = () => {\n for (const widget of document.widgets) writeWidget(map, widget)\n writeOrder(map, document.widgets)\n }\n transact(map, fill, options.origin)\n}\n\n/** Writes the keys and places that differ between `before` and `after` into a widget's map. */\nfunction writeDifference(fields: Shared, before: DashboardWidget, after: DashboardWidget) {\n const a = before as unknown as Item\n const b = after as unknown as Item\n for (const key of new Set([...Object.keys(a), ...Object.keys(b)])) {\n if (key === 'id' || key[0] === '$') continue\n if (key === PLACE) {\n const from = before.place ?? {}\n const to = after.place ?? {}\n const found = fields.get(PLACE)\n let places = found instanceof Y.Map ? (found as Shared) : null\n for (const name of new Set([...Object.keys(from), ...Object.keys(to)])) {\n if (same(from[name], to[name])) continue\n if (!places) fields.set(PLACE, (places = new Y.Map<unknown>()))\n const place = to[name]\n if (place) places.set(name, { ...place })\n else places.delete(name)\n }\n } else if (!same(a[key], b[key])) {\n if (b[key] === undefined) fields.delete(key)\n else {\n warnUnshareable(after.id, key, b[key])\n fields.set(key, b[key])\n }\n }\n }\n}\n\n/**\n * Gives `placing` (widgets added or moved in `next`) order keys among the\n * widgets as they are ordered in the map now: right after the widget before\n * them in `next`; those before the first widget that stays go right before\n * it, and those after the last one go after all the others. Everyone else\n * keeps their key.\n */\nfunction placeOrder(map: Shared, next: readonly DashboardWidget[], placing: Set<string>) {\n if (!placing.size) return\n const list = readMap(map)\n .widgets.map((widget) => widget.id)\n .filter((id) => !placing.has(id))\n const listed = new Set(list)\n const stays = (id: string) => listed.has(id)\n let last = -1\n next.forEach((widget, index) => {\n if (stays(widget.id)) last = index\n })\n const leading: string[] = []\n const trailing: string[] = []\n const after = new Map<string, string[]>()\n let first: string | null = null\n let previous: string | null = null\n next.forEach((widget, index) => {\n if (stays(widget.id)) {\n first ??= widget.id\n previous = widget.id\n } else if (placing.has(widget.id)) {\n if (index > last) trailing.push(widget.id)\n else if (previous === null) leading.push(widget.id)\n else {\n const attached = after.get(previous)\n if (attached) attached.push(widget.id)\n else after.set(previous, [widget.id])\n }\n }\n })\n const ordered: string[] = []\n for (const id of list) {\n if (id === first) ordered.push(...leading)\n ordered.push(id, ...(after.get(id) ?? []))\n }\n ordered.push(...trailing)\n const shared = ordered.map((id) => map.get(id) as Shared)\n const keys = orderKeys(\n shared.map((fields, index) =>\n placing.has(ordered[index]!) ? null : ((fields.get(ORDER) as string | undefined) ?? null),\n ),\n )\n shared.forEach((fields, index) => {\n if (fields.get(ORDER) !== keys[index]) fields.set(ORDER, keys[index])\n })\n}\n\n/**\n * While developing: each widget `readMap` gave, as it was then, to tell a\n * widget of `base` changed in place from one others changed meanwhile.\n */\nconst asRead = new WeakMap<object, unknown>()\n\n/** The widgets of `next` that can be written: each id once, each with a type. */\nfunction writable(next: DashboardDocument, base: DashboardDocument | undefined) {\n const dev = process.env.NODE_ENV !== 'production'\n if (dev && base && (next === base || next.widgets === base.widgets))\n console.warn(\n '[@dou-ui/dashboard] updateMap got the same object as `next` and `base`, so nothing is written: change a copy of `base`, e.g. structuredClone(base).',\n )\n else if (dev && base) {\n // a widget of base that is no longer as `readMap` gave it was changed in place\n const kept = new Set<DashboardWidget>(base.widgets)\n for (const widget of next.widgets) {\n const snapshot = kept.has(widget) ? asRead.get(widget) : undefined\n if (!snapshot || same(snapshot, widget)) continue\n console.warn(\n `[@dou-ui/dashboard] updateMap got the widget \"${widget.id}\" of \\`base\\` itself in \\`next\\`, changed in place: the change isn't written. Change a copy of \\`base\\`, e.g. structuredClone(base).`,\n )\n break\n }\n }\n const seen = new Set<string>()\n return next.widgets.filter((widget) => {\n if (typeof widget.type !== 'string') {\n if (dev)\n console.warn(\n `[@dou-ui/dashboard] updateMap skips \"${widget.id}\": a widget needs a \\`type\\`.`,\n )\n return false\n }\n if (seen.has(widget.id)) {\n if (dev)\n console.warn(\n `[@dou-ui/dashboard] updateMap skips the second widget \"${widget.id}\": ids must be unique.`,\n )\n return false\n }\n seen.add(widget.id)\n return true\n })\n}\n\nexport interface UpdateMapOptions extends FillMapOptions {\n /**\n * The version `next` was made from, e.g. the one read with `readMap` and\n * given to a program that took a while to write `next`. Only what differs\n * between `base` and `next` is written: added and removed widgets, changed\n * keys, places on the breakpoints that changed, and the order of the\n * widgets that moved. What others changed meanwhile stays. Left out, the\n * map as it is now: then every widget not in `next` is removed.\n */\n base?: DashboardDocument\n}\n\n/**\n * Change a shared map from `options.base` to `next`, without a dashboard,\n * e.g. on the server when a program wrote a new version while people edit\n * the dashboard. Only what differs is written.\n */\nexport function updateMap(\n map: Y.Map<unknown>,\n next: DashboardDocument,\n options: UpdateMapOptions = {},\n) {\n // a map not in a document can't be read, so nothing could be merged\n if (!map.doc) throw new Error('[@dou-ui/dashboard] updateMap needs a map of a Y.Doc')\n const update = () => {\n const base = options.base ?? readMap(map)\n const widgets = writable(next, options.base)\n const before = new Map(base.widgets.map((widget) => [widget.id, widget]))\n const ids = new Set(next.widgets.map((widget) => widget.id))\n for (const id of before.keys()) if (!ids.has(id)) map.delete(id)\n\n // the widgets both have, in `base`'s order: those out of it in `next` moved\n const index = new Map(base.widgets.map((widget, i) => [widget.id, i]))\n const common = widgets.filter((widget) => index.has(widget.id))\n const kept = keptIndexes(common.map((widget) => String(index.get(widget.id)).padStart(10, '0')))\n const placing = new Set(common.filter((_, i) => !kept.has(i)).map((widget) => widget.id))\n\n for (const widget of widgets) {\n const old = before.get(widget.id)\n const fields = map.get(widget.id)\n if (!old) {\n writeWidget(map, widget)\n placing.add(widget.id)\n } else if (fields instanceof Y.Map) writeDifference(fields, old, widget)\n // removed meanwhile by someone else: it stays removed\n else placing.delete(widget.id)\n }\n placeOrder(map, widgets, placing)\n }\n transact(map, update, options.origin)\n}\n\n/** The dashboard the map holds: its widgets by their order keys (then ids, the same for everyone). */\nexport function readMap(map: Y.Map<unknown>): DashboardDocument {\n const list: { widget: DashboardWidget; order: string }[] = []\n map.forEach((value, id) => {\n if (!(value instanceof Y.Map)) return\n const { [ORDER]: order, ...rest } = value.toJSON() as Item\n if (typeof rest.type !== 'string') return\n const place = rest[PLACE]\n list.push({\n widget: {\n ...rest,\n id,\n place: place && typeof place === 'object' ? place : {},\n } as DashboardWidget,\n order: typeof order === 'string' ? order : '',\n })\n })\n // oxlint-disable-next-line unicorn/no-array-sort -- sorts the list built here\n list.sort((a, b) =>\n a.order < b.order ? -1 : a.order > b.order ? 1 : a.widget.id < b.widget.id ? -1 : 1,\n )\n const widgets = list.map((entry) => entry.widget)\n if (process.env.NODE_ENV !== 'production')\n for (const widget of widgets) asRead.set(widget, structuredClone(widget))\n return { version: 1, widgets }\n}\n\n/** The widgets and keys a transaction changed, written to the map. */\nfunction writeChanges(map: Shared, tr: DashboardTransaction) {\n const after = new Map(tr.doc.widgets.map((widget) => [widget.id, widget]))\n const touched = new Map<string, Set<string> | true>()\n let added = false\n let removed = false\n for (const step of tr.steps) {\n if (step.type === 'add') {\n touched.set(step.widget.id, true)\n added = true\n } else if (step.type === 'remove') {\n removed = true\n touched.delete(step.id)\n } else {\n const current = touched.get(step.id)\n if (current === true) continue\n const keys = current ?? new Set<string>()\n for (const key of Object.keys(step.patch)) keys.add(key)\n touched.set(step.id, keys)\n }\n }\n if (removed)\n for (const widget of tr.before.widgets) if (!after.has(widget.id)) map.delete(widget.id)\n for (const [id, keys] of touched) {\n const widget = after.get(id)\n if (!widget) continue\n // a widget removed and added again in one change is written anew\n if (keys === true && map.get(id) instanceof Y.Map && removed) map.delete(id)\n writeWidget(map, widget, keys === true ? undefined : keys)\n }\n if (added) writeOrder(map, tr.doc.widgets)\n}\n\nconst NO_PEERS: readonly DashboardPeer[] = []\n\n/**\n * Edit a dashboard together through a Yjs document: pass the result to\n * `DouDashboard` as `collaboration`. Each widget lives in `widgets` as its\n * own `Y.Map`, each key on its own and each breakpoint's place on its own,\n * so two people changing different widgets, or different settings of one,\n * both keep their change. When two people move widgets at once, everyone\n * lays the places out the same way, so the dashboards agree. While a widget\n * is dragged, changes from others wait until it is let go. The dashboard's\n * `v-model` only mirrors the shared document: a document set through\n * `v-model` from outside is not written back. Undo only takes back this\n * user's own changes. With `awareness`, each person sees which widget the\n * others are at, outlined in their colors, and whether they are moving,\n * resizing or changing its settings.\n */\nexport function collaboration(options: CollaborationOptions): DashboardCollaboration {\n const { widgets: map, awareness, user } = options\n const ydoc = map.doc\n if (!ydoc) throw new Error('[@dou-ui/dashboard] widgets must be a map of a Y.Doc')\n if (awareness && user) {\n const color = user.color ?? collaborationColors[ydoc.clientID % collaborationColors.length]\n awareness.setLocalStateField('user', { name: user.name, color })\n }\n\n /** This dashboard's own changes, which undo can take back. */\n const tracked = { dashboard: 'tracked' }\n /** Changes undo leaves alone, e.g. filling the map. */\n const untracked = { dashboard: 'untracked' }\n const undoManager = new Y.UndoManager([map], {\n trackedOrigins: new Set([tracked]),\n captureTimeout: 500,\n })\n /** Bumped when the undo stacks change, so `canUndo` and `canRedo` update. */\n const stacks = shallowRef(0)\n const bump = () => stacks.value++\n undoManager.on('stack-item-added', bump)\n undoManager.on('stack-item-popped', bump)\n undoManager.on('stack-cleared', bump)\n\n let host: DashboardCollaborationHost | undefined\n /** What undo or redo is running, to name the change. */\n let travelling: 'undo' | 'redo' | null = null\n let lastGroup: string | undefined\n\n /** Brings the dashboard in line with the map. */\n function pull() {\n host?.apply(readMap(map), travelling ?? 'remote')\n }\n\n function run(action: 'undo' | 'redo') {\n const stack = action === 'undo' ? undoManager.undoStack : undoManager.redoStack\n if (!stack.length) return false\n travelling = action\n try {\n undoManager[action]()\n } finally {\n travelling = null\n }\n return true\n }\n\n /** The others at each widget. */\n const peersByWidget = shallowRef(new Map<string, DashboardPeer[]>())\n function readPeers() {\n if (!awareness) return\n const next = new Map<string, DashboardPeer[]>()\n awareness.getStates().forEach((state, client) => {\n if (client === awareness.clientID) return\n const who = state.user as { name?: string; color?: string } | undefined\n const presence = state.dashboard as DashboardPresence | null | undefined\n if (!who || !presence?.widget) return\n const peer: DashboardPeer = {\n client,\n name: who.name ?? '',\n color: who.color ?? 'currentColor',\n action: presence.action,\n }\n const list = next.get(presence.widget)\n if (list) list.push(peer)\n else next.set(presence.widget, [peer])\n })\n peersByWidget.value = next\n }\n\n return {\n setup(dashboard) {\n host = dashboard\n // the first one to open the dashboard brings its widgets; everyone else reads the map\n if (!map.size) ydoc.transact(() => fillMap(map, dashboard.document()), untracked)\n pull()\n\n // changes from others, and from undo: applied once per Yjs transaction\n let dirty = false\n const observe = (_events: unknown, transaction: Y.Transaction) => {\n if (transaction.origin !== tracked && transaction.origin !== untracked) dirty = true\n }\n const flush = () => {\n if (!dirty) return\n dirty = false\n pull()\n }\n map.observeDeep(observe)\n ydoc.on('afterTransaction', flush)\n if (awareness) {\n readPeers()\n awareness.on('change', readPeers)\n }\n return () => {\n map.unobserveDeep(observe)\n ydoc.off('afterTransaction', flush)\n if (awareness) awareness.off('change', readPeers)\n host = undefined\n }\n },\n transaction(tr) {\n const tracking = tr.meta.history !== false\n // like the dashboard's own history: only changes of one group undo together\n if (tracking && (!tr.meta.group || tr.meta.group !== lastGroup)) undoManager.stopCapturing()\n if (tracking) lastGroup = tr.meta.group\n ydoc.transact(() => writeChanges(map, tr), tracking ? tracked : untracked)\n },\n sync: pull,\n undo: () => run('undo'),\n redo: () => run('redo'),\n canUndo: () => (stacks.value, undoManager.undoStack.length > 0),\n canRedo: () => (stacks.value, undoManager.redoStack.length > 0),\n presence: awareness\n ? (presence) => awareness.setLocalStateField('dashboard', presence)\n : undefined,\n peers: awareness ? (id) => peersByWidget.value.get(id) ?? NO_PEERS : undefined,\n }\n}\n"],"mappings":";;;;;;;AA8CA,IAAM,QAAQ;;AAEd,IAAM,QAAQ;;AAGd,IAAM,OAAO;;AAGb,SAAS,YAAY,OAAgB,MAA6B;CAChE,IAAI,UAAU,QAAQ;EAAC;EAAU;EAAU;EAAW;CAAW,CAAC,CAAC,SAAS,OAAO,KAAK,GACtF,OAAO;CACT,IAAI,OAAO,UAAU,UAAU,OAAO;CACtC,MAAM,QAAQ,OAAO,eAAe,KAAK;CACzC,IAAI,CAAC,MAAM,QAAQ,KAAK,KAAK,UAAU,OAAO,aAAa,UAAU,MAAM,OAAO;CAClF,KAAK,MAAM,CAAC,KAAK,SAAS,OAAO,QAAQ,KAAK,GAAG;EAC/C,MAAM,QAAQ,YAAY,MAAM,MAAM,QAAQ,KAAK,IAAI,GAAG,KAAK,GAAG,IAAI,KAAK,GAAG,KAAK,GAAG,KAAK;EAC3F,IAAI,OAAO,OAAO;CACpB;CACA,OAAO;AACT;AAEA,IAAM,yBAAS,IAAI,IAAY;;AAE/B,SAAS,gBAAgB,IAAY,KAAa,OAAgB;CAEhE,IAAA,QAAA,IAAA,aAA6B,cAAc;CAC3C,MAAM,OAAO,YAAY,OAAO,GAAG;CACnC,IAAI,CAAC,QAAQ,OAAO,IAAI,GAAG,GAAG,GAAG,MAAM,GAAG;CAC1C,OAAO,IAAI,GAAG,GAAG,GAAG,MAAM;CAC1B,QAAQ,KACN,wBAAwB,GAAG,QAAQ,KAAK,4DAC1C;AACF;;AAGA,SAAS,WAAW,QAAgB,OAAiC;CACnE,MAAM,SAAS,OAAO,IAAI,KAAK;CAC/B,IAAI,EAAE,kBAAkB,EAAE,MAAM;EAC9B,MAAM,MAAM,IAAI,EAAE,IAAa;EAC/B,KAAK,MAAM,CAAC,MAAM,U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AAU,SAAS;EAC7D,UAAU,aACL,aAAa,UAAU,mBAAmB,aAAa,QAAQ,IAChE,KAAA;EACJ,OAAO,aAAa,OAAO,cAAc,MAAM,IAAI,EAAE,KAAK,WAAW,KAAA;CACvE;AACF"}
|
package/dist/core/transaction.js
CHANGED
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
// src/core/transaction.ts
|
|
2
2
|
var PROTECTED = /* @__PURE__ */ new Set(["id", "type"]);
|
|
3
|
-
/**
|
|
3
|
+
/** The keys JSON keeps: those whose value isn't `undefined`. */
|
|
4
|
+
var jsonKeys = (value) => Object.keys(value).filter((key) => value[key] !== void 0);
|
|
5
|
+
/** Deep equality of JSON values; key order and keys set to `undefined` don't matter. */
|
|
4
6
|
function sameValue(a, b) {
|
|
5
7
|
if (a === b) return true;
|
|
6
8
|
if (!a || !b || typeof a !== "object" || typeof b !== "object") return false;
|
|
7
9
|
if (Array.isArray(a) !== Array.isArray(b)) return false;
|
|
8
|
-
|
|
9
|
-
|
|
10
|
+
if (Array.isArray(a)) return a.length === b.length && a.every((item, i) => sameValue(item, b[i]));
|
|
11
|
+
const keys = jsonKeys(a);
|
|
12
|
+
if (keys.length !== jsonKeys(b).length) return false;
|
|
10
13
|
return keys.every((key) => sameValue(a[key], b[key]));
|
|
11
14
|
}
|
|
12
15
|
/** Applies one step: the new document and the step that undoes it, or `null` when it changes nothing. */
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"transaction.js","names":[],"sources":["../../src/core/transaction.ts"],"sourcesContent":["import type { DashboardDocument, DashboardWidget } from './types'\n\n/**\n * Keys of a widget to set; `undefined` removes the key. `id` and `type`\n * cannot be set: remove the widget and add another instead.\n */\nexport type DashboardWidgetPatch = { [key: string]: unknown } & {\n id?: never\n type?: never\n}\n\n/** One change to a dashboard. `index` is where in `widgets` an added widget goes. */\nexport type DashboardStep =\n | { type: 'add'; widget: DashboardWidget; index: number }\n | { type: 'remove'; id: string }\n | { type: 'update'; id: string; patch: DashboardWidgetPatch }\n\nexport interface DashboardTransactionMeta {\n /** `false` keeps the change out of undo, e.g. a change from another person. */\n history?: boolean\n /** Changes of the same group within a second become one undo step, e.g. typing in a setting. */\n group?: string\n /** Where the change came from: `layout`, `menu`, `keyboard`, `settings`, `api`… */\n origin?: string\n [key: string]: unknown\n}\n\nconst PROTECTED = new Set(['id', 'type'])\n\nexport interface ReplaceWidgetsOptions {\n /** A widget that stays keeps its `comments`; `true` by default. */\n keepComments?: boolean\n}\n\n/** Deep equality of JSON values; key order doesn't matter. */\nexport function sameValue(a: unknown, b: unknown): boolean {\n if (a === b) return true\n if (!a || !b || typeof a !== 'object' || typeof b !== 'object') return false\n if (Array.isArray(a) !== Array.isArray(b)) return false\n const keys = Object.keys(a)\n if (keys.length !== Object.keys(b).length) return false\n return keys.every((key) =>\n sameValue((a as Record<string, unknown>)[key], (b as Record<string, unknown>)[key]),\n )\n}\n\n/** Applies one step: the new document and the step that undoes it, or `null` when it changes nothing. */\nfunction applyStep(\n document: DashboardDocument,\n step: DashboardStep,\n): [DashboardDocument, DashboardStep, DashboardStep] | null {\n const widgets = document.widgets\n const indexOf = (id: string) => widgets.findIndex((widget) => widget.id === id)\n if (step.type === 'add') {\n if (indexOf(step.widget.id) >= 0) return null\n const index = Math.max(0, Math.min(widgets.length, step.index))\n const next = [...widgets.slice(0, index), step.widget, ...widgets.slice(index)]\n return [\n { ...document, widgets: next },\n { ...step, index },\n { type: 'remove', id: step.widget.id },\n ]\n }\n const index = indexOf(step.id)\n if (index < 0) return null\n const widget = widgets[index]!\n if (step.type === 'remove') {\n const next = [...widgets.slice(0, index), ...widgets.slice(index + 1)]\n return [{ ...document, widgets: next }, step, { type: 'add', widget, index }]\n }\n const old = widget as unknown as Record<string, unknown>\n const changed: Record<string, unknown> = { ...old }\n const patch: Record<string, unknown> = {}\n const undo: Record<string, unknown> = {}\n for (const [key, value] of Object.entries(step.patch)) {\n if (PROTECTED.has(key))\n throw new Error(`[@dou-ui/dashboard] a widget's \"${key}\" cannot be changed`)\n if (sameValue(old[key], value)) continue\n patch[key] = value\n undo[key] = old[key]\n if (value === undefined) delete changed[key]\n else changed[key] = value\n }\n if (!Object.keys(patch).length) return null\n const next = widgets.slice()\n next[index] = changed as unknown as DashboardWidget\n return [\n { ...document, widgets: next },\n { type: 'update', id: step.id, patch: patch as DashboardWidgetPatch },\n { type: 'update', id: step.id, patch: undo as DashboardWidgetPatch },\n ]\n}\n\n/**\n * Changes to a dashboard, made step by step and then handed to the\n * dashboard's `dispatch` as one change (one undo step). A step that changes\n * nothing (a widget that isn't there, a patch to the same values, an id\n * already in use) is left out.\n *\n * ```ts\n * const tr = dashboard.value.transaction()\n * tr.update('orders', { title: '本周订单' }).remove('memo')\n * dashboard.value.dispatch(tr)\n * ```\n */\nexport class DashboardTransaction {\n /** The document after the steps so far. */\n doc: DashboardDocument\n readonly steps: DashboardStep[] = []\n /** The steps that undo the ones so far, last one first. */\n readonly inverse: DashboardStep[] = []\n readonly meta: DashboardTransactionMeta = {}\n\n constructor(\n /** The document the transaction starts from. */\n readonly before: DashboardDocument,\n ) {\n this.doc = before\n }\n\n /** Whether any step was made. */\n get changed() {\n return this.steps.length > 0\n }\n\n /** Apply a step as it is. */\n step(step: DashboardStep) {\n const result = applyStep(this.doc, step)\n if (!result) return this\n this.doc = result[0]\n this.steps.push(result[1])\n this.inverse.unshift(result[2])\n return this\n }\n\n /** Put a widget into the document at `index` (the end by default). */\n add(widget: DashboardWidget, index = Infinity) {\n return this.step({ type: 'add', widget, index })\n }\n\n /** Take a widget out. */\n remove(id: string) {\n return this.step({ type: 'remove', id })\n }\n\n /** Set keys of a widget, e.g. `{ title }`, `{ props }` or `{ place }`; `undefined` removes a key. */\n update(id: string, patch: DashboardWidgetPatch) {\n return this.step({ type: 'update', id, patch })\n }\n\n setMeta(key: string, value: unknown) {\n this.meta[key] = value\n return this\n }\n}\n\n/**\n * Applies steps to a document in one go, giving the transaction: its `doc`\n * (unchanged widgets are kept as they are) and the `inverse` steps that\n * undo it. For the server, tests, or saved steps.\n */\nexport function applySteps(\n document: DashboardDocument,\n steps: readonly DashboardStep[],\n meta: DashboardTransactionMeta = {},\n): DashboardTransaction {\n const tr = new DashboardTransaction(document)\n Object.assign(tr.meta, meta)\n for (const step of steps) tr.step(step)\n return tr\n}\n\n/** A deep copy of a JSON value (also of a reactive one). */\nexport const cloneJson = <T>(value: T): T => JSON.parse(JSON.stringify(value)) as T\n\n/** The indexes of the longest strictly increasing run of `values`. */\nfunction longestRun(values: readonly number[]): Set<number> {\n const tails: number[] = []\n const previous: number[] = []\n values.forEach((value, index) => {\n let low = 0\n let high = tails.length\n while (low < high) {\n const mid = (low + high) >> 1\n if (values[tails[mid]!]! < value) low = mid + 1\n else high = mid\n }\n previous[index] = low > 0 ? tails[low - 1]! : -1\n tails[low] = index\n })\n const kept = new Set<number>()\n let index = tails.length ? tails[tails.length - 1]! : -1\n while (index >= 0) {\n kept.add(index)\n index = previous[index]!\n }\n return kept\n}\n\n/**\n * Adds the steps that turn the transaction's document into `next`: widgets\n * that are gone are removed, new ones added, changed keys updated, and the\n * fewest widgets taken out and put back to match the order. Unchanged\n * widgets keep their objects.\n *\n * A widget that stays keeps its `comments`, the discussions on it, as they\n * are: restoring a default layout doesn't take them off. Pass\n * `{ keepComments: false }` for the exact steps, e.g. to apply a shared\n * document.\n */\nexport function replaceWidgets(\n tr: DashboardTransaction,\n next: DashboardDocument,\n options: ReplaceWidgetsOptions = {},\n) {\n const keep = options.keepComments !== false\n const wanted = new Map(next.widgets.map((widget, index) => [widget.id, index]))\n for (const widget of tr.doc.widgets) {\n const index = wanted.get(widget.id)\n // a widget whose type changed is a new widget\n if (index === undefined || next.widgets[index]!.type !== widget.type) tr.remove(widget.id)\n }\n const current = tr.doc.widgets\n const kept = longestRun(current.map((widget) => wanted.get(widget.id)!))\n for (const [index, widget] of current.entries()) if (!kept.has(index)) tr.remove(widget.id)\n next.widgets.forEach((widget, index) => {\n const there = tr.doc.widgets[index]\n if (there?.id !== widget.id) return void tr.add(widget, index)\n const patch: Record<string, unknown> = {}\n const old = there as unknown as Record<string, unknown>\n const item = widget as unknown as Record<string, unknown>\n for (const key of new Set([...Object.keys(old), ...Object.keys(item)]))\n if (!PROTECTED.has(key) && !(keep && key === 'comments')) patch[key] = item[key]\n tr.update(widget.id, patch as DashboardWidgetPatch)\n })\n return tr\n}\n"],"mappings":";AA2BA,IAAM,4BAAY,IAAI,IAAI,CAAC,MAAM,MAAM,CAAC;;AAQxC,SAAgB,UAAU,GAAY,GAAqB;CACzD,IAAI,MAAM,GAAG,OAAO;CACpB,IAAI,CAAC,KAAK,CAAC,KAAK,OAAO,MAAM,YAAY,OAAO,MAAM,UAAU,OAAO;CACvE,IAAI,MAAM,QAAQ,CAAC,MAAM,MAAM,QAAQ,CAAC,GAAG,OAAO;CAClD,MAAM,OAAO,OAAO,KAAK,CAAC;CAC1B,IAAI,KAAK,WAAW,OAAO,KAAK,CAAC,CAAC,CAAC,QAAQ,OAAO;CAClD,OAAO,KAAK,OAAO,QACjB,UAAW,EAA8B,MAAO,EAA8B,IAAI,CACpF;AACF;;AAGA,SAAS,UACP,UACA,MAC0D;CAC1D,MAAM,UAAU,SAAS;CACzB,MAAM,WAAW,OAAe,QAAQ,WAAW,WAAW,OAAO,OAAO,EAAE;CAC9E,IAAI,KAAK,SAAS,OAAO;EACvB,IAAI,QAAQ,KAAK,OAAO,EAAE,KAAK,GAAG,OAAO;EACzC,MAAM,QAAQ,KAAK,IAAI,GAAG,KAAK,IAAI,QAAQ,QAAQ,KAAK,KAAK,CAAC;EAC9D,MAAM,OAAO;GAAC,GAAG,QAAQ,MAAM,GAAG,KAAK;GAAG,KAAK;GAAQ,GAAG,QAAQ,MAAM,KAAK;EAAC;EAC9E,OAAO;GACL;IAAE,GAAG;IAAU,SAAS;GAAK;GAC7B;IAAE,GAAG;IAAM;GAAM;GACjB;IAAE,MAAM;IAAU,IAAI,KAAK,OAAO;GAAG;EACvC;CACF;CACA,MAAM,QAA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/transaction.ts"],"sourcesContent":["import type { DashboardDocument, DashboardWidget } from './types'\n\n/**\n * Keys of a widget to set; `undefined` removes the key. `id` and `type`\n * cannot be set: remove the widget and add another instead.\n */\nexport type DashboardWidgetPatch = { [key: string]: unknown } & {\n id?: never\n type?: never\n}\n\n/** One change to a dashboard. `index` is where in `widgets` an added widget goes. */\nexport type DashboardStep =\n | { type: 'add'; widget: DashboardWidget; index: number }\n | { type: 'remove'; id: string }\n | { type: 'update'; id: string; patch: DashboardWidgetPatch }\n\nexport interface DashboardTransactionMeta {\n /** `false` keeps the change out of undo, e.g. a change from another person. */\n history?: boolean\n /** Changes of the same group within a second become one undo step, e.g. typing in a setting. */\n group?: string\n /** Where the change came from: `layout`, `menu`, `keyboard`, `settings`, `api`… */\n origin?: string\n [key: string]: unknown\n}\n\nconst PROTECTED = new Set(['id', 'type'])\n\nexport interface ReplaceWidgetsOptions {\n /** A widget that stays keeps its `comments`; `true` by default. */\n keepComments?: boolean\n}\n\n/** The keys JSON keeps: those whose value isn't `undefined`. */\nconst jsonKeys = (value: object) =>\n Object.keys(value).filter((key) => (value as Record<string, unknown>)[key] !== undefined)\n\n/** Deep equality of JSON values; key order and keys set to `undefined` don't matter. */\nexport function sameValue(a: unknown, b: unknown): boolean {\n if (a === b) return true\n if (!a || !b || typeof a !== 'object' || typeof b !== 'object') return false\n if (Array.isArray(a) !== Array.isArray(b)) return false\n if (Array.isArray(a))\n return (\n a.length === (b as unknown[]).length &&\n a.every((item, i) => sameValue(item, (b as unknown[])[i]))\n )\n const keys = jsonKeys(a)\n if (keys.length !== jsonKeys(b).length) return false\n return keys.every((key) =>\n sameValue((a as Record<string, unknown>)[key], (b as Record<string, unknown>)[key]),\n )\n}\n\n/** Applies one step: the new document and the step that undoes it, or `null` when it changes nothing. */\nfunction applyStep(\n document: DashboardDocument,\n step: DashboardStep,\n): [DashboardDocument, DashboardStep, DashboardStep] | null {\n const widgets = document.widgets\n const indexOf = (id: string) => widgets.findIndex((widget) => widget.id === id)\n if (step.type === 'add') {\n if (indexOf(step.widget.id) >= 0) return null\n const index = Math.max(0, Math.min(widgets.length, step.index))\n const next = [...widgets.slice(0, index), step.widget, ...widgets.slice(index)]\n return [\n { ...document, widgets: next },\n { ...step, index },\n { type: 'remove', id: step.widget.id },\n ]\n }\n const index = indexOf(step.id)\n if (index < 0) return null\n const widget = widgets[index]!\n if (step.type === 'remove') {\n const next = [...widgets.slice(0, index), ...widgets.slice(index + 1)]\n return [{ ...document, widgets: next }, step, { type: 'add', widget, index }]\n }\n const old = widget as unknown as Record<string, unknown>\n const changed: Record<string, unknown> = { ...old }\n const patch: Record<string, unknown> = {}\n const undo: Record<string, unknown> = {}\n for (const [key, value] of Object.entries(step.patch)) {\n if (PROTECTED.has(key))\n throw new Error(`[@dou-ui/dashboard] a widget's \"${key}\" cannot be changed`)\n if (sameValue(old[key], value)) continue\n patch[key] = value\n undo[key] = old[key]\n if (value === undefined) delete changed[key]\n else changed[key] = value\n }\n if (!Object.keys(patch).length) return null\n const next = widgets.slice()\n next[index] = changed as unknown as DashboardWidget\n return [\n { ...document, widgets: next },\n { type: 'update', id: step.id, patch: patch as DashboardWidgetPatch },\n { type: 'update', id: step.id, patch: undo as DashboardWidgetPatch },\n ]\n}\n\n/**\n * Changes to a dashboard, made step by step and then handed to the\n * dashboard's `dispatch` as one change (one undo step). A step that changes\n * nothing (a widget that isn't there, a patch to the same values, an id\n * already in use) is left out.\n *\n * ```ts\n * const tr = dashboard.value.transaction()\n * tr.update('orders', { title: '本周订单' }).remove('memo')\n * dashboard.value.dispatch(tr)\n * ```\n */\nexport class DashboardTransaction {\n /** The document after the steps so far. */\n doc: DashboardDocument\n readonly steps: DashboardStep[] = []\n /** The steps that undo the ones so far, last one first. */\n readonly inverse: DashboardStep[] = []\n readonly meta: DashboardTransactionMeta = {}\n\n constructor(\n /** The document the transaction starts from. */\n readonly before: DashboardDocument,\n ) {\n this.doc = before\n }\n\n /** Whether any step was made. */\n get changed() {\n return this.steps.length > 0\n }\n\n /** Apply a step as it is. */\n step(step: DashboardStep) {\n const result = applyStep(this.doc, step)\n if (!result) return this\n this.doc = result[0]\n this.steps.push(result[1])\n this.inverse.unshift(result[2])\n return this\n }\n\n /** Put a widget into the document at `index` (the end by default). */\n add(widget: DashboardWidget, index = Infinity) {\n return this.step({ type: 'add', widget, index })\n }\n\n /** Take a widget out. */\n remove(id: string) {\n return this.step({ type: 'remove', id })\n }\n\n /** Set keys of a widget, e.g. `{ title }`, `{ props }` or `{ place }`; `undefined` removes a key. */\n update(id: string, patch: DashboardWidgetPatch) {\n return this.step({ type: 'update', id, patch })\n }\n\n setMeta(key: string, value: unknown) {\n this.meta[key] = value\n return this\n }\n}\n\n/**\n * Applies steps to a document in one go, giving the transaction: its `doc`\n * (unchanged widgets are kept as they are) and the `inverse` steps that\n * undo it. For the server, tests, or saved steps.\n */\nexport function applySteps(\n document: DashboardDocument,\n steps: readonly DashboardStep[],\n meta: DashboardTransactionMeta = {},\n): DashboardTransaction {\n const tr = new DashboardTransaction(document)\n Object.assign(tr.meta, meta)\n for (const step of steps) tr.step(step)\n return tr\n}\n\n/** A deep copy of a JSON value (also of a reactive one). */\nexport const cloneJson = <T>(value: T): T => JSON.parse(JSON.stringify(value)) as T\n\n/** The indexes of the longest strictly increasing run of `values`. */\nfunction longestRun(values: readonly number[]): Set<number> {\n const tails: number[] = []\n const previous: number[] = []\n values.forEach((value, index) => {\n let low = 0\n let high = tails.length\n while (low < high) {\n const mid = (low + high) >> 1\n if (values[tails[mid]!]! < value) low = mid + 1\n else high = mid\n }\n previous[index] = low > 0 ? tails[low - 1]! : -1\n tails[low] = index\n })\n const kept = new Set<number>()\n let index = tails.length ? tails[tails.length - 1]! : -1\n while (index >= 0) {\n kept.add(index)\n index = previous[index]!\n }\n return kept\n}\n\n/**\n * Adds the steps that turn the transaction's document into `next`: widgets\n * that are gone are removed, new ones added, changed keys updated, and the\n * fewest widgets taken out and put back to match the order. Unchanged\n * widgets keep their objects.\n *\n * A widget that stays keeps its `comments`, the discussions on it, as they\n * are: restoring a default layout doesn't take them off. Pass\n * `{ keepComments: false }` for the exact steps, e.g. to apply a shared\n * document.\n */\nexport function replaceWidgets(\n tr: DashboardTransaction,\n next: DashboardDocument,\n options: ReplaceWidgetsOptions = {},\n) {\n const keep = options.keepComments !== false\n const wanted = new Map(next.widgets.map((widget, index) => [widget.id, index]))\n for (const widget of tr.doc.widgets) {\n const index = wanted.get(widget.id)\n // a widget whose type changed is a new widget\n if (index === undefined || next.widgets[index]!.type !== widget.type) tr.remove(widget.id)\n }\n const current = tr.doc.widgets\n const kept = longestRun(current.map((widget) => wanted.get(widget.id)!))\n for (const [index, widget] of current.entries()) if (!kept.has(index)) tr.remove(widget.id)\n next.widgets.forEach((widget, index) => {\n const there = tr.doc.widgets[index]\n if (there?.id !== widget.id) return void tr.add(widget, index)\n const patch: Record<string, unknown> = {}\n const old = there as unknown as Record<string, unknown>\n const item = widget as unknown as Record<string, unknown>\n for (const key of new Set([...Object.keys(old), ...Object.keys(item)]))\n if (!PROTECTED.has(key) && !(keep && key === 'comments')) patch[key] = item[key]\n tr.update(widget.id, patch as DashboardWidgetPatch)\n })\n return tr\n}\n"],"mappings":";AA2BA,IAAM,4BAAY,IAAI,IAAI,CAAC,MAAM,MAAM,CAAC;;AAQxC,IAAM,YAAY,UAChB,OAAO,KAAK,KAAK,CAAC,CAAC,QAAQ,QAAS,MAAkC,SAAS,KAAA,CAAS;;AAG1F,SAAgB,UAAU,GAAY,GAAqB;CACzD,IAAI,MAAM,GAAG,OAAO;CACpB,IAAI,CAAC,KAAK,CAAC,KAAK,OAAO,MAAM,YAAY,OAAO,MAAM,UAAU,OAAO;CACvE,IAAI,MAAM,QAAQ,CAAC,MAAM,MAAM,QAAQ,CAAC,GAAG,OAAO;CAClD,IAAI,MAAM,QAAQ,CAAC,GACjB,OACE,EAAE,WAAY,EAAgB,UAC9B,EAAE,OAAO,MAAM,MAAM,UAAU,MAAO,EAAgB,EAAE,CAAC;C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OAAO,WAAW,QAAQ,KAAK,WAAW,OAAO,IAAI,OAAO,EAAE,CAAE,CAAC;CACvE,KAAK,MAAM,CAAC,OAAO,WAAW,QAAQ,QAAQ,GAAG,IAAI,CAAC,KAAK,IAAI,KAAK,GAAG,GAAG,OAAO,OAAO,EAAE;CAC1F,KAAK,QAAQ,SAAS,QAAQ,UAAU;EACtC,MAAM,QAAQ,GAAG,IAAI,QAAQ;EAC7B,IAAI,OAAO,OAAO,OAAO,IAAI,OAAO,KAAK,GAAG,IAAI,QAAQ,KAAK;EAC7D,MAAM,QAAiC,CAAC;EACxC,MAAM,MAAM;EACZ,MAAM,OAAO;EACb,KAAK,MAAM,uBAAO,IAAI,IAAI,CAAC,GAAG,OAAO,KAAK,GAAG,GAAG,GAAG,OAAO,KAAK,IAAI,CAAC,CAAC,GACnE,IAAI,CAAC,UAAU,IAAI,GAAG,KAAK,EAAE,QAAQ,QAAQ,aAAa,MAAM,OAAO,KAAK;EAC9E,GAAG,OAAO,OAAO,IAAI,KAA6B;CACpD,CAAC;CACD,OAAO;AACT"}
|
|
@@ -27,13 +27,34 @@ export interface CollaborationOptions {
|
|
|
27
27
|
}
|
|
28
28
|
export { collaborationColors };
|
|
29
29
|
export type { DashboardPeer, DashboardPresence };
|
|
30
|
+
export interface FillMapOptions {
|
|
31
|
+
/** The origin of the Yjs transaction, so the others can tell who wrote it. */
|
|
32
|
+
origin?: unknown;
|
|
33
|
+
}
|
|
30
34
|
/**
|
|
31
35
|
* Writes a dashboard into an empty map, e.g. when a dashboard so far saved
|
|
32
36
|
* as JSON starts being edited together. Do it once, where the shared
|
|
33
37
|
* document is created; otherwise the first person to open the dashboard
|
|
34
38
|
* brings theirs.
|
|
35
39
|
*/
|
|
36
|
-
export declare function fillMap(map: Y.Map<unknown>, document: DashboardDocument): void;
|
|
40
|
+
export declare function fillMap(map: Y.Map<unknown>, document: DashboardDocument, options?: FillMapOptions): void;
|
|
41
|
+
export interface UpdateMapOptions extends FillMapOptions {
|
|
42
|
+
/**
|
|
43
|
+
* The version `next` was made from, e.g. the one read with `readMap` and
|
|
44
|
+
* given to a program that took a while to write `next`. Only what differs
|
|
45
|
+
* between `base` and `next` is written: added and removed widgets, changed
|
|
46
|
+
* keys, places on the breakpoints that changed, and the order of the
|
|
47
|
+
* widgets that moved. What others changed meanwhile stays. Left out, the
|
|
48
|
+
* map as it is now: then every widget not in `next` is removed.
|
|
49
|
+
*/
|
|
50
|
+
base?: DashboardDocument;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Change a shared map from `options.base` to `next`, without a dashboard,
|
|
54
|
+
* e.g. on the server when a program wrote a new version while people edit
|
|
55
|
+
* the dashboard. Only what differs is written.
|
|
56
|
+
*/
|
|
57
|
+
export declare function updateMap(map: Y.Map<unknown>, next: DashboardDocument, options?: UpdateMapOptions): void;
|
|
37
58
|
/** The dashboard the map holds: its widgets by their order keys (then ids, the same for everyone). */
|
|
38
59
|
export declare function readMap(map: Y.Map<unknown>): DashboardDocument;
|
|
39
60
|
/**
|
|
@@ -35,7 +35,7 @@ export interface ReplaceWidgetsOptions {
|
|
|
35
35
|
/** A widget that stays keeps its `comments`; `true` by default. */
|
|
36
36
|
keepComments?: boolean;
|
|
37
37
|
}
|
|
38
|
-
/** Deep equality of JSON values; key order
|
|
38
|
+
/** Deep equality of JSON values; key order and keys set to `undefined` don't matter. */
|
|
39
39
|
export declare function sameValue(a: unknown, b: unknown): boolean;
|
|
40
40
|
/**
|
|
41
41
|
* Changes to a dashboard, made step by step and then handed to the
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dou-ui/dashboard",
|
|
3
|
-
"version": "0.1.0-alpha.
|
|
3
|
+
"version": "0.1.0-alpha.13",
|
|
4
4
|
"description": "DouUI 的仪表盘:可拖动、可缩放的网格布局与仪表盘设计器",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"vue",
|
|
@@ -67,9 +67,9 @@
|
|
|
67
67
|
"build": "vite build && vue-tsc -p tsconfig.build.json && bun scripts/build-style.ts"
|
|
68
68
|
},
|
|
69
69
|
"peerDependencies": {
|
|
70
|
-
"@dou-ui/chart": "0.1.0-alpha.
|
|
71
|
-
"@dou-ui/pivot": "0.1.0-alpha.
|
|
72
|
-
"dou-ui": "0.1.0-alpha.
|
|
70
|
+
"@dou-ui/chart": "0.1.0-alpha.13",
|
|
71
|
+
"@dou-ui/pivot": "0.1.0-alpha.13",
|
|
72
|
+
"dou-ui": "0.1.0-alpha.13",
|
|
73
73
|
"vue": "^3.5.0",
|
|
74
74
|
"yjs": "^13.6.0"
|
|
75
75
|
},
|
|
@@ -85,12 +85,12 @@
|
|
|
85
85
|
}
|
|
86
86
|
},
|
|
87
87
|
"dependencies": {
|
|
88
|
-
"@dou-ui/icons": "0.1.0-alpha.
|
|
89
|
-
"@dou-ui/sheet": "0.1.0-alpha.
|
|
88
|
+
"@dou-ui/icons": "0.1.0-alpha.13",
|
|
89
|
+
"@dou-ui/sheet": "0.1.0-alpha.13"
|
|
90
90
|
},
|
|
91
91
|
"devDependencies": {
|
|
92
|
-
"@dou-ui/chart": "0.1.0-alpha.
|
|
93
|
-
"@dou-ui/pivot": "0.1.0-alpha.
|
|
92
|
+
"@dou-ui/chart": "0.1.0-alpha.13",
|
|
93
|
+
"@dou-ui/pivot": "0.1.0-alpha.13",
|
|
94
94
|
"y-protocols": "^1.0.7",
|
|
95
95
|
"yjs": "^13.6.33"
|
|
96
96
|
},
|