A platform for community discussion. Free, open, simple.
FEATURE: Show dialog on translated post to choose to edit original or translated (#33184)
Keegan George0a62f641d56d
diff --git a/app/assets/javascripts/discourse/app/components/d-editor-original-translation-preview.gjs b/app/assets/javascripts/discourse/app/components/d-editor-original-translation-preview.gjsnew file mode 100644index 00000000000..aaf32918cd2--- /dev/null+++ b/app/assets/javascripts/discourse/app/components/d-editor-original-translation-preview.gjs@@ -0,0 +1,30 @@+import Component from "@glimmer/component";+import { service } from "@ember/service";+import { htmlSafe } from "@ember/template";+import CookText from "discourse/components/cook-text";+import { i18n } from "discourse-i18n";++export default class DEditorOriginalTranslationPreview extends Component {+ @service siteSettings;++ get originalLocale() {+ return this.args.model.postLocale;+ }++ <template>+ <div class="d-editor-translation-preview-wrapper">+ <span class="d-editor-translation-preview-wrapper__header">+ {{i18n "composer.translations.original_content"}}+ <span class="d-editor-translation-preview-wrapper__original-locale">+ {{this.originalLocale}}+ </span>+ </span>++ {{#if @model.cookedPost}}+ {{htmlSafe @model.cookedPost}}+ {{else if @model.rawPost}}+ <CookText @rawText={{@model.rawPost}} />+ {{/if}}+ </div>+ </template>+}diff --git a/app/assets/javascripts/discourse/app/components/d-editor.gjs b/app/assets/javascripts/discourse/app/components/d-editor.gjsindex db15fdbb687..c6a218ee13e 100644--- a/app/assets/javascripts/discourse/app/components/d-editor.gjs+++ b/app/assets/javascripts/discourse/app/components/d-editor.gjs@@ -757,12 +757,19 @@ export default class DEditor extends Component {/></div></div>- <DEditorPreview- @preview={{if @hijackPreview @hijackPreview this.preview}}- @forcePreview={{this.forcePreview}}- @onPreviewUpdated={{this.previewUpdated}}- @outletArgs={{this.outletArgs}}- />++ {{#if @hijackPreview}}+ <div class="d-editor-preview-wrapper">+ <@hijackPreview.component @model={{@hijackPreview.model}} />+ </div>+ {{else}}+ <DEditorPreview+ @preview={{this.preview}}+ @forcePreview={{this.forcePreview}}+ @onPreviewUpdated={{this.previewUpdated}}+ @outletArgs={{this.outletArgs}}+ />+ {{/if}}</div></template>}diff --git a/app/assets/javascripts/discourse/app/components/modal/post-translations.gjs b/app/assets/javascripts/discourse/app/components/modal/post-translations.gjsindex 1cac252813b..cc4b01faa50 100644--- a/app/assets/javascripts/discourse/app/components/modal/post-translations.gjs+++ b/app/assets/javascripts/discourse/app/components/modal/post-translations.gjs@@ -5,7 +5,9 @@ import { action } from "@ember/object";import { service } from "@ember/service";import ConditionalLoadingSpinner from "discourse/components/conditional-loading-spinner";import DButton from "discourse/components/d-button";+import DEditorOriginalTranslationPreview from "discourse/components/d-editor-original-translation-preview";import DModal from "discourse/components/d-modal";+import { ajax } from "discourse/lib/ajax";import { popupAjaxError } from "discourse/lib/ajax-error";import Composer from "discourse/models/composer";import PostLocalization from "discourse/models/post-localization";@@ -26,21 +28,6 @@ export default class PostTranslationsModal extends Component {this.loadPostLocalizations();}- get originalPostContent() {- const originalLocale =- this.args.model.post?.locale || this.siteSettings.default_locale;-- return `<div class='d-editor-translation-preview-wrapper'>- <span class='d-editor-translation-preview-wrapper__header'>- ${i18n("composer.translations.original_content")}- <span class='d-editor-translation-preview-wrapper__original-locale'>- ${originalLocale}- </span>- </span>- ${this.args.model.post.cooked}- </div>`;- }-async loadPostLocalizations() {this.loading = true;@@ -68,21 +55,26 @@ export default class PostTranslationsModal extends Component {this.args.closeModal();+ const originalLocale = this.args.model.post?.locale;++ const { raw } = await ajax(`/posts/${this.args.model.post.id}.json`);+const composerOpts = {action: Composer.ADD_TRANSLATION,draftKey: "translation",warningsDisabled: true,- hijackPreview: this.originalPostContent,+ hijackPreview: {+ component: DEditorOriginalTranslationPreview,+ model: {+ postLocale: originalLocale,+ rawPost: raw,+ },+ },post: this.args.model.post,selectedTranslationLocale: locale.locale,};- if (locale?.topic_localization) {- composerOpts.topicTitle = locale.topic_localization?.title;- }-await this.composer.open(composerOpts);- this.composer.model.set("reply", locale.raw);}@actiondiff --git a/app/assets/javascripts/discourse/app/components/post-text-selection-toolbar.gjs b/app/assets/javascripts/discourse/app/components/post-text-selection-toolbar.gjsindex bfd3d0b9430..7f1614cdb62 100644--- a/app/assets/javascripts/discourse/app/components/post-text-selection-toolbar.gjs+++ b/app/assets/javascripts/discourse/app/components/post-text-selection-toolbar.gjs@@ -15,10 +15,12 @@ import { i18n } from "discourse-i18n";export default class PostTextSelectionToolbar extends Component {@service currentUser;@service modal;+ @service dialog;@service site;@service siteSettings;@service appEvents;@service toasts;+ @service composer;get topic() {return this.args.data.topic;diff --git a/app/assets/javascripts/discourse/app/components/post-text-selection.gjs b/app/assets/javascripts/discourse/app/components/post-text-selection.gjsindex d1f69d6806b..a8f9749d489 100644--- a/app/assets/javascripts/discourse/app/components/post-text-selection.gjs+++ b/app/assets/javascripts/discourse/app/components/post-text-selection.gjs@@ -306,6 +306,13 @@ export default class PostTextSelection extends Component {supportsFastEdit = false;}+ if (+ this.post?.is_localized &&+ this.siteSettings.content_localization_enabled+ ) {+ supportsFastEdit = false;+ }+if (supportsFastEdit) {const regexp = new RegExp(escapeRegExp(quoteState.buffer), "gi");const matches = cooked.innerHTML.match(regexp);diff --git a/app/assets/javascripts/discourse/app/components/post-translation-editor.gjs b/app/assets/javascripts/discourse/app/components/post-translation-editor.gjsindex 1acec2227d3..9c6d27502c5 100644--- a/app/assets/javascripts/discourse/app/components/post-translation-editor.gjs+++ b/app/assets/javascripts/discourse/app/components/post-translation-editor.gjs@@ -14,6 +14,27 @@ export default class PostTranslationEditor extends Component {@service composer;@service siteSettings;+ constructor() {+ super(...arguments);+ this.initializeFromSelectedLocale();+ }++ async initializeFromSelectedLocale() {+ if (this.composer.selectedTranslationLocale && !this.composer.model.reply) {+ const localization = await this.findCurrentLocalization();+ if (localization) {+ this.composer.model.set("reply", localization.raw);++ if (localization?.topic_localization) {+ this.composer.model.set(+ "title",+ localization.topic_localization.title+ );+ }+ }+ }+ }+async findCurrentLocalization() {try {const { post_localizations } = await PostLocalization.find(diff --git a/app/assets/javascripts/discourse/app/components/post/menu/buttons/add-translation.gjs b/app/assets/javascripts/discourse/app/components/post/menu/buttons/add-translation.gjsindex a9c88d1a025..7cb8f62fa42 100644--- a/app/assets/javascripts/discourse/app/components/post/menu/buttons/add-translation.gjs+++ b/app/assets/javascripts/discourse/app/components/post/menu/buttons/add-translation.gjs@@ -4,8 +4,10 @@ import { array } from "@ember/helper";import { action } from "@ember/object";import { service } from "@ember/service";import DButton from "discourse/components/d-button";+import DEditorOriginalTranslationPreview from "discourse/components/d-editor-original-translation-preview";import DropdownMenu from "discourse/components/dropdown-menu";import PostTranslationsModal from "discourse/components/modal/post-translations";+import { ajax } from "discourse/lib/ajax";import Composer from "discourse/models/composer";import { i18n } from "discourse-i18n";import DMenu from "float-kit/components/d-menu";@@ -18,21 +20,6 @@ export default class PostMenuAddTranslationButton extends Component {@tracked showComposer = false;- get originalPostContent() {- const originalLocale =- this.args.post?.locale || this.siteSettings.default_locale;-- return `<div class='d-editor-translation-preview-wrapper'>- <span class='d-editor-translation-preview-wrapper__header'>- ${i18n("composer.translations.original_content")}- <span class='d-editor-translation-preview-wrapper__original-locale'>- ${originalLocale}- </span>- </span>- ${this.args.post.cooked}- </div>`;- }-get showTranslationButton() {return (this.currentUser &&@@ -66,11 +53,19 @@ export default class PostMenuAddTranslationButton extends Component {return;}+ const { raw } = await ajax(`/posts/${this.args.post.id}.json`);+await this.composer.open({action: Composer.ADD_TRANSLATION,draftKey: "translation",warningsDisabled: true,- hijackPreview: this.originalPostContent,+ hijackPreview: {+ component: DEditorOriginalTranslationPreview,+ model: {+ postLocale: this.args.post.locale,+ rawPost: raw,+ },+ },post: this.args.post,});}diff --git a/app/assets/javascripts/discourse/app/controllers/topic.js b/app/assets/javascripts/discourse/app/controllers/topic.jsindex cb0b700997f..bb8f30d59c4 100644--- a/app/assets/javascripts/discourse/app/controllers/topic.js+++ b/app/assets/javascripts/discourse/app/controllers/topic.js@@ -9,6 +9,7 @@ import { isEmpty, isPresent } from "@ember/utils";import { observes } from "@ember-decorators/object";import BufferedProxy from "ember-buffered-proxy/proxy";import { Promise } from "rsvp";+import DEditorOriginalTranslationPreview from "discourse/components/d-editor-original-translation-preview";import {CLOSE_INITIATED_BY_BUTTON,CLOSE_INITIATED_BY_ESC,@@ -877,6 +878,37 @@ export default class TopicController extends Controller {const topic = this.model;+ const editingLocalizedPost =+ post?.is_localized && this.siteSettings.content_localization_enabled;++ if (editingLocalizedPost) {+ if (!this.currentUser.can_localize_content) {+ return this._openComposerForEdit(topic, post);+ }++ return this.dialog.alert({+ message: i18n("post.localizations.edit_warning.message", {+ language: post.language,+ }),+ buttons: [+ {+ label: i18n("post.localizations.edit_warning.action_original"),+ class: "btn-primary",+ action: () => this._openComposerForEdit(topic, post),+ },+ {+ label: i18n("post.localizations.edit_warning.action_translation"),+ class: "btn-default",+ action: () => this._openComposerForEditTranslation(topic, post),+ },+ ],+ });+ }++ return this._openComposerForEdit(topic, post);+ }++ _openComposerForEdit(topic, post) {let editingSharedDraft = false;let draftsCategoryId = this.get("site.shared_drafts_category_id");if (draftsCategoryId && draftsCategoryId === topic.get("category.id")) {@@ -904,6 +936,24 @@ export default class TopicController extends Controller {return editingSamePost ? composer.unshrink() : composer.open(opts);}+ async _openComposerForEditTranslation(topic, post) {+ const { raw } = await ajax(`/posts/${post.id}.json`);++ const composerOpts = {+ action: Composer.ADD_TRANSLATION,+ draftKey: "translation",+ warningsDisabled: true,+ hijackPreview: {+ component: DEditorOriginalTranslationPreview,+ model: { postLocale: post.locale, rawPost: raw },+ },+ post,+ selectedTranslationLocale: this.currentUser?.effective_locale,+ };++ await this.composer.open(composerOpts);+ }+@actiontoggleBookmark(post) {if (!this.currentUser) {diff --git a/app/assets/javascripts/discourse/app/models/composer.js b/app/assets/javascripts/discourse/app/models/composer.jsindex 41f93efa768..9b3a3164f58 100644--- a/app/assets/javascripts/discourse/app/models/composer.js+++ b/app/assets/javascripts/discourse/app/models/composer.js@@ -209,7 +209,10 @@ export default class Composer extends RestModel {@tracked post;@tracked reply;@tracked whisper;- @tracked locale = this.post?.locale || this.siteSettings.default_locale;+ @tracked+ locale = this.siteSettings.content_localization_enabled+ ? this.post?.locale+ : null;unlistTopic = false;noBump = false;@@ -1182,7 +1185,9 @@ export default class Composer extends RestModel {typingTime: this.typingTime,composerTime: this.composerTime,metaData: this.metaData,- locale: this.locale,+ locale: this.siteSettings.content_localization_enabled+ ? this.locale+ : null,});this.serialize(_create_serializer, createdPost);diff --git a/app/assets/javascripts/discourse/app/services/composer.js b/app/assets/javascripts/discourse/app/services/composer.jsindex fd197e7797a..23fb4a9795c 100644--- a/app/assets/javascripts/discourse/app/services/composer.js+++ b/app/assets/javascripts/discourse/app/services/composer.js@@ -1337,7 +1337,7 @@ export default class ComposerService extends Service {@param {String} [opts.draftSequence]@param {Boolean} [opts.skipJumpOnSave] Option to skip navigating to the post when saved in this composer session@param {Boolean} [opts.skipFormTemplate] Option to skip the form template even if configured for the category- @param {String} [opts.hijackPreview] Option to hijack the preview with custom content+ @param {String} [opts.hijackPreview] Option to hijack the preview with a custom component, you must pass { component: CustomPreviewComponent, model: { ... } }@param {String} [opts.selectedTranslationLocale] The locale to use for the translation**/async open(opts = {}) {@@ -1460,8 +1460,7 @@ export default class ComposerService extends Service {action: CREATE_TOPIC,draftKey: this.topicDraftKey,draftSequence: 0,- locale:- this.currentUser.effective_locale || this.siteSettings.default_locale,+ locale: null,});}diff --git a/app/assets/stylesheets/common/d-editor.scss b/app/assets/stylesheets/common/d-editor.scssindex 34f3edec62d..59f7ebf0f86 100644--- a/app/assets/stylesheets/common/d-editor.scss+++ b/app/assets/stylesheets/common/d-editor.scss@@ -427,7 +427,7 @@}}- .d-editor-preview .d-editor-translation-preview-wrapper {+ .d-editor-translation-preview-wrapper {--translation-preview-padding: 1rem;box-sizing: border-box;border: var(--d-input-border);diff --git a/config/locales/client.en.yml b/config/locales/client.en.ymlindex 1ccfffd527e..4b7ce20c1c7 100644--- a/config/locales/client.en.yml+++ b/config/locales/client.en.yml@@ -4026,6 +4026,10 @@ en:success: "Translations have been updated successfully"post_language_selector:none: "None"+ edit_warning:+ message: "This is a translated post.<br><br> Do you want to edit the original post in %{language} or edit this specific translation?"+ action_original: "Edit original post"+ action_translation: "Edit this translation"errors:create: "Sorry, there was an error creating your post. Please try again."diff --git a/spec/system/content_localization_spec.rb b/spec/system/content_localization_spec.rbindex 644e17625d4..949769c6175 100644--- a/spec/system/content_localization_spec.rb+++ b/spec/system/content_localization_spec.rb@@ -34,6 +34,8 @@ describe "Content Localization" dolet(:post_1_obj) { PageObjects::Components::Post.new(1) }let(:post_4_obj) { PageObjects::Components::Post.new(4) }let(:post_history_modal) { PageObjects::Modals::PostHistory.new }+ let(:edit_localized_post_dialog) { PageObjects::Components::Dialog.new }+ let(:fast_editor) { PageObjects::Components::FastEditor.new }before doFabricate(:topic_localization, topic:, locale: "ja", fancy_title: "孫子兵法からの人生戦略")@@ -114,9 +116,33 @@ describe "Content Localization" dotopic_page.visit_topic(topic)topic_page.expand_post_actions(post_3)topic_page.click_post_action_button(post_3, :edit)+ expect(edit_localized_post_dialog).to be_open+ edit_localized_post_dialog.click_yesexpect(composer).to have_content(post_3.raw)end+ it "allows editing translated content when post is localized" do+ sign_in(admin)++ topic_page.visit_topic(topic)+ topic_page.expand_post_actions(post_3)+ topic_page.click_post_action_button(post_3, :edit)+ expect(edit_localized_post_dialog).to be_open+ edit_localized_post_dialog.click_no+ expect(page).to have_css(".action-title", text: I18n.t("js.composer.translations.title"))+ end++ it "does not open the fast editor for localized posts" do+ sign_in(admin)++ topic_page.visit_topic(topic)+ select_text_range("#{topic_page.post_by_number_selector(post_3.post_number)} .cooked", 0, 5)+ expect(topic_page.fast_edit_button).to be_visible+ topic_page.click_fast_edit_button+ expect(page).to have_no_css("#fast-edit-input")+ expect(edit_localized_post_dialog).to be_open+ end+context "for post edit histories" dobefore doSiteSetting.editing_grace_period = 0diff --git a/spec/system/post_translation_spec.rb b/spec/system/post_translation_spec.rbindex 69725b86b67..58aed5e5fde 100644--- a/spec/system/post_translation_spec.rb+++ b/spec/system/post_translation_spec.rb@@ -179,32 +179,10 @@ describe "Post translations", type: :system doendend- context "when the user's default locale is different from the site default" do- before do- SiteSetting.allow_user_locale = true- admin.update!(locale: "fr")- end-- it "should show the user's locale as the default in the post language switcher" do- visit("/latest")- page.find("#create-topic").click- expect(- page.has_css?("#{POST_LANGUAGE_SWITCHER_SELECTOR} .d-button-label", text: "FR"),- ).to be true- end- end-- context "when the user's default locale is different from the site default but not an available language" do- before do- SiteSetting.allow_user_locale = true- admin.update!(locale: "de")- end-- it "should make the selected language blank" do- visit("/latest")- page.find("#create-topic").click- expect(page.has_no_css?("#{POST_LANGUAGE_SWITCHER_SELECTOR} .d-button-label")).to be true- end+ it "should not have a locale set by default" do+ visit("/latest")+ page.find("#create-topic").click+ expect(page.has_no_css?("#{POST_LANGUAGE_SWITCHER_SELECTOR} .d-button-label")).to be trueendendenddiff --git a/themes/horizon/scss/composer.scss b/themes/horizon/scss/composer.scssindex fece52f2748..7fb78ec2ee6 100644--- a/themes/horizon/scss/composer.scss+++ b/themes/horizon/scss/composer.scss@@ -103,11 +103,11 @@}#reply-control.composer-action-add_translation {- .d-editor-preview .d-editor-translation-preview-wrapper {+ .d-editor-translation-preview-wrapper {border-color: var(--d-sidebar-border-color);}- .d-editor-preview .d-editor-translation-preview-wrapper__header {+ .d-editor-translation-preview-wrapper__header {top: 6.5rem;padding: 0.25rem 0.75rem;background: var(--background-color);