diff --git a/src/components/sidebar/screenList.tsx b/src/components/sidebar/screenList.tsx index a4384f8..22719de 100644 --- a/src/components/sidebar/screenList.tsx +++ b/src/components/sidebar/screenList.tsx @@ -378,6 +378,10 @@ const TEXT_HEIGHT = 25; const INPUT_WIDTH = 650; const INPUT_HEIGHT = 40; + const VISIBLE = true; + const ENABLE = true; + + const DEFAULT_COLOR = "black"; const screenId = projectActions.addScreen(screenName); curScreenActions.selectComponent(-1); @@ -403,8 +407,11 @@ 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, }, + color: DEFAULT_COLOR, generalData: { text: `${columnName}` }, @@ -420,7 +427,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,8 +450,11 @@ posX: 1200, posY: 750, width: 150, - height: 40 + height: 40, + visible: true, + enable: true }, + color: DEFAULT_COLOR, generalData: { text: "登録" }, @@ -467,8 +479,11 @@ posX: 200, posY: 750, width: 150, - height: 40 + height: 40, + visible: true, + enable: true }, + color: DEFAULT_COLOR, generalData: { text: "戻る" }, diff --git a/src/export/conversion.ts b/src/export/conversion.ts new file mode 100644 index 0000000..7a6ec56 --- /dev/null +++ b/src/export/conversion.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, wColor: c.color}; + case "text": return {...base, wText: c.generalData.text, wColor: c.color}; + 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/lib/jsonExport.ts b/src/lib/jsonExport.ts index 7507611..c3750a4 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/conversion"; 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");