Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/Cell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,20 @@ export const Cell: React.FC<Types.CellComponentProps> = ({
[setCellDimensions, select, dragging, point]
);

const modeRef = React.useRef<Types.Mode>();
React.useEffect(() => {
modeRef.current = mode;
}, [mode]);

React.useEffect(() => {
const root = rootRef.current;
if (selected && root) {
setCellDimensions(point, getOffsetRect(root));
}
if (root && active && mode === "view") {
if (root && active && modeRef.current === "view") {
root.focus();
}
}, [setCellDimensions, selected, active, mode, point, data]);
}, [setCellDimensions, selected, active, point, data]);

if (data && data.DataViewer) {
// @ts-ignore
Expand Down
19 changes: 9 additions & 10 deletions src/Spreadsheet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -233,17 +233,17 @@ const Spreadsheet = <CellType extends Types.CellBase>(
}, [onActivate, onBlur, state.active]);

// Listen to data changes
const prevDataRef = React.useRef<Matrix.Matrix<CellType>>(state.model.data);
const currentModelDataRef = React.useRef<Matrix.Matrix<CellType>>(
state.model.data
);
React.useEffect(() => {
if (state.model.data !== prevDataRef.current) {
// Call on change only if the data change internal
if (state.model.data !== props.data) {
onChange(state.model.data);
}
}
currentModelDataRef.current = state.model.data;
}, [state.model.data]);

prevDataRef.current = state.model.data;
}, [state.model.data, onChange, props.data]);
React.useEffect(() => {
if (state.lastUpdateDate === null) return;
onChange(currentModelDataRef.current);
}, [state.lastUpdateDate, onChange]);

const prevEvaluatedDataRef = React.useRef<Matrix.Matrix<CellType>>(
state.model.evaluatedData
Expand All @@ -252,7 +252,6 @@ const Spreadsheet = <CellType extends Types.CellBase>(
if (state?.model?.evaluatedData !== prevEvaluatedDataRef?.current) {
onEvaluatedDataChange(state?.model?.evaluatedData);
}

prevEvaluatedDataRef.current = state.model.evaluatedData;
}, [state?.model?.evaluatedData, onEvaluatedDataChange]);

Expand Down
8 changes: 7 additions & 1 deletion src/reducer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,13 @@ describe("reducer", () => {
],
];
test.each(cases)("%s", (name, state, action, expected) => {
expect(reducer(state, action)).toEqual(expected);
if (name === "setCellData") {
// Addressing this case separately since the test may fail due to slight time-related
// differences between the generation of the expected and obtained results.
const result = reducer(state, action);
result.lastUpdateDate = null;
expect(result).toEqual(expected);
} else expect(reducer(state, action)).toEqual(expected);
});
});

Expand Down
5 changes: 5 additions & 0 deletions src/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export const INITIAL_STATE: Types.StoreState = {
rowDimensions: {},
columnDimensions: {},
lastChanged: null,
lastUpdateDate: null,
hasPasted: false,
cut: false,
dragging: false,
Expand Down Expand Up @@ -132,6 +133,7 @@ export default function reducer(
...state,
model: updateCellValue(state.model, active, cellData),
lastChanged: active,
lastUpdateDate: new Date(),
};
}
case Actions.SET_CELL_DIMENSIONS: {
Expand Down Expand Up @@ -206,6 +208,7 @@ export default function reducer(
hasPasted: true,
mode: "view",
lastCommit: commit,
lastUpdateDate: new Date(),
};
}

Expand Down Expand Up @@ -276,6 +279,7 @@ export default function reducer(
hasPasted: true,
mode: "view",
lastCommit: acc.commit,
lastUpdateDate: new Date(),
};
}

Expand Down Expand Up @@ -385,6 +389,7 @@ function clear(state: Types.StoreState): Types.StoreState {
...state,
model: new Model(createFormulaParser, newData),
...commit(changes),
lastUpdateDate: new Date(),
};
}

Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export type StoreState<Cell extends CellBase = CellBase> = {
>;
dragging: boolean;
lastChanged: Point | null;
lastUpdateDate: Date | null;
lastCommit: null | CellChange<Cell>[];
};

Expand Down
1 change: 1 addition & 0 deletions src/util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ const EXAMPLE_STATE: Types.StoreState = {
},
},
lastChanged: null,
lastUpdateDate: null,
hasPasted: false,
cut: false,
dragging: false,
Expand Down