Skip to content

Commit 9abad5f

Browse files
authored
DEV: Remove the legacy widget post menu code (#261)
1 parent 892cdb6 commit 9abad5f

File tree

1 file changed

+11
-120
lines changed

1 file changed

+11
-120
lines changed

assets/javascripts/discourse/initializers/extend-for-translate-button.js

Lines changed: 11 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
import { ajax } from "discourse/lib/ajax";
2-
import { popupAjaxError } from "discourse/lib/ajax-error";
3-
import { withSilencedDeprecations } from "discourse/lib/deprecated";
41
import { withPluginApi } from "discourse/lib/plugin-api";
5-
import { i18n } from "discourse-i18n";
62
import LanguageSwitcher from "../components/language-switcher";
73
import ToggleTranslationButton from "../components/post-menu/toggle-translation-button";
84
import ShowOriginalContent from "../components/show-original-content";
@@ -38,130 +34,25 @@ function initializeTranslation(api) {
3834
customizePostMenu(api);
3935
}
4036

41-
function customizePostMenu(api, container) {
42-
const transformerRegistered = api.registerValueTransformer(
37+
function customizePostMenu(api) {
38+
api.registerValueTransformer(
4339
"post-menu-buttons",
4440
({ value: dag, context: { firstButtonKey } }) => {
4541
dag.add("translate", ToggleTranslationButton, { before: firstButtonKey });
4642
}
4743
);
4844

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"
7753
);
7854

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);
16556
}
16657

16758
export default {

0 commit comments

Comments
 (0)