Skip to content

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

DEV: Drive the developer tools toolbar from a tool table (#41891)

Sérgio Saquetim9c8155fdf5e5
diff --git a/frontend/discourse/app/initializers/dev-tools.js b/frontend/discourse/app/initializers/dev-tools.ts
similarity index 84%
rename from frontend/discourse/app/initializers/dev-tools.js
rename to frontend/discourse/app/initializers/dev-tools.ts
index 5c8b39434d7..c1a09576939 100644
--- a/frontend/discourse/app/initializers/dev-tools.js
+++ b/frontend/discourse/app/initializers/dev-tools.ts
@@ -1,6 +1,14 @@
import { DEBUG } from "@glimmer/env";
+import type Application from "@ember/application";
import { isDevelopment } from "discourse/lib/environment";
+declare global {
+ interface Window {
+ enableDevTools(): void;
+ disableDevTools(): void;
+ }
+}
+
const KEY = "discourse__dev_tools";
function parseStoredValue() {
@@ -17,18 +25,18 @@ function parseStoredValue() {
export default {
after: ["discourse-bootstrap"],
- initialize(app) {
+ initialize(app: Application) {
let defaultEnabled = false;
if (DEBUG && isDevelopment()) {
defaultEnabled = true;
}
- function storeValue(value) {
+ function storeValue(value: boolean) {
if (value === defaultEnabled) {
window.localStorage?.removeItem(KEY);
} else {
- window.localStorage?.setItem(KEY, value);
+ window.localStorage?.setItem(KEY, String(value));
}
}
diff --git a/frontend/discourse/app/static/dev-tools/entrypoint.js b/frontend/discourse/app/static/dev-tools/entrypoint.ts
similarity index 100%
rename from frontend/discourse/app/static/dev-tools/entrypoint.js
rename to frontend/discourse/app/static/dev-tools/entrypoint.ts
diff --git a/frontend/discourse/app/static/dev-tools/toolbar.gjs b/frontend/discourse/app/static/dev-tools/toolbar.gts
similarity index 71%
rename from frontend/discourse/app/static/dev-tools/toolbar.gjs
rename to frontend/discourse/app/static/dev-tools/toolbar.gts
index 5cff14b26fc..05d278d4192 100644
--- a/frontend/discourse/app/static/dev-tools/toolbar.gjs
+++ b/frontend/discourse/app/static/dev-tools/toolbar.gts
@@ -8,14 +8,10 @@ import dIcon from "discourse/ui-kit/helpers/d-icon";
import dOnResize from "discourse/ui-kit/modifiers/d-on-resize";
import dPointerDrag from "discourse/ui-kit/modifiers/d-pointer-drag";
import I18n, { i18n } from "discourse-i18n";
-import BlockDebugButton from "./block-debug/button";
-import PluginOutletDebugButton from "./plugin-outlet-debug/button";
-import SafeModeButton from "./safe-mode/button";
-import UpcomingChangesDebugButton from "./upcoming-changes-debug/button";
-import VerboseLocalizationButton from "./verbose-localization/button";
+import { CORE_TOOLS } from "./tools";
export default class Toolbar extends Component {
- @tracked activeDragOffset = null;
+ @tracked activeDragOffset: number | null = null;
@tracked ownSize = 0;
@tracked top = 250;
@@ -39,12 +35,12 @@ export default class Toolbar extends Component {
}
@action
- didStartDrag(event) {
- const realTop = event.target
- .closest(".dev-tools-toolbar")
- .getBoundingClientRect().top;
- const dragStartedAtY = event.pageY;
- this.activeDragOffset = dragStartedAtY - realTop;
+ didStartDrag(event: PointerEvent) {
+ // The gripper renders inside the toolbar, so the press always has an
+ // ancestor to measure against.
+ const toolbar = (event.target as Element).closest(".dev-tools-toolbar")!;
+
+ this.activeDragOffset = event.pageY - toolbar.getBoundingClientRect().top;
}
@action
@@ -53,13 +49,14 @@ export default class Toolbar extends Component {
}
@action
- dragMove(event) {
- const dragY = event.pageY;
- this.top = dragY - this.activeDragOffset;
+ dragMove(event: PointerEvent) {
+ // Only ever called between a drag starting and ending, so the offset the
+ // start captured is always there.
+ this.top = event.pageY - this.activeDragOffset!;
}
@action
- onResize(entries) {
+ onResize(entries: ResizeObserverEntry[]) {
this.ownSize = entries[0].contentRect.height;
}
@@ -85,11 +82,9 @@ export default class Toolbar extends Component {
>
{{dIcon "grip-vertical"}}
</button>
- <PluginOutletDebugButton />
- <BlockDebugButton />
- <UpcomingChangesDebugButton />
- <SafeModeButton />
- <VerboseLocalizationButton />
+ {{#each CORE_TOOLS key="id" as |tool|}}
+ <tool.component />
+ {{/each}}
<button
type="button"
title={{i18n "dev_tools.disable_dev_tools"}}
diff --git a/frontend/discourse/app/static/dev-tools/tools.ts b/frontend/discourse/app/static/dev-tools/tools.ts
new file mode 100644
index 00000000000..344ca1581cc
--- /dev/null
+++ b/frontend/discourse/app/static/dev-tools/tools.ts
@@ -0,0 +1,26 @@
+import type { ComponentLike } from "@glint/template";
+import BlockDebugButton from "./block-debug/button";
+import PluginOutletDebugButton from "./plugin-outlet-debug/button";
+import SafeModeButton from "./safe-mode/button";
+import UpcomingChangesDebugButton from "./upcoming-changes-debug/button";
+import VerboseLocalizationButton from "./verbose-localization/button";
+
+interface DevTool {
+ id: string;
+ component: ComponentLike;
+}
+
+/**
+ * The tools shipped with Discourse, in the order they appear in the toolbar.
+ *
+ * Adding a tool is an entry here and a component; the toolbar reads this list
+ * rather than naming each button itself. Identifiers match the directory a tool
+ * lives in.
+ */
+export const CORE_TOOLS: readonly DevTool[] = [
+ { id: "plugin-outlet-debug", component: PluginOutletDebugButton },
+ { id: "block-debug", component: BlockDebugButton },
+ { id: "upcoming-changes-debug", component: UpcomingChangesDebugButton },
+ { id: "safe-mode", component: SafeModeButton },
+ { id: "verbose-localization", component: VerboseLocalizationButton },
+];
diff --git a/frontend/discourse/tests/integration/components/dev-tools-toolbar-test.gjs b/frontend/discourse/tests/integration/components/dev-tools-toolbar-test.gjs
index 82426f22cae..6ab4de48382 100644
--- a/frontend/discourse/tests/integration/components/dev-tools-toolbar-test.gjs
+++ b/frontend/discourse/tests/integration/components/dev-tools-toolbar-test.gjs
@@ -1,9 +1,56 @@
import { find, render, triggerEvent } from "@ember/test-helpers";
import { module, test } from "qunit";
import Toolbar from "discourse/static/dev-tools/toolbar";
+import { CORE_TOOLS } from "discourse/static/dev-tools/tools";
import { setupRenderingTest } from "discourse/tests/helpers/component-test";
import { stubPointerCapture } from "discourse/tests/helpers/ui-kit/pointer-gesture-helper";
+/**
+ * The toolbar as it is meant to read, left to right. Spelled out rather than
+ * derived from the tool table, so that the two assertions below can disagree:
+ * one holds the rendered toolbar to this order, the other holds the table to
+ * it. Deriving it would let both move together and catch neither.
+ */
+const EXPECTED_ORDER = [
+ "gripper",
+ "plugin-outlet-debug",
+ "block-debug",
+ "upcoming-changes-debug",
+ "safe-mode",
+ "verbose-localization",
+ "disable-dev-tools",
+];
+
+const SELECTORS = {
+ gripper: ".gripper",
+ "plugin-outlet-debug": ".toggle-plugin-outlets",
+ "block-debug": ".toggle-blocks",
+ "upcoming-changes-debug": ".toggle-upcoming-changes-menu",
+ "safe-mode": ".toggle-safe-mode",
+ "verbose-localization": ".toggle-verbose-localization",
+ "disable-dev-tools": ".disable-dev-tools",
+};
+
+/**
+ * The order the toolbar's landmarks appear in, read back from the rendered DOM.
+ * Matches descendants rather than direct children, so a tool that wraps its
+ * button is still found, and reports repeats rather than collapsing them, so a
+ * landmark rendered twice fails instead of reading back as one.
+ */
+function renderedOrder() {
+ const seen = [];
+
+ for (const element of document.querySelectorAll(".dev-tools-toolbar *")) {
+ for (const [id, selector] of Object.entries(SELECTORS)) {
+ if (element.matches(selector)) {
+ seen.push(id);
+ }
+ }
+ }
+
+ return seen;
+}
+
/**
* The toolbar positions itself with `top: min(100dvh - <own height>px, <top>px)`,
* so the tracked value is read back out of the declaration rather than guessed at.
@@ -167,6 +214,14 @@ module("Integration | Component | DevTools | Toolbar", function (hooks) {
pageX: 10,
pageY: 300,
});
+ await triggerEvent(gripper, "pointermove", {
+ pointerId: 1,
+ clientX: 10,
+ clientY: 380,
+ pageX: 10,
+ pageY: 380,
+ });
+ const dragged = declaredTop();
await triggerEvent(gripper, "pointercancel", { pointerId: 1 });
assert
@@ -181,5 +236,39 @@ module("Integration | Component | DevTools | Toolbar", function (hooks) {
"dragging",
"the cancelled gesture releases the page mark"
);
+ assert.strictEqual(
+ declaredTop(),
+ dragged,
+ "the interruption commits rather than discards, so the toolbar stays where it was dragged to instead of snapping back"
+ );
+ });
+
+ test("renders every tool, in order, between the gripper and the disable button", async function (assert) {
+ await render(<template><Toolbar /></template>);
+
+ assert.deepEqual(
+ renderedOrder(),
+ EXPECTED_ORDER,
+ "each tool mounts its button, in the order the table lists it"
+ );
+ });
+
+ test("the tool table is what the toolbar renders", async function (assert) {
+ assert.deepEqual(
+ CORE_TOOLS.map(({ id }) => id),
+ EXPECTED_ORDER.slice(1, -1),
+ "the table lists exactly the tools the toolbar is expected to show, in that order"
+ );
+ });
+
+ test("the gripper and disable button render outside the tool list", async function (assert) {
+ await render(<template><Toolbar /></template>);
+
+ assert
+ .dom(".dev-tools-toolbar > .gripper")
+ .exists("the gripper is a child of the toolbar, not of a tool");
+ assert
+ .dom(".dev-tools-toolbar > .disable-dev-tools")
+ .exists("the disable button is a child of the toolbar, not of a tool");
});
});