A platform for community discussion. Free, open, simple.
FEAT: add copy button to code blocks in chat (#38332)
Steven Changc9aee79707d5
diff --git a/plugins/chat/assets/javascripts/discourse/initializers/chat-decorators.js b/plugins/chat/assets/javascripts/discourse/initializers/chat-decorators.jsindex a0d5424ed7c..510f75e53a2 100644--- a/plugins/chat/assets/javascripts/discourse/initializers/chat-decorators.js+++ b/plugins/chat/assets/javascripts/discourse/initializers/chat-decorators.js@@ -1,4 +1,5 @@import { decorateGithubOneboxBody } from "discourse/instance-initializers/onebox-decorators";+import CodeblockButtons from "discourse/lib/codeblock-buttons";import { samePrefix } from "discourse/lib/get-url";import { decorateHashtags } from "discourse/lib/hashtag-decorator";import highlightSyntax from "discourse/lib/highlight-syntax";@@ -70,6 +71,28 @@ export default {api.decorateChatMessage((element) => decorateHashtags(element, site), {id: "hashtagIcons",});++ api.decorateChatMessage(+ (element) => {+ if (!siteSettings.chat_show_copy_button_on_codeblocks) {+ return;+ }+ if (!element.querySelector("pre > code")) {+ return;+ }+ const cb = new CodeblockButtons({+ site,+ showFullscreen: true,+ showCopy: true,+ });+ cb.attachToGeneric(element);++ return cb.cleanup;+ },+ {+ id: "codeblockButtons",+ }+ );},_getScrollParent(node, maxParentSelector) {diff --git a/plugins/chat/config/locales/server.en.yml b/plugins/chat/config/locales/server.en.ymlindex 0c32054c884..853678d8486 100644--- a/plugins/chat/config/locales/server.en.yml+++ b/plugins/chat/config/locales/server.en.yml@@ -15,6 +15,7 @@ en:chat_duplicate_message_sensitivity: "The likelihood that a duplicate message by the same sender will be blocked in a short period. Decimal number between 0 and 1.0, with 1.0 being the highest setting (blocks messages more frequently in a shorter amount of time). Set to `0` to allow duplicate messages."chat_minimum_message_length: "Minimum number of characters for a chat message."chat_allow_uploads: "Allow uploads in public chat channels and direct message channels."+ chat_show_copy_button_on_codeblocks: "Show copy button on codeblocks in chat messages."chat_archive_destination_topic_status: "The status that the destination topic should be once a channel archive is completed. This only applies when the destination topic is a new topic, not an existing one."direct_message_enabled_groups: "Allow users within these groups to create user-to-user Personal Chats. Note: staff can always create Personal Chats, and users will be able to reply to Personal Chats initiated by users who have permission to create them."chat_message_flag_allowed_groups: "Users in these groups are allowed to flag chat messages. Note that admins and moderators can always flag chat messages."diff --git a/plugins/chat/config/settings.yml b/plugins/chat/config/settings.ymlindex d060b76e712..763998623b4 100644--- a/plugins/chat/config/settings.yml+++ b/plugins/chat/config/settings.yml@@ -98,6 +98,10 @@ chat:default: trueclient: truevalidator: "Chat::AllowUploadsValidator"+ chat_show_copy_button_on_codeblocks:+ client: true+ default: true+ hidden: truemax_chat_auto_joined_users:min: 0default: 10000diff --git a/plugins/chat/spec/system/chat_message_codeblock_spec.rb b/plugins/chat/spec/system/chat_message_codeblock_spec.rbnew file mode 100644index 00000000000..0aa631a9f7c--- /dev/null+++ b/plugins/chat/spec/system/chat_message_codeblock_spec.rb@@ -0,0 +1,43 @@+# frozen_string_literal: true++RSpec.describe "Chat message codeblock", type: :system do+ let(:chat_page) { PageObjects::Pages::Chat.new }+ let(:channel_page) { PageObjects::Pages::ChatChannel.new }++ fab!(:current_user, :user)+ fab!(:channel_1, :category_channel)++ before do+ chat_system_bootstrap+ channel_1.add(current_user)+ sign_in(current_user)+ end++ context "when sending a message with a codeblock" do+ it "renders the codeblock and shows the copy button" do+ SiteSetting.chat_show_copy_button_on_codeblocks = true+ chat_page.visit_channel(channel_1)+ channel_page.send_message(+ "Here is some code:\n```ruby\ndef hello\n puts 'Hello, world!'\nend\n```",+ )++ expect(page).to have_selector(".chat-message .codeblock-buttons")+ expect(page).to have_selector(".chat-message .codeblock-buttons .codeblock-button-wrapper")+ end+ end++ context "when the setting to show copy button on codeblocks is disabled" do+ it "renders the codeblock but does not show the copy button" do+ SiteSetting.chat_show_copy_button_on_codeblocks = false+ chat_page.visit_channel(channel_1)+ channel_page.send_message(+ "Here is some code:\n```ruby\ndef hello\n puts 'Hello, world!'\nend\n```",+ )++ expect(page).to have_selector(".chat-message pre code")+ expect(page).not_to have_selector(+ ".chat-message .codeblock-buttons .codeblock-button-wrapper",+ )+ end+ end+end