You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何禁用Excalidraw移动端双触添加文本功能?双击已生效

解决Excalidraw移动端双触添加文本的禁用问题

你当前代码的核心问题有两个:一是lastTap的作用域错误,导致双触时间差计算失效;二是仅阻止事件传播但未阻止默认行为,且没命中Excalidraw实际处理双触的事件阶段。

修正后的代码方案

$(document).ready(function() {
    // 将lastTap移到定时器外部,避免每次检测元素时重置
    let lastTap = 0;
    const checkExist = setInterval(function() {
        const canvas = $(".excalidraw__canvas");
        if (canvas.length) {
            // 桌面双击禁用(保留原有逻辑并增强阻止行为)
            canvas.dblclick(function(e) {
                console.log("双击已拦截");
                e.stopPropagation();
                e.preventDefault();
            });

            // 移动端双触拦截:监听touchend(Excalidraw通常在此阶段处理双触逻辑)
            canvas.on("touchend", function(e) {
                const currentTime = new Date().getTime();
                const tapInterval = currentTime - lastTap;
                
                // 检测300ms内的连续触摸
                if (tapInterval > 0 && tapInterval < 300) {
                    console.log("双触已拦截");
                    e.stopPropagation();
                    e.preventDefault();
                    // 重置lastTap避免连续误判
                    lastTap = 0;
                    return false;
                }
                lastTap = currentTime;
            });

            // 补充监听touchstart,覆盖更多触发场景
            canvas.on("touchstart", function(e) {
                const currentTime = new Date().getTime();
                const tapInterval = currentTime - lastTap;
                if (tapInterval > 0 && tapInterval < 300) {
                    e.stopPropagation();
                    e.preventDefault();
                    lastTap = 0;
                }
                lastTap = currentTime;
            });

            clearInterval(checkExist);
        }
    }, 100);
});

关键修改说明

  • 作用域修正:把lastTap移到setInterval外部,确保两次触摸的时间差能正确计算,不会每次定时器执行都被重置为0。
  • 事件类型调整:Excalidraw的双触文本添加逻辑大概率在touchend阶段处理,新增该事件监听比仅监听touchstart更有效。
  • 增强阻止逻辑:同时调用e.stopPropagation()和e.preventDefault(),既阻止事件冒泡到Excalidraw内部处理函数,也阻止浏览器默认触摸行为。
  • 重置lastTap:拦截到双触后手动重置变量,避免短时间内的连续误判。

备选方案:直接修改Excalidraw配置(若可行)

如果你是通过嵌入Excalidraw实例使用,可以尝试初始化时添加自定义配置(该选项为非官方公开,需注意版本兼容性):

const excalidraw = createExcalidraw({
  // 其他配置项...
  disableDoubleClickToCreateText: true,
});

优先推荐前面的事件拦截方案,稳定性更强。


内容的提问来源于stack exchange,提问作者バッカマル

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 15:00:06