Skip to content

Commit

Permalink
Switch to diffing approach
Browse files Browse the repository at this point in the history
  • Loading branch information
legnes committed Apr 21, 2024
1 parent 1f86609 commit dcd6d80
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 53 deletions.
37 changes: 31 additions & 6 deletions app/reducers/builderReducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { DBPuzzleT } from '../lib/dbtypes';
import { ViewableGrid, ViewableEntry, fromCells } from '../lib/viewableGrid';
import { gridWithEntrySet } from '../lib/gridBase';
import { Timestamp } from '../lib/timestamp';
import equal from 'fast-deep-equal';
import equal from 'fast-deep-equal/es6';
import { getDocId } from '../lib/firebaseWrapper';
import { GridSelection, emptySelection } from '../lib/selection';
import {
Expand All @@ -19,7 +19,7 @@ import {
gridInterfaceReducer,
} from './gridReducer';
import { PuzzleAction } from './commonActions';
import { postEdit, pushToHistory, validateGrid } from './builderUtils';
import { postEdit, validateGrid } from './builderUtils';

export type BuilderEntry = ViewableEntry;
export type BuilderGrid = ViewableGrid<BuilderEntry>;
Expand Down Expand Up @@ -442,6 +442,24 @@ function removeAnswer(answers: string[], toRemove: string): string[] {
return Array.from(updated.values());
}

function pushToHistory(state: BuilderState): BuilderState {
const prevGrid = state.undoHistory[state.undoIndex];
if (equal(prevGrid, state.grid)) {
return state;
}

const MAX_HISTORY_LENGTH = 20;
const start = state.undoIndex === MAX_HISTORY_LENGTH - 1 ? 1 : 0;
const end = state.undoIndex + 1;
const undoHistory = [
...state.undoHistory.slice(start, end),
fromCells(state.grid),
];
const undoIndex = undoHistory.length - 1;

return { ...state, undoHistory, undoIndex };
}

export function getClueProps(
sortedEntries: number[],
entries: ViewableEntry[],
Expand Down Expand Up @@ -483,11 +501,10 @@ export function getClueProps(
return { ac, an, dc, dn };
}

export function builderReducer(
function _builderReducer(
state: BuilderState,
action: PuzzleAction
): BuilderState {
state = gridInterfaceReducer(state, action);
if (isStartSelectionAction(action)) {
return {
...closeRebus(state),
Expand Down Expand Up @@ -518,7 +535,6 @@ export function builderReducer(
}
if (isSetHighlightAction(action)) {
state.grid.highlight = action.highlight;
return pushToHistory(state);
}
if (isSetClueAction(action)) {
const newVal = state.clues[action.word] ?? [];
Expand Down Expand Up @@ -594,7 +610,6 @@ export function builderReducer(
if (isClickedFillAction(action)) {
const grid = gridWithEntrySet(state.grid, action.entryIndex, action.value);
state = postEdit({ ...state, grid }, 0);
state = pushToHistory(state);
return state;
}
if (action.type === 'CLEARPUBLISHERRORS') {
Expand Down Expand Up @@ -791,3 +806,13 @@ export function builderReducer(
}
return state;
}

export function builderReducer(
state: BuilderState,
action: PuzzleAction
): BuilderState {
state = gridInterfaceReducer(state, action);
state = _builderReducer(state, action);
state = pushToHistory(state);
return state;
}
24 changes: 0 additions & 24 deletions app/reducers/builderUtils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import equal from 'fast-deep-equal/es6';
import { entryWord } from '../lib/gridBase';
import { emptySelection, hasMultipleCells } from '../lib/selection';
import { fromCells } from '../lib/viewableGrid';
import type { BuilderState } from './builderReducer';
import type { GridInterfaceState } from './gridReducer';

Expand Down Expand Up @@ -67,25 +65,3 @@ export function clearSelection<T extends GridInterfaceState>(state: T): T {
selection: emptySelection(),
};
}

export function pushToHistory<T extends GridInterfaceState>(state: T): T {
if (!isBuilderState(state)) {
return state;
}

const prevGrid = state.undoHistory[state.undoIndex];
if (equal(prevGrid, state.grid)) {
return state;
}

const MAX_HISTORY_LENGTH = 20;
const start = state.undoIndex === MAX_HISTORY_LENGTH - 1 ? 1 : 0;
const end = state.undoIndex + 1;
const undoHistory = [
...state.undoHistory.slice(start, end),
fromCells(state.grid),
];
const undoIndex = undoHistory.length - 1;

return { ...state, undoHistory, undoIndex };
}
27 changes: 4 additions & 23 deletions app/reducers/gridReducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ import {
postEdit as builderPostEdit,
clearSelection,
hasSelection,
pushToHistory,
} from './builderUtils';
import type { PuzzleState } from './puzzleReducer';
import { isPuzzleState, postEdit as puzzlePostEdit } from './puzzleUtils';
Expand Down Expand Up @@ -244,16 +243,12 @@ export function gridInterfaceReducer<T extends GridInterfaceState>(
state = enterCharAt(state, pos, EMPTY, Symmetry.None);
}
});
if (isCutAction(action)) {
state = pushToHistory(state);
}
} else {
const val = valAt(state.grid, state.active);
if (val !== BLOCK && val !== EMPTY) {
toCopy = val;
if (isCutAction(action)) {
state = enterText(state, EMPTY);
state = pushToHistory(state);
}
}
}
Expand Down Expand Up @@ -295,7 +290,6 @@ export function gridInterfaceReducer<T extends GridInterfaceState>(
}
});
});
state = pushToHistory(state);
state = {
...state,
wasEntryClick: false,
Expand Down Expand Up @@ -336,7 +330,6 @@ export function gridInterfaceReducer<T extends GridInterfaceState>(
} else {
state.grid.highlighted.add(ci);
}
state = pushToHistory(state);
}
return { ...state };
}
Expand All @@ -349,7 +342,7 @@ export function gridInterfaceReducer<T extends GridInterfaceState>(
rebusValue: state.rebusValue ? state.rebusValue.slice(0, -1) : '',
};
} else if (key.k === KeyK.Enter) {
state = {
return {
...closeRebus(state),
wasEntryClick: false,
active: advancePosition(
Expand All @@ -359,8 +352,6 @@ export function gridInterfaceReducer<T extends GridInterfaceState>(
isPuzzleState(state) ? state.prefs : undefined
),
};
state = pushToHistory(state);
return state;
} else if (key.k === KeyK.Escape) {
return { ...state, isEnteringRebus: false, rebusValue: '' };
}
Expand Down Expand Up @@ -526,13 +517,11 @@ export function gridInterfaceReducer<T extends GridInterfaceState>(
const symmetry = isBuilderState(state) ? state.symmetry : Symmetry.None;
const grid = gridWithBlockToggled(state.grid, state.active, symmetry);
state = clearSelection(state);
state = {
return {
...postEdit({ ...state, grid }, ci),
wasEntryClick: false,
active: nextCell(state.grid, state.active),
};
state = pushToHistory(state);
return state;
}
return state;
} else if (key.k === KeyK.Comma && state.grid.allowBlockEditing) {
Expand All @@ -541,18 +530,15 @@ export function gridInterfaceReducer<T extends GridInterfaceState>(
const symmetry = isBuilderState(state) ? state.symmetry : Symmetry.None;
const grid = gridWithBarToggled(state.grid, state.active, symmetry);
state = clearSelection(state);
state = {
return {
...postEdit({ ...state, grid }, ci),
wasEntryClick: false,
};
state = pushToHistory(state);
return state;
}
return state;
} else if (key.k === KeyK.AllowedCharacter) {
const char = key.c.toUpperCase();
state = enterText(state, char);
state = pushToHistory(state);
return {
...state,
wasEntryClick: false,
Expand Down Expand Up @@ -583,7 +569,6 @@ export function gridInterfaceReducer<T extends GridInterfaceState>(
}
}
state = clearSelection(state);
state = pushToHistory(state);
return {
...state,
wasEntryClick: false,
Expand All @@ -609,7 +594,6 @@ export function gridInterfaceReducer<T extends GridInterfaceState>(
}
}
state = clearSelection(state);
state = pushToHistory(state);
return {
...state,
wasEntryClick: false,
Expand All @@ -620,14 +604,11 @@ export function gridInterfaceReducer<T extends GridInterfaceState>(
if (state.isEditable(ci)) {
const symmetry = isBuilderState(state) ? state.symmetry : Symmetry.None;
const grid = gridWithHiddenToggled(state.grid, state.active, symmetry);
state = {
return {
...postEdit({ ...state, grid }, ci),
wasEntryClick: false,
};
state = pushToHistory(state);
return state;
}
return state;
}
}
return state;
Expand Down

0 comments on commit dcd6d80

Please sign in to comment.