This repository has been archived by the owner on Feb 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
164 additions
and
43 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,44 @@ | ||
import { computed, reactive } from 'vue' | ||
import { computed, type ComputedRef } from 'vue' | ||
import { Persis } from './persis' | ||
|
||
export const history = reactive<{ | ||
backward: string[] | ||
forward: string[] | ||
}>({ backward: [], forward: [] }) | ||
class History { | ||
info: Persis<{ active: string | null }> | ||
current: ComputedRef<string | null> | ||
|
||
export function historyPush(v: string) { | ||
history.backward.push(v) | ||
history.forward = [] | ||
} | ||
constructor() { | ||
this.info = new Persis({ | ||
active: null | ||
}) | ||
this.current = computed(() => { | ||
return this.info.now().value.active | ||
}) | ||
} | ||
|
||
export const canUndo = computed(() => history.backward.length > 0) | ||
export const canRedo = computed(() => history.forward.length > 0) | ||
push(v: string) { | ||
this.info.change(h => { | ||
h.active = v | ||
}) | ||
} | ||
|
||
export function historyUndo() { | ||
history.forward.push(history.backward.pop()!) | ||
} | ||
canUndo() { | ||
return this.info.canUndo | ||
} | ||
|
||
canRedo() { | ||
return this.info.canRedo | ||
} | ||
|
||
export function historyRedo() { | ||
const v = history.forward.pop()! | ||
history.backward.push(v) | ||
undo() { | ||
this.info.undo() | ||
} | ||
|
||
redo() { | ||
this.info.redo() | ||
} | ||
|
||
move(ofs: number) { | ||
this.info.move(ofs) | ||
} | ||
} | ||
|
||
export const history = new History() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import { produce } from 'immer' | ||
import { shallowRef, type ComputedRef, type ShallowRef, computed } from 'vue' | ||
|
||
type ImmerInfo<T> = [ShallowRef<T>, (updater: (target: T) => void) => void] | ||
|
||
function useImmer<T>(init: T): ImmerInfo<T> { | ||
const state = shallowRef<T>(init) | ||
const update = (updater: (target: T) => void) => { | ||
state.value = produce(state.value, updater) | ||
} | ||
|
||
return [state, update] | ||
} | ||
|
||
export class Persis<T> { | ||
current: ImmerInfo<T> | ||
prev: ImmerInfo<T[]> | ||
next: ImmerInfo<T[]> | ||
|
||
canUndo: ComputedRef<boolean> | ||
canRedo: ComputedRef<boolean> | ||
|
||
constructor(data: T) { | ||
this.current = useImmer(data) | ||
this.prev = useImmer([]) | ||
this.next = useImmer([]) | ||
|
||
this.canUndo = computed(() => { | ||
return this.prev[0].value.length > 0 | ||
}) | ||
this.canRedo = computed(() => { | ||
return this.next[0].value.length > 0 | ||
}) | ||
} | ||
|
||
now() { | ||
return this.current[0] | ||
} | ||
|
||
change(proc: (v: T) => void) { | ||
this.next[0].value = [] | ||
this.prev[1](draft => { | ||
draft.push(this.current[0].value) | ||
}) | ||
this.current[1](proc) | ||
} | ||
|
||
move(offset: number) { | ||
if (offset < 0) { | ||
if (this.prev[0].value.length < -offset) { | ||
return | ||
} | ||
|
||
this.next[1](draft => { | ||
draft.push(this.current[0].value) | ||
if (-offset > 1) { | ||
draft.push(...this.prev[0].value.slice(offset + 1).reverse()) | ||
} | ||
}) | ||
|
||
this.current[0].value = | ||
this.prev[0].value[this.prev[0].value.length + offset] | ||
|
||
this.prev[1](draft => { | ||
draft.splice(offset) | ||
}) | ||
} else if (offset > 0) { | ||
if (this.next[0].value.length < offset) { | ||
return | ||
} | ||
|
||
this.prev[1](draft => { | ||
draft.push(this.current[0].value) | ||
if (offset > 1) { | ||
draft.push(...this.next[0].value.slice(-offset + 1).reverse()) | ||
} | ||
}) | ||
|
||
this.current[0].value = | ||
this.next[0].value[this.next[0].value.length - offset] | ||
|
||
this.next[1](draft => { | ||
draft.splice(-offset) | ||
}) | ||
} | ||
} | ||
|
||
undo() { | ||
if (this.canUndo.value) { | ||
this.move(-1) | ||
} | ||
} | ||
|
||
redo() { | ||
if (this.canRedo.value) { | ||
this.move(1) | ||
} | ||
} | ||
} |