A platform for community discussion. Free, open, simple.
FIX: improves text selection of posts (#33143)
Joffrey JAFFEUX876dbe66299a
diff --git a/app/assets/javascripts/discourse/app/components/post-text-selection-toolbar.gjs b/app/assets/javascripts/discourse/app/components/post-text-selection-toolbar.gjsindex b3c86583eee..5125745bec2 100644--- a/app/assets/javascripts/discourse/app/components/post-text-selection-toolbar.gjs+++ b/app/assets/javascripts/discourse/app/components/post-text-selection-toolbar.gjs@@ -201,6 +201,8 @@ export default class PostTextSelectionToolbar extends Component {<div{{on "mousedown" this.trapEvents}}{{on "mouseup" this.trapEvents}}+ {{on "pointerup" this.trapEvents}}+ {{on "pointerdown" this.trapEvents}}class={{concatClass"quote-button""visible"diff --git a/app/assets/javascripts/discourse/app/components/post-text-selection.gjs b/app/assets/javascripts/discourse/app/components/post-text-selection.gjsindex 6dcaca3ad7c..e0a2eb8fb06 100644--- a/app/assets/javascripts/discourse/app/components/post-text-selection.gjs+++ b/app/assets/javascripts/discourse/app/components/post-text-selection.gjs@@ -49,7 +49,6 @@ export default class PostTextSelection extends Component {@service siteSettings;@service menu;- @tracked isSelecting = false;@tracked preventClose = applyValueTransformer("post-text-selection-prevent-close",false@@ -64,14 +63,17 @@ export default class PostTextSelection extends Component {});documentListeners = modifier(() => {- document.addEventListener("mousedown", this.mousedown, { passive: true });- document.addEventListener("mouseup", this.mouseup, { passive: true });- document.addEventListener("selectionchange", this.onSelectionChanged);+ document.addEventListener("pointerup", this.pointerup, { passive: true });+ document.addEventListener("selectionchange", this.selectionchange);+ // fires when user finishes adjusting text selection on android+ document.addEventListener("contextmenu", this.contextmenu, {+ passive: true,+ });return () => {- document.removeEventListener("mousedown", this.mousedown);- document.removeEventListener("mouseup", this.mouseup);- document.removeEventListener("selectionchange", this.onSelectionChanged);+ document.removeEventListener("pointerup", this.pointerup);+ document.removeEventListener("selectionchange", this.selectionchange);+ document.removeEventListener("contextmenu", this.contextmenu);};});@@ -101,15 +103,10 @@ export default class PostTextSelection extends Component {if (this.preventClose) {return;}- this.args.quoteState.clear();await this.menuInstance?.close();}- async selectionChanged(options = {}) {- if (this.isSelecting) {- return;- }-+ async selectionChanged(options = {}, cooked, postId) {const _selectedText = selectedText();const selection = window.getSelection();@@ -134,29 +131,6 @@ export default class PostTextSelection extends Component {this.prevSelectedText = _selectedText;- // ensure we selected content inside 1 post *only*- let postId;- for (let r = 0; r < selection.rangeCount; r++) {- const range = selection.getRangeAt(r);- const selectionStart = getElement(range.startContainer);- const ancestor = getElement(range.commonAncestorContainer);-- if (!selectionStart.closest(".cooked")) {- return await this.hideToolbar();- }-- postId ||= ancestor.closest(".boxed, .reply")?.dataset?.postId;-- if (!ancestor.closest(".contents") || !postId) {- return await this.hideToolbar();- }- }-- const _selectedElement = getElement(selectedNode());- const cooked =- _selectedElement.querySelector(".cooked") ||- _selectedElement.closest(".cooked");-// computing markdown takes a lot of time on long posts// this code attempts to compute it only when we can't fast tracklet opts = {@@ -166,6 +140,7 @@ export default class PostTextSelection extends Component {: _selectedText === toMarkdown(cooked.innerHTML),};+ const _selectedElement = getElement(selectedNode());for (let element = _selectedElement;element && element.tagName !== "ARTICLE";@@ -185,7 +160,6 @@ export default class PostTextSelection extends Component {let supportsFastEdit = this.canEditPost;const start = getElement(selection.getRangeAt(0).startContainer);-if (!start || start.closest(CSS_TO_DISABLE_FAST_EDIT)) {supportsFastEdit = false;}@@ -220,7 +194,8 @@ export default class PostTextSelection extends Component {// so we need more space// - the end of the selection is not in viewport, in this case our menu will be shown at the top// of the screen, so we need more space to avoid overlapping with the native menu- offset = 70;+ const { isAndroid } = this.capabilities;+ offset = isAndroid ? 90 : 70;}}@@ -257,29 +232,72 @@ export default class PostTextSelection extends Component {}@bind- onSelectionChanged() {- if (this.isSelecting) {- return;- }+ contextmenu() {+ this.handleSelection();+ this.enablePointerEventsOnMenu();+ }- const { isIOS, isWinphone, isAndroid } = this.capabilities;- const wait = isIOS || isWinphone || isAndroid ? INPUT_DELAY : 25;- this.selectionChangeHandler = discourseDebounce(- this,- this.selectionChanged,- wait- );+ @bind+ selectionchange() {+ this.disablePointerEventsOnMenu();}@bind- mousedown() {- this.isSelecting = true;+ pointerup() {+ this.handleSelection();+ this.enablePointerEventsOnMenu();+ }++ enablePointerEventsOnMenu() {+ document+ .querySelector(".post-text-selection-toolbar-content")+ ?.classList.remove("-disable-pointer-events");+ }++ disablePointerEventsOnMenu() {+ // on android there's no reliable way to know when user has finished text selection+ // so we use contextmenu which will be fired while holding the selection even if+ // the user didn't release the pointer yet, as a result menu will be shown+ // while selecting and we want to be sure it's not interfering+ document+ .querySelector(".post-text-selection-toolbar-content")+ ?.classList.add("-disable-pointer-events");}@bind- mouseup() {- this.isSelecting = false;- this.onSelectionChanged();+ handleSelection(options = {}) {+ const selection = window.getSelection();+ if (selection.rangeCount) {+ const range = selection.getRangeAt(0);+ if (range.collapsed) {+ this.args.quoteState.clear();+ return;+ }++ const parent =+ range.commonAncestorContainer.nodeType === Node.TEXT_NODE+ ? range.commonAncestorContainer.parentNode+ : range.commonAncestorContainer;+ const cooked = parent.closest(".cooked");+ if (cooked) {+ const article = cooked.closest(".boxed, .reply");+ const postId = article.dataset.postId;+ const { isIOS, isWinphone, isAndroid } = this.capabilities;+ const wait = isIOS || isWinphone || isAndroid ? INPUT_DELAY : 25;+ this.selectionChangeHandler = discourseDebounce(+ this,+ this.selectionChanged,+ options,+ cooked,+ postId,+ wait+ );+ } else {+ this.args.quoteState.clear();+ this.hideToolbar();+ cancel(this.selectionChangeHandler);+ }+ }}get post() {@@ -309,7 +327,7 @@ export default class PostTextSelection extends Component {if (this.site.mobileView) {this.debouncedSelectionChanged = debounce(this,- this.selectionChanged,+ this.handleSelection,{ force: true },250,falsediff --git a/app/assets/javascripts/discourse/app/lib/utilities.js b/app/assets/javascripts/discourse/app/lib/utilities.jsindex 54b81bdf8f8..2e30ad2dc1a 100644--- a/app/assets/javascripts/discourse/app/lib/utilities.js+++ b/app/assets/javascripts/discourse/app/lib/utilities.js@@ -122,12 +122,6 @@ export function selectedText() {? range.commonAncestorContainer: range.commonAncestorContainer.parentElement;- // ensure we never quote text in the post menu area- const postMenuArea = ancestor.querySelector(".post-menu-area");- if (postMenuArea) {- range.setEndBefore(postMenuArea);- }-const oneboxTest = ancestor.closest("aside.onebox[data-onebox-src]");const codeBlockTest = ancestor.closest("pre");if (codeBlockTest) {diff --git a/app/assets/javascripts/discourse/tests/helpers/qunit-helpers.js b/app/assets/javascripts/discourse/tests/helpers/qunit-helpers.jsindex ebcaa91cfd4..9018154bda0 100644--- a/app/assets/javascripts/discourse/tests/helpers/qunit-helpers.js+++ b/app/assets/javascripts/discourse/tests/helpers/qunit-helpers.js@@ -564,6 +564,9 @@ export async function selectText(selector, endOffset = null) {performSelection();+ const event = new PointerEvent("pointerup");+ document.dispatchEvent(event);+await settled();}diff --git a/app/assets/javascripts/float-kit/addon/components/d-inline-float.gjs b/app/assets/javascripts/float-kit/addon/components/d-inline-float.gjsindex 4eb1db94679..ecfd5262321 100644--- a/app/assets/javascripts/float-kit/addon/components/d-inline-float.gjs+++ b/app/assets/javascripts/float-kit/addon/components/d-inline-float.gjs@@ -35,7 +35,10 @@ export default class DInlineFloat extends Component {<DFloatBody@instance={{@instance}}@trapTab={{@trapTab}}- @mainClass={{@mainClass}}+ @mainClass={{concatClass+ @mainClass+ (concat @instance.options.identifier "-content")+ }}@innerClass={{@innerClass}}@role={{@role}}@portalOutletElement={{@instance.portalOutletElement}}diff --git a/app/assets/stylesheets/common/base/topic-post.scss b/app/assets/stylesheets/common/base/topic-post.scssindex 1afa40e3501..0de02f05dfd 100644--- a/app/assets/stylesheets/common/base/topic-post.scss+++ b/app/assets/stylesheets/common/base/topic-post.scss@@ -737,10 +737,6 @@ aside.quote {opacity: 0.4;}-.fk-d-menu[data-identifier="post-text-selection-toolbar"] {- @include user-select(none);-}-.quote-button {flex-direction: column;diff --git a/app/assets/stylesheets/mobile/topic-post.scss b/app/assets/stylesheets/mobile/topic-post.scssindex 9d36a68eac7..d8784148a41 100644--- a/app/assets/stylesheets/mobile/topic-post.scss+++ b/app/assets/stylesheets/mobile/topic-post.scss@@ -323,6 +323,11 @@ blockquote {margin-right: 0;}+// most importantly for android to prevent the quote menu to interfere with the text selection+.post-text-selection-toolbar-content.-disable-pointer-events {+ pointer-events: none;+}+pre.codeblock-buttons code {padding-right: 2.75em;}diff --git a/spec/support/system_helpers.rb b/spec/support/system_helpers.rbindex ed3229826e1..634dfb2068e 100644--- a/spec/support/system_helpers.rb+++ b/spec/support/system_helpers.rb@@ -162,6 +162,8 @@ module SystemHelpersrange.setEnd(node, arguments[1] + arguments[2]);selection.removeAllRanges();selection.addRange(range);+ const event = new PointerEvent("pointerup");+ document.dispatchEvent(event);JSpage.execute_script(js, selector, start, offset)