-
Notifications
You must be signed in to change notification settings - Fork 193
feat: undo/redo, action enhancements #128
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
This pull request is being automatically deployed with Vercel (learn more). 🔍 Inspect: https://vercel.com/timc/kbar/B5q4wxPacn31Qba9LKRTwDGHkN1F |
example/src/App.tsx
Outdated
document.documentElement.setAttribute("data-theme-dark", ""), | ||
perform: () => { | ||
document.documentElement.setAttribute("data-theme-dark", ""); | ||
return () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Return an optional cleanup function similar to useEffect
which will give your action the ability to undo.
@@ -160,7 +163,7 @@ const App = () => { | |||
}; | |||
|
|||
function RenderResults() { | |||
const { results, rootActionId } = useDeepMatches(); | |||
const { results, rootActionId } = useMatches(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#124 🧹
src/types.ts
Outdated
undo: (item?: IHistoryItem) => void; | ||
redo: (item?: IHistoryItem) => void; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Todo: History
should probably only store and retrieve items; we should leave the undo
/redo
to the implementer, which in our case is the Command
object.
if (!currentRootActionId) return action.ancestors; | ||
const index = action.ancestors.findIndex( | ||
(ancestor) => ancestor.id === currentRootActionId | ||
); | ||
// +1 removes the currentRootAction; e.g. | ||
// if we are on the "Set theme" parent action, | ||
// the UI should not display "Set theme… > Dark" | ||
// but rather just "Dark" | ||
return action.ancestors.slice(index + 1); | ||
}, [action.ancestors, currentRootActionId]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be wonderful to have this logic handled within ActionImpl
, but ActionImpl
shouldn't have context of currentRootActionId
.
return () => { | ||
if (isDark) doc.setAttribute(attribute, ""); | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🍖 An action's undo is defined by the return function of its perform
Closes #129.
Introducing the Command interface, which extends a given
action.perform
and adds the ability to undo/redo.Command
&History
Undo/redo