A platform for community discussion. Free, open, simple.
Revert "FIX: improves text selection of posts (#33143)" (#33190)
Joffrey JAFFEUXbd6cf794477e
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 5125745bec2..b3c86583eee 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,8 +201,6 @@ 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 e0a2eb8fb06..6dcaca3ad7c 100644--- a/app/assets/javascripts/discourse/app/components/post-text-selection.gjs+++ b/app/assets/javascripts/discourse/app/components/post-text-selection.gjs@@ -49,6 +49,7 @@ export default class PostTextSelection extends Component {@service siteSettings;@service menu;+ @tracked isSelecting = false;@tracked preventClose = applyValueTransformer("post-text-selection-prevent-close",false@@ -63,17 +64,14 @@ export default class PostTextSelection extends Component {});documentListeners = modifier(() => {- 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,- });+ document.addEventListener("mousedown", this.mousedown, { passive: true });+ document.addEventListener("mouseup", this.mouseup, { passive: true });+ document.addEventListener("selectionchange", this.onSelectionChanged);return () => {- document.removeEventListener("pointerup", this.pointerup);- document.removeEventListener("selectionchange", this.selectionchange);- document.removeEventListener("contextmenu", this.contextmenu);+ document.removeEventListener("mousedown", this.mousedown);+ document.removeEventListener("mouseup", this.mouseup);+ document.removeEventListener("selectionchange", this.onSelectionChanged);};});@@ -103,10 +101,15 @@ export default class PostTextSelection extends Component {if (this.preventClose) {return;}+ this.args.quoteState.clear();await this.menuInstance?.close();}- async selectionChanged(options = {}, cooked, postId) {+ async selectionChanged(options = {}) {+ if (this.isSelecting) {+ return;+ }+const _selectedText = selectedText();const selection = window.getSelection();@@ -131,6 +134,29 @@ 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 = {@@ -140,7 +166,6 @@ export default class PostTextSelection extends Component {: _selectedText === toMarkdown(cooked.innerHTML),};- const _selectedElement = getElement(selectedNode());for (let element = _selectedElement;element && element.tagName !== "ARTICLE";@@ -160,6 +185,7 @@ 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;}@@ -194,8 +220,7 @@ 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- const { isAndroid } = this.capabilities;- offset = isAndroid ? 90 : 70;+ offset = 70;}}@@ -232,72 +257,29 @@ export default class PostTextSelection extends Component {}@bind- contextmenu() {- this.handleSelection();- this.enablePointerEventsOnMenu();- }+ onSelectionChanged() {+ if (this.isSelecting) {+ return;+ }- @bind- selectionchange() {- this.disablePointerEventsOnMenu();+ const { isIOS, isWinphone, isAndroid } = this.capabilities;+ const wait = isIOS || isWinphone || isAndroid ? INPUT_DELAY : 25;+ this.selectionChangeHandler = discourseDebounce(+ this,+ this.selectionChanged,+ wait+ );}@bind- 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");+ mousedown() {+ this.isSelecting = true;}@bind- 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);- }- }+ mouseup() {+ this.isSelecting = false;+ this.onSelectionChanged();}get post() {@@ -327,7 +309,7 @@ export default class PostTextSelection extends Component {if (this.site.mobileView) {this.debouncedSelectionChanged = debounce(this,- this.handleSelection,+ this.selectionChanged,{ force: true },250,falsediff --git a/app/assets/javascripts/discourse/app/lib/utilities.js b/app/assets/javascripts/discourse/app/lib/utilities.jsindex 2e30ad2dc1a..54b81bdf8f8 100644--- a/app/assets/javascripts/discourse/app/lib/utilities.js+++ b/app/assets/javascripts/discourse/app/lib/utilities.js@@ -122,6 +122,12 @@ 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 9018154bda0..ebcaa91cfd4 100644--- a/app/assets/javascripts/discourse/tests/helpers/qunit-helpers.js+++ b/app/assets/javascripts/discourse/tests/helpers/qunit-helpers.js@@ -564,9 +564,6 @@ 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 ecfd5262321..4eb1db94679 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,10 +35,7 @@ export default class DInlineFloat extends Component {<DFloatBody@instance={{@instance}}@trapTab={{@trapTab}}- @mainClass={{concatClass- @mainClass- (concat @instance.options.identifier "-content")- }}+ @mainClass={{@mainClass}}@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 0de02f05dfd..1afa40e3501 100644--- a/app/assets/stylesheets/common/base/topic-post.scss+++ b/app/assets/stylesheets/common/base/topic-post.scss@@ -737,6 +737,10 @@ 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 d8784148a41..9d36a68eac7 100644--- a/app/assets/stylesheets/mobile/topic-post.scss+++ b/app/assets/stylesheets/mobile/topic-post.scss@@ -323,11 +323,6 @@ 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 634dfb2068e..ed3229826e1 100644--- a/spec/support/system_helpers.rb+++ b/spec/support/system_helpers.rb@@ -162,8 +162,6 @@ 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)