@selvajs/ui 6.6.0-beta.7 → 6.6.0-beta.9
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.
- package/dist/components/compute/AppLayout.svelte +37 -7
- package/dist/components/compute/AppLayout.svelte.d.ts +5 -0
- package/dist/components/compute/ParameterPresetManager.svelte +8 -20
- package/dist/components/compute/SolveMessages.svelte +26 -3
- package/dist/components/compute/SolveMessages.svelte.d.ts +7 -0
- package/dist/components/layout/PageFooter.svelte +3 -1
- package/dist/components/layout/SideNav.svelte +55 -14
- package/dist/components/preview/ChartOutput.svelte +18 -13
- package/dist/components/preview/Group.svelte +6 -1
- package/dist/components/preview/ImageOutput.svelte +3 -1
- package/dist/components/preview/InputControl.svelte +108 -44
- package/dist/components/preview/InputControl.svelte.d.ts +5 -1
- package/dist/components/preview/TabContent.svelte +61 -29
- package/dist/components/preview/TabContent.svelte.d.ts +5 -1
- package/dist/components/preview/TabLayout.svelte +25 -3
- package/dist/components/preview/TabLayout.svelte.d.ts +5 -0
- package/dist/components/preview/inputs/ChecklistInput.svelte +7 -2
- package/dist/components/preview/inputs/ChecklistInput.svelte.d.ts +2 -1
- package/dist/components/preview/inputs/DropdownInput.svelte +16 -4
- package/dist/components/preview/inputs/DropdownInput.svelte.d.ts +2 -1
- package/dist/components/preview/inputs/FileInput.svelte +56 -65
- package/dist/components/preview/inputs/GeometryInput.svelte +128 -0
- package/dist/components/preview/inputs/GeometryInput.svelte.d.ts +21 -0
- package/dist/components/preview/inputs/OptionGlyph.svelte +29 -0
- package/dist/components/preview/inputs/OptionGlyph.svelte.d.ts +8 -0
- package/dist/components/preview/inputs/SegmentedInput.svelte +44 -0
- package/dist/components/preview/inputs/SegmentedInput.svelte.d.ts +12 -0
- package/dist/components/preview/inputs/TileInput.svelte +80 -0
- package/dist/components/preview/inputs/TileInput.svelte.d.ts +12 -0
- package/dist/components/preview/inputs/index.d.ts +3 -0
- package/dist/components/preview/inputs/index.js +3 -0
- package/dist/components/primitives/SolvingIndicator.svelte +28 -3
- package/dist/components/primitives/SolvingIndicator.svelte.d.ts +7 -0
- package/dist/components/primitives/button/button.svelte +3 -0
- package/dist/components/primitives/button/button.svelte.d.ts +3 -0
- package/dist/components/primitives/toggle/toggle.svelte +3 -1
- package/dist/components/primitives/toggle/toggle.svelte.d.ts +3 -0
- package/dist/components/viewer/Viewer.svelte +11 -11
- package/dist/compute/solving.svelte.d.ts +7 -1
- package/dist/compute/solving.svelte.js +12 -1
- package/dist/contexts/hostContext.svelte.d.ts +39 -0
- package/dist/contexts/hostContext.svelte.js +9 -0
- package/dist/i18n/messages.d.ts +33 -0
- package/dist/i18n/messages.js +58 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +3 -0
- package/dist/schema/host-support.d.ts +29 -0
- package/dist/schema/host-support.js +97 -0
- package/dist/schema/option-display.d.ts +12 -0
- package/dist/schema/option-display.js +29 -0
- package/dist/schema/param-exporter.d.ts +4 -1
- package/dist/schema/param-exporter.js +10 -6
- package/dist/styles/base.css +6 -3
- package/dist/utils/file-download.js +10 -22
- package/dist/utils/host-files.d.ts +12 -0
- package/dist/utils/host-files.js +46 -0
- package/dist/utils/webp-to-png.d.ts +10 -0
- package/dist/utils/webp-to-png.js +41 -0
- package/package.json +9 -9
- package/src/lib/components/compute/AppLayout.svelte +37 -7
- package/src/lib/components/compute/ParameterPresetManager.svelte +8 -20
- package/src/lib/components/compute/SolveMessages.svelte +26 -3
- package/src/lib/components/layout/PageFooter.svelte +3 -1
- package/src/lib/components/layout/SideNav.svelte +55 -14
- package/src/lib/components/preview/ChartOutput.svelte +18 -13
- package/src/lib/components/preview/Group.svelte +6 -1
- package/src/lib/components/preview/ImageOutput.svelte +3 -1
- package/src/lib/components/preview/InputControl.svelte +108 -44
- package/src/lib/components/preview/TabContent.svelte +61 -29
- package/src/lib/components/preview/TabLayout.svelte +25 -3
- package/src/lib/components/preview/inputs/ChecklistInput.svelte +7 -2
- package/src/lib/components/preview/inputs/DropdownInput.svelte +16 -4
- package/src/lib/components/preview/inputs/FileInput.svelte +56 -65
- package/src/lib/components/preview/inputs/GeometryInput.svelte +128 -0
- package/src/lib/components/preview/inputs/OptionGlyph.svelte +29 -0
- package/src/lib/components/preview/inputs/SegmentedInput.svelte +44 -0
- package/src/lib/components/preview/inputs/TileInput.svelte +80 -0
- package/src/lib/components/preview/inputs/index.ts +3 -0
- package/src/lib/components/primitives/SolvingIndicator.svelte +28 -3
- package/src/lib/components/primitives/button/button.svelte +3 -0
- package/src/lib/components/primitives/toggle/toggle.svelte +3 -1
- package/src/lib/components/viewer/Viewer.svelte +11 -11
- package/src/lib/compute/solving.svelte.ts +16 -1
- package/src/lib/contexts/hostContext.svelte.ts +51 -0
- package/src/lib/i18n/messages.ts +96 -0
- package/src/lib/index.ts +20 -0
- package/src/lib/schema/host-support.ts +129 -0
- package/src/lib/schema/option-display.ts +44 -0
- package/src/lib/schema/param-exporter.ts +10 -7
- package/src/lib/styles/base.css +6 -3
- package/src/lib/utils/file-download.ts +11 -21
- package/src/lib/utils/host-files.ts +61 -0
- package/src/lib/utils/webp-to-png.ts +48 -0
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
// Which elements of a schema each host renders. Worked out from the schema on every read and
|
|
2
|
+
// never stored in it: a stored copy goes stale the way a saved paramType does. Making an element
|
|
3
|
+
// work on Selva later is a change to SUPPORT, with no schema change and no migration.
|
|
4
|
+
|
|
5
|
+
import type {
|
|
6
|
+
InputLayoutItem,
|
|
7
|
+
LayoutItem,
|
|
8
|
+
SchemaInput,
|
|
9
|
+
UISchema,
|
|
10
|
+
CheckboxWidgetConfig
|
|
11
|
+
} from '@selvajs/schemas';
|
|
12
|
+
import { getGroups } from '@selvajs/schemas';
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Where a UI runs: `rhino` is the plugin's page inside Rhino, solving in the user's own
|
|
16
|
+
* Grasshopper; `selva` is the Selva app, solving headless on Rhino.Compute.
|
|
17
|
+
*/
|
|
18
|
+
export type UIHost = 'rhino' | 'selva';
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* - `value`: an input or output every host renders.
|
|
22
|
+
* - `geometry`: a Rhino Get Geometry/Point/Line/Plane input, filled by picking in the viewport.
|
|
23
|
+
* - `trigger`: a boolean input shown as a button; a press solves once with `true`.
|
|
24
|
+
* - `unknown`: a layout item from a newer schema than this build knows.
|
|
25
|
+
*/
|
|
26
|
+
export type ElementKind = 'value' | 'geometry' | 'trigger' | 'unknown';
|
|
27
|
+
|
|
28
|
+
const SUPPORT: Record<ElementKind, readonly UIHost[]> = {
|
|
29
|
+
value: ['rhino', 'selva'],
|
|
30
|
+
geometry: ['rhino'],
|
|
31
|
+
trigger: ['rhino'],
|
|
32
|
+
unknown: []
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
export const HOST_LABELS: Record<UIHost, string> = { rhino: 'Rhino', selva: 'Selva' };
|
|
36
|
+
|
|
37
|
+
const GEOMETRY_PARAM_TYPES: ReadonlySet<string> = new Set(['geometry', 'point', 'line', 'plane']);
|
|
38
|
+
|
|
39
|
+
type WidgetTypeOf<T extends 'input' | 'output'> = Extract<LayoutItem, { type: T }>['widgetType'];
|
|
40
|
+
|
|
41
|
+
// Keyed by the schema's own widget unions, so a widget added to the schema fails to compile here
|
|
42
|
+
// until someone decides which hosts render it.
|
|
43
|
+
const INPUT_WIDGETS = {
|
|
44
|
+
number: true,
|
|
45
|
+
text: true,
|
|
46
|
+
dropdown: true,
|
|
47
|
+
dynamicValueList: true,
|
|
48
|
+
checkbox: true,
|
|
49
|
+
file: true,
|
|
50
|
+
color: true
|
|
51
|
+
} as const satisfies Record<WidgetTypeOf<'input'>, true>;
|
|
52
|
+
const OUTPUT_WIDGETS = {
|
|
53
|
+
text: true,
|
|
54
|
+
number: true,
|
|
55
|
+
file: true,
|
|
56
|
+
chart: true,
|
|
57
|
+
image: true,
|
|
58
|
+
dynamicValueList: true
|
|
59
|
+
} as const satisfies Record<WidgetTypeOf<'output'>, true>;
|
|
60
|
+
|
|
61
|
+
const KNOWN_WIDGETS: Record<'input' | 'output', ReadonlySet<string>> = {
|
|
62
|
+
input: new Set(Object.keys(INPUT_WIDGETS)),
|
|
63
|
+
output: new Set(Object.keys(OUTPUT_WIDGETS))
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
/** The control comes from this, whatever the saved widgetType says: saved ones hold `text`. */
|
|
67
|
+
export function isGeometryParamType(paramType: string | undefined): boolean {
|
|
68
|
+
return paramType !== undefined && GEOMETRY_PARAM_TYPES.has(paramType);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export function isTriggerItem(item: LayoutItem): boolean {
|
|
72
|
+
return (
|
|
73
|
+
item.type === 'input' &&
|
|
74
|
+
item.widgetType === 'checkbox' &&
|
|
75
|
+
(item.config as CheckboxWidgetConfig | undefined)?.displayAs === 'button'
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export function layoutItemKind(item: LayoutItem, inputs: readonly SchemaInput[]): ElementKind {
|
|
80
|
+
if (item.type === 'linebreak') return 'value';
|
|
81
|
+
if (item.type !== 'input' && item.type !== 'output') return 'unknown';
|
|
82
|
+
if (item.type === 'input') {
|
|
83
|
+
const paramType = inputs.find((i) => i.id === item.paramId)?.paramType;
|
|
84
|
+
if (isGeometryParamType(paramType)) return 'geometry';
|
|
85
|
+
}
|
|
86
|
+
if (!KNOWN_WIDGETS[item.type].has(item.widgetType)) return 'unknown';
|
|
87
|
+
return isTriggerItem(item) ? 'trigger' : 'value';
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export function hostsFor(kind: ElementKind): readonly UIHost[] {
|
|
91
|
+
return SUPPORT[kind];
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export function isSupportedOn(host: UIHost, kind: ElementKind): boolean {
|
|
95
|
+
return SUPPORT[kind].includes(host);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Inputs the form keeps no value for: geometry lives on the Grasshopper param after a pick, and a
|
|
100
|
+
* button only sends a press. Value updates and presets skip them; holding a button's `true` would
|
|
101
|
+
* fire its action again on every solve.
|
|
102
|
+
*/
|
|
103
|
+
export function isValuelessInput(item: InputLayoutItem, input: SchemaInput | undefined): boolean {
|
|
104
|
+
return isGeometryParamType(input?.paramType) || isTriggerItem(item);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export function valuelessInputIds(schema: UISchema): Set<string> {
|
|
108
|
+
const ids = new Set<string>();
|
|
109
|
+
for (const input of schema.inputs) {
|
|
110
|
+
if (isGeometryParamType(input.paramType)) ids.add(input.id);
|
|
111
|
+
}
|
|
112
|
+
for (const group of getGroups(schema)) {
|
|
113
|
+
for (const item of group.items ?? []) {
|
|
114
|
+
if (isTriggerItem(item)) ids.add((item as InputLayoutItem).paramId);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
return ids;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** The placed items a host won't render, for the builder's summary of where the UI runs. */
|
|
121
|
+
export function unsupportedItems(schema: UISchema, host: UIHost): LayoutItem[] {
|
|
122
|
+
const items: LayoutItem[] = [];
|
|
123
|
+
for (const group of getGroups(schema)) {
|
|
124
|
+
for (const item of group.items ?? []) {
|
|
125
|
+
if (!isSupportedOn(host, layoutItemKind(item, schema.inputs))) items.push(item);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return items;
|
|
129
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type { DropdownWidgetConfig, OptionMedia, UISchema } from '@selvajs/schemas';
|
|
2
|
+
|
|
3
|
+
export type OptionDisplay = NonNullable<DropdownWidgetConfig['displayAs']>;
|
|
4
|
+
|
|
5
|
+
// Past this a button row no longer fits a panel column. A computed list can grow past it at
|
|
6
|
+
// runtime, so the row turns back into a dropdown instead of wrapping.
|
|
7
|
+
const MAX_SEGMENTED_OPTIONS = 6;
|
|
8
|
+
|
|
9
|
+
export function resolveOptionDisplay(
|
|
10
|
+
displayAs: string | undefined,
|
|
11
|
+
optionCount: number
|
|
12
|
+
): OptionDisplay {
|
|
13
|
+
switch (displayAs) {
|
|
14
|
+
case 'checklist':
|
|
15
|
+
case 'tiles':
|
|
16
|
+
return displayAs;
|
|
17
|
+
case 'segmented':
|
|
18
|
+
return optionCount <= MAX_SEGMENTED_OPTIONS ? 'segmented' : 'dropdown';
|
|
19
|
+
default:
|
|
20
|
+
return 'dropdown';
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export type GlyphSource = { kind: 'icon'; d: string } | { kind: 'image'; src: string };
|
|
25
|
+
|
|
26
|
+
// The schema can come from any .gh, so an image reaches <img> only as an inline raster data URI.
|
|
27
|
+
// Anything else could be a URL that makes every viewer's browser call wherever the file points.
|
|
28
|
+
const RASTER_DATA_URI = /^data:image\/(webp|jpeg|png);base64,[A-Za-z0-9+/=]+$/;
|
|
29
|
+
|
|
30
|
+
/** What to draw beside an option: its thumbnail, else its icon. */
|
|
31
|
+
export function optionGlyph(
|
|
32
|
+
media: OptionMedia | undefined,
|
|
33
|
+
assets: UISchema['assets'] | undefined
|
|
34
|
+
): GlyphSource | undefined {
|
|
35
|
+
if (!media || !assets) return undefined;
|
|
36
|
+
|
|
37
|
+
if (media.image) {
|
|
38
|
+
const src = assets[media.image]?.img;
|
|
39
|
+
if (src && RASTER_DATA_URI.test(src)) return { kind: 'image', src };
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const d = media.icon ? assets[media.icon]?.d : undefined;
|
|
43
|
+
return d ? { kind: 'icon', d } : undefined;
|
|
44
|
+
}
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { randomId } from '../utils/randomId';
|
|
2
|
+
import { saveBlob } from '../utils/host-files';
|
|
2
3
|
import { getGroups } from '@selvajs/schemas';
|
|
4
|
+
import { isValuelessInput, valuelessInputIds } from './host-support';
|
|
3
5
|
import type {
|
|
4
6
|
UISchema,
|
|
5
7
|
ParameterPreset,
|
|
@@ -24,6 +26,7 @@ export function createSavedState(
|
|
|
24
26
|
if (item.type !== 'input') continue;
|
|
25
27
|
const value = currentValues[item.paramId];
|
|
26
28
|
const inputDef = schema.inputs.find((i) => i.id === item.paramId);
|
|
29
|
+
if (isValuelessInput(item, inputDef)) continue;
|
|
27
30
|
if (value !== undefined && inputDef) {
|
|
28
31
|
parameters.push({
|
|
29
32
|
paramId: item.paramId,
|
|
@@ -110,15 +113,18 @@ export function validateSavedState(
|
|
|
110
113
|
* Params the current schema no longer has are dropped. That's the same condition
|
|
111
114
|
* `validateSavedState` flags as an error, but the rule is re-derived from the schema here
|
|
112
115
|
* rather than read back off the issue list — nothing couples to how sentinel ids are encoded.
|
|
116
|
+
* Valueless inputs are dropped too: a preset saved before an input became a button or a
|
|
117
|
+
* geometry input still holds a value for it.
|
|
113
118
|
*/
|
|
114
119
|
export function extractLoadableValues(
|
|
115
120
|
savedState: ParameterPreset,
|
|
116
121
|
currentSchema: UISchema
|
|
117
122
|
): Record<string, unknown> {
|
|
118
123
|
const values: Record<string, unknown> = {};
|
|
124
|
+
const valueless = valuelessInputIds(currentSchema);
|
|
119
125
|
for (const paramState of savedState.parameters) {
|
|
120
126
|
const exists = currentSchema.inputs.some((i) => i.id === paramState.paramId);
|
|
121
|
-
if (exists) {
|
|
127
|
+
if (exists && !valueless.has(paramState.paramId)) {
|
|
122
128
|
values[paramState.paramId] = paramState.value;
|
|
123
129
|
}
|
|
124
130
|
}
|
|
@@ -147,18 +153,15 @@ export function loadPreset(savedState: ParameterPreset, currentSchema: UISchema)
|
|
|
147
153
|
};
|
|
148
154
|
}
|
|
149
155
|
|
|
150
|
-
|
|
156
|
+
/** Resolves false when the user cancelled the save. */
|
|
157
|
+
export function exportStateAsJson(savedState: ParameterPreset): Promise<boolean> {
|
|
151
158
|
const blob = new Blob([JSON.stringify(savedState, null, 2)], { type: 'application/json' });
|
|
152
159
|
const safeName = savedState.name.replace(/[^a-z0-9]/gi, '_');
|
|
153
160
|
const date = savedState.timestamp.split('T')[0].replace(/-/g, '_');
|
|
154
161
|
|
|
155
|
-
const link = document.createElement('a');
|
|
156
|
-
link.href = URL.createObjectURL(blob);
|
|
157
162
|
// .slvp = Selva parameter preset. Import still accepts the pre-rename .sps — same JSON,
|
|
158
163
|
// only the writer's extension changed.
|
|
159
|
-
|
|
160
|
-
link.click();
|
|
161
|
-
URL.revokeObjectURL(link.href);
|
|
164
|
+
return saveBlob(blob, `${safeName}_${date}.slvp`);
|
|
162
165
|
}
|
|
163
166
|
|
|
164
167
|
export async function importStateFromJson(file: File): Promise<ParameterPreset> {
|
package/src/lib/styles/base.css
CHANGED
|
@@ -7,11 +7,14 @@
|
|
|
7
7
|
--page-py: 1.75rem;
|
|
8
8
|
--background: oklch(0.99 0 0);
|
|
9
9
|
--foreground: oklch(0.22 0 0);
|
|
10
|
-
|
|
10
|
+
/* White, so a card stands apart from background and muted; Group adds a border, since
|
|
11
|
+
white on background alone is too close to see. */
|
|
12
|
+
--card: oklch(1 0 0);
|
|
11
13
|
--card-foreground: oklch(0.22 0 0);
|
|
12
14
|
--popover: oklch(0.99 0 0);
|
|
13
15
|
--popover-foreground: oklch(0.22 0 0);
|
|
14
|
-
|
|
16
|
+
/* 4.5:1 as text on background, card and muted, and under white text. */
|
|
17
|
+
--primary: oklch(0.52 0.07 135);
|
|
15
18
|
--primary-foreground: oklch(0.985 0 0);
|
|
16
19
|
--secondary: oklch(0.94 0 0);
|
|
17
20
|
--secondary-foreground: oklch(0.28 0 0);
|
|
@@ -40,7 +43,7 @@
|
|
|
40
43
|
--chart-5: oklch(0.68 0.15 30);
|
|
41
44
|
--sidebar: oklch(0.96 0 0);
|
|
42
45
|
--sidebar-foreground: oklch(0.25 0 0);
|
|
43
|
-
--sidebar-primary: oklch(0.
|
|
46
|
+
--sidebar-primary: oklch(0.52 0.07 135);
|
|
44
47
|
--sidebar-primary-foreground: oklch(0.985 0 0);
|
|
45
48
|
--sidebar-accent: oklch(0.93 0 0);
|
|
46
49
|
--sidebar-accent-foreground: oklch(0.3 0 0);
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import { downloadFileDataByRoot, type FileData } from '@selvajs/compute/core';
|
|
1
|
+
import { decodeBase64ToBinary, downloadFileDataByRoot, type FileData } from '@selvajs/compute/core';
|
|
2
2
|
import { SvelteMap } from 'svelte/reactivity';
|
|
3
3
|
import { APP_DEFAULTS } from '../constants';
|
|
4
|
+
import { saveBlob } from './host-files';
|
|
4
5
|
|
|
5
6
|
export const MIME_BY_EXT: Record<string, string> = {
|
|
6
7
|
'.png': 'image/png',
|
|
@@ -19,26 +20,15 @@ export const MIME_BY_EXT: Record<string, string> = {
|
|
|
19
20
|
'.stl': 'model/stl'
|
|
20
21
|
};
|
|
21
22
|
|
|
22
|
-
function saveSingleFile(file: FileData):
|
|
23
|
+
function saveSingleFile(file: FileData): Promise<boolean> {
|
|
23
24
|
const ext = (file.fileType ?? '').toLowerCase();
|
|
24
25
|
const mime = MIME_BY_EXT[ext] ?? 'application/octet-stream';
|
|
25
26
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
const bytes = new Uint8Array(binary.length);
|
|
30
|
-
for (let i = 0; i < binary.length; i++) bytes[i] = binary.charCodeAt(i);
|
|
31
|
-
blob = new Blob([bytes], { type: mime });
|
|
32
|
-
} else {
|
|
33
|
-
blob = new Blob([file.data], { type: mime });
|
|
34
|
-
}
|
|
27
|
+
const content = file.isBase64Encoded ? decodeBase64ToBinary(file.data) : file.data;
|
|
28
|
+
// decodeBase64ToBinary returns an ArrayBuffer-backed copy, never a SharedArrayBuffer view.
|
|
29
|
+
const blob = new Blob([content as BlobPart], { type: mime });
|
|
35
30
|
|
|
36
|
-
|
|
37
|
-
const a = document.createElement('a');
|
|
38
|
-
a.href = URL.createObjectURL(blob);
|
|
39
|
-
a.download = fullName;
|
|
40
|
-
a.click();
|
|
41
|
-
URL.revokeObjectURL(a.href);
|
|
31
|
+
return saveBlob(blob, `${file.fileName}${file.fileType ?? ''}`);
|
|
42
32
|
}
|
|
43
33
|
|
|
44
34
|
export async function downloadFiles(
|
|
@@ -53,14 +43,14 @@ export async function downloadFiles(
|
|
|
53
43
|
return;
|
|
54
44
|
}
|
|
55
45
|
|
|
56
|
-
// A lone file is saved as itself rather than zipped
|
|
57
|
-
//
|
|
46
|
+
// A lone file is saved as itself rather than zipped. Everything else, including the
|
|
47
|
+
// per-root archive split, belongs to compute.
|
|
58
48
|
if (filesArray.length === 1) {
|
|
59
|
-
saveSingleFile(filesArray[0]);
|
|
49
|
+
await saveSingleFile(filesArray[0]);
|
|
60
50
|
return;
|
|
61
51
|
}
|
|
62
52
|
|
|
63
|
-
await downloadFileDataByRoot(filesArray, fileName);
|
|
53
|
+
await downloadFileDataByRoot(filesArray, fileName, null, { saveBlob });
|
|
64
54
|
} catch (error) {
|
|
65
55
|
console.error('[FileDownload] Error downloading files:', error);
|
|
66
56
|
throw error;
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
// Where a file the page offers goes, and where a file the page asks for comes from. A browser
|
|
2
|
+
// gets an <a download> click and a hidden <input type=file>. A host whose WebView can't do
|
|
3
|
+
// either safely (the Rhino panel) installs its own once at start-up, before the first save or pick.
|
|
4
|
+
|
|
5
|
+
/** Resolves false when the user cancelled. An anchor download always resolves true: it can't tell. */
|
|
6
|
+
export type BlobSaver = (blob: Blob, fileName: string) => Promise<boolean>;
|
|
7
|
+
|
|
8
|
+
/** `accept` is what `<input accept>` takes: `.slvp,.sps`, `image/*`. Resolves null on cancel. */
|
|
9
|
+
export type FilePicker = (options: { accept: string }) => Promise<File | null>;
|
|
10
|
+
|
|
11
|
+
let saver: BlobSaver = saveWithAnchor;
|
|
12
|
+
let picker: FilePicker = pickWithInput;
|
|
13
|
+
|
|
14
|
+
export function setBlobSaver(next: BlobSaver): void {
|
|
15
|
+
saver = next;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function setFilePicker(next: FilePicker): void {
|
|
19
|
+
picker = next;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function saveBlob(blob: Blob, fileName: string): Promise<boolean> {
|
|
23
|
+
return saver(blob, fileName);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function pickFile(options: { accept: string }): Promise<File | null> {
|
|
27
|
+
return picker(options);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
async function saveWithAnchor(blob: Blob, fileName: string): Promise<boolean> {
|
|
31
|
+
const url = URL.createObjectURL(blob);
|
|
32
|
+
const a = document.createElement('a');
|
|
33
|
+
a.href = url;
|
|
34
|
+
a.download = fileName;
|
|
35
|
+
// Firefox only downloads from an anchor that is in the document.
|
|
36
|
+
document.body.appendChild(a);
|
|
37
|
+
a.click();
|
|
38
|
+
a.remove();
|
|
39
|
+
// Revoking synchronously can abort the download: the browser pins the blob only once it starts.
|
|
40
|
+
setTimeout(() => URL.revokeObjectURL(url), 10_000);
|
|
41
|
+
return true;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function pickWithInput({ accept }: { accept: string }): Promise<File | null> {
|
|
45
|
+
return new Promise((resolve) => {
|
|
46
|
+
const input = document.createElement('input');
|
|
47
|
+
input.type = 'file';
|
|
48
|
+
input.accept = accept;
|
|
49
|
+
input.style.display = 'none';
|
|
50
|
+
|
|
51
|
+
const settle = (file: File | null) => {
|
|
52
|
+
input.remove();
|
|
53
|
+
resolve(file);
|
|
54
|
+
};
|
|
55
|
+
input.addEventListener('change', () => settle(input.files?.[0] ?? null), { once: true });
|
|
56
|
+
input.addEventListener('cancel', () => settle(null), { once: true });
|
|
57
|
+
|
|
58
|
+
document.body.appendChild(input);
|
|
59
|
+
input.click();
|
|
60
|
+
});
|
|
61
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
// Grasshopper's PDF export can't read WebP: the plugin's PDF library has no WebP decoder, and
|
|
2
|
+
// nothing on Windows or Rhino.Compute decodes it reliably. The browser can, so a WebP picked for
|
|
3
|
+
// an input that also takes PNG is re-encoded here. PNG rather than JPEG keeps transparency.
|
|
4
|
+
|
|
5
|
+
/** True when a WebP should go out as PNG: the input takes PNG, so a PNG is a valid value. */
|
|
6
|
+
export function shouldConvertWebp(fileEnding: string, acceptedFormats: readonly string[]): boolean {
|
|
7
|
+
return fileEnding === '.webp' && acceptedFormats.includes('.png');
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/** Whether the input takes a file with this ending, counting a WebP it would get as PNG. */
|
|
11
|
+
export function acceptsFileEnding(fileEnding: string, acceptedFormats: readonly string[]): boolean {
|
|
12
|
+
const ending = fileEnding.toLowerCase();
|
|
13
|
+
if (acceptedFormats.length === 0 || acceptedFormats.includes(ending)) return true;
|
|
14
|
+
return shouldConvertWebp(ending, acceptedFormats);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const ascii = (bytes: Uint8Array, start: number, end: number) =>
|
|
18
|
+
String.fromCharCode(...bytes.subarray(start, end));
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* By content, not name: image CDNs serve WebP from URLs with no extension or one naming another
|
|
22
|
+
* format. A WebP file starts `RIFF`, a 4-byte size, then `WEBP`.
|
|
23
|
+
*/
|
|
24
|
+
export async function isWebp(blob: Blob): Promise<boolean> {
|
|
25
|
+
if (blob.type === 'image/webp') return true;
|
|
26
|
+
const head = new Uint8Array(await blob.slice(0, 12).arrayBuffer());
|
|
27
|
+
return head.length === 12 && ascii(head, 0, 4) === 'RIFF' && ascii(head, 8, 12) === 'WEBP';
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export async function webpToPng(blob: Blob): Promise<Blob> {
|
|
31
|
+
const bitmap = await createImageBitmap(blob);
|
|
32
|
+
try {
|
|
33
|
+
const canvas = document.createElement('canvas');
|
|
34
|
+
canvas.width = bitmap.width;
|
|
35
|
+
canvas.height = bitmap.height;
|
|
36
|
+
const context = canvas.getContext('2d');
|
|
37
|
+
if (!context) throw new Error('Canvas 2D context unavailable');
|
|
38
|
+
context.drawImage(bitmap, 0, 0);
|
|
39
|
+
return await new Promise<Blob>((resolve, reject) =>
|
|
40
|
+
canvas.toBlob(
|
|
41
|
+
(png) => (png ? resolve(png) : reject(new Error('PNG encoding failed'))),
|
|
42
|
+
'image/png'
|
|
43
|
+
)
|
|
44
|
+
);
|
|
45
|
+
} finally {
|
|
46
|
+
bitmap.close();
|
|
47
|
+
}
|
|
48
|
+
}
|