Skip to content

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

UX: Use inline title editor instead of composer when editing translation (#36847)

Natalie Tay3bb19668d255
diff --git a/app/assets/stylesheets/common/base/topic.scss b/app/assets/stylesheets/common/base/topic.scss
index 42d18096924..c5644089f7f 100644
--- a/app/assets/stylesheets/common/base/topic.scss
+++ b/app/assets/stylesheets/common/base/topic.scss
@@ -273,6 +273,15 @@
width: 100%;
margin: 0;
}
+
+ .editing-localization-indicator {
+ display: flex;
+ align-items: center;
+ gap: var(--space-1);
+ margin: var(--space-1);
+ font-size: var(--font-down-1);
+ color: var(--primary-medium);
+ }
}
.edit-category__wrapper {
diff --git a/app/controllers/topic_localizations_controller.rb b/app/controllers/topic_localizations_controller.rb
index 7bc7047f541..cbd98e8c5eb 100644
--- a/app/controllers/topic_localizations_controller.rb
+++ b/app/controllers/topic_localizations_controller.rb
@@ -3,6 +3,18 @@
class TopicLocalizationsController < ApplicationController
before_action :ensure_logged_in
+ def show
+ topic = Topic.find_by(id: params[:topic_id])
+ raise Discourse::NotFound unless topic
+
+ guardian.ensure_can_see!(topic)
+
+ localization = TopicLocalization.find_by(topic_id: topic.id, locale: params[:locale])
+ raise Discourse::NotFound unless localization
+
+ render json: TopicLocalizationSerializer.new(localization, root: false)
+ end
+
def create_or_update
topic_id, locale, title = params.require(%i[topic_id locale title])
diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml
index 8268c0acde6..db245895571 100644
--- a/config/locales/client.en.yml
+++ b/config/locales/client.en.yml
@@ -4154,9 +4154,10 @@ en:
deleted_by_author_simple: "(topic deleted by author)"
localizations:
title_edit_warning:
- message: "This is a translated topic title.<br><br>Do you want to edit the original title in %{language} or edit this specific translation?"
- action_original: "Edit original topic title"
+ message: "This is a translated topic title.<br><br>Do you want to edit the <b>original</b> category, tags, and title in %{language} or edit this specific <b>translation</b>?"
+ action_original: "Edit original"
action_translation: "Edit translated topic title"
+ editing_translation: "Editing %{language} translation"
post_list:
title: "Latest posts"
diff --git a/config/routes.rb b/config/routes.rb
index 41e92912f2f..679e98da8bf 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -1274,6 +1274,7 @@ Discourse::Application.routes.draw do
post "/post_localizations/create_or_update", to: "post_localizations#create_or_update"
delete "/post_localizations/destroy", to: "post_localizations#destroy"
+ get "topic_localizations/:topic_id/:locale" => "topic_localizations#show"
post "topic_localizations/create_or_update", to: "topic_localizations#create_or_update"
delete "topic_localizations/destroy", to: "topic_localizations#destroy"
diff --git a/frontend/discourse/app/components/topic-title-editor.gjs b/frontend/discourse/app/components/topic-title-editor.gjs
new file mode 100644
index 00000000000..1353f4ada2a
--- /dev/null
+++ b/frontend/discourse/app/components/topic-title-editor.gjs
@@ -0,0 +1,53 @@
+import Component from "@glimmer/component";
+import { service } from "@ember/service";
+import TextField from "discourse/components/text-field";
+import icon from "discourse/helpers/d-icon";
+import lazyHash from "discourse/helpers/lazy-hash";
+import autoFocus from "discourse/modifiers/auto-focus";
+import { i18n } from "discourse-i18n";
+import PluginOutlet from "./plugin-outlet";
+
+export default class TopicTitleEditor extends Component {
+ @service languageNameLookup;
+ @service siteSettings;
+
+ get translationLocaleName() {
+ return this.languageNameLookup.getLanguageName(this.args.translationLocale);
+ }
+
+ <template>
+ <div class="edit-title__wrapper">
+ {{#if @isEditingLocalization}}
+ <span class="editing-localization-indicator">
+ {{icon "language"}}
+ {{i18n
+ "topic.localizations.editing_translation"
+ language=this.translationLocaleName
+ }}
+ </span>
+ {{/if}}
+ <PluginOutlet
+ @name="edit-topic-title"
+ @outletArgs={{lazyHash model=@model buffered=@buffered}}
+ >
+ {{#if @isEditingLocalization}}
+ <TextField
+ @id="edit-title"
+ @value={{@translationTitle}}
+ @maxlength={{this.siteSettings.max_topic_title_length}}
+ @autofocus={{true}}
+ {{autoFocus}}
+ />
+ {{else}}
+ <TextField
+ @id="edit-title"
+ @value={{@bufferedTitle}}
+ @maxlength={{this.siteSettings.max_topic_title_length}}
+ @autofocus={{true}}
+ {{autoFocus}}
+ />
+ {{/if}}
+ </PluginOutlet>
+ </div>
+ </template>
+}
diff --git a/frontend/discourse/app/controllers/topic.js b/frontend/discourse/app/controllers/topic.js
index 01590c0aac3..3962b1ffca1 100644
--- a/frontend/discourse/app/controllers/topic.js
+++ b/frontend/discourse/app/controllers/topic.js
@@ -34,6 +34,7 @@ import { deepMerge } from "discourse/lib/object";
import { buildQuote } from "discourse/lib/quote";
import QuoteState from "discourse/lib/quote-state";
import { extractLinkMeta } from "discourse/lib/render-topic-featured-link";
+import { fancyTitle } from "discourse/lib/topic-fancy-title";
import { trackedArray } from "discourse/lib/tracked-tools";
import DiscourseURL, { userPath } from "discourse/lib/url";
import { escapeExpression } from "discourse/lib/utilities";
@@ -43,6 +44,7 @@ import Composer from "discourse/models/composer";
import Draft from "discourse/models/draft";
import Post from "discourse/models/post";
import Topic from "discourse/models/topic";
+import TopicLocalization from "discourse/models/topic-localization";
import TopicTimer from "discourse/models/topic-timer";
import { i18n } from "discourse-i18n";
@@ -82,6 +84,9 @@ export default class TopicController extends Controller {
@tracked multiSelect = false;
@tracked hasScrolled = null;
+ @tracked translationLocale = null;
+ @tracked translationTitle = null;
+ @tracked editingTopicLocalization = false;
@trackedArray bookmarks = [];
@trackedArray selectedPostIds = [];
@@ -109,6 +114,8 @@ export default class TopicController extends Controller {
currentPostId = null;
userLastReadPostNumber = null;
highestPostNumber = null;
+ _localizationFetchPromise = null;
+ _originalTranslationTitle = null;
_progressIndex = null;
_retryInProgress = false;
_retryRateLimited = false;
@@ -379,24 +386,29 @@ export default class TopicController extends Controller {
return this.set("editingTopic", true);
}
- if (this.composer.isOpen) {
- return;
- }
-
const topic = this.model;
- const firstPost = await topic.firstPost();
if (canEditTitle && !canLocalize) {
- return this._openComposerForEdit(topic, firstPost);
+ return this.set("editingTopic", true);
}
if (titleLocalized && !canEditTitle) {
- return this._openComposerForEditTranslation(topic, firstPost);
+ this._localizationFetchPromise = TopicLocalization.fetch(
+ topic.id,
+ this.currentUser.effective_locale
+ );
+ return this._startEditingTranslation();
}
if (titleLocalized) {
const topicLocale = topic.locale;
const language = this.languageNameLookup.getLanguageName(topicLocale);
+
+ this._localizationFetchPromise = TopicLocalization.fetch(
+ topic.id,
+ this.currentUser.effective_locale
+ );
+
return this.dialog.alert({
message: i18n("topic.localizations.title_edit_warning.message", {
language,
@@ -407,21 +419,36 @@ export default class TopicController extends Controller {
"topic.localizations.title_edit_warning.action_original"
),
class: "btn-primary",
- action: () => this._openComposerForEdit(topic, firstPost),
+ action: () => this.set("editingTopic", true),
},
{
label: i18n(
"topic.localizations.title_edit_warning.action_translation"
),
class: "btn-default",
- action: () =>
- this._openComposerForEditTranslation(topic, firstPost),
+ action: () => this._startEditingTranslation(),
},
],
});
}
}
+ async _startEditingTranslation() {
+ this.translationLocale = this.currentUser.effective_locale;
+
+ try {
+ const localization = await this._localizationFetchPromise;
+ this.translationTitle = localization?.title || "";
+ this._originalTranslationTitle = this.translationTitle;
+ } catch {
+ this.translationTitle = "";
+ this._originalTranslationTitle = "";
+ }
+
+ this.editingTopicLocalization = true;
+ this.set("editingTopic", true);
+ }
+
@action
jumpTop(event) {
if (event && wantsNewWindow(event)) {
@@ -1209,24 +1236,65 @@ export default class TopicController extends Controller {
cancelEditingTopic() {
this.set("editingTopic", false);
this.buffered.discardChanges();
+ this._resetTranslationState();
}
@action
- finishedEditingTopic() {
+ async finishedEditingTopic() {
if (!this.editingTopic) {
return;
}
-
- // save the modifications
const props = this.get("buffered.buffer");
+ const hasCategoryOrTagChanges =
+ props.category_id !== undefined || props.tags !== undefined;
- Topic.update(this.model, props, { fastEdit: true })
- .then(() => {
- // We roll back on success here because `update` saves the properties to the topic
- this.buffered.discardChanges();
- this.set("editingTopic", false);
- })
- .catch(popupAjaxError);
+ try {
+ if (this.editingTopicLocalization) {
+ await this._saveTopicLocalization();
+ }
+
+ if (hasCategoryOrTagChanges || !this.editingTopicLocalization) {
+ await Topic.update(this.model, props, { fastEdit: true });
+ }
+
+ this.buffered.discardChanges();
+ this._resetTranslationState();
+ this.set("editingTopic", false);
+ } catch (error) {
+ popupAjaxError(error);
+ }
+ }
+
+ async _saveTopicLocalization() {
+ const titleChanged =
+ this.translationTitle !== this._originalTranslationTitle;
+
+ if (!titleChanged) {
+ return;
+ }
+
+ await TopicLocalization.createOrUpdate(
+ this.model.id,
+ this.translationLocale,
+ this.translationTitle
+ );
+
+ if (this.model.fancy_title_localized) {
+ this.model.set(
+ "fancy_title",
+ fancyTitle(
+ this.translationTitle,
+ this.siteSettings.support_mixed_text_direction
+ )
+ );
+ }
+ }
+
+ _resetTranslationState() {
+ this.editingTopicLocalization = false;
+ this.translationLocale = null;
+ this.translationTitle = null;
+ this._localizationFetchPromise = null;
}
@action
diff --git a/frontend/discourse/app/models/composer.js b/frontend/discourse/app/models/composer.js
index e35fa636219..a7bde2beca3 100644
--- a/frontend/discourse/app/models/composer.js
+++ b/frontend/discourse/app/models/composer.js
@@ -1138,17 +1138,12 @@ export default class Composer extends RestModel {
const cooked = this.getCookedHtml();
post.setProperties({ cooked, staged: true });
- // only save post if content changed as topic metadata is already handled above
- const skipPostSave = !this.replyDirty && post.post_number === 1;
- const savePromise = skipPostSave
- ? Promise.resolve({ target: post, responseJson: { post } })
- : post.save(props);
-
return promise
- .then(() => savePromise)
- .then((result) => {
- this.clearState();
- return result;
+ .then(() => {
+ return post.save(props).then((result) => {
+ this.clearState();
+ return result;
+ });
})
.catch(rollback)
.finally(() => {
diff --git a/frontend/discourse/app/models/topic-localization.js b/frontend/discourse/app/models/topic-localization.js
index 9e90fc1d01f..f32bec3bc75 100644
--- a/frontend/discourse/app/models/topic-localization.js
+++ b/frontend/discourse/app/models/topic-localization.js
@@ -2,6 +2,10 @@ import { ajax } from "discourse/lib/ajax";
import RestModel from "discourse/models/rest";
export default class TopicLocalization extends RestModel {
+ static fetch(topicId, locale) {
+ return ajax(`/topic_localizations/${topicId}/${locale}`);
+ }
+
static createOrUpdate(topicId, locale, title) {
return ajax("/topic_localizations/create_or_update", {
type: "POST",
diff --git a/frontend/discourse/app/templates/topic.gjs b/frontend/discourse/app/templates/topic.gjs
index 820e8f528f0..c43612960aa 100644
--- a/frontend/discourse/app/templates/topic.gjs
+++ b/frontend/discourse/app/templates/topic.gjs
@@ -22,7 +22,6 @@ import SelectedPosts from "discourse/components/selected-posts";
import SharedDraftControls from "discourse/components/shared-draft-controls";
import SignupCta from "discourse/components/signup-cta";
import SlowModeInfo from "discourse/components/slow-mode-info";
-import TextField from "discourse/components/text-field";
import TopicAdminMenu from "discourse/components/topic-admin-menu";
import TopicCategory from "discourse/components/topic-category";
import TopicFooterButtons from "discourse/components/topic-footer-buttons";
@@ -35,13 +34,13 @@ import TopicStatus from "discourse/components/topic-status";
import TopicTimeline from "discourse/components/topic-timeline";
import TopicTimerInfo from "discourse/components/topic-timer-info";
import TopicTitle from "discourse/components/topic-title";
+import TopicTitleEditor from "discourse/components/topic-title-editor";
import ageWithTooltip from "discourse/helpers/age-with-tooltip";
import bodyClass from "discourse/helpers/body-class";
import icon from "discourse/helpers/d-icon";
import hideApplicationFooter from "discourse/helpers/hide-application-footer";
import lazyHash from "discourse/helpers/lazy-hash";
import routeAction from "discourse/helpers/route-action";
-import autoFocus from "discourse/modifiers/auto-focus";
import CategoryChooser from "discourse/select-kit/components/category-chooser";
import MiniTagChooser from "discourse/select-kit/components/mini-tag-chooser";
import { and, eq } from "discourse/truth-helpers";
@@ -108,23 +107,14 @@ export default <template>
@shouldShow={{@controller.model.isPrivateMessage}}
/>
- <div class="edit-title__wrapper">
- <PluginOutlet
- @name="edit-topic-title"
- @outletArgs={{lazyHash
- }}
- >
- <TextField
- @id="edit-title"
- @value={{@controller.buffered.title}}
- @maxlength={{@controller.siteSettings.max_topic_title_length}}
- @autofocus={{true}}
- {{autoFocus}}
- />
- </PluginOutlet>
- </div>
+ <TopicTitleEditor
+ @isEditingLocalization={{@controller.editingTopicLocalization}}
+ @translationTitle={{@controller.translationTitle}}
+ @translationLocale={{@controller.translationLocale}}
+ @bufferedTitle={{@controller.buffered.title}}
+ @model={{@controller.model}}
+ @buffered={{@controller.buffered}}
+ />
{{#if @controller.showCategoryChooser}}
<div class="edit-category__wrapper">
diff --git a/frontend/discourse/tests/acceptance/composer-test.js b/frontend/discourse/tests/acceptance/composer-test.js
index 14e4e298ff8..5fd4ae2c99a 100644
--- a/frontend/discourse/tests/acceptance/composer-test.js
+++ b/frontend/discourse/tests/acceptance/composer-test.js
@@ -598,52 +598,6 @@ acceptance(`Composer`, function (needs) {
.includesText("This is the new text for the post", "updates the post");
});
- test("Editing post content calls post save", async function (assert) {
- let postSaveCalled = false;
-
- pretender.put("/posts/:post_id", () => {
- postSaveCalled = true;
- return response(200, { post: { id: 398, post_number: 1 } });
- });
-
- await visit("/t/internationalization-localization/280");
-
- await click(".topic-post[data-post-number='1'] button.show-more-actions");
- await click(".topic-post[data-post-number='1'] button.edit");
-
- await fillIn(".d-editor-input", "This is new post content");
- await click("#reply-control button.create");
-
- assert.dom(".d-editor-input").doesNotExist("closes the composer");
- assert.true(
- postSaveCalled,
- "post.save() should be called when content changes"
- );
- });
-
- test("Editing only topic title does not call post save since content is unchanged", async function (assert) {
- let postSaveCalled = false;
-
- pretender.put("/posts/:post_id", () => {
- postSaveCalled = true;
- return response(200, { post: { id: 398, post_number: 1 } });
- });
-
- await visit("/t/internationalization-localization/280");
-
- await click(".topic-post[data-post-number='1'] button.show-more-actions");
- await click(".topic-post[data-post-number='1'] button.edit");
-
- await fillIn("#reply-title", "This is a new title only");
- await click("#reply-control button.create");
-
- assert.dom(".d-editor-input").doesNotExist("closes the composer");
- assert.false(
- postSaveCalled,
- "post.save() should not be called when only title changes"
- );
- });
-
test("Editing a post stages new content", async function (assert) {
await visit("/t/internationalization-localization/280");
await click(".topic-post button.show-more-actions");
diff --git a/spec/requests/topic_localizations_controller_spec.rb b/spec/requests/topic_localizations_controller_spec.rb
index 8141336d4b3..51d2fd4f2ef 100644
--- a/spec/requests/topic_localizations_controller_spec.rb
+++ b/spec/requests/topic_localizations_controller_spec.rb
@@ -15,6 +15,27 @@ describe TopicLocalizationsController do
sign_in(user)
end
+ describe "#show" do
+ fab!(:topic_localization) { Fabricate(:topic_localization, topic:, locale: "ja") }
+
+ it "returns the localization" do
+ get "/topic_localizations/#{topic.id}/ja.json"
+ expect(response.status).to eq(200)
+ expect(response.parsed_body["title"]).to eq(topic_localization.title)
+ expect(response.parsed_body["locale"]).to eq("ja")
+ end
+
+ it "returns 404 for non-existent localization" do
+ get "/topic_localizations/#{topic.id}/fr.json"
+ expect(response.status).to eq(404)
+ end
+
+ it "returns 404 for non-existent topic" do
+ get "/topic_localizations/-1/ja.json"
+ expect(response.status).to eq(404)
+ end
+ end
+
describe "#create_or_update" do
context "when localization does not exist" do
it "creates a new localization" do
diff --git a/spec/system/content_localization_spec.rb b/spec/system/content_localization_spec.rb
index ae359e8b507..6f6298bc9f4 100644
--- a/spec/system/content_localization_spec.rb
+++ b/spec/system/content_localization_spec.rb
@@ -208,7 +208,7 @@ describe "Content Localization" do
context "for topic titles" do
fab!(:untranslated_topic) { Fabricate(:post).topic }
- it "shows title editor in composer when user can edit localizations" do
+ it "shows inline title editor when user can edit localizations" do
sign_in(japanese_user)
topic_page.visit_topic(untranslated_topic)
@@ -217,41 +217,79 @@ describe "Content Localization" do
expect(topic_page).to have_no_topic_title_editor
topic_page.visit_topic(topic)
- # does not see regular title editor
- # sent directly to translation composer to edit translation
+
topic_page.click_topic_edit_title
- expect(topic_page).to have_no_topic_title_editor
- expect(translation_composer).to have_translation_title(topic_ja_localization.title)
+ expect(topic_page).to have_topic_title_editor
+ expect(topic_page).to have_editing_localization_indicator
+ expect(page).to have_field("edit-title", with: topic_ja_localization.title)
end
- it "opens a dialog for choosing which title to edit for admins" do
- admin.update(locale: "ja") # force the admin to view the jap localizations
+ it "opens a dialog for choosing which title to edit for admins when title is localized" do
+ admin.update(locale: "ja")
sign_in(admin)
topic_page.visit_topic(untranslated_topic)
topic_page.click_topic_edit_title
expect(topic_page).to have_topic_title_editor
+ expect(topic_page).to have_no_editing_localization_indicator
+
+ topic_page.click_topic_title_cancel_edit
topic_page.visit_topic(topic)
- expect(topic_page).to have_topic_title(topic_ja_localization.fancy_title)
+ original_translated_title = topic_ja_localization.fancy_title
+ expect(topic_page).to have_topic_title(original_translated_title)
topic_page.click_topic_edit_title
- # does not see regular title editor
- expect(topic_page).to have_no_topic_title_editor
- # asked to use composer to edit original title
expect(edit_localized_post_dialog).to be_open
+ # Viewing translation - Edit Original
edit_localized_post_dialog.click_yes
- expect(composer).to have_input_title(topic.title)
+ expect(topic_page).to have_topic_title_editor
+ expect(topic_page).to have_no_editing_localization_indicator
+ expect(page).to have_field("edit-title", with: topic.title)
- composer.close
- expect(composer).to be_closed
+ # Viewing translation - Save original title change and displayed title should NOT update (still viewing translation)
+ find("#edit-title").fill_in(with: "New Original Title")
+ topic_page.click_topic_title_submit_edit
+ expect(topic_page).to have_topic_title(original_translated_title)
- # use translation composer to edit localized title
+ # View original - displayed title should update to new original title
+ page.find(TOGGLE_LOCALIZE_BUTTON_SELECTOR).click
+ expect(topic_page).to have_topic_title("New Original Title")
+
+ # switch back to Japanese to test translation editing
+ page.find(TOGGLE_LOCALIZE_BUTTON_SELECTOR).click
+
+ # Viewing translation - Edit translation
topic_page.click_topic_edit_title
expect(edit_localized_post_dialog).to be_open
edit_localized_post_dialog.click_no
- expect(translation_composer).to have_translation_title(topic_ja_localization.title)
+ expect(topic_page).to have_topic_title_editor
+ expect(topic_page).to have_editing_localization_indicator
+ expect(page).to have_field("edit-title", with: topic_ja_localization.title)
+
+ # Viewing translation - Save translation title change and displayed title should update immediately
+ find("#edit-title").fill_in(with: "New Japanese Title")
+ topic_page.click_topic_title_submit_edit
+ expect(topic_page).to have_topic_title("New Japanese Title")
+ page.refresh
+ expect(topic_page).to have_topic_title("New Japanese Title")
+ end
+
+ it "discards changes when cancelled" do
+ admin.update(locale: "ja")
+ sign_in(admin)
+
+ topic_page.visit_topic(topic)
+ original_title = topic_ja_localization.fancy_title
+
+ topic_page.click_topic_edit_title
+ edit_localized_post_dialog.click_no
+
+ find("#edit-title").fill_in(with: "Changed Title")
+ topic_page.click_topic_title_cancel_edit
+
+ expect(topic_page).to have_topic_title(original_title)
end
end
end
diff --git a/spec/system/page_objects/pages/topic.rb b/spec/system/page_objects/pages/topic.rb
index 65deea86198..3abc5d84bcb 100644
--- a/spec/system/page_objects/pages/topic.rb
+++ b/spec/system/page_objects/pages/topic.rb
@@ -202,6 +202,18 @@ module PageObjects
find("#topic-title .submit-edit").click
end
+ def click_topic_title_cancel_edit
+ find("#topic-title .cancel-edit").click
+ end
+
+ def has_editing_localization_indicator?
+ has_css?("#topic-title .editing-localization-indicator")
+ end
+
+ def has_no_editing_localization_indicator?
+ has_no_css?("#topic-title .editing-localization-indicator")
+ end
+
def has_topic_bookmarked?(topic)
within_topic_footer_buttons do
has_css?(".bookmark-menu-trigger.bookmarked", text: "Edit Bookmark")