Skip to content

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

DEV: Deprecate the widgets rendering system (#33684)

Sérgio Saquetim5c0dbd2bacc3
diff --git a/app/assets/javascripts/discourse/app/components/decorated-html.gjs b/app/assets/javascripts/discourse/app/components/decorated-html.gjs
index 973a62c9db7..3a094c34b5d 100644
--- a/app/assets/javascripts/discourse/app/components/decorated-html.gjs
+++ b/app/assets/javascripts/discourse/app/components/decorated-html.gjs
@@ -4,7 +4,7 @@ import { htmlSafe, isHTMLSafe } from "@ember/template";
import { TrackedArray } from "@ember-compat/tracked-built-ins";
import helperFn from "discourse/helpers/helper-fn";
import deprecated from "discourse/lib/deprecated";
-import { POST_STREAM_DEPRECATION_OPTIONS } from "discourse/widgets/post-stream";
+import { POST_STREAM_DEPRECATION_OPTIONS } from "discourse/widgets/widget";
const detachedDocument = document.implementation.createHTMLDocument("detached");
diff --git a/app/assets/javascripts/discourse/app/components/mount-widget.gjs b/app/assets/javascripts/discourse/app/components/mount-widget.gjs
index 28063a124ef..2a6ac53e37b 100644
--- a/app/assets/javascripts/discourse/app/components/mount-widget.gjs
+++ b/app/assets/javascripts/discourse/app/components/mount-widget.gjs
@@ -1,12 +1,17 @@
import ArrayProxy from "@ember/array/proxy";
import Component from "@ember/component";
import { cancel, scheduleOnce } from "@ember/runloop";
+import { service } from "@ember/service";
import { camelize } from "@ember/string";
import { diff, patch } from "virtual-dom";
import DirtyKeys from "discourse/lib/dirty-keys";
import { getRegister } from "discourse/lib/get-owner";
import { WidgetClickHook } from "discourse/widgets/hooks";
-import { queryRegistry, traverseCustomWidgets } from "discourse/widgets/widget";
+import {
+ queryRegistry,
+ traverseCustomWidgets,
+ warnWidgetsDeprecation,
+} from "discourse/widgets/widget";
let _cleanCallbacks = {};
@@ -34,6 +39,8 @@ export function resetWidgetCleanCallbacks() {
}
export default class MountWidget extends Component {
+ @service siteSettings;
+
dirtyKeys = null;
_tree = null;
_rootNode = null;
@@ -47,6 +54,18 @@ export default class MountWidget extends Component {
super.init(...arguments);
const name = this.widget;
+ if (this.isDeactivated) {
+ warnWidgetsDeprecation(
+ `Widgets are deactivated and won't be rendered. Your site may not work properly. Affected widget: ${name}.`,
+ true
+ );
+ return;
+ } else {
+ warnWidgetsDeprecation(
+ `The \`MountWidget\` component is deprecated and will soon stop working. Use Glimmer components instead. Affected widget: ${name}.`
+ );
+ }
+
if (name === "post-cooked") {
throw [
"Cannot use <MountWidget /> with `post-cooked`.",
@@ -83,8 +102,16 @@ export default class MountWidget extends Component {
this.dirtyKeys = new DirtyKeys(name);
}
+ get isDeactivated() {
+ return this.siteSettings.deactivate_widgets_rendering;
+ }
+
didInsertElement() {
super.didInsertElement(...arguments);
+ if (this.isDeactivated) {
+ return;
+ }
+
WidgetClickHook.setupDocumentCallback();
this._rootNode = document.createElement("div");
@@ -93,6 +120,10 @@ export default class MountWidget extends Component {
}
willClearRender() {
+ if (this.isDeactivated) {
+ return;
+ }
+
super.willClearRender(...arguments);
const callbacks = _cleanCallbacks[this.widget];
if (callbacks) {
@@ -109,6 +140,10 @@ export default class MountWidget extends Component {
willDestroyElement() {
super.willDestroyElement(...arguments);
+ if (this.isDeactivated) {
+ return;
+ }
+
this._dispatched.forEach((evt) => {
const [eventName, caller] = evt;
this.appEvents.off(eventName, this, caller);
@@ -121,6 +156,10 @@ export default class MountWidget extends Component {
afterPatch() {}
eventDispatched(eventName, key, refreshArg) {
+ if (this.isDeactivated) {
+ return;
+ }
+
key = typeof key === "function" ? key(refreshArg) : key;
const onRefresh = camelize(eventName.replace(/:/, "-"));
this.dirtyKeys.keyDirty(key, { onRefresh, refreshArg });
@@ -128,6 +167,10 @@ export default class MountWidget extends Component {
}
dispatch(eventName, key) {
+ if (this.isDeactivated) {
+ return;
+ }
+
this._childEvents.push(eventName);
const caller = (refreshArg) =>
@@ -137,6 +180,10 @@ export default class MountWidget extends Component {
}
queueRerender(callback) {
+ if (this.isDeactivated) {
+ return;
+ }
+
if (callback && !this._renderCallback) {
this._renderCallback = callback;
}
@@ -147,6 +194,10 @@ export default class MountWidget extends Component {
buildArgs() {}
rerenderWidget() {
+ if (this.isDeactivated) {
+ return;
+ }
+
cancel(this._timeout);
if (this._rootNode) {
@@ -191,26 +242,40 @@ export default class MountWidget extends Component {
}
mountChildComponent(info) {
+ if (this.isDeactivated) {
+ return;
+ }
+
this._childComponents.pushObject(info);
}
unmountChildComponent(info) {
+ if (this.isDeactivated) {
+ return;
+ }
+
this._childComponents.removeObject(info);
}
didUpdateAttrs() {
super.didUpdateAttrs(...arguments);
+ if (this.isDeactivated) {
+ return;
+ }
+
this.queueRerender();
}
<template>
- {{#each this._childComponents as |info|}}
- {{#in-element info.element insertBefore=null}}
- <info.component
- @data={{info.data}}
- @setWrapperElementAttrs={{info.setWrapperElementAttrs}}
- />
- {{/in-element}}
- {{/each}}
+ {{#unless this.isDeactivated}}
+ {{#each this._childComponents as |info|}}
+ {{#in-element info.element insertBefore=null}}
+ <info.component
+ @data={{info.data}}
+ @setWrapperElementAttrs={{info.setWrapperElementAttrs}}
+ />
+ {{/in-element}}
+ {{/each}}
+ {{/unless}}
</template>
}
diff --git a/app/assets/javascripts/discourse/app/lib/deprecated.js b/app/assets/javascripts/discourse/app/lib/deprecated.js
index b2513c35ba6..f17094d57a7 100644
--- a/app/assets/javascripts/discourse/app/lib/deprecated.js
+++ b/app/assets/javascripts/discourse/app/lib/deprecated.js
@@ -110,6 +110,15 @@ export async function withSilencedDeprecationsAsync(deprecationIds, callback) {
}
}
+/**
+ * Checks if a given deprecation ID is currently silenced
+ * @param {String} id The deprecation id to check
+ * @returns {boolean} True if the deprecation is silenced, false otherwise
+ */
+export function isDeprecationSilenced(id) {
+ return id && disabledDeprecations.has(id);
+}
+
function ensureEmberDeprecationSilencer() {
if (emberDeprecationSilencer) {
return;
diff --git a/app/assets/javascripts/discourse/app/lib/plugin-api.gjs b/app/assets/javascripts/discourse/app/lib/plugin-api.gjs
index 3eeadba1354..b59c88184df 100644
--- a/app/assets/javascripts/discourse/app/lib/plugin-api.gjs
+++ b/app/assets/javascripts/discourse/app/lib/plugin-api.gjs
@@ -146,7 +146,6 @@ import {
} from "discourse/widgets/post-small-action";
import {
addPostTransformCallback,
- POST_STREAM_DEPRECATION_OPTIONS,
preventCloak,
} from "discourse/widgets/post-stream";
import { disableNameSuppression } from "discourse/widgets/poster-name";
@@ -154,8 +153,10 @@ import {
changeSetting,
createWidget,
decorateWidget,
+ POST_STREAM_DEPRECATION_OPTIONS,
queryRegistry,
reopenWidget,
+ warnWidgetsDeprecation,
} from "discourse/widgets/widget";
import { addImageWrapperButton } from "discourse-markdown-it/features/image-controls";
import { CUSTOM_USER_SEARCH_OPTIONS } from "select-kit/components/user-chooser";
@@ -750,7 +751,7 @@ class PluginApi {
}
// TODO (glimmer-post-stream): remove the fallback when removing the legacy post stream code
- withSilencedDeprecations("discourse.post-stream-widget-overrides", () => {
+ withSilencedDeprecations(POST_STREAM_DEPRECATION_OPTIONS.id, () => {
decorateWidget(`poster-name:${loc}`, (dec) => {
const attrs = dec.attrs;
let results = cb(attrs.userCustomFields || {}, attrs);
@@ -3452,6 +3453,10 @@ class PluginApi {
`The \`${widgetName}\` widget has been deprecated and \`api.${override}\` is no longer a supported override.`,
POST_STREAM_DEPRECATION_OPTIONS
);
+ } else {
+ warnWidgetsDeprecation(
+ `Using \`api.${override}\` is deprecated and will soon stop working. Affected widget: ${widgetName}.`
+ );
}
}
}
diff --git a/app/assets/javascripts/discourse/app/models/site.js b/app/assets/javascripts/discourse/app/models/site.js
index b3565cb53d5..d41f50482a7 100644
--- a/app/assets/javascripts/discourse/app/models/site.js
+++ b/app/assets/javascripts/discourse/app/models/site.js
@@ -128,7 +128,9 @@ export default class Site extends RestModel {
let enabled;
/* eslint-disable no-console */
- let settingValue = this.siteSettings.glimmer_post_stream_mode;
+ let settingValue = this.siteSettings.deactivate_widgets_rendering
+ ? "enabled" // if widgets rendering is deactivated, we always use the glimmer post stream
+ : this.siteSettings.glimmer_post_stream_mode;
if (
settingValue === "disabled" &&
this.currentUser?.use_glimmer_post_stream_mode_auto_mode
diff --git a/app/assets/javascripts/discourse/app/widgets/decorator-helper.js b/app/assets/javascripts/discourse/app/widgets/decorator-helper.js
index f9a06e07142..f896243666e 100644
--- a/app/assets/javascripts/discourse/app/widgets/decorator-helper.js
+++ b/app/assets/javascripts/discourse/app/widgets/decorator-helper.js
@@ -3,9 +3,9 @@ import { h } from "virtual-dom";
import deprecated from "discourse/lib/deprecated";
import Connector from "discourse/widgets/connector";
import PostCooked from "discourse/widgets/post-cooked";
-import { POST_STREAM_DEPRECATION_OPTIONS } from "discourse/widgets/post-stream";
import RawHtml from "discourse/widgets/raw-html";
import RenderGlimmer from "discourse/widgets/render-glimmer";
+import { POST_STREAM_DEPRECATION_OPTIONS } from "discourse/widgets/widget";
class DecoratorHelper {
constructor(widget, attrs, state) {
@@ -173,6 +173,7 @@ class DecoratorHelper {
return renderGlimmer.element;
}
}
+
DecoratorHelper.prototype.h = h;
export default DecoratorHelper;
diff --git a/app/assets/javascripts/discourse/app/widgets/post-stream.js b/app/assets/javascripts/discourse/app/widgets/post-stream.js
index e3fff3899a5..c238a1dd0ab 100644
--- a/app/assets/javascripts/discourse/app/widgets/post-stream.js
+++ b/app/assets/javascripts/discourse/app/widgets/post-stream.js
@@ -11,19 +11,16 @@ import transformPost from "discourse/lib/transform-post";
import DiscourseURL from "discourse/lib/url";
import { avatarFor } from "discourse/widgets/post";
import RenderGlimmer from "discourse/widgets/render-glimmer";
-import { createWidget } from "discourse/widgets/widget";
+import {
+ createWidget,
+ POST_STREAM_DEPRECATION_OPTIONS,
+} from "discourse/widgets/widget";
import { i18n } from "discourse-i18n";
-export const POST_STREAM_DEPRECATION_OPTIONS = {
- since: "v3.5.0.beta1-dev",
- id: "discourse.post-stream-widget-overrides",
- url: "https://meta.discourse.org/t/372063/1",
-};
-
export let havePostStreamWidgetExtensions = null;
registerDeprecationHandler((_, opts) => {
- if (opts?.id === "discourse.post-stream-widget-overrides") {
+ if (opts?.id === POST_STREAM_DEPRECATION_OPTIONS.id) {
if (!havePostStreamWidgetExtensions) {
havePostStreamWidgetExtensions = new Set();
}
diff --git a/app/assets/javascripts/discourse/app/widgets/render-glimmer.js b/app/assets/javascripts/discourse/app/widgets/render-glimmer.js
index c9b57d62de4..0dae2e79eb8 100644
--- a/app/assets/javascripts/discourse/app/widgets/render-glimmer.js
+++ b/app/assets/javascripts/discourse/app/widgets/render-glimmer.js
@@ -3,7 +3,11 @@ import { tracked } from "@glimmer/tracking";
import { setComponentTemplate } from "@ember/component";
import templateOnly from "@ember/component/template-only";
import { assert } from "@ember/debug";
-import { createWidgetFrom } from "discourse/widgets/widget";
+import { getOwnerWithFallback } from "discourse/lib/get-owner";
+import {
+ createWidgetFrom,
+ warnWidgetsDeprecation,
+} from "discourse/widgets/widget";
const INITIAL_CLASSES = Symbol("RENDER_GLIMMER_INITIAL_CLASSES");
@@ -213,6 +217,20 @@ RenderGlimmer.prototype.type = "Widget";
* @param template - a glimmer template compiled via ember-cli-htmlbars
*/
export function registerWidgetShim(name, tagName, template) {
+ if (
+ getOwnerWithFallback(this)?.lookup(`service:site-settings`)
+ ?.deactivate_widgets_rendering
+ ) {
+ warnWidgetsDeprecation(
+ `Widgets are deactivated and shims can't be registered. Your site may not work properly. Affected widget: ${name}.`
+ );
+ return;
+ } else {
+ warnWidgetsDeprecation(
+ `Using \`registerWidgetShim\` is deprecated and will soon stop working. Affected widgetShim: ${name}.`
+ );
+ }
+
const RenderGlimmerShim = class MyClass extends RenderGlimmer {
constructor(attrs) {
super(null, tagName, template, attrs);
@@ -224,6 +242,7 @@ export function registerWidgetShim(name, tagName, template) {
}
didRenderWidget() {}
+
willRerenderWidget() {}
};
diff --git a/app/assets/javascripts/discourse/app/widgets/widget.js b/app/assets/javascripts/discourse/app/widgets/widget.js
index 0860f11451b..532d2f660d7 100644
--- a/app/assets/javascripts/discourse/app/widgets/widget.js
+++ b/app/assets/javascripts/discourse/app/widgets/widget.js
@@ -3,7 +3,9 @@ import { getOwner, setOwner } from "@ember/owner";
import { camelize } from "@ember/string";
import { Promise } from "rsvp";
import { h } from "virtual-dom";
+import deprecated, { isDeprecationSilenced } from "discourse/lib/deprecated";
import { isProduction } from "discourse/lib/environment";
+import { getOwnerWithFallback } from "discourse/lib/get-owner";
import { deepMerge } from "discourse/lib/object";
import { consolePrefix } from "discourse/lib/source-identifier";
import DecoratorHelper from "discourse/widgets/decorator-helper";
@@ -30,6 +32,27 @@ import {
} from "discourse/widgets/hooks";
import { i18n } from "discourse-i18n";
+export const WIDGET_DEPRECATION_OPTIONS = {
+ since: "v3.5.0.beta8-dev",
+ id: "discourse.widgets-end-of-life",
+ url: "https://meta.discourse.org/t/375332/1",
+};
+
+export const POST_STREAM_DEPRECATION_OPTIONS = {
+ since: "v3.5.0.beta1-dev",
+ id: "discourse.post-stream-widget-overrides",
+ url: "https://meta.discourse.org/t/372063/1",
+};
+
+export function warnWidgetsDeprecation(message, dontSkipCore = false) {
+ if (
+ (dontSkipCore || consolePrefix()) &&
+ !isDeprecationSilenced(POST_STREAM_DEPRECATION_OPTIONS.id)
+ ) {
+ deprecated(message, WIDGET_DEPRECATION_OPTIONS);
+ }
+}
+
const _registry = {};
export function queryRegistry(name) {
@@ -114,6 +137,20 @@ export function createWidgetFrom(base, name, opts) {
}
export function createWidget(name, opts) {
+ if (
+ getOwnerWithFallback(this)?.lookup(`service:site-settings`)
+ ?.deactivate_widgets_rendering
+ ) {
+ warnWidgetsDeprecation(
+ `Widgets are deactivated and can't be created. Your site may not work properly. Affected widget: ${name}.`
+ );
+ return;
+ } else {
+ warnWidgetsDeprecation(
+ `Using \`api.createWidget\` is deprecated and will soon stop working. Use Glimmer components instead. Affected widget: ${name}.`
+ );
+ }
+
return createWidgetFrom(Widget, name, opts);
}
diff --git a/app/assets/javascripts/discourse/tests/integration/components/decorated-html-test.gjs b/app/assets/javascripts/discourse/tests/integration/components/decorated-html-test.gjs
index 8af071e111e..25046ec675c 100644
--- a/app/assets/javascripts/discourse/tests/integration/components/decorated-html-test.gjs
+++ b/app/assets/javascripts/discourse/tests/integration/components/decorated-html-test.gjs
@@ -8,6 +8,7 @@ import DecoratedHtml from "discourse/components/decorated-html";
import { withSilencedDeprecations } from "discourse/lib/deprecated";
import { getOwnerWithFallback } from "discourse/lib/get-owner";
import { setupRenderingTest } from "discourse/tests/helpers/component-test";
+import { POST_STREAM_DEPRECATION_OPTIONS } from "discourse/widgets/widget";
module("Integration | Component | <DecoratedHtml />", function (hooks) {
setupRenderingTest(hooks);
@@ -91,7 +92,7 @@ module("Integration | Component | <DecoratedHtml />", function (hooks) {
const decorateWithStringTarget = (element, helper) => {
element.innerHTML += "<div id='appended'>Appended</div>";
- withSilencedDeprecations("discourse.post-stream-widget-overrides", () => {
+ withSilencedDeprecations(POST_STREAM_DEPRECATION_OPTIONS.id, () => {
helper.renderGlimmer(
"div",
<template>
@@ -131,7 +132,7 @@ module("Integration | Component | <DecoratedHtml />", function (hooks) {
const decorateWithHbsTemplate = (element, helper) => {
element.innerHTML += "<div id='appended'>Appended</div>";
- withSilencedDeprecations("discourse.post-stream-widget-overrides", () => {
+ withSilencedDeprecations(POST_STREAM_DEPRECATION_OPTIONS.id, () => {
helper.renderGlimmer(
element,
hbs("<div id='render-glimmer'>Hello from Glimmer Component</div>")
diff --git a/app/assets/javascripts/discourse/tests/integration/components/widgets/post-small-action-test.gjs b/app/assets/javascripts/discourse/tests/integration/components/widgets/post-small-action-test.gjs
index a561bb89674..67c99469390 100644
--- a/app/assets/javascripts/discourse/tests/integration/components/widgets/post-small-action-test.gjs
+++ b/app/assets/javascripts/discourse/tests/integration/components/widgets/post-small-action-test.gjs
@@ -4,6 +4,7 @@ import MountWidget from "discourse/components/mount-widget";
import { withSilencedDeprecations } from "discourse/lib/deprecated";
import { withPluginApi } from "discourse/lib/plugin-api";
import { setupRenderingTest } from "discourse/tests/helpers/component-test";
+import { POST_STREAM_DEPRECATION_OPTIONS } from "discourse/widgets/widget";
import I18n from "discourse-i18n";
// TODO (glimmer-post-stream) remove this test when removing the widget post stream code
@@ -154,7 +155,7 @@ module(
test("`addPostSmallActionClassesCallback` plugin api", async function (assert) {
const self = this;
- withSilencedDeprecations("discourse.post-stream-widget-overrides", () => {
+ withSilencedDeprecations(POST_STREAM_DEPRECATION_OPTIONS.id, () => {
withPluginApi("1.6.0", (api) => {
api.addPostSmallActionClassesCallback((postAttrs) => {
if (postAttrs.canRecover) {
diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml
index 396d5fafb59..5d96ed7ea18 100644
--- a/config/locales/server.en.yml
+++ b/config/locales/server.en.yml
@@ -2724,6 +2724,7 @@ en:
experimental_new_new_view_groups: 'Enable a new topics list that combines unread and new topics and make the "Everything" link in the sidebar link to it.'
glimmer_post_stream_mode: "Control whether the new 'glimmer' post stream implementation is used. 'auto' will enable automatically once all your themes and plugins are ready. This implementation is under active development, and is not intended for production use. Do not develop themes/plugins against it until the implementation is finalized and announced."
glimmer_post_stream_mode_auto_groups: "Enable the new 'glimmer' post stream implementation in 'auto' mode for the specified user groups. This implementation is under active development, and is not intended for production use. Do not develop themes/plugins against it until the implementation is finalized and announced."
+ deactivate_widgets_rendering: "Disable the legacy widgets rendering engine. This will disable all widgets that have not been updated to Glimmer components, it will also force the Glimmer Post Stream to be enabled unconditionally regardless of the value of the `glimmer_post_stream_mode` setting. This setting is not recommended for production sites, as it may break existing themes and plugins."
experimental_form_templates: "Enable the form templates feature. Manage the templates at <a href='%{base_path}/admin/customize/form-templates'>Customize / Templates</a>."
show_preview_for_form_templates: "Enable the preview for form templates feature"
lazy_load_categories_groups: "Lazy load category information only for users of these groups. This improves performance on sites with many categories."
diff --git a/config/site_settings.yml b/config/site_settings.yml
index f903a2b36ea..f51511906fd 100644
--- a/config/site_settings.yml
+++ b/config/site_settings.yml
@@ -4083,6 +4083,9 @@ experimental:
- auto
- enabled
default: "auto"
+ deactivate_widgets_rendering:
+ client: true
+ default: false
enable_rich_text_paste:
client: true
default: true
diff --git a/plugins/discourse-gamification/test/javascripts/acceptance/gamification-score-test.js b/plugins/discourse-gamification/test/javascripts/acceptance/gamification-score-test.js
index 6f38f249c77..1eb8ae849fd 100644
--- a/plugins/discourse-gamification/test/javascripts/acceptance/gamification-score-test.js
+++ b/plugins/discourse-gamification/test/javascripts/acceptance/gamification-score-test.js
@@ -18,7 +18,7 @@ acceptance(
test("user card gamification score - score is present", async function (assert) {
await visit("/t/internationalization-localization/280");
await click(".topic-map__users-trigger");
- await click('a[data-user-card="charlie"]');
+ await click('.topic-map__users-list a[data-user-card="charlie"]');
assert
.dom(".user-card .gamification-score")