@perspective-dev/viewer 5.1.0 → 5.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.
Files changed (245) hide show
  1. package/dist/cdn/perspective-viewer.js +9 -9
  2. package/dist/cdn/perspective-viewer.js.map +4 -4
  3. package/dist/css/botanical.css +1 -1
  4. package/dist/css/dracula.css +1 -1
  5. package/dist/css/gruvbox-dark.css +1 -1
  6. package/dist/css/gruvbox.css +1 -1
  7. package/dist/css/icons.css +1 -1
  8. package/dist/css/intl/de.css +1 -1
  9. package/dist/css/intl/es.css +1 -1
  10. package/dist/css/intl/fr.css +1 -1
  11. package/dist/css/intl/ja.css +1 -1
  12. package/dist/css/intl/pt.css +1 -1
  13. package/dist/css/intl/zh.css +1 -1
  14. package/dist/css/intl.css +1 -1
  15. package/dist/css/monokai.css +1 -1
  16. package/dist/css/phosphor.css +1 -1
  17. package/dist/css/pro-dark.css +1 -1
  18. package/dist/css/pro.css +1 -1
  19. package/dist/css/solarized-dark.css +1 -1
  20. package/dist/css/solarized.css +1 -1
  21. package/dist/css/themes.css +1 -1
  22. package/dist/css/vaporwave.css +1 -1
  23. package/dist/docs/perspective-docs.json +1 -0
  24. package/dist/esm/column-format.d.ts +19 -28
  25. package/dist/esm/perspective-viewer.d.ts +3 -0
  26. package/dist/esm/perspective-viewer.inline.js +9 -9
  27. package/dist/esm/perspective-viewer.inline.js.map +4 -4
  28. package/dist/esm/perspective-viewer.js +9 -9
  29. package/dist/esm/perspective-viewer.js.map +4 -4
  30. package/dist/esm/plugin.d.ts +10 -0
  31. package/dist/esm/providers.d.ts +79 -0
  32. package/dist/esm/ts-rs/CompactDisplay.d.ts +5 -0
  33. package/dist/esm/ts-rs/CurrencyCode.d.ts +1 -0
  34. package/dist/esm/ts-rs/CurrencyDisplay.d.ts +1 -0
  35. package/dist/esm/ts-rs/CurrencyNumberFormatStyle.d.ts +8 -0
  36. package/dist/esm/ts-rs/CurrencySign.d.ts +1 -0
  37. package/dist/esm/ts-rs/CustomDatetimeFormat.d.ts +1 -0
  38. package/dist/esm/ts-rs/CustomDatetimeStyleConfig.d.ts +54 -0
  39. package/dist/esm/ts-rs/CustomNumberFormatConfig.d.ts +34 -0
  40. package/dist/esm/ts-rs/DatetimeColorMode.d.ts +1 -0
  41. package/dist/esm/ts-rs/DatetimeFormatType.d.ts +6 -0
  42. package/dist/esm/ts-rs/FormatMode.d.ts +1 -0
  43. package/dist/esm/ts-rs/FormatUnit.d.ts +1 -0
  44. package/dist/esm/ts-rs/Notation.d.ts +17 -0
  45. package/dist/esm/ts-rs/NumberFormatStyle.d.ts +18 -0
  46. package/dist/esm/ts-rs/PanelViewerConfig.d.ts +42 -2
  47. package/dist/esm/ts-rs/PluginStaticConfig.d.ts +35 -0
  48. package/dist/esm/ts-rs/RestoreOptions.d.ts +19 -0
  49. package/dist/esm/ts-rs/RoundingMode.d.ts +1 -0
  50. package/dist/esm/ts-rs/RoundingPriority.d.ts +1 -0
  51. package/dist/esm/ts-rs/SaveWorkspaceOptions.d.ts +10 -0
  52. package/dist/esm/ts-rs/SignDisplay.d.ts +1 -0
  53. package/dist/esm/ts-rs/SimpleDatetimeFormat.d.ts +1 -0
  54. package/dist/esm/ts-rs/SimpleDatetimeStyleConfig.d.ts +24 -0
  55. package/dist/esm/ts-rs/SplitRollupMode.d.ts +9 -0
  56. package/dist/esm/ts-rs/StringColorMode.d.ts +1 -0
  57. package/dist/esm/ts-rs/TrailingZeroDisplay.d.ts +1 -0
  58. package/dist/esm/ts-rs/Unit.d.ts +1 -0
  59. package/dist/esm/ts-rs/UnitDisplay.d.ts +1 -0
  60. package/dist/esm/ts-rs/UnitNumberFormatStyle.d.ts +6 -0
  61. package/dist/esm/ts-rs/UseGrouping.d.ts +1 -0
  62. package/dist/esm/ts-rs/ViewerConfig.d.ts +42 -2
  63. package/dist/esm/ts-rs/ViewerConfigInitial.d.ts +175 -0
  64. package/dist/esm/ts-rs/ViewerConfigUpdate.d.ts +63 -0
  65. package/dist/esm/ts-rs/WindowSpec.d.ts +3 -2
  66. package/dist/esm/ts-rs/WorkspaceConfig.d.ts +9 -1
  67. package/dist/esm/ts-rs/WorkspaceConfigUpdate.d.ts +20 -9
  68. package/dist/wasm/perspective-viewer.d.ts +157 -41
  69. package/dist/wasm/perspective-viewer.js +452 -151
  70. package/dist/wasm/perspective-viewer.wasm +0 -0
  71. package/dist/wasm/perspective-viewer.wasm.d.ts +22 -17
  72. package/package.json +2 -1
  73. package/src/css/_viewer_bundle.css +1 -0
  74. package/src/css/column-selector.css +8 -0
  75. package/src/css/column-settings-panel.css +9 -5
  76. package/src/css/column-style.css +209 -31
  77. package/src/css/config-selector.css +38 -3
  78. package/src/css/dom/select.css +2 -1
  79. package/src/css/form/code-editor.css +6 -7
  80. package/src/css/form/mirrored-textarea.css +81 -0
  81. package/src/css/viewer.css +328 -0
  82. package/src/rust/agent/client/mod.rs +19 -0
  83. package/src/rust/agent/client/protocol.rs +320 -0
  84. package/src/rust/agent/client/transport.rs +287 -0
  85. package/src/rust/agent/client/turn.rs +174 -0
  86. package/src/rust/agent/config.rs +159 -0
  87. package/src/rust/agent/docs.rs +293 -0
  88. package/src/rust/agent/markdown/block.rs +509 -0
  89. package/src/rust/agent/markdown/entity.rs +52 -0
  90. package/src/rust/agent/markdown/events.rs +71 -0
  91. package/src/rust/agent/markdown/inline.rs +497 -0
  92. package/src/rust/agent/markdown/mod.rs +466 -0
  93. package/src/rust/agent/mod.rs +23 -0
  94. package/src/rust/agent/preamble.md +16 -0
  95. package/src/rust/agent/runtime.rs +201 -0
  96. package/src/rust/agent/slot.rs +236 -0
  97. package/src/rust/agent/tools.rs +1141 -0
  98. package/src/rust/components/chat_panel.rs +289 -0
  99. package/src/rust/components/column_selector/active_column.rs +2 -2
  100. package/src/rust/components/column_selector/add_expression_button.rs +3 -3
  101. package/src/rust/components/column_selector/aggregate_selector.rs +2 -3
  102. package/src/rust/components/column_selector/config_selector.rs +84 -7
  103. package/src/rust/components/column_selector/expr_edit_button.rs +4 -10
  104. package/src/rust/components/column_selector/inactive_column.rs +2 -2
  105. package/src/rust/components/column_selector.rs +55 -4
  106. package/src/rust/components/column_settings_sidebar/style_tab/primitive_field.rs +293 -126
  107. package/src/rust/components/column_settings_sidebar/style_tab.rs +262 -225
  108. package/src/rust/components/column_settings_sidebar.rs +88 -69
  109. package/src/rust/components/containers/dragdrop_list.rs +37 -0
  110. package/src/rust/components/editable_header.rs +85 -144
  111. package/src/rust/components/expression_editor.rs +9 -16
  112. package/src/rust/components/form/code_editor.rs +29 -28
  113. package/src/rust/components/form/debug.rs +9 -5
  114. package/src/rust/components/form/mirrored_textarea.rs +165 -0
  115. package/src/rust/components/form/mod.rs +4 -1
  116. package/src/rust/components/form/multi_stop_gradient_selector.rs +476 -0
  117. package/src/rust/components/form/named_value_picker.rs +86 -0
  118. package/src/rust/components/form/palette_selector.rs +440 -0
  119. package/src/rust/components/main_panel/msg.rs +2 -2
  120. package/src/rust/components/main_panel/reconcile.rs +4 -4
  121. package/src/rust/components/mod.rs +2 -0
  122. package/src/rust/components/plugin_tab.rs +4 -28
  123. package/src/rust/components/settings_panel.rs +105 -22
  124. package/src/rust/components/viewer/msg.rs +46 -7
  125. package/src/rust/components/viewer/panels.rs +17 -20
  126. package/src/rust/components/viewer/render.rs +10 -3
  127. package/src/rust/components/viewer/settings.rs +163 -23
  128. package/src/rust/components/viewer/snapshots.rs +46 -21
  129. package/src/rust/components/viewer/wiring.rs +14 -25
  130. package/src/rust/components/viewer.rs +12 -8
  131. package/src/rust/components/window_editor.rs +75 -103
  132. package/src/rust/config/column_config_schema.rs +100 -41
  133. package/src/rust/config/css_scale.rs +882 -0
  134. package/src/rust/config/datetime_column_style/custom.rs +17 -0
  135. package/src/rust/config/datetime_column_style/simple.rs +10 -0
  136. package/src/rust/config/mod.rs +2 -0
  137. package/src/rust/config/number_string_format.rs +33 -0
  138. package/src/rust/config/options.rs +28 -0
  139. package/src/rust/config/plugin_static_config.rs +120 -5
  140. package/src/rust/config/viewer_config.rs +216 -3
  141. package/src/rust/config/workspace_config.rs +138 -10
  142. package/src/rust/custom_elements/viewer.rs +369 -119
  143. package/src/rust/custom_events.rs +43 -1
  144. package/src/rust/js/regular_layout.rs +3 -3
  145. package/src/rust/lib.rs +15 -0
  146. package/src/rust/presentation/column_locator.rs +19 -5
  147. package/src/rust/presentation.rs +200 -36
  148. package/src/rust/queries/column_locator.rs +74 -44
  149. package/src/rust/queries/get_viewer_config.rs +2 -1
  150. package/src/rust/queries/mod.rs +2 -0
  151. package/src/rust/queries/palette_set.rs +82 -0
  152. package/src/rust/queries/plugin_column_styles.rs +6 -2
  153. package/src/rust/renderer/dispatch.rs +16 -4
  154. package/src/rust/renderer/plugin_config.rs +309 -47
  155. package/src/rust/renderer.rs +15 -42
  156. package/src/rust/session/column_defaults_update.rs +32 -7
  157. package/src/rust/session/metadata.rs +23 -21
  158. package/src/rust/session/props.rs +1 -0
  159. package/src/rust/session/replace_expression_update.rs +1 -0
  160. package/src/rust/session.rs +9 -6
  161. package/src/rust/tasks/create_panel.rs +46 -49
  162. package/src/rust/tasks/edit_expression.rs +19 -23
  163. package/src/rust/tasks/edit_window.rs +9 -11
  164. package/src/rust/tasks/eject.rs +4 -1
  165. package/src/rust/tasks/pipeline.rs +2 -2
  166. package/src/rust/tasks/presize_panels.rs +44 -5
  167. package/src/rust/tasks/reset_all.rs +4 -4
  168. package/src/rust/tasks/restore_and_render.rs +17 -44
  169. package/src/rust/tasks/restore_panel.rs +37 -22
  170. package/src/rust/tasks/set_edit_mode.rs +3 -1
  171. package/src/rust/tasks/sync_update_panels.rs +18 -24
  172. package/src/rust/tasks/update_theme.rs +23 -18
  173. package/src/rust/utils/css_vars.rs +26 -0
  174. package/src/rust/utils/mod.rs +2 -0
  175. package/src/rust/utils/spawn.rs +1 -1
  176. package/src/rust/workspace.rs +454 -199
  177. package/src/svg/lock.svg +7 -0
  178. package/src/themes/botanical.css +10 -0
  179. package/src/themes/defaults.css +12 -0
  180. package/src/themes/dracula.css +17 -0
  181. package/src/themes/gruvbox-dark.css +20 -0
  182. package/src/themes/gruvbox.css +10 -0
  183. package/src/themes/icons.css +1 -0
  184. package/src/themes/intl/de.css +16 -0
  185. package/src/themes/intl/es.css +16 -0
  186. package/src/themes/intl/fr.css +16 -0
  187. package/src/themes/intl/ja.css +16 -0
  188. package/src/themes/intl/pt.css +16 -0
  189. package/src/themes/intl/zh.css +16 -0
  190. package/src/themes/intl.css +16 -0
  191. package/src/themes/monokai.css +22 -0
  192. package/src/themes/phosphor.css +5 -0
  193. package/src/themes/pro-dark.css +10 -0
  194. package/src/themes/solarized-dark.css +7 -0
  195. package/src/themes/solarized.css +7 -0
  196. package/src/themes/vaporwave.css +20 -0
  197. package/src/ts/column-format.ts +67 -46
  198. package/src/ts/perspective-viewer.ts +3 -0
  199. package/src/ts/plugin.ts +14 -0
  200. package/src/ts/providers.ts +96 -0
  201. package/src/ts/ts-rs/CompactDisplay.ts +3 -0
  202. package/src/ts/ts-rs/CurrencyCode.ts +3 -0
  203. package/src/ts/ts-rs/CurrencyDisplay.ts +3 -0
  204. package/src/ts/ts-rs/CurrencyNumberFormatStyle.ts +6 -0
  205. package/src/ts/ts-rs/CurrencySign.ts +3 -0
  206. package/src/ts/ts-rs/CustomDatetimeFormat.ts +3 -0
  207. package/src/ts/ts-rs/CustomDatetimeStyleConfig.ts +54 -0
  208. package/src/ts/ts-rs/CustomNumberFormatConfig.ts +25 -0
  209. package/src/ts/ts-rs/DatetimeColorMode.ts +3 -0
  210. package/src/ts/ts-rs/DatetimeFormatType.ts +8 -0
  211. package/src/ts/ts-rs/FormatMode.ts +3 -0
  212. package/src/ts/ts-rs/FormatUnit.ts +3 -0
  213. package/src/ts/ts-rs/Notation.ts +11 -0
  214. package/src/ts/ts-rs/NumberFormatStyle.ts +12 -0
  215. package/src/ts/ts-rs/PanelViewerConfig.ts +48 -2
  216. package/src/ts/ts-rs/PluginStaticConfig.ts +36 -1
  217. package/src/ts/ts-rs/RestoreOptions.ts +20 -0
  218. package/src/ts/ts-rs/RoundingMode.ts +3 -0
  219. package/src/ts/ts-rs/RoundingPriority.ts +3 -0
  220. package/src/ts/ts-rs/SaveWorkspaceOptions.ts +11 -0
  221. package/src/ts/ts-rs/SignDisplay.ts +3 -0
  222. package/src/ts/ts-rs/SimpleDatetimeFormat.ts +3 -0
  223. package/src/ts/ts-rs/SimpleDatetimeStyleConfig.ts +25 -0
  224. package/src/ts/ts-rs/SplitRollupMode.ts +11 -0
  225. package/src/ts/ts-rs/StringColorMode.ts +3 -0
  226. package/src/ts/ts-rs/TrailingZeroDisplay.ts +3 -0
  227. package/src/ts/ts-rs/Unit.ts +3 -0
  228. package/src/ts/ts-rs/UnitDisplay.ts +3 -0
  229. package/src/ts/ts-rs/UnitNumberFormatStyle.ts +5 -0
  230. package/src/ts/ts-rs/UseGrouping.ts +3 -0
  231. package/src/ts/ts-rs/ViewerConfig.ts +48 -2
  232. package/src/ts/ts-rs/ViewerConfigInitial.ts +164 -0
  233. package/src/ts/ts-rs/ViewerConfigUpdate.ts +72 -2
  234. package/src/ts/ts-rs/WindowSpec.ts +3 -2
  235. package/src/ts/ts-rs/WorkspaceConfig.ts +8 -2
  236. package/src/ts/ts-rs/WorkspaceConfigUpdate.ts +18 -9
  237. package/dist/esm/ts-rs/WindowAggregate.d.ts +0 -1
  238. package/src/rust/components/form/color_range_selector.rs +0 -137
  239. package/src/ts/ts-rs/WindowAggregate.ts +0 -3
  240. /package/dist/wasm/snippets/{perspective-viewer-8a8c7d5d696b43ae → perspective-viewer-39c3c3b28e3cbd48}/inline0.js +0 -0
  241. /package/dist/wasm/snippets/{perspective-viewer-8a8c7d5d696b43ae → perspective-viewer-39c3c3b28e3cbd48}/inline1.js +0 -0
  242. /package/dist/wasm/snippets/{perspective-viewer-8a8c7d5d696b43ae → perspective-viewer-39c3c3b28e3cbd48}/inline2.js +0 -0
  243. /package/dist/wasm/snippets/{perspective-viewer-8a8c7d5d696b43ae → perspective-viewer-39c3c3b28e3cbd48}/inline3.js +0 -0
  244. /package/dist/wasm/snippets/{perspective-viewer-8a8c7d5d696b43ae → perspective-viewer-39c3c3b28e3cbd48}/inline4.js +0 -0
  245. /package/dist/wasm/snippets/{perspective-viewer-8a8c7d5d696b43ae → perspective-viewer-39c3c3b28e3cbd48}/inline5.js +0 -0
