Skip to content

Commit

Permalink
refactor: Organize code and add batch function
Browse files Browse the repository at this point in the history
  • Loading branch information
surajshetty3416 committed Dec 11, 2024
1 parent 66f41c2 commit ea3de6b
Showing 1 changed file with 51 additions and 50 deletions.
101 changes: 51 additions & 50 deletions frontend/src/utils/useCanvasHistory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,15 @@ type CanvasState = {
block: string;
selectedBlockIds: string[];
};

type PauseId = string & { __brand: "PauseId" };

const CAPACITY = 200;
const DEBOUNCE_DELAY = 200;
const DEBOUNCE_DELAY = 100;

export function useCanvasHistory(source: Ref<Block>, selectedBlockIds: Ref<string[]>) {
const undoStack = ref([]) as Ref<CanvasState[]>;
const redoStack = ref([]) as Ref<CanvasState[]>;
const last = ref(createHistoryRecord(source, selectedBlockIds));
const last = ref(createHistoryRecord());
const pauseIdSet = new Set<PauseId>();

const {
Expand All @@ -32,20 +31,6 @@ export function useCanvasHistory(source: Ref<Block>, selectedBlockIds: Ref<strin
resume: resumeSelectionWatcher,
} = pausableFilter();

function commit() {
// console.log("committing...");
undoStack.value.unshift(last.value);
last.value = createHistoryRecord(source, selectedBlockIds);
if (undoStack.value.length > CAPACITY) {
undoStack.value.splice(CAPACITY, Number.POSITIVE_INFINITY);
}
if (redoStack.value.length) {
redoStack.value.splice(0, redoStack.value.length);
}
}

// const debouncedCommit = useDebounceFn(commit, DEBOUNCE_DELAY);

const {
ignoreUpdates: ignoreBlockUpdates,
ignorePrevAsyncUpdates: ignorePrevAsyncBlockUpdates,
Expand All @@ -66,6 +51,30 @@ export function useCanvasHistory(source: Ref<Block>, selectedBlockIds: Ref<strin
eventFilter: selectionWatherFilter,
});

function commit() {
undoStack.value.unshift(last.value);
last.value = createHistoryRecord();
if (undoStack.value.length > CAPACITY) {
undoStack.value.splice(CAPACITY, Number.POSITIVE_INFINITY);
}
if (redoStack.value.length) {
redoStack.value.splice(0, redoStack.value.length);
}
}

function updateSelections() {
nextTick(() => {
last.value.selectedBlockIds = [...selectedBlockIds.value];
});
}

function createHistoryRecord() {
return {
block: getBlockString(source.value),
selectedBlockIds: selectedBlockIds.value,
};
}

function setSource(value: CanvasState) {
ignorePrevAsyncBlockUpdates();
ignoreBlockUpdates(() => {
Expand All @@ -86,6 +95,10 @@ export function useCanvasHistory(source: Ref<Block>, selectedBlockIds: Ref<strin
}
}

function canUndo() {
return undoStack.value.length > 0;
}

function redo() {
const state = redoStack.value.shift();
if (state) {
Expand All @@ -94,6 +107,15 @@ export function useCanvasHistory(source: Ref<Block>, selectedBlockIds: Ref<strin
}
}

function canRedo() {
return redoStack.value.length > 0;
}

function stop() {
stopBlockWatcher();
stopSelectedBlockUpdates();
}

const clear = () => {
undoStack.value.splice(0, undoStack.value.length);
redoStack.value.splice(0, redoStack.value.length);
Expand All @@ -104,29 +126,19 @@ export function useCanvasHistory(source: Ref<Block>, selectedBlockIds: Ref<strin
clear();
}

function canUndo() {
return undoStack.value.length > 0;
}

function canRedo() {
return redoStack.value.length > 0;
}

function updateSelections() {
nextTick(() => {
last.value.selectedBlockIds = [...selectedBlockIds.value];
});
}

function pause() {
pauseBlockWatcher();
pauseSelectionWatcher();
const pauseId = generateId() as PauseId;
pauseIdSet.add(pauseId);
// console.log("\npausing...", pauseId);
return pauseId as PauseId;
}

function resumeTracking() {
resumeBlockWatcher();
resumeSelectionWatcher();
}

function resume(pauseId?: PauseId, commitNow?: boolean, force?: boolean) {
nextTick(() => {
// console.log("resuming...", pauseId);
Expand All @@ -144,34 +156,23 @@ export function useCanvasHistory(source: Ref<Block>, selectedBlockIds: Ref<strin
});
}

function resumeTracking() {
resumeBlockWatcher();
resumeSelectionWatcher();
}

function stop() {
stopBlockWatcher();
stopSelectedBlockUpdates();
function batch(callback: () => void) {
const pauseId = pause();
callback();
resume(pauseId, true);
}

return {
undo,
redo,
dispose,
pause,
resume,
batch,
canUndo,
canRedo,
isTracking,
batch: () => {},
dispose,
undoStack,
redoStack,
};
}

function createHistoryRecord(source: Ref<Block>, selectedBlockIds: Ref<string[]>) {
return {
block: getBlockString(source.value),
selectedBlockIds: selectedBlockIds.value,
isTracking,
};
}

0 comments on commit ea3de6b

Please sign in to comment.