excalidraw/src/actions/actionExport.tsx

87 lines
2.3 KiB
TypeScript
Raw Normal View History

import React from "react";
import { Action } from "./types";
2020-01-29 02:25:47 +02:00
import { ProjectName } from "../components/ProjectName";
import { saveAsJSON, loadFromJSON } from "../scene";
import { load, save } from "../components/icons";
import { ToolButton } from "../components/ToolButton";
import { t } from "../i18n";
import useIsMobile from "../is-mobile";
export const actionChangeProjectName: Action = {
name: "changeProjectName",
perform: (elements, appState, value) => {
return { appState: { ...appState, name: value } };
},
PanelComponent: ({ appState, updateData }) => (
2020-01-29 02:25:47 +02:00
<ProjectName
label={t("labels.fileTitle")}
value={appState.name || "Unnamed"}
onChange={(name: string) => updateData(name)}
/>
2020-01-24 12:04:54 +02:00
),
};
export const actionChangeExportBackground: Action = {
name: "changeExportBackground",
perform: (elements, appState, value) => {
return { appState: { ...appState, exportBackground: value } };
},
PanelComponent: ({ appState, updateData }) => (
<label>
<input
type="checkbox"
checked={appState.exportBackground}
onChange={e => {
updateData(e.target.checked);
}}
/>{" "}
{t("labels.withBackground")}
</label>
2020-01-24 12:04:54 +02:00
),
};
export const actionSaveScene: Action = {
name: "saveScene",
perform: (elements, appState, value) => {
saveAsJSON(elements, appState).catch(err => console.error(err));
return {};
},
PanelComponent: ({ updateData }) => (
<ToolButton
type="button"
icon={save}
title={t("buttons.save")}
aria-label={t("buttons.save")}
showAriaLabel={useIsMobile()}
onClick={() => updateData(null)}
/>
2020-01-24 12:04:54 +02:00
),
};
export const actionLoadScene: Action = {
name: "loadScene",
perform: (
elements,
appState,
2020-01-24 12:04:54 +02:00
{ elements: loadedElements, appState: loadedAppState },
) => {
return { elements: loadedElements, appState: loadedAppState };
},
PanelComponent: ({ updateData }) => (
<ToolButton
type="button"
icon={load}
title={t("buttons.load")}
aria-label={t("buttons.load")}
showAriaLabel={useIsMobile()}
onClick={() => {
loadFromJSON()
.then(({ elements, appState }) => {
updateData({ elements: elements, appState: appState });
})
.catch(err => console.error(err));
}}
/>
2020-01-24 12:04:54 +02:00
),
};