matterviz 0.3.2 → 0.3.4

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 (281) hide show
  1. package/dist/EmptyState.svelte +10 -2
  2. package/dist/FilePicker.svelte +123 -82
  3. package/dist/Icon.svelte +18 -12
  4. package/dist/MillerIndexInput.svelte +27 -21
  5. package/dist/api/optimade.js +6 -6
  6. package/dist/app.css +216 -207
  7. package/dist/brillouin/BrillouinZone.svelte +292 -149
  8. package/dist/brillouin/BrillouinZone.svelte.d.ts +1 -1
  9. package/dist/brillouin/BrillouinZoneControls.svelte +32 -5
  10. package/dist/brillouin/BrillouinZoneExportPane.svelte +69 -42
  11. package/dist/brillouin/BrillouinZoneExportPane.svelte.d.ts +1 -1
  12. package/dist/brillouin/BrillouinZoneInfoPane.svelte +99 -68
  13. package/dist/brillouin/BrillouinZoneScene.svelte +275 -163
  14. package/dist/brillouin/BrillouinZoneScene.svelte.d.ts +1 -1
  15. package/dist/brillouin/BrillouinZoneTooltip.svelte +17 -7
  16. package/dist/brillouin/compute.js +11 -6
  17. package/dist/chempot-diagram/ChemPotDiagram.svelte +162 -27
  18. package/dist/chempot-diagram/ChemPotDiagram2D.svelte +451 -281
  19. package/dist/chempot-diagram/ChemPotDiagram3D.svelte +2148 -1642
  20. package/dist/chempot-diagram/ChemPotScene3D.svelte +8 -5
  21. package/dist/chempot-diagram/async-compute.svelte.d.ts +3 -0
  22. package/dist/chempot-diagram/async-compute.svelte.js +77 -0
  23. package/dist/chempot-diagram/chempot-worker.d.ts +1 -0
  24. package/dist/chempot-diagram/chempot-worker.js +11 -0
  25. package/dist/chempot-diagram/color.js +1 -2
  26. package/dist/chempot-diagram/compute.d.ts +10 -0
  27. package/dist/chempot-diagram/compute.js +250 -88
  28. package/dist/chempot-diagram/index.d.ts +2 -1
  29. package/dist/chempot-diagram/index.js +2 -1
  30. package/dist/chempot-diagram/temperature.js +8 -9
  31. package/dist/chempot-diagram/types.d.ts +3 -0
  32. package/dist/chempot-diagram/types.js +1 -0
  33. package/dist/colors/index.d.ts +1 -1
  34. package/dist/colors/index.js +5 -3
  35. package/dist/composition/BarChart.svelte +128 -55
  36. package/dist/composition/BubbleChart.svelte +102 -49
  37. package/dist/composition/Composition.svelte +100 -79
  38. package/dist/composition/Formula.svelte +108 -62
  39. package/dist/composition/FormulaFilter.svelte +665 -537
  40. package/dist/composition/PieChart.svelte +183 -108
  41. package/dist/composition/format.d.ts +5 -0
  42. package/dist/composition/format.js +20 -3
  43. package/dist/composition/parse.js +14 -9
  44. package/dist/convex-hull/ConvexHull.svelte +93 -40
  45. package/dist/convex-hull/ConvexHull.svelte.d.ts +1 -1
  46. package/dist/convex-hull/ConvexHull2D.svelte +549 -360
  47. package/dist/convex-hull/ConvexHull2D.svelte.d.ts +1 -1
  48. package/dist/convex-hull/ConvexHull3D.svelte +1296 -827
  49. package/dist/convex-hull/ConvexHull3D.svelte.d.ts +1 -1
  50. package/dist/convex-hull/ConvexHull4D.svelte +1004 -688
  51. package/dist/convex-hull/ConvexHull4D.svelte.d.ts +1 -1
  52. package/dist/convex-hull/ConvexHullControls.svelte +115 -28
  53. package/dist/convex-hull/ConvexHullControls.svelte.d.ts +1 -1
  54. package/dist/convex-hull/ConvexHullInfoPane.svelte +29 -3
  55. package/dist/convex-hull/ConvexHullStats.svelte +425 -328
  56. package/dist/convex-hull/ConvexHullTooltip.svelte +40 -16
  57. package/dist/convex-hull/GasPressureControls.svelte +104 -61
  58. package/dist/convex-hull/StructurePopup.svelte +25 -4
  59. package/dist/convex-hull/TemperatureSlider.svelte +45 -25
  60. package/dist/convex-hull/barycentric-coords.js +13 -7
  61. package/dist/convex-hull/demo-temperature.js +8 -4
  62. package/dist/convex-hull/gas-thermodynamics.js +17 -12
  63. package/dist/convex-hull/helpers.d.ts +9 -0
  64. package/dist/convex-hull/helpers.js +77 -34
  65. package/dist/convex-hull/thermodynamics.js +61 -56
  66. package/dist/convex-hull/types.d.ts +9 -14
  67. package/dist/convex-hull/types.js +0 -17
  68. package/dist/coordination/CoordinationBarPlot.svelte +227 -154
  69. package/dist/element/BohrAtom.svelte +55 -12
  70. package/dist/element/ElementHeading.svelte +7 -2
  71. package/dist/element/ElementPhoto.svelte +15 -9
  72. package/dist/element/ElementStats.svelte +10 -4
  73. package/dist/element/ElementTile.svelte +137 -73
  74. package/dist/element/Nucleus.svelte +39 -11
  75. package/dist/element/data.js +1 -1
  76. package/dist/feedback/ClickFeedback.svelte +16 -5
  77. package/dist/feedback/DragOverlay.svelte +10 -2
  78. package/dist/feedback/Spinner.svelte +4 -2
  79. package/dist/feedback/StatusMessage.svelte +8 -2
  80. package/dist/fermi-surface/FermiSlice.svelte +118 -88
  81. package/dist/fermi-surface/FermiSurface.svelte +328 -187
  82. package/dist/fermi-surface/FermiSurface.svelte.d.ts +1 -1
  83. package/dist/fermi-surface/FermiSurfaceControls.svelte +113 -46
  84. package/dist/fermi-surface/FermiSurfaceControls.svelte.d.ts +1 -1
  85. package/dist/fermi-surface/FermiSurfaceScene.svelte +535 -342
  86. package/dist/fermi-surface/FermiSurfaceScene.svelte.d.ts +1 -1
  87. package/dist/fermi-surface/FermiSurfaceTooltip.svelte +14 -5
  88. package/dist/fermi-surface/compute.js +16 -20
  89. package/dist/fermi-surface/parse.js +24 -14
  90. package/dist/fermi-surface/symmetry.js +2 -7
  91. package/dist/fermi-surface/types.d.ts +3 -5
  92. package/dist/heatmap-matrix/HeatmapMatrix.svelte +1019 -765
  93. package/dist/heatmap-matrix/HeatmapMatrix.svelte.d.ts +1 -1
  94. package/dist/heatmap-matrix/HeatmapMatrixControls.svelte +76 -22
  95. package/dist/heatmap-matrix/HeatmapMatrixControls.svelte.d.ts +2 -3
  96. package/dist/icons.js +47 -0
  97. package/dist/index.d.ts +2 -1
  98. package/dist/index.js +2 -1
  99. package/dist/io/decompress.js +1 -1
  100. package/dist/io/export.d.ts +3 -0
  101. package/dist/io/export.js +129 -143
  102. package/dist/io/is-binary.js +2 -3
  103. package/dist/io/url-drop.js +1 -2
  104. package/dist/isosurface/Isosurface.svelte +202 -148
  105. package/dist/isosurface/IsosurfaceControls.svelte +46 -28
  106. package/dist/isosurface/parse.js +34 -29
  107. package/dist/isosurface/slice.js +5 -10
  108. package/dist/isosurface/types.d.ts +2 -1
  109. package/dist/isosurface/types.js +61 -12
  110. package/dist/labels.js +11 -8
  111. package/dist/layout/FullscreenToggle.svelte +11 -2
  112. package/dist/layout/InfoCard.svelte +38 -6
  113. package/dist/layout/InfoTag.svelte +63 -32
  114. package/dist/layout/PropertyFilter.svelte +82 -37
  115. package/dist/layout/SettingsSection.svelte +85 -55
  116. package/dist/layout/SubpageGrid.svelte +10 -2
  117. package/dist/layout/json-tree/JsonNode.svelte +183 -138
  118. package/dist/layout/json-tree/JsonTree.svelte +499 -413
  119. package/dist/layout/json-tree/JsonValue.svelte +127 -99
  120. package/dist/layout/json-tree/utils.js +4 -2
  121. package/dist/marching-cubes.js +25 -2
  122. package/dist/math.d.ts +13 -17
  123. package/dist/math.js +133 -67
  124. package/dist/overlays/ContextMenu.svelte +65 -40
  125. package/dist/overlays/DraggablePane.svelte +211 -139
  126. package/dist/periodic-table/PeriodicTable.svelte +278 -145
  127. package/dist/periodic-table/PeriodicTableControls.svelte +178 -128
  128. package/dist/periodic-table/PropertySelect.svelte +25 -7
  129. package/dist/periodic-table/TableInset.svelte +8 -3
  130. package/dist/phase-diagram/IsobaricBinaryPhaseDiagram.svelte +446 -309
  131. package/dist/phase-diagram/IsobaricBinaryPhaseDiagram.svelte.d.ts +1 -1
  132. package/dist/phase-diagram/PhaseDiagramControls.svelte +102 -43
  133. package/dist/phase-diagram/PhaseDiagramControls.svelte.d.ts +1 -1
  134. package/dist/phase-diagram/PhaseDiagramEditorPane.svelte +63 -40
  135. package/dist/phase-diagram/PhaseDiagramExportPane.svelte +71 -28
  136. package/dist/phase-diagram/PhaseDiagramExportPane.svelte.d.ts +1 -1
  137. package/dist/phase-diagram/PhaseDiagramTooltip.svelte +158 -101
  138. package/dist/phase-diagram/TdbInfoPanel.svelte +28 -4
  139. package/dist/phase-diagram/build-diagram.js +9 -9
  140. package/dist/phase-diagram/colors.js +1 -3
  141. package/dist/phase-diagram/parse.js +10 -9
  142. package/dist/phase-diagram/svg-to-diagram.js +53 -49
  143. package/dist/phase-diagram/utils.d.ts +1 -0
  144. package/dist/phase-diagram/utils.js +80 -25
  145. package/dist/plot/AxisLabel.svelte +28 -3
  146. package/dist/plot/BarPlot.svelte +1182 -734
  147. package/dist/plot/BarPlot.svelte.d.ts +2 -2
  148. package/dist/plot/BarPlotControls.svelte +31 -5
  149. package/dist/plot/BarPlotControls.svelte.d.ts +1 -1
  150. package/dist/plot/ColorBar.svelte +479 -329
  151. package/dist/plot/ColorScaleSelect.svelte +27 -6
  152. package/dist/plot/ElementScatter.svelte +36 -15
  153. package/dist/plot/FillArea.svelte +152 -95
  154. package/dist/plot/Histogram.svelte +934 -571
  155. package/dist/plot/Histogram.svelte.d.ts +1 -1
  156. package/dist/plot/HistogramControls.svelte +53 -9
  157. package/dist/plot/HistogramControls.svelte.d.ts +1 -1
  158. package/dist/plot/InteractiveAxisLabel.svelte +34 -11
  159. package/dist/plot/InteractiveAxisLabel.svelte.d.ts +1 -1
  160. package/dist/plot/Line.svelte +63 -28
  161. package/dist/plot/PlotControls.svelte +157 -114
  162. package/dist/plot/PlotControls.svelte.d.ts +1 -1
  163. package/dist/plot/PlotLegend.svelte +174 -91
  164. package/dist/plot/PlotTooltip.svelte +45 -6
  165. package/dist/plot/PortalSelect.svelte +175 -147
  166. package/dist/plot/ReferenceLine.svelte +76 -22
  167. package/dist/plot/ReferenceLine3D.svelte +132 -107
  168. package/dist/plot/ReferencePlane.svelte +146 -121
  169. package/dist/plot/ScatterPlot.svelte +1681 -1091
  170. package/dist/plot/ScatterPlot.svelte.d.ts +2 -2
  171. package/dist/plot/ScatterPlot3D.svelte +256 -131
  172. package/dist/plot/ScatterPlot3D.svelte.d.ts +2 -2
  173. package/dist/plot/ScatterPlot3DControls.svelte +113 -63
  174. package/dist/plot/ScatterPlot3DControls.svelte.d.ts +2 -1
  175. package/dist/plot/ScatterPlot3DScene.svelte +608 -403
  176. package/dist/plot/ScatterPlot3DScene.svelte.d.ts +2 -2
  177. package/dist/plot/ScatterPlotControls.svelte +65 -25
  178. package/dist/plot/ScatterPlotControls.svelte.d.ts +1 -1
  179. package/dist/plot/ScatterPoint.svelte +98 -26
  180. package/dist/plot/ScatterPoint.svelte.d.ts +1 -0
  181. package/dist/plot/SpacegroupBarPlot.svelte +142 -85
  182. package/dist/plot/Surface3D.svelte +159 -108
  183. package/dist/plot/ZeroLines.svelte +55 -3
  184. package/dist/plot/ZoomRect.svelte +4 -2
  185. package/dist/plot/axis-utils.js +1 -3
  186. package/dist/plot/data-cleaning.js +12 -28
  187. package/dist/plot/data-transform.js +2 -1
  188. package/dist/plot/fill-utils.js +2 -0
  189. package/dist/plot/layout.d.ts +4 -1
  190. package/dist/plot/layout.js +33 -14
  191. package/dist/plot/reference-line.d.ts +2 -2
  192. package/dist/plot/reference-line.js +7 -5
  193. package/dist/plot/scales.js +24 -36
  194. package/dist/plot/types.d.ts +11 -23
  195. package/dist/plot/types.js +6 -11
  196. package/dist/plot/utils/label-placement.d.ts +32 -15
  197. package/dist/plot/utils/label-placement.js +227 -66
  198. package/dist/plot/utils/series-visibility.js +2 -3
  199. package/dist/rdf/RdfPlot.svelte +143 -91
  200. package/dist/rdf/calc-rdf.js +4 -5
  201. package/dist/sanitize.d.ts +4 -0
  202. package/dist/sanitize.js +107 -0
  203. package/dist/settings.d.ts +18 -6
  204. package/dist/settings.js +46 -16
  205. package/dist/spectral/Bands.svelte +632 -453
  206. package/dist/spectral/BandsAndDos.svelte +90 -49
  207. package/dist/spectral/BrillouinBandsDos.svelte +151 -93
  208. package/dist/spectral/Dos.svelte +389 -258
  209. package/dist/spectral/helpers.js +55 -43
  210. package/dist/state.svelte.d.ts +1 -1
  211. package/dist/state.svelte.js +3 -2
  212. package/dist/structure/Arrow.svelte +59 -20
  213. package/dist/structure/AtomLegend.svelte +215 -134
  214. package/dist/structure/Bond.svelte +73 -47
  215. package/dist/structure/CanvasTooltip.svelte +10 -2
  216. package/dist/structure/CellSelect.svelte +72 -45
  217. package/dist/structure/Cylinder.svelte +33 -17
  218. package/dist/structure/Lattice.svelte +88 -33
  219. package/dist/structure/Structure.svelte +1063 -797
  220. package/dist/structure/Structure.svelte.d.ts +1 -1
  221. package/dist/structure/StructureControls.svelte +349 -118
  222. package/dist/structure/StructureExportPane.svelte +124 -89
  223. package/dist/structure/StructureExportPane.svelte.d.ts +1 -1
  224. package/dist/structure/StructureInfoPane.svelte +304 -237
  225. package/dist/structure/StructureScene.svelte +879 -443
  226. package/dist/structure/StructureScene.svelte.d.ts +15 -7
  227. package/dist/structure/atom-properties.js +8 -8
  228. package/dist/structure/bonding.js +6 -7
  229. package/dist/structure/export.js +14 -29
  230. package/dist/structure/ferrox-wasm.js +1 -1
  231. package/dist/structure/index.d.ts +13 -3
  232. package/dist/structure/index.js +83 -23
  233. package/dist/structure/measure.d.ts +2 -2
  234. package/dist/structure/measure.js +4 -44
  235. package/dist/structure/parse.js +113 -141
  236. package/dist/structure/partial-occupancy.js +7 -10
  237. package/dist/structure/pbc.d.ts +1 -0
  238. package/dist/structure/pbc.js +16 -6
  239. package/dist/structure/supercell.d.ts +2 -2
  240. package/dist/structure/supercell.js +12 -22
  241. package/dist/structure/validation.js +1 -2
  242. package/dist/symmetry/SymmetryStats.svelte +84 -41
  243. package/dist/symmetry/WyckoffTable.svelte +26 -6
  244. package/dist/symmetry/cell-transform.js +5 -3
  245. package/dist/symmetry/index.js +8 -7
  246. package/dist/symmetry/spacegroups.js +148 -148
  247. package/dist/table/HeatmapTable.svelte +790 -554
  248. package/dist/table/HeatmapTable.svelte.d.ts +1 -1
  249. package/dist/table/ToggleMenu.svelte +125 -92
  250. package/dist/table/index.js +2 -4
  251. package/dist/theme/ThemeControl.svelte +21 -12
  252. package/dist/time.js +4 -1
  253. package/dist/tooltip/TooltipContent.svelte +33 -8
  254. package/dist/trajectory/Trajectory.svelte +758 -558
  255. package/dist/trajectory/TrajectoryError.svelte +14 -3
  256. package/dist/trajectory/TrajectoryExportPane.svelte +137 -83
  257. package/dist/trajectory/TrajectoryInfoPane.svelte +272 -143
  258. package/dist/trajectory/extract.js +10 -26
  259. package/dist/trajectory/format-detect.js +5 -5
  260. package/dist/trajectory/frame-reader.d.ts +1 -1
  261. package/dist/trajectory/frame-reader.js +5 -12
  262. package/dist/trajectory/helpers.d.ts +0 -1
  263. package/dist/trajectory/helpers.js +2 -17
  264. package/dist/trajectory/index.js +14 -12
  265. package/dist/trajectory/parse/ase.js +5 -4
  266. package/dist/trajectory/parse/hdf5.js +26 -18
  267. package/dist/trajectory/parse/index.js +13 -18
  268. package/dist/trajectory/parse/lammps.js +17 -7
  269. package/dist/trajectory/parse/vasp.js +5 -2
  270. package/dist/trajectory/parse/xyz.js +8 -7
  271. package/dist/trajectory/plotting.js +13 -8
  272. package/dist/utils.d.ts +1 -0
  273. package/dist/utils.js +13 -0
  274. package/dist/xrd/XrdPlot.svelte +337 -247
  275. package/dist/xrd/broadening.js +14 -9
  276. package/dist/xrd/calc-xrd.js +12 -18
  277. package/dist/xrd/parse.d.ts +1 -1
  278. package/dist/xrd/parse.js +17 -17
  279. package/package.json +99 -103
  280. package/readme.md +1 -1
  281. /package/dist/theme/{themes.js → themes.mjs} +0 -0