@@ -24,7 +24,7 @@ use crate::renderer::ColumnConfigMap;
24
24
 
25
25
  /// The state of an entire `custom_elements::PerspectiveViewerElement` component
26
26
  /// and its `Plugin`: the element-level `settings` flag plus the per-panel
27
- /// [`PanelViewerConfig`]. The split exists so the whole-element config format
27
+ /// [`PanelViewerConfig`]. The split exists so the workspace config format
28
28
  /// can serialize panel entries *without* a `settings` key (it is element-level
29
29
  /// state there, carried by the top-level `active` field instead), while the
30
30
  /// single-panel format flattens back to the legacy shape.
@@ -39,15 +39,45 @@ pub struct ViewerConfig<V: TS = String> {
39
39
 
40
40
  /// The per-panel state of a [`ViewerConfig`] — everything except the
41
41
  /// element-level `settings` flag. This is the `panels` entry type of the
42
- /// whole-element config format.
42
+ /// workspace config format.
43
43
  #[derive(Debug, Default, Serialize, PartialEq, TS)]
44
44
  pub struct PanelViewerConfig<V: TS = String> {
45
+ /// The `@perspective-dev/viewer` version that wrote this config,
46
+ /// stamped on save and used to migrate older tokens. Callers do not
47
+ /// set it.
45
48
  pub version: V,
49
+
50
+ /// Per-column styling, keyed by column name. The viewer treats the
51
+ /// values as opaque — their shape is defined by the ACTIVE plugin,
52
+ /// so a config written for one plugin may carry keys another
53
+ /// ignores. Query the live shape with the `get_style_schema` agent
54
+ /// tool, or the plugin's `column_config_schema()`.
46
55
  pub columns_config: ColumnConfigMap,
56
+
57
+ /// Name of the visualization plugin, from the set registered on the
58
+ /// page (the `list_plugins` agent tool, or the plugin picker). This
59
+ /// also decides what the view fields MEAN visually: `columns` is
60
+ /// positional and each plugin reads the positions differently, and
61
+ /// `group_by`/`split_by` draw different things per plugin.
47
62
  pub plugin: String,
63
+
64
+ /// Plugin-wide settings (as opposed to the per-column
65
+ /// [`Self::columns_config`]). Opaque to the viewer and defined by
66
+ /// the active plugin; see `get_style_schema`.
48
67
  pub plugin_config: serde_json::Map<String, Value>,
49
- pub table: Option<String>,
68
+
69
+ /// Name of the `Table` this panel renders, as hosted on the panel's
70
+ /// `Client`. Every placed panel has a table binding (creation
71
+ /// requires one by type — [`ViewerConfigInitial`]), so the saved
72
+ /// config carries it unconditionally.
73
+ pub table: String,
74
+
75
+ /// Selected theme NAME (e.g. `"Pro Dark"`) — not a CSS value. Valid
76
+ /// names are the Perspective themes loaded on the page, which
77
+ /// `resetThemes()` re-scans. `None` selects the first available.
50
78
  pub theme: Option<String>,
79
+
80
+ /// Panel title, shown in its tab. `None` renders the default title.
51
81
  pub title: Option<String>,
52
82
 
53
83
  #[serde(flatten)]
@@ -93,41 +123,73 @@ impl Deref for PluginConfig {
93
123
 
94
124
  #[derive(Clone, Debug, Default, Deserialize, PartialEq, Serialize, TS)]
95
125
  pub struct ViewerConfigUpdate {
126
+ /// The `@perspective-dev/viewer` version a saved config was written
127
+ /// by, used to migrate older tokens. Omit it — the viewer stamps
128
+ /// the current version on save.
96
129
  #[serde(default)]
97
130
  #[ts(as = "Option<_>")]
98
131
  #[ts(optional)]
99
132
  pub version: VersionUpdate,
100
133
 
134
+ /// Name of the visualization plugin to switch to, from the set
135
+ /// registered on the page (the `list_plugins` agent tool, or the
136
+ /// plugin picker). Changing it changes what the view fields MEAN:
137
+ /// `columns` is positional and every plugin reads the positions
138
+ /// differently, and `group_by`/`split_by` draw different things per
139
+ /// plugin — so read the new plugin's roles before writing `columns`
140
+ /// for it.
101
141
  #[serde(default)]
102
142
  #[ts(as = "Option<_>")]
103
143
  #[ts(optional)]
104
144
  pub plugin: PluginUpdate,
105
145
 
146
+ /// Panel title, shown in its tab. `null` restores the default
147
+ /// title; omitting the field leaves the current one.
106
148
  #[serde(default)]
107
149
  #[ts(as = "Option<_>")]
108
150
  #[ts(optional)]
109
151
  pub title: TitleUpdate,
110
152
 
153
+ /// Name of the `Table` to render, as hosted on this panel's
154
+ /// `Client`. Rebinding an existing panel to another table keeps the
155
+ /// rest of the config, so column names that do not exist in the new
156
+ /// table will fail validation.
111
157
  #[serde(default)]
112
158
  #[ts(as = "Option<_>")]
113
159
  #[ts(optional)]
114
160
  pub table: TableUpdate,
115
161
 
162
+ /// Theme NAME (e.g. `"Pro Dark"`) — not a CSS value. Valid names are
163
+ /// the Perspective themes loaded on the page, re-scanned by
164
+ /// `resetThemes()`. `null` selects the default.
116
165
  #[serde(default)]
117
166
  #[ts(as = "Option<_>")]
118
167
  #[ts(optional)]
119
168
  pub theme: ThemeUpdate,
120
169
 
170
+ /// Whether the settings sidebar is OPEN. Purely cosmetic chrome —
171
+ /// it does not affect what the viewer renders, and it is
172
+ /// element-level rather than per-panel.
121
173
  #[serde(default)]
122
174
  #[ts(as = "Option<_>")]
123
175
  #[ts(optional)]
124
176
  pub settings: SettingsUpdate,
125
177
 
178
+ /// Plugin-wide settings (as opposed to the per-column
179
+ /// [`Self::columns_config`]). The viewer passes these through
180
+ /// opaquely — their valid keys are defined by the ACTIVE plugin and
181
+ /// vary by plugin and by state, so query them with the
182
+ /// `get_style_schema` agent tool rather than guessing.
126
183
  #[serde(default)]
127
184
  #[ts(as = "Option<_>")]
128
185
  #[ts(optional)]
129
186
  pub plugin_config: PluginConfigUpdate,
130
187
 
188
+ /// Per-column styling — formatting, colors, and other per-column
189
+ /// controls — keyed by column name. Opaque to the viewer and
190
+ /// plugin-defined like [`Self::plugin_config`]; `get_style_schema`
191
+ /// reports the valid keys for a given column under the active
192
+ /// plugin.
131
193
  #[serde(default)]
132
194
  #[ts(as = "Option<_>")]
133
195
  #[ts(optional)]
@@ -150,6 +212,157 @@ impl ViewerConfigUpdate {
150
212
  }
151
213
  }
152
214
 
215
+ /// The initial configuration of a NEW panel (`addPanel`, `restore`'s
216
+ /// panel-creating upsert, `restoreWorkspace` `panels` entries). Unlike
217
+ /// [`ViewerConfigUpdate`] — a patch against existing state — creation has
218
+ /// no prior state: `table` is REQUIRED (a placed panel without a table
219
+ /// binding would be permanently blank), absent fields mean "default"
220
+ /// rather than "leave unchanged" (so no [`OptionalUpdate`] tri-state), and
221
+ /// there is no `settings` field (element-level, not per-panel).
222
+ #[derive(Clone, Debug, Deserialize, PartialEq, Serialize, TS)]
223
+ pub struct ViewerConfigInitial {
224
+ /// Name of the `Table` the new panel renders, as hosted on the
225
+ /// `Client`. REQUIRED: a placed panel with no table binding would be
226
+ /// permanently blank.
227
+ pub table: String,
228
+
229
+ /// The `@perspective-dev/viewer` version a saved config was written
230
+ /// by. Omit it when creating a panel.
231
+ #[ts(optional)]
232
+ pub version: Option<String>,
233
+
234
+ /// Name of the visualization plugin, from the set registered on the
235
+ /// page (the `list_plugins` agent tool, or the plugin picker).
236
+ /// Decides what the view fields MEAN: `columns` is positional and
237
+ /// every plugin reads the positions differently, and
238
+ /// `group_by`/`split_by` draw different things per plugin. Absent
239
+ /// uses the default plugin.
240
+ #[ts(optional)]
241
+ pub plugin: Option<String>,
242
+
243
+ /// Panel title, shown in its tab. Absent renders the default title.
244
+ #[ts(optional)]
245
+ pub title: Option<String>,
246
+
247
+ /// Theme NAME (e.g. `"Pro Dark"`) — not a CSS value. Valid names are
248
+ /// the Perspective themes loaded on the page. Absent uses the
249
+ /// default.
250
+ #[ts(optional)]
251
+ pub theme: Option<String>,
252
+
253
+ /// Plugin-wide settings (as opposed to the per-column
254
+ /// [`Self::columns_config`]). Opaque to the viewer and defined by
255
+ /// the ACTIVE plugin; query the valid keys with `get_style_schema`
256
+ /// rather than guessing.
257
+ #[ts(optional)]
258
+ pub plugin_config: Option<serde_json::Map<String, Value>>,
259
+
260
+ /// Per-column styling — formatting, colors, and other per-column
261
+ /// controls — keyed by column name. Plugin-defined like
262
+ /// [`Self::plugin_config`]; see `get_style_schema`.
263
+ #[ts(optional)]
264
+ pub columns_config: Option<ColumnConfigMap>,
265
+
266
+ #[serde(flatten)]
267
+ pub view_config: ViewConfigUpdate,
268
+ }
269
+
270
+ impl ViewerConfigInitial {
271
+ /// Decode a `JsValue` by auto-detecting format from JavaScript type.
272
+ pub fn decode(config: &JsValue) -> ApiResult<Self> {
273
+ Ok(config.into_serde_ext()?)
274
+ }
275
+
276
+ /// A default-config panel bound to `table` (the "New panel" menus).
277
+ /// Exhaustive on purpose — same drift alarm as the `From` impl below.
278
+ pub fn new(table: impl Into<String>) -> Self {
279
+ Self {
280
+ table: table.into(),
281
+ version: None,
282
+ plugin: None,
283
+ title: None,
284
+ theme: None,
285
+ plugin_config: None,
286
+ columns_config: None,
287
+ view_config: ViewConfigUpdate::default(),
288
+ }
289
+ }
290
+ }
291
+
292
+ fn up<T: Clone>(value: Option<T>) -> OptionalUpdate<T> {
293
+ match value {
294
+ Some(value) => OptionalUpdate::Update(value),
295
+ None => OptionalUpdate::Missing,
296
+ }
297
+ }
298
+
299
+ // Constructed EXHAUSTIVELY on purpose: a field added to
300
+ // `ViewerConfigUpdate` breaks this impl at compile time, forcing the
301
+ // "should creation carry it?" decision (the drift alarm).
302
+ impl From<ViewerConfigInitial> for ViewerConfigUpdate {
303
+ fn from(value: ViewerConfigInitial) -> Self {
304
+ ViewerConfigUpdate {
305
+ version: up(value.version),
306
+ plugin: up(value.plugin),
307
+ title: up(value.title),
308
+ table: OptionalUpdate::Update(value.table),
309
+ theme: up(value.theme),
310
+ settings: OptionalUpdate::Missing,
311
+ plugin_config: up(value.plugin_config),
312
+ columns_config: up(value.columns_config),
313
+ view_config: value.view_config,
314
+ }
315
+ }
316
+ }
317
+
318
+ /// The rejection every panel-creating route without a `table` resolves to.
319
+ pub const CREATE_REQUIRES_TABLE: &str = "Cannot create a panel without a `table` — `load()` a \
320
+ `Client` and include `table` in the config, or use \
321
+ `addPanel()`";
322
+
323
+ fn down<T: Clone>(value: OptionalUpdate<T>) -> Option<T> {
324
+ match value {
325
+ OptionalUpdate::Update(value) => Some(value),
326
+ OptionalUpdate::Missing | OptionalUpdate::SetDefault => None,
327
+ }
328
+ }
329
+
330
+ impl TryFrom<ViewerConfigUpdate> for ViewerConfigInitial {
331
+ type Error = ApiError;
332
+
333
+ fn try_from(value: ViewerConfigUpdate) -> Result<Self, Self::Error> {
334
+ // Exhaustive (no `..`) on purpose — the same drift alarm as the
335
+ // `From<ViewerConfigInitial>` impl above. `settings` is element-level
336
+ // state, stripped by `restore()` before resolution; discarded here.
337
+ let ViewerConfigUpdate {
338
+ version,
339
+ plugin,
340
+ title,
341
+ table,
342
+ theme,
343
+ settings: _settings,
344
+ plugin_config,
345
+ columns_config,
346
+ view_config,
347
+ } = value;
348
+
349
+ let OptionalUpdate::Update(table) = table else {
350
+ return Err(ApiError::new(CREATE_REQUIRES_TABLE));
351
+ };
352
+
353
+ Ok(Self {
354
+ table,
355
+ version: down(version),
356
+ plugin: down(plugin),
357
+ title: down(title),
358
+ theme: down(theme),
359
+ plugin_config: down(plugin_config),
360
+ columns_config: down(columns_config),
361
+ view_config,
362
+ })
363
+ }
364
+ }
365
+
153
366
  impl std::fmt::Display for ViewerConfigUpdate {
154
367
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
155
368
  write!(
@@ -14,9 +14,9 @@ use std::collections::BTreeMap;
14
14
 
15
15
  use perspective_client::config::Filter;
16
16
 
17
- use crate::config::{PanelViewerConfig, ViewerConfigUpdate};
17
+ use crate::config::{CssKind, PanelViewerConfig, ViewerConfigInitial};
18
18
 
19
- /// The whole-element config format (`{version, active?, layout, panels}`) —
19
+ /// The workspace config format (`{version, active?, layout, panels}`) —
20
20
  /// the multi-panel counterpart of the single-panel [`ViewerConfig`] — as
21
21
  /// emitted by [`PerspectiveViewerElement::save`].
22
22
  ///
@@ -56,13 +56,22 @@ pub struct WorkspaceConfig {
56
56
  #[ts(as = "Option<_>")]
57
57
  #[ts(optional)]
58
58
  pub masters: Vec<String>,
59
+
60
+ /// Named color-scale definitions shared by every panel: CSS custom
61
+ /// property name (`--psp-user--<kind>-<name>`) → canonical CSS
62
+ /// value.
63
+ #[serde(skip_serializing_if = "BTreeMap::is_empty")]
64
+ #[ts(as = "Option<_>")]
65
+ #[ts(optional)]
66
+ pub palette: BTreeMap<String, String>,
59
67
  }
60
68
 
61
- /// The parse target of a whole-element config in
62
- /// [`PerspectiveViewerElement::restore`]. Mirrors [`WorkspaceConfig`],
63
- /// but `panels` entries are full [`ViewerConfigUpdate`]s so a stray per-panel
64
- /// `settings` can be detected (warned, then ignored — `create_panel` strips
65
- /// it).
69
+ /// The parse target of a workspace config in
70
+ /// [`PerspectiveViewerElement::restoreWorkspace`]. Mirrors
71
+ /// [`WorkspaceConfig`], but `panels` entries are [`ViewerConfigInitial`]s —
72
+ /// every entry creates a NEW panel, so `table` is required by type (a
73
+ /// stray per-panel `settings` key is ignored; it is element-level state,
74
+ /// carried by the top-level `active` field).
66
75
  #[derive(serde::Deserialize, ts_rs::TS)]
67
76
  pub struct WorkspaceConfigUpdate {
68
77
  #[serde(default)]
@@ -73,20 +82,139 @@ pub struct WorkspaceConfigUpdate {
73
82
  #[ts(optional)]
74
83
  pub layout: Option<crate::js::Layout>,
75
84
 
76
- pub panels: BTreeMap<String, ViewerConfigUpdate>,
85
+ pub panels: BTreeMap<String, ViewerConfigInitial>,
77
86
 
78
87
  /// The element-level global (master/detail cross-) filters to re-apply as
79
88
  /// a transient overlay on every DETAIL panel. Restored as one
80
89
  /// unattributed bucket: the next selection on any master replaces it.
90
+ /// `Option` so an explicit `undefined` property deserializes as
91
+ /// `None` like an absent key (also `masters` / `palette` below).
81
92
  #[serde(default)]
82
93
  #[ts(as = "Option<_>")]
83
94
  #[ts(optional)]
84
- pub global_filters: Vec<Filter>,
95
+ pub global_filters: Option<Vec<Filter>>,
85
96
 
86
97
  /// The master (filter-source) panels, by saved `panels` key. An id not in
87
98
  /// `panels` warns and is dropped.
88
99
  #[serde(default)]
89
100
  #[ts(as = "Option<_>")]
90
101
  #[ts(optional)]
91
- pub masters: Vec<String>,
102
+ pub masters: Option<Vec<String>>,
103
+
104
+ /// Named color-scale definitions to apply to the host (see
105
+ /// [`WorkspaceConfig::palette`]), replacing any previously restored
106
+ /// palette.
107
+ #[serde(default)]
108
+ #[ts(as = "Option<_>")]
109
+ #[ts(optional)]
110
+ pub palette: Option<BTreeMap<String, String>>,
111
+ }
112
+
113
+ /// Validate a restored palette map: each key's `--psp-user--<kind>-`
114
+ /// prefix selects the reader that canonicalizes its value.
115
+ pub fn validate_palette(
116
+ palette: BTreeMap<String, String>,
117
+ ) -> Result<BTreeMap<String, String>, String> {
118
+ palette
119
+ .into_iter()
120
+ .map(|(name, value)| {
121
+ let kind = CssKind::of_var(&name).ok_or_else(|| {
122
+ format!(
123
+ "`palette` key `{name}` must start with `--psp-user--gradient-`, \
124
+ `--psp-user--palette-` or `--psp-user--color-`"
125
+ )
126
+ })?;
127
+
128
+ let canonical = kind
129
+ .canonicalize(&value)
130
+ .map_err(|error| format!("`palette[\"{name}\"]`: {error}"))?;
131
+
132
+ Ok((name, canonical))
133
+ })
134
+ .collect()
135
+ }
136
+
137
+ #[cfg(test)]
138
+ mod tests {
139
+ use serde_json::json;
140
+
141
+ use super::*;
142
+
143
+ #[test]
144
+ fn palette_serializes_only_when_present() {
145
+ let config = WorkspaceConfig {
146
+ version: "x".to_owned(),
147
+ active: None,
148
+ layout: None,
149
+ panels: BTreeMap::new(),
150
+ global_filters: vec![],
151
+ masters: vec![],
152
+ palette: BTreeMap::new(),
153
+ };
154
+
155
+ assert_eq!(
156
+ serde_json::to_value(&config).unwrap(),
157
+ json!({ "version": "x", "layout": null, "panels": {} })
158
+ );
159
+
160
+ let mut palette = BTreeMap::new();
161
+ palette.insert("--psp-user--color-hot".to_owned(), "#ff0000".to_owned());
162
+
163
+ let config = WorkspaceConfig { palette, ..config };
164
+ assert_eq!(
165
+ serde_json::to_value(&config).unwrap(),
166
+ json!({
167
+ "version": "x",
168
+ "layout": null,
169
+ "panels": {},
170
+ "palette": { "--psp-user--color-hot": "#ff0000" },
171
+ })
172
+ );
173
+ }
174
+
175
+ #[test]
176
+ fn update_palette_defaults_empty_and_validates_by_prefix() {
177
+ let update: WorkspaceConfigUpdate =
178
+ serde_json::from_value(json!({ "panels": {} })).unwrap();
179
+ assert!(update.palette.is_none());
180
+
181
+ let update: WorkspaceConfigUpdate = serde_json::from_value(json!({
182
+ "panels": {},
183
+ "palette": {
184
+ "--psp-user--gradient-1": "linear-gradient(#000, #fff)",
185
+ "--psp-user--palette-warm": "linear-gradient(90deg, RGB(255,0,0), #ff0)",
186
+ "--psp-user--color-hot": "#F00",
187
+ },
188
+ }))
189
+ .unwrap();
190
+
191
+ let valid = validate_palette(update.palette.unwrap()).unwrap();
192
+ assert_eq!(
193
+ valid.get("--psp-user--gradient-1").unwrap(),
194
+ "linear-gradient(to right, #000000 0%, #ffffff 100%)"
195
+ );
196
+ assert_eq!(
197
+ valid.get("--psp-user--palette-warm").unwrap(),
198
+ "linear-gradient(to right, #ff0000, #ffff00)"
199
+ );
200
+ assert_eq!(valid.get("--psp-user--color-hot").unwrap(), "#ff0000");
201
+
202
+ let bad = |name: &str, value: &str| {
203
+ let mut map = BTreeMap::new();
204
+ map.insert(name.to_owned(), value.to_owned());
205
+ validate_palette(map).unwrap_err()
206
+ };
207
+
208
+ assert!(bad("--psp-user--other-1", "#ff0000").contains("--psp-user--other-1"));
209
+ assert!(bad("--psp-charts--gradient", "#ff0000").contains("must start with"));
210
+ assert!(
211
+ bad(
212
+ "--psp-user--palette-1",
213
+ "linear-gradient(#000 0%, #fff 100%)"
214
+ )
215
+ .contains("--psp-user--palette-1")
216
+ );
217
+ assert!(bad("--psp-user--gradient-1", "#ff0000").contains("linear-gradient"));
218
+ assert!(bad("--psp-user--color-1", "red").contains("red"));
219
+ }
92
220
  }