本帖最后由 dexteryao 于 2021-2-3 13:55 编辑
上一期我们将了如何自定义右键菜单,本期我们在其基础上进行扩展,将讲解如何自定义多级的右键菜单
下面实例将定制一个多级菜单用于设置文字旋转功能,提供三个子菜单-90度,水平,90度
首先我们先定义菜单文字旋转。
- designerConfig.commandMap["textRotation"]= {
- text: "文字旋转",
- commandName: "textRotation",
- subCommands:[
- "negativeVertical",
- "horizontal",
- "vertical"
- ],
- visibleContext:"ClickViewport"
- }
复制代码 在父级菜单中我们没有定义具体的execute以为改菜单不需要执行任何操作,我们定义了subCommands来扩展三个子菜单
- subCommands:[
- "negativeVertical",
- "horizontal",
- "vertical"
- ]
复制代码 之后我们同样在commandMap中定义三个子菜单
- designerConfig.commandMap["negativeVertical"]= {
- text:"-90度",
- commandName: "negativeVertical",
- //enableContext: "allowEditObject",
- execute: async (context) => {
- var spread = context.getWorkbook();
- var commandManager = spread.commandManager();
- commandManager.execute({cmd: "executeRotation", sheetName: spread.getActiveSheet().name(),degree:-90});
- }
- }
- designerConfig.commandMap["horizontal"]= {
- text:"水平",
- commandName: "horizontal",
- //enableContext: "allowEditObject",
- execute: async (context) => {
- var spread = context.getWorkbook();
- var commandManager = spread.commandManager();
- commandManager.execute({cmd: "executeRotation", sheetName: spread.getActiveSheet().name(),degree:0});
- }
- }
- designerConfig.commandMap["vertical"]= {
- text:"90度",
- commandName: "vertical",
- //enableContext: "allowEditObject",
- execute: async (context) => {
- var spread = context.getWorkbook();
- var commandManager = spread.commandManager();
- commandManager.execute({cmd: "executeRotation", sheetName: spread.getActiveSheet().name(),degree:90});
- }
- }
复制代码 这里需要定义具体的execute,因为子菜单点击后需要进行操作,操作中我们为了方便undo redo,选择用命令实现具体操作。具体命令定义如下
- var spread = designer.getWorkbook();
- var commandManager = spread.commandManager();
- var _executeRotation = {
- canUndo: true,
- execute: function (context, options, isUndo) {
- var Commands = GC.Spread.Sheets.Commands;
- if (isUndo) {
- Commands.undoTransaction(context, options);
- return true;
- } else {
- Commands.startTransaction(context, options);
- var sheet = context.getSheetFromName(options.sheetName);
- sheet.suspendPaint();
- var selections = sheet.getSelections();
- for(var i=0;i<selections.length;i++){
- sheet.getRange(selections[i].row,
- selections[i].col,
- selections[i].rowCount,
- selections[i].colCount,
- GC.Spread.Sheets.SheetArea.viewport).textOrientation(options.degree);
- }
- sheet.resumePaint();
- Commands.endTransaction(context, options);
- return true;
- }
- }
- };
- commandManager.register("executeRotation", _executeRotation);
复制代码 最后我们需要将父级菜单push到contextmenu中,这里注意只需要push父级菜单即可,子菜单不需要因为子菜单是通过父级菜单扩展弹出的。但是子菜单需要定义在commandMap中。
- designerConfig.contextMenu.push("textRotation");
复制代码
|
|