import * as React from "react"; import * as d from "../../localData"; import { CommonValue, buttonValue, listValue, oneLineTextValue, } from "../editor/common"; import { createRandomId, listDeleteAt, listUpdateAt } from "../../common/util"; import { Button } from "./Button"; import { Editor } from "./Editor"; import { ListItem } from "../editor/list"; export const LocalProjectPage = (): React.ReactElement => { const fileInputElement = React.useRef(null); const [isLoaded, setIsLoaded] = React.useState(false); const onInputFile = () => { if (fileInputElement.current === null) { console.log("null のままだ!"); return; } const file = fileInputElement.current.files?.item(0); if (file === null || file === undefined) { console.log("ファイルを読み取れなかった!"); return; } console.log("ファイルを受け取れた!", file.name); setIsLoaded(true); }; const onClickStartFromEmpty = () => { setIsLoaded(true); }; if (isLoaded) { return ; } return (
ファイルから始めるか, 空のプロジェクトから始めるか
); }; type LocalProjectPageEditState = { readonly typePartList: ReadonlyArray; readonly partList: ReadonlyArray; }; type LocalTypePart = { readonly id: d.TypePartId; readonly name: string; }; type LocalPart = { readonly id: d.PartId; readonly name: string; }; type LocalLocation = | { readonly tag: "project"; } | { readonly tag: "typePart"; readonly typePartId: d.TypePartId; }; const LoadedLocalProjectPage = (): React.ReactElement => { const [state, setState] = React.useState({ partList: [], typePartList: [], }); const [localLocation, setLocalLocation] = React.useState({ tag: "project", }); const setTypePartList = ( typePartList: ReadonlyArray ): void => { setState((beforeState) => ({ ...beforeState, typePartList })); }; const setPartList = (partList: ReadonlyArray): void => { setState((beforeState) => ({ ...beforeState, partList })); }; if (localLocation.tag === "project") { return ( ); } return ( ); }; const ProjectPage = (props: { readonly state: LocalProjectPageEditState; readonly setTypePartList: (list: ReadonlyArray) => void; readonly setPartList: (list: ReadonlyArray) => void; readonly setLocalLocation: (l: LocalLocation) => void; }): React.ReactElement => { return ( ); }; const TypePartPage = (props: { readonly state: LocalProjectPageEditState; readonly setTypePartList: (list: ReadonlyArray) => void; readonly setPartList: (list: ReadonlyArray) => void; readonly typePartId: d.TypePartId; }): React.ReactElement => { const typePart = props.state.typePartList.find( (e) => e.id === props.typePartId ); if (typePart === undefined) { return
存在しない型パーツ
; } return ( ); }; const typePartListEditor = ( typePartList: ReadonlyArray, setTypePartList: (list: ReadonlyArray) => void, setLocalLocation: (l: LocalLocation) => void ): CommonValue => { return listValue({ items: typePartList.map((typePart, index) => ({ searchText: typePart.name, commonValue: buttonValue({ text: typePart.name, onClick: () => setLocalLocation({ tag: "typePart", typePartId: typePart.id }), }), })), deleteAt: (index) => { setTypePartList(listDeleteAt(typePartList, index)); }, deleteAll: () => { setTypePartList([]); }, addInLast: () => { setTypePartList([ ...typePartList, { id: createRandomTypePartId(), name: "name" }, ]); }, }); }; const partListEditor = ( partList: ReadonlyArray, setPartList: (list: ReadonlyArray) => void ): CommonValue => { return listValue({ items: partList.map((part, index) => ({ searchText: part.name, commonValue: oneLineTextValue({ text: part.name, onChange: undefined, }), })), deleteAt: (index) => { setPartList(listDeleteAt(partList, index)); }, deleteAll: () => { setPartList([]); }, addInLast: () => { setPartList([...partList, { id: createRandomPartId(), name: "name" }]); }, }); }; const createRandomTypePartId = (): d.TypePartId => { return d.TypePartId.fromString(createRandomId()); }; const createRandomPartId = (): d.PartId => { return d.PartId.fromString(createRandomId()); };