Skip to content

Commit

Permalink
[IMP] highlight: support unbounded zone
Browse files Browse the repository at this point in the history
Previously when moving or resizing highlights, the unbounded property of
the zone was not preserved. This commit uses the unbounded zone as the
parameter of the changing highlight command so this property is kept.

task 3122611

closes #2161

Signed-off-by: Lucas Lefèvre (lul) <lul@odoo.com>
  • Loading branch information
Chenyun Yang authored and rrahir committed Jun 2, 2023
1 parent 98c8b59 commit d39aaaf
Show file tree
Hide file tree
Showing 7 changed files with 355 additions and 82 deletions.
22 changes: 13 additions & 9 deletions src/components/highlight/highlight/highlight.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Component, useState } from "@odoo/owl";
import { ComponentsImportance } from "../../../constants";
import { clip, isEqual } from "../../../helpers";
import { Color, Pixel, SpreadsheetChildEnv, Zone } from "../../../types";
import { Color, HeaderIndex, Pixel, SpreadsheetChildEnv, Zone } from "../../../types";
import { css } from "../../helpers/css";
import { gridOverlayPosition } from "../../helpers/dom_helpers";
import { dragAndDropBeyondTheViewport } from "../../helpers/drag_and_drop";
Expand Down Expand Up @@ -34,7 +34,7 @@ export class Highlight extends Component<Props, SpreadsheetChildEnv> {
});

onResizeHighlight(isLeft: boolean, isTop: boolean) {
const activeSheet = this.env.model.getters.getActiveSheet();
const activeSheetId = this.env.model.getters.getActiveSheetId();

this.highlightState.shiftingMode = "isResizing";
const z = this.props.zone;
Expand All @@ -46,12 +46,11 @@ export class Highlight extends Component<Props, SpreadsheetChildEnv> {
let currentZone = z;

this.env.model.dispatch("START_CHANGE_HIGHLIGHT", {
range: this.env.model.getters.getRangeDataFromZone(activeSheet.id, currentZone),
range: this.env.model.getters.getRangeDataFromZone(activeSheetId, currentZone),
});

const mouseMove = (col, row) => {
const mouseMove = (col: HeaderIndex, row: HeaderIndex) => {
if (lastCol !== col || lastRow !== row) {
const activeSheetId = this.env.model.getters.getActiveSheetId();
lastCol = clip(
col === -1 ? lastCol : col,
0,
Expand All @@ -74,7 +73,10 @@ export class Highlight extends Component<Props, SpreadsheetChildEnv> {

if (!isEqual(newZone, currentZone)) {
this.env.model.dispatch("CHANGE_HIGHLIGHT", {
range: this.env.model.getters.getRangeDataFromZone(activeSheet.id, newZone),
range: this.env.model.getters.getRangeFromZone(
activeSheetId,
this.env.model.getters.getUnboundedZone(activeSheetId, newZone)
).rangeData,
});
currentZone = newZone;
}
Expand Down Expand Up @@ -116,7 +118,7 @@ export class Highlight extends Component<Props, SpreadsheetChildEnv> {
let lastCol = initCol;
let lastRow = initRow;

const mouseMove = (col, row) => {
const mouseMove = (col: HeaderIndex, row: HeaderIndex) => {
if (lastCol !== col || lastRow !== row) {
lastCol = col === -1 ? lastCol : col;
lastRow = row === -1 ? lastRow : row;
Expand All @@ -131,10 +133,12 @@ export class Highlight extends Component<Props, SpreadsheetChildEnv> {
};

newZone = this.env.model.getters.expandZone(activeSheetId, newZone);

if (!isEqual(newZone, currentZone)) {
this.env.model.dispatch("CHANGE_HIGHLIGHT", {
range: this.env.model.getters.getRangeDataFromZone(activeSheetId, newZone),
range: this.env.model.getters.getRangeFromZone(
activeSheetId,
this.env.model.getters.getUnboundedZone(activeSheetId, newZone)
).rangeData,
});
currentZone = newZone;
}
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/core/range.ts
Original file line number Diff line number Diff line change
Expand Up @@ -382,11 +382,11 @@ export class RangeAdapter implements CommandHandler<CoreCommand> {
return this.getters.getRangeFromSheetXC(sheetId, xc).rangeData;
}

getRangeDataFromZone(sheetId: UID, zone: Zone): RangeData {
getRangeDataFromZone(sheetId: UID, zone: Zone | UnboundedZone): RangeData {
return { _sheetId: sheetId, _zone: zone };
}

getRangeFromZone(sheetId: UID, zone: UnboundedZone): Range {
getRangeFromZone(sheetId: UID, zone: Zone | UnboundedZone): Range {
return new RangeImpl(
{
sheetId,
Expand Down
3 changes: 2 additions & 1 deletion src/plugins/core/sheet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -456,8 +456,9 @@ export class SheetPlugin extends CorePlugin<SheetState> implements SheetState {
const isFullCol = zone.top === 0 && zone.bottom === this.getNumberRows(sheetId) - 1;
return {
...zone,
right: isFullRow ? undefined : zone.right,
bottom: isFullCol ? undefined : zone.bottom,
// cannot be unbounded in the 2 dimensions at once
right: isFullRow && !isFullCol ? undefined : zone.right,
};
}

Expand Down
9 changes: 6 additions & 3 deletions src/plugins/ui_stateful/edition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
RemoveColumnsRowsCommand,
UID,
UnboundedZone,
Zone,
} from "../../types";
import { SelectionEvent } from "../../types/event_stream";
import { UIPlugin } from "../ui_plugin";
Expand Down Expand Up @@ -170,7 +171,9 @@ export class EditionPlugin extends UIPlugin {
}
break;
case "START_CHANGE_HIGHLIGHT":
// FIXME: thiws whole ordeal could be handled with the Selection Processor which would extend the feature to selection inputs
this.dispatch("STOP_COMPOSER_RANGE_SELECTION");
// FIXME: we should check range SheetId compared to this.activeSheetId r maybe not have a sheetId in the payload ??
const range = this.getters.getRangeFromRangeData(cmd.range);
const previousRefToken = this.currentTokens
.filter((token) => token.type === "REFERENCE")
Expand Down Expand Up @@ -556,7 +559,7 @@ export class EditionPlugin extends UIPlugin {
}
}

private insertSelectedRange(zone: UnboundedZone) {
private insertSelectedRange(zone: Zone | UnboundedZone) {
// infer if range selected or selecting range from cursor position
const start = Math.min(this.selectionStart, this.selectionEnd);
const ref = this.getZoneReference(zone);
Expand All @@ -571,12 +574,12 @@ export class EditionPlugin extends UIPlugin {
/**
* Replace the current reference selected by the new one.
* */
private replaceSelectedRanges(zone: UnboundedZone) {
private replaceSelectedRanges(zone: Zone | UnboundedZone) {
const ref = this.getZoneReference(zone);
this.replaceText(ref, this.selectionInitialStart, this.selectionEnd);
}

private getZoneReference(zone: UnboundedZone): string {
private getZoneReference(zone: Zone | UnboundedZone): string {
const inputSheetId = this.getters.getCurrentEditedCell().sheetId;
const sheetId = this.getters.getActiveSheetId();
const range = this.getters.getRangeFromZone(sheetId, zone);
Expand Down
1 change: 1 addition & 0 deletions src/types/range.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export interface Range extends Cloneable<Range> {
readonly invalidSheetName?: string;
/** the sheet on which the range is defined */
readonly sheetId: UID;
readonly rangeData: RangeData;
}

export interface RangeData {
Expand Down
Loading

0 comments on commit d39aaaf

Please sign in to comment.