fix: @后会出现无法删除的情况

This commit is contained in:
itellyou 2022-01-27 16:00:04 +08:00
parent 93887057e6
commit 95315707bf
2 changed files with 16 additions and 3 deletions

View File

@ -1,3 +1,4 @@
import { CARD_ELEMENT_KEY } from '../../constants';
import { EngineInterface, RangeInterface } from '../../types';
class Backspace {
@ -44,12 +45,20 @@ class Backspace {
if (
cloneRange.startContainer.nodeType === Node.TEXT_NODE &&
(function (range: RangeInterface) {
const { commonAncestorContainer } = range;
const { commonAncestorContainer, commonAncestorNode } =
range;
const commonAncestorParent = commonAncestorNode.parent();
const commonAncestorAttributes =
commonAncestorParent?.attributes();
if (
range.collapsed &&
1 === range.startOffset &&
range.startContainer === commonAncestorContainer &&
commonAncestorContainer.nodeType === Node.TEXT_NODE
commonAncestorContainer.nodeType === Node.TEXT_NODE &&
(!commonAncestorAttributes ||
!['left', 'right'].includes(
commonAncestorAttributes[CARD_ELEMENT_KEY],
))
) {
range = range.cloneRange();
if (

View File

@ -49,7 +49,11 @@ class Backspace {
*/
trigger(event: KeyboardEvent) {
const { change } = this.engine;
const range = change.range.get();
const range = change.range
.get()
.cloneRange()
.shrinkToElementNode()
.shrinkToTextNode();
if (!range.collapsed) return;
// 查找当前光标所在卡片
const card = this.engine.card.find(range.startNode);