FIX: Chat composer shortcuts should respect context (#21130)

This commit fixes an issue where if you pressed a format
shortcut (e.g. bold, italic, code) for the composer and
you had the thread panel open as well, the shortcut would
trigger in both composers, not just the one that was focused.
This commit is contained in:
Martin Brennan 2023-04-19 09:07:52 +10:00 committed by GitHub
parent a8cf8e57b4
commit 5b3420d854
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 56 additions and 16 deletions

View File

@ -45,6 +45,7 @@
@placeholder={{this.placeholder}} @placeholder={{this.placeholder}}
@focus-in={{action "onTextareaFocusIn" value="target"}} @focus-in={{action "onTextareaFocusIn" value="target"}}
@rows={{1}} @rows={{1}}
data-chat-composer-context={{@context}}
/> />
{{#if this.isNetworkUnreliable}} {{#if this.isNetworkUnreliable}}

View File

@ -108,7 +108,10 @@ export default Component.extend(TextareaTextManipulation, {
this.set("ready", true); this.set("ready", true);
}, },
_modifySelection(opts = { type: null }) { _modifySelection(opts = { type: null, context: null }) {
if (opts.context !== this.context) {
return;
}
const sel = this.getSelected("", { lineVal: true }); const sel = this.getSelected("", { lineVal: true });
if (opts.type === "bold") { if (opts.type === "bold") {
this.applySurround(sel, "**", "**", "bold_text"); this.applySurround(sel, "**", "**", "bold_text");

View File

@ -58,7 +58,10 @@ export default {
} }
event.preventDefault(); event.preventDefault();
event.stopPropagation(); event.stopPropagation();
appEvents.trigger("chat:modify-selection", { type }); appEvents.trigger("chat:modify-selection", {
type,
context: event.target.dataset.chatComposerContext,
});
}; };
const openInsertLinkModal = (event) => { const openInsertLinkModal = (event) => {

View File

@ -20,26 +20,24 @@ RSpec.describe "Shortcuts | chat composer", type: :system, js: true do
end end
context "when using meta + b" do context "when using meta + b" do
xit "adds bold text" do it "adds bold text" do
chat.visit_channel(channel_1) chat.visit_channel(channel_1)
within(".chat-composer-input") do |composer| composer = find(".chat-composer-input")
composer.send_keys([key_modifier, "b"]) composer.send_keys([key_modifier, "b"])
expect(composer.value).to eq("**strong text**") expect(composer.value).to eq("**strong text**")
end
end end
end end
context "when using meta + i" do context "when using meta + i" do
xit "adds italic text" do it "adds italic text" do
chat.visit_channel(channel_1) chat.visit_channel(channel_1)
within(".chat-composer-input") do |composer| composer = find(".chat-composer-input")
composer.send_keys([key_modifier, "i"]) composer.send_keys([key_modifier, "i"])
expect(composer.value).to eq("_emphasized text_") expect(composer.value).to eq("_emphasized text_")
end
end end
end end
@ -47,11 +45,46 @@ RSpec.describe "Shortcuts | chat composer", type: :system, js: true do
it "adds preformatted text" do it "adds preformatted text" do
chat.visit_channel(channel_1) chat.visit_channel(channel_1)
within(".chat-composer-input") do |composer| composer = find(".chat-composer-input")
composer.send_keys([key_modifier, "e"]) composer.send_keys([key_modifier, "e"])
expect(composer.value).to eq("`indent preformatted text by 4 spaces`") expect(composer.value).to eq("`indent preformatted text by 4 spaces`")
end end
end
context "when the thread panel is also open" do
fab!(:user_2) { Fabricate(:user) }
fab!(:thread) do
chat_thread_chain_bootstrap(
channel: channel_1,
users: [current_user, user_2],
messages_count: 2,
)
end
before do
SiteSetting.enable_experimental_chat_threaded_discussions = true
channel_1.update!(threading_enabled: true)
end
it "directs the shortcut to the focused composer" do
chat.visit_channel(channel_1)
channel_page.message_thread_indicator(thread.original_message).click
composer = find(".chat-composer-input--channel")
thread_composer = find(".chat-composer-input--thread")
composer.send_keys([key_modifier, "i"])
expect(composer.value).to eq("_emphasized text_")
expect(thread_composer.value).to eq("")
composer.fill_in(with: "")
thread_composer.fill_in(with: "")
thread_composer.send_keys([key_modifier, "i"])
expect(composer.value).to eq("")
expect(thread_composer.value).to eq("_emphasized text_")
end end
end end