import React from "react"; import { Modal } from "@gland/react/modal"; import { FabricButton } from "@gland/react/button"; import { TextInput } from "@gland/react/input"; import { ChoiceGroup } from "@gland/react/radio"; import { ToolAttr } from "../utils"; import { toolClasses as classes } from "../style"; function reducer(state, action) { let newState = state; let type = action?.type; let payload = action?.payload; if (type === "OpenModal") { let catalog = payload.data?.catalog || lastCatalog; return Object.assign({}, state, { open: true, loading: false, key: payload.key, data: { mediaId: payload.data?.mediaId || "", URL: payload.data?.URL || "", isRemote: payload.data?.isRemote || false, catalog, title: payload.data?.title || "", align: payload.data?.align || "center", }, filename: "", href: payload.key && payload.data?.catalog === "0" ? payload.data?.URL || "" : "", }); } else if (type === "StateChange") { return Object.assign({}, state, payload); } else if (type == "CloseModal") { return Object.assign({}, state, { open: false, loading: false, }); } return newState; } function initer(props) { return { open: false, loading: false, key: "", data: { catalog: "0", mediaId: "", URL: "", isRemote: false, title: "", align: "center", }, filename: "", href: "", }; } let lastCatalog = "0"; export function AudioEditModal(props: { attr: ToolAttr }) { const [state, dispatch] = React.useReducer(reducer, props, initer); const attr = props.attr; const lang = attr.lang; React.useEffect(() => { const attr = props.attr; function OpenEdit(options) { dispatch({ type: "OpenModal", payload: options, }); } attr.event.on(attr.editEvent.openEditAudio, OpenEdit); return () => { attr.event.off(attr.editEvent.openEditAudio, OpenEdit); }; }, []); const modalAttr = React.useRef({} as { input }).current; return ( { if (state.loading) return; dispatch({ type: "CloseModal" }); attr.event.emit(attr.editEvent.focus); }} >
{lang.other.audioFrom}: { dispatch({ type: "StateChange", payload: { data: { ...state.data, catalog: item.key, }, filename: "", }, }); lastCatalog = item.key; }} />
{state.data.catalog === "0" ? ( { dispatch({ type: "StateChange", payload: { href: e.target.value, }, }); }} /> ) : (
{lang.other.audioNew}: {state.filename || lang.base.unselected}
{ modalAttr.input.click(); }} > {lang.base.selectFile} (modalAttr.input = r)} style={{ display: "none" }} type="file" accept="audio/*" onChange={(e) => { let filename = e.target.files[0].name; dispatch({ type: "StateChange", payload: { filename } }); }} />
)}
{ dispatch({ type: "StateChange", payload: { data: { ...state.data, title: e.target.value, }, }, }); }} />
{lang.base.align}: { dispatch({ type: "StateChange", payload: { data: Object.assign({}, state.data, { align: item.key }), }, }); }} />
{ if (state.loading) return; let key = state.key; let data = Object.assign({}, state.data); if (data.catalog === "0") { data.URL = state.href; delete data.mediaId; delete data.isRemote; writeData(); } else { let file = modalAttr.input.files; file = file && file[0]; if (!file) { writeData(); } else { const remote = props.attr.editorCtx.editor?.remote; if (!remote) { throw new Error("No RemoteDataProvider"); } if (!remote.addContentAsset) { throw new Error("No RemoteDataProvider.addContentAsset"); } dispatch({ type: "StateChange", payload: { loading: true, }, }); remote .addContentAsset(file, "Audio") .then((res) => { data.mediaId = res.mediaId; data.URL = res.URL; if (res.isRemote) { data.isRemote = res.isRemote; } writeData(); }) .then((err) => { dispatch({ type: "StateChange", payload: { loading: false, }, }); }); } } function writeData() { dispatch({ type: "CloseModal" }); if (key) { attr.event.emit(attr.editEvent.editAudio, { key, data, }); } else { attr.event.emit(attr.editEvent.addAudio, data); } } }} > {lang.base.confirm} { if (state.loading) return; dispatch({ type: "CloseModal" }); attr.event.emit(attr.editEvent.focus); }} > {lang.base.cancel}
); }