From 4c1e3ce4b55befc1ed7bc52616753be6a201dc3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9rgio=20Saquetim?= Date: Mon, 23 Dec 2024 19:31:51 -0300 Subject: [PATCH 1/5] DEV: Upgrade post menu buttons to the Glimmer API --- .discourse-compatibility | 1 + README.md | 6 ++- .../initialize-for-raw-post-button.js | 38 +++++++----------- .../discourse/components/show-raw-button.gjs | 40 +++++++++++++++++++ locales/en.yml | 1 + spec/system/raw_post_button_spec.rb | 2 +- 6 files changed, 62 insertions(+), 26 deletions(-) create mode 100644 javascripts/discourse/components/show-raw-button.gjs diff --git a/.discourse-compatibility b/.discourse-compatibility index 0581115..e0a0dde 100644 --- a/.discourse-compatibility +++ b/.discourse-compatibility @@ -1,2 +1,3 @@ +< 3.4.0.beta3-dev: 7ee4963dbcfb9daa1d9391cf9f917b19b7be8ef7 < 3.4.0.beta1-dev: ebdcb7c5fd4fed3beaa4f96a535a038dcfabaa1c < 3.3.0.beta1-dev: 68e25649fb16deba5caab0e63e37103fedd8313e diff --git a/README.md b/README.md index 6b5898b..dc2d614 100644 --- a/README.md +++ b/README.md @@ -1 +1,5 @@ -# raw-post-button \ No newline at end of file +# ** raw-post-button ** + +Raw Post Button will add a button to the post menu. When you click the button, you’ll get a modal / popover with the raw (markdown) version of the post. + +For more information, please see: https://meta.discourse.org/t/raw-post-button/152542 \ No newline at end of file diff --git a/javascripts/discourse/api-initializers/initialize-for-raw-post-button.js b/javascripts/discourse/api-initializers/initialize-for-raw-post-button.js index 32d8b70..c09d9c9 100644 --- a/javascripts/discourse/api-initializers/initialize-for-raw-post-button.js +++ b/javascripts/discourse/api-initializers/initialize-for-raw-post-button.js @@ -1,9 +1,7 @@ -import FullscreenCode from "discourse/components/modal/fullscreen-code"; -import { ajax } from "discourse/lib/ajax"; -import { popupAjaxError } from "discourse/lib/ajax-error"; import { apiInitializer } from "discourse/lib/api"; +import ShowRawButton from "../components/show-raw-button"; -export default apiInitializer("0.8", (api) => { +export default apiInitializer("1.34.0", (api) => { const currentUser = api.getCurrentUser(); if (!currentUser) { return; @@ -15,24 +13,16 @@ export default apiInitializer("0.8", (api) => { return; } - api.addPostMenuButton("show-raw", () => ({ - async action({ post }) { - try { - const response = await ajax(`/posts/${post.id}/raw`, { - dataType: "text", - }); - api.container.lookup("service:modal").show(FullscreenCode, { - model: { - code: response, - }, - }); - } catch (e) { - popupAjaxError(e); - } - }, - icon: "file-alt", - className: "raw-post", - title: themePrefix("button_title"), - position: "second-last-hidden", - })); + api.registerValueTransformer( + "post-menu-buttons", + ({ + value: dag, + context: { lastHiddenButtonKey, secondLastHiddenButtonKey }, + }) => { + dag.add("show-raw", ShowRawButton, { + before: lastHiddenButtonKey, + after: secondLastHiddenButtonKey, + }); + } + ); }); diff --git a/javascripts/discourse/components/show-raw-button.gjs b/javascripts/discourse/components/show-raw-button.gjs new file mode 100644 index 0000000..5c7409e --- /dev/null +++ b/javascripts/discourse/components/show-raw-button.gjs @@ -0,0 +1,40 @@ +import Component from "@glimmer/component"; +import { action } from "@ember/object"; +import { service } from "@ember/service"; +import DButton from "discourse/components/d-button"; +import FullscreenCode from "discourse/components/modal/fullscreen-code"; +import { ajax } from "discourse/lib/ajax"; +import { popupAjaxError } from "discourse/lib/ajax-error"; + +export default class ShowRawButton extends Component { + static hidden = true; + + @service modal; + + @action + async showRaw() { + try { + const response = await ajax(`/posts/${this.args.post.id}/raw`, { + dataType: "text", + }); + this.modal.show(FullscreenCode, { + model: { + code: response, + }, + }); + } catch (e) { + popupAjaxError(e); + } + } + + +} diff --git a/locales/en.yml b/locales/en.yml index 63fc847..53acc57 100644 --- a/locales/en.yml +++ b/locales/en.yml @@ -1,2 +1,3 @@ en: + button_label: "Raw Post" button_title: "Raw Post" diff --git a/spec/system/raw_post_button_spec.rb b/spec/system/raw_post_button_spec.rb index c482c3e..2e4a17f 100644 --- a/spec/system/raw_post_button_spec.rb +++ b/spec/system/raw_post_button_spec.rb @@ -11,7 +11,7 @@ sign_in user visit "/t/#{post.topic_id}" - find(".extra-buttons .raw-post").click + find(".post-controls .actions .post-action-menu__raw-post").click expect(page).to have_css(".fullscreen-code-modal") expect(find(".fullscreen-code-modal code")).to have_text("**This is a test post**") From ec60ddf85554081a5f87d6c01c767b0b191ccca0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9rgio=20Saquetim?= Date: Mon, 23 Dec 2024 19:34:04 -0300 Subject: [PATCH 2/5] Oops, linting --- javascripts/discourse/components/show-raw-button.gjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/javascripts/discourse/components/show-raw-button.gjs b/javascripts/discourse/components/show-raw-button.gjs index 5c7409e..55252cc 100644 --- a/javascripts/discourse/components/show-raw-button.gjs +++ b/javascripts/discourse/components/show-raw-button.gjs @@ -33,7 +33,7 @@ export default class ShowRawButton extends Component { ...attributes @action={{this.showRaw}} @icon="file-lines" - @label={{if this.args.showLabel (themePrefix "button_label")}} + @label={{if @showLabel (themePrefix "button_label")}} @title={{themePrefix "button_title"}} /> From 9dc663abe8b5bfc435542cb335373c94e2c83abb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9rgio=20Saquetim?= Date: Mon, 23 Dec 2024 19:52:07 -0300 Subject: [PATCH 3/5] Fixed spec --- spec/system/raw_post_button_spec.rb | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/spec/system/raw_post_button_spec.rb b/spec/system/raw_post_button_spec.rb index 2e4a17f..b3c10f2 100644 --- a/spec/system/raw_post_button_spec.rb +++ b/spec/system/raw_post_button_spec.rb @@ -11,6 +11,12 @@ sign_in user visit "/t/#{post.topic_id}" + + # test if the button is hidden by default + expect(page).to have_no_css(".post-controls .actions .post-action-menu__raw-post") + + # expand the buttons and click in raw post + find(".post-controls .actions .post-action-menu__show-more").click find(".post-controls .actions .post-action-menu__raw-post").click expect(page).to have_css(".fullscreen-code-modal") From e620c235ba12336926f50096ed20c20624b0429a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9rgio=20Saquetim?= Date: Mon, 23 Dec 2024 20:05:34 -0300 Subject: [PATCH 4/5] Stree --- spec/system/raw_post_button_spec.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/spec/system/raw_post_button_spec.rb b/spec/system/raw_post_button_spec.rb index b3c10f2..bf66af2 100644 --- a/spec/system/raw_post_button_spec.rb +++ b/spec/system/raw_post_button_spec.rb @@ -11,7 +11,6 @@ sign_in user visit "/t/#{post.topic_id}" - # test if the button is hidden by default expect(page).to have_no_css(".post-controls .actions .post-action-menu__raw-post") From a7213307d3d4ba4d585c83c630db99fecea02a33 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9rgio=20Saquetim?= Date: Tue, 24 Dec 2024 10:23:44 -0300 Subject: [PATCH 5/5] Wait dialog to be closed in the action --- javascripts/discourse/components/show-raw-button.gjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/javascripts/discourse/components/show-raw-button.gjs b/javascripts/discourse/components/show-raw-button.gjs index 55252cc..5deceb3 100644 --- a/javascripts/discourse/components/show-raw-button.gjs +++ b/javascripts/discourse/components/show-raw-button.gjs @@ -17,7 +17,7 @@ export default class ShowRawButton extends Component { const response = await ajax(`/posts/${this.args.post.id}/raw`, { dataType: "text", }); - this.modal.show(FullscreenCode, { + await this.modal.show(FullscreenCode, { model: { code: response, },