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
8 changes: 8 additions & 0 deletions src/components/grid/grid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -911,4 +911,12 @@ export class Grid extends Component<Props, SpreadsheetChildEnv> {
get displaySelectionHandler() {
return this.env.isMobile() && this.composerFocusStore.activeComposer.editionMode === "inactive";
}

get displayTableResizer() {
return (
this.env.model.getters.isGridSelectionActive() &&
!this.env.model.getters.isReadonly() &&
!this.env.model.getters.isSheetLocked(this.env.model.getters.getActiveSheetId())
);
}
}
2 changes: 1 addition & 1 deletion src/components/grid/grid.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
onClose="() => this.closeMenu()"
/>
<t
t-if="!this.env.model.getters.isReadonly()"
t-if="this.displayTableResizer"
t-foreach="this.staticTables"
t-as="table"
t-key="table.id">
Expand Down
5 changes: 0 additions & 5 deletions src/components/tables/table_resizer/table_resizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,6 @@ export class TableResizer extends Component<Props, SpreadsheetChildEnv> {

get containerStyle(): string {
const tableZone = this.props.table.range.zone;
const sheetId = this.props.table.range.sheetId;

if (this.env.model.getters.isReadonly() || this.env.model.getters.isSheetLocked(sheetId)) {
return cssPropertiesToCss({ display: "none" });
}
const bottomRight = { ...tableZone, left: tableZone.right, top: tableZone.bottom };
const rect = this.env.model.getters.getVisibleRect(bottomRight);
if (rect.height === 0 || rect.width === 0) {
Expand Down
23 changes: 21 additions & 2 deletions src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ import {
statefulUIPluginRegistry,
} from "./plugins/plugin_registries";
import { UIPlugin, UIPluginConfig, UIPluginConstructor } from "./plugins/ui_plugin";
import { SelectionStreamProcessorImpl } from "./selection_stream/selection_stream_processor";
import {
selectionModifiers,
SelectionStreamProcessorImpl,
} from "./selection_stream/selection_stream_processor";
import { StateObserver } from "./state_observer";
import { _t, setDefaultTranslationMethod } from "./translation";
import { StateUpdateMessage } from "./types/collaborative/transport_service";
Expand Down Expand Up @@ -121,6 +124,7 @@ export class Model extends EventBus<any> implements CommandDispatcher {
private state: StateObserver;

readonly selection: SelectionStreamProcessor;
readonly pluginSelection: SelectionStreamProcessor;

/**
* Getters are the main way the rest of the UI read data from the model. Also,
Expand Down Expand Up @@ -187,6 +191,21 @@ export class Model extends EventBus<any> implements CommandDispatcher {

// Initiate stream processor
this.selection = new SelectionStreamProcessorImpl(this.getters);
this.pluginSelection = new Proxy(this.selection, {
observable: false,
get: (target, prop, receiver) => {
const value = Reflect.get(target, prop, receiver);
if (typeof value !== "function" || !selectionModifiers.has(prop)) {
return value;
}
return (...args: unknown[]) => {
if (!this.getters.isGridSelectionActive()) {
this.selection.getBackToDefault();
}
return (value as Function).apply(target, args);
};
},
} as ProxyHandler<SelectionStreamProcessorImpl>);

this.coreHandlers.push(this.range);
this.handlers.push(this.range);
Expand Down Expand Up @@ -433,7 +452,7 @@ export class Model extends EventBus<any> implements CommandDispatcher {
stateObserver: this.state,
dispatch: this.dispatch,
canDispatch: this.canDispatch,
selection: this.selection,
selection: this.pluginSelection,
moveClient: this.session.move.bind(this.session),
custom: this.config.custom,
uiActions: this.config,
Expand Down
16 changes: 16 additions & 0 deletions src/selection_stream/selection_stream_processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -677,3 +677,19 @@ export class SelectionStreamProcessorImpl implements SelectionStreamProcessor {
);
}
}

export const selectionModifiers: Set<string | symbol> = new Set([
"selectZone",
"selectCell",
"moveAnchorCell",
"updateAnchorCell",
"setAnchorCorner",
"addCellToSelection",
"resizeAnchorZone",
"selectColumn",
"selectRow",
"selectAll",
"loopSelection",
"selectTableAroundSelection",
"commitSelection",
]);
37 changes: 37 additions & 0 deletions tests/model/model.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -420,4 +420,41 @@ describe("Model", () => {
type: "SNAPSHOT",
});
});

test("Selection Processor falls back to gridSelection when invoked from the plugins", () => {
class MyUIPlugin extends UIPlugin {
handle(cmd: Command) {
// @ts-ignore
if (cmd.type === "CAPTURE") {
this.selection.capture(
this,
{
cell: { col: 0, row: 0 },
zone: toZone("A1"),
},
{
handleEvent: () => {
throw new Error("Should not be called");
},
}
);
}
// @ts-ignore
else if (cmd.type === "MOVE") {
this.selection.selectCell(0, 1);
}
}
}
addTestPlugin(featurePluginRegistry, MyUIPlugin);
const model = new Model();
expect(model.getters.isGridSelectionActive()).toBe(true);
expect(model.getters.getSelectedZone()).toEqual(toZone("A1"));
// @ts-ignore
model.dispatch("CAPTURE");
expect(model.getters.isGridSelectionActive()).toBe(false);
// @ts-ignore
model.dispatch("MOVE");
expect(model.getters.isGridSelectionActive()).toBe(true);
expect(model.getters.getSelectedZone()).toEqual(toZone("A2"));
});
});
11 changes: 11 additions & 0 deletions tests/table/table_resizer_component.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Model, UID } from "../../src";
import { CellComposerStore } from "../../src/components/composer/composer/cell_composer_store";
import { Grid } from "../../src/components/grid/grid";
import { DEFAULT_CELL_HEIGHT, DEFAULT_CELL_WIDTH } from "../../src/constants";
import { toZone, zoneToXc } from "../../src/helpers/zones";
Expand Down Expand Up @@ -114,4 +115,14 @@ describe("Table resizer component", () => {
await nextTick();
expect(".o-table-resizer").toHaveCount(0);
});

test("table resizer is not loaded when the grid selection is not active", async () => {
createTable(model, "A1:B2");
await nextTick();
expect(".o-table-resizer").toHaveCount(1);
// start the edition and steal the selection from the grid
env.getStore(CellComposerStore).startEdition();
await nextTick();
expect(".o-table-resizer").toHaveCount(0);
});
});