Skip to content

Commit 4b5af55

Browse files
committed
Extract and test getCopiedRange from Copied
1 parent 9bf3040 commit 4b5af55

File tree

3 files changed

+38
-13
lines changed

3 files changed

+38
-13
lines changed

src/Copied.tsx

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import * as React from "react";
22
import { connect } from "unistore/react";
33
import * as Types from "./types";
4-
import * as PointSet from "./point-set";
5-
import * as PointMap from "./point-map";
6-
import { getRangeDimensions } from "./util";
4+
import { getCopiedRange, getRangeDimensions } from "./util";
75
import FloatingRect, {
86
Props as FloatingRectProps,
97
StateProps,
@@ -12,19 +10,13 @@ import FloatingRect, {
1210
type Props = Omit<FloatingRectProps, "variant">;
1311

1412
const Copied: React.FC<Props> = (props) => (
15-
<FloatingRect {...props} variant="copied" />
13+
<FloatingRect {...props} variant="copied" dragging={false} />
1614
);
1715

1816
export default connect<{}, {}, Types.StoreState, StateProps>((state) => {
19-
const cells = state.hasPasted
20-
? PointSet.from([])
21-
: PointMap.map(() => true, state.copied);
22-
const hidden = PointSet.size(cells) === 0;
17+
const range = getCopiedRange(state.copied, state.hasPasted);
2318
return {
24-
dimensions: hidden
25-
? null
26-
: getRangeDimensions(state, PointSet.toRange(cells)),
27-
hidden,
28-
dragging: false,
19+
dimensions: range && getRangeDimensions(state, range),
20+
hidden: range === null,
2921
};
3022
})(Copied);

src/util.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import {
2727
getOffsetRect,
2828
readTextFromClipboard,
2929
normalizeSelected,
30+
getCopiedRange,
3031
} from "./util";
3132

3233
const EXAMPLE_INPUT_VALUE = "EXAMPLE_INPUT_VALUE";
@@ -95,6 +96,8 @@ const MOCK_FORMULA_PARSER = {
9596
} as unknown as FormulaParser;
9697
const EXAMPLE_FORMULA_RESULT = true;
9798
const EXAMPLE_FORMULA_ERROR = "EXAMPLE_ERROR";
99+
const EXAMPLE_EMPTY_COPIED = PointMap.from<Types.CellBase>([]);
100+
const EXAMPLE_COPIED = PointMap.from([[Point.ORIGIN, EXAMPLE_CELL]]);
98101

99102
beforeEach(() => {
100103
jest.clearAllMocks();
@@ -449,3 +452,19 @@ describe("normalizeSelected()", () => {
449452
);
450453
});
451454
});
455+
456+
describe("getCopiedRange()", () => {
457+
const cases = [
458+
[
459+
"Returns range of copied cells",
460+
EXAMPLE_COPIED,
461+
false,
462+
PointRange.create(Point.ORIGIN, Point.ORIGIN),
463+
],
464+
["Returns null if none is copied", EXAMPLE_EMPTY_COPIED, false, null],
465+
["Returns null if hasPasted is true", EXAMPLE_COPIED, true, null],
466+
] as const;
467+
test.each(cases)("%s", (name, copied, hasPasted, expected) => {
468+
expect(getCopiedRange(copied, hasPasted)).toEqual(expected);
469+
});
470+
});

src/util.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import * as Types from "./types";
33
import * as Matrix from "./matrix";
44
import * as Point from "./point";
55
import * as PointRange from "./point-range";
6+
import * as PointMap from "./point-map";
7+
import * as PointSet from "./point-set";
68
import * as Formula from "./formula";
79

810
export { createEmpty as createEmptyMatrix } from "./matrix";
@@ -204,3 +206,15 @@ export function calculateSpreadsheetSize(
204206
columns: columnLabels ? Math.max(columns, columnLabels.length) : columns,
205207
};
206208
}
209+
210+
/** Get the range of copied cells. If none are copied return null */
211+
export function getCopiedRange(
212+
copied: Types.StoreState["copied"],
213+
hasPasted: boolean
214+
): PointRange.PointRange | null {
215+
if (hasPasted || PointMap.isEmpty(copied)) {
216+
return null;
217+
}
218+
const set: PointSet.PointSet = PointMap.map(() => true, copied);
219+
return PointSet.toRange(set);
220+
}

0 commit comments

Comments
 (0)