@@ -1,26 +1,70 @@
1
- <script lang="ts">import Icon from '../Icon.svelte';
2
- import { get_alphabetical_formula } from './format';
3
- import { ELEM_SYMBOLS } from '../labels';
4
- import { tooltip } from 'svelte-multiselect';
5
- import { extract_formula_elements, has_wildcards, normalize_element_symbols, parse_formula, parse_formula_with_wildcards, } from './parse';
6
- const DEFAULT_SEARCH_EXAMPLES = [
1
+ <script lang="ts">
2
+ import Icon from '../Icon.svelte'
3
+ import { get_alphabetical_formula } from './format'
4
+ import { ELEM_SYMBOLS } from '../labels'
5
+ import { tooltip } from 'svelte-multiselect'
6
+ import type { HTMLAttributes } from 'svelte/elements'
7
+ import type { FormulaSearchMode } from './index'
8
+ import {
9
+ extract_formula_elements,
10
+ has_wildcards,
11
+ normalize_element_symbols,
12
+ parse_formula,
13
+ parse_formula_with_wildcards,
14
+ } from './parse'
15
+
16
+ type SearchExampleCategory = {
17
+ label: string
18
+ description: string
19
+ examples: string[]
20
+ }
21
+
22
+ export type FormulaFilterToken = {
23
+ raw: string
24
+ element: string
25
+ operator: `include` | `exclude`
26
+ constraint: string | null
27
+ is_wildcard: boolean
28
+ is_valid: boolean
29
+ }
30
+
31
+ export type FormulaFilterParseResult = {
32
+ value: string
33
+ normalized_value: string
34
+ search_mode: FormulaSearchMode
35
+ tokens: FormulaFilterToken[]
36
+ has_wildcards: boolean
37
+ is_valid: boolean
38
+ error_message: string | null
39
+ }
40
+
41
+ export type FormulaFilterValidation = {
42
+ state: `valid` | `warning` | `invalid`
43
+ message: string | null
44
+ }
45
+
46
+ const DEFAULT_SEARCH_EXAMPLES: SearchExampleCategory[] = [
7
47
  {
8
- label: `Has elements`,
9
- description: `Materials containing these elements. Operators/ranges: +Li,-O,Fe:1-2. Use * for any element.`,
10
- examples: [`Li,Fe`, `+Li,-O`, `Li,*,*`],
48
+ label: `Has elements`,
49
+ description:
50
+ `Materials containing these elements. Operators/ranges: +Li,-O,Fe:1-2. Use * for any element.`,
51
+ examples: [`Li,Fe`, `+Li,-O`, `Li,*,*`],
11
52
  },
12
53
  {
13
- label: `Chemical system`,
14
- description: `Materials with only these elements (no others). Wildcards/ranges supported.`,
15
- examples: [`Li-Fe-O`, `Li-Fe-*-*`, `*-*-O`],
54
+ label: `Chemical system`,
55
+ description:
56
+ `Materials with only these elements (no others). Wildcards/ranges supported.`,
57
+ examples: [`Li-Fe-O`, `Li-Fe-*-*`, `*-*-O`],
16
58
  },
17
59
  {
18
- label: `Exact formula`,
19
- description: `Materials with this exact stoichiometry. Unicode paste, wildcards, and canonicalization supported.`,
20
- examples: [`LiFePO4`, `LiFe*2*`, `*2O3`],
60
+ label: `Exact formula`,
61
+ description:
62
+ `Materials with this exact stoichiometry. Unicode paste, wildcards, and canonicalization supported.`,
63
+ examples: [`LiFePO4`, `LiFe*2*`, `*2O3`],
21
64
  },
22
- ];
23
- const SUBSCRIPT_TO_ASCII = {
65
+ ]
66
+
67
+ const SUBSCRIPT_TO_ASCII: Record<string, string> = {
24
68
  [`\u2080`]: `0`,
25
69
  [`\u2081`]: `1`,
26
70
  [`\u2082`]: `2`,
@@ -31,8 +75,9 @@ const SUBSCRIPT_TO_ASCII = {
31
75
  [`\u2087`]: `7`,
32
76
  [`\u2088`]: `8`,
33
77
  [`\u2089`]: `9`,
34
- };
35
- const SUPERSCRIPT_TO_ASCII = {
78
+ }
79
+
80
+ const SUPERSCRIPT_TO_ASCII: Record<string, string> = {
36
81
  [`\u2070`]: `0`,
37
82
  [`\u00B9`]: `1`,
38
83
  [`\u00B2`]: `2`,
@@ -45,602 +90,685 @@ const SUPERSCRIPT_TO_ASCII = {
45
90
  [`\u2079`]: `9`,
46
91
  [`\u207A`]: `+`,
47
92
  [`\u207B`]: `-`,
48
- };
49
- let { value = $bindable(``), search_mode = $bindable(`elements`), input_element = $bindable(null), show_clear_button = true, show_examples = true, show_mode_lock = true, show_chip_editor = true, normalize_exact = true, examples = DEFAULT_SEARCH_EXAMPLES, disabled = false, mode_locked = $bindable(false), max_history = 5, // Max recent inputs to remember; 0 disables history dropdown
50
- history_key = `formula-filter-history`, // localStorage key for persisting history
51
- validate, onparse, on_validation, onchange, onclear, ...rest } = $props();
52
- let input_value = $state(value);
53
- let examples_open = $state(false);
54
- let history_open = $state(false);
55
- let wrapper = $state(null);
56
- let examples_wrapper = $state(null);
57
- let focused_item_idx = $state(-1);
58
- let focused_history_idx = $state(-1);
59
- let anchor_left = $state(false);
60
- let history_query = $state(``);
61
- let validation = $state({ state: `valid`, message: null });
62
- // Flatten examples for keyboard navigation
63
- let all_examples = $derived(examples.flatMap((cat) => cat.examples));
64
- // === History Management ===
65
- const has_storage = typeof localStorage !== `undefined`;
66
- const history_pins_key = $derived(`${history_key}-pins`);
67
- function load_history() {
68
- if (max_history <= 0 || !has_storage)
69
- return [];
93
+ }
94
+
95
+ let {
96
+ value = $bindable(``),
97
+ search_mode = $bindable(`elements`),
98
+ input_element = $bindable(null),
99
+ show_clear_button = true,
100
+ show_examples = true,
101
+ show_mode_lock = true,
102
+ show_chip_editor = true,
103
+ normalize_exact = true,
104
+ examples = DEFAULT_SEARCH_EXAMPLES,
105
+ disabled = false,
106
+ mode_locked = $bindable(false),
107
+ max_history = 5, // Max recent inputs to remember; 0 disables history dropdown
108
+ history_key = `formula-filter-history`, // localStorage key for persisting history
109
+ validate,
110
+ onparse,
111
+ on_validation,
112
+ onchange,
113
+ onclear,
114
+ ...rest
115
+ }: {
116
+ value: string // Current filter value (normalized on blur/enter)
117
+ search_mode?: FormulaSearchMode // Inferred search mode based on input format
118
+ input_element?: HTMLInputElement | null // Reference to the input element for programmatic focus
119
+ show_clear_button?: boolean // Show clear button when value is non-empty
120
+ show_examples?: boolean // Show the help button and examples dropdown
121
+ show_mode_lock?: boolean // Show mode lock toggle button
122
+ show_chip_editor?: boolean // Show token chip editor for tokenized modes
123
+ normalize_exact?: boolean // Canonicalize exact formulas on submit
124
+ examples?: SearchExampleCategory[] // Override built-in search example categories
125
+ disabled?: boolean // Disable all inputs
126
+ mode_locked?: boolean // Prevent auto mode inference and mode cycling
127
+ max_history?: number // Max recent inputs to remember; 0 disables history dropdown
128
+ history_key?: string // localStorage key for persisting history
129
+ validate?: (
130
+ value: string,
131
+ search_mode: FormulaSearchMode,
132
+ parsed: FormulaFilterParseResult,
133
+ ) => FormulaFilterValidation | null
134
+ onparse?: (parsed: FormulaFilterParseResult) => void
135
+ on_validation?: (validation: FormulaFilterValidation) => void
136
+ onchange?: (value: string, search_mode: FormulaSearchMode) => void // Callback when value changes
137
+ onclear?: () => void // Callback when clear button is clicked
138
+ } & HTMLAttributes<HTMLDivElement> = $props()
139
+
140
+ let input_value = $state(value)
141
+ let examples_open = $state(false)
142
+ let history_open = $state(false)
143
+ let wrapper: HTMLDivElement | null = $state(null)
144
+ let examples_wrapper: HTMLDivElement | null = $state(null)
145
+ let focused_item_idx = $state(-1)
146
+ let focused_history_idx = $state(-1)
147
+ let anchor_left = $state(false)
148
+ let history_query = $state(``)
149
+ let validation = $state<FormulaFilterValidation>({ state: `valid`, message: null })
150
+
151
+ // Flatten examples for keyboard navigation
152
+ let all_examples = $derived(examples.flatMap((cat) => cat.examples))
153
+
154
+ // === History Management ===
155
+ const has_storage = typeof localStorage !== `undefined`
156
+ const history_pins_key = $derived(`${history_key}-pins`)
157
+
158
+ function load_history(): string[] {
159
+ if (max_history <= 0 || !has_storage) return []
70
160
  try {
71
- const raw = localStorage.getItem(history_key);
72
- if (!raw)
73
- return [];
74
- const parsed = JSON.parse(raw);
75
- if (!Array.isArray(parsed))
76
- return [];
77
- return parsed.filter((item) => typeof item === `string`).slice(0, max_history);
78
- }
79
- catch {
80
- return [];
161
+ const raw = localStorage.getItem(history_key)
162
+ if (!raw) return []
163
+ const parsed: unknown = JSON.parse(raw)
164
+ if (!Array.isArray(parsed)) return []
165
+ return parsed.filter((item): item is string => typeof item === `string`).slice(
166
+ 0,
167
+ max_history,
168
+ )
169
+ } catch {
170
+ return []
81
171
  }
82
- }
83
- function save_history(entries) {
84
- if (max_history <= 0 || !has_storage)
85
- return;
172
+ }
173
+
174
+ function save_history(entries: string[]): void {
175
+ if (max_history <= 0 || !has_storage) return
86
176
  try {
87
- localStorage.setItem(history_key, JSON.stringify(entries.slice(0, max_history)));
88
- }
89
- catch {
90
- // localStorage may be unavailable (e.g. private browsing)
177
+ localStorage.setItem(history_key, JSON.stringify(entries.slice(0, max_history)))
178
+ } catch {
179
+ // localStorage may be unavailable (e.g. private browsing)
91
180
  }
92
- }
93
- function load_pinned() {
94
- if (max_history <= 0 || !has_storage)
95
- return [];
181
+ }
182
+
183
+ function load_pinned(): string[] {
184
+ if (max_history <= 0 || !has_storage) return []
96
185
  try {
97
- const raw = localStorage.getItem(history_pins_key);
98
- if (!raw)
99
- return [];
100
- const parsed = JSON.parse(raw);
101
- if (!Array.isArray(parsed))
102
- return [];
103
- return parsed.filter((item) => typeof item === `string`);
186
+ const raw = localStorage.getItem(history_pins_key)
187
+ if (!raw) return []
188
+ const parsed: unknown = JSON.parse(raw)
189
+ if (!Array.isArray(parsed)) return []
190
+ return parsed.filter((item): item is string => typeof item === `string`)
191
+ } catch {
192
+ return []
104
193
  }
105
- catch {
106
- return [];
107
- }
108
- }
109
- function save_pinned(entries) {
110
- if (max_history <= 0 || !has_storage)
111
- return;
194
+ }
195
+
196
+ function save_pinned(entries: string[]): void {
197
+ if (max_history <= 0 || !has_storage) return
112
198
  try {
113
- localStorage.setItem(history_pins_key, JSON.stringify(entries));
114
- }
115
- catch {
116
- // localStorage may be unavailable
199
+ localStorage.setItem(history_pins_key, JSON.stringify(entries))
200
+ } catch {
201
+ // localStorage may be unavailable
117
202
  }
118
- }
119
- let history = $state(load_history());
120
- let pinned_history = $state(load_pinned());
121
- function add_to_history(entry) {
122
- if (max_history <= 0 || !entry.trim())
123
- return;
203
+ }
204
+
205
+ let history = $state<string[]>(load_history())
206
+ let pinned_history = $state<string[]>(load_pinned())
207
+
208
+ function add_to_history(entry: string): void {
209
+ if (max_history <= 0 || !entry.trim()) return
124
210
  // Remove duplicate if present, then prepend
125
- const filtered = history.filter((item) => item !== entry);
126
- history = [entry, ...filtered].slice(0, max_history);
211
+ const filtered = history.filter((item) => item !== entry)
212
+ history = [entry, ...filtered].slice(0, max_history)
127
213
  // Keep pin state for retained entries only
128
- pinned_history = pinned_history.filter((item) => history.includes(item));
129
- save_history(history);
130
- save_pinned(pinned_history);
131
- }
132
- function remove_from_history(entry) {
133
- history = history.filter((item) => item !== entry);
134
- pinned_history = pinned_history.filter((item) => item !== entry);
135
- save_history(history);
136
- save_pinned(pinned_history);
214
+ pinned_history = pinned_history.filter((item) => history.includes(item))
215
+ save_history(history)
216
+ save_pinned(pinned_history)
217
+ }
218
+
219
+ function remove_from_history(entry: string): void {
220
+ history = history.filter((item) => item !== entry)
221
+ pinned_history = pinned_history.filter((item) => item !== entry)
222
+ save_history(history)
223
+ save_pinned(pinned_history)
137
224
  // Clamp focused index to prevent out-of-bounds access on Enter
138
- if (history.length === 0)
139
- history_open = false;
225
+ if (history.length === 0) history_open = false
140
226
  else if (focused_history_idx >= visible_history.length) {
141
- focused_history_idx = visible_history.length - 1;
227
+ focused_history_idx = visible_history.length - 1
142
228
  }
143
- }
144
- function toggle_pin_history(entry) {
229
+ }
230
+
231
+ function toggle_pin_history(entry: string): void {
145
232
  pinned_history = pinned_history.includes(entry)
146
- ? pinned_history.filter((item) => item !== entry)
147
- : [entry, ...pinned_history.filter((item) => item !== entry)];
148
- save_pinned(pinned_history);
149
- }
150
- function clear_history() {
151
- history = [];
152
- pinned_history = [];
153
- save_history(history);
154
- save_pinned(pinned_history);
155
- close_history();
156
- }
157
- function is_pinned(entry) {
158
- return pinned_history.includes(entry);
159
- }
160
- // Filtered history: exclude current value to avoid redundant suggestion
161
- let visible_history = $derived.by(() => {
233
+ ? pinned_history.filter((item) => item !== entry)
234
+ : [entry, ...pinned_history.filter((item) => item !== entry)]
235
+ save_pinned(pinned_history)
236
+ }
237
+
238
+ function clear_history(): void {
239
+ history = []
240
+ pinned_history = []
241
+ save_history(history)
242
+ save_pinned(pinned_history)
243
+ close_history()
244
+ }
245
+
246
+ function is_pinned(entry: string): boolean {
247
+ return pinned_history.includes(entry)
248
+ }
249
+
250
+ // Filtered history: exclude current value to avoid redundant suggestion
251
+ let visible_history = $derived.by(() => {
162
252
  const filtered = history
163
- .filter((item) => item !== value)
164
- .filter((item) => item.toLowerCase().includes(history_query.toLowerCase().trim()));
165
- const pinned = filtered.filter((item) => pinned_history.includes(item));
166
- const unpinned = filtered.filter((item) => !pinned_history.includes(item));
167
- return [...pinned, ...unpinned];
168
- });
169
- function close_history() {
170
- history_open = false;
171
- history_query = ``;
172
- focused_history_idx = -1;
173
- }
174
- function open_history() {
175
- if (max_history <= 0 || visible_history.length === 0 || examples_open)
176
- return;
177
- history_open = true;
178
- history_query = ``;
179
- focused_history_idx = -1;
180
- }
181
- function handle_document_click(event) {
182
- if (!wrapper || (!examples_open && !history_open))
183
- return;
184
- const target = event.target;
185
- if (!(target instanceof Node))
186
- return;
253
+ .filter((item) => item !== value)
254
+ .filter((item) =>
255
+ item.toLowerCase().includes(history_query.toLowerCase().trim())
256
+ )
257
+ const pinned = filtered.filter((item) => pinned_history.includes(item))
258
+ const unpinned = filtered.filter((item) => !pinned_history.includes(item))
259
+ return [...pinned, ...unpinned]
260
+ })
261
+
262
+ function close_history(): void {
263
+ history_open = false
264
+ history_query = ``
265
+ focused_history_idx = -1
266
+ }
267
+
268
+ function open_history(): void {
269
+ if (max_history <= 0 || visible_history.length === 0 || examples_open) return
270
+ history_open = true
271
+ history_query = ``
272
+ focused_history_idx = -1
273
+ }
274
+
275
+ function handle_document_click(event: MouseEvent): void {
276
+ if (!wrapper || (!examples_open && !history_open)) return
277
+ const target = event.target
278
+ if (!(target instanceof Node)) return
187
279
  if (!wrapper.contains(target)) {
188
- if (examples_open)
189
- close_examples();
190
- if (history_open)
191
- close_history();
280
+ if (examples_open) close_examples()
281
+ if (history_open) close_history()
192
282
  }
193
- }
194
- function close_examples(restore_focus = true) {
195
- examples_open = false;
196
- focused_item_idx = -1;
197
- if (restore_focus)
198
- input_element?.focus({ preventScroll: true });
199
- }
200
- // Track last synced value to detect external changes (e.g. from URL params)
201
- // and re-infer mode accordingly. Without this, mode would only be set on first render.
202
- let last_synced = $state(null);
203
- $effect(() => {
283
+ }
284
+
285
+ function close_examples(restore_focus = true): void {
286
+ examples_open = false
287
+ focused_item_idx = -1
288
+ if (restore_focus) input_element?.focus({ preventScroll: true })
289
+ }
290
+
291
+ // Track last synced value to detect external changes (e.g. from URL params)
292
+ // and re-infer mode accordingly. Without this, mode would only be set on first render.
293
+ let last_synced = $state<string | null>(null)
294
+ $effect(() => {
204
295
  if (value !== last_synced) {
205
- last_synced = value;
206
- input_value = value;
207
- if (value && !mode_locked) {
208
- const inferred = infer_mode(value);
209
- if (inferred !== search_mode)
210
- search_mode = inferred;
211
- }
212
- run_validation(value, search_mode);
296
+ last_synced = value
297
+ input_value = value
298
+ if (value && !mode_locked) {
299
+ const inferred = infer_mode(value)
300
+ if (inferred !== search_mode) search_mode = inferred
301
+ }
302
+ run_validation(value, search_mode)
213
303
  }
214
- });
215
- // Detect if dropdown would exit viewport on the right and adjust anchor
216
- $effect(() => {
217
- if (!examples_open || !examples_wrapper)
218
- return;
304
+ })
305
+
306
+ // Detect if dropdown would exit viewport on the right and adjust anchor
307
+ $effect(() => {
308
+ if (!examples_open || !examples_wrapper) return
219
309
  requestAnimationFrame(() => {
220
- const dropdown = examples_wrapper?.querySelector(`.examples-dropdown`);
221
- if (!dropdown)
222
- return;
223
- const rect = dropdown.getBoundingClientRect();
224
- if (rect.right > window.innerWidth && !anchor_left)
225
- anchor_left = true;
226
- });
227
- });
228
- // Infer search mode from input format
229
- function infer_mode(input) {
230
- const trimmed = input.trim();
231
- if (!trimmed)
232
- return `elements`;
233
- if (/^[+\-!]\s*\w/.test(trimmed))
234
- return `elements`;
235
- if (trimmed.includes(`+`) || trimmed.includes(`!`))
236
- return `elements`;
237
- if (trimmed.includes(`:`))
238
- return trimmed.includes(`-`) ? `chemsys` : `elements`;
239
- if (trimmed.includes(`,`))
240
- return `elements`; // Li,Fe,O → has elements
241
- if (trimmed.includes(`-`))
242
- return `chemsys`; // Li-Fe-O chemical system
243
- return `exact`; // LiFePO4 → exact formula
244
- }
245
- // Cycle through modes: elements → chemsys → exact → elements
246
- const MODE_CYCLE = [`elements`, `chemsys`, `exact`];
247
- function normalize_unicode_formula(input) {
248
- let normalized = input;
310
+ const dropdown = examples_wrapper?.querySelector(`.examples-dropdown`) as
311
+ | HTMLElement
312
+ | null
313
+ if (!dropdown) return
314
+ const rect = dropdown.getBoundingClientRect()
315
+ if (rect.right > window.innerWidth && !anchor_left) anchor_left = true
316
+ })
317
+ })
318
+
319
+ // Infer search mode from input format
320
+ function infer_mode(input: string): FormulaSearchMode {
321
+ const trimmed = input.trim()
322
+ if (!trimmed) return `elements`
323
+ if (/^[+\-!]\s*\w/.test(trimmed)) return `elements`
324
+ if (trimmed.includes(`+`) || trimmed.includes(`!`)) return `elements`
325
+ if (trimmed.includes(`:`)) return trimmed.includes(`-`) ? `chemsys` : `elements`
326
+ if (trimmed.includes(`,`)) return `elements` // Li,Fe,O → has elements
327
+ if (trimmed.includes(`-`)) return `chemsys` // Li-Fe-O → chemical system
328
+ return `exact` // LiFePO4 → exact formula
329
+ }
330
+
331
+ // Cycle through modes: elements → chemsys → exact → elements
332
+ const MODE_CYCLE: FormulaSearchMode[] = [`elements`, `chemsys`, `exact`]
333
+
334
+ function normalize_unicode_formula(input: string): string {
335
+ let normalized = input
249
336
  for (const [subscript, ascii] of Object.entries(SUBSCRIPT_TO_ASCII)) {
250
- normalized = normalized.replaceAll(subscript, ascii);
337
+ normalized = normalized.replaceAll(subscript, ascii)
251
338
  }
252
339
  for (const [superscript, ascii] of Object.entries(SUPERSCRIPT_TO_ASCII)) {
253
- normalized = normalized.replaceAll(superscript, ascii);
340
+ normalized = normalized.replaceAll(superscript, ascii)
254
341
  }
255
342
  return normalized
256
- .replaceAll(`·`, ``)
257
- .replaceAll(`⋅`, ``)
258
- .replaceAll(`−`, `-`)
259
- .replace(/\s+/g, ``);
260
- }
261
- function normalize_exact_formula(input) {
262
- const sanitized_input = normalize_unicode_formula(input.trim());
263
- if (!sanitize_exact_formula(sanitized_input).is_valid)
264
- return sanitized_input;
343
+ .replaceAll(`·`, ``)
344
+ .replaceAll(`⋅`, ``)
345
+ .replaceAll(`−`, `-`)
346
+ .replace(/\s+/g, ``)
347
+ }
348
+
349
+ function normalize_exact_formula(input: string): string {
350
+ const sanitized_input = normalize_unicode_formula(input.trim())
351
+ if (!sanitize_exact_formula(sanitized_input).is_valid) return sanitized_input
352
+
265
353
  if (!has_wildcards(sanitized_input)) {
266
- const canonical = get_alphabetical_formula(sanitized_input, true, ``);
267
- return canonical || sanitized_input;
354
+ const canonical = get_alphabetical_formula(sanitized_input, true, ``)
355
+ return canonical || sanitized_input
268
356
  }
357
+
269
358
  try {
270
- const tokens = parse_formula_with_wildcards(sanitized_input);
271
- const explicit = tokens
272
- .filter((token) => token.element !== null)
273
- .map((token) => ({ element: token.element, count: token.count }));
274
- const wildcard_tokens = tokens.filter((token) => token.element === null);
275
- // Merge explicit element counts before sorting.
276
- const merged_explicit = [];
277
- for (const token of explicit) {
278
- const existing = merged_explicit.find((item) => item.element === token.element);
279
- if (existing)
280
- existing.count += token.count;
281
- else
282
- merged_explicit.push(token);
283
- }
284
- const sorted_explicit = merged_explicit.sort((elem_a, elem_b) => elem_a.element.localeCompare(elem_b.element));
285
- const wildcard_str = wildcard_tokens.map((token) => token.count > 1 ? `*${token.count}` : `*`).join(``);
286
- const explicit_str = sorted_explicit.map((token) => token.count > 1 ? `${token.element}${token.count}` : token.element).join(``);
287
- return `${explicit_str}${wildcard_str}`;
288
- }
289
- catch {
290
- return sanitized_input;
359
+ const tokens = parse_formula_with_wildcards(sanitized_input)
360
+ const explicit = tokens
361
+ .filter((token) => token.element !== null)
362
+ .map((token) => ({ element: token.element as string, count: token.count }))
363
+ const wildcard_tokens = tokens.filter((token) => token.element === null)
364
+
365
+ // Merge explicit element counts before sorting.
366
+ const merged_explicit: Array<{ element: string; count: number }> = []
367
+ for (const token of explicit) {
368
+ const existing = merged_explicit.find((item) =>
369
+ item.element === token.element
370
+ )
371
+ if (existing) existing.count += token.count
372
+ else merged_explicit.push(token)
373
+ }
374
+ const sorted_explicit = merged_explicit.sort((elem_a, elem_b) =>
375
+ elem_a.element.localeCompare(elem_b.element)
376
+ )
377
+ const wildcard_str = wildcard_tokens.map((token) =>
378
+ token.count > 1 ? `*${token.count}` : `*`
379
+ ).join(``)
380
+ const explicit_str = sorted_explicit.map((token) =>
381
+ token.count > 1 ? `${token.element}${token.count}` : token.element
382
+ ).join(``)
383
+ return `${explicit_str}${wildcard_str}`
384
+ } catch {
385
+ return sanitized_input
291
386
  }
292
- }
293
- function is_valid_constraint(constraint) {
294
- if (!constraint)
295
- return true;
387
+ }
388
+
389
+ function is_valid_constraint(constraint: string): boolean {
390
+ if (!constraint) return true
296
391
  return /^\d+$/.test(constraint) || /^\d+-\d+$/.test(constraint) ||
297
- /^(>=|<=|>|<)\d+$/.test(constraint);
298
- }
299
- function strip_operator_prefix(token) {
392
+ /^(>=|<=|>|<)\d+$/.test(constraint)
393
+ }
394
+
395
+ function strip_operator_prefix(
396
+ token: string,
397
+ ): { operator: FormulaFilterToken[`operator`]; value: string } {
300
398
  const operator = token.startsWith(`-`) || token.startsWith(`!`)
301
- ? `exclude`
302
- : `include`;
303
- const value = token.startsWith(`+`) || token.startsWith(`-`) || token.startsWith(`!`)
399
+ ? `exclude`
400
+ : `include`
401
+ const value =
402
+ token.startsWith(`+`) || token.startsWith(`-`) || token.startsWith(`!`)
304
403
  ? token.slice(1)
305
- : token;
306
- return { operator, value };
307
- }
308
- function serialize_token(token) {
309
- const prefix = token.operator === `exclude` ? `-` : ``;
310
- const suffix = token.constraint ? `:${token.constraint}` : ``;
311
- return `${prefix}${token.element}${suffix}`;
312
- }
313
- function token_chip_label(token) {
314
- const prefix = token.operator === `exclude` ? `-` : `+`;
315
- const suffix = token.constraint ? `:${token.constraint}` : ``;
316
- return `${prefix}${token.element}${suffix}`;
317
- }
318
- function parse_token(raw_token) {
319
- const token = raw_token.trim();
320
- const { operator, value: without_operator } = strip_operator_prefix(token);
321
- const [element_part, constraint] = without_operator.split(`:`);
322
- const element = element_part.trim();
323
- const is_wildcard = element === `*`;
404
+ : token
405
+ return { operator, value }
406
+ }
407
+
408
+ function serialize_token(
409
+ token: Pick<FormulaFilterToken, `operator` | `element` | `constraint`>,
410
+ ): string {
411
+ const prefix = token.operator === `exclude` ? `-` : ``
412
+ const suffix = token.constraint ? `:${token.constraint}` : ``
413
+ return `${prefix}${token.element}${suffix}`
414
+ }
415
+
416
+ function token_chip_label(
417
+ token: Pick<FormulaFilterToken, `operator` | `element` | `constraint`>,
418
+ ): string {
419
+ const prefix = token.operator === `exclude` ? `-` : `+`
420
+ const suffix = token.constraint ? `:${token.constraint}` : ``
421
+ return `${prefix}${token.element}${suffix}`
422
+ }
423
+
424
+ function parse_token(raw_token: string): FormulaFilterToken {
425
+ const token = raw_token.trim()
426
+ const { operator, value: without_operator } = strip_operator_prefix(token)
427
+ const [element_part, constraint] = without_operator.split(`:`)
428
+ const element = element_part.trim()
429
+ const is_wildcard = element === `*`
324
430
  const is_valid_element = is_wildcard ||
325
- ELEM_SYMBOLS.includes(element);
326
- const normalized_constraint = constraint?.trim() || null;
431
+ ELEM_SYMBOLS.includes(element as (typeof ELEM_SYMBOLS)[number])
432
+ const normalized_constraint = constraint?.trim() || null
327
433
  const is_valid = is_valid_element && (normalized_constraint === null ||
328
- is_valid_constraint(normalized_constraint));
434
+ is_valid_constraint(normalized_constraint))
435
+
329
436
  return {
330
- raw: raw_token,
331
- element,
332
- operator,
333
- constraint: normalized_constraint,
334
- is_wildcard,
335
- is_valid,
336
- };
337
- }
338
- function tokenize_query(input, mode) {
339
- const trimmed = input.trim();
340
- if (!trimmed)
341
- return [];
437
+ raw: raw_token,
438
+ element,
439
+ operator,
440
+ constraint: normalized_constraint,
441
+ is_wildcard,
442
+ is_valid,
443
+ }
444
+ }
445
+
446
+ function tokenize_query(
447
+ input: string,
448
+ mode: FormulaSearchMode,
449
+ ): FormulaFilterToken[] {
450
+ const trimmed = input.trim()
451
+ if (!trimmed) return []
342
452
  if (mode === `exact`) {
343
- return [{
344
- raw: trimmed,
345
- element: trimmed,
346
- operator: `include`,
347
- constraint: null,
348
- is_wildcard: has_wildcards(trimmed),
349
- is_valid: sanitize_exact_formula(trimmed).is_valid,
350
- }];
453
+ return [{
454
+ raw: trimmed,
455
+ element: trimmed,
456
+ operator: `include`,
457
+ constraint: null,
458
+ is_wildcard: has_wildcards(trimmed),
459
+ is_valid: sanitize_exact_formula(trimmed).is_valid,
460
+ }]
351
461
  }
352
- const normalized = mode === `chemsys` ? trimmed.replaceAll(`,`, `-`) : trimmed;
462
+ const normalized = mode === `chemsys` ? trimmed.replaceAll(`,`, `-`) : trimmed
353
463
  const tokens = mode === `chemsys`
354
- // Keep range constraints like Fe:1-2 intact while splitting token separators.
355
- ? normalized.split(/-(?!\d)/)
356
- : normalized.split(`,`);
464
+ // Keep range constraints like Fe:1-2 intact while splitting token separators.
465
+ ? normalized.split(/-(?!\d)/)
466
+ : normalized.split(`,`)
357
467
  return tokens
358
- .map((token) => token.trim())
359
- .filter(Boolean)
360
- .map(parse_token);
361
- }
362
- function sanitize_exact_formula(input) {
363
- const trimmed = input.trim();
364
- if (!trimmed)
365
- return { is_valid: true, error_message: null };
468
+ .map((token) => token.trim())
469
+ .filter(Boolean)
470
+ .map(parse_token)
471
+ }
472
+
473
+ function sanitize_exact_formula(
474
+ input: string,
475
+ ): { is_valid: boolean; error_message: string | null } {
476
+ const trimmed = input.trim()
477
+ if (!trimmed) return { is_valid: true, error_message: null }
366
478
  try {
367
- if (has_wildcards(trimmed)) {
368
- parse_formula_with_wildcards(trimmed);
369
- }
370
- else {
371
- parse_formula(trimmed);
372
- }
373
- return { is_valid: true, error_message: null };
374
- }
375
- catch (error) {
376
- const message = error instanceof Error ? error.message : `Invalid exact formula`;
377
- return { is_valid: false, error_message: message };
479
+ if (has_wildcards(trimmed)) {
480
+ parse_formula_with_wildcards(trimmed)
481
+ } else {
482
+ parse_formula(trimmed)
483
+ }
484
+ return { is_valid: true, error_message: null }
485
+ } catch (error) {
486
+ const message = error instanceof Error ? error.message : `Invalid exact formula`
487
+ return { is_valid: false, error_message: message }
378
488
  }
379
- }
380
- function normalize_tokenized_input(input, mode) {
381
- const separator = mode === `chemsys` ? `-` : `,`;
382
- const parsed_tokens = tokenize_query(input, mode);
383
- if (parsed_tokens.length === 0)
384
- return ``;
489
+ }
490
+
491
+ function normalize_tokenized_input(input: string, mode: FormulaSearchMode): string {
492
+ const separator = mode === `chemsys` ? `-` : `,`
493
+ const parsed_tokens = tokenize_query(input, mode)
494
+ if (parsed_tokens.length === 0) return ``
495
+
385
496
  const normalized_tokens = parsed_tokens
386
- .filter((token) => token.is_valid)
387
- .map((token) => ({
497
+ .filter((token) => token.is_valid)
498
+ .map((token) => ({
388
499
  ...token,
389
500
  element: token.is_wildcard
390
- ? `*`
391
- : normalize_element_symbols(token.element).at(0) || token.element,
392
- }))
393
- .sort((token_a, token_b) => {
501
+ ? `*`
502
+ : normalize_element_symbols(token.element).at(0) || token.element,
503
+ }))
504
+ .sort((token_a, token_b) => {
394
505
  if (token_a.operator !== token_b.operator) {
395
- return token_a.operator === `include` ? -1 : 1;
506
+ return token_a.operator === `include` ? -1 : 1
396
507
  }
397
508
  if (token_a.is_wildcard !== token_b.is_wildcard) {
398
- return token_a.is_wildcard ? 1 : -1;
509
+ return token_a.is_wildcard ? 1 : -1
399
510
  }
400
- return token_a.element.localeCompare(token_b.element);
401
- });
511
+ return token_a.element.localeCompare(token_b.element)
512
+ })
513
+
402
514
  return normalized_tokens
403
- .map(serialize_token)
404
- .join(separator);
405
- }
406
- function parse_query(normalized_value, mode) {
407
- const tokens = tokenize_query(normalized_value, mode);
408
- const first_invalid_token = tokens.find((token) => !token.is_valid);
515
+ .map(serialize_token)
516
+ .join(separator)
517
+ }
518
+
519
+ function parse_query(
520
+ normalized_value: string,
521
+ mode: FormulaSearchMode,
522
+ ): FormulaFilterParseResult {
523
+ const tokens = tokenize_query(normalized_value, mode)
524
+ const first_invalid_token = tokens.find((token) => !token.is_valid)
409
525
  const exact_validation = mode === `exact`
410
- ? sanitize_exact_formula(normalized_value)
411
- : {
412
- is_valid: !first_invalid_token,
413
- error_message: first_invalid_token
414
- ? `Invalid token: ${first_invalid_token.raw}`
415
- : null,
416
- };
526
+ ? sanitize_exact_formula(normalized_value)
527
+ : {
528
+ is_valid: !first_invalid_token,
529
+ error_message: first_invalid_token
530
+ ? `Invalid token: ${first_invalid_token.raw}`
531
+ : null,
532
+ }
417
533
  return {
418
- value: normalized_value,
419
- normalized_value,
420
- search_mode: mode,
421
- tokens,
422
- has_wildcards: tokens.some((token) => token.is_wildcard),
423
- is_valid: exact_validation.is_valid,
424
- error_message: exact_validation.error_message,
425
- };
426
- }
427
- function run_validation(next_value, next_mode) {
428
- const parsed = parse_query(next_value, next_mode);
429
- onparse?.(parsed);
430
- const default_validation = parsed.is_valid
431
- ? { state: `valid`, message: null }
432
- : { state: `invalid`, message: parsed.error_message ?? `Invalid filter query` };
433
- const custom_validation = validate?.(next_value, next_mode, parsed);
434
- validation = custom_validation ?? default_validation;
435
- on_validation?.(validation);
436
- }
437
- // Extract elements from any input format (formula, comma-separated, dash-separated)
438
- // Always returns elements in alphabetical order for consistency, preserving wildcards (*)
439
- function extract_elements(input) {
440
- const trimmed = input.trim();
441
- if (!trimmed)
442
- return [];
534
+ value: normalized_value,
535
+ normalized_value,
536
+ search_mode: mode,
537
+ tokens,
538
+ has_wildcards: tokens.some((token) => token.is_wildcard),
539
+ is_valid: exact_validation.is_valid,
540
+ error_message: exact_validation.error_message,
541
+ }
542
+ }
543
+
544
+ function run_validation(next_value: string, next_mode: FormulaSearchMode): void {
545
+ const parsed = parse_query(next_value, next_mode)
546
+ onparse?.(parsed)
547
+
548
+ const default_validation: FormulaFilterValidation = parsed.is_valid
549
+ ? { state: `valid`, message: null }
550
+ : { state: `invalid`, message: parsed.error_message ?? `Invalid filter query` }
551
+ const custom_validation = validate?.(next_value, next_mode, parsed)
552
+ validation = custom_validation ?? default_validation
553
+ on_validation?.(validation)
554
+ }
555
+
556
+ // Extract elements from any input format (formula, comma-separated, dash-separated)
557
+ // Always returns elements in alphabetical order for consistency, preserving wildcards (*)
558
+ function extract_elements(input: string): string[] {
559
+ const trimmed = input.trim()
560
+ if (!trimmed) return []
443
561
  // If contains commas or dashes, split by those and sort alphabetically
444
562
  if (trimmed.includes(`,`) || trimmed.includes(`-`)) {
445
- const parts = trimmed.split(/[-,]/).map((str) => str.trim()).filter(Boolean);
446
- // Separate wildcards from regular elements
447
- const wildcards = parts.filter((part) => part === `*`);
448
- const regular_parts = parts.filter((part) => part !== `*`);
449
- // Filter valid elements and sort alphabetically, then append wildcards
450
- const valid_elements = normalize_element_symbols(regular_parts.join(`,`)).sort();
451
- return [...valid_elements, ...wildcards];
563
+ const parts = trimmed.split(/[-,]/).map((str) => str.trim()).filter(Boolean)
564
+ // Separate wildcards from regular elements
565
+ const wildcards = parts.filter((part) => part === `*`)
566
+ const regular_parts = parts.filter((part) => part !== `*`)
567
+ // Filter valid elements and sort alphabetically, then append wildcards
568
+ const valid_elements = normalize_element_symbols(regular_parts.join(`,`)).sort()
569
+ return [...valid_elements, ...wildcards]
452
570
  }
453
571
  // Otherwise parse as formula (already returns sorted by default)
454
572
  // For formulas with wildcards, we can't parse them normally
455
573
  if (has_wildcards(trimmed)) { // Use shared utility and extract unique elements
456
- const tokens = parse_formula_with_wildcards(trimmed);
457
- const unique_elements = [];
458
- for (const token of tokens) {
459
- if (token.element !== null && !unique_elements.includes(token.element)) {
460
- unique_elements.push(token.element);
461
- }
574
+ const tokens = parse_formula_with_wildcards(trimmed)
575
+ const unique_elements: string[] = []
576
+ for (const token of tokens) {
577
+ if (token.element !== null && !unique_elements.includes(token.element)) {
578
+ unique_elements.push(token.element)
462
579
  }
463
- const elements = unique_elements.sort();
464
- const wildcards = tokens.filter((token) => token.element === null).map(() => `*`);
465
- return [...elements, ...wildcards];
580
+ }
581
+ const elements = unique_elements.sort()
582
+ const wildcards = tokens.filter((token) => token.element === null).map(() =>
583
+ `*`
584
+ )
585
+ return [...elements, ...wildcards]
466
586
  }
467
587
  try {
468
- return extract_formula_elements(trimmed, { sorted: true });
588
+ return extract_formula_elements(trimmed, { sorted: true })
589
+ } catch {
590
+ return []
469
591
  }
470
- catch {
471
- return [];
472
- }
473
- }
474
- // Format elements for the given mode
475
- function format_for_mode(elements, mode) {
476
- if (elements.length === 0)
477
- return ``;
478
- if (mode === `elements`)
479
- return elements.join(`,`);
480
- if (mode === `chemsys`)
481
- return elements.join(`-`);
592
+ }
593
+
594
+ // Format elements for the given mode
595
+ function format_for_mode(elements: string[], mode: FormulaSearchMode): string {
596
+ if (elements.length === 0) return ``
597
+ if (mode === `elements`) return elements.join(`,`)
598
+ if (mode === `chemsys`) return elements.join(`-`)
482
599
  // For exact mode, just join without separator (user will need to add counts)
483
- return elements.join(``);
484
- }
485
- function cycle_mode() {
486
- if (mode_locked)
487
- return;
488
- const current_idx = MODE_CYCLE.indexOf(search_mode);
489
- const next_idx = (current_idx + 1) % MODE_CYCLE.length;
490
- const next_mode = MODE_CYCLE[next_idx];
600
+ return elements.join(``)
601
+ }
602
+
603
+ function cycle_mode(): void {
604
+ if (mode_locked) return
605
+ const current_idx = MODE_CYCLE.indexOf(search_mode)
606
+ const next_idx = (current_idx + 1) % MODE_CYCLE.length
607
+ const next_mode = MODE_CYCLE[next_idx]
608
+
491
609
  // Extract elements from current value and reformat for new mode
492
- const elements = extract_elements(value);
493
- const reformatted = format_for_mode(elements, next_mode);
494
- search_mode = next_mode;
495
- last_synced = value = input_value = reformatted; // update last_synced to prevent effect re-inference
496
- run_validation(reformatted, next_mode);
497
- onchange?.(reformatted, next_mode);
498
- }
499
- function set_value(new_value, forced_mode) {
500
- const mode = forced_mode ?? (mode_locked ? search_mode : infer_mode(new_value));
501
- last_synced = value = input_value = new_value; // update last_synced to prevent effect re-inference
502
- search_mode = mode;
503
- if (new_value.trim())
504
- add_to_history(new_value);
505
- close_history();
506
- run_validation(value, mode);
507
- onchange?.(value, mode);
508
- }
509
- function sync_value() {
510
- const trimmed = normalize_unicode_formula(input_value).trim();
511
- if (!trimmed)
512
- return set_value(``);
513
- const mode = mode_locked ? search_mode : infer_mode(trimmed);
610
+ const elements = extract_elements(value)
611
+ const reformatted = format_for_mode(elements, next_mode)
612
+
613
+ search_mode = next_mode
614
+ last_synced = value = input_value = reformatted // update last_synced to prevent effect re-inference
615
+ run_validation(reformatted, next_mode)
616
+ onchange?.(reformatted, next_mode)
617
+ }
618
+
619
+ function set_value(new_value: string, forced_mode?: FormulaSearchMode): void {
620
+ const mode = forced_mode ?? (mode_locked ? search_mode : infer_mode(new_value))
621
+ last_synced = value = input_value = new_value // update last_synced to prevent effect re-inference
622
+ search_mode = mode
623
+ if (new_value.trim()) add_to_history(new_value)
624
+ close_history()
625
+ run_validation(value, mode)
626
+ onchange?.(value, mode)
627
+ }
628
+
629
+ function sync_value(): void {
630
+ const trimmed = normalize_unicode_formula(input_value).trim()
631
+ if (!trimmed) return set_value(``)
632
+
633
+ const mode = mode_locked ? search_mode : infer_mode(trimmed)
514
634
  if (mode === `exact`) {
515
- const exact_value = normalize_exact ? normalize_exact_formula(trimmed) : trimmed;
516
- return set_value(exact_value, mode);
635
+ const exact_value = normalize_exact ? normalize_exact_formula(trimmed) : trimmed
636
+ return set_value(exact_value, mode)
517
637
  }
518
- const parsed = parse_query(trimmed, mode);
638
+
639
+ const parsed = parse_query(trimmed, mode)
519
640
  if (!parsed.is_valid) {
520
- // Preserve user input on invalid tokens instead of silently dropping them.
521
- input_value = trimmed;
522
- run_validation(trimmed, mode);
523
- return;
641
+ // Preserve user input on invalid tokens instead of silently dropping them.
642
+ input_value = trimmed
643
+ run_validation(trimmed, mode)
644
+ return
524
645
  }
525
- const normalized = normalize_tokenized_input(trimmed, mode);
526
- set_value(normalized, mode);
527
- }
528
- function onkeydown(event) {
646
+
647
+ const normalized = normalize_tokenized_input(trimmed, mode)
648
+ set_value(normalized, mode)
649
+ }
650
+
651
+ function onkeydown(event: KeyboardEvent): void {
529
652
  if (event.key === `Enter`) {
530
- event.preventDefault();
531
- if (history_open && focused_history_idx >= 0) {
532
- set_value(visible_history[focused_history_idx]);
533
- }
534
- else {
535
- sync_value();
536
- }
537
- }
538
- else if (event.key === `Escape`) {
539
- if (history_open)
540
- close_history();
541
- else if (examples_open)
542
- examples_open = false;
543
- else if (input_value)
544
- clear_filter();
545
- }
546
- else if (history_open && visible_history.length > 0) {
547
- const len = visible_history.length;
548
- if (event.key === `ArrowDown`) {
549
- event.preventDefault();
550
- focused_history_idx = (focused_history_idx + 1) % len;
551
- }
552
- else if (event.key === `ArrowUp`) {
553
- event.preventDefault();
554
- focused_history_idx = focused_history_idx <= 0
555
- ? len - 1
556
- : focused_history_idx - 1;
557
- }
653
+ event.preventDefault()
654
+ if (history_open && focused_history_idx >= 0) {
655
+ set_value(visible_history[focused_history_idx])
656
+ } else {
657
+ sync_value()
658
+ }
659
+ } else if (event.key === `Escape`) {
660
+ if (history_open) close_history()
661
+ else if (examples_open) examples_open = false
662
+ else if (input_value) clear_filter()
663
+ } else if (history_open && visible_history.length > 0) {
664
+ const len = visible_history.length
665
+ if (event.key === `ArrowDown`) {
666
+ event.preventDefault()
667
+ focused_history_idx = (focused_history_idx + 1) % len
668
+ } else if (event.key === `ArrowUp`) {
669
+ event.preventDefault()
670
+ focused_history_idx = focused_history_idx <= 0
671
+ ? len - 1
672
+ : focused_history_idx - 1
673
+ }
558
674
  }
559
- }
560
- function oninput() {
675
+ }
676
+
677
+ function oninput(): void {
561
678
  if (history_open) {
562
- history_query = input_value;
563
- focused_history_idx = visible_history.length > 0 ? 0 : -1;
679
+ history_query = input_value
680
+ focused_history_idx = visible_history.length > 0 ? 0 : -1
564
681
  }
565
- const mode = mode_locked ? search_mode : infer_mode(input_value);
566
- run_validation(input_value, mode);
567
- }
568
- function clear_filter() {
569
- onclear?.();
570
- set_value(``);
571
- }
572
- function apply_example(example) {
573
- set_value(example, mode_locked ? search_mode : infer_mode(example));
574
- close_examples();
575
- }
576
- function toggle_examples(event) {
577
- event.stopPropagation();
578
- close_history();
579
- examples_open = !examples_open;
580
- focused_item_idx = examples_open ? 0 : -1;
581
- if (examples_open)
582
- anchor_left = false;
583
- }
584
- function handle_menu_keydown(event) {
585
- const len = all_examples.length;
586
- if (!len)
587
- return;
682
+ const mode = mode_locked ? search_mode : infer_mode(input_value)
683
+ run_validation(input_value, mode)
684
+ }
685
+
686
+ function clear_filter(): void {
687
+ onclear?.()
688
+ set_value(``)
689
+ }
690
+
691
+ function apply_example(example: string): void {
692
+ set_value(example, mode_locked ? search_mode : infer_mode(example))
693
+ close_examples()
694
+ }
695
+
696
+ function toggle_examples(event: MouseEvent): void {
697
+ event.stopPropagation()
698
+ close_history()
699
+ examples_open = !examples_open
700
+ focused_item_idx = examples_open ? 0 : -1
701
+ if (examples_open) anchor_left = false
702
+ }
703
+
704
+ function handle_menu_keydown(event: KeyboardEvent): void {
705
+ const len = all_examples.length
706
+ if (!len) return
588
707
  const is_button_activation = (event.key === `Enter` || event.key === ` `) &&
589
- event.target instanceof HTMLButtonElement;
590
- if (is_button_activation)
591
- return;
592
- const key_actions = {
593
- ArrowDown: () => (focused_item_idx = (focused_item_idx + 1) % len),
594
- ArrowUp: () => (focused_item_idx = (focused_item_idx - 1 + len) % len),
595
- Home: () => (focused_item_idx = 0),
596
- End: () => (focused_item_idx = len - 1),
597
- Escape: close_examples,
598
- };
708
+ event.target instanceof HTMLButtonElement
709
+ if (is_button_activation) return
710
+
711
+ const key_actions: Record<string, () => void> = {
712
+ ArrowDown: () => (focused_item_idx = (focused_item_idx + 1) % len),
713
+ ArrowUp: () => (focused_item_idx = (focused_item_idx - 1 + len) % len),
714
+ Home: () => (focused_item_idx = 0),
715
+ End: () => (focused_item_idx = len - 1),
716
+ Escape: close_examples,
717
+ }
718
+
599
719
  if (event.key in key_actions) {
600
- event.preventDefault();
601
- key_actions[event.key]();
720
+ event.preventDefault()
721
+ key_actions[event.key]()
602
722
  }
603
- }
604
- function toggle_mode_lock() {
605
- mode_locked = !mode_locked;
606
- }
607
- function remove_token(token_idx) {
608
- if (search_mode === `exact`)
609
- return;
610
- const separator = search_mode === `chemsys` ? `-` : `,`;
723
+ }
724
+
725
+ function toggle_mode_lock(): void {
726
+ mode_locked = !mode_locked
727
+ }
728
+
729
+ function remove_token(token_idx: number): void {
730
+ if (search_mode === `exact`) return
731
+ const separator = search_mode === `chemsys` ? `-` : `,`
611
732
  const tokens = tokenize_query(input_value, search_mode)
612
- .filter((_, idx) => idx !== token_idx);
613
- const next_value = tokens.map(serialize_token).join(separator);
614
- input_value = next_value;
615
- set_value(next_value, search_mode);
616
- }
617
- // Focus the active menu item when index changes
618
- $effect(() => {
619
- if (!examples_open || focused_item_idx < 0)
620
- return;
621
- const items = wrapper?.querySelectorAll(`[data-example-item]`);
622
- items?.[focused_item_idx]?.focus({ preventScroll: true });
623
- });
624
- let placeholder = $derived(search_mode === `chemsys`
625
- ? `Li-Fe-O or Li-*-*`
626
- : search_mode === `exact`
627
- ? `LiFePO4 or LiFe*2*`
628
- : `Li,Fe,O or Li,*,*`);
629
- const MODE_LABELS = {
733
+ .filter((_, idx) => idx !== token_idx)
734
+ const next_value = tokens.map(serialize_token).join(separator)
735
+ input_value = next_value
736
+ set_value(next_value, search_mode)
737
+ }
738
+
739
+ // Focus the active menu item when index changes
740
+ $effect(() => {
741
+ if (!examples_open || focused_item_idx < 0) return
742
+ const items = wrapper?.querySelectorAll<HTMLButtonElement>(`[data-example-item]`)
743
+ items?.[focused_item_idx]?.focus({ preventScroll: true })
744
+ })
745
+
746
+ let placeholder = $derived(
747
+ search_mode === `chemsys`
748
+ ? `Li-Fe-O or Li-*-*`
749
+ : search_mode === `exact`
750
+ ? `LiFePO4 or LiFe*2*`
751
+ : `Li,Fe,O or Li,*,*`,
752
+ )
753
+
754
+ const MODE_LABELS: Record<FormulaSearchMode, string> = {
630
755
  elements: `has elements`,
631
756
  chemsys: `chemical system`,
632
757
  exact: `exact formula`,
633
- };
634
- let mode_hint = $derived(MODE_LABELS[search_mode]);
635
- let parsed_tokens = $derived(tokenize_query(input_value, search_mode));
636
- let show_chip_row = $derived(show_chip_editor && search_mode !== `exact` && parsed_tokens.length > 0);
637
- // Preview of next mode cycle step for tooltip
638
- let next_mode = $derived.by(() => {
639
- const next = MODE_CYCLE[(MODE_CYCLE.indexOf(search_mode) + 1) % MODE_CYCLE.length];
640
- const mode = MODE_LABELS[next];
641
- const next_value = format_for_mode(extract_elements(value), next);
642
- return { mode, value: next_value };
643
- });
758
+ }
759
+
760
+ let mode_hint = $derived(MODE_LABELS[search_mode])
761
+ let parsed_tokens = $derived(tokenize_query(input_value, search_mode))
762
+ let show_chip_row = $derived(
763
+ show_chip_editor && search_mode !== `exact` && parsed_tokens.length > 0,
764
+ )
765
+ // Preview of next mode cycle step for tooltip
766
+ let next_mode = $derived.by(() => {
767
+ const next = MODE_CYCLE[(MODE_CYCLE.indexOf(search_mode) + 1) % MODE_CYCLE.length]
768
+ const mode = MODE_LABELS[next]
769
+ const next_value = format_for_mode(extract_elements(value), next)
770
+ return { mode, value: next_value }
771
+ })
644
772
  </script>
645
773
 
646
774
  <svelte:document onclick={handle_document_click} />