use-undoable
Advanced tools
Comparing version 3.3.11 to 3.3.12
@@ -1,3 +0,2 @@ | ||
declare const payloadError: (func: string) => never; | ||
declare const invalidBehavior: (behavior: string) => never; | ||
export { payloadError, invalidBehavior, }; | ||
export declare const payloadError: (func: string) => never; | ||
export declare const invalidBehavior: (behavior: string) => never; |
@@ -1,2 +0,3 @@ | ||
import useUndoable from './useUndoable'; | ||
import useUndoable from "./useUndoable"; | ||
export default useUndoable; | ||
export * from './types'; |
@@ -24,3 +24,2 @@ var react = require('react'); | ||
}; | ||
var invalidBehavior = function invalidBehavior(behavior) { | ||
@@ -57,3 +56,3 @@ throw new Error("Mutation behavior must be one of: mergePastReversed, mergePast, keepFuture, or destroyFuture. Not: " + behavior); | ||
if (!payload) { | ||
payloadError('mutate'); | ||
payloadError("mutate"); | ||
} | ||
@@ -71,3 +70,3 @@ | ||
if (historyLimit !== 'infinium' && historyLimit !== 'infinity') { | ||
if (historyLimit !== "infinium" && historyLimit !== "infinity") { | ||
mPast = ensureLimit(historyLimit, past); | ||
@@ -105,2 +104,7 @@ } | ||
}; | ||
if (typeof behavior === "undefined") { | ||
return behaviorMap.mergePastReversed; | ||
} | ||
if (!behaviorMap.hasOwnProperty(behavior)) invalidBehavior(behavior); | ||
@@ -160,3 +164,3 @@ return behaviorMap[behavior]; | ||
if (!payload) { | ||
payloadError('resetInitialState'); | ||
payloadError("resetInitialState"); | ||
} | ||
@@ -189,3 +193,3 @@ | ||
var defaultOptions = { | ||
behavior: 'mergePastReversed', | ||
behavior: "mergePastReversed", | ||
historyLimit: 100, | ||
@@ -216,3 +220,3 @@ ignoreIdenticalMutations: true, | ||
dispatch({ | ||
type: 'undo' | ||
type: "undo" | ||
}); | ||
@@ -224,3 +228,3 @@ } | ||
dispatch({ | ||
type: 'redo' | ||
type: "redo" | ||
}); | ||
@@ -235,3 +239,3 @@ } | ||
return dispatch({ | ||
type: 'reset', | ||
type: "reset", | ||
payload: payload | ||
@@ -242,3 +246,3 @@ }); | ||
return dispatch({ | ||
type: 'resetInitialState', | ||
type: "resetInitialState", | ||
payload: payload | ||
@@ -249,3 +253,3 @@ }); | ||
return dispatch(_extends({ | ||
type: 'update', | ||
type: "update", | ||
payload: payload, | ||
@@ -265,5 +269,6 @@ behavior: mutationBehavior, | ||
return typeof payload === 'function' ? update(payload(state.present), mutationBehavior, ignoreAction) : update(payload, mutationBehavior, ignoreAction); | ||
return typeof payload === "function" ? update(payload(state.present), mutationBehavior, ignoreAction) : update(payload, mutationBehavior, ignoreAction); | ||
}, [state]); | ||
var static_setState = react.useCallback(function (payload, mutationBehavior, ignoreAction) { | ||
var static_setState = function static_setState(payload, mutationBehavior, ignoreAction) { | ||
if (mutationBehavior === void 0) { | ||
@@ -278,3 +283,4 @@ mutationBehavior = options.behavior; | ||
update(payload, mutationBehavior, ignoreAction); | ||
}, []); | ||
}; | ||
return [state.present, setState, { | ||
@@ -281,0 +287,0 @@ past: state.past, |
@@ -24,3 +24,2 @@ import { useReducer, useCallback } from 'react'; | ||
}; | ||
var invalidBehavior = function invalidBehavior(behavior) { | ||
@@ -57,3 +56,3 @@ throw new Error("Mutation behavior must be one of: mergePastReversed, mergePast, keepFuture, or destroyFuture. Not: " + behavior); | ||
if (!payload) { | ||
payloadError('mutate'); | ||
payloadError("mutate"); | ||
} | ||
@@ -71,3 +70,3 @@ | ||
if (historyLimit !== 'infinium' && historyLimit !== 'infinity') { | ||
if (historyLimit !== "infinium" && historyLimit !== "infinity") { | ||
mPast = ensureLimit(historyLimit, past); | ||
@@ -105,2 +104,7 @@ } | ||
}; | ||
if (typeof behavior === "undefined") { | ||
return behaviorMap.mergePastReversed; | ||
} | ||
if (!behaviorMap.hasOwnProperty(behavior)) invalidBehavior(behavior); | ||
@@ -160,3 +164,3 @@ return behaviorMap[behavior]; | ||
if (!payload) { | ||
payloadError('resetInitialState'); | ||
payloadError("resetInitialState"); | ||
} | ||
@@ -189,3 +193,3 @@ | ||
var defaultOptions = { | ||
behavior: 'mergePastReversed', | ||
behavior: "mergePastReversed", | ||
historyLimit: 100, | ||
@@ -216,3 +220,3 @@ ignoreIdenticalMutations: true, | ||
dispatch({ | ||
type: 'undo' | ||
type: "undo" | ||
}); | ||
@@ -224,3 +228,3 @@ } | ||
dispatch({ | ||
type: 'redo' | ||
type: "redo" | ||
}); | ||
@@ -235,3 +239,3 @@ } | ||
return dispatch({ | ||
type: 'reset', | ||
type: "reset", | ||
payload: payload | ||
@@ -242,3 +246,3 @@ }); | ||
return dispatch({ | ||
type: 'resetInitialState', | ||
type: "resetInitialState", | ||
payload: payload | ||
@@ -249,3 +253,3 @@ }); | ||
return dispatch(_extends({ | ||
type: 'update', | ||
type: "update", | ||
payload: payload, | ||
@@ -265,5 +269,6 @@ behavior: mutationBehavior, | ||
return typeof payload === 'function' ? update(payload(state.present), mutationBehavior, ignoreAction) : update(payload, mutationBehavior, ignoreAction); | ||
return typeof payload === "function" ? update(payload(state.present), mutationBehavior, ignoreAction) : update(payload, mutationBehavior, ignoreAction); | ||
}, [state]); | ||
var static_setState = useCallback(function (payload, mutationBehavior, ignoreAction) { | ||
var static_setState = function static_setState(payload, mutationBehavior, ignoreAction) { | ||
if (mutationBehavior === void 0) { | ||
@@ -278,3 +283,4 @@ mutationBehavior = options.behavior; | ||
update(payload, mutationBehavior, ignoreAction); | ||
}, []); | ||
}; | ||
return [state.present, setState, { | ||
@@ -281,0 +287,0 @@ past: state.past, |
@@ -1,3 +0,3 @@ | ||
import type { Action, State } from './types'; | ||
declare const mutate: (state: State, action: Action) => any; | ||
export { mutate, }; | ||
import type { Action, State } from "./types"; | ||
declare const mutate: (state: State, action: Action) => State; | ||
export { mutate }; |
@@ -1,3 +0,2 @@ | ||
import type { Action, State } from './types'; | ||
declare const reducer: (state: State, action: Action) => State; | ||
export { reducer }; | ||
import type { Action, State } from "./types"; | ||
export declare const reducer: (state: State, action: Action) => State; |
@@ -1,5 +0,5 @@ | ||
declare type ActionType = 'undo' | 'redo' | 'update' | 'reset' | 'resetInitialState'; | ||
declare type HistoryLimit = number | 'infinium' | 'infinity'; | ||
declare type MutationBehavior = 'mergePastReversed' | 'mergePast' | 'destroyFuture' | 'keepFuture'; | ||
interface Action { | ||
export declare type ActionType = "undo" | "redo" | "update" | "reset" | "resetInitialState"; | ||
export declare type HistoryLimit = number | "infinium" | "infinity"; | ||
export declare type MutationBehavior = "mergePastReversed" | "mergePast" | "destroyFuture" | "keepFuture"; | ||
export interface Action { | ||
type: ActionType; | ||
@@ -13,3 +13,3 @@ payload?: any; | ||
} | ||
interface State { | ||
export interface State { | ||
past: any[]; | ||
@@ -19,3 +19,3 @@ present: any; | ||
} | ||
interface Options { | ||
export interface Options { | ||
behavior?: MutationBehavior; | ||
@@ -26,2 +26,12 @@ historyLimit?: HistoryLimit; | ||
} | ||
export type { HistoryLimit, MutationBehavior, Action, State, Options }; | ||
export declare type UseUndoable<T> = [T, (payload: T | ((oldValue: T) => T), behavior?: MutationBehavior, ignoreAction?: boolean) => void, { | ||
past: T[]; | ||
future: T[]; | ||
undo: () => void; | ||
canUndo: boolean; | ||
redo: () => void; | ||
canRedo: boolean; | ||
reset: (initialState?: T) => void; | ||
resetInitialState: (newInitialState: T) => void; | ||
static_setState: (payload: T, behavior?: MutationBehavior, ignoreAction?: boolean) => void; | ||
}]; |
@@ -1,13 +0,3 @@ | ||
import type { MutationBehavior, Options } from './types'; | ||
declare const useUndoable: <T = any>(initialPresent: T, options?: Options) => [T, (payload: T | ((oldValue: T) => T), behavior?: "mergePastReversed" | "mergePast" | "destroyFuture" | "keepFuture" | undefined, ignoreAction?: boolean | undefined) => void, { | ||
past: T[]; | ||
future: T[]; | ||
undo: () => void; | ||
canUndo: boolean; | ||
redo: () => void; | ||
canRedo: boolean; | ||
reset: (initialState?: T | undefined) => void; | ||
resetInitialState: (newInitialState: T) => void; | ||
static_setState: (payload: T, behavior?: "mergePastReversed" | "mergePast" | "destroyFuture" | "keepFuture" | undefined, ignoreAction?: boolean | undefined) => void; | ||
}]; | ||
import type { Options, UseUndoable } from "./types"; | ||
declare const useUndoable: <T = any>(initialPresent: T, options?: Options) => UseUndoable<T>; | ||
export default useUndoable; |
{ | ||
"name": "use-undoable", | ||
"version": "3.3.11", | ||
"version": "3.3.12", | ||
"description": "React hook for undo/redo functionality without the hassle.", | ||
"private": false, | ||
"author": "Infinium LLC.", | ||
"author": "Tristan Brewster <tristan@infi.im>", | ||
"license": "MIT", | ||
"repository": "https://github.com/Infinium8/useUndoable", | ||
"repository": "https://github.com/xplato/useUndoable", | ||
"main": "dist/index.js", | ||
@@ -10,0 +10,0 @@ "module": "dist/index.modern.js", |
123
README.md
@@ -7,3 +7,3 @@ # useUndoable | ||
Note: Information about integration with React Flow v10 is near the bottom of this document. | ||
Integrating with React Flow v10? [Read this.](#integration-with-react-flow-v10) | ||
@@ -25,16 +25,16 @@ ## Installation | ||
```js | ||
import useUndoable from 'use-undoable'; | ||
import useUndoable from "use-undoable" | ||
const MyComponent = () => { | ||
const initialState = 0; | ||
const initialState = 0 | ||
const [count, setCount, { undo, redo }] = useUndoable(initialState); | ||
const [count, setCount, { undo, redo }] = useUndoable(initialState) | ||
return ( | ||
<> | ||
<p>{count}</p> | ||
<button onClick={undo}>undo</button> | ||
</> | ||
); | ||
}; | ||
return ( | ||
<> | ||
<p>{count}</p> | ||
<button onClick={undo}>undo</button> | ||
</> | ||
) | ||
} | ||
``` | ||
@@ -84,3 +84,3 @@ | ||
```js | ||
const [yourState, setYourState, { undo, redo }] = useUndoable(initialState); | ||
const [yourState, setYourState, { undo, redo }] = useUndoable(initialState) | ||
``` | ||
@@ -91,3 +91,3 @@ | ||
```js | ||
const [yourState, setYourState] = useState(initialState); | ||
const [yourState, setYourState] = useState(initialState) | ||
``` | ||
@@ -102,7 +102,7 @@ | ||
```js | ||
setYourState(yourState + 1); | ||
setYourState(yourState + 1) | ||
``` | ||
```js | ||
setYourState((currentState) => currentState + 1); | ||
setYourState(currentState => currentState + 1) | ||
``` | ||
@@ -112,5 +112,5 @@ | ||
- The state you want to set or a function to set it | ||
- The mutation behavior of this one `setState` call (optional) | ||
- The `ignoreAction` boolean (optional) | ||
- The state you want to set or a function to set it | ||
- The mutation behavior of this one `setState` call (optional) | ||
- The `ignoreAction` boolean (optional) | ||
@@ -124,5 +124,5 @@ The mutation behavior is described below. Normally, the mutation behavior is a global value, but you can alter it within individual state updates if you want. | ||
```js | ||
const [yourState, setYourState, { undo, redo }] = useUndoable(); | ||
const [yourState, setYourState, { undo, redo }] = useUndoable() | ||
setYourState(yourState + 1, null, true); | ||
setYourState(yourState + 1, null, true) | ||
``` | ||
@@ -174,7 +174,11 @@ | ||
interface Options { | ||
behavior?: 'mergePastReversed' | 'mergePast' | 'destroyFuture' | 'keepFuture'; | ||
historyLimit?: number | 'infinium' | 'infinity'; | ||
ignoreIdenticalMutations?: boolean; | ||
cloneState?: boolean; | ||
}; | ||
behavior?: | ||
| "mergePastReversed" | ||
| "mergePast" | ||
| "destroyFuture" | ||
| "keepFuture" | ||
historyLimit?: number | "infinium" | "infinity" | ||
ignoreIdenticalMutations?: boolean | ||
cloneState?: boolean | ||
} | ||
``` | ||
@@ -196,3 +200,3 @@ | ||
You can customize the behavior of undo/redo actions by specifying one of the following: `mergePastReversed`, `mergePast`, `destroyFuture`, or `keepFuture` | ||
You can customize the behavior of undo/redo actions by specifying one of the following: `mergePastReversed`, `mergePast`, `destroyFuture`, or `keepFuture` | ||
@@ -231,3 +235,3 @@ To describe these, let's go through an example. | ||
Great. This is the starting point for the behavior. | ||
Great. This is the starting point for the behavior. | ||
@@ -270,3 +274,2 @@ Calling `undo()` essentially creates a new branch of state changes. The `behavior` specifies how to recover from _after_ a state change that followed an `undo()`. | ||
Therefore, contrary to the `destroyFuture` option, the resulting object would look like: | ||
@@ -288,17 +291,17 @@ | ||
const [ | ||
state, | ||
setState, | ||
state, | ||
setState, | ||
{ | ||
past, | ||
future, | ||
{ | ||
past, | ||
future, | ||
undo, | ||
canUndo, | ||
redo, | ||
canRedo, | ||
reset, | ||
static_setState, | ||
}, | ||
] = useUndoable(initialState, options); | ||
undo, | ||
canUndo, | ||
redo, | ||
canRedo, | ||
reset, | ||
static_setState, | ||
}, | ||
] = useUndoable(initialState, options) | ||
``` | ||
@@ -404,23 +407,23 @@ | ||
const [ | ||
count, | ||
setCount, | ||
{ | ||
past: currentPast, | ||
future: currentFuture, | ||
count, | ||
setCount, | ||
{ | ||
past: currentPast, | ||
future: currentFuture, | ||
undo: undoWithCustomName, | ||
canUndo: canUndoWithCustomName, | ||
redo: redoWithCustomName, | ||
canRedo: canRedoWithCustomName, | ||
reset: deleteItAll, | ||
}, | ||
undo: undoWithCustomName, | ||
canUndo: canUndoWithCustomName, | ||
redo: redoWithCustomName, | ||
canRedo: canRedoWithCustomName, | ||
reset: deleteItAll, | ||
}, | ||
] = useUndoable([ | ||
{ | ||
count: 1, | ||
}, | ||
]); | ||
{ | ||
count: 1, | ||
}, | ||
]) | ||
deleteItAll({ | ||
state: 'My new state', | ||
}); | ||
state: "My new state", | ||
}) | ||
``` | ||
@@ -446,4 +449,4 @@ | ||
- You have Node A with position 0, 0 | ||
- You move Node A to position 100, 100 | ||
- You have Node A with position 0, 0 | ||
- You move Node A to position 100, 100 | ||
@@ -450,0 +453,0 @@ This is not one state update in React Flow v10. Instead, a `drag` event is fired for, more or less, every unit you move: 0 -> 1 -> 2 -> 3 -> and so on. |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
453
64956
13
537