本帖最后由 DerrickJiao 于 2020-10-27 10:52 编辑
背景:我们知道使用 SpreadJS 能够根据自己的需求自定义右键菜单的功能。以此实现在工作簿的不同区域右键点击,呼出不同的右键菜单,实际的项目中有小伙伴有这样的需求:想要在右击图片时,呼出自定义的右键菜单实现自己额外对图片处理的逻辑。
示例实现: 1. 右键图片呼出自定义菜单
2. 双击图片响应事件
3.双击单元格、行头、列头响应事件
右键菜单的主要思路是通过继承ContextMenu的方式自定义右键菜单逻辑
- function ContextMenu(){}
- ContextMenu.prototype = new GC.Spread.Sheets.ContextMenu.ContextMenu(spread);
- ContextMenu.prototype.onOpenMenu = function (menuData, itemsDataForShown, hitInfo, spread) {}
复制代码
判断右键区域并编写呼出菜单的逻辑
- if (hitInfo.worksheetHitInfo.floatingObjectHitInfo && hitInfo.worksheetHitInfo.floatingObjectHitInfo.floatingObject) {
-
- itemsDataForShown.push({text: "新增图片", name: "add", command: "addPic",
- workArea: "viewportcolHeaderrowHeaderslicercorner"});
- itemsDataForShown.push({text: "删除图片", name: "delete", command: "delePic",
- workArea: "viewportcolHeaderrowHeaderslicercorner"});
- itemsDataForShown.push({text: "选择图片", name: "select", command: "selectPic",
- workArea: "viewportcolHeaderrowHeaderslicercorner"});
- itemsDataForShown.push({text: "缩放图片", name: "zoom", command: "zoomPic",
- workArea: "viewportcolHeaderrowHeaderslicercorner"});
-
- }
复制代码
需要实现右键菜单的命令还需要注册对应的命令
- spread.commandManager().register("addPic",{
- canUndo: true,
- execute: function (context, options, isUndo) {
- var Commands = GC.Spread.Sheets.Commands;
- // 在此加cmd
- options.cmd = "addPic";
- if (isUndo) {
- Commands.undoTransaction(context, options);
- return true;
- } else {
- Commands.startTransaction(context, options);
复制代码
双击响应事件主要是依靠hitTest函数实现,也就是通过判断鼠标双击的区域做出响应,关于命中测试可以参考学习指南:
https://demo.grapecity.com.cn/sp ... rkbook/hit-testing#
- function getHitAreaName(result){
- if(result) {
- var str = "";
- var worksheetHitInfo = result.worksheetHitInfo;
- var tabStripHitInfo = result.tabStripHitInfo;
- if (worksheetHitInfo.floatingObjectHitInfo && worksheetHitInfo.floatingObjectHitInfo.floatingObject) {
- alert("我是图片")
- } else {
- var hitTestType = worksheetHitInfo.hitTestType;
- if (hitTestType === 0) {
- str = '我是表角';
- } else if (hitTestType === 1) {
- str = '我是列头';
- alert(str);
- } else if (hitTestType === 2) {
- str = '我是行头';
- alert(str);
- } else {
- var row = worksheetHitInfo.row;
- var col = worksheetHitInfo.col;
- str = '我是单元格';
- alert(str)
- }
- }
- }
- }
复制代码
下载附件即可查看完整的demo!
|
|