Skip to content

A platform for community discussion. Free, open, simple.

UX: Open the composer at its final size (#42411)

Renato Atilioe0396e49b5fc
diff --git a/frontend/discourse/app/components/topic-navigation.gjs b/frontend/discourse/app/components/topic-navigation.gjs
index f3c90ce9ded..706ca4a5e90 100644
--- a/frontend/discourse/app/components/topic-navigation.gjs
+++ b/frontend/discourse/app/components/topic-navigation.gjs
@@ -144,7 +144,7 @@ export default class TopicNavigation extends Component {
}
// If composer is open, check we have enough vertical space.
- if (this.composer.isPreviewVisible) {
+ if (this.composer.isPreviewActive) {
return this.heightQuery?.matches ?? false;
}
@@ -153,7 +153,7 @@ export default class TopicNavigation extends Component {
@cached
get heightQuery() {
- const threshold = this.composer.isPreviewVisible
+ const threshold = this.composer.isPreviewActive
? MIN_HEIGHT_TIMELINE + this.composerHeight + headerOffset()
: null;
diff --git a/frontend/discourse/app/components/topic-timeline/container.gjs b/frontend/discourse/app/components/topic-timeline/container.gjs
index e9f2d66c997..1bebeedf4f1 100644
--- a/frontend/discourse/app/components/topic-timeline/container.gjs
+++ b/frontend/discourse/app/components/topic-timeline/container.gjs
@@ -224,10 +224,10 @@ export default class TopicTimelineScrollArea extends Component {
}
get scrollareaHeight() {
- const composerHeight = this.composer.isPreviewVisible
- ? document.getElementById("reply-control").offsetHeight || 0
- : 0,
- headerHeight = document.querySelector(".d-header")?.offsetHeight || 0;
+ const composerHeight = this.composer.isPreviewActive
+ ? document.getElementById("reply-control")?.offsetHeight || 0
+ : 0;
+ const headerHeight = document.querySelector(".d-header")?.offsetHeight || 0;
// scrollarea takes up about half of the timeline's height
const availableHeight =
@@ -235,12 +235,12 @@ export default class TopicTimelineScrollArea extends Component {
const minHeight = this.site.mobileView
? DEFAULT_MIN_SCROLLAREA_HEIGHT
- : this.composer.isPreviewVisible
+ : this.composer.isPreviewActive
? desktopMinScrollAreaHeight
: DEFAULT_MIN_SCROLLAREA_HEIGHT;
const maxHeight = this.site.mobileView
? DEFAULT_MAX_SCROLLAREA_HEIGHT
- : this.composer.isPreviewVisible
+ : this.composer.isPreviewActive
? desktopMaxScrollAreaHeight
: DEFAULT_MAX_SCROLLAREA_HEIGHT;
diff --git a/frontend/discourse/app/services/composer.js b/frontend/discourse/app/services/composer.js
index fc989964471..710966dc596 100644
--- a/frontend/discourse/app/services/composer.js
+++ b/frontend/discourse/app/services/composer.js
@@ -19,6 +19,7 @@ import {
} from "discourse/helpers/slow-mode";
import { popupAjaxError } from "discourse/lib/ajax-error";
import { customPopupMenuOptions } from "discourse/lib/composer/custom-popup-menu-options";
+import { USER_OPTION_COMPOSITION_MODES } from "discourse/lib/constants";
import discourseDebounce from "discourse/lib/debounce";
import { bind } from "discourse/lib/decorators";
import deprecated from "discourse/lib/deprecated";
@@ -120,7 +121,6 @@ export default class ComposerService extends Service {
@service store;
@service toasts;
- @tracked allowPreview = false;
@tracked selectedTranslationLocale = null;
checkedMessages = false;
messageCount = null;
@@ -137,6 +137,7 @@ export default class ComposerService extends Service {
topic = null;
linkLookup = null;
+ @tracked _allowPreview = null;
@tracked _showPreview;
@tracked _isStaffUserOverride;
@@ -251,6 +252,44 @@ export default class ComposerService extends Service {
this._showPreview = value;
}
+ // predicted from the editor mode that will load, so the composer is sized
+ // correctly before the editor mounts and sets the authoritative value
+ get allowPreview() {
+ if (this._allowPreview !== null) {
+ return this._allowPreview;
+ }
+
+ if (!this.currentUser) {
+ return false;
+ }
+
+ if (this.hasFormTemplate) {
+ return this.siteSettings.show_preview_for_form_templates;
+ }
+
+ const forcedMode = applyValueTransformer(
+ "composer-force-editor-mode",
+ null,
+ {
+ model: this.model,
+ }
+ );
+
+ return forcedMode !== null
+ ? forcedMode !== USER_OPTION_COMPOSITION_MODES.rich
+ : !this.currentUser.useRichEditor;
+ }
+
+ set allowPreview(value) {
+ // the editor confirms the prediction during render; skipping the no-op
+ // write avoids dirtying an already-consumed tag
+ if (this.allowPreview === value) {
+ return;
+ }
+
+ this._allowPreview = value;
+ }
+
/**
* @returns {import("discourse/controllers/topic").default};
*/
@@ -262,6 +301,10 @@ export default class ComposerService extends Service {
return this.showPreview && this.allowPreview;
}
+ get isPreviewActive() {
+ return Boolean(this.visible && this.isPreviewVisible);
+ }
+
@computed("model.action", "model.post.can_localize_post")
get showTranslationSelector() {
return (
@@ -2081,7 +2124,11 @@ export default class ComposerService extends Service {
document.activeElement?.blur();
document.documentElement.style.removeProperty("--composer-height");
- this.setProperties({ model: null, lastValidatedAt: null });
+ this.setProperties({
+ model: null,
+ lastValidatedAt: null,
+ _allowPreview: null,
+ });
// This is a temporary solution to reset the saved form template state while we don't store drafts
this.set("formTemplateInitialValues", undefined);
diff --git a/frontend/discourse/tests/acceptance/composer-preview-sizing-test.js b/frontend/discourse/tests/acceptance/composer-preview-sizing-test.js
new file mode 100644
index 00000000000..bed22a43242
--- /dev/null
+++ b/frontend/discourse/tests/acceptance/composer-preview-sizing-test.js
@@ -0,0 +1,40 @@
+import { click, find, settled, visit } from "@ember/test-helpers";
+import { test } from "qunit";
+import { USER_OPTION_COMPOSITION_MODES } from "discourse/lib/constants";
+import { acceptance } from "discourse/tests/helpers/qunit-helpers";
+
+acceptance("Composer preview sizing", function (needs) {
+ needs.user({
+ "user_option.composition_mode": USER_OPTION_COMPOSITION_MODES.markdown,
+ });
+ needs.settings({ allow_uncategorized_topics: true });
+
+ test("markdown has its final preview width before the first opening", async function (assert) {
+ await visit("/");
+ this.owner.lookup("service:composer").showPreview = true;
+ await settled();
+
+ assert
+ .dom("#reply-control")
+ .hasClass("closed", "the composer starts closed");
+ assert
+ .dom("#reply-control")
+ .hasClass("show-preview", "preview width is reserved before opening");
+ const initialMaxWidth = getComputedStyle(find("#reply-control")).maxWidth;
+
+ await click("#create-topic");
+
+ assert.dom("#reply-control").hasClass("open", "the composer opens");
+ assert
+ .dom("#reply-control")
+ .hasClass(
+ "show-preview",
+ "the mounted editor keeps the predicted preview"
+ );
+ assert.strictEqual(
+ getComputedStyle(find("#reply-control")).maxWidth,
+ initialMaxWidth,
+ "opening does not change the width target or trigger horizontal expansion"
+ );
+ });
+});
diff --git a/frontend/discourse/tests/acceptance/topic-timeline-test.js b/frontend/discourse/tests/acceptance/topic-timeline-test.js
index 05afcccb3d8..0e6f9555a26 100644
--- a/frontend/discourse/tests/acceptance/topic-timeline-test.js
+++ b/frontend/discourse/tests/acceptance/topic-timeline-test.js
@@ -1,6 +1,11 @@
import { click, currentURL, visit } from "@ember/test-helpers";
import { test } from "qunit";
-import { acceptance } from "discourse/tests/helpers/qunit-helpers";
+import { setDesktopScrollAreaHeight } from "discourse/components/topic-timeline/container";
+import { USER_OPTION_COMPOSITION_MODES } from "discourse/lib/constants";
+import {
+ acceptance,
+ loggedInUser,
+} from "discourse/tests/helpers/qunit-helpers";
acceptance("Glimmer Topic Timeline", function (needs) {
needs.user({
@@ -333,6 +338,24 @@ acceptance("Glimmer Topic Timeline", function (needs) {
});
});
+ needs.hooks.afterEach(function () {
+ setDesktopScrollAreaHeight();
+ });
+
+ test("predicted composer preview does not affect the closed timeline", async function (assert) {
+ loggedInUser().set(
+ "user_option.composition_mode",
+ USER_OPTION_COMPOSITION_MODES.markdown
+ );
+ setDesktopScrollAreaHeight({ min: 600, max: 600 });
+
+ await visit("/t/internationalization-localization/280/1");
+
+ assert
+ .dom(".timeline-scrollarea")
+ .hasStyle({ height: "300px" }, "the closed composer has no effect");
+ });
+
test("has a reply-to-post button", async function (assert) {
await visit("/t/internationalization-localization");
assert
diff --git a/frontend/discourse/tests/integration/components/topic-navigation-test.gjs b/frontend/discourse/tests/integration/components/topic-navigation-test.gjs
index 3f062eea97d..e89582b53f9 100644
--- a/frontend/discourse/tests/integration/components/topic-navigation-test.gjs
+++ b/frontend/discourse/tests/integration/components/topic-navigation-test.gjs
@@ -1,8 +1,11 @@
-import { render } from "@ember/test-helpers";
+import { render, settled } from "@ember/test-helpers";
import { module, test } from "qunit";
+import sinon from "sinon";
import TopicNavigation from "discourse/components/topic-navigation";
+import { USER_OPTION_COMPOSITION_MODES } from "discourse/lib/constants";
import { forceMobile } from "discourse/lib/mobile";
import { withPluginApi } from "discourse/lib/plugin-api";
+import Composer from "discourse/models/composer";
import { setupRenderingTest } from "discourse/tests/helpers/component-test";
module("Integration | Component | TopicNavigation", function (hooks) {
@@ -51,4 +54,58 @@ module("Integration | Component | TopicNavigation", function (hooks) {
assert.dom(".render-timeline").hasText("true");
assert.dom(".with-timeline").exists();
});
+
+ module("short desktop viewport", function (viewportHooks) {
+ viewportHooks.beforeEach(function () {
+ this.matchMedia = sinon.stub(window, "matchMedia").callsFake((query) => {
+ const matcher = new EventTarget();
+ matcher.matches = query.startsWith("(min-width:");
+ return matcher;
+ });
+ this.currentUser.set(
+ "user_option.composition_mode",
+ USER_OPTION_COMPOSITION_MODES.markdown
+ );
+ this.composer = this.owner.lookup("service:composer");
+ this.composer.showPreview = true;
+ });
+
+ viewportHooks.afterEach(function () {
+ this.matchMedia.restore();
+ });
+
+ test("the preview constrains the timeline only while the composer is visible", async function (assert) {
+ const topic = this.topic;
+
+ await render(<template><TopicNavigation @topic={{topic}} /></template>);
+
+ assert.true(
+ this.composer.isPreviewVisible,
+ "the composer predicts its preview width before opening"
+ );
+ assert
+ .dom(".with-timeline")
+ .exists("a closed composer does not take space from the timeline");
+
+ this.composer.set(
+ "model",
+ this.owner.lookup("service:store").createRecord("composer", {
+ action: Composer.CREATE_TOPIC,
+ composeState: Composer.OPEN,
+ })
+ );
+ await settled();
+
+ assert
+ .dom(".with-topic-progress")
+ .exists("an open preview leaves insufficient room for the timeline");
+
+ this.composer.close();
+ await settled();
+
+ assert
+ .dom(".with-timeline")
+ .exists("closing restores the timeline despite the predicted preview");
+ });
+ });
});
diff --git a/frontend/discourse/tests/unit/services/composer-test.js b/frontend/discourse/tests/unit/services/composer-test.js
new file mode 100644
index 00000000000..71c48f0d27b
--- /dev/null
+++ b/frontend/discourse/tests/unit/services/composer-test.js
@@ -0,0 +1,68 @@
+import { setupTest } from "ember-qunit";
+import { module, test } from "qunit";
+import { USER_OPTION_COMPOSITION_MODES } from "discourse/lib/constants";
+import Composer from "discourse/models/composer";
+import { logIn } from "discourse/tests/helpers/qunit-helpers";
+
+module("Unit | Service | composer", function (hooks) {
+ setupTest(hooks);
+
+ hooks.beforeEach(function () {
+ this.currentUser = logIn(this.owner);
+ this.currentUser.set(
+ "user_option.composition_mode",
+ USER_OPTION_COMPOSITION_MODES.markdown
+ );
+ this.composer = this.owner.lookup("service:composer");
+ this.composer.showPreview = true;
+ });
+
+ function setComposerModel(context) {
+ context.composer.set(
+ "model",
+ context.owner.lookup("service:store").createRecord("composer", {
+ action: Composer.CREATE_TOPIC,
+ composeState: Composer.OPEN,
+ })
+ );
+ }
+
+ test("predicts the markdown preview before a composer model exists", function (assert) {
+ assert.true(
+ this.composer.allowPreview,
+ "markdown allows a preview even before opening"
+ );
+ assert.true(
+ this.composer.isPreviewVisible,
+ "the closed composer reserves its final preview width"
+ );
+ });
+
+ test("predicts no preview for the rich editor before it mounts", function (assert) {
+ this.currentUser.set(
+ "user_option.composition_mode",
+ USER_OPTION_COMPOSITION_MODES.rich
+ );
+ assert.false(this.composer.allowPreview, "the rich editor has no preview");
+ assert.false(
+ this.composer.isPreviewVisible,
+ "the rich editor does not reserve preview space before it mounts"
+ );
+ });
+
+ test("closing clears the mounted editor override", function (assert) {
+ setComposerModel(this);
+ this.composer.set("allowPreview", false);
+
+ assert.false(
+ this.composer.allowPreview,
+ "the mounted editor can override the prediction for this composer"
+ );
+ this.composer.close();
+
+ assert.true(
+ this.composer.allowPreview,
+ "the next composer uses the predicted markdown mode"
+ );
+ });
+});