diff --git a/src/components/ModalOpen.tsx b/src/components/ModalOpen.tsx index d2450336..fbe5c5fd 100644 --- a/src/components/ModalOpen.tsx +++ b/src/components/ModalOpen.tsx @@ -135,29 +135,37 @@ class ModalOpenInternal extends React.Component { - const [, file] = files[0]; - const reader = new FileReader(); - + onOpenFile = async () => { this.clearError(); - reader.readAsText(file, "UTF-8"); - reader.onload = e => { - let mapStyle; - try { - mapStyle = JSON.parse(e.target?.result as string) - } - catch(err) { - this.setState({ - error: (err as Error).toString() - }); - return; - } - mapStyle = style.ensureStyleValidity(mapStyle) - this.props.onStyleOpen(mapStyle); - this.onOpenToggle(); + const pickerOpts = { + types: [ + { + description: "Style JSON", + accept: {"application/json": [".json"]}, + }, + ], + excludeAcceptAllOption: true, + multiple: false, + }; + + const [fileHandle] = await window.showOpenFilePicker(pickerOpts); + const file = await fileHandle.getFile(); + const content = await file.text(); + + let mapStyle; + try { + mapStyle = JSON.parse(content) + } catch (err) { + this.setState({ + error: (err as Error).toString() + }); + return; } - reader.onerror = e => console.log(e.target); + mapStyle = style.ensureStyleValidity(mapStyle) + this.props.onStyleOpen(mapStyle); + this.onOpenToggle(); + return file; } onOpenToggle() { @@ -196,7 +204,7 @@ class ModalOpenInternal extends React.Component {errorElement}
-

{t("Upload Style")}

-

{t("Upload a JSON style from your computer.")}

- - {t("Upload")} - +

{t("Open local Style")}

+

{t("Open a local JSON style from your computer.")}

+
+ {t("Open Style")} + +