use global state

This commit is contained in:
Joscha
2025-01-06 18:51:47 +01:00
parent 0251d7feba
commit 16b5790184
3 changed files with 51 additions and 18 deletions
+11 -1
View File
@@ -129,6 +129,7 @@ type AppState = {
export: boolean export: boolean
debug: boolean debug: boolean
} }
fileHandle: FileSystemFileHandle | null
} }
export default class App extends React.Component<any, AppState> { export default class App extends React.Component<any, AppState> {
@@ -284,6 +285,7 @@ export default class App extends React.Component<any, AppState> {
openlayersDebugOptions: { openlayersDebugOptions: {
debugToolbox: false, debugToolbox: false,
}, },
fileHandle: null,
} }
this.layerWatcher = new LayerWatcher({ this.layerWatcher = new LayerWatcher({
@@ -611,7 +613,8 @@ export default class App extends React.Component<any, AppState> {
} }
} }
openStyle = (styleObj: StyleSpecification & {id: string}) => { openStyle = (styleObj: StyleSpecification & {id: string}, fileHandle: FileSystemFileHandle | null) => {
this.setState({fileHandle: fileHandle});
styleObj = this.setDefaultValues(styleObj) styleObj = this.setDefaultValues(styleObj)
this.onStyleChanged(styleObj) this.onStyleChanged(styleObj)
} }
@@ -847,6 +850,10 @@ export default class App extends React.Component<any, AppState> {
this.setModal(modalName, !this.state.isOpen[modalName]); this.setModal(modalName, !this.state.isOpen[modalName]);
} }
onSetFileHandle(fileHandle: FileSystemFileHandle | null) {
this.setState({fileHandle: fileHandle});
}
onChangeOpenlayersDebug = (key: keyof AppState["openlayersDebugOptions"], value: boolean) => { onChangeOpenlayersDebug = (key: keyof AppState["openlayersDebugOptions"], value: boolean) => {
this.setState({ this.setState({
openlayersDebugOptions: { openlayersDebugOptions: {
@@ -949,11 +956,14 @@ export default class App extends React.Component<any, AppState> {
onStyleChanged={this.onStyleChanged} onStyleChanged={this.onStyleChanged}
isOpen={this.state.isOpen.export} isOpen={this.state.isOpen.export}
onOpenToggle={this.toggleModal.bind(this, 'export')} onOpenToggle={this.toggleModal.bind(this, 'export')}
fileHandle={this.state.fileHandle}
onSetFileHandle={this.onSetFileHandle}
/> />
<ModalOpen <ModalOpen
isOpen={this.state.isOpen.open} isOpen={this.state.isOpen.open}
onStyleOpen={this.openStyle} onStyleOpen={this.openStyle}
onOpenToggle={this.toggleModal.bind(this, 'open')} onOpenToggle={this.toggleModal.bind(this, 'open')}
fileHandle={this.state.fileHandle}
/> />
<ModalSources <ModalSources
mapStyle={this.state.mapStyle} mapStyle={this.state.mapStyle}
+37 -14
View File
@@ -22,6 +22,8 @@ type ModalExportInternalProps = {
onStyleChanged(...args: unknown[]): unknown onStyleChanged(...args: unknown[]): unknown
isOpen: boolean isOpen: boolean
onOpenToggle(...args: unknown[]): unknown onOpenToggle(...args: unknown[]): unknown
onSetFileHandle(fileHandle: FileSystemFileHandle | null): unknown
fileHandle: FileSystemFileHandle | null
} & WithTranslation; } & WithTranslation;
@@ -82,29 +84,50 @@ class ModalExportInternal extends React.Component<ModalExportInternalProps> {
} }
async saveStyle() { async saveStyle() {
let fileHandle: FileSystemFileHandle;
const tokenStyle = this.tokenizedStyle(); const tokenStyle = this.tokenizedStyle();
if (fileHandle != null) { let fileHandle = this.props.fileHandle;
const writable = await fileHandle.createWritable(); if (fileHandle == null) {
await writable.write(tokenStyle); fileHandle = await this.createFileHandle();
await writable.close(); this.props.onSetFileHandle(fileHandle)
} else { if (fileHandle == null) return;
await this.saveStyleAs();
} }
const writable = await fileHandle.createWritable();
await writable.write(tokenStyle);
await writable.close();
this.props.onOpenToggle();
} }
async saveStyleAs() { async saveStyleAs() {
const tokenStyle = this.tokenizedStyle(); const tokenStyle = this.tokenizedStyle();
const root = await navigator.storage.getDirectory(); const fileHandle = await this.createFileHandle();
const draftHandle = await root.getFileHandle(this.exportName(), { create: true }); this.props.onSetFileHandle(fileHandle)
const writeable = await draftHandle.createWritable(); if (fileHandle == null) return;
await writeable.write(tokenStyle); const writable = await fileHandle.createWritable();
await writeable.close(); await writable.write(tokenStyle);
// TODO close existing fileHandle await writable.close();
// TODO this.props.fileHandle = accessHandle; this.props.onOpenToggle();
}
async createFileHandle() : Promise<FileSystemFileHandle | null> {
const pickerOpts = {
types: [
{
description: "Style JSON",
accept: {"application/json": [".json"]},
suggestedName: this.exportName(),
},
],
excludeAcceptAllOption: true,
multiple: false,
};
const fileHandle = await window.showSaveFilePicker(pickerOpts) as FileSystemFileHandle;
this.props.onSetFileHandle(fileHandle)
return fileHandle;
} }
changeMetadataProperty(property: string, value: any) { changeMetadataProperty(property: string, value: any) {
+3 -3
View File
@@ -1,7 +1,6 @@
import React, { FormEvent } from 'react' import React, { FormEvent } from 'react'
import {MdFileUpload} from 'react-icons/md' import {MdFileUpload} from 'react-icons/md'
import {MdAddCircleOutline} from 'react-icons/md' import {MdAddCircleOutline} from 'react-icons/md'
import FileReaderInput, { Result } from 'react-file-reader-input'
import { Trans, WithTranslation, withTranslation } from 'react-i18next'; import { Trans, WithTranslation, withTranslation } from 'react-i18next';
import ModalLoading from './ModalLoading' import ModalLoading from './ModalLoading'
@@ -47,6 +46,7 @@ type ModalOpenInternalProps = {
isOpen: boolean isOpen: boolean
onOpenToggle(...args: unknown[]): unknown onOpenToggle(...args: unknown[]): unknown
onStyleOpen(...args: unknown[]): unknown onStyleOpen(...args: unknown[]): unknown
fileHandle: FileSystemFileHandle | null
} & WithTranslation; } & WithTranslation;
type ModalOpenState = { type ModalOpenState = {
@@ -137,7 +137,6 @@ class ModalOpenInternal extends React.Component<ModalOpenInternalProps, ModalOpe
onOpenFile = async () => { onOpenFile = async () => {
this.clearError(); this.clearError();
// TODO close existing file handles
const pickerOpts = { const pickerOpts = {
types: [ types: [
@@ -164,7 +163,8 @@ class ModalOpenInternal extends React.Component<ModalOpenInternalProps, ModalOpe
return; return;
} }
mapStyle = style.ensureStyleValidity(mapStyle) mapStyle = style.ensureStyleValidity(mapStyle)
this.props.onStyleOpen(mapStyle);
this.props.onStyleOpen(mapStyle, fileHandle);
this.onOpenToggle(); this.onOpenToggle();
return file; return file;
} }