📄 index.ts¶
📊 Analysis Summary¶
| Metric | Count |
|---|---|
| 🔧 Functions | 12 |
| 📦 Imports | 8 |
| 📊 Variables & Constants | 1 |
| 🟢 Vue Composition API | 3 |
| 📐 Interfaces | 3 |
| 📑 Type Aliases | 1 |
📚 Table of Contents¶
🛠️ File Location:¶
📂 packages/core/useManualRefHistory/index.ts
📦 Imports¶
| Name | Source |
|---|---|
ComputedRef |
vue |
Ref |
vue |
CloneFn |
../useCloned |
timestamp |
@vueuse/shared |
computed |
vue |
deepRef |
vue |
markRaw |
vue |
cloneFnJSON |
../useCloned |
Variables & Constants¶
| Name | Type | Kind | Value | Exported |
|---|---|---|---|---|
last |
Ref<UseRefHistoryRecord<Serialized>> |
const | deepRef(_createHistoryRecord()) as Ref<UseRefHistoryRecord<Serialized>> |
✗ |
Vue Composition API¶
| Name | Type | Reactive Variables | Composables |
|---|---|---|---|
computed |
computed | none | none |
computed |
computed | none | none |
computed |
computed | none | none |
Functions¶
fnBypass(v: F): T¶
- Parameters:
v: F- Return Type:
T
fnSetSource(source: Ref<F>, value: F): F¶
- Parameters:
source: Ref<F>value: F- Return Type:
F
defaultDump(clone: boolean | CloneFn<R>): FnCloneOrBypass<R, S>¶
Code
- Parameters:
clone: boolean | CloneFn<R>- Return Type:
FnCloneOrBypass<R, S>
defaultParse(clone: boolean | CloneFn<R>): FnCloneOrBypass<S, R>¶
Code
- Parameters:
clone: boolean | CloneFn<R>- Return Type:
FnCloneOrBypass<S, R>
useManualRefHistory(source: Ref<Raw>, options: UseManualRefHistoryOptions<Raw, Serialized>): UseManualRefHistoryReturn<Raw, Serialized>¶
Code
export function useManualRefHistory<Raw, Serialized = Raw>(
source: Ref<Raw>,
options: UseManualRefHistoryOptions<Raw, Serialized> = {},
): UseManualRefHistoryReturn<Raw, Serialized> {
const {
clone = false,
dump = defaultDump<Raw, Serialized>(clone),
parse = defaultParse<Raw, Serialized>(clone),
setSource = fnSetSource,
} = options
function _createHistoryRecord(): UseRefHistoryRecord<Serialized> {
return markRaw({
snapshot: dump(source.value),
timestamp: timestamp(),
})
}
const last: Ref<UseRefHistoryRecord<Serialized>> = deepRef(_createHistoryRecord()) as Ref<UseRefHistoryRecord<Serialized>>
const undoStack: Ref<UseRefHistoryRecord<Serialized>[]> = deepRef([])
const redoStack: Ref<UseRefHistoryRecord<Serialized>[]> = deepRef([])
const _setSource = (record: UseRefHistoryRecord<Serialized>) => {
setSource(source, parse(record.snapshot))
last.value = record
}
const commit = () => {
undoStack.value.unshift(last.value)
last.value = _createHistoryRecord()
if (options.capacity && undoStack.value.length > options.capacity)
undoStack.value.splice(options.capacity, Number.POSITIVE_INFINITY)
if (redoStack.value.length)
redoStack.value.splice(0, redoStack.value.length)
}
const clear = () => {
undoStack.value.splice(0, undoStack.value.length)
redoStack.value.splice(0, redoStack.value.length)
}
const undo = () => {
const state = undoStack.value.shift()
if (state) {
redoStack.value.unshift(last.value)
_setSource(state)
}
}
const redo = () => {
const state = redoStack.value.shift()
if (state) {
undoStack.value.unshift(last.value)
_setSource(state)
}
}
const reset = () => {
_setSource(last.value)
}
const history = computed(() => [last.value, ...undoStack.value])
const canUndo = computed(() => undoStack.value.length > 0)
const canRedo = computed(() => redoStack.value.length > 0)
return {
source,
undoStack,
redoStack,
last,
history,
canUndo,
canRedo,
clear,
commit,
reset,
undo,
redo,
}
}
-
JSDoc:
-
Parameters:
source: Ref<Raw>options: UseManualRefHistoryOptions<Raw, Serialized>- Return Type:
UseManualRefHistoryReturn<Raw, Serialized> - Calls:
defaultDumpdefaultParsemarkRaw (from vue)dumptimestamp (from @vueuse/shared)deepRef (from vue)_createHistoryRecordsetSourceparseundoStack.value.unshiftundoStack.value.spliceredoStack.value.spliceundoStack.value.shiftredoStack.value.unshift_setSourceredoStack.value.shiftcomputed (from vue)
_createHistoryRecord(): UseRefHistoryRecord<Serialized>¶
Code
- Return Type:
UseRefHistoryRecord<Serialized> - Calls:
markRaw (from vue)dumptimestamp (from @vueuse/shared)
_setSource(record: UseRefHistoryRecord<Serialized>): void¶
Code
- Parameters:
record: UseRefHistoryRecord<Serialized>- Return Type:
void - Calls:
setSourceparse
commit(): void¶
Code
- Return Type:
void - Calls:
undoStack.value.unshift_createHistoryRecordundoStack.value.spliceredoStack.value.splice
clear(): void¶
Code
- Return Type:
void - Calls:
undoStack.value.spliceredoStack.value.splice
undo(): void¶
Code
- Return Type:
void - Calls:
undoStack.value.shiftredoStack.value.unshift_setSource
redo(): void¶
Code
- Return Type:
void - Calls:
redoStack.value.shiftundoStack.value.unshift_setSource
reset(): void¶
- Return Type:
void - Calls:
_setSource
Interfaces¶
UseRefHistoryRecord<T>¶
Properties¶
| Name | Type | Optional | Description |
|---|---|---|---|
snapshot |
T |
✗ | |
timestamp |
number |
✗ |
UseManualRefHistoryOptions<Raw, Serialized = Raw>¶
Interface Code
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
}
Properties¶
| Name | Type | Optional | Description |
|---|---|---|---|
capacity |
number |
✓ | |
clone |
boolean | CloneFn<Raw> |
✓ | |
dump |
(v: Raw) => Serialized |
✓ | |
parse |
(v: Serialized) => Raw |
✓ | |
setSource |
(source: Ref<Raw>, v: Raw) => void |
✓ |
UseManualRefHistoryReturn<Raw, Serialized>¶
Interface Code
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
}
Properties¶
| Name | Type | Optional | Description |
|---|---|---|---|
source |
Ref<Raw> |
✗ | |
history |
Ref<UseRefHistoryRecord<Serialized>[]> |
✗ | |
last |
Ref<UseRefHistoryRecord<Serialized>> |
✗ | |
undoStack |
Ref<UseRefHistoryRecord<Serialized>[]> |
✗ | |
redoStack |
Ref<UseRefHistoryRecord<Serialized>[]> |
✗ | |
canUndo |
ComputedRef<boolean> |
✗ | |
canRedo |
ComputedRef<boolean> |
✗ | |
undo |
() => void |
✗ | |
redo |
() => void |
✗ | |
clear |
() => void |
✗ | |
commit |
() => void |
✗ | |
reset |
() => void |
✗ |