@floegence/redevplugin-ui 3.1.0 → 3.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,283 +0,0 @@
1
- import { opaqueSurfaceRenderLimits } from "./opaque-surface-policy.gen.js";
2
- import { PluginUIReconcileError, validatePluginUITree, } from "./ui-patch-validator.js";
3
- export { PluginUIReconcileError, validatePluginUITree } from "./ui-patch-validator.js";
4
- const editableValueTags = new Set(["input", "textarea", "select", "option"]);
5
- const emptyAttributes = {};
6
- const maxPluginUIPatchOperations = opaqueSurfaceRenderLimits.max_patch_operations;
7
- export function reconcilePluginUITrees(current, next, options = {}) {
8
- validatePluginUITree(current);
9
- validatePluginUITree(next);
10
- return reconcileValidatedPluginUITrees(current, next, options);
11
- }
12
- // Internal fast path for callers that retain trees returned by validatePluginUITree.
13
- export function reconcileValidatedPluginUITrees(current, next, options = {}) {
14
- if (current.key !== next.key || current.tag !== next.tag) {
15
- throw new PluginUIReconcileError("Plugin UI root key and tag are immutable");
16
- }
17
- const currentIndex = indexTree(current);
18
- const nextIndex = indexTree(next);
19
- const transferredCanvasKeys = options.transferredCanvasKeys ?? new Set();
20
- for (const key of transferredCanvasKeys) {
21
- const left = currentIndex.byKey.get(key);
22
- const right = nextIndex.byKey.get(key);
23
- if (!left || !right) {
24
- throw new PluginUIReconcileError(`Transferred canvas ${key} cannot be removed`);
25
- }
26
- if (!sameNodeIdentity(left.node, right.node)) {
27
- throw new PluginUIReconcileError(`Transferred canvas ${key} cannot be replaced`);
28
- }
29
- if (left.parentKey !== right.parentKey || left.childIndex !== right.childIndex) {
30
- throw new PluginUIReconcileError(`Transferred canvas ${key} cannot be moved`);
31
- }
32
- if (left.node.type !== "element" || right.node.type !== "element" || !sameAttributes(left.node.attributes, right.node.attributes)) {
33
- throw new PluginUIReconcileError(`Transferred canvas ${key} cannot be patched`);
34
- }
35
- }
36
- const maxOperations = options.maxOperations ?? maxPluginUIPatchOperations;
37
- if (!Number.isSafeInteger(maxOperations) || maxOperations < 1 || maxOperations > maxPluginUIPatchOperations) {
38
- throw new PluginUIReconcileError(`Plugin UI maxOperations must be an integer between 1 and ${maxPluginUIPatchOperations}`);
39
- }
40
- const operations = [];
41
- const append = (operation) => {
42
- if (operations.length >= maxOperations) {
43
- throw new PluginUIReconcileError("Plugin UI patch exceeds the operation limit");
44
- }
45
- operations.push(operation);
46
- };
47
- let structurallyChanged = currentIndex.byKey.size !== nextIndex.byKey.size;
48
- const contentOperations = [];
49
- const replacementKeys = new Set();
50
- for (const key of nextIndex.order) {
51
- const leftInfo = currentIndex.byKey.get(key);
52
- const rightInfo = nextIndex.byKey.get(key);
53
- const left = leftInfo?.node;
54
- const right = rightInfo?.node;
55
- if (!left || !right) {
56
- structurallyChanged = true;
57
- continue;
58
- }
59
- if (!sameNodeIdentity(left, right)) {
60
- replacementKeys.add(key);
61
- structurallyChanged = true;
62
- continue;
63
- }
64
- if (leftInfo.parentKey !== rightInfo.parentKey || leftInfo.childIndex !== rightInfo.childIndex) {
65
- structurallyChanged = true;
66
- }
67
- if (left.type === "text" && right.type === "text") {
68
- if (left.text !== right.text)
69
- contentOperations.push({ type: "set_text", target_key: key, text: right.text });
70
- }
71
- else if (left.type === "element" && right.type === "element") {
72
- reconcileAttributes(left, right, options.controlEditRevisions, (operation) => contentOperations.push(operation));
73
- }
74
- }
75
- if (replacementKeys.has(current.key)) {
76
- throw new PluginUIReconcileError("Plugin UI root key and tag are immutable");
77
- }
78
- if (!structurallyChanged) {
79
- if (contentOperations.length > maxOperations) {
80
- throw new PluginUIReconcileError("Plugin UI patch exceeds the operation limit");
81
- }
82
- return contentOperations;
83
- }
84
- const removedRoots = [];
85
- for (const key of currentIndex.order) {
86
- if (key === current.key)
87
- continue;
88
- if (nextIndex.byKey.has(key) && !replacementKeys.has(key))
89
- continue;
90
- let ancestorKey = currentIndex.byKey.get(key)?.parentKey;
91
- let coveredByRemovedAncestor = false;
92
- while (ancestorKey) {
93
- if (!nextIndex.byKey.has(ancestorKey) || replacementKeys.has(ancestorKey)) {
94
- coveredByRemovedAncestor = true;
95
- break;
96
- }
97
- ancestorKey = currentIndex.byKey.get(ancestorKey)?.parentKey;
98
- }
99
- if (!coveredByRemovedAncestor)
100
- removedRoots.push(key);
101
- }
102
- const removedCoverage = new Set();
103
- for (const key of removedRoots) {
104
- collectSubtreeKeys(currentIndex, key, removedCoverage);
105
- ensureCanvasStable(currentIndex, key, transferredCanvasKeys, "removed or replaced");
106
- append({ type: "remove_child", target_key: key });
107
- }
108
- for (const operation of contentOperations) {
109
- const targetKey = "target_key" in operation ? operation.target_key : undefined;
110
- if (!targetKey || !removedCoverage.has(targetKey))
111
- append(operation);
112
- }
113
- const coveredByFullInsertion = new Set();
114
- for (const parentKey of nextIndex.order) {
115
- if (coveredByFullInsertion.has(parentKey))
116
- continue;
117
- const parent = nextIndex.byKey.get(parentKey)?.node;
118
- if (!parent || parent.type !== "element")
119
- continue;
120
- const desiredKeys = nextIndex.byKey.get(parentKey)?.childKeys ?? [];
121
- const currentSequence = desiredKeys.map((key) => {
122
- const currentInfo = currentIndex.byKey.get(key);
123
- if (!currentInfo || removedCoverage.has(key) || currentInfo.parentKey !== parentKey)
124
- return -1;
125
- return currentInfo.childIndex;
126
- });
127
- const stablePositions = longestIncreasingSubsequencePositions(currentSequence);
128
- for (let index = desiredKeys.length - 1; index >= 0; index -= 1) {
129
- const key = desiredKeys[index];
130
- const beforeKey = desiredKeys[index + 1] ?? null;
131
- const currentInfo = currentIndex.byKey.get(key);
132
- const isAvailable = Boolean(currentInfo && !removedCoverage.has(key));
133
- if (!isAvailable) {
134
- const node = nextIndex.byKey.get(key)?.node;
135
- if (!node)
136
- throw new PluginUIReconcileError("Plugin UI insertion index is inconsistent");
137
- const canInsertFullSubtree = !subtreeContainsAvailableKey(nextIndex, key, currentIndex, removedCoverage);
138
- const insertion = canInsertFullSubtree ? node : shallowNode(node);
139
- append({ type: "insert_child", parent_key: parentKey, before_key: beforeKey, node: insertion });
140
- if (canInsertFullSubtree)
141
- collectSubtreeKeys(nextIndex, key, coveredByFullInsertion);
142
- continue;
143
- }
144
- if (currentInfo?.parentKey !== parentKey || !stablePositions.has(index)) {
145
- ensureCanvasStable(currentIndex, key, transferredCanvasKeys, "moved");
146
- append({ type: "move_child", target_key: key, parent_key: parentKey, before_key: beforeKey });
147
- }
148
- }
149
- }
150
- return operations;
151
- }
152
- function indexTree(root) {
153
- const byKey = new Map();
154
- const order = [];
155
- const visit = (node, parentKey, childIndex, depth) => {
156
- const children = node.type === "element" ? node.children ?? [] : [];
157
- const childKeys = children.length > 0 ? children.map((child) => child.key) : undefined;
158
- byKey.set(node.key, { node, parentKey, childIndex, ...(childKeys ? { childKeys } : {}), depth });
159
- order.push(node.key);
160
- for (let index = 0; index < children.length; index += 1) {
161
- visit(children[index], node.key, index, depth + 1);
162
- }
163
- };
164
- visit(root, undefined, 0, 1);
165
- return { byKey, order };
166
- }
167
- function reconcileAttributes(current, next, controlEditRevisions, append) {
168
- const left = current.attributes;
169
- const right = next.attributes;
170
- let set;
171
- let remove;
172
- for (const name in right) {
173
- const value = right[name];
174
- if (!isEditableControlAttribute(current.tag, name) && left?.[name] !== value) {
175
- (set ??= {})[name] = value;
176
- }
177
- }
178
- for (const name in left) {
179
- if (!isEditableControlAttribute(current.tag, name) && !(name in (right ?? emptyAttributes))) {
180
- (remove ??= []).push(name);
181
- }
182
- }
183
- if (set || remove) {
184
- append({ type: "patch_attributes", target_key: current.key, set: set ?? {}, remove: remove ?? [] });
185
- }
186
- const valueChanged = editableValueTags.has(current.tag) && left?.value !== right?.value;
187
- const checkedChanged = current.tag === "input" && left?.checked !== right?.checked;
188
- if (valueChanged || checkedChanged) {
189
- append({
190
- type: "patch_control",
191
- target_key: current.key,
192
- edit_revision: controlEditRevisions?.get(current.key) ?? 0,
193
- ...(valueChanged ? { value: right?.value === undefined ? null : String(right.value) } : {}),
194
- ...(checkedChanged ? { checked: right?.checked === undefined ? null : Boolean(right.checked) } : {}),
195
- });
196
- }
197
- }
198
- function longestIncreasingSubsequencePositions(values) {
199
- const tails = [];
200
- const predecessors = new Array(values.length).fill(-1);
201
- for (let index = 0; index < values.length; index += 1) {
202
- const value = values[index];
203
- if (value < 0)
204
- continue;
205
- let low = 0;
206
- let high = tails.length;
207
- while (low < high) {
208
- const middle = (low + high) >>> 1;
209
- if ((values[tails[middle]] ?? Infinity) < value)
210
- low = middle + 1;
211
- else
212
- high = middle;
213
- }
214
- if (low > 0)
215
- predecessors[index] = tails[low - 1] ?? -1;
216
- tails[low] = index;
217
- }
218
- const positions = new Set();
219
- let cursor = tails[tails.length - 1] ?? -1;
220
- while (cursor >= 0) {
221
- positions.add(cursor);
222
- cursor = predecessors[cursor] ?? -1;
223
- }
224
- return positions;
225
- }
226
- function collectSubtreeKeys(index, rootKey, output) {
227
- const stack = [rootKey];
228
- while (stack.length > 0) {
229
- const key = stack.pop();
230
- if (!key || output.has(key))
231
- continue;
232
- output.add(key);
233
- stack.push(...(index.byKey.get(key)?.childKeys ?? []));
234
- }
235
- }
236
- function subtreeContainsAvailableKey(nextIndex, rootKey, currentIndex, removedCoverage) {
237
- const stack = [...(nextIndex.byKey.get(rootKey)?.childKeys ?? [])];
238
- while (stack.length > 0) {
239
- const key = stack.pop();
240
- if (!key)
241
- continue;
242
- if (currentIndex.byKey.has(key) && !removedCoverage.has(key))
243
- return true;
244
- stack.push(...(nextIndex.byKey.get(key)?.childKeys ?? []));
245
- }
246
- return false;
247
- }
248
- function shallowNode(node) {
249
- if (node.type === "text")
250
- return node;
251
- return {
252
- type: "element",
253
- key: node.key,
254
- tag: node.tag,
255
- ...(node.attributes ? { attributes: node.attributes } : {}),
256
- children: [],
257
- };
258
- }
259
- function ensureCanvasStable(index, rootKey, transferredCanvasKeys, action) {
260
- const stack = [rootKey];
261
- while (stack.length > 0) {
262
- const key = stack.pop();
263
- if (!key)
264
- continue;
265
- if (transferredCanvasKeys.has(key)) {
266
- throw new PluginUIReconcileError(`Transferred canvas ${key} cannot be ${action}`);
267
- }
268
- stack.push(...(index.byKey.get(key)?.childKeys ?? []));
269
- }
270
- }
271
- function sameNodeIdentity(left, right) {
272
- return left.type === right.type && (left.type === "text" || (right.type === "element" && left.tag === right.tag));
273
- }
274
- function sameAttributes(left, right) {
275
- const leftEntries = Object.entries(left ?? emptyAttributes);
276
- const rightEntries = Object.entries(right ?? emptyAttributes);
277
- return leftEntries.length === rightEntries.length && leftEntries.every(([name, value]) => right?.[name] === value);
278
- }
279
- function isEditableControlAttribute(tag, name) {
280
- const normalized = name.toLowerCase();
281
- return (normalized === "value" && editableValueTags.has(tag)) ||
282
- (normalized === "checked" && tag === "input");
283
- }