openscad-playground/src/index.tsx

92 lines
2.6 KiB
TypeScript
Raw Normal View History

// Portions of this file are Copyright 2021 Google LLC, and licensed under GPL2+. See COPYING.
import React from 'react';
import ReactDOM from 'react-dom/client';
import {App} from './App';
import { createEditorFS } from './filesystem';
import { registerOpenSCADLanguage } from './language/openscad-register-language';
import { zipArchives } from './zip-archives';
import {readStateFromFragment} from './fragment-state'
import { State } from './app-state';
2023-03-25 19:30:34 +00:00
import defaultScad from './default-scad'
2023-03-25 19:34:07 +00:00
import './index.css';
2023-03-26 07:48:04 +01:00
// import "react-widgets/styles.css";
2023-03-25 19:30:34 +00:00
import debug from 'debug';
const log = debug('app:log');
if (process.env.NODE_ENV !== 'production') {
debug.enable('*');
log('Logging is enabled!');
} else {
debug.disable();
}
(async () => {
const workingDir = '/home';
2023-03-26 07:48:04 +01:00
const fs = await createEditorFS(workingDir)!;
await registerOpenSCADLanguage(fs, workingDir, zipArchives);
2023-03-26 07:48:04 +01:00
type Mode = State['view']['layout']['mode'];
const mode: Mode = window.matchMedia("(min-width: 768px)").matches
? 'multi' : 'single';
const defaultSourcePath = '/home/playground.scad';
const initialState: State = {
params: {
2023-03-26 07:48:04 +01:00
sourcePath: defaultSourcePath,
2023-03-25 19:30:34 +00:00
source: defaultScad,
features: [],
2023-03-26 07:48:04 +01:00
},
view: {
layout: {
mode: 'multi',
editor: true,
viewer: true,
customizer: false,
} as any
},
...(readStateFromFragment() ?? {})
2023-03-25 19:30:34 +00:00
};
2023-03-26 07:48:04 +01:00
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'
}
}
}
fs.writeFile(initialState.params.sourcePath, initialState.params.source);
if (initialState.params.sourcePath !== defaultSourcePath) {
fs.writeFile(defaultSourcePath, defaultScad);
}
const defaultFeatures = ['manifold', 'fast-csg', 'lazy-union'];
defaultFeatures.forEach(f => {
if (initialState.params.features.indexOf(f) < 0)
initialState.params.features.push(f);
});
const root = ReactDOM.createRoot(
document.getElementById('root') as HTMLElement
);
root.render(
<React.StrictMode>
2023-03-26 07:48:04 +01:00
<App initialState={initialState} fs={fs} />
</React.StrictMode>
);
})();