diff --git a/dist/index.cjs b/dist/index.cjs index a16387f..5dd5c4e 100644 --- a/dist/index.cjs +++ b/dist/index.cjs @@ -4886,6 +4886,15 @@ class MouseSelection { this.mustSelect = mustSelect; this.scrollSpeed = { x: 0, y: 0 }; this.scrolling = -1; + this.moveFrame = -1; + this.pendingMove = false; + this.finishFrame = () => { + this.moveFrame = -1; + if (this.pendingMove) { + this.pendingMove = false; + this.move(this.lastEvent); + } + }; this.lastEvent = startEvent; this.scrollParents = scrollableParents(view.contentDOM); this.atoms = view.state.facet(atomicRanges).map(f => f(view)); @@ -4907,7 +4916,15 @@ class MouseSelection { return this.destroy(); if (this.dragging || this.dragging == null && dist(this.startEvent, event) < 10) return; - this.select(this.lastEvent = event); + // Track the first move immediately. Of the moves arriving before the next + // paint, only the most recent needs hit testing, selection and autoscroll. + this.lastEvent = event; + if (this.moveFrame >= 0) { + this.pendingMove = true; + return; + } + this.moveFrame = this.view.win.requestAnimationFrame(this.finishFrame); + this.select(event); let sx = 0, sy = 0; let left = 0, top = 0, right = this.view.win.innerWidth, bottom = this.view.win.innerHeight; if (this.scrollParents.x) @@ -4926,13 +4943,15 @@ class MouseSelection { this.setScrollSpeed(sx, sy); } up(event) { - if (this.dragging == null) + if (this.dragging == null || this.pendingMove && !this.dragging) this.select(this.lastEvent); if (!this.dragging) event.preventDefault(); this.destroy(); } destroy() { + if (this.moveFrame >= 0) + this.view.win.cancelAnimationFrame(this.moveFrame); this.setScrollSpeed(0, 0); let doc = this.view.contentDOM.ownerDocument; doc.removeEventListener("mousemove", this.move); @@ -6699,6 +6718,14 @@ class ViewState { } gaps.push(gap); }; + let head = this.scrollTarget ? this.scrollTarget.range.head : -1, targetX = null; + if (head >= 0 && !wrapping) { + // Unwrapped lines all centre on the target's x, taken from the target's own line (long or not). The + // target's fraction of another line is 1 above it and negative below it, so every long line above + // the caret was drawn from its end, then back after the scroll. + let line = this.state.doc.lineAt(head), structure = lineStructure(line.from, line.to, this.stateDeco); + targetX = (structure.total ? findFraction(structure, head) * structure.total : 0) * this.heightOracle.charWidth; + } let checkLine = (line) => { if (line.length < doubleMargin || line.type != exports.BlockType.Text) return; @@ -6735,11 +6762,10 @@ class ViewState { } let pxLeft = this.pixelViewport.left + horizOffset, pxRight = this.pixelViewport.right + horizOffset; let left, right; - if (target != null) { - let targetFrac = findFraction(structure, target); - let spaceFrac = ((pxRight - pxLeft) / 2 + marginWidth) / totalWidth; - left = targetFrac - spaceFrac; - right = targetFrac + spaceFrac; + if (targetX != null) { + let space = (pxRight - pxLeft) / 2 + marginWidth; + left = (targetX - space) / totalWidth; + right = (targetX + space) / totalWidth; } else { left = (pxLeft - marginWidth) / totalWidth; @@ -9840,7 +9866,9 @@ const hideNativeSelection = state.Prec.highest(EditorView.theme({ "& :focus": { caretColor: "initial !important", ...drawMainRange ? { - "&::selection, & ::selection": { + // Not `& ::selection` too: a pseudo-class with a universal descendant restyles the + // whole subtree of every element that gains or loses focus. + "&::selection": { backgroundColor: "Highlight !important" } } : {} diff --git a/dist/index.js b/dist/index.js index 66dd7d5..8155d9a 100644 --- a/dist/index.js +++ b/dist/index.js @@ -4882,6 +4882,15 @@ class MouseSelection { this.mustSelect = mustSelect; this.scrollSpeed = { x: 0, y: 0 }; this.scrolling = -1; + this.moveFrame = -1; + this.pendingMove = false; + this.finishFrame = () => { + this.moveFrame = -1; + if (this.pendingMove) { + this.pendingMove = false; + this.move(this.lastEvent); + } + }; this.lastEvent = startEvent; this.scrollParents = scrollableParents(view.contentDOM); this.atoms = view.state.facet(atomicRanges).map(f => f(view)); @@ -4903,7 +4912,15 @@ class MouseSelection { return this.destroy(); if (this.dragging || this.dragging == null && dist(this.startEvent, event) < 10) return; - this.select(this.lastEvent = event); + // Track the first move immediately. Of the moves arriving before the next + // paint, only the most recent needs hit testing, selection and autoscroll. + this.lastEvent = event; + if (this.moveFrame >= 0) { + this.pendingMove = true; + return; + } + this.moveFrame = this.view.win.requestAnimationFrame(this.finishFrame); + this.select(event); let sx = 0, sy = 0; let left = 0, top = 0, right = this.view.win.innerWidth, bottom = this.view.win.innerHeight; if (this.scrollParents.x) @@ -4922,13 +4939,15 @@ class MouseSelection { this.setScrollSpeed(sx, sy); } up(event) { - if (this.dragging == null) + if (this.dragging == null || this.pendingMove && !this.dragging) this.select(this.lastEvent); if (!this.dragging) event.preventDefault(); this.destroy(); } destroy() { + if (this.moveFrame >= 0) + this.view.win.cancelAnimationFrame(this.moveFrame); this.setScrollSpeed(0, 0); let doc = this.view.contentDOM.ownerDocument; doc.removeEventListener("mousemove", this.move); @@ -6694,6 +6713,14 @@ class ViewState { } gaps.push(gap); }; + let head = this.scrollTarget ? this.scrollTarget.range.head : -1, targetX = null; + if (head >= 0 && !wrapping) { + // Unwrapped lines all centre on the target's x, taken from the target's own line (long or not). The + // target's fraction of another line is 1 above it and negative below it, so every long line above + // the caret was drawn from its end, then back after the scroll. + let line = this.state.doc.lineAt(head), structure = lineStructure(line.from, line.to, this.stateDeco); + targetX = (structure.total ? findFraction(structure, head) * structure.total : 0) * this.heightOracle.charWidth; + } let checkLine = (line) => { if (line.length < doubleMargin || line.type != BlockType.Text) return; @@ -6730,11 +6757,10 @@ class ViewState { } let pxLeft = this.pixelViewport.left + horizOffset, pxRight = this.pixelViewport.right + horizOffset; let left, right; - if (target != null) { - let targetFrac = findFraction(structure, target); - let spaceFrac = ((pxRight - pxLeft) / 2 + marginWidth) / totalWidth; - left = targetFrac - spaceFrac; - right = targetFrac + spaceFrac; + if (targetX != null) { + let space = (pxRight - pxLeft) / 2 + marginWidth; + left = (targetX - space) / totalWidth; + right = (targetX + space) / totalWidth; } else { left = (pxLeft - marginWidth) / totalWidth; @@ -9835,7 +9861,9 @@ const hideNativeSelection = /*@__PURE__*/Prec.highest(/*@__PURE__*/EditorView.th "& :focus": { caretColor: "initial !important", ...drawMainRange ? { - "&::selection, & ::selection": { + // Not `& ::selection` too: a pseudo-class with a universal descendant restyles the + // whole subtree of every element that gains or loses focus. + "&::selection": { backgroundColor: "Highlight !important" } } : {}