Database diagrams editor that allows you to visualize and design your DB with a single query.
TypeScript databasedbdb-migrationeditorerdmariadbmssqlmysqlpostgresqlreactreact-flowschema-migrationssqlsqlitetypescriptvisualizationxyflow
add redo undo
Guy Ben-Aharon7a7d7a92e585
diff --git a/src/context/chartdb-context/chartdb-context.tsx b/src/context/chartdb-context/chartdb-context.tsxindex 3520c0a7..94e78fe8 100644--- a/src/context/chartdb-context/chartdb-context.tsx+++ b/src/context/chartdb-context/chartdb-context.tsx@@ -16,7 +16,10 @@ export interface ChartDBContext {// General operationsupdateDiagramId: (id: string) => Promise<void>;- updateDiagramName: (name: string) => Promise<void>;+ updateDiagramName: (+ name: string,+ options?: { updateHistory: boolean }+ ) => Promise<void>;loadDiagram: (diagramId: string) => Promise<Diagram | undefined>;// Database type operations@@ -24,13 +27,23 @@ export interface ChartDBContext {// Table operationscreateTable: () => Promise<DBTable>;- addTable: (table: DBTable) => Promise<void>;+ addTable: (+ table: DBTable,+ options?: { updateHistory: boolean }+ ) => Promise<void>;getTable: (id: string) => DBTable | null;- removeTable: (id: string) => Promise<void>;- updateTable: (id: string, table: Partial<DBTable>) => Promise<void>;- // updateTables: (tables: PartialExcept<DBTable, 'id'>[]) => Promise<void>;+ removeTable: (+ id: string,+ options?: { updateHistory: boolean }+ ) => Promise<void>;+ updateTable: (+ id: string,+ table: Partial<DBTable>,+ options?: { updateHistory: boolean }+ ) => Promise<void>;updateTablesState: (- updateFn: (tables: DBTable[]) => PartialExcept<DBTable, 'id'>[]+ updateFn: (tables: DBTable[]) => PartialExcept<DBTable, 'id'>[],+ options?: { updateHistory: boolean }) => Promise<void>;// Field operations@@ -38,21 +51,39 @@ export interface ChartDBContext {updateField: (tableId: string,fieldId: string,- field: Partial<DBField>+ field: Partial<DBField>,+ options?: { updateHistory: boolean }+ ) => Promise<void>;+ removeField: (+ tableId: string,+ fieldId: string,+ options?: { updateHistory: boolean }) => Promise<void>;- removeField: (tableId: string, fieldId: string) => Promise<void>;createField: (tableId: string) => Promise<DBField>;- addField: (tableId: string, field: DBField) => Promise<void>;+ addField: (+ tableId: string,+ field: DBField,+ options?: { updateHistory: boolean }+ ) => Promise<void>;// Index operationscreateIndex: (tableId: string) => Promise<DBIndex>;- addIndex: (tableId: string, index: DBIndex) => Promise<void>;+ addIndex: (+ tableId: string,+ index: DBIndex,+ options?: { updateHistory: boolean }+ ) => Promise<void>;getIndex: (tableId: string, indexId: string) => DBIndex | null;- removeIndex: (tableId: string, indexId: string) => Promise<void>;+ removeIndex: (+ tableId: string,+ indexId: string,+ options?: { updateHistory: boolean }+ ) => Promise<void>;updateIndex: (tableId: string,indexId: string,- index: Partial<DBIndex>+ index: Partial<DBIndex>,+ options?: { updateHistory: boolean }) => Promise<void>;// Relationship operations@@ -62,13 +93,27 @@ export interface ChartDBContext {sourceFieldId: string;targetFieldId: string;}) => Promise<DBRelationship>;- addRelationship: (relationship: DBRelationship) => Promise<void>;+ addRelationship: (+ relationship: DBRelationship,+ options?: { updateHistory: boolean }+ ) => Promise<void>;+ addRelationships: (+ relationships: DBRelationship[],+ options?: { updateHistory: boolean }+ ) => Promise<void>;getRelationship: (id: string) => DBRelationship | null;- removeRelationship: (id: string) => Promise<void>;- removeRelationships: (...ids: string[]) => Promise<void>;+ removeRelationship: (+ id: string,+ options?: { updateHistory: boolean }+ ) => Promise<void>;+ removeRelationships: (+ ids: string[],+ options?: { updateHistory: boolean }+ ) => Promise<void>;updateRelationship: (id: string,- relationship: Partial<DBRelationship>+ relationship: Partial<DBRelationship>,+ options?: { updateHistory: boolean }) => Promise<void>;}@@ -117,4 +162,5 @@ export const chartDBContext = createContext<ChartDBContext>({removeRelationship: emptyFn,updateRelationship: emptyFn,removeRelationships: emptyFn,+ addRelationships: emptyFn,});diff --git a/src/context/chartdb-context/chartdb-provider.tsx b/src/context/chartdb-context/chartdb-provider.tsxindex 74b36805..f5ea2692 100644--- a/src/context/chartdb-context/chartdb-provider.tsx+++ b/src/context/chartdb-context/chartdb-provider.tsx@@ -13,14 +13,7 @@ export const ChartDBProvider: React.FC<React.PropsWithChildren> = ({children,}) => {const db = useStorage();- const { setRedoStack, setUndoStack } = useRedoUndoStack();- console.log({ setRedoStack, setUndoStack });- // const [undoStack, setUndoStack] = React.useState<- // {- // action: string;- // data: any;- // }[]- // >([]);+ const { addUndoAction, resetRedoStack } = useRedoUndoStack();const [diagramId, setDiagramId] = React.useState('');const [diagramName, setDiagramName] = React.useState('');const [databaseType, setDatabaseType] = React.useState<DatabaseType>(@@ -54,19 +47,45 @@ export const ChartDBProvider: React.FC<React.PropsWithChildren> = ({);const updateDiagramName: ChartDBContext['updateDiagramName'] = useCallback(- async (name) => {+ async (name, options = { updateHistory: true }) => {+ const prevName = diagramName;setDiagramName(name);await db.updateDiagram({ id: diagramId, attributes: { name } });++ if (options.updateHistory) {+ addUndoAction({+ action: 'updateDiagramName',+ redoData: { name },+ undoData: { name: prevName },+ });+ resetRedoStack();+ }},- [db, diagramId, setDiagramName]+ [+ db,+ diagramId,+ setDiagramName,+ addUndoAction,+ diagramName,+ resetRedoStack,+ ]);const addTable: ChartDBContext['addTable'] = useCallback(- (table: DBTable) => {+ async (table: DBTable, options = { updateHistory: true }) => {setTables((tables) => [...tables, table]);- return db.addTable({ diagramId, table });+ await db.addTable({ diagramId, table });++ if (options.updateHistory) {+ addUndoAction({+ action: 'addTable',+ redoData: { table },+ undoData: { tableId: table.id },+ });+ resetRedoStack();+ }},- [db, diagramId, setTables]+ [db, diagramId, setTables, addUndoAction, resetRedoStack]);const createTable: ChartDBContext['createTable'] = useCallback(async () => {@@ -101,50 +120,53 @@ export const ChartDBProvider: React.FC<React.PropsWithChildren> = ({);const removeTable: ChartDBContext['removeTable'] = useCallback(- async (id: string) => {+ async (id: string, options = { updateHistory: true }) => {+ const table = getTable(id);setTables((tables) => tables.filter((table) => table.id !== id));await db.deleteTable({ diagramId, id });++ if (!!table && options.updateHistory) {+ addUndoAction({+ action: 'removeTable',+ redoData: { tableId: id },+ undoData: { table },+ });+ resetRedoStack();+ }},- [db, diagramId, setTables]+ [db, diagramId, setTables, addUndoAction, resetRedoStack, getTable]);const updateTable: ChartDBContext['updateTable'] = useCallback(- async (id: string, table: Partial<DBTable>) => {+ async (+ id: string,+ table: Partial<DBTable>,+ options = { updateHistory: true }+ ) => {+ const prevTable = getTable(id);setTables((tables) =>tables.map((t) => (t.id === id ? { ...t, ...table } : t)));await db.updateTable({ id, attributes: table });++ if (!!prevTable && options.updateHistory) {+ addUndoAction({+ action: 'updateTable',+ redoData: { tableId: id, table },+ undoData: { tableId: id, table: prevTable },+ });+ resetRedoStack();+ }},- [db, setTables]+ [db, setTables, addUndoAction, resetRedoStack, getTable]);- // const updateTables: ChartDBContext['updateTables'] = useCallback(- // async (tables: PartialExcept<DBTable, 'id'>[]) => {- // setTables((currentTables) =>- // currentTables.map((table) => {- // const updatedTable = tables.find((t) => t.id === table.id);- // return updatedTable ? { ...table, ...updatedTable } : table;- // })- // );-- // const promises = [];- // for (const table of tables) {- // promises.push(- // db.updateTable({ id: table.id, attributes: table })- // );- // }-- // await Promise.all(promises);- // },- // [db, setTables]- // );-const updateTablesState: ChartDBContext['updateTablesState'] = useCallback(async (- updateFn: (tables: DBTable[]) => PartialExcept<DBTable, 'id'>[]+ updateFn: (tables: DBTable[]) => PartialExcept<DBTable, 'id'>[],+ options = { updateHistory: true }) => {- const updatedTables = updateFn(tables);- setTables((prevTables) => {+ const updateTables = (prevTables: DBTable[]) => {const updatedTables = updateFn(prevTables);return prevTables.map((prevTable) => {@@ -158,7 +180,12 @@ export const ChartDBProvider: React.FC<React.PropsWithChildren> = ({.filter((prevTable) =>updatedTables.some((t) => t.id === prevTable.id));- });+ };++ const prevTables = [...tables];+ // const updatedTablesAttrs = updateFn(tables);+ const updatedTables = updateTables(tables);+ setTables(updateTables);const promises = [];for (const updatedTable of updatedTables) {@@ -179,12 +206,35 @@ export const ChartDBProvider: React.FC<React.PropsWithChildren> = ({}await Promise.all(promises);++ if (options.updateHistory) {+ addUndoAction({+ action: 'updateTablesState',+ redoData: { tables: updatedTables },+ undoData: { tables: prevTables },+ });+ resetRedoStack();+ }},- [db, tables, setTables, diagramId]+ [db, tables, setTables, diagramId, addUndoAction, resetRedoStack]+ );++ const getField: ChartDBContext['getField'] = useCallback(+ (tableId: string, fieldId: string) => {+ const table = getTable(tableId);+ return table?.fields.find((f) => f.id === fieldId) ?? null;+ },+ [getTable]);const updateField: ChartDBContext['updateField'] = useCallback(- async (tableId: string, fieldId: string, field: Partial<DBField>) => {+ async (+ tableId: string,+ fieldId: string,+ field: Partial<DBField>,+ options = { updateHistory: true }+ ) => {+ const prevField = getField(tableId, fieldId);setTables((tables) =>tables.map((table) =>table.id === tableId@@ -212,12 +262,30 @@ export const ChartDBProvider: React.FC<React.PropsWithChildren> = ({),},});++ if (!!prevField && options.updateHistory) {+ addUndoAction({+ action: 'updateField',+ redoData: {+ tableId,+ fieldId,+ field: { ...prevField, ...field },+ },+ undoData: { tableId, fieldId, field: prevField },+ });+ resetRedoStack();+ }},- [db, diagramId, setTables]+ [db, diagramId, setTables, addUndoAction, resetRedoStack, getField]);const removeField: ChartDBContext['removeField'] = useCallback(- async (tableId: string, fieldId: string) => {+ async (+ tableId: string,+ fieldId: string,+ options = { updateHistory: true }+ ) => {+ const prevField = getField(tableId, fieldId);setTables((tables) =>tables.map((table) =>table.id === tableId@@ -243,12 +311,25 @@ export const ChartDBProvider: React.FC<React.PropsWithChildren> = ({fields: table.fields.filter((f) => f.id !== fieldId),},});++ if (!!prevField && options.updateHistory) {+ addUndoAction({+ action: 'removeField',+ redoData: { tableId, fieldId },+ undoData: { tableId, field: prevField },+ });+ resetRedoStack();+ }},- [db, diagramId, setTables]+ [db, diagramId, setTables, addUndoAction, resetRedoStack, getField]);const addField: ChartDBContext['addField'] = useCallback(- async (tableId: string, field: DBField) => {+ async (+ tableId: string,+ field: DBField,+ options = { updateHistory: true }+ ) => {setTables((tables) =>tables.map((table) =>table.id === tableId@@ -270,16 +351,17 @@ export const ChartDBProvider: React.FC<React.PropsWithChildren> = ({fields: [...table.fields, field],},});- },- [db, diagramId, setTables]- );- const getField: ChartDBContext['getField'] = useCallback(- (tableId: string, fieldId: string) => {- const table = getTable(tableId);- return table?.fields.find((f) => f.id === fieldId) ?? null;+ if (options.updateHistory) {+ addUndoAction({+ action: 'addField',+ redoData: { tableId, field },+ undoData: { tableId, fieldId: field.id },+ });+ resetRedoStack();+ }},- [getTable]+ [db, diagramId, setTables, addUndoAction, resetRedoStack]);const createField: ChartDBContext['createField'] = useCallback(@@ -302,8 +384,20 @@ export const ChartDBProvider: React.FC<React.PropsWithChildren> = ({[addField, getTable]);+ const getIndex: ChartDBContext['getIndex'] = useCallback(+ (tableId: string, indexId: string) => {+ const table = getTable(tableId);+ return table?.indexes.find((i) => i.id === indexId) ?? null;+ },+ [getTable]+ );+const addIndex: ChartDBContext['addIndex'] = useCallback(- async (tableId: string, index: DBIndex) => {+ async (+ tableId: string,+ index: DBIndex,+ options = { updateHistory: true }+ ) => {setTables((tables) =>tables.map((table) =>table.id === tableId@@ -324,12 +418,26 @@ export const ChartDBProvider: React.FC<React.PropsWithChildren> = ({indexes: [...dbTable.indexes, index],},});++ if (options.updateHistory) {+ addUndoAction({+ action: 'addIndex',+ redoData: { tableId, index },+ undoData: { tableId, indexId: index.id },+ });+ resetRedoStack();+ }},- [db, diagramId, setTables]+ [db, diagramId, setTables, addUndoAction, resetRedoStack]);const removeIndex: ChartDBContext['removeIndex'] = useCallback(- async (tableId: string, indexId: string) => {+ async (+ tableId: string,+ indexId: string,+ options = { updateHistory: true }+ ) => {+ const prevIndex = getIndex(tableId, indexId);setTables((tables) =>tables.map((table) =>table.id === tableId@@ -359,8 +467,17 @@ export const ChartDBProvider: React.FC<React.PropsWithChildren> = ({indexes: dbTable.indexes.filter((i) => i.id !== indexId),},});++ if (!!prevIndex && options.updateHistory) {+ addUndoAction({+ action: 'removeIndex',+ redoData: { indexId, tableId },+ undoData: { tableId, index: prevIndex },+ });+ resetRedoStack();+ }},- [db, diagramId, setTables]+ [db, diagramId, setTables, addUndoAction, resetRedoStack, getIndex]);const createIndex: ChartDBContext['createIndex'] = useCallback(@@ -381,16 +498,14 @@ export const ChartDBProvider: React.FC<React.PropsWithChildren> = ({[addIndex, getTable]);- const getIndex: ChartDBContext['getIndex'] = useCallback(- (tableId: string, indexId: string) => {- const table = getTable(tableId);- return table?.indexes.find((i) => i.id === indexId) ?? null;- },- [getTable]- );-const updateIndex: ChartDBContext['updateIndex'] = useCallback(- async (tableId: string, indexId: string, index: Partial<DBIndex>) => {+ async (+ tableId: string,+ indexId: string,+ index: Partial<DBIndex>,+ options = { updateHistory: true }+ ) => {+ const prevIndex = getIndex(tableId, indexId);setTables((tables) =>tables.map((table) =>table.id === tableId@@ -419,20 +534,71 @@ export const ChartDBProvider: React.FC<React.PropsWithChildren> = ({),},});++ if (!!prevIndex && options.updateHistory) {+ addUndoAction({+ action: 'updateIndex',+ redoData: { tableId, indexId, index },+ undoData: { tableId, indexId, index: prevIndex },+ });+ resetRedoStack();+ }},- [db, diagramId, setTables]+ [db, diagramId, setTables, addUndoAction, resetRedoStack, getIndex]);const addRelationship: ChartDBContext['addRelationship'] = useCallback(- async (relationship: DBRelationship) => {+ async (+ relationship: DBRelationship,+ options = { updateHistory: true }+ ) => {setRelationships((relationships) => [...relationships,relationship,]);await db.addRelationship({ diagramId, relationship });++ if (options.updateHistory) {+ addUndoAction({+ action: 'addRelationship',+ redoData: { relationship },+ undoData: { relationshipId: relationship.id },+ });+ resetRedoStack();+ }+ },+ [db, diagramId, setRelationships, addUndoAction, resetRedoStack]+ );++ const addRelationships: ChartDBContext['addRelationships'] = useCallback(+ async (+ relationships: DBRelationship[],+ options = { updateHistory: true }+ ) => {+ setRelationships((currentRelationships) => [+ ...currentRelationships,+ ...relationships,+ ]);++ await Promise.all(+ relationships.map((relationship) =>+ db.addRelationship({ diagramId, relationship })+ )+ );++ if (options.updateHistory) {+ addUndoAction({+ action: 'addRelationships',+ redoData: { relationships },+ undoData: {+ relationshipIds: relationships.map((r) => r.id),+ },+ });+ resetRedoStack();+ }},- [db, diagramId, setRelationships]+ [db, diagramId, setRelationships, addUndoAction, resetRedoStack]);const createRelationship: ChartDBContext['createRelationship'] =@@ -472,7 +638,8 @@ export const ChartDBProvider: React.FC<React.PropsWithChildren> = ({const removeRelationship: ChartDBContext['removeRelationship'] =useCallback(- async (id: string) => {+ async (id: string, options = { updateHistory: true }) => {+ const relationship = getRelationship(id);setRelationships((relationships) =>relationships.filter((relationship) => relationship.id !== id@@ -480,13 +647,32 @@ export const ChartDBProvider: React.FC<React.PropsWithChildren> = ({);await db.deleteRelationship({ diagramId, id });++ if (!!relationship && options.updateHistory) {+ addUndoAction({+ action: 'removeRelationship',+ redoData: { relationshipId: id },+ undoData: { relationship },+ });+ resetRedoStack();+ }},- [db, diagramId, setRelationships]+ [+ db,+ diagramId,+ setRelationships,+ addUndoAction,+ resetRedoStack,+ getRelationship,+ ]);const removeRelationships: ChartDBContext['removeRelationships'] =useCallback(- async (...ids: string[]) => {+ async (ids: string[], options = { updateHistory: true }) => {+ const prevRelationships = relationships.filter(+ (relationship) => !ids.includes(relationship.id)+ );setRelationships((relationships) =>relationships.filter((relationship) => !ids.includes(relationship.id)@@ -496,13 +682,34 @@ export const ChartDBProvider: React.FC<React.PropsWithChildren> = ({await Promise.all(ids.map((id) => db.deleteRelationship({ diagramId, id })));++ if (options.updateHistory) {+ addUndoAction({+ action: 'removeRelationships',+ redoData: { relationshipsIds: ids },+ undoData: { relationships: prevRelationships },+ });+ resetRedoStack();+ }},- [db, diagramId, setRelationships]+ [+ db,+ diagramId,+ setRelationships,+ relationships,+ addUndoAction,+ resetRedoStack,+ ]);const updateRelationship: ChartDBContext['updateRelationship'] =useCallback(- async (id: string, relationship: Partial<DBRelationship>) => {+ async (+ id: string,+ relationship: Partial<DBRelationship>,+ options = { updateHistory: true }+ ) => {+ const prevRelationship = getRelationship(id);setRelationships((relationships) =>relationships.map((r) =>r.id === id ? { ...r, ...relationship } : r@@ -510,8 +717,26 @@ export const ChartDBProvider: React.FC<React.PropsWithChildren> = ({);await db.updateRelationship({ id, attributes: relationship });++ if (!!prevRelationship && options.updateHistory) {+ addUndoAction({+ action: 'updateRelationship',+ redoData: { relationshipId: id, relationship },+ undoData: {+ relationshipId: id,+ relationship: prevRelationship,+ },+ });+ resetRedoStack();+ }},- [db, setRelationships]+ [+ db,+ setRelationships,+ addUndoAction,+ getRelationship,+ resetRedoStack,+ ]);const loadDiagram: ChartDBContext['loadDiagram'] = useCallback(@@ -571,6 +796,7 @@ export const ChartDBProvider: React.FC<React.PropsWithChildren> = ({getIndex,updateIndex,addRelationship,+ addRelationships,createRelationship,getRelationship,removeRelationship,diff --git a/src/context/history-context/history-context.tsx b/src/context/history-context/history-context.tsxindex 7b93e925..7746e1b7 100644--- a/src/context/history-context/history-context.tsx+++ b/src/context/history-context/history-context.tsx@@ -4,9 +4,13 @@ import { createContext } from 'react';export interface HistoryContext {undo: () => void;redo: () => void;+ hasUndo: boolean;+ hasRedo: boolean;}export const historyContext = createContext<HistoryContext>({undo: emptyFn,redo: emptyFn,+ hasUndo: false,+ hasRedo: false,});diff --git a/src/context/history-context/history-provider.tsx b/src/context/history-context/history-provider.tsxindex 655666bc..e51fc9da 100644--- a/src/context/history-context/history-provider.tsx+++ b/src/context/history-context/history-provider.tsx@@ -1,73 +1,245 @@import React, { useMemo } from 'react';import { historyContext } from './history-context';-import { RedoUndoActionHandlers } from './redo-undo-action';import { useChartDB } from '@/hooks/use-chartdb';import { useRedoUndoStack } from '@/hooks/use-redo-undo-stack';+import { RedoUndoActionHandlers } from './redo-undo-action';export const HistoryProvider: React.FC<React.PropsWithChildren> = ({children,}) => {- const { setRedoStack, setUndoStack, undoStack, redoStack } =- useRedoUndoStack();- console.log({ setRedoStack, setUndoStack, undoStack, redoStack });- const { addTable, removeTable, updateTable } = useChartDB();+ const {+ addRedoAction,+ addUndoAction,+ undoStack,+ redoStack,+ hasRedo,+ hasUndo,+ } = useRedoUndoStack();+ const {+ addTable,+ removeTable,+ updateTable,+ updateDiagramName,+ removeField,+ addField,+ updateField,+ addRelationship,+ addRelationships,+ removeRelationship,+ updateRelationship,+ updateTablesState,+ addIndex,+ removeIndex,+ updateIndex,+ removeRelationships,+ } = useChartDB();const redoActionHandlers = useMemo(- (): Partial<RedoUndoActionHandlers> => ({- addTable: ({ table }) => {- return addTable(table!);+ (): RedoUndoActionHandlers => ({+ updateDiagramName: ({ redoData: { name } }) => {+ return updateDiagramName(name, { updateHistory: false });+ },+ addTable: ({ redoData: { table } }) => {+ return addTable(table, { updateHistory: false });+ },+ removeTable: ({ redoData: { tableId } }) => {+ return removeTable(tableId, { updateHistory: false });+ },+ updateTable: ({ redoData: { tableId, table } }) => {+ return updateTable(tableId, table, { updateHistory: false });+ },+ updateTablesState: ({ redoData: { tables } }) => {+ return updateTablesState(() => tables, {+ updateHistory: false,+ });+ },+ addField: ({ redoData: { tableId, field } }) => {+ return addField(tableId, field, { updateHistory: false });+ },+ removeField: ({ redoData: { tableId, fieldId } }) => {+ return removeField(tableId, fieldId, { updateHistory: false });+ },+ updateField: ({ redoData: { tableId, fieldId, field } }) => {+ return updateField(tableId, fieldId, field, {+ updateHistory: false,+ });+ },+ addRelationship: ({ redoData: { relationship } }) => {+ return addRelationship(relationship, { updateHistory: false });+ },+ addRelationships: ({ redoData: { relationships } }) => {+ return addRelationships(relationships, {+ updateHistory: false,+ });+ },+ removeRelationship: ({ redoData: { relationshipId } }) => {+ return removeRelationship(relationshipId, {+ updateHistory: false,+ });+ },+ updateRelationship: ({+ redoData: { relationshipId, relationship },+ }) => {+ return updateRelationship(relationshipId, relationship, {+ updateHistory: false,+ });+ },+ removeRelationships: ({ redoData: { relationshipsIds } }) => {+ return removeRelationships(relationshipsIds, {+ updateHistory: false,+ });+ },+ addIndex: ({ redoData: { tableId, index } }) => {+ return addIndex(tableId, index, { updateHistory: false });+ },+ removeIndex: ({ redoData: { tableId, indexId } }) => {+ return removeIndex(tableId, indexId, { updateHistory: false });+ },+ updateIndex: ({ redoData: { tableId, indexId, index } }) => {+ return updateIndex(tableId, indexId, index, {+ updateHistory: false,+ });},}),- [addTable]+ [+ addTable,+ removeTable,+ updateTable,+ updateDiagramName,+ removeField,+ addField,+ updateField,+ addRelationship,+ addRelationships,+ removeRelationship,+ updateRelationship,+ updateTablesState,+ addIndex,+ removeIndex,+ updateIndex,+ removeRelationships,+ ]);const undoActionHandlers = useMemo(- (): Partial<RedoUndoActionHandlers> => ({- addTable: ({ tableId }) => {- return removeTable(tableId!);+ (): RedoUndoActionHandlers => ({+ updateDiagramName: ({ undoData: { name } }) => {+ return updateDiagramName(name, { updateHistory: false });},- removeTable: ({ table }) => {- return addTable(table!);+ addTable: ({ undoData: { tableId } }) => {+ return removeTable(tableId, { updateHistory: false });},- updateTable: ({ tableId, table }) => {- return updateTable(tableId!, table!);+ removeTable: ({ undoData: { table } }) => {+ return addTable(table, { updateHistory: false });+ },+ updateTable: ({ undoData: { tableId, table } }) => {+ return updateTable(tableId, table, { updateHistory: false });+ },+ addField: ({ undoData: { fieldId, tableId } }) => {+ return removeField(tableId, fieldId, { updateHistory: false });+ },+ removeField: ({ undoData: { tableId, field } }) => {+ return addField(tableId, field, { updateHistory: false });+ },+ updateField: ({ undoData: { tableId, fieldId, field } }) => {+ return updateField(tableId, fieldId, field, {+ updateHistory: false,+ });+ },+ addRelationship: ({ undoData: { relationshipId } }) => {+ return removeRelationship(relationshipId, {+ updateHistory: false,+ });+ },+ removeRelationship: ({ undoData: { relationship } }) => {+ return addRelationship(relationship, { updateHistory: false });+ },+ removeRelationships: ({ undoData: { relationships } }) => {+ return addRelationships(relationships, {+ updateHistory: false,+ });+ },+ updateRelationship: ({+ undoData: { relationshipId, relationship },+ }) => {+ return updateRelationship(relationshipId, relationship, {+ updateHistory: false,+ });+ },+ updateTablesState: ({ undoData: { tables } }) => {+ return updateTablesState(() => tables, {+ updateHistory: false,+ });+ },+ addIndex: ({ undoData: { tableId, indexId } }) => {+ return removeIndex(tableId, indexId, { updateHistory: false });+ },+ removeIndex: ({ undoData: { tableId, index } }) => {+ return addIndex(tableId, index, { updateHistory: false });+ },+ updateIndex: ({ undoData: { tableId, indexId, index } }) => {+ return updateIndex(tableId, indexId, index, {+ updateHistory: false,+ });+ },+ addRelationships: ({ undoData: { relationshipIds } }) => {+ return removeRelationships(relationshipIds, {+ updateHistory: false,+ });},}),- [addTable, removeTable, updateTable]+ [+ addTable,+ removeTable,+ updateTable,+ updateDiagramName,+ removeField,+ addField,+ updateField,+ addRelationship,+ addRelationships,+ removeRelationship,+ updateRelationship,+ updateTablesState,+ addIndex,+ removeIndex,+ updateIndex,+ removeRelationships,+ ]);const undo = async () => {- if (undoStack.length === 0) {+ const action = undoStack.pop();+ if (!action) {return;}- const action = undoStack[undoStack.length - 1];- if (action) {- const handler = undoActionHandlers[action.action];+ const handler = undoActionHandlers[action.action];+ addRedoAction(action);++ await handler?.({+ undoData: action.undoData,// eslint-disable-next-line @typescript-eslint/no-explicit-any- await handler?.(action.data as any);- setRedoStack([...redoStack, action]);- setUndoStack(undoStack.slice(0, undoStack.length - 1));- }+ } as any);};const redo = async () => {- if (redoStack.length === 0) {+ const action = redoStack.pop();+ if (!action) {return;}- const action = redoStack[redoStack.length - 1];- if (action) {- const handler = redoActionHandlers[action.action];+ const handler = redoActionHandlers[action.action];+ addUndoAction(action);++ await handler?.({+ redoData: action.redoData,// eslint-disable-next-line @typescript-eslint/no-explicit-any- await handler?.(action.data as any);- setUndoStack([...undoStack, action]);- setRedoStack(redoStack.slice(0, redoStack.length - 1));- }+ } as any);};return (- <historyContext.Provider value={{ undo, redo }}>+ <historyContext.Provider value={{ undo, redo, hasRedo, hasUndo }}>{children}</historyContext.Provider>);diff --git a/src/context/history-context/redo-undo-action.ts b/src/context/history-context/redo-undo-action.tsindex 8fe81d85..05d4f4f3 100644--- a/src/context/history-context/redo-undo-action.ts+++ b/src/context/history-context/redo-undo-action.ts@@ -1,5 +1,4 @@import { DBTable } from '@/lib/domain/db-table';-import { DatabaseType } from '@/lib/domain/database-type';import { ChartDBContext } from '../chartdb-context/chartdb-context';import { DBField } from '@/lib/domain/db-field';import { DBIndex } from '@/lib/domain/db-index';@@ -7,103 +6,113 @@ import { DBRelationship } from '@/lib/domain/db-relationship';type Action = keyof ChartDBContext;-type RedoUndoActionBase<T extends Action, D> = {+type RedoUndoActionBase<T extends Action, RD, UD> = {action: T;- data: D;+ redoData: RD;+ undoData: UD;};-type RedoUndoActionUpdateDiagramId = RedoUndoActionBase<- 'updateDiagramId',- { id: string }->;-type RedoUndoActionUpdateDiagramName = RedoUndoActionBase<'updateDiagramName',+ { name: string },{ name: string }>;-type RedoUndoActionUpdateDatabaseType = RedoUndoActionBase<- 'updateDatabaseType',- { databaseType: DatabaseType }->;-type RedoUndoActionUpdateTable = RedoUndoActionBase<'updateTable',+ { tableId: string; table: Partial<DBTable> },{ tableId: string; table: Partial<DBTable> }>;type RedoUndoActionAddTable = RedoUndoActionBase<'addTable',- { tableId?: string; table?: DBTable }+ { table: DBTable },+ { tableId: string }>;type RedoUndoActionRemoveTable = RedoUndoActionBase<'removeTable',- { tableId?: string; table?: DBTable }+ { tableId: string },+ { table: DBTable }>;type RedoUndoActionUpdateTablesState = RedoUndoActionBase<'updateTablesState',+ { tables: DBTable[] },{ tables: DBTable[] }>;type RedoUndoActionAddField = RedoUndoActionBase<'addField',- { tableId: string; fieldId?: string; field?: DBField }+ { tableId: string; field: DBField },+ { tableId: string; fieldId: string }>;type RedoUndoActionRemoveField = RedoUndoActionBase<'removeField',- { tableId: string; fieldId?: string; field?: DBField }+ { tableId: string; fieldId: string },+ { tableId: string; field: DBField }>;type RedoUndoActionUpdateField = RedoUndoActionBase<'updateField',+ { tableId: string; fieldId: string; field: Partial<DBField> },{ tableId: string; fieldId: string; field: Partial<DBField> }>;type RedoUndoActionAddIndex = RedoUndoActionBase<'addIndex',- { tableId: string; indexId?: string; index?: DBIndex }+ { tableId: string; index: DBIndex },+ { tableId: string; indexId: string }>;type RedoUndoActionRemoveIndex = RedoUndoActionBase<'removeIndex',- { tableId: string; indexId?: string; index?: DBIndex }+ { tableId: string; indexId: string },+ { tableId: string; index: DBIndex }>;type RedoUndoActionUpdateIndex = RedoUndoActionBase<'updateIndex',+ { tableId: string; indexId: string; index: Partial<DBIndex> },{ tableId: string; indexId: string; index: Partial<DBIndex> }>;type RedoUndoActionAddRelationship = RedoUndoActionBase<'addRelationship',- { relationshipId?: string; relationship?: DBRelationship }+ { relationship: DBRelationship },+ { relationshipId: string }+>;++type RedoUndoActionAddRelationships = RedoUndoActionBase<+ 'addRelationships',+ { relationships: DBRelationship[] },+ { relationshipIds: string[] }>;type RedoUndoActionRemoveRelationship = RedoUndoActionBase<'removeRelationship',- { relationshipId?: string; relationship?: DBRelationship }+ { relationshipId: string },+ { relationship: DBRelationship }>;type RedoUndoActionUpdateRelationship = RedoUndoActionBase<'updateRelationship',+ { relationshipId: string; relationship: Partial<DBRelationship> },{ relationshipId: string; relationship: Partial<DBRelationship> }>;type RedoUndoActionRemoveRelationships = RedoUndoActionBase<'removeRelationships',- { relationshipsIds?: string[]; relationships?: DBRelationship[] }+ { relationshipsIds: string[] },+ { relationships: DBRelationship[] }>;export type RedoUndoAction =| RedoUndoActionAddTable| RedoUndoActionRemoveTable| RedoUndoActionUpdateTable- | RedoUndoActionUpdateDatabaseType| RedoUndoActionUpdateDiagramName- | RedoUndoActionUpdateDiagramId| RedoUndoActionUpdateTablesState| RedoUndoActionAddField| RedoUndoActionRemoveField@@ -112,15 +121,24 @@ export type RedoUndoAction =| RedoUndoActionRemoveIndex| RedoUndoActionUpdateIndex| RedoUndoActionAddRelationship+ | RedoUndoActionAddRelationships| RedoUndoActionRemoveRelationship| RedoUndoActionUpdateRelationship| RedoUndoActionRemoveRelationships;-export type RedoUndoActionData<T extends Action> = Extract<+export type RedoActionData<T extends Action> = Extract<+ RedoUndoAction,+ { action: T }+>['redoData'];++export type UndoActionData<T extends Action> = Extract<RedoUndoAction,{ action: T }->['data'];+>['undoData'];export type RedoUndoActionHandlers = {- [K in Action]: (data: RedoUndoActionData<K>) => Promise<void>;+ [K in RedoUndoAction['action']]: (args: {+ redoData: RedoActionData<K>;+ undoData: UndoActionData<K>;+ }) => Promise<void>;};diff --git a/src/context/history-context/redo-undo-stack-context.tsx b/src/context/history-context/redo-undo-stack-context.tsxindex 9359f773..2a2f1eeb 100644--- a/src/context/history-context/redo-undo-stack-context.tsx+++ b/src/context/history-context/redo-undo-stack-context.tsx@@ -1,4 +1,3 @@-import React from 'react';import { createContext } from 'react';import { RedoUndoAction } from './redo-undo-action';import { emptyFn } from '@/lib/utils';@@ -6,13 +5,19 @@ import { emptyFn } from '@/lib/utils';export interface RedoUndoStackContext {redoStack: RedoUndoAction[];undoStack: RedoUndoAction[];- setRedoStack: React.Dispatch<React.SetStateAction<RedoUndoAction[]>>;- setUndoStack: React.Dispatch<React.SetStateAction<RedoUndoAction[]>>;+ addRedoAction: (action: RedoUndoAction) => void;+ addUndoAction: (action: RedoUndoAction) => void;+ resetRedoStack: () => void;+ hasRedo: boolean;+ hasUndo: boolean;}export const redoUndoStackContext = createContext<RedoUndoStackContext>({redoStack: [],undoStack: [],- setRedoStack: emptyFn,- setUndoStack: emptyFn,+ addRedoAction: emptyFn,+ addUndoAction: emptyFn,+ resetRedoStack: emptyFn,+ hasRedo: false,+ hasUndo: false,});diff --git a/src/context/history-context/redo-undo-stack-provider.tsx b/src/context/history-context/redo-undo-stack-provider.tsxindex e52c2180..3eb66c4d 100644--- a/src/context/history-context/redo-undo-stack-provider.tsx+++ b/src/context/history-context/redo-undo-stack-provider.tsx@@ -1,6 +1,9 @@-import React from 'react';+import React, { useCallback } from 'react';import { RedoUndoAction } from './redo-undo-action';-import { redoUndoStackContext } from './redo-undo-stack-context';+import {+ RedoUndoStackContext,+ redoUndoStackContext,+} from './redo-undo-stack-context';export const RedoUndoStackProvider: React.FC<React.PropsWithChildren> = ({children,@@ -8,13 +11,38 @@ export const RedoUndoStackProvider: React.FC<React.PropsWithChildren> = ({const [undoStack, setUndoStack] = React.useState<RedoUndoAction[]>([]);const [redoStack, setRedoStack] = React.useState<RedoUndoAction[]>([]);+ const addRedoAction: RedoUndoStackContext['addRedoAction'] = useCallback(+ (action) => {+ setRedoStack((prev) => [...prev, action]);+ },+ [setRedoStack]+ );++ const addUndoAction: RedoUndoStackContext['addUndoAction'] = useCallback(+ (action) => {+ setUndoStack((prev) => [...prev, action]);+ },+ [setUndoStack]+ );++ const resetRedoStack: RedoUndoStackContext['resetRedoStack'] =+ useCallback(() => {+ setRedoStack([]);+ }, [setRedoStack]);++ const hasRedo = redoStack.length > 0;+ const hasUndo = undoStack.length > 0;+return (<redoUndoStackContext.Providervalue={{redoStack,undoStack,- setRedoStack,- setUndoStack,+ addRedoAction,+ addUndoAction,+ resetRedoStack,+ hasRedo,+ hasUndo,}}>{children}diff --git a/src/pages/editor-page/canvas/canvas.tsx b/src/pages/editor-page/canvas/canvas.tsxindex 365157b8..ce87b2e1 100644--- a/src/pages/editor-page/canvas/canvas.tsx+++ b/src/pages/editor-page/canvas/canvas.tsx@@ -156,7 +156,7 @@ export const Canvas: React.FC<CanvasProps> = () => {.filter((id) => !!id) as string[];if (relationshipsToRemove.length > 0) {- removeRelationships(...relationshipsToRemove);+ removeRelationships(relationshipsToRemove);}const selectionChanges = changes.filter(diff --git a/src/pages/editor-page/toolbar/toolbar.tsx b/src/pages/editor-page/toolbar/toolbar.tsxindex 33c48284..e90344e5 100644--- a/src/pages/editor-page/toolbar/toolbar.tsx+++ b/src/pages/editor-page/toolbar/toolbar.tsx@@ -1,12 +1,14 @@import React from 'react';import { Card, CardContent } from '@/components/card/card';-import { ZoomIn, ZoomOut, Save } from 'lucide-react';+import { ZoomIn, ZoomOut, Save, Redo, Undo } from 'lucide-react';import { Separator } from '@/components/separator/separator';import { ToolbarButton } from './toolbar-button';+import { useHistory } from '@/hooks/use-history';export interface ToolbarProps {}export const Toolbar: React.FC<ToolbarProps> = () => {+ const { redo, undo, hasRedo, hasUndo } = useHistory();return (<div className="px-1"><Card className="shadow-none p-0 bg-secondary h-[44px]">@@ -21,6 +23,13 @@ export const Toolbar: React.FC<ToolbarProps> = () => {<ToolbarButton><Save /></ToolbarButton>+ <Separator orientation="vertical" />+ <ToolbarButton onClick={undo} disabled={!hasUndo}>+ <Undo />+ </ToolbarButton>+ <ToolbarButton onClick={redo} disabled={!hasRedo}>+ <Redo />+ </ToolbarButton></CardContent></Card></div>