[Feature] Can I add a listener for a cell double-click event? #3890
Replies: 3 comments 1 reply
-
Origin Title: [Feature] 可以增加一个单元格双击事件的监听吗 Title: [Feature] Can I add a listener for a cell double-click event? Initial list
questionI have tried to use the universalAPI.value.onBeforeCommandExecute method to listen for double-click events, but this event is also triggered when the user inputs. If a capture cell double-click event can be provided, more business needs can be met. |
Beta Was this translation helpful? Give feedback.
-
onBeforeCommandExecute能监听到双击事件嘛?请问监听的是哪个命令 |
Beta Was this translation helpful? Give feedback.
-
目前好像作者没这个计划,有一个临时的解决办法,去监听判断doc.operation.set-selections事件是不是同一个单元格且在短时间内连续触发2次。下面代码供参考: /** 短时间触发n次执行回调 */
export function triggerCallbackOnNTimes(callback: Function, n = 2, timeFrame = 200) {
let count = 0;
let timeoutId;
return function(...args) {
count++;
if (count === 1) {
// 第一次调用,设置计时器
timeoutId = setTimeout(() => {
count = 0; // 超时重置计数
}, timeFrame);
}
if (count >= n) {
// 达到 n 次调用,执行回调
callback(...args);
count = 0; // 重置计数
clearTimeout(timeoutId); // 清除计时器
}
};
}
/** 要执行的函数 */
const func = triggerCallbackOnNTimes((cell: ICellData) => {
console.log(cell)
}, 2 , 500)
/** 判断是否在单个单元格里双击 */
let lastClickCellInfo = { row: -1, col: -1 }
const judgeDoubleClick = triggerCallbackOnNTimes(() => {
const range = univerAPI.getActiveWorkbook().getActiveRange()
const row = range.getRange().startRow
const col = range.getRange().startColumn
if (lastClickCellInfo.row !== row || lastClickCellInfo.col!== col) {
lastClickCellInfo.row = row
lastClickCellInfo.col = col
return
}
const cellData = range.getCellData()
if (!cellData) return
func(cellData)
}, 2, 500)
/** 判断双击事件 */
univerAPI.onBeforeCommandExecute((command) => {
if (command.id === 'doc.operation.set-selections') {
judgeDoubleClick()
}
}) |
Beta Was this translation helpful? Give feedback.
-
初始清单
问题
已经尝试过用univerAPI.value.onBeforeCommandExecute这个方法去监听双击事件,但是在用户输入的时候也触发到了这个事件,如果能够提供一个捕捉单元格双击事件的话,就可以满足更多的业务需求。
Beta Was this translation helpful? Give feedback.
All reactions