跳到主要內容

useManualRefHistory

類別
匯出大小
518 B
最近更改
3 週前
相關

當使用者呼叫 commit() 時,手動追蹤 ref 的變更歷史,並提供 undo 和 redo 功能

範例

計數: 0
/

歷史紀錄 (範例限制為 10 筆)
2025-03-10 03:47:02{ value: 0 }

用法

ts
import { useManualRefHistory } from '@vueuse/core'
import { shallowRef } from 'vue'

const counter = shallowRef(0)
const { history, commit, undo, redo } = useManualRefHistory(counter)

counter.value += 1
commit()

console.log(history.value)
/* [
  { snapshot: 1, timestamp: 1601912898062 },
  { snapshot: 0, timestamp: 1601912898061 }
] */

您可以使用 `undo` 將 ref 的值重設為上一個歷史紀錄點。

ts
console.log(counter.value) // 1
undo()
console.log(counter.value) // 0

可變物件的歷史紀錄

如果您要變更來源,您需要傳遞自訂的 clone 函式,或使用 `clone` `true` 作為參數,這是最小 clone 函式 `x => JSON.parse(JSON.stringify(x))` 的捷徑,該函式將用於 `dump` 和 `parse` 中。

ts
import { useManualRefHistory } from '@vueuse/core'
import { ref } from 'vue'

const counter = ref({ foo: 1, bar: 2 })
const { history, commit, undo, redo } = useManualRefHistory(counter, { clone: true })

counter.value.foo += 1
commit()

自訂 Clone 函式

若要使用功能完整或自訂的 clone 函式,您可以透過 `clone` 選項進行設定。

例如,使用 structuredClone

ts
import { useManualRefHistory } from '@vueuse/core'

const refHistory = useManualRefHistory(target, { clone: structuredClone })

或者使用 lodash 的 cloneDeep

ts
import { useManualRefHistory } from '@vueuse/core'
import { cloneDeep } from 'lodash-es'

const refHistory = useManualRefHistory(target, { clone: cloneDeep })

或者更輕量的 klona

ts
import { useManualRefHistory } from '@vueuse/core'
import { klona } from 'klona'

const refHistory = useManualRefHistory(target, { clone: klona })

自訂 Dump 和 Parse 函式

除了使用 `clone` 選項,您可以傳遞自訂函式來控制序列化和解析。如果您不需要歷史紀錄值是物件,這可以在還原時節省額外的 clone。如果您希望快照已經字串化以便儲存到 local storage,這也很有用。

ts
import { useManualRefHistory } from '@vueuse/core'

const refHistory = useManualRefHistory(target, {
  dump: JSON.stringify,
  parse: JSON.parse,
})

歷史紀錄容量

預設情況下,我們會保留所有歷史紀錄 (無限制),直到您明確清除它們,您可以透過 `capacity` 選項設定要保留的最大歷史紀錄量。

ts
const refHistory = useManualRefHistory(target, {
  capacity: 15, // limit to 15 history records
})

refHistory.clear() // explicitly clear all the history

類型宣告

顯示類型宣告
typescript
export interface UseRefHistoryRecord<T> {
  snapshot: T
  timestamp: number
}
export interface UseManualRefHistoryOptions<Raw, Serialized = Raw> {
  /**
   * Maximum number of history to be kept. Default to unlimited.
   */
  capacity?: number
  /**
   * Clone when taking a snapshot, shortcut for dump: JSON.parse(JSON.stringify(value)).
   * Default to false
   *
   * @default false
   */
  clone?: boolean | CloneFn<Raw>
  /**
   * Serialize data into the history
   */
  dump?: (v: Raw) => Serialized
  /**
   * Deserialize data from the history
   */
  parse?: (v: Serialized) => Raw
  /**
   * set data source
   */
  setSource?: (source: Ref<Raw>, v: Raw) => void
}
export interface UseManualRefHistoryReturn<Raw, Serialized> {
  /**
   * Bypassed tracking ref from the argument
   */
  source: Ref<Raw>
  /**
   * An array of history records for undo, newest comes to first
   */
  history: Ref<UseRefHistoryRecord<Serialized>[]>
  /**
   * Last history point, source can be different if paused
   */
  last: Ref<UseRefHistoryRecord<Serialized>>
  /**
   * Same as {@link UseManualRefHistoryReturn.history | history}
   */
  undoStack: Ref<UseRefHistoryRecord<Serialized>[]>
  /**
   * Records array for redo
   */
  redoStack: Ref<UseRefHistoryRecord<Serialized>[]>
  /**
   * A ref representing if undo is possible (non empty undoStack)
   */
  canUndo: ComputedRef<boolean>
  /**
   * A ref representing if redo is possible (non empty redoStack)
   */
  canRedo: ComputedRef<boolean>
  /**
   * Undo changes
   */
  undo: () => void
  /**
   * Redo changes
   */
  redo: () => void
  /**
   * Clear all the history
   */
  clear: () => void
  /**
   * Create a new history record
   */
  commit: () => void
  /**
   * Reset ref's value with latest history
   */
  reset: () => void
}
/**
 * Track the change history of a ref, also provides undo and redo functionality.
 *
 * @see https://vueuse.dev.org.tw/useManualRefHistory
 * @param source
 * @param options
 */
export declare function useManualRefHistory<Raw, Serialized = Raw>(
  source: Ref<Raw>,
  options?: UseManualRefHistoryOptions<Raw, Serialized>,
): UseManualRefHistoryReturn<Raw, Serialized>

原始碼

原始碼範例文件

貢獻者

Anthony Fu
Matias Capeletto
Anthony Fu
IlyaL
trent
Lov`u`e
Егор
丶远方
azaleta
Eduardo Wesley
Sahin D
vaakian X
Hollis Wu
wheat
Alex Kozack

變更日誌

v12.0.0-beta.1 於 11/21/2024
0a9ed - feat!: 移除 Vue 2 支援,優化打包和清理 (#4349)
v11.2.0 於 10/30/2024
b46d2 - fix: canUndocanRedo 類型為計算 ref (#4261)
v10.0.0-beta.5 於 4/13/2023
cb644 - refactor!: 移除 isFunctionisString 工具

以 MIT 授權條款發布。