Fix bug when selecting text next to frame by clicking three times (#1604)

This commit is contained in:
Henrik Giesel 2022-01-19 01:15:53 +01:00 committed by GitHub
parent fe7a8db231
commit 0bb04e2951
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 9 deletions

View File

@ -8,7 +8,7 @@ import {
hasBlockAttribute, hasBlockAttribute,
} from "../lib/dom"; } from "../lib/dom";
import { on } from "../lib/events"; import { on } from "../lib/events";
import { getSelection } from "../lib/cross-browser"; import { getSelection, isSelectionCollapsed } from "../lib/cross-browser";
import { moveChildOutOfElement } from "../domlib/move-nodes"; import { moveChildOutOfElement } from "../domlib/move-nodes";
import { placeCaretBefore, placeCaretAfter } from "../domlib/place-caret"; import { placeCaretBefore, placeCaretAfter } from "../domlib/place-caret";
import { import {
@ -238,7 +238,10 @@ function checkIfInsertingLineBreakAdjacentToBlockFrame() {
const selection = getSelection(frame)!; const selection = getSelection(frame)!;
if (selection.anchorNode === frame.framedElement && selection.isCollapsed) { if (
selection.anchorNode === frame.framedElement &&
isSelectionCollapsed(selection)
) {
frame.insertLineBreak(selection.anchorOffset); frame.insertLineBreak(selection.anchorOffset);
} }
} }

View File

@ -3,7 +3,7 @@
import { nodeIsText, nodeIsElement, elementIsEmpty } from "../lib/dom"; import { nodeIsText, nodeIsElement, elementIsEmpty } from "../lib/dom";
import { on } from "../lib/events"; import { on } from "../lib/events";
import { getSelection } from "../lib/cross-browser"; import { getSelection, isSelectionCollapsed } from "../lib/cross-browser";
import { moveChildOutOfElement } from "../domlib/move-nodes"; import { moveChildOutOfElement } from "../domlib/move-nodes";
import { placeCaretAfter } from "../domlib/place-caret"; import { placeCaretAfter } from "../domlib/place-caret";
import type { FrameElement } from "./frame-element"; import type { FrameElement } from "./frame-element";
@ -277,7 +277,10 @@ export function checkWhetherMovingIntoHandle(): void {
for (const handle of handles) { for (const handle of handles) {
const selection = getSelection(handle)!; const selection = getSelection(handle)!;
if (selection.anchorNode === handle.firstChild && selection.isCollapsed) { if (
selection.anchorNode === handle.firstChild &&
isSelectionCollapsed(selection)
) {
handle.notifyMoveIn(selection.anchorOffset); handle.notifyMoveIn(selection.anchorOffset);
} }
} }

View File

@ -1,11 +1,6 @@
// Copyright: Ankitects Pty Ltd and contributors // Copyright: Ankitects Pty Ltd and contributors
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html // License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
/**
* NOTES:
* - Avoid using selection.isCollapsed: will always return true in shadow root in Gecko
*/
/** /**
* Gecko has no .getSelection on ShadowRoot, only .activeElement * Gecko has no .getSelection on ShadowRoot, only .activeElement
*/ */
@ -29,3 +24,12 @@ export function getRange(selection: Selection): Range | null {
return rangeCount === 0 ? null : selection.getRangeAt(rangeCount - 1); return rangeCount === 0 ? null : selection.getRangeAt(rangeCount - 1);
} }
/**
* Avoid using selection.isCollapsed: it will always return
* true in shadow root in Gecko
* (this bug seems to also happens in Blink)
*/
export function isSelectionCollapsed(selection: Selection): boolean {
return getRange(selection)!.collapsed;
}