Abstract TransformContext

This commit is contained in:
1ilit 2024-03-10 19:24:19 +02:00
parent dd4ec59e56
commit 23bc6278a2
5 changed files with 150 additions and 131 deletions

View File

@ -9,11 +9,11 @@ import {
TableContext, TableContext,
UndoRedoContext, UndoRedoContext,
SelectContext, SelectContext,
TransformContext,
} from "../pages/Editor"; } from "../pages/Editor";
import Note from "./Note"; import Note from "./Note";
import { Toast } from "@douyinfe/semi-ui"; import { Toast } from "@douyinfe/semi-ui";
import useSettings from "../hooks/useSettings"; import useSettings from "../hooks/useSettings";
import useTransform from "../hooks/useTransform";
export default function Canvas() { export default function Canvas() {
const { tables, updateTable, relationships, addRelationship } = const { tables, updateTable, relationships, addRelationship } =
@ -22,7 +22,7 @@ export default function Canvas() {
const { notes, updateNote } = useContext(NoteContext); const { notes, updateNote } = useContext(NoteContext);
const { settings } = useSettings(); const { settings } = useSettings();
const { setUndoStack, setRedoStack } = useContext(UndoRedoContext); const { setUndoStack, setRedoStack } = useContext(UndoRedoContext);
const { transform, setTransform } = useContext(TransformContext); const { transform, setTransform } = useTransform();
const { selectedElement, setSelectedElement } = useContext(SelectContext); const { selectedElement, setSelectedElement } = useContext(SelectContext);
const [dragging, setDragging] = useState({ const [dragging, setDragging] = useState({
element: ObjectType.NONE, element: ObjectType.NONE,

View File

@ -51,7 +51,6 @@ import {
import { import {
AreaContext, AreaContext,
NoteContext, NoteContext,
TransformContext,
SelectContext, SelectContext,
StateContext, StateContext,
TabContext, TabContext,
@ -73,6 +72,7 @@ import Todo from "./Todo";
import { Thumbnail } from "./Thumbnail"; import { Thumbnail } from "./Thumbnail";
import useLayout from "../hooks/useLayout"; import useLayout from "../hooks/useLayout";
import useSettings from "../hooks/useSettings"; import useSettings from "../hooks/useSettings";
import useTransform from "../hooks/useTransform";
export default function ControlPanel({ export default function ControlPanel({
diagramId, diagramId,
@ -147,7 +147,7 @@ export default function ControlPanel({
useContext(UndoRedoContext); useContext(UndoRedoContext);
const { selectedElement, setSelectedElement } = useContext(SelectContext); const { selectedElement, setSelectedElement } = useContext(SelectContext);
const { tab, setTab } = useContext(TabContext); const { tab, setTab } = useContext(TabContext);
const { transform, setTransform } = useContext(TransformContext); const { transform, setTransform } = useTransform();
const invertLayout = (component) => const invertLayout = (component) =>
setLayout((prev) => ({ ...prev, [component]: !prev[component] })); setLayout((prev) => ({ ...prev, [component]: !prev[component] }));

View File

@ -0,0 +1,16 @@
import { createContext, useState } from "react";
export const TransformContext = createContext(null);
export default function TransformContextProvider({ children }) {
const [transform, setTransform] = useState({
zoom: 1,
pan: { x: 0, y: 0 },
});
return (
<TransformContext.Provider value={{ transform, setTransform }}>
{children}
</TransformContext.Provider>
);
}

View File

@ -0,0 +1,6 @@
import { useContext } from "react";
import { TransformContext } from "../context/TransformContext";
export default function useTransform() {
return useContext(TransformContext);
}

View File

@ -16,6 +16,8 @@ import { exitFullscreen } from "../utils";
import useLayout from "../hooks/useLayout"; import useLayout from "../hooks/useLayout";
import LayoutContextProvider from "../context/LayoutContext"; import LayoutContextProvider from "../context/LayoutContext";
import useSettings from "../hooks/useSettings"; import useSettings from "../hooks/useSettings";
import TransformContextProvider from "../context/TransformContext";
import useTransform from "../hooks/useTransform";
export const StateContext = createContext(); export const StateContext = createContext();
export const TableContext = createContext(); export const TableContext = createContext();
@ -28,12 +30,14 @@ export const TaskContext = createContext();
export const MessageContext = createContext(); export const MessageContext = createContext();
export const BotMessageContext = createContext(); export const BotMessageContext = createContext();
export const TypeContext = createContext(); export const TypeContext = createContext();
export const TransformContext = createContext(); // export const TransformContext = createContext();
export default function Editor() { export default function Editor() {
return ( return (
<LayoutContextProvider> <LayoutContextProvider>
<TransformContextProvider>
<WorkSpace /> <WorkSpace />
</TransformContextProvider>
</LayoutContextProvider> </LayoutContextProvider>
); );
} }
@ -53,10 +57,7 @@ function WorkSpace() {
const [tab, setTab] = useState(Tab.tables); const [tab, setTab] = useState(Tab.tables);
const { layout, setLayout } = useLayout(); const { layout, setLayout } = useLayout();
const { settings, setSettings } = useSettings(); const { settings, setSettings } = useSettings();
const [transform, setTransform] = useState({ const { transform, setTransform } = useTransform();
zoom: 1,
pan: { x: 0, y: 0 },
});
const [tasks, setTasks] = useState([]); const [tasks, setTasks] = useState([]);
const [undoStack, setUndoStack] = useState([]); const [undoStack, setUndoStack] = useState([]);
const [redoStack, setRedoStack] = useState([]); const [redoStack, setRedoStack] = useState([]);
@ -682,11 +683,10 @@ function WorkSpace() {
break; break;
} }
} }
}, [setSettings]); }, [setSettings, setTransform]);
return ( return (
<StateContext.Provider value={{ state, setState }}> <StateContext.Provider value={{ state, setState }}>
<TransformContext.Provider value={{ transform, setTransform }}>
<TableContext.Provider <TableContext.Provider
value={{ value={{
tables, tables,
@ -714,9 +714,7 @@ function WorkSpace() {
<SelectContext.Provider <SelectContext.Provider
value={{ selectedElement, setSelectedElement }} value={{ selectedElement, setSelectedElement }}
> >
<TaskContext.Provider <TaskContext.Provider value={{ tasks, setTasks, updateTask }}>
value={{ tasks, setTasks, updateTask }}
>
<TypeContext.Provider <TypeContext.Provider
value={{ value={{
types, types,
@ -813,7 +811,6 @@ function WorkSpace() {
</NoteContext.Provider> </NoteContext.Provider>
</AreaContext.Provider> </AreaContext.Provider>
</TableContext.Provider> </TableContext.Provider>
</TransformContext.Provider>
</StateContext.Provider> </StateContext.Provider>
); );
} }