@perkyzz/emvb 0.6.0 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (208) hide show
  1. package/README.md +2 -2
  2. package/THIRD_PARTY_NOTICES.md +1 -1
  3. package/package.json +7 -7
  4. package/src/admin/access/RequireEditor.tsx +7 -7
  5. package/src/admin/access/role.ts +5 -2
  6. package/src/admin/content-api.ts +10 -3
  7. package/src/admin/content-item.ts +1 -1
  8. package/src/admin/editor/A11yCopilot.tsx +104 -56
  9. package/src/admin/editor/CommandPalette.tsx +29 -13
  10. package/src/admin/editor/DialogCloseButton.tsx +16 -0
  11. package/src/admin/editor/Editor.tsx +115 -20
  12. package/src/admin/editor/EditorToasts.tsx +26 -0
  13. package/src/admin/editor/PerfMeter.tsx +92 -73
  14. package/src/admin/editor/SavedSectionDialogs.tsx +169 -0
  15. package/src/admin/editor/ShareLink.tsx +102 -72
  16. package/src/admin/editor/ShortcutsDialog.tsx +37 -18
  17. package/src/admin/editor/Timeline.tsx +250 -160
  18. package/src/admin/editor/TopBar.tsx +71 -14
  19. package/src/admin/editor/canvas/CanvasFrame.tsx +10 -1
  20. package/src/admin/editor/canvas/CanvasTextEdit.tsx +8 -2
  21. package/src/admin/editor/canvas/drop-line.ts +11 -3
  22. package/src/admin/editor/canvas/frame-focus-ring.ts +37 -0
  23. package/src/admin/editor/canvas/use-canvas-dnd.ts +32 -3
  24. package/src/admin/editor/canvas/vnode-react.tsx +7 -0
  25. package/src/admin/editor/collection-previews.ts +10 -0
  26. package/src/admin/editor/dialogs.tsx +41 -28
  27. package/src/admin/editor/dnd/drop-target.ts +26 -1
  28. package/src/admin/editor/edited-noun.ts +15 -0
  29. package/src/admin/editor/editor-chrome.tsx +43 -7
  30. package/src/admin/editor/editor-commands.ts +17 -2
  31. package/src/admin/editor/editor-css.ts +75 -24
  32. package/src/admin/editor/element-chrome.ts +21 -11
  33. package/src/admin/editor/feature-css.ts +56 -14
  34. package/src/admin/editor/focus-when-lost.ts +23 -0
  35. package/src/admin/editor/icons/IconLibraryDialog.tsx +54 -18
  36. package/src/admin/editor/icons/library.ts +1 -1
  37. package/src/admin/editor/icons/use-icon-library.ts +5 -2
  38. package/src/admin/editor/ime.ts +29 -0
  39. package/src/admin/editor/palette-items.ts +27 -3
  40. package/src/admin/editor/panels/AddPanel.tsx +44 -11
  41. package/src/admin/editor/panels/ClassesSection.tsx +18 -7
  42. package/src/admin/editor/panels/ColorControl.tsx +130 -23
  43. package/src/admin/editor/panels/ConditionsEditor.tsx +52 -26
  44. package/src/admin/editor/panels/ElementPanel.tsx +134 -24
  45. package/src/admin/editor/panels/LayersPanel.tsx +61 -20
  46. package/src/admin/editor/panels/LeftPanel.tsx +9 -1
  47. package/src/admin/editor/panels/PageSettings.tsx +66 -35
  48. package/src/admin/editor/panels/SavedSectionsGroup.tsx +246 -0
  49. package/src/admin/editor/panels/SiteStylesDrawer.tsx +224 -42
  50. package/src/admin/editor/panels/TokenScales.tsx +88 -48
  51. package/src/admin/editor/panels/TriggerSettings.tsx +50 -12
  52. package/src/admin/editor/panels/TriggersEditor.tsx +3 -2
  53. package/src/admin/editor/panels/VariableSection.tsx +48 -18
  54. package/src/admin/editor/panels/color-space.ts +23 -29
  55. package/src/admin/editor/panels/settings/AdvancedFields.tsx +12 -2
  56. package/src/admin/editor/panels/settings/AudienceEditor.tsx +87 -74
  57. package/src/admin/editor/panels/settings/BackgroundControls.tsx +55 -11
  58. package/src/admin/editor/panels/settings/BackgroundFill.tsx +3 -2
  59. package/src/admin/editor/panels/settings/BindingsEditor.tsx +15 -18
  60. package/src/admin/editor/panels/settings/BoxControl.tsx +2 -1
  61. package/src/admin/editor/panels/settings/ClassChip.tsx +21 -9
  62. package/src/admin/editor/panels/settings/ClassChipInput.tsx +24 -16
  63. package/src/admin/editor/panels/settings/DraftTextField.tsx +41 -3
  64. package/src/admin/editor/panels/settings/EffectsControls.tsx +38 -14
  65. package/src/admin/editor/panels/settings/FieldBindControl.tsx +46 -28
  66. package/src/admin/editor/panels/settings/FieldControl.tsx +20 -8
  67. package/src/admin/editor/panels/settings/FormBindControl.tsx +2 -2
  68. package/src/admin/editor/panels/settings/HintedSwitch.tsx +33 -0
  69. package/src/admin/editor/panels/settings/IconControls.tsx +33 -13
  70. package/src/admin/editor/panels/settings/IconPicker.tsx +2 -0
  71. package/src/admin/editor/panels/settings/ImageSourcesField.tsx +3 -1
  72. package/src/admin/editor/panels/settings/ItemList.tsx +15 -2
  73. package/src/admin/editor/panels/settings/LengthRow.tsx +8 -6
  74. package/src/admin/editor/panels/settings/LoopItemBindControl.tsx +1 -1
  75. package/src/admin/editor/panels/settings/MediaPicker.tsx +127 -23
  76. package/src/admin/editor/panels/settings/MotionControl.tsx +28 -15
  77. package/src/admin/editor/panels/settings/NumberRow.tsx +9 -3
  78. package/src/admin/editor/panels/settings/PanelSelect.tsx +50 -0
  79. package/src/admin/editor/panels/settings/QueryStyleRows.tsx +25 -18
  80. package/src/admin/editor/panels/settings/RequiredFieldsNotice.tsx +66 -1
  81. package/src/admin/editor/panels/settings/StyleRow.tsx +10 -3
  82. package/src/admin/editor/panels/settings/StyleSources.tsx +4 -1
  83. package/src/admin/editor/panels/settings/VariantEditor.tsx +10 -1
  84. package/src/admin/editor/panels/settings/box-link-name.ts +6 -2
  85. package/src/admin/editor/panels/settings/class-suggest.ts +7 -7
  86. package/src/admin/editor/panels/settings/query-style.ts +38 -9
  87. package/src/admin/editor/panels/settings/svg-images.ts +13 -0
  88. package/src/admin/editor/panels/site-list.tsx +20 -6
  89. package/src/admin/editor/panels/variable-values.ts +11 -4
  90. package/src/admin/editor/restore-deleted.ts +21 -2
  91. package/src/admin/editor/section-templates.ts +81 -12
  92. package/src/admin/editor/shortcuts.ts +31 -2
  93. package/src/admin/editor/store.ts +7 -0
  94. package/src/admin/editor/useEditorCommands.ts +61 -15
  95. package/src/admin/editor/useEditorData.ts +4 -2
  96. package/src/admin/editor/useEditorShortcuts.ts +17 -7
  97. package/src/admin/editor/useNodeActions.ts +61 -6
  98. package/src/admin/editor/useSave.ts +22 -7
  99. package/src/admin/editor/useSavedSections.ts +197 -0
  100. package/src/admin/forms-api.ts +2 -0
  101. package/src/admin/image-editor/Canvas.tsx +61 -17
  102. package/src/admin/image-editor/CurvesPanel.tsx +2 -1
  103. package/src/admin/image-editor/ExportDialog.tsx +48 -18
  104. package/src/admin/image-editor/ImageEditorDialog.tsx +179 -8
  105. package/src/admin/image-editor/MetadataPanel.tsx +15 -4
  106. package/src/admin/image-editor/crop-geometry.ts +3 -2
  107. package/src/admin/image-editor/image-editor-css.ts +15 -10
  108. package/src/admin/image-editor/load.ts +9 -2
  109. package/src/admin/image-editor/parts.tsx +69 -19
  110. package/src/admin/image-editor/save.ts +54 -8
  111. package/src/admin/media-api.ts +2 -1
  112. package/src/admin/pages/AgentsPage.tsx +138 -27
  113. package/src/admin/pages/CreateDialog.tsx +97 -30
  114. package/src/admin/pages/NewPageDialog.tsx +21 -3
  115. package/src/admin/pages/NewThemePartDialog.tsx +11 -3
  116. package/src/admin/pages/PageList.tsx +14 -6
  117. package/src/admin/pages/SetupShell.tsx +28 -12
  118. package/src/admin/pages/ThemePartList.tsx +59 -11
  119. package/src/admin/pages/list-kit.tsx +106 -24
  120. package/src/admin/saved-sections-api.ts +51 -0
  121. package/src/admin/search.ts +15 -0
  122. package/src/admin/setup/useSetupStatus.ts +1 -1
  123. package/src/admin/theme-api.ts +16 -5
  124. package/src/admin/ui.ts +82 -22
  125. package/src/astro/floats/runtime.ts +21 -0
  126. package/src/astro/menu/runtime.ts +34 -1
  127. package/src/astro/popups/runtime.ts +41 -9
  128. package/src/astro/render.ts +3 -0
  129. package/src/astro/resolve-theme.ts +53 -11
  130. package/src/astro/resolve.ts +9 -1
  131. package/src/astro/tabs/runtime.ts +12 -0
  132. package/src/astro/theme-posts.ts +2 -2
  133. package/src/astro/visitor.ts +2 -0
  134. package/src/constants.ts +1 -1
  135. package/src/core/a11y/audit.ts +196 -24
  136. package/src/core/arrange.ts +48 -12
  137. package/src/core/audience/rules.ts +5 -1
  138. package/src/core/audience/variants.ts +29 -3
  139. package/src/core/clipboard.ts +7 -0
  140. package/src/core/css/generate.ts +31 -6
  141. package/src/core/css/print.ts +5 -0
  142. package/src/core/data/bindings.ts +8 -1
  143. package/src/core/design/classes.ts +39 -16
  144. package/src/core/design/devices.ts +7 -13
  145. package/src/core/design/diff.ts +6 -0
  146. package/src/core/design/merge.ts +90 -2
  147. package/src/core/design/states.ts +7 -13
  148. package/src/core/design/tokens.ts +16 -2
  149. package/src/core/design/transfer.ts +33 -6
  150. package/src/core/design/variables.ts +30 -9
  151. package/src/core/elements/dynamic-elements.ts +53 -14
  152. package/src/core/elements/form-elements.ts +55 -16
  153. package/src/core/elements/index.ts +84 -39
  154. package/src/core/elements/menu.ts +40 -19
  155. package/src/core/elements/select-prompt.ts +3 -0
  156. package/src/core/elements/svg-ids.ts +39 -0
  157. package/src/core/forms/binding.ts +57 -16
  158. package/src/core/forms/definition.ts +72 -0
  159. package/src/core/history/diff.ts +18 -1
  160. package/src/core/icons/animation-runs.ts +11 -0
  161. package/src/core/icons/catalog.ts +3 -2
  162. package/src/core/image-edit/schema.ts +11 -1
  163. package/src/core/image-edit/summary.ts +6 -3
  164. package/src/core/index.ts +35 -1
  165. package/src/core/limits.ts +12 -1
  166. package/src/core/migrate/index.ts +3 -0
  167. package/src/core/perf/meter.ts +42 -7
  168. package/src/core/recipes/index.ts +5 -4
  169. package/src/core/render/context.ts +8 -0
  170. package/src/core/render/dynamic-post.ts +69 -12
  171. package/src/core/render/extras.ts +77 -4
  172. package/src/core/render/form.ts +140 -6
  173. package/src/core/render/index.ts +156 -20
  174. package/src/core/render/loop.ts +44 -8
  175. package/src/core/render/pagination.ts +18 -4
  176. package/src/core/render/section.ts +27 -0
  177. package/src/core/render/tabs.ts +12 -3
  178. package/src/core/render/vnode.ts +1 -1
  179. package/src/core/sanitize/css.ts +29 -8
  180. package/src/core/sanitize/embed-url.ts +42 -5
  181. package/src/core/sanitize/url.ts +2 -1
  182. package/src/core/saved-sections.ts +318 -0
  183. package/src/core/schema/design.ts +1 -1
  184. package/src/core/schema/layout.ts +16 -7
  185. package/src/core/schema/style.ts +4 -2
  186. package/src/core/stats.ts +2 -1
  187. package/src/core/theme/conditions.ts +3 -1
  188. package/src/core/theme/dynamic.ts +36 -5
  189. package/src/core/theme/float.ts +12 -2
  190. package/src/core/theme/pagination.ts +50 -29
  191. package/src/core/theme/part-types.ts +4 -4
  192. package/src/core/theme/popup-markup.ts +23 -5
  193. package/src/core/theme/popup-rules.ts +1 -0
  194. package/src/core/theme/triggers.ts +3 -2
  195. package/src/core/tree-ops.ts +27 -0
  196. package/src/mcp/ports.ts +22 -1
  197. package/src/mcp/rate-limit.ts +2 -1
  198. package/src/mcp/read.ts +15 -3
  199. package/src/mcp/refs.ts +18 -0
  200. package/src/mcp/sections.ts +183 -0
  201. package/src/mcp/tools.ts +44 -0
  202. package/src/mcp/write.ts +178 -14
  203. package/src/server/agent-settings.ts +1 -1
  204. package/src/server/hooks.ts +5 -3
  205. package/src/server/icon-routes.ts +30 -9
  206. package/src/server/mcp-routes.ts +8 -1
  207. package/src/server/plugin.ts +14 -0
  208. package/src/server/saved-sections.ts +163 -0
