diff --git a/src/components/editor/canvas.tsx b/src/components/editor/canvas.tsx index d88bb5c..32ff5b3 100644 --- a/src/components/editor/canvas.tsx +++ b/src/components/editor/canvas.tsx @@ -230,7 +230,7 @@ } } - const selectComopnent = (e: React.MouseEvent, index: number) => { + const selectComponent = (e: React.MouseEvent, index: number) => { e.stopPropagation(); curScreenContext.curScreenActions.selectComponent(index); } @@ -260,7 +260,7 @@
{ selectComopnent(e, index) }} + onClick={(e) => { selectComponent(e, index) }} draggable onDragStart={(e) => { if (curScreenStates.selectingComponentIndex === index) onDragStart(e) }} onDrag={(e) => { if (curScreenStates.selectingComponentIndex === index) draggingComponent(e) }} diff --git a/src/components/header.tsx b/src/components/header.tsx index 82f7f88..7dbc242 100644 --- a/src/components/header.tsx +++ b/src/components/header.tsx @@ -3,6 +3,7 @@ import { useContext, useRef, useState } from 'react'; import { FaFileExport } from 'react-icons/fa'; import { ProjectContext } from '../hooks/useProject'; +import { RDLExport } from '../lib/RDLExport'; export default function Header() { @@ -23,6 +24,11 @@ setExportTypeOpen(false); } + const exportRDL = async() => { + RDLExport(project); + setExportTypeOpen(false); + } + const importJson = () => { jsonInputRef.current?.click(); } @@ -104,6 +110,12 @@ > DTRAM modelファイル */} +
  • + RDLテキストファイル +
  • } diff --git a/src/components/sidebar/screenList.tsx b/src/components/sidebar/screenList.tsx index a4384f8..abbc2b7 100644 --- a/src/components/sidebar/screenList.tsx +++ b/src/components/sidebar/screenList.tsx @@ -378,6 +378,8 @@ const TEXT_HEIGHT = 25; const INPUT_WIDTH = 650; const INPUT_HEIGHT = 40; + const VISIBLE = true; + const ENABLE = true; const screenId = projectActions.addScreen(screenName); curScreenActions.selectComponent(-1); @@ -403,7 +405,9 @@ posX: MARGIN_X * (i % 2 + 1) + INPUT_WIDTH * (i % 2), posY: COLUMN_MARGIN_Y * (Math.floor(i / 2) + 1) + (TEXT_HEIGHT + INPUT_HEIGHT + LABEL_MARGIN_Y) * (Math.floor(i / 2)), width: TEXT_WIDTH, - height: TEXT_HEIGHT + height: TEXT_HEIGHT, + visible: VISIBLE, + enable: ENABLE, }, generalData: { text: `${columnName}` @@ -420,7 +424,9 @@ posX: MARGIN_X * (i % 2 + 1) + INPUT_WIDTH * (i % 2), posY: (COLUMN_MARGIN_Y + LABEL_MARGIN_Y) * (Math.floor(i / 2) + 1) + (TEXT_HEIGHT + INPUT_HEIGHT) * (Math.floor(i / 2)), width: INPUT_WIDTH, - height: INPUT_HEIGHT + height: INPUT_HEIGHT, + visible: VISIBLE, + enable: ENABLE, }, generalData: { text: "" @@ -441,7 +447,9 @@ posX: 1200, posY: 750, width: 150, - height: 40 + height: 40, + visible: true, + enable: true }, generalData: { text: "登録" @@ -467,7 +475,9 @@ posX: 200, posY: 750, width: 150, - height: 40 + height: 40, + visible: true, + enable: true }, generalData: { text: "戻る" diff --git a/src/export/jsonConversion.ts b/src/export/jsonConversion.ts new file mode 100644 index 0000000..93cb156 --- /dev/null +++ b/src/export/jsonConversion.ts @@ -0,0 +1,65 @@ +import type { Component } from "../types/component"; +import type {Screen} from "../types/screen"; +import type {Table} from "../types/table"; +import type { Project } from "../types/project"; +import type { WidgetJson, TableJson, OutputJson } from "./jsonTypes"; + +const byIndex = (a: T, b: T) => a.index - b.index; + +// name をキーにしたオブジェクトに変換(重複していたら例外) +const toRecord = ( + items: T[], + getKey: (item: T) => string, + getValue: (item: T) => V, + label: string, +): Record => { + const result: Record = {}; + for (const item of items) { + const key = getKey(item); + if (key in result) throw new Error(`${label} の名前が重複しています: ${key}`); + result[key] = getValue(item); + } + return result; +}; + +const toWidgetJson = (c: Component): WidgetJson => { + const base = { + wType: c.type, + wX: Math.round(c.uiData.posX), + wY: Math.round(c.uiData.posY), + wWidth: Math.round(c.uiData.width), + wHeight: Math.round(c.uiData.height), + wEnabled: c.uiData.enable, + wVisible: c.uiData.visible + }; + switch(c.type) { + case "button": return {...base, wText: c.generalData.text}; + case "text": return {...base, wText: c.generalData.text}; + case "textField": return {...base, wText: c.generalData.text}; + case "table": return {...base}; + } +}; + +const toScreenJson = (s: Screen) => + toRecord( + [...s.components].sort(byIndex), + (c) => c.name, + toWidgetJson, + `画面「${s.name}」のコンポーネント`, + ); + +const toTableJson = (t: Table): TableJson => { + const keyColumn = t.columns.find((col) => col.isPrimary); + if (!keyColumn) { + throw new Error(`テーブル[${t.name}]に主キーがありません`) + } + return { + keyName: keyColumn.name, + columnName: t.columns.map((col) => col.name) + }; +}; + +export const toOutputJson = (p: Project): OutputJson => ({ + screen: toRecord([...p.screens].sort(byIndex), (s) => s.name, toScreenJson, "画面"), + table: toRecord(p.tables, (t) => t.name, toTableJson, "テーブル"), +}); \ No newline at end of file diff --git a/src/export/jsonTypes.ts b/src/export/jsonTypes.ts new file mode 100644 index 0000000..d72f116 --- /dev/null +++ b/src/export/jsonTypes.ts @@ -0,0 +1,22 @@ + +export type WidgetJson = { + wType: string; + wText?: string; + wX: number; + wY: number; + wWidth: number; + wHeight: number; + wEnabled?: boolean; + wVisible?: boolean; + wColor?: string; +} + +export type TableJson = { + keyName: string; + columnName: string[]; +}; + +export type OutputJson = { + screen: Record>; + table: Record; +}; \ No newline at end of file diff --git a/src/export/rdlConversion.ts b/src/export/rdlConversion.ts new file mode 100644 index 0000000..12a09dc --- /dev/null +++ b/src/export/rdlConversion.ts @@ -0,0 +1,62 @@ +import type { ButtonComponent, Component } from "../types/component"; +import type { Project } from "../types/project"; +import type { EquationSides, ActionElementType } from "./rdlTypes"; + +// コンポーネントがButtonかどうかの判別メゾット(filter用) +const isButton = (c: Component): c is ButtonComponent => c.type === 'button' + +// React用のコンポーネントデータをRDL出力用に変換するメゾット +const toRecord = (b: ButtonComponent): ActionElementType | undefined => { + const curBase = { curWId: b.name, curScreenId: b.screenId}; + + switch (b.action?.type) { + case "navigate": + return {...curBase, actionType: "navigate", nextScreenId: b.action.action.nextScreenName}; + case "addData": + return {...curBase, actionType: "addData", addData: b.action.action.newData, addTableId: b.action.action.tableName, successScreenId: b.action.action.successScreenName, failedScreenId: b.action.action.failScreenName}; + case "updateData": + return {...curBase, actionType: "updateData", updateData: b.action.action.newData, updateTableId: b.action.action.tableName, successScreenId: b.action.action.successScreenName, failedScreenId: b.action.action.failScreenName}; + case "deleteData": + return {...curBase, actionType: "deleteData", deleteData: b.action.action.deleteData, deleteTableId: b.action.action.tableName, successScreenId: b.action.action.successScreenName, failedScreenId: b.action.action.failScreenName} + case "searchData": + return {...curBase, actionType: "searchData", searchTableId: b.action.action.tableName, searchData: b.action.action.searchData, targetWId: b.action.action.targetComponentName + } + default: + return undefined; + } +} + +// Reactコンポーネントをfilterにかけ、RDL出力用に変換 +export const collectRecords = (components:Component[]): ActionElementType[] => + components.filter(isButton).map(toRecord).filter((r): r is ActionElementType => r !== undefined); + +// RDL出力用に変換されたオブジェクトをRDL式(左辺, 右辺)に変換するメソッド +const formatRecordToTerm = (r: ActionElementType): EquationSides => { + switch (r.actionType) { + case "navigate": + return {leftSide: `"${r.nextScreenId}"`, rightSide: `[curScId' : [[wState : wId' -> "${r.curWId}"] : scId' -> "${r.curScreenId}"] -> "clicked"]`} + case "addData": + return {leftSide: "", rightSide: ""} + case "updateData": + return {leftSide: "", rightSide: ""} + case "deleteData": + return {leftSide: "", rightSide: ""} + case "searchData": + return {leftSide: "", rightSide: ""} + default: { + const _exhaustive: never = r; + return _exhaustive; + } + } +} + +export const buildRDLText = (records: ActionElementType[]): string => records.map(formatRecordToTerm) + .filter(({ leftSide, rightSide }) => leftSide !== "" || rightSide !== "") + .map(({ leftSide, rightSide }) => `${leftSide} = ${rightSide}`) + .join("\n"); + +export const toOutputRDL = (p: Project): string => { + const records = p.screens.map((s) => collectRecords(s.components)).flat(); + + return buildRDLText(records) +} \ No newline at end of file diff --git a/src/export/rdlTypes.ts b/src/export/rdlTypes.ts new file mode 100644 index 0000000..784b818 --- /dev/null +++ b/src/export/rdlTypes.ts @@ -0,0 +1,54 @@ + + +export type ActionElementType = NavigateElement | addDataElement | upDateDataElement | deleteDataElement | searchDataElement + +export interface CurWidgetData { + curWId: string, + curScreenId: string, +} + +export interface NavigateElement extends CurWidgetData { + actionType: "navigate", + nextScreenId: string +} + +// Data追加の型、再構築必要 +export interface addDataElement extends CurWidgetData { + actionType: "addData", + addData: { [k: string]: string; }, + addTableId: string, + successScreenId: string, + failedScreenId: string +} + +export interface upDateDataElement extends CurWidgetData { + actionType: "updateData", + updateTableId: string, + updateData: { [k: string]: string; }, + successScreenId: string, + failedScreenId: string +} + +export interface deleteDataElement extends CurWidgetData { + actionType: "deleteData", + deleteTableId: string, + deleteData: { [key: string]: string } + successScreenId: string, + failedScreenId: string +} + +export interface searchDataElement extends CurWidgetData { + actionType: "searchData", + searchTableId: string, + searchData: { [k: string]: string }, + targetWId: string, +} + + + + + +export type EquationSides = { + rightSide: string, + leftSide: string +} \ No newline at end of file diff --git a/src/hooks/useDragComponent.ts b/src/hooks/useDragComponent.ts index 3af6f9d..49831ad 100644 --- a/src/hooks/useDragComponent.ts +++ b/src/hooks/useDragComponent.ts @@ -12,10 +12,14 @@ const [componentY, setComponentY] = useState(0); const [componentW, setComponentW] = useState(0); const [componentH, setComponentH] = useState(0); + const [componentV, setComponentV] = useState(true); + const [componentE, setComponentE] = useState(true); const baseX = selectingComponent ? selectingComponent.uiData.posX : 0; const baseY = selectingComponent ? selectingComponent.uiData.posY : 0; const baseW = selectingComponent ? selectingComponent.uiData.width : 0; const baseH = selectingComponent ? selectingComponent.uiData.height : 0; + const baseV = selectingComponent ? selectingComponent.uiData.visible : true; + const baseE = selectingComponent ? selectingComponent.uiData.enable : true; const [prevX, setPrevX] = useState(0); const [prevY, setPrevY] = useState(0); @@ -43,6 +47,16 @@ setComponentH(baseH); }, [baseH]) + useEffect(() => { + // eslint-disable-next-line react-hooks/set-state-in-effect + setComponentV(baseV); + }, [baseV]) + + useEffect(() => { + // eslint-disable-next-line react-hooks/set-state-in-effect + setComponentE(baseE); + }, [baseE]) + const onDragStart = (e: React.DragEvent) => { e.stopPropagation(); setPrevX(e.clientX); @@ -114,7 +128,9 @@ posX: componentX, posY: componentY, width: componentW, - height: componentH + height: componentH, + visible: componentV, + enable: componentE } moveComponent(selectingComponent.screenId, selectingComponent.id, nextUiData); } diff --git a/src/hooks/useProject.ts b/src/hooks/useProject.ts index 6adf91d..97df079 100644 --- a/src/hooks/useProject.ts +++ b/src/hooks/useProject.ts @@ -52,7 +52,7 @@ updatedTime: "", screens: [ { - id: "screen1", + id: "screen0", components: [], index: 0, name: "screen1", @@ -102,7 +102,9 @@ posX: 100, posY: 100, width: 75, - height: 25 + height: 25, + visible: true, + enable: true }; const newId = screen.id + "Widget" + nextIndex; let newComponent: Component; diff --git a/src/lib/RDLExport.ts b/src/lib/RDLExport.ts new file mode 100644 index 0000000..f439c21 --- /dev/null +++ b/src/lib/RDLExport.ts @@ -0,0 +1,14 @@ +import type { Project } from "../types/project"; +import { toOutputRDL } from "../export/rdlConversion"; + +// UIに反映するためにメソッドをかく +export function RDLExport(project: Project) { + const text = toOutputRDL(project) + const blob = new Blob([text], { type: "text/plain;charset=utf-8" }) + const url = URL.createObjectURL(blob); + const a = document.createElement("a"); + a.href = url; + a.download = `${project.title}_RDL.txt` + a.click(); + URL.revokeObjectURL(url); +} \ No newline at end of file diff --git a/src/lib/jsonExport.ts b/src/lib/jsonExport.ts index 7507611..e946cd3 100644 --- a/src/lib/jsonExport.ts +++ b/src/lib/jsonExport.ts @@ -1,7 +1,9 @@ import type { Project } from "../types/project"; +import { toOutputJson } from "../export/jsonConversion"; export function jsonExport(project: Project) { - const text = JSON.stringify(project, null, 2); + // const text = JSON.stringify(project, null, 2); + const text = JSON.stringify(toOutputJson(project), null, 2) const blob = new Blob([text], { type: "text/plain;charset=utf-8" }) const url = URL.createObjectURL(blob); const a = document.createElement("a"); diff --git a/src/types/component.ts b/src/types/component.ts index 302f404..5acd399 100644 --- a/src/types/component.ts +++ b/src/types/component.ts @@ -13,7 +13,9 @@ posX: number, posY: number, width: number, - height: number + height: number, + visible: boolean, + enable: boolean } export interface TextGeneralData { @@ -39,7 +41,7 @@ export type Component = ButtonComponent | TextComponent | TextFieldComponent | TableComponent export interface ButtonComponent { - id: string + id: string, screenId: string, name: string, type: "button",