Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

修正 ExpandMarker 规则,避免无法取消选中区域 #681

Closed
wants to merge 8 commits into from
Closed
18 changes: 15 additions & 3 deletions src/ts/ir/expandMarker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,28 @@ const previousIsNode = (range: Range) => {
return false;
};

export const expandMarker = (range: Range, vditor: IVditor) => {
export const expandMarker = (range: Range, vditor: IVditor, event: MouseEvent = null) => {
vditor.ir.element.querySelectorAll(".vditor-ir__node--expand").forEach((item) => {
item.classList.remove("vditor-ir__node--expand");
});
const nodeElement = hasTopClosestByClassName(range.startContainer, "vditor-ir__node");

let eventTarget = event ? event.target : null;
if (eventTarget === vditor.ir.element) {
// 鼠标选取多行文本时, 会触发 click 事件,且 target 为 ir.element,此时保持原有规则
eventTarget = null;
}

// range.collapsed = false 且 触发 click 事件时,range 并不会改变,此时 eventTarget 与 range 不一致,应该按照 eventTarget 处理
const target = (!range.collapsed && eventTarget) ? eventTarget as HTMLElement : range.startContainer;
const nodeElement = hasTopClosestByClassName(target, "vditor-ir__node");
if (nodeElement) {
nodeElement.classList.add("vditor-ir__node--expand");
nodeElement.classList.remove("vditor-ir__node--hidden");
// https://github.com/Vanessa219/vditor/issues/615 safari中光标位置跳动
setSelectionFocus(range);
// range.collapsed = false 时无需修正 range,否则会导致选中多行文本,且首行为 nodeElement 时,无法取消选择
if (range.collapsed) {
setSelectionFocus(range);
}
}

const nextNode = nextIsNode(range);
Expand Down
2 changes: 1 addition & 1 deletion src/ts/ir/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ class IR {
}
}

expandMarker(range, vditor);
expandMarker(range, vditor, event);
highlightToolbarIR(vditor);
});

Expand Down