-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathinitial-state.ts
95 lines (82 loc) · 2.87 KB
/
initial-state.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Portions of this file are Copyright 2021 Google LLC, and licensed under GPL2+. See COPYING.
import defaultScad from './default-scad.ts';
import { State } from './app-state.ts';
export const defaultSourcePath = '/playground.scad';
export const defaultModelColor = '#f9d72c';
const defaultBlurhash = "|KSPX^%3~qtjMx$lR*x]t7n,R%xuxbM{WBt7ayfk_3bY9FnAt8XOxanjNF%fxbMyIn%3t7NFoLaeoeV[WBo{xar^IoS1xbxcR*S0xbofRjV[j[kCNGofxaWBNHW-xasDR*WTkBxuWBM{s:t7bYahRjfkozWUadofbIW:jZ";
export function createInitialState(state: State | null, source?: {content?: string, path?: string, url?: string, blurhash?: string}): State {
type Mode = State['view']['layout']['mode'];
const mode: Mode = window.matchMedia("(min-width: 768px)").matches
? 'multi' : 'single';
let initialState: State;
if (state) {
if (source) throw new Error('Cannot provide source when state is provided');
initialState = state;
} else {
let content, path, url, blurhash;
if (source) {
content = source.content;
path = source.path;
url = source.url;
blurhash = source.blurhash;
} else {
content = defaultScad;
path = defaultSourcePath;
blurhash = defaultBlurhash;
}
let activePath = path ?? (url && new URL(url).pathname.split('/').pop()) ?? defaultSourcePath;
initialState = {
params: {
activePath,
sources: [{path: activePath, content, url}],
features: [],
exportFormat2D: 'svg',
exportFormat3D: 'stl',
},
view: {
layout: {
mode: 'multi',
editor: true,
viewer: true,
customizer: false,
} as any,
color: defaultModelColor,
},
preview: blurhash ? {blurhash} : undefined,
};
}
if (initialState.view.layout.mode != mode) {
if (mode === 'multi' && initialState.view.layout.mode === 'single') {
initialState.view.layout = {
mode,
editor: true,
viewer: true,
customizer: initialState.view.layout.focus == 'customizer'
}
} else if (mode === 'single' && initialState.view.layout.mode === 'multi') {
initialState.view.layout = {
mode,
focus: initialState.view.layout.viewer ? 'viewer'
: initialState.view.layout.customizer ? 'customizer'
: 'editor'
}
}
}
initialState.view.showAxes ??= true;
// fs.writeFile(initialState.params.sourcePath, initialState.params.source);
// if (initialState.params.sourcePath !== defaultSourcePath) {
// fs.writeFile(defaultSourcePath, defaultScad);
// }
const defaultFeatures = ['lazy-union'];
defaultFeatures.forEach(f => {
if (initialState.params.features.indexOf(f) < 0)
initialState.params.features.push(f);
});
return initialState;
}
export function getBlankProjectState() {
return createInitialState(null, {
path: defaultSourcePath,
content: defaultScad,
});
}