Skip to content

Commit

Permalink
tweak: lint fix
Browse files Browse the repository at this point in the history
  • Loading branch information
pei-pay committed May 18, 2024
1 parent e782340 commit 5dda8a2
Showing 1 changed file with 13 additions and 14 deletions.
27 changes: 13 additions & 14 deletions starter/useRefHistory.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type Ref, ref, watch, markRaw, computed } from 'vue'
import { type Ref, computed, markRaw, ref, watch } from 'vue'

interface UseRefHistoryRecord<T> {
snapshot: T
Expand All @@ -15,11 +15,13 @@ interface UseRefHistoryReturn<Raw> {
export const timestamp = () => +Date.now()

export function useRefHistory<Raw>(source: Ref<Raw>): UseRefHistoryReturn<Raw> {
const ignore = ref(false)
const last: Ref<UseRefHistoryRecord<Raw>> = ref(_createHistoryRecord()) as Ref<UseRefHistoryRecord<Raw>>

function _createHistoryRecord(): UseRefHistoryRecord<Raw> {
return markRaw({
snapshot: source.value,
timestamp: timestamp()
timestamp: timestamp(),
})
}

Expand All @@ -30,36 +32,33 @@ export function useRefHistory<Raw>(source: Ref<Raw>): UseRefHistoryReturn<Raw> {
ignore.value = false
}

const ignore = ref(false)

const last: Ref<UseRefHistoryRecord<Raw>> = ref(_createHistoryRecord()) as Ref<UseRefHistoryRecord<Raw>>

const undoStack: Ref<UseRefHistoryRecord<Raw>[]> = ref([]);
const undoStack: Ref<UseRefHistoryRecord<Raw>[]> = ref([])
const redoStack: Ref<UseRefHistoryRecord<Raw>[]> = ref([])

const undo = () => {
const state = undoStack.value.shift()
if(state) {
if (state) {
redoStack.value.unshift(last.value)
_setSource(state)
}
}
const redo = () => {
const state = redoStack.value.shift();
const state = redoStack.value.shift()

if (state) {
undoStack.value.unshift(last.value);
_setSource(state);
undoStack.value.unshift(last.value)
_setSource(state)
}
}

watch(source, () => {
if(ignore.value) return
if (ignore.value)
return
undoStack.value.unshift(last.value)
last.value = _createHistoryRecord()
if(redoStack.value.length)
if (redoStack.value.length)
redoStack.value.splice(0, redoStack.value.length)
}, { flush: 'sync'} )
}, { flush: 'sync' })

const history = computed(() => [last.value, ...undoStack.value])
const canUndo = computed(() => undoStack.value.length > 0)
Expand Down

0 comments on commit 5dda8a2

Please sign in to comment.