Skip to content

Commit

Permalink
make ui feature plugin :sad
Browse files Browse the repository at this point in the history
  • Loading branch information
pro-odoo committed Apr 10, 2024
1 parent 3615c6b commit 496a4ff
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 46 deletions.
47 changes: 2 additions & 45 deletions src/actions/menu_items_actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@ import {
import { centerFigurePosition, getMaxFigureSize } from "../helpers/figures/figure/figure";
import {
areZonesContinuous,
getFullReference,
getZoneArea,
isConsecutive,
isEqual,
largeMax,
largeMin,
numberToLetters,
zoneToXc,
} from "../helpers/index";
import { DEFAULT_TABLE_CONFIG } from "../helpers/table_presets";
import { interactivePaste, interactivePasteFromOS } from "../helpers/ui/paste_interactive";
Expand Down Expand Up @@ -408,51 +406,10 @@ export const CREATE_CHART = (env: SpreadsheetChildEnv) => {
//------------------------------------------------------------------------------

export const CREATE_PIVOT = (env: SpreadsheetChildEnv) => {
const getters = env.model.getters;
const pivotId = env.model.uuidGenerator.uuidv4();

if (getZoneArea(getters.getSelectedZone()) === 1) {
env.model.selection.selectTableAroundSelection();
}

const range = getFullReference(
getters.getSheetName(getters.getActiveSheetId()),
zoneToXc(getters.getSelectedZone())
);

//TODOPRO Ui feature because of history
const result = env.model.dispatch("ADD_PIVOT", {
pivotId,
pivot: {
range,
columns: [],
rows: [],
measures: [],
name: _t("New pivot"),
type: "SPREADSHEET",
},
});
const newSheetId = env.model.uuidGenerator.uuidv4();
const result = env.model.dispatch("INSERT_NEW_PIVOT", { pivotId, newSheetId });
if (result.isSuccessful) {
const sheetId = env.model.uuidGenerator.uuidv4();
const position =
getters.getSheetIds().findIndex((sheetId) => sheetId === getters.getActiveSheetId()) + 1;
const formulaId = getters.getPivotFormulaId(pivotId);
env.model.dispatch("CREATE_SHEET", {
sheetId,
name: _t("Pivot #%s", formulaId),
position,
});
env.model.dispatch("ACTIVATE_SHEET", {
sheetIdFrom: getters.getActiveSheetId(),
sheetIdTo: sheetId,
});
env.model.dispatch("UPDATE_CELL", {
sheetId,
col: 0,
row: 0,
content: `=PIVOT("${formulaId}")`,
});

env.openSidePanel("PivotSidePanel", { pivotId });
}
};
Expand Down
2 changes: 2 additions & 0 deletions src/plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import {
FindAndReplacePlugin,
FormatPlugin,
HeaderVisibilityUIPlugin,
InsertPivotPlugin,
SheetUIPlugin,
SortPlugin,
UIOptionsPlugin,
Expand Down Expand Up @@ -79,6 +80,7 @@ export const featurePluginRegistry = new Registry<UIPluginConstructor>()
.add("sort", SortPlugin)
.add("automatic_sum", AutomaticSumPlugin)
.add("format", FormatPlugin)
.add("insert_pivot", InsertPivotPlugin)
.add("split_to_columns", SplitToColumnsPlugin)
.add("collaborative", CollaborativePlugin)
.add("history", HistoryPlugin)
Expand Down
1 change: 1 addition & 0 deletions src/plugins/ui_feature/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export * from "./data_cleanup";
export * from "./find_and_replace";
export * from "./format";
export * from "./header_visibility_ui";
export * from "./insert_pivot";
export * from "./sort";
export * from "./ui_options";
export * from "./ui_sheet";
62 changes: 62 additions & 0 deletions src/plugins/ui_feature/insert_pivot.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { getFullReference } from "../../helpers";
import { getZoneArea, zoneToXc } from "../../helpers/zones";
import { _t } from "../../translation";
import { UID } from "../../types";
import { Command } from "../../types/commands";
import { UIPlugin } from "../ui_plugin";

export class InsertPivotPlugin extends UIPlugin {
static getters = [] as const;

handle(cmd: Command) {
switch (cmd.type) {
case "INSERT_NEW_PIVOT":
this.insertNewPivot(cmd.pivotId, cmd.newSheetId);
break;
}
}

insertNewPivot(pivotId: UID, sheetId: UID) {
if (getZoneArea(this.getters.getSelectedZone()) === 1) {
this.selection.selectTableAroundSelection();
}

const range = getFullReference(
this.getters.getSheetName(this.getters.getActiveSheetId()),
zoneToXc(this.getters.getSelectedZone())
);

this.dispatch("ADD_PIVOT", {
pivotId,
pivot: {
range,
columns: [],
rows: [],
measures: [],
name: _t("New pivot"),
type: "SPREADSHEET", //TODOPRO Should support all types of pivots
},
});

const position =
this.getters
.getSheetIds()
.findIndex((sheetId) => sheetId === this.getters.getActiveSheetId()) + 1;
const formulaId = this.getters.getPivotFormulaId(pivotId);
this.dispatch("CREATE_SHEET", {
sheetId,
name: _t("Pivot #%s", formulaId),
position,
});
this.dispatch("ACTIVATE_SHEET", {
sheetIdFrom: this.getters.getActiveSheetId(),
sheetIdTo: sheetId,
});
this.dispatch("UPDATE_CELL", {
sheetId,
col: 0,
row: 0,
content: `=PIVOT("${formulaId}")`,
});
}
}
9 changes: 8 additions & 1 deletion src/types/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -912,6 +912,12 @@ export interface RefreshPivotCommand {
id: UID;
}

export interface InsertNewPivotCommand {
type: "INSERT_NEW_PIVOT";
pivotId: UID;
newSheetId: UID;
}

export type CoreCommand =
// /** History */
// | SelectiveUndoCommand
Expand Down Expand Up @@ -1052,7 +1058,8 @@ export type LocalCommand =
| RemoveDuplicatesCommand
| TrimWhitespaceCommand
| RenderCanvasCommand
| RefreshPivotCommand;
| RefreshPivotCommand
| InsertNewPivotCommand;

export type Command = CoreCommand | LocalCommand;

Expand Down

0 comments on commit 496a4ff

Please sign in to comment.