@@ -96,10 +96,14 @@ export const MAX_CLASSES = 100;
96
96
 
97
97
  /** A class name as saved: control characters gone, trimmed, at most 60 characters (W-218). */
98
98
  export function cleanClassName(name: string): string {
99
- const spaced = Array.from(name, (ch) => {
99
+ // W-568: one form for accents, C1 controls spaced like C0, and invisible marks (zero-width
100
+ // space, word joiner, BOM, bidi overrides) dropped so two names that look alike compare alike.
101
+ const spaced = Array.from(name.normalize("NFC"), (ch) => {
100
102
  const code = ch.charCodeAt(0);
101
- return code < 32 || code === 127 ? " " : ch;
102
- }).join("");
103
+ return code < 32 || (code >= 127 && code <= 159) ? " " : ch;
104
+ })
105
+ .join("")
106
+ .replace(/[\u200B\u2060\uFEFF\u200E\u200F\u202A-\u202E\u2066-\u2069]/g, "");
103
107
  const spacedTrim = spaced.replace(/ {2,}/g, " ").trim();
104
108
  return clipText(spacedTrim, 60).trim();
105
109
  }
@@ -122,10 +126,10 @@ export function localStylesToClass(
122
126
  node: LayoutNode,
123
127
  name: string,
124
128
  ): { design: DesignSystem; node: LayoutNode; classId: string } | null {
125
- const trimmed = clipText(name.trim(), 60);
129
+ const trimmed = cleanClassName(name);
126
130
  if (!trimmed || localToClassRefusal(design, node)) return null;
127
131
  const classes = design.classes ?? [];
128
- const base = slugify(trimmed).slice(0, 34) || "class";
132
+ const base = slugify(trimmed).slice(0, 34).replace(/-+$/, "") || "class";
129
133
  const taken = new Set(classes.map((c) => c.id));
130
134
  let id = base;
131
135
  for (let n = 2; taken.has(id); n++) id = `${base}-${n}`;
@@ -161,7 +165,7 @@ export function duplicateClass(
161
165
  for (let n = 2; n <= MAX_CLASSES && classNameTaken(design, baseName); n++) {
162
166
  baseName = `${stem} ${n}`;
163
167
  }
164
- const base = slugify(baseName).slice(0, 34) || "class";
168
+ const base = slugify(baseName).slice(0, 34).replace(/-+$/, "") || "class";
165
169
  const taken = new Set(classes.map((c) => c.id));
166
170
  let id = randomSuffix ? `${base}-${randomSuffix}`.slice(0, 40) : base;
167
171
  for (let n = 2; taken.has(id); n++) id = `${base}-${n}`.slice(0, 40);
@@ -174,9 +178,14 @@ export function duplicateClass(
174
178
  * W-277: whether another class already has this name (case-insensitive). Two classes with one
175
179
  * name can't be told apart in the class picker or the Site styles list.
176
180
  */
181
+ /** W-570: the form two class or variable names are compared in (cleaned, any case). */
182
+ export const nameKey = (name: string) => cleanClassName(name).toLowerCase();
183
+
177
184
  export function classNameTaken(design: DesignSystem, name: string, exceptId?: string): boolean {
178
185
  const wanted = cleanClassName(name).toLowerCase();
179
- return (design.classes ?? []).some((c) => c.id !== exceptId && c.name.toLowerCase() === wanted);
186
+ return (design.classes ?? []).some(
187
+ (c) => c.id !== exceptId && cleanClassName(c.name).toLowerCase() === wanted,
188
+ );
180
189
  }
181
190
 
182
191
  /** The message shown when a new or renamed class would take another class's name (W-277). */
@@ -198,25 +207,39 @@ export function renameClass(design: DesignSystem, classId: string, name: string)
198
207
  };
199
208
  }
200
209
 
201
- /** Merge `patch` into a class's style; `undefined` values clear their property. Pure (W-087). */
202
- export function patchClassStyle(
210
+ type DesignClass = NonNullable<DesignSystem["classes"]>[number];
211
+
212
+ /**
213
+ * Replace one class with `edit(copy)`; the design comes back unchanged when no class has that id.
214
+ * The copy is shallow, so `edit` may set or delete keys on it. Pure.
215
+ */
216
+ export function editClass(
203
217
  design: DesignSystem,
204
218
  classId: string,
205
- patch: Partial<StyleProps>,
219
+ edit: (copy: DesignClass) => DesignClass,
206
220
  ): DesignSystem {
207
221
  const classes = design.classes ?? [];
208
222
  if (!classes.some((c) => c.id === classId)) return design;
209
223
  return {
210
224
  ...design,
211
- classes: classes.map((c) => {
212
- if (c.id !== classId) return c;
213
- const style: Record<string, unknown> = { ...c.style, ...patch };
214
- for (const [key, value] of Object.entries(patch)) if (value === undefined) delete style[key];
215
- return Object.assign({}, c, { style: style as StyleProps });
216
- }),
225
+ classes: classes.map((c) => (c.id === classId ? edit(Object.assign({}, c)) : c)),
217
226
  };
218
227
  }
219
228
 
229
+ /** Merge `patch` into a class's style; `undefined` values clear their property. Pure (W-087). */
230
+ export function patchClassStyle(
231
+ design: DesignSystem,
232
+ classId: string,
233
+ patch: Partial<StyleProps>,
234
+ ): DesignSystem {
235
+ return editClass(design, classId, (next) => {
236
+ const style: Record<string, unknown> = { ...next.style, ...patch };
237
+ for (const [key, value] of Object.entries(patch)) if (value === undefined) delete style[key];
238
+ next.style = style as StyleProps;
239
+ return next;
240
+ });
241
+ }
242
+
220
243
  /** Swap one applied class id for another in place, keeping the cascade order. Pure (W-087). */
221
244
  export function replaceClassId(ids: readonly string[], from: string, to: string): string[] {
222
245
  if (ids.includes(to)) return removeClassId(ids, from);
@@ -1,4 +1,5 @@
1
1
  import type { DesignSystem } from "../schema/design.ts";
2
+ import { editClass } from "./classes.ts";
2
3
  import type { DeviceStyles, StyleProps } from "../schema/style.ts";
3
4
  import { POPUP_DEVICES, type PopupDevice } from "../theme/popup-rules.ts";
4
5
 
@@ -28,19 +29,12 @@ export function patchClassDevices(
28
29
  device: ResponsiveDevice,
29
30
  patch: Partial<StyleProps>,
30
31
  ): DesignSystem {
31
- const classes = design.classes ?? [];
32
- if (!classes.some((c) => c.id === classId)) return design;
33
- return {
34
- ...design,
35
- classes: classes.map((c) => {
36
- if (c.id !== classId) return c;
37
- const next = Object.assign({}, c);
38
- const devices = patchDeviceStyle(c.devices, device, patch);
39
- if (devices) next.devices = devices;
40
- else delete next.devices;
41
- return next;
42
- }),
43
- };
32
+ return editClass(design, classId, (next) => {
33
+ const devices = patchDeviceStyle(next.devices, device, patch);
34
+ if (devices) next.devices = devices;
35
+ else delete next.devices;
36
+ return next;
37
+ });
44
38
  }
45
39
 
46
40
  /** Add or remove one device. The result stays in desktop, tablet, mobile order, or undefined. */
@@ -18,6 +18,12 @@ export function designChanges(published: DesignSystem, draft: DesignSystem): Des
18
18
  listChanges(published.variables.fontSizes, draft.variables.fontSizes, "variable", changes);
19
19
  listChanges(published.variables.spacings, draft.variables.spacings, "variable", changes);
20
20
  listChanges(published.classes, draft.classes, "class", changes);
21
+ // W-641: class order is the cascade order, so a reorder alone changes how the page looks.
22
+ const kept = new Set((draft.classes ?? []).map((c) => c.id));
23
+ const before = (published.classes ?? []).map((c) => c.id).filter((id) => kept.has(id));
24
+ const had = new Set(before);
25
+ const after = (draft.classes ?? []).map((c) => c.id).filter((id) => had.has(id));
26
+ if (!same(before, after)) changes.push({ kind: "changed", what: "setting", name: "Class order" });
21
27
  if (!same(published.direction, draft.direction)) {
22
28
  changes.push({ kind: "changed", what: "setting", name: "Direction" });
23
29
  }
@@ -1,4 +1,5 @@
1
1
  import { sameJson } from "../stable-json.ts";
2
+ import { nameKey } from "./classes.ts";
2
3
  import { emptyDesign, type DesignSystem } from "../schema/design.ts";
3
4
 
4
5
  export type DesignConflict = { kind: "variable" | "class" | "setting"; name: string };
@@ -30,6 +31,7 @@ export function mergeDesign(
30
31
  theirs.variables.colors,
31
32
  conflicts,
32
33
  "variable",
34
+ 200,
33
35
  ),
34
36
  fonts: mergeList(
35
37
  from.variables.fonts,
@@ -37,6 +39,7 @@ export function mergeDesign(
37
39
  theirs.variables.fonts,
38
40
  conflicts,
39
41
  "variable",
42
+ 50,
40
43
  ),
41
44
  fontSizes: mergeList(
42
45
  from.variables.fontSizes,
@@ -44,6 +47,7 @@ export function mergeDesign(
44
47
  theirs.variables.fontSizes,
45
48
  conflicts,
46
49
  "variable",
50
+ 50,
47
51
  ),
48
52
  spacings: mergeList(
49
53
  from.variables.spacings,
@@ -51,9 +55,10 @@ export function mergeDesign(
51
55
  theirs.variables.spacings,
52
56
  conflicts,
53
57
  "variable",
58
+ 100,
54
59
  ),
55
60
  },
56
- classes: mergeList(from.classes, ours.classes, theirs.classes, conflicts, "class"),
61
+ classes: mergeList(from.classes, ours.classes, theirs.classes, conflicts, "class", 100),
57
62
  fluidRange: pickSetting(
58
63
  from.fluidRange,
59
64
  ours.fluidRange,
@@ -79,6 +84,32 @@ function mergeList<T extends Named>(
79
84
  theirs: T[] | undefined,
80
85
  conflicts: DesignConflict[],
81
86
  kind: "variable" | "class",
87
+ /** The list's limit in schema/design.ts. */
88
+ max: number,
89
+ ): T[] {
90
+ const merged = mergeListUncapped(base, ours, theirs, conflicts, kind);
91
+ if (merged.length <= max) return merged;
92
+ // W-647: both tabs added items and together they pass the limit, so the save would be refused.
93
+ // The server's items stay; this tab's new ones past the limit are reported as conflicts.
94
+ const theirIds = new Set((theirs ?? []).map((item) => item.id));
95
+ let over = merged.length - max;
96
+ const dropped = new Set<string>();
97
+ for (const item of merged.toReversed()) {
98
+ if (over === 0) break;
99
+ if (theirIds.has(item.id)) continue;
100
+ dropped.add(item.id);
101
+ conflicts.push({ kind, name: item.name });
102
+ over--;
103
+ }
104
+ return merged.filter((item) => !dropped.has(item.id));
105
+ }
106
+
107
+ function mergeListUncapped<T extends Named>(
108
+ base: T[] | undefined,
109
+ ours: T[] | undefined,
110
+ theirs: T[] | undefined,
111
+ conflicts: DesignConflict[],
112
+ kind: "variable" | "class",
82
113
  ): T[] {
83
114
  const baseItems = base ?? [];
84
115
  const ourItems = ours ?? [];
@@ -89,6 +120,8 @@ function mergeList<T extends Named>(
89
120
  const theirsBy = by(theirItems);
90
121
  const result: T[] = [];
91
122
  const seen = new Set<string>();
123
+ /** Ids whose kept copy is this tab's change (W-620). */
124
+ const fromUs = new Set<string>();
92
125
  const take = (id: string) => {
93
126
  if (seen.has(id)) return;
94
127
  seen.add(id);
@@ -102,6 +135,7 @@ function mergeList<T extends Named>(
102
135
  return;
103
136
  }
104
137
  if (!theyChanged || same(ourItem, theirItem)) {
138
+ if (ourItem && !same(ourItem, theirItem)) fromUs.add(ourItem.id);
105
139
  if (ourItem) result.push(ourItem);
106
140
  return;
107
141
  }
@@ -110,7 +144,26 @@ function mergeList<T extends Named>(
110
144
  };
111
145
  for (const item of theirItems) take(item.id);
112
146
  for (const item of ourItems) take(item.id);
113
- return result;
147
+ // W-620: this tab and the other one each named a different class (or variable) the same, so the
148
+ // merge would keep two with one name; names must stay unique, so it's a conflict.
149
+ const theirNames = new Map(
150
+ result.filter((item) => !fromUs.has(item.id)).map((item) => [nameKey(item.name), item.id]),
151
+ );
152
+ const clashes = result.filter((item) => {
153
+ if (!fromUs.has(item.id)) return false;
154
+ const other = theirNames.get(nameKey(item.name));
155
+ return other !== undefined && other !== item.id;
156
+ });
157
+ if (clashes.length === 0)
158
+ return inOurOrder(baseItems, ourItems, theirItems, result, conflicts, kind);
159
+ for (const item of clashes) conflicts.push({ kind, name: item.name });
160
+ // The server copy stays, as in any conflict: their version of a renamed item, nothing for a new one.
161
+ const clashed = new Set(clashes.map((item) => item.id));
162
+ return result.flatMap((item) => {
163
+ if (!clashed.has(item.id)) return [item];
164
+ const theirItem = theirsBy.get(item.id);
165
+ return theirItem ? [theirItem] : [];
166
+ });
114
167
  }
115
168
 
116
169
  function pickSetting<T>(
@@ -127,3 +180,38 @@ function pickSetting<T>(
127
180
  conflicts.push({ kind: "setting", name });
128
181
  return theirs;
129
182
  }
183
+
184
+ /** The ids both lists share, in `list`'s order. */
185
+ const sharedOrder = (list: Named[], other: Set<string>) =>
186
+ list.filter((item) => other.has(item.id)).map((item) => item.id);
187
+
188
+ /**
189
+ * W-621: class order is the cascade order, so a reorder here was lost when the other tab
190
+ * changed anything. Our order wins when only we reordered; both reordering is a conflict.
191
+ */
192
+ function inOurOrder<T extends Named>(
193
+ base: T[],
194
+ ours: T[],
195
+ theirs: T[],
196
+ result: T[],
197
+ conflicts: DesignConflict[],
198
+ kind: "variable" | "class",
199
+ ): T[] {
200
+ const baseIds = new Set(base.map((item) => item.id));
201
+ const ourIds = new Set(ours.map((item) => item.id));
202
+ const theirIds = new Set(theirs.map((item) => item.id));
203
+ const inAll = new Set([...baseIds].filter((id) => ourIds.has(id) && theirIds.has(id)));
204
+ const baseOrder = sharedOrder(base, inAll).join("\n");
205
+ const ourOrder = sharedOrder(ours, inAll).join("\n");
206
+ const theirOrder = sharedOrder(theirs, inAll).join("\n");
207
+ if (ourOrder === baseOrder || ourOrder === theirOrder) return result;
208
+ if (theirOrder !== baseOrder) {
209
+ conflicts.push({ kind: "setting", name: kind === "class" ? "Class order" : "Variable order" });
210
+ return result;
211
+ }
212
+ const rank = new Map(ours.map((item, index) => [item.id, index]));
213
+ const keptOurs = result.filter((item) => rank.has(item.id));
214
+ keptOurs.sort((a, b) => (rank.get(a.id) as number) - (rank.get(b.id) as number));
215
+ let next = 0;
216
+ return result.map((item) => (rank.has(item.id) ? (keptOurs[next++] as T) : item));
217
+ }
@@ -1,4 +1,5 @@
1
1
  import type { DesignSystem } from "../schema/design.ts";
2
+ import { editClass } from "./classes.ts";
2
3
  import { STYLE_STATES, type StyleStateName } from "../schema/state-names.ts";
3
4
  import type { StyleProps, StyleStates } from "../schema/style.ts";
4
5
 
@@ -33,17 +34,10 @@ export function patchClassState(
33
34
  state: StyleStateName,
34
35
  patch: Partial<StyleProps>,
35
36
  ): DesignSystem {
36
- const classes = design.classes ?? [];
37
- if (!classes.some((c) => c.id === classId)) return design;
38
- return {
39
- ...design,
40
- classes: classes.map((c) => {
41
- if (c.id !== classId) return c;
42
- const next = Object.assign({}, c);
43
- const states = patchStates(c.states, state, patch);
44
- if (states) next.states = states;
45
- else delete next.states;
46
- return next;
47
- }),
48
- };
37
+ return editClass(design, classId, (next) => {
38
+ const states = patchStates(next.states, state, patch);
39
+ if (states) next.states = states;
40
+ else delete next.states;
41
+ return next;
42
+ });
49
43
  }
@@ -119,9 +119,23 @@ export function applyTokens(
119
119
  ): DesignSystem {
120
120
  const current = design.variables[list] ?? [];
121
121
  const byId = new Map(tokens.map((t) => [t.id, t]));
122
- const replaced = current.map((v) => byId.get(v.id) ?? v);
122
+ // W-640: an updated token keeps the name the site gave it.
123
+ const replaced = current.map((v) => {
124
+ const t = byId.get(v.id);
125
+ return t ? Object.assign({}, t, { name: v.name }) : v;
126
+ });
123
127
  const have = new Set(current.map((v) => v.id));
124
128
  const limit = list === "fontSizes" ? 50 : 100;
125
- const added = tokens.filter((t) => !have.has(t.id)).slice(0, Math.max(0, limit - current.length));
129
+ // W-640: a new token whose name another variable has gets "Display 2", so pickers can tell them apart.
130
+ const taken = new Set(current.map((v) => v.name.trim().toLowerCase()));
131
+ const added = tokens
132
+ .filter((t) => !have.has(t.id))
133
+ .slice(0, Math.max(0, limit - current.length))
134
+ .map((t) => {
135
+ let name = t.name;
136
+ for (let n = 2; taken.has(name.toLowerCase()); n++) name = `${t.name} ${n}`;
137
+ taken.add(name.toLowerCase());
138
+ return name === t.name ? t : Object.assign({}, t, { name });
139
+ });
126
140
  return { ...design, variables: { ...design.variables, [list]: [...replaced, ...added] } };
127
141
  }
@@ -3,7 +3,7 @@ import { DesignSystem as DesignSystemSchema, DESIGN_SCHEMA_VERSION } from "../sc
3
3
  import type { DesignSystem } from "../schema/design.ts";
4
4
  import { DESIGN_MIGRATIONS, upgradeLayout } from "../migrate/index.ts";
5
5
  import type { Layout } from "../schema/layout.ts";
6
- import { findClassUsages } from "./classes.ts";
6
+ import { findClassUsages, nameKey } from "./classes.ts";
7
7
  import { findVariableUsages } from "./variables.ts";
8
8
 
9
9
  /** The design document as JSON. This file is the backup: there is no revision history. */
@@ -23,22 +23,22 @@ function freeName(name: string, taken: Set<string>): string {
23
23
  for (let n = 2; ; n++) {
24
24
  const suffix = ` ${n}`;
25
25
  const next = `${clipText(name, 60 - suffix.length).trim()}${suffix}`;
26
- if (!taken.has(next.toLowerCase())) return next;
26
+ if (!taken.has(nameKey(next))) return next;
27
27
  }
28
28
  }
29
29
 
30
30
  function dedupe<T extends { name: string }>(items: T[], renamed: ImportRename[]): T[] {
31
- const taken = new Set(items.map((item) => item.name.toLowerCase()));
31
+ const taken = new Set(items.map((item) => nameKey(item.name)));
32
32
  const seen = new Set<string>();
33
33
  return items.map((item) => {
34
- const key = item.name.toLowerCase();
34
+ const key = nameKey(item.name);
35
35
  if (!seen.has(key)) {
36
36
  seen.add(key);
37
37
  return item;
38
38
  }
39
39
  const to = freeName(item.name, taken);
40
- taken.add(to.toLowerCase());
41
- seen.add(to.toLowerCase());
40
+ taken.add(nameKey(to));
41
+ seen.add(nameKey(to));
42
42
  renamed.push({ from: item.name, to });
43
43
  return { ...item, name: to };
44
44
  });
@@ -64,6 +64,24 @@ export function uniqueImportNames(design: DesignSystem): {
64
64
  return { design: { ...design, variables, ...(classes ? { classes } : {}) }, renamed };
65
65
  }
66
66
 
67
+ function repeatedId(design: DesignSystem): { what: string; id: string } | null {
68
+ const lists: [string, { id: string }[] | undefined][] = [
69
+ ["classes", design.classes],
70
+ ...VARIABLE_GROUPS.map(([group]): [string, { id: string }[] | undefined] => [
71
+ "variables",
72
+ design.variables[group],
73
+ ]),
74
+ ];
75
+ for (const [what, list] of lists) {
76
+ const seen = new Set<string>();
77
+ for (const item of list ?? []) {
78
+ if (seen.has(item.id)) return { what, id: item.id };
79
+ seen.add(item.id);
80
+ }
81
+ }
82
+ return null;
83
+ }
84
+
67
85
  /** Parse an exported design. Older schema versions are upgraded. A newer file is refused. */
68
86
  export function designFromJson(text: string): DesignImport {
69
87
  let parsed: unknown;
@@ -84,6 +102,15 @@ export function designFromJson(text: string): DesignImport {
84
102
  }
85
103
  const result = DesignSystemSchema.safeParse(upgraded.doc);
86
104
  if (!result.success) return { ok: false, message: "That file is not an EmVB design document." };
105
+ // W-619: every reference points at an id, so two classes (or two variables of one kind) with
106
+ // the same id can't both be kept; the editor never writes such a file.
107
+ const repeated = repeatedId(result.data);
108
+ if (repeated) {
109
+ return {
110
+ ok: false,
111
+ message: `That file has two ${repeated.what} with the id "${clipText(repeated.id, 40)}". Give one of them a different id, then import it again.`,
112
+ };
113
+ }
87
114
  const imported = uniqueImportNames(result.data);
88
115
  // W-516: the file cap is 1 MB, but the saved design is capped lower; a design over that cap
89
116
  // replaced the site styles in the editor and then every save of them was refused.
@@ -68,7 +68,12 @@ const RESPONSIVE = ["tablet", "mobile"] as const;
68
68
 
69
69
  /** Style, state styles and device overrides. A nested usage is `<state|device>.<prop>`. */
70
70
  function collectAllUsages(
71
- owner: { style?: StyleProps; states?: StyleStates; devices?: DeviceStyles },
71
+ owner: {
72
+ style?: StyleProps;
73
+ states?: StyleStates;
74
+ devices?: DeviceStyles;
75
+ query?: { style: StyleProps };
76
+ },
72
77
  id: string,
73
78
  kind: VariableKind | undefined,
74
79
  hit: (prop: string) => void,
@@ -80,6 +85,8 @@ function collectAllUsages(
80
85
  for (const device of RESPONSIVE) {
81
86
  collectStyleUsages(owner.devices?.[device], id, kind, (prop) => hit(`${device}.${prop}`));
82
87
  }
88
+ // W-643: a container-query override (W-346) can use a variable too.
89
+ collectStyleUsages(owner.query?.style, id, kind, (prop) => hit(`query.${prop}`));
83
90
  }
84
91
 
85
92
  /** Find every style property on `layout` that references variable `id` (optionally of `kind`). */
@@ -191,19 +198,33 @@ function stripRefsFromDevices(
191
198
  }
192
199
 
193
200
  /** Style, states and device overrides with the refs removed. Same object when nothing changed. */
194
- function stripOwner<T extends { style?: StyleProps; states?: StyleStates; devices?: DeviceStyles }>(
195
- owner: T,
196
- id: string,
197
- kind: VariableKind | undefined,
198
- ): T {
201
+ function stripOwner<
202
+ T extends {
203
+ style?: StyleProps;
204
+ states?: StyleStates;
205
+ devices?: DeviceStyles;
206
+ query?: { min: number; style: StyleProps };
207
+ },
208
+ >(owner: T, id: string, kind: VariableKind | undefined): T {
199
209
  const style = stripRefsFromStyle(owner.style, id, kind);
200
210
  const states = stripRefsFromStates(owner.states, id, kind);
201
211
  const devices = stripRefsFromDevices(owner.devices, id, kind);
202
- if (style === owner.style && states === owner.states && devices === owner.devices) return owner;
212
+ const queryStyle = owner.query ? stripRefsFromStyle(owner.query.style, id, kind) : undefined;
213
+ const queryChanged = owner.query !== undefined && queryStyle !== owner.query.style;
214
+ if (
215
+ style === owner.style &&
216
+ states === owner.states &&
217
+ devices === owner.devices &&
218
+ !queryChanged
219
+ ) {
220
+ return owner;
221
+ }
203
222
  const next: T = { ...owner, style, states, devices };
204
223
  if (style === undefined) delete next.style;
205
224
  if (states === undefined) delete next.states;
206
225
  if (devices === undefined) delete next.devices;
226
+ // W-643: the container-query override keeps its width; an emptied one keeps `{}`.
227
+ if (queryChanged && owner.query) next.query = { ...owner.query, style: queryStyle ?? {} };
207
228
  return next;
208
229
  }
209
230
 
@@ -285,7 +306,7 @@ export function duplicateVariable(
285
306
  for (let n = 2; n <= MAX_VARIABLES[kind] && variableNameTaken(design, kind, name); n++) {
286
307
  name = `${stem} ${n}`;
287
308
  }
288
- const base = slugify(name).slice(0, 34) || kind;
309
+ const base = slugify(name).slice(0, 34).replace(/-+$/, "") || kind;
289
310
  const taken = new Set(list.map((entry) => entry.id));
290
311
  let nextId = base;
291
312
  for (let n = 2; taken.has(nextId); n++) nextId = `${base}-${n}`.slice(0, 40);
@@ -312,7 +333,7 @@ export function variableNameTaken(
312
333
  ): boolean {
313
334
  const wanted = cleanClassName(name).toLowerCase();
314
335
  return (design.variables[KIND_TO_LIST[kind]] ?? []).some(
315
- (v) => v.id !== exceptId && v.name.toLowerCase() === wanted,
336
+ (v) => v.id !== exceptId && cleanClassName(v.name).toLowerCase() === wanted,
316
337
  );
317
338
  }
318
339