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 22719de..abbc2b7 100644 --- a/src/components/sidebar/screenList.tsx +++ b/src/components/sidebar/screenList.tsx @@ -381,8 +381,6 @@ const VISIBLE = true; const ENABLE = true; - const DEFAULT_COLOR = "black"; - const screenId = projectActions.addScreen(screenName); curScreenActions.selectComponent(-1); const table = tables.find((table) => table.id === selectedTableId); @@ -411,7 +409,6 @@ visible: VISIBLE, enable: ENABLE, }, - color: DEFAULT_COLOR, generalData: { text: `${columnName}` }, @@ -454,7 +451,6 @@ visible: true, enable: true }, - color: DEFAULT_COLOR, generalData: { text: "登録" }, @@ -483,7 +479,6 @@ visible: true, enable: true }, - color: DEFAULT_COLOR, generalData: { text: "戻る" }, diff --git a/src/export/conversion.ts b/src/export/conversion.ts deleted file mode 100644 index 7a6ec56..0000000 --- a/src/export/conversion.ts +++ /dev/null @@ -1,65 +0,0 @@ -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/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/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/useProject.ts b/src/hooks/useProject.ts index 82f5568..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", @@ -115,7 +115,6 @@ name: newId, type: type, index: nextIndex, - color: `blue`, screenId: screen.id, generalData: getInitialGeneralData(type) as ButtonGeneralData, style: {}, @@ -128,7 +127,6 @@ name: newId, type: type, index: nextIndex, - color: `black`, screenId: screen.id, generalData: getInitialGeneralData(type) as TextGeneralData, style: {}, 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 c3750a4..e946cd3 100644 --- a/src/lib/jsonExport.ts +++ b/src/lib/jsonExport.ts @@ -1,5 +1,5 @@ import type { Project } from "../types/project"; -import {toOutputJson} from "../export/conversion"; +import { toOutputJson } from "../export/jsonConversion"; export function jsonExport(project: Project) { // const text = JSON.stringify(project, null, 2); diff --git a/src/types/component.ts b/src/types/component.ts index 4bf5b3f..5acd399 100644 --- a/src/types/component.ts +++ b/src/types/component.ts @@ -41,13 +41,12 @@ export type Component = ButtonComponent | TextComponent | TextFieldComponent | TableComponent export interface ButtonComponent { - id: string + id: string, screenId: string, name: string, type: "button", uiData: UIData, index: number, - color: string, generalData: ButtonGeneralData, style: { [k: string]: string }, action?: ButtonAction @@ -60,7 +59,6 @@ type: "text", uiData: UIData, index: number, - color: string, generalData: TextGeneralData, style: { [k: string]: string }, }