Repository navigation
fix(web): editable file drag selection no longer breaks across virtualized scroll windows #39574
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
Changes from all commits
8e6c11b
530daad
a19a402
e8af571
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| diff --git a/dist/editor/editor.js b/dist/editor/editor.js | ||
| index b4c280390b52434f63fbe7916d81b150d9006fb4..ae0e8e6f9e0473a152b921a50a8c7833cb466a03 100644 | ||
| index b4c280390b52434f63fbe7916d81b150d9006fb4..8f6df87aa893fe413811b95f7b7d2ab7ea515750 100644 | ||
| --- a/dist/editor/editor.js | ||
| +++ b/dist/editor/editor.js | ||
| @@ -95,6 +95,8 @@ var Editor = class { | ||
|
|
@@ -11,23 +11,50 @@ index b4c280390b52434f63fbe7916d81b150d9006fb4..ae0e8e6f9e0473a152b921a50a8c7833 | |
| #caretElements; | ||
| #caretHighlightElements; | ||
| #primaryCaretElement; | ||
| @@ -536,6 +538,7 @@ var Editor = class { | ||
| @@ -125,6 +127,7 @@ var Editor = class { | ||
| #altColumnDrag; | ||
| #shiftKeyPressed = false; | ||
| #selectionStart; | ||
| + #dragAnchorUnmounted = false; | ||
| #deletedSelectionText = ""; | ||
| #reservedSelections; | ||
| #initSelections; | ||
| @@ -536,6 +539,7 @@ var Editor = class { | ||
| const fileInstance = this.#fileInstance; | ||
| const recycle = reason === "recycle"; | ||
| this.#virtualizedInstance?.cancelScroll(this.#handleCaretRevealDone); | ||
| + this.#virtualizedInstance?.cancelScroll(this.#handleLineRevealDone); | ||
| this.#cancelEditPrediction(true, false); | ||
| if (!recycle) this.#editPredictionHistory = []; | ||
| const editSession = this.#editSession; | ||
| @@ -584,6 +587,7 @@ var Editor = class { | ||
| @@ -584,6 +588,7 @@ var Editor = class { | ||
| this.#contentHasFocus = false; | ||
| this.#overlayElement?.remove(); | ||
| this.#overlayElement = void 0; | ||
| + this.#fontProbeElement = void 0; | ||
| this.#resizeObserver?.disconnect(); | ||
| this.#resizeObserver = void 0; | ||
| this.#fontRemeasureScheduled = false; | ||
| @@ -1022,6 +1026,19 @@ var Editor = class { | ||
| @@ -657,6 +662,7 @@ var Editor = class { | ||
| } | ||
| /** @internal */ | ||
| __captureFocusForDOMReplacement() { | ||
| + if (this.#isContentMouseDown) return; | ||
| const contentElement = this.#contentElement; | ||
| const shadowActiveElement = this.#fileContainer?.shadowRoot?.activeElement; | ||
| if (contentElement != null && (this.#contentHasFocus || shadowActiveElement === contentElement || shadowActiveElement != null && contentElement.contains(shadowActiveElement))) { | ||
| @@ -839,7 +845,9 @@ var Editor = class { | ||
| this.#initSelections = void 0; | ||
| this.#scrollToPrimaryCaret(false, "center"); | ||
| } else if (this.#scrollingToLine !== void 0) this.#scrollToLine(this.#scrollingToLine, this.#scrollingToLineChar, this.#scrollingToLineNoFocus); | ||
| - else if (this.#focusRequest?.restore != null) this.#restoreReplacementFocus(); | ||
| + else if (this.#isContentMouseDown) { | ||
| + if (this.#focusRequest?.restore != null) this.#cancelFocusRequest(); | ||
| + } else if (this.#focusRequest?.restore != null) this.#restoreReplacementFocus(); | ||
| else if (this.#selections !== void 0 && this.#selections.length > 0 && this.#contentHasFocus && !this.#retainSearchPanelFocus) this.focus({ preventScroll: true }); | ||
| if (this.#retainSearchPanelFocus) this.#searchPanel?.focus(); | ||
| if (restoreEditorStateOnSync) { | ||
| @@ -1022,6 +1030,19 @@ var Editor = class { | ||
| this.#tokenizer?.applyStyle(); | ||
| this.#spriteElement = createSpriteElement(); | ||
| this.#overlayElement = h("div", { dataset: "editorOverlay" }); | ||
|
|
@@ -47,23 +74,103 @@ index b4c280390b52434f63fbe7916d81b150d9006fb4..ae0e8e6f9e0473a152b921a50a8c7833 | |
| const replacementFocusTargetIsContent = (event) => { | ||
| const target = event.composedPath()[0]; | ||
| return target instanceof Node && this.#contentElement?.contains(target) === true; | ||
| @@ -1130,6 +1147,7 @@ var Editor = class { | ||
| @@ -1061,15 +1082,17 @@ var Editor = class { | ||
| if (this.#selections !== void 0 && this.#selections.length > 1 && !this.#isContentMouseDown) return; | ||
| const composedRange = getComposedSelectionRange(shadowRoot); | ||
| if (composedRange === void 0 || !this.#rangeBelongsToEditor(composedRange)) return; | ||
| - const selection = convertSelection(composedRange, 0); | ||
| + let selection = convertSelection(composedRange, 0); | ||
| + if (this.#isContentMouseDown && !this.#shiftKeyPressed && this.#selectionStart !== void 0) selection = this.#resolveMouseDragSelection(composedRange, selection); | ||
| if (selection === void 0) return; | ||
| if (this.#isContentMouseDown && this.#shiftKeyPressed && this.#selections !== void 0 && this.#selections.length > 0) { | ||
| const primarySelection = this.#selections.at(-1); | ||
| this.#updateSelections([extendSelection(primarySelection, selection)]); | ||
| return; | ||
| } | ||
| - if (this.#selectionStart !== void 0) selection.direction = createSelectionFrom(this.#selectionStart, selection).direction; | ||
| - else if (this.#isContentMouseDown) this.#selectionStart = selection; | ||
| + if (this.#selectionStart !== void 0) { | ||
| + if (!this.#dragAnchorUnmounted) selection.direction = createSelectionFrom(this.#selectionStart, selection).direction; | ||
| + } else if (this.#isContentMouseDown) this.#selectionStart = selection; | ||
| else if (this.#selections !== void 0 && this.#selections.length === 1) { | ||
| const previous = this.#selections[0]; | ||
| if (comparePosition(previous.start, selection.start) === 0 && comparePosition(previous.end, selection.end) === 0) selection.direction = previous.direction; | ||
| @@ -1104,8 +1127,50 @@ var Editor = class { | ||
| this.#altColumnDrag = void 0; | ||
| this.#shiftKeyPressed = false; | ||
| this.#selectionStart = void 0; | ||
| + this.#dragAnchorUnmounted = false; | ||
| this.#reservedSelections = void 0; | ||
| } | ||
| + #resolveMouseDragSelection(composedRange, selection) { | ||
| + const selectionStart = this.#selectionStart; | ||
| + if (selectionStart === void 0) return selection; | ||
| + const renderRange = this.#renderRange; | ||
| + if (renderRange !== void 0 && (selectionStart.start.line < renderRange.startingLine || selectionStart.end.line >= renderRange.startingLine + renderRange.totalLines)) this.#dragAnchorUnmounted = true; | ||
| + if (!this.#dragAnchorUnmounted) return selection; | ||
| + let focus; | ||
| + if (selection !== void 0) { | ||
| + const direction = window.getSelection()?.direction; | ||
| + if (direction === "backward") focus = selection.start; | ||
| + else if (direction === "forward") focus = selection.end; | ||
| + else focus = renderRange !== void 0 && selectionStart.end.line >= renderRange.startingLine + renderRange.totalLines ? selection.start : selection.end; | ||
| + } else { | ||
| + const startPos = convertSelection({ | ||
| + startContainer: composedRange.startContainer, | ||
| + startOffset: composedRange.startOffset, | ||
| + endContainer: composedRange.startContainer, | ||
| + endOffset: composedRange.startOffset | ||
| + }, 0)?.start; | ||
| + const endPos = convertSelection({ | ||
| + startContainer: composedRange.endContainer, | ||
| + startOffset: composedRange.endOffset, | ||
| + endContainer: composedRange.endContainer, | ||
| + endOffset: composedRange.endOffset | ||
| + }, 0)?.end; | ||
| + if (startPos !== void 0 && endPos === void 0) focus = startPos; | ||
| + else if (endPos !== void 0 && startPos === void 0) focus = endPos; | ||
| + else return; | ||
| + } | ||
| + if (comparePosition(focus, selectionStart.start) < 0) return { | ||
| + start: focus, | ||
| + end: selectionStart.end, | ||
| + direction: -1 | ||
| + }; | ||
| + if (comparePosition(focus, selectionStart.end) > 0) return { | ||
| + start: selectionStart.start, | ||
| + end: focus, | ||
| + direction: 1 | ||
| + }; | ||
| + return { ...selectionStart }; | ||
| + } | ||
| #replaceSelectEventListeners(disposes) { | ||
| this.#selectEventDisposes?.forEach((dispose) => dispose()); | ||
| this.#selectEventDisposes = disposes; | ||
| @@ -1130,6 +1195,7 @@ var Editor = class { | ||
| this.#editorEventDisposes = [ | ||
| addEventListener(contentEl, "focus", () => { | ||
| this.#contentHasFocus = true; | ||
| + this.#retainSearchPanelFocus = false; | ||
| onFocus?.(); | ||
| if (!this.#isContentMouseDown && !this.#isComposing && this.#focusRequest == null && this.#selections !== void 0 && this.#selections.length > 0) this.#setWindowSelection(this.#selections.at(-1)); | ||
| }, { passive: true }), | ||
| @@ -1397,6 +1415,7 @@ var Editor = class { | ||
| @@ -1163,6 +1229,7 @@ var Editor = class { | ||
| this.#isContentMouseDown = true; | ||
| const isAltColumnDrag = e.button === 0 && e.altKey && !e.ctrlKey && !e.metaKey && !e.shiftKey; | ||
| this.#selectionStart = void 0; | ||
| + this.#dragAnchorUnmounted = false; | ||
| if (isAltColumnDrag) { | ||
| this.#altColumnDrag = { | ||
| pointerId: e.pointerId, | ||
| @@ -1397,6 +1464,7 @@ var Editor = class { | ||
| this.#resizeObserver = new ResizeObserver(this.#handleLayoutResize); | ||
| this.#resizeObserver.observe(contentEl); | ||
| this.#resizeObserver.observe(contentEl.parentElement); | ||
| + if (this.#fontProbeElement !== void 0) this.#resizeObserver.observe(this.#fontProbeElement); | ||
| this.#computeContentOffset(contentEl); | ||
| } | ||
| #handleCustomPasteEvent = async () => { | ||
| @@ -1852,7 +1871,9 @@ var Editor = class { | ||
| @@ -1852,7 +1920,9 @@ var Editor = class { | ||
| const change = textDocument.applyEdits(edits, true, selections, nextSelections, true); | ||
| if (change !== void 0) this.#applyChange(change, nextSelections); | ||
| } | ||
|
|
@@ -74,7 +181,7 @@ index b4c280390b52434f63fbe7916d81b150d9006fb4..ae0e8e6f9e0473a152b921a50a8c7833 | |
| const lineAnnotations = this.#lineAnnotations?.length ?? 0; | ||
| const prevGutterWidth = this.#gutterWidthCache; | ||
| const prevContentWidth = this.#contentWidthCache; | ||
| @@ -1860,12 +1881,12 @@ var Editor = class { | ||
| @@ -1860,12 +1930,12 @@ var Editor = class { | ||
| this.#contentWidthCache = void 0; | ||
| const gutterWidthChanged = this.#getGutterWidth() !== prevGutterWidth; | ||
| const contentWidthChanged = this.#getContentWidth() !== prevContentWidth; | ||
|
|
@@ -90,7 +197,22 @@ index b4c280390b52434f63fbe7916d81b150d9006fb4..ae0e8e6f9e0473a152b921a50a8c7833 | |
| } | ||
| if (this.#selections !== void 0 || this.#matches !== void 0 || this.#markerRenderer !== void 0) { | ||
| this.#updateSelections(this.#selections ?? []); | ||
| @@ -2123,6 +2144,16 @@ var Editor = class { | ||
| @@ -2013,7 +2083,13 @@ var Editor = class { | ||
| return; | ||
| } | ||
| const primaryPosition = primarySelection.direction === -1 ? primarySelection.end : primarySelection.start; | ||
| - if (this.#getLineElement(primaryPosition.line) == null) return; | ||
| + if (this.#getLineElement(primaryPosition.line) == null) { | ||
| + const request = { restore: void 0 }; | ||
| + this.#cancelFocusRequest(); | ||
| + this.#focusRequest = request; | ||
| + queueRender(() => this.#releaseFocusRequest(request)); | ||
| + return; | ||
| + } | ||
| this.#focus(primaryPosition, true, restore); | ||
| } | ||
| #replacementFocusIsAvailable(contentElement) { | ||
| @@ -2123,6 +2199,16 @@ var Editor = class { | ||
| this.#scrollCaretIntoView(line, character, "nearest"); | ||
| } | ||
| }; | ||
|
|
@@ -107,7 +229,7 @@ index b4c280390b52434f63fbe7916d81b150d9006fb4..ae0e8e6f9e0473a152b921a50a8c7833 | |
| #focusAt(position) { | ||
| if (this.#focusRequest?.restore != null) this.#restoreReplacementFocus(position); | ||
| else this.#focus(position); | ||
| @@ -2145,6 +2176,19 @@ var Editor = class { | ||
| @@ -2145,6 +2231,19 @@ var Editor = class { | ||
| this.#scrollingToLineNoFocus = false; | ||
| return; | ||
|
Comment on lines
233
to
234
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cancel the pending host reveal before returning for a mounted target. If Enter requests offscreen line 350 and Shift+Enter returns to mounted line 5 before settlement, this early return leaves the line-350 host request active. The host then scrolls away from the newer search destination. Enter navigation and native Before this return, cancel
|
||
| } | ||
|
|
@@ -127,7 +249,7 @@ index b4c280390b52434f63fbe7916d81b150d9006fb4..ae0e8e6f9e0473a152b921a50a8c7833 | |
| let yFix = 0; | ||
| if (this.#scrollingToLine === line && this.#contentElement !== void 0) for (let i = this.#contentElement.childElementCount - 1; i >= 0; i--) { | ||
| const child = this.#contentElement.children[i]; | ||
| @@ -3370,6 +3414,7 @@ var Editor = class { | ||
| @@ -3370,6 +3469,7 @@ var Editor = class { | ||
| this.#searchPanel = searchPanel; | ||
| this.#retainSearchPanelFocus = false; | ||
| searchPanel.mount(); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Release the left mouse button when the drag test exits early.
If the window-shift assertion at lines 481-488 fails, the test skips
mouseReleasedat line 489. The button stays pressed in the browser page. A later pointer test in the same file can then start from a held left button and see unexpected selection or drag behavior.Add a scoped async cleanup that sends
mouseReleasedat the drag end point when the test exits before the normal release. Keep the normal release before the overlay and clipboard assertions, so the release does not run twice on success.