From 77bdb77a9ed1f74197416547ef15bb56776b00d3 Mon Sep 17 00:00:00 2001 From: 1ilit Date: Fri, 7 Jun 2024 14:18:05 +0300 Subject: [PATCH] Enable editing index names --- .../TablesTab/IndexDetails.jsx | 48 +++++++++++++++++-- .../EditorSidePanel/TablesTab/TableField.jsx | 2 +- .../EditorSidePanel/TablesTab/TableInfo.jsx | 4 +- src/i18n/locales/en.js | 1 + src/utils/issues.js | 7 +++ 5 files changed, 55 insertions(+), 7 deletions(-) diff --git a/src/components/EditorSidePanel/TablesTab/IndexDetails.jsx b/src/components/EditorSidePanel/TablesTab/IndexDetails.jsx index c990d63..1af09c2 100644 --- a/src/components/EditorSidePanel/TablesTab/IndexDetails.jsx +++ b/src/components/EditorSidePanel/TablesTab/IndexDetails.jsx @@ -3,11 +3,13 @@ import { Input, Button, Popover, Checkbox, Select } from "@douyinfe/semi-ui"; import { IconMore, IconDeleteStroked } from "@douyinfe/semi-icons"; import { useTables, useUndoRedo } from "../../../hooks"; import { useTranslation } from "react-i18next"; +import { useState } from "react"; export default function IndexDetails({ data, fields, iid, tid }) { const { t } = useTranslation(); const { tables, updateTable } = useTables(); const { setUndoStack, setRedoStack } = useUndoRedo(); + const [editField, setEditField] = useState({}); return (
@@ -29,11 +31,9 @@ export default function IndexDetails({ data, fields, iid, tid }) { iid: iid, undo: { fields: [...data.fields], - name: `${data.fields.join("_")}_index`, }, redo: { fields: [...value], - name: `${value.join("_")}_index`, }, message: t("edit_table", { tableName: tables[tid].name, @@ -48,7 +48,6 @@ export default function IndexDetails({ data, fields, iid, tid }) { ? { ...index, fields: [...value], - name: `${value.join("_")}_index`, } : index, ), @@ -59,7 +58,48 @@ export default function IndexDetails({ data, fields, iid, tid }) { content={
{t("name")}:
- + + setEditField({ + name: data.name, + }) + } + onChange={(value) => + updateTable(tid, { + indices: tables[tid].indices.map((index) => + index.id === iid + ? { + ...index, + name: value, + } + : index, + ), + }) + } + onBlur={(e) => { + if (e.target.value === editField.name) return; + setUndoStack((prev) => [ + ...prev, + { + action: Action.EDIT, + element: ObjectType.TABLE, + component: "index", + tid: tid, + iid: iid, + undo: editField, + redo: { name: e.target.value }, + message: t("edit_table", { + tableName: tables[tid].name, + extra: "[index]", + }), + }, + ]); + setRedoStack([]); + }} + />
{t("unique")}
updateField(tid, index, { name: value })} onFocus={(e) => setEditField({ name: e.target.value })} diff --git a/src/components/EditorSidePanel/TablesTab/TableInfo.jsx b/src/components/EditorSidePanel/TablesTab/TableInfo.jsx index 63bf392..b5bd39c 100644 --- a/src/components/EditorSidePanel/TablesTab/TableInfo.jsx +++ b/src/components/EditorSidePanel/TablesTab/TableInfo.jsx @@ -33,7 +33,7 @@ export default function TableInfo({ data }) {
{t("name")}:
updateTable(data.id, { name: value })} @@ -290,7 +290,7 @@ export default function TableInfo({ data }) { ...data.indices, { id: data.indices.length, - name: `index_${data.indices.length}`, + name: `${data.name}_index_${data.indices.length}`, unique: false, fields: [], }, diff --git a/src/i18n/locales/en.js b/src/i18n/locales/en.js index 32c7ef7..1359a98 100644 --- a/src/i18n/locales/en.js +++ b/src/i18n/locales/en.js @@ -212,6 +212,7 @@ const en = { edit_relationship: "{{extra}} Edit relationship {{refName}}", delete_relationship: "Delete relationship {{refName}}", not_found: "Not found", + empty_index_name: "Declared an index with no name in table '{{tableName}}'", }, }; diff --git a/src/utils/issues.js b/src/utils/issues.js index d7cde0f..986be22 100644 --- a/src/utils/issues.js +++ b/src/utils/issues.js @@ -168,6 +168,13 @@ export function getIssues(diagram) { }); table.indices.forEach((index) => { + if (index.name.trim() === "") { + issues.push( + i18n.t("empty_index_name", { + tableName: table.name, + }), + ); + } if (index.fields.length === 0) { issues.push( i18n.t("empty_index", {