From 967e612fc07013efcaf3699cee5afa8b173a2b40 Mon Sep 17 00:00:00 2001 From: YMCoke <1272698173@qq.com> Date: Fri, 19 Mar 2021 17:09:49 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E9=BC=A0=E6=A0=87?= =?UTF-8?q?=E7=A7=BB=E5=87=BA=E7=BC=96=E8=BE=91=E5=8C=BA=E5=90=8E=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=E9=80=89=E5=8C=BA=E5=BC=82=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/text/index.ts | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/text/index.ts b/src/text/index.ts index 7c0f38c56..63d6a16c9 100644 --- a/src/text/index.ts +++ b/src/text/index.ts @@ -285,18 +285,25 @@ class Text { $textElem.on('click', onceClickSaveRange) function handleMouseUp() { - // 在编辑器区域之内完成点击,取消鼠标滑动到编辑区外面的事件 - $textElem.off('mouseleave', saveRange) - + // 在编辑器区域之外完成抬起,保存此时编辑区内的新选区,取消此时鼠标抬起事件 + saveRange() $document.off('mouseup', handleMouseUp) } - $textElem.on('mousedown', () => { - // mousedown 状态下,鼠标滑动到编辑区域外面,也需要保存选区 - $textElem.on('mouseleave', saveRange) + function listenMouseLeave() { + // 当鼠标移动到外面,要监听鼠标抬起操作 $document.on('mouseup', handleMouseUp) + // 首次移出时即接触leave监听,防止用户不断移入移出多次注册handleMouseUp + $textElem.off('mouseleave', listenMouseLeave) + } + $textElem.on('mousedown', () => { + // mousedown 状态下,要坚听鼠标滑动到编辑区域外面 + $textElem.on('mouseleave', listenMouseLeave) }) $textElem.on('mouseup', (e: MouseEvent) => { + // 记得移除$textElem的mouseleave事件, 避免内存泄露 + $textElem.off('mouseleave', listenMouseLeave) + const selection = editor.selection const range = selection.getRange()