A platform for community discussion. Free, open, simple.
UX: composer shortcut opens create new message from /messages view (#35941)
Yuriy Kurantaee3db7cddab
diff --git a/config/locales/client.en.yml b/config/locales/client.en.ymlindex 1d1003fae5c..c8d81a94925 100644--- a/config/locales/client.en.yml+++ b/config/locales/client.en.yml@@ -4953,7 +4953,7 @@ en:go_to_unread_post: "%{shortcut} Go to the first unread post"application:title: "Application"- create: "%{shortcut} Create a new topic"+ create: "%{shortcut} Create a new topic or a personal message"notifications: "%{shortcut} Open notifications"hamburger_menu: "%{shortcut} Open navigation menu"user_profile_menu: "%{shortcut} Open user menu"diff --git a/frontend/discourse/app/lib/keyboard-shortcuts.js b/frontend/discourse/app/lib/keyboard-shortcuts.jsindex d273ea622f4..44d50c40f39 100644--- a/frontend/discourse/app/lib/keyboard-shortcuts.js+++ b/frontend/discourse/app/lib/keyboard-shortcuts.js@@ -154,9 +154,11 @@ export default {this.keyTrapper = new keyTrapper();this._stopCallback();- this.searchService = owner.lookup("service:search");this.appEvents = owner.lookup("service:app-events");+ this.composer = owner.lookup("service:composer");this.currentUser = owner.lookup("service:current-user");+ this.router = owner.lookup("service:router");+ this.searchService = owner.lookup("service:search");this.siteSettings = owner.lookup("service:site-settings");this.site = owner.lookup("service:site");@@ -464,32 +466,57 @@ export default {event.preventDefault();- // If the page has a create-topic button, use it for context sensitive attributes like category+ // create new message: if at PM topic route+ if (this.router.currentRouteName.includes("topic")) {+ const topicController = getOwner(this).lookup("controller:topic");+ if (topicController.model.archetype === "private_message") {+ this.composer.open({+ action: Composer.PRIVATE_MESSAGE,+ archetypeId: "private_message",+ draftKey: this.composer.privateMessageDraftKey,+ draftSequence: 0,+ });+ return;+ }+ }++ // create new message: if at messages list route+ if (+ this.router.currentRouteName.includes("userPrivateMessages") ||+ this.router.currentRouteName.includes("group.messages")+ ) {+ const newPMButton = document.querySelector("#new-private-message-btn");+ if (newPMButton) {+ newPMButton.click();+ return;+ }+ }++ // create new topic: context-aware (preserves category, etc.)const createTopicButton = document.querySelector("#create-topic");if (createTopicButton) {createTopicButton.click();return;}- getOwner(this).lookup("service:composer").open({+ // create new topic: default action+ this.composer.open({action: Composer.CREATE_TOPIC,draftKey: Composer.NEW_TOPIC_KEY,});},focusComposer(event) {- const composer = getOwner(this).lookup("service:composer");if (event) {event.preventDefault();event.stopPropagation();}- composer.focusComposer(event);+ this.composer.focusComposer(event);},fullscreenComposer() {- const composer = getOwner(this).lookup("service:composer");- if (composer.get("model")) {- composer.toggleFullscreen();+ if (this.composer.get("model")) {+ this.composer.toggleFullscreen();}},diff --git a/frontend/discourse/app/templates/user/messages.gjs b/frontend/discourse/app/templates/user/messages.gjsindex f771cfa5170..8ebe8a2e8c0 100644--- a/frontend/discourse/app/templates/user/messages.gjs+++ b/frontend/discourse/app/templates/user/messages.gjs@@ -54,6 +54,7 @@ export default <template>@action={{routeAction "composePrivateMessage"}}@icon="envelope"@label="user.new_private_message"+ id="new-private-message-btn"class="btn-primary new-private-message"/>{{/if}}diff --git a/frontend/discourse/tests/acceptance/keyboard-shortcuts-test.js b/frontend/discourse/tests/acceptance/keyboard-shortcuts-test.jsindex 14601c62063..cf59426614c 100644--- a/frontend/discourse/tests/acceptance/keyboard-shortcuts-test.js+++ b/frontend/discourse/tests/acceptance/keyboard-shortcuts-test.js@@ -1,5 +1,5 @@import { click, currentURL, triggerKeyEvent, visit } from "@ember/test-helpers";-import { test } from "qunit";+import { module, test } from "qunit";import { cloneJSON } from "discourse/lib/object";import DiscoveryFixtures from "discourse/tests/fixtures/discovery-fixtures";import { acceptance, chromeTest } from "discourse/tests/helpers/qunit-helpers";@@ -63,7 +63,7 @@ acceptance("Keyboard Shortcuts - Anonymous Users", function (needs) {.exists("pressing k moves selection to previous post");});- // FIXME: For reasons unknown this test if flaky on firefox+ // FIXME: For reasons unknown this test is flaky on firefoxchromeTest("j/k navigation skips hidden elements", async function (assert) {await visit("/t/internationalization-localization/280");@@ -270,4 +270,42 @@ acceptance("Keyboard Shortcuts - Authenticated Users", function (needs) {await click(".modal-close");});++ module("context aware create new shortcuts", function () {+ test("C key opens composer in new topic mode from topics list", async function (assert) {+ await visit("/");+ await triggerKeyEvent(document, "keypress", "C");++ assert+ .dom(".composer-action-create-topic .composer-action-title")+ .includesText(+ i18n("composer.composer_actions.create_topic.desc"),+ "composer shows create topic title"+ );+ });++ test("C key opens composer in new PM mode from messages list", async function (assert) {+ await visit("/my/messages");+ await triggerKeyEvent(document, "keypress", "C");++ assert+ .dom(".composer-action-private-message .composer-action-title")+ .includesText(+ i18n("topic.private_message"),+ "composer shows create message title"+ );+ });++ test("C key opens composer in new PM mode from PM topic", async function (assert) {+ await visit("/t/pm-for-testing/12");+ await triggerKeyEvent(document, "keypress", "C");++ assert+ .dom(".composer-action-private-message .composer-action-title")+ .includesText(+ i18n("topic.private_message"),+ "composer shows create message title"+ );+ });+ });});