Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add onPaste event and a flag to control the missing rows addition when pasting a data #176

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
22 changes: 20 additions & 2 deletions src/Spreadsheet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ export type Props<CellType extends Types.CellBase> = {
* Defaults to: `false`.
*/
hideColumnIndicators?: boolean;
/**
* If set to true, automatically creates missing rows when pasting from clipboard
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about missing columns?

* Defaults to: `true`
*/
autoPadRowsOnPaste?: boolean;
// Custom Components
/** Component rendered above each column. */
ColumnIndicator?: Types.ColumnIndicatorComponent;
Expand Down Expand Up @@ -105,6 +110,8 @@ export type Props<CellType extends Types.CellBase> = {
onSelect?: (selected: Point.Point[]) => void;
/** Callback called when Spreadsheet's active cell changes. */
onActivate?: (active: Point.Point) => void;
/** Callback called when Spreadhseet data pasted */
onPaste?: (selected: Point.Point[]) => void;
/** Callback called when the Spreadsheet loses focus */
onBlur?: () => void;
onCellCommit?: (
Expand All @@ -127,6 +134,7 @@ const Spreadsheet = <CellType extends Types.CellBase>(
rowLabels,
hideColumnIndicators,
hideRowIndicators,
autoPadRowsOnPaste = true,
onKeyDown,
Table = DefaultTable,
Row = DefaultRow,
Expand All @@ -140,6 +148,7 @@ const Spreadsheet = <CellType extends Types.CellBase>(
onChange = () => {},
onModeChange = () => {},
onSelect = () => {},
onPaste = () => {},
onActivate = () => {},
onBlur = () => {},
onCellCommit = () => {},
Expand Down Expand Up @@ -168,6 +177,7 @@ const Spreadsheet = <CellType extends Types.CellBase>(
const prevStateRef = React.useRef<Types.StoreState<CellType>>({
...INITIAL_STATE,
data: props.data,
pasted: null,
selected: null,
copied: PointMap.from([]),
bindings: PointMap.from([]),
Expand All @@ -177,8 +187,8 @@ const Spreadsheet = <CellType extends Types.CellBase>(
const copy = React.useCallback(() => dispatch(Actions.copy()), [dispatch]);
const cut = React.useCallback(() => dispatch(Actions.cut()), [dispatch]);
const paste = React.useCallback(
(data) => dispatch(Actions.paste(data)),
[dispatch]
(data) => dispatch(Actions.paste(data, autoPadRowsOnPaste)),
[dispatch, autoPadRowsOnPaste]
);
const onKeyDownAction = React.useCallback(
(event) => dispatch(Actions.keyDown(event)),
Expand Down Expand Up @@ -228,6 +238,13 @@ const Spreadsheet = <CellType extends Types.CellBase>(
onSelect(points);
}

if (state.pasted !== prevState.pasted) {
const points = state.pasted
? Array.from(PointRange.iterate(state.pasted))
: [];
onPaste(points);
}

if (state.active !== prevState.active) {
if (state.active) {
onActivate(state.active);
Expand All @@ -250,6 +267,7 @@ const Spreadsheet = <CellType extends Types.CellBase>(
onChange,
onModeChange,
onSelect,
onPaste,
rowLabels,
columnLabels,
]);
Expand Down
9 changes: 7 additions & 2 deletions src/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,14 @@ export const setCellDimensions = createAction<
export const copy = createAction("COPY");
export const cut = createAction("CUT");
export const paste = createAction<
(data: string) => { payload: { data: string } },
(
data: string,
autoPadRowsOnPaste: boolean
) => { payload: { data: string; autoPadRowsOnPaste: boolean } },
"PASTE"
>("PASTE", (data) => ({ payload: { data } }));
>("PASTE", (data, autoPadRowsOnPaste) => ({
payload: { data, autoPadRowsOnPaste },
}));
export const edit = createAction("EDIT");
export const view = createAction("VIEW");
export const clear = createAction("CLEAR");
Expand Down
22 changes: 15 additions & 7 deletions src/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export const INITIAL_STATE: Types.StoreState = {
dragging: false,
data: [],
selected: null,
pasted: null,
copied: PointMap.from([]),
bindings: PointMap.from([]),
lastCommit: null,
Expand Down Expand Up @@ -110,7 +111,7 @@ const reducer = createReducer(INITIAL_STATE, (builder) => {
};
});
builder.addCase(Actions.paste, (state, action) => {
const { data: text } = action.payload;
const { data: text, autoPadRowsOnPaste } = action.payload;
const { active } = state;
if (!active) {
return;
Expand All @@ -126,8 +127,11 @@ const reducer = createReducer(INITIAL_STATE, (builder) => {
};

const copiedSize = Matrix.getSize(copiedMatrix);
const requiredRows = active.row + copiedSize.rows;
const paddedData = Matrix.padRows(state.data, requiredRows);
const paddedRowsCount = active.row + copiedSize.rows;
const requiredRowsCount = !autoPadRowsOnPaste
? Math.min(paddedRowsCount, state.data.length)
: paddedRowsCount;
const paddedData = Matrix.padRows(state.data, requiredRowsCount);

const { data, commit } = PointMap.reduce<Accumulator, Types.CellBase>(
(acc, value, point) => {
Expand Down Expand Up @@ -165,13 +169,17 @@ const reducer = createReducer(INITIAL_STATE, (builder) => {
copied,
{ data: paddedData, commit: [] }
);

const selectedRange = PointRange.create(active, {
row: autoPadRowsOnPaste ? paddedRowsCount - 1 : requiredRowsCount - 1,
column: active.column + copiedSize.columns - 1,
});

return {
...state,
data,
selected: PointRange.create(active, {
row: active.row + copiedSize.rows - 1,
column: active.column + copiedSize.columns - 1,
}),
selected: selectedRange,
pasted: selectedRange,
cut: false,
hasPasted: true,
mode: "view",
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export type Dimensions = {
export type StoreState<Cell extends CellBase = CellBase> = {
data: Matrix<Cell>;
selected: PointRange | null;
pasted: PointRange | null;
copied: PointMap<Cell>;
hasPasted: boolean;
cut: boolean;
Expand Down
1 change: 1 addition & 0 deletions src/util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ const EXAMPLE_STATE: Types.StoreState = {
dragging: false,
data: EXAMPLE_DATA,
selected: null,
pasted: null,
copied: PointMap.from([]),
bindings: PointMap.from([]),
lastCommit: null,
Expand Down