Skip to content

Commit

Permalink
[REF] menu registry: factorize menu registries by menu items
Browse files Browse the repository at this point in the history
Part-of: #2046
  • Loading branch information
laa-odoo authored and LucasLefevre committed Apr 14, 2023
1 parent c02a7e9 commit 950abc4
Show file tree
Hide file tree
Showing 14 changed files with 1,029 additions and 574 deletions.
2 changes: 1 addition & 1 deletion src/registries/menu_items_registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export interface MenuItem {
textColor?: Color;
}

type MenuItemsBuilder = (env: SpreadsheetChildEnv) => MenuItemSpec[];
export type MenuItemsBuilder = (env: SpreadsheetChildEnv) => MenuItemSpec[];
type MenuChildren = (MenuItemSpec | MenuItemsBuilder)[];

export function createMenu(menuItems: MenuItemSpec[]): MenuItem[] {
Expand Down
62 changes: 22 additions & 40 deletions src/registries/menus/cell_menu_registry.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { _lt } from "../../translation";
import { MenuItemRegistry } from "../menu_items_registry";
import * as ACTIONS from "./menu_items_actions";

import * as ACTION_EDIT from "./items/edit_menu_items";
import * as ACTION_INSERT from "./items/insert_menu_items";

//------------------------------------------------------------------------------
// Context Menu Registry
Expand All @@ -10,98 +12,78 @@ export const cellMenuRegistry = new MenuItemRegistry();

cellMenuRegistry
.add("cut", {
name: _lt("Cut"),
description: "Ctrl+X",
...ACTION_EDIT.cutMenuItem,
sequence: 10,
action: ACTIONS.CUT_ACTION,
})
.add("copy", {
name: _lt("Copy"),
description: "Ctrl+C",
...ACTION_EDIT.copyMenuItem,
sequence: 20,
isReadonlyAllowed: true,
action: ACTIONS.COPY_ACTION,
})
.add("paste", {
name: _lt("Paste"),
description: "Ctrl+V",
...ACTION_EDIT.pasteMenuItem,
sequence: 30,
action: ACTIONS.PASTE_ACTION,
})
.add("paste_special", {
name: _lt("Paste special"),
...ACTION_EDIT.pasteSpecialMenuItem,
sequence: 40,
separator: true,
isVisible: ACTIONS.IS_NOT_CUT_OPERATION,
})
.addChild("paste_value_only", ["paste_special"], {
name: _lt("Paste values only"),
...ACTION_EDIT.pasteSpecialValueMenuItem,
sequence: 10,
action: ACTIONS.PASTE_VALUE_ACTION,
})
.addChild("paste_format_only", ["paste_special"], {
name: _lt("Paste format only"),
...ACTION_EDIT.pasteSpecialFormatMenuItem,
sequence: 20,
action: ACTIONS.PASTE_FORMAT_ACTION,
})
.add("add_row_before", {
name: ACTIONS.CELL_INSERT_ROWS_BEFORE_NAME,
...ACTION_INSERT.cellInsertRowsBeforeMenuItem,
sequence: 70,
action: ACTIONS.INSERT_ROWS_BEFORE_ACTION,
isVisible: ACTIONS.IS_ONLY_ONE_RANGE,
})
.add("add_column_before", {
name: ACTIONS.CELL_INSERT_COLUMNS_BEFORE_NAME,
...ACTION_INSERT.cellInsertColsBeforeMenuItem,
sequence: 90,
action: ACTIONS.INSERT_COLUMNS_BEFORE_ACTION,
isVisible: ACTIONS.IS_ONLY_ONE_RANGE,
})
.add("insert_cell", {
name: _lt("Insert cells"),
...ACTION_INSERT.insertCellMenuItem,
sequence: 100,
isVisible: ACTIONS.IS_ONLY_ONE_RANGE,
separator: true,
})
.addChild("insert_cell_down", ["insert_cell"], {
...ACTION_INSERT.insertCellShiftDownMenuItem,
name: _lt("Shift down"),
sequence: 10,
action: ACTIONS.INSERT_CELL_SHIFT_DOWN,
})
.addChild("insert_cell_right", ["insert_cell"], {
...ACTION_INSERT.insertCellShiftRightMenuItem,
name: _lt("Shift right"),
sequence: 20,
action: ACTIONS.INSERT_CELL_SHIFT_RIGHT,
})
.add("delete_row", {
name: ACTIONS.REMOVE_ROWS_NAME,
...ACTION_EDIT.deleteRowMenuItem,
sequence: 110,
action: ACTIONS.REMOVE_ROWS_ACTION,
isVisible: ACTIONS.IS_ONLY_ONE_RANGE,
})
.add("delete_column", {
name: ACTIONS.REMOVE_COLUMNS_NAME,
...ACTION_EDIT.deleteColMenuItem,
sequence: 120,
action: ACTIONS.REMOVE_COLUMNS_ACTION,
isVisible: ACTIONS.IS_ONLY_ONE_RANGE,
})
.add("delete_cell", {
name: _lt("Delete cells"),
...ACTION_EDIT.deleteCellsMenuItem,
sequence: 130,
isVisible: ACTIONS.IS_ONLY_ONE_RANGE,
})
.addChild("delete_cell_up", ["delete_cell"], {
...ACTION_EDIT.deleteCellShiftUpMenuItem,
name: _lt("Shift up"),
sequence: 10,
action: ACTIONS.DELETE_CELL_SHIFT_UP,
})
.addChild("delete_cell_down", ["delete_cell"], {
.addChild("delete_cell_left", ["delete_cell"], {
...ACTION_EDIT.deleteCellShiftLeftMenuItem,
name: _lt("Shift left"),
sequence: 20,
action: ACTIONS.DELETE_CELL_SHIFT_LEFT,
})
.add("insert_link", {
...ACTION_INSERT.insertLinkMenuItem,
name: _lt("Insert link"),
separator: true,
sequence: 150,
action: ACTIONS.INSERT_LINK,
separator: true,
});
73 changes: 21 additions & 52 deletions src/registries/menus/col_menu_registry.ts
Original file line number Diff line number Diff line change
@@ -1,112 +1,81 @@
import { _lt } from "../../translation";
import { SpreadsheetChildEnv } from "../../types";
import { MenuItemRegistry } from "../menu_items_registry";
import * as ACTIONS from "./menu_items_actions";
import * as ACTION_DATA from "./items/data_menu_items";
import * as ACTION_EDIT from "./items/edit_menu_items";
import * as ACTION_FORMAT from "./items/format_menu_items";
import * as ACTION_INSERT from "./items/insert_menu_items";
import * as ACTION_VIEW from "./items/view_menu_items";

export const colMenuRegistry = new MenuItemRegistry();

colMenuRegistry
.add("cut", {
name: _lt("Cut"),
description: "Ctrl+X",
...ACTION_EDIT.cutMenuItem,
sequence: 10,
action: ACTIONS.CUT_ACTION,
})
.add("copy", {
name: _lt("Copy"),
description: "Ctrl+C",
...ACTION_EDIT.copyMenuItem,
sequence: 20,
isReadonlyAllowed: true,
action: ACTIONS.COPY_ACTION,
})
.add("paste", {
name: _lt("Paste"),
description: "Ctrl+V",
...ACTION_EDIT.pasteMenuItem,
sequence: 30,
action: ACTIONS.PASTE_ACTION,
})
.add("paste_special", {
name: _lt("Paste special"),
...ACTION_EDIT.pasteSpecialMenuItem,
sequence: 40,
separator: true,
isVisible: ACTIONS.IS_NOT_CUT_OPERATION,
})
.addChild("paste_value_only", ["paste_special"], {
name: _lt("Paste value only"),
...ACTION_EDIT.pasteSpecialValueMenuItem,
sequence: 10,
action: ACTIONS.PASTE_VALUE_ACTION,
})
.addChild("paste_format_only", ["paste_special"], {
name: _lt("Paste format only"),
...ACTION_EDIT.pasteSpecialFormatMenuItem,
sequence: 20,
action: ACTIONS.PASTE_FORMAT_ACTION,
})
.add("sort_columns", {
...ACTION_DATA.sortRangeMenuItem,
name: (env) =>
env.model.getters.getActiveCols().size > 1 ? _lt("Sort columns") : _lt("Sort column"),
sequence: 50,
isVisible: ACTIONS.IS_ONLY_ONE_RANGE,
separator: true,
})
.addChild("sort_ascending", ["sort_columns"], {
name: _lt("Ascending (A ⟶ Z)"),
...ACTION_DATA.sortAscendingenuItem,
sequence: 10,
action: ACTIONS.SORT_CELLS_ASCENDING,
})
.addChild("sort_descending", ["sort_columns"], {
name: _lt("Descending (Z ⟶ A)"),
...ACTION_DATA.sortDescendingMenuItem,
sequence: 20,
action: ACTIONS.SORT_CELLS_DESCENDING,
})
.add("add_column_before", {
name: ACTIONS.COLUMN_INSERT_COLUMNS_BEFORE_NAME,
...ACTION_INSERT.colInsertColsBeforeMenuItem,
sequence: 70,
action: ACTIONS.INSERT_COLUMNS_BEFORE_ACTION,
})
.add("add_column_after", {
name: ACTIONS.COLUMN_INSERT_COLUMNS_AFTER_NAME,
...ACTION_INSERT.colInsertColsAfterMenuItem,
sequence: 80,
action: ACTIONS.INSERT_COLUMNS_AFTER_ACTION,
})
.add("delete_column", {
name: ACTIONS.REMOVE_COLUMNS_NAME,
...ACTION_EDIT.deleteColsMenuItem,
sequence: 90,
action: ACTIONS.REMOVE_COLUMNS_ACTION,
})
.add("clear_column", {
name: ACTIONS.DELETE_CONTENT_COLUMNS_NAME,
...ACTION_EDIT.clearColsMenuItem,
sequence: 100,
action: ACTIONS.DELETE_CONTENT_COLUMNS_ACTION,
})
.add("hide_columns", {
name: ACTIONS.HIDE_COLUMNS_NAME,
...ACTION_VIEW.hideColsMenuItem,
sequence: 85,
action: ACTIONS.HIDE_COLUMNS_ACTION,
isVisible: (env: SpreadsheetChildEnv) => {
const sheetId = env.model.getters.getActiveSheetId();
const hiddenCols = env.model.getters.getHiddenColsGroups(sheetId).flat();
return (
env.model.getters.getNumberCols(sheetId) >
hiddenCols.length + env.model.getters.getElementsFromSelection("COL").length
);
},
separator: true,
})
.add("unhide_columns", {
name: _lt("Unhide columns"),
...ACTION_VIEW.unhideColsMenuItem,
sequence: 86,
action: ACTIONS.UNHIDE_COLUMNS_ACTION,
isVisible: (env: SpreadsheetChildEnv) => {
const hiddenCols = env.model.getters
.getHiddenColsGroups(env.model.getters.getActiveSheetId())
.flat();
const currentCols = env.model.getters.getElementsFromSelection("COL");
return currentCols.some((col) => hiddenCols.includes(col));
},
separator: true,
})
.add("conditional_formatting", {
name: _lt("Conditional formatting"),
...ACTION_FORMAT.formatCFMenuItem,
sequence: 110,
action: ACTIONS.OPEN_CF_SIDEPANEL_ACTION,
});
33 changes: 33 additions & 0 deletions src/registries/menus/items/data_menu_items.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { _lt } from "../../../translation";
import { MenuItemSpec } from "../../menu_items_registry";
import * as ACTIONS from "./../menu_items_actions";

export const sortRangeMenuItem: MenuItemSpec = {
name: _lt("Sort range"),
isVisible: ACTIONS.IS_ONLY_ONE_RANGE,
};

export const sortAscendingenuItem: MenuItemSpec = {
name: _lt("Ascending (A ⟶ Z)"),
action: ACTIONS.SORT_CELLS_ASCENDING,
};

export const sortDescendingMenuItem: MenuItemSpec = {
name: _lt("Descending (Z ⟶ A)"),
action: ACTIONS.SORT_CELLS_DESCENDING,
};

export const addDataFilterMenuItem: MenuItemSpec = {
name: _lt("Create filter"),
action: ACTIONS.FILTERS_CREATE_FILTER_TABLE,
isVisible: (env) => !ACTIONS.SELECTION_CONTAINS_FILTER(env),
isEnabled: (env) => ACTIONS.SELECTION_IS_CONTINUOUS(env),
icon: "o-spreadsheet-Icon.FILTER_ICON_INACTIVE",
};

export const removeDataFilterMenuItem: MenuItemSpec = {
name: _lt("Remove filter"),
action: ACTIONS.FILTERS_REMOVE_FILTER_TABLE,
isVisible: ACTIONS.SELECTION_CONTAINS_FILTER,
icon: "o-spreadsheet-Icon.FILTER_ICON_INACTIVE",
};
Loading

0 comments on commit 950abc4

Please sign in to comment.