Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Apply VueUse composables in Vue 3/Nuxt projects to replace custom implementations with battle-tested utilities.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/useManualRefHistory.md
1---2category: State3related: useRefHistory4---56# useManualRefHistory78Manually track the change history of a ref when the using calls `commit()`, also provides undo and redo functionality910## Usage1112```ts {5} twoslash include usage13import { useManualRefHistory } from '@vueuse/core'14import { shallowRef } from 'vue'1516const counter = shallowRef(0)17const { history, commit, undo, redo } = useManualRefHistory(counter)1819counter.value += 120commit()2122console.log(history.value)23/* [24{ snapshot: 1, timestamp: 1601912898062 },25{ snapshot: 0, timestamp: 1601912898061 }26] */27```2829You can use `undo` to reset the ref value to the last history point.3031```ts32// @include: usage33// ---cut---34console.log(counter.value) // 135undo()36console.log(counter.value) // 037```3839#### History of mutable objects4041If you are going to mutate the source, you need to pass a custom clone function or use `clone` `true` as a param, that is a shortcut for a minimal clone function `x => JSON.parse(JSON.stringify(x))` that will be used in both `dump` and `parse`.4243```ts {5}44import { useManualRefHistory } from '@vueuse/core'45import { ref } from 'vue'4647const counter = ref({ foo: 1, bar: 2 })48const { history, commit, undo, redo } = useManualRefHistory(counter, { clone: true })4950counter.value.foo += 151commit()52```5354#### Custom Clone Function5556To use a full featured or custom clone function, you can set up via the `clone` options.5758For example, using [structuredClone](https://developer.mozilla.org/en-US/docs/Web/API/structuredClone):5960```ts61import { useManualRefHistory } from '@vueuse/core'6263const refHistory = useManualRefHistory(target, { clone: structuredClone })64```6566Or by using [lodash's `cloneDeep`](https://lodash.com/docs/4.17.15#cloneDeep):6768```ts69import { useManualRefHistory } from '@vueuse/core'70import { cloneDeep } from 'lodash-es'7172const refHistory = useManualRefHistory(target, { clone: cloneDeep })73```7475Or a more lightweight [`klona`](https://github.com/lukeed/klona):7677```ts78import { useManualRefHistory } from '@vueuse/core'79import { klona } from 'klona'8081const refHistory = useManualRefHistory(target, { clone: klona })82```8384#### Custom Dump and Parse Function8586Instead of using the `clone` options, you can pass custom functions to control the serialization and parsing. In case you do not need history values to be objects, this can save an extra clone when undoing. It is also useful in case you want to have the snapshots already stringified to be saved to local storage for example.8788```ts89import { useManualRefHistory } from '@vueuse/core'9091const refHistory = useManualRefHistory(target, {92dump: JSON.stringify,93parse: JSON.parse,94})95```9697### History Capacity9899We will keep all the history by default (unlimited) until you explicitly clear them up, you can set the maximal amount of history to be kept by `capacity` options.100101```ts102import { useManualRefHistory } from '@vueuse/core'103104const refHistory = useManualRefHistory(target, {105capacity: 15, // limit to 15 history records106})107108refHistory.clear() // explicitly clear all the history109```110111## Type Declarations112113```ts114export interface UseRefHistoryRecord<T> {115snapshot: T116timestamp: number117}118export interface UseManualRefHistoryOptions<Raw, Serialized = Raw> {119/**120* Maximum number of history to be kept. Default to unlimited.121*/122capacity?: number123/**124* Clone when taking a snapshot, shortcut for dump: JSON.parse(JSON.stringify(value)).125* Default to false126*127* @default false128*/129clone?: boolean | CloneFn<Raw>130/**131* Serialize data into the history132*/133dump?: (v: Raw) => Serialized134/**135* Deserialize data from the history136*/137parse?: (v: Serialized) => Raw138/**139* set data source140*/141setSource?: (source: Ref<Raw>, v: Raw) => void142}143export interface UseManualRefHistoryReturn<Raw, Serialized> {144/**145* Bypassed tracking ref from the argument146*/147source: Ref<Raw>148/**149* An array of history records for undo, newest comes to first150*/151history: Ref<UseRefHistoryRecord<Serialized>[]>152/**153* Last history point, source can be different if paused154*/155last: Ref<UseRefHistoryRecord<Serialized>>156/**157* Same as {@link UseManualRefHistoryReturn.history | history}158*/159undoStack: Ref<UseRefHistoryRecord<Serialized>[]>160/**161* Records array for redo162*/163redoStack: Ref<UseRefHistoryRecord<Serialized>[]>164/**165* A ref representing if undo is possible (non empty undoStack)166*/167canUndo: ComputedRef<boolean>168/**169* A ref representing if redo is possible (non empty redoStack)170*/171canRedo: ComputedRef<boolean>172/**173* Undo changes174*/175undo: () => void176/**177* Redo changes178*/179redo: () => void180/**181* Clear all the history182*/183clear: () => void184/**185* Create a new history record186*/187commit: () => void188/**189* Reset ref's value with latest history190*/191reset: () => void192}193/**194* Track the change history of a ref, also provides undo and redo functionality.195*196* @see https://vueuse.org/useManualRefHistory197* @param source198* @param options199*/200export declare function useManualRefHistory<Raw, Serialized = Raw>(201source: Ref<Raw>,202options?: UseManualRefHistoryOptions<Raw, Serialized>,203): UseManualRefHistoryReturn<Raw, Serialized>204```205