Skip to content

Commit

Permalink
fix: editor history (#94)
Browse files Browse the repository at this point in the history
* feat: set initial data from props

* fix: undo lose focus

* fix: activate selected element tab
  • Loading branch information
bepyan authored Aug 22, 2024
1 parent 2df03a2 commit b3da446
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
15 changes: 11 additions & 4 deletions src/components/editor/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ const updateEditorHistory = (
state: {
...editor.state,
selectedElement: newSelectedElement,
currentTabValue: editorTabValue.ELEMENT_SETTINGS,
},
data: newData,
history: {
Expand Down Expand Up @@ -368,10 +369,8 @@ const actionHandlers: {
const isValidSelect = isValidSelectEditorElement(payload.elementDetails);

const newTabValue = isValidSelect
? editorTabValue.ELEMENTS
: editor.state.currentTabValue === editorTabValue.ELEMENT_SETTINGS
? editorTabValue.ELEMENT_SETTINGS
: editor.state.currentTabValue;
? editorTabValue.ELEMENT_SETTINGS
: editor.state.currentTabValue;

return {
...editor,
Expand Down Expand Up @@ -433,6 +432,10 @@ const actionHandlers: {
...editor.history,
currentIndex: nextIndex,
},
state: {
...editor.state,
selectedElement: emptyElement,
},
};
}
return editor;
Expand All @@ -448,6 +451,10 @@ const actionHandlers: {
...editor.history,
currentIndex: prevIndex,
},
state: {
...editor.state,
selectedElement: emptyElement,
},
};
}
return editor;
Expand Down
22 changes: 18 additions & 4 deletions src/components/editor/provider.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
"use client";

import { createContext, useContext, useReducer, type Dispatch } from "react";
import {
createContext,
useContext,
useMemo,
useReducer,
type Dispatch,
} from "react";
import { editorReducer, type EditorAction } from "~/components/editor/action";
import {
initialEditor,
Expand Down Expand Up @@ -36,12 +42,20 @@ export default function EditorProvider({
editorData,
editorState,
}: EditorProviderProps) {
const [editor, dispatch] = useReducer(editorReducer, {
...initialEditor,
data: {
const initialData = useMemo(() => {
return {
...initialEditor.data,
...editorData,
};
}, [editorData]);

const [editor, dispatch] = useReducer(editorReducer, {
...initialEditor,
history: {
...initialEditor.history,
list: [initialData],
},
data: initialData,
config: {
...initialEditorConfig,
...editorConfig,
Expand Down

0 comments on commit b3da446

Please sign in to comment.