Skip to content

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

FIX: use editing-specific text for composer discard and fix false dirty state (#39149)

Régis HANOL18ba9fe86e45
diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml
index e4b588903f9..ba32c32b6cf 100644
--- a/config/locales/client.en.yml
+++ b/config/locales/client.en.yml
@@ -3189,6 +3189,7 @@ en:
title: "Or press %{modifier}Enter"
save_and_close: "Save and close"
discard: "Discard"
+ cancel_edit: "Cancel edit"
draft_saved: "Draft saved!"
users_placeholder: "Add users or groups"
@@ -4544,7 +4545,9 @@ en:
cancel_composer:
confirm: "Do you want to discard your post?"
+ confirm_edit: "Do you want to discard your changes?"
discard: "Discard"
+ discard_edit: "Discard changes"
via_email: "this post arrived via email"
via_auto_generated_email: "this post arrived via an auto generated email"
diff --git a/frontend/discourse/app/components/composer-container.gjs b/frontend/discourse/app/components/composer-container.gjs
index 341fb57b521..9d214bab8f4 100644
--- a/frontend/discourse/app/components/composer-container.gjs
+++ b/frontend/discourse/app/components/composer-container.gjs
@@ -421,17 +421,17 @@ export default class ComposerContainer extends Component {
<DButton
@action={{this.composer.cancel}}
class="discard-button btn-transparent"
- @title="composer.discard"
- @label="composer.discard"
+ @title={{this.composer.cancelLabel}}
+ @label={{this.composer.cancelLabel}}
/>
{{/unless}}
{{#if this.site.mobileView}}
<DButton
@action={{this.composer.cancel}}
- @icon="trash-can"
+ @icon={{this.composer.cancelIcon}}
class="discard-button btn-transparent"
- @title="composer.discard"
+ @title={{this.composer.cancelLabel}}
/>
{{#if this.composer.model.noBump}}
diff --git a/frontend/discourse/app/components/modal/discard-draft.gjs b/frontend/discourse/app/components/modal/discard-draft.gjs
index e7499ed28a2..98e253892a6 100644
--- a/frontend/discourse/app/components/modal/discard-draft.gjs
+++ b/frontend/discourse/app/components/modal/discard-draft.gjs
@@ -25,14 +25,14 @@ export default class DiscardDraftModal extends Component {
>
<:body>
<div class="instructions" role="heading" aria-level="1">
- {{i18n "post.cancel_composer.confirm"}}
+ {{i18n @model.confirmMessageKey}}
</div>
</:body>
<:footer>
<DButton
@icon="trash-can"
- @label="post.cancel_composer.discard"
+ @label={{@model.discardButtonKey}}
@action={{this.discardDraft}}
class="btn-danger discard-draft-modal__discard-btn"
/>
diff --git a/frontend/discourse/app/models/composer.js b/frontend/discourse/app/models/composer.js
index 44ed11c71d0..47cfdd2fef6 100644
--- a/frontend/discourse/app/models/composer.js
+++ b/frontend/discourse/app/models/composer.js
@@ -331,9 +331,13 @@ export default class Composer extends RestModel {
return (this.title || "").trim() !== (this.originalTitle || "").trim();
}
- @computed("replyDirty", "titleDirty", "hasMetaData")
+ @computed("replyDirty", "titleDirty", "canEditTitle", "hasMetaData")
get anyDirty() {
- return this.replyDirty || this.titleDirty || this.hasMetaData;
+ return (
+ this.replyDirty ||
+ (this.canEditTitle && this.titleDirty) ||
+ this.hasMetaData
+ );
}
@dependentKeyCompat
@@ -701,7 +705,7 @@ export default class Composer extends RestModel {
@computed("metaData")
get hasMetaData() {
- return this.metaData ? isEmpty(Object.keys(this.metaData)) : false;
+ return this.metaData ? !isEmpty(Object.keys(this.metaData)) : false;
}
@computed("minimumTitleLength", "titleLength")
@@ -1063,11 +1067,11 @@ export default class Composer extends RestModel {
post,
reply: post.raw,
originalText: post.raw,
+ originalTitle: this.topic.title,
});
if (post.post_number === 1 && this.canEditTitle) {
this.setProperties({
- originalTitle: this.topic.title,
originalTags: this.topic.tags,
});
}
diff --git a/frontend/discourse/app/services/composer.js b/frontend/discourse/app/services/composer.js
index a20f5302b2d..df47877c0a2 100644
--- a/frontend/discourse/app/services/composer.js
+++ b/frontend/discourse/app/services/composer.js
@@ -471,6 +471,18 @@ export default class ComposerService extends Service {
return SAVE_LABELS[this.model?.action];
}
+ @computed("model.editingPost")
+ get cancelLabel() {
+ return this.model?.editingPost
+ ? "composer.cancel_edit"
+ : "composer.discard";
+ }
+
+ @computed("model.editingPost")
+ get cancelIcon() {
+ return this.model?.editingPost ? "xmark" : "trash-can";
+ }
+
@computed("whisperer", "model.action")
get canWhisper() {
return this.whisperer && this.model?.action === Composer.REPLY;
@@ -1756,6 +1768,12 @@ export default class ComposerService extends Service {
if (this.get("model.anyDirty")) {
this.modal.show(DiscardDraftModal, {
model: {
+ confirmMessageKey: this.get("model.editingPost")
+ ? "post.cancel_composer.confirm_edit"
+ : "post.cancel_composer.confirm",
+ discardButtonKey: this.get("model.editingPost")
+ ? "post.cancel_composer.discard_edit"
+ : "post.cancel_composer.discard",
onDestroyDraft: () => {
return this.destroyDraft()
.then(() => {
diff --git a/spec/system/composer/drafts_spec.rb b/spec/system/composer/drafts_spec.rb
index a5a36599edc..3a78890b8a4 100644
--- a/spec/system/composer/drafts_spec.rb
+++ b/spec/system/composer/drafts_spec.rb
@@ -125,6 +125,107 @@ describe "Composer - Drafts" do
end
end
+ context "when discarding while editing a post" do
+ fab!(:post_1) { Fabricate(:post, topic:, user: current_user) }
+
+ before { Jobs.run_immediately! }
+
+ it "does not show the discard modal when there are no changes" do
+ topic_page.visit_topic(post_1.topic)
+ topic_page.click_post_action_button(post_1, :edit)
+
+ expect(composer).to be_opened
+ composer.discard
+
+ expect(discard_draft_modal).to be_closed
+ expect(composer).to be_closed
+ end
+
+ it "does not show the discard modal when editing the first post with no changes" do
+ first_post = topic.first_post
+ first_post.update!(user: current_user)
+
+ topic_page.visit_topic(topic)
+ topic_page.click_post_action_button(first_post, :edit)
+
+ expect(composer).to be_opened
+ composer.discard
+
+ expect(discard_draft_modal).to be_closed
+ expect(composer).to be_closed
+ end
+
+ it "shows the discard modal with editing-specific text when there are changes" do
+ topic_page.visit_topic(post_1.topic)
+ topic_page.click_post_action_button(post_1, :edit)
+ composer.fill_content("edited content here")
+ composer.discard
+
+ expect(discard_draft_modal).to be_open
+ expect(page).to have_css(
+ ".discard-draft-modal",
+ text: I18n.t("js.post.cancel_composer.confirm_edit"),
+ )
+ expect(page).to have_css(
+ ".discard-draft-modal__discard-btn",
+ text: I18n.t("js.post.cancel_composer.discard_edit"),
+ )
+ end
+
+ it "shows the discard modal with editing-specific text when editing the first post" do
+ first_post = topic.first_post
+ first_post.update!(user: current_user)
+
+ topic_page.visit_topic(topic)
+ topic_page.click_post_action_button(first_post, :edit)
+ composer.fill_content("edited content here")
+ composer.discard
+
+ expect(discard_draft_modal).to be_open
+ expect(page).to have_css(
+ ".discard-draft-modal",
+ text: I18n.t("js.post.cancel_composer.confirm_edit"),
+ )
+ end
+
+ it "shows the cancel edit button instead of discard" do
+ topic_page.visit_topic(post_1.topic)
+ topic_page.click_post_action_button(post_1, :edit)
+
+ expect(composer).to be_opened
+ expect(page).to have_css(".discard-button", text: I18n.t("js.composer.cancel_edit"))
+ end
+ end
+
+ context "when discarding while creating a post" do
+ before { Jobs.run_immediately! }
+
+ it "shows the discard modal with post-specific text when there are changes" do
+ visit "/new-topic"
+
+ composer.fill_title("this is a test topic")
+ composer.fill_content("a b c d e f g")
+ composer.discard
+
+ expect(discard_draft_modal).to be_open
+ expect(page).to have_css(
+ ".discard-draft-modal",
+ text: I18n.t("js.post.cancel_composer.confirm"),
+ )
+ expect(page).to have_css(
+ ".discard-draft-modal__discard-btn",
+ text: I18n.t("js.post.cancel_composer.discard"),
+ )
+ end
+
+ it "shows the discard button" do
+ visit "/new-topic"
+
+ expect(composer).to be_opened
+ expect(page).to have_css(".discard-button", text: I18n.t("js.composer.discard"))
+ end
+ end
+
context "when editing different post" do
fab!(:post_1) { Fabricate(:post, topic:, user: current_user) }
fab!(:post_2) { Fabricate(:post, topic:, user: current_user) }