Skip to content

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

Merged
merged 24 commits into from
Nov 25, 2021
Merged

feat: undo/redo, action enhancements #128

merged 24 commits into from
Nov 25, 2021

Conversation

timc1
Copy link
Owner

@timc1 timc1 commented Nov 21, 2021

Closes #129.

Introducing the Command interface, which extends a given action.perform and adds the ability to undo/redo.

  • Add invariant
  • Fix imports; lib imports should import relative, example imports from root
  • Make history optional/toggleable
  • Add example action where perform displays toast to undo
  • Add additional tests for Command & History
  • Update docs

Undo/redo

demo

Not Found
@vercel
Copy link

vercel bot commented Nov 21, 2021

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: https://vercel.com/timc/kbar/B5q4wxPacn31Qba9LKRTwDGHkN1F
✅ Preview: https://kbar-git-next-timc.vercel.app

document.documentElement.setAttribute("data-theme-dark", ""),
perform: () => {
document.documentElement.setAttribute("data-theme-dark", "");
return () => {
Copy link
Owner Author

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();
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#124 🧹

@timc1 timc1 changed the title feat: action improvements, undo/redo feat: undo/redo, action enhancements Nov 23, 2021
src/types.ts Outdated
Comment on lines 100 to 101
undo: (item?: IHistoryItem) => void;
redo: (item?: IHistoryItem) => void;
Copy link
Owner Author

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.

Comment on lines +184 to +193
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]);
Copy link
Owner Author

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.

Comment on lines +120 to +122
return () => {
if (isDark) doc.setAttribute(attribute, "");
};
Copy link
Owner Author

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

@timc1 timc1 marked this pull request as ready for review November 25, 2021 19:11
@timc1 timc1 merged commit e9bd63f into main Nov 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add support for undo/redo
1 participant