|
1 | | -import { ajax } from "discourse/lib/ajax"; |
2 | | -import { popupAjaxError } from "discourse/lib/ajax-error"; |
3 | | -import { withSilencedDeprecations } from "discourse/lib/deprecated"; |
4 | 1 | import { withPluginApi } from "discourse/lib/plugin-api"; |
5 | | -import { i18n } from "discourse-i18n"; |
6 | 2 | import LanguageSwitcher from "../components/language-switcher"; |
7 | 3 | import ToggleTranslationButton from "../components/post-menu/toggle-translation-button"; |
8 | 4 | import ShowOriginalContent from "../components/show-original-content"; |
@@ -38,130 +34,25 @@ function initializeTranslation(api) { |
38 | 34 | customizePostMenu(api); |
39 | 35 | } |
40 | 36 |
|
41 | | -function customizePostMenu(api, container) { |
42 | | - const transformerRegistered = api.registerValueTransformer( |
| 37 | +function customizePostMenu(api) { |
| 38 | + api.registerValueTransformer( |
43 | 39 | "post-menu-buttons", |
44 | 40 | ({ value: dag, context: { firstButtonKey } }) => { |
45 | 41 | dag.add("translate", ToggleTranslationButton, { before: firstButtonKey }); |
46 | 42 | } |
47 | 43 | ); |
48 | 44 |
|
49 | | - if (transformerRegistered) { |
50 | | - // the plugin outlet is not updated when the post instance is modified unless we register the new properties as |
51 | | - // tracked |
52 | | - api.addTrackedPostProperties( |
53 | | - "detectedLang", |
54 | | - "isTranslating", |
55 | | - "isTranslated", |
56 | | - "translatedText", |
57 | | - "translatedTitle" |
58 | | - ); |
59 | | - |
60 | | - api.renderBeforeWrapperOutlet("post-menu", TranslatedPost); |
61 | | - } |
62 | | - |
63 | | - const silencedKey = |
64 | | - transformerRegistered && "discourse.post-menu-widget-overrides"; |
65 | | - |
66 | | - withSilencedDeprecations(silencedKey, () => |
67 | | - customizeWidgetPostMenu(api, container) |
68 | | - ); |
69 | | -} |
70 | | - |
71 | | -function customizeWidgetPostMenu(api) { |
72 | | - api.includePostAttributes( |
73 | | - "can_translate", |
74 | | - "translated_text", |
75 | | - "detected_lang", |
76 | | - "translated_title" |
| 45 | + // the plugin outlet is not updated when the post instance is modified unless we register the new properties as |
| 46 | + // tracked |
| 47 | + api.addTrackedPostProperties( |
| 48 | + "detectedLang", |
| 49 | + "isTranslating", |
| 50 | + "isTranslated", |
| 51 | + "translatedText", |
| 52 | + "translatedTitle" |
77 | 53 | ); |
78 | 54 |
|
79 | | - const siteSettings = api.container.lookup("service:site-settings"); |
80 | | - api.decorateWidget("post-menu:before", (dec) => { |
81 | | - if (!dec.state.isTranslated) { |
82 | | - return; |
83 | | - } |
84 | | - |
85 | | - if (dec.state.isTranslating) { |
86 | | - return dec.h("div.spinner.small"); |
87 | | - } |
88 | | - |
89 | | - const language = dec.attrs.detected_lang; |
90 | | - const translator = siteSettings.translator_provider; |
91 | | - |
92 | | - let titleElements = []; |
93 | | - |
94 | | - if (dec.attrs.translated_title) { |
95 | | - titleElements = [ |
96 | | - dec.h("div.topic-attribution", dec.attrs.translated_title), |
97 | | - ]; |
98 | | - } |
99 | | - |
100 | | - return dec.h("div.post-translation", [ |
101 | | - dec.h("hr"), |
102 | | - ...titleElements, |
103 | | - dec.h( |
104 | | - "div.post-attribution", |
105 | | - i18n("translator.translated_from", { language, translator }) |
106 | | - ), |
107 | | - dec.cooked(dec.attrs.translated_text), |
108 | | - ]); |
109 | | - }); |
110 | | - |
111 | | - api.attachWidgetAction("post-menu", "translate", function () { |
112 | | - const state = this.state; |
113 | | - state.isTranslated = true; |
114 | | - state.isTranslating = true; |
115 | | - this.scheduleRerender(); |
116 | | - |
117 | | - const post = this.findAncestorModel(); |
118 | | - |
119 | | - if (post) { |
120 | | - return ajax("/translator/translate", { |
121 | | - type: "POST", |
122 | | - data: { post_id: post.get("id") }, |
123 | | - }) |
124 | | - .then(function (res) { |
125 | | - post.setProperties({ |
126 | | - translated_text: res.translation, |
127 | | - detected_lang: res.detected_lang, |
128 | | - translated_title: res.title_translation, |
129 | | - }); |
130 | | - }) |
131 | | - .catch((error) => { |
132 | | - popupAjaxError(error); |
133 | | - state.isTranslating = false; |
134 | | - state.isTranslated = false; |
135 | | - }) |
136 | | - .finally(() => (state.isTranslating = false)); |
137 | | - } |
138 | | - }); |
139 | | - |
140 | | - api.attachWidgetAction("post-menu", "hideTranslation", function () { |
141 | | - this.state.isTranslated = false; |
142 | | - const post = this.findAncestorModel(); |
143 | | - if (post) { |
144 | | - post.set("translated_text", ""); |
145 | | - } |
146 | | - }); |
147 | | - |
148 | | - api.addPostMenuButton("translate", (attrs, state) => { |
149 | | - if (!attrs.can_translate) { |
150 | | - return; |
151 | | - } |
152 | | - |
153 | | - const [action, title] = !state.isTranslated |
154 | | - ? ["translate", "translator.view_translation"] |
155 | | - : ["hideTranslation", "translator.hide_translation"]; |
156 | | - |
157 | | - return { |
158 | | - action, |
159 | | - title, |
160 | | - icon: "globe", |
161 | | - position: "first", |
162 | | - className: state.isTranslated ? "translated" : null, |
163 | | - }; |
164 | | - }); |
| 55 | + api.renderBeforeWrapperOutlet("post-menu", TranslatedPost); |
165 | 56 | } |
166 | 57 |
|
167 | 58 | export default { |
|
0 commit comments