@decocms/blocks 7.85.1 → 7.85.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@decocms/blocks",
3
- "version": "7.85.1",
3
+ "version": "7.85.2",
4
4
  "type": "module",
5
5
  "engines": {
6
6
  "node": ">=24"
@@ -0,0 +1,58 @@
1
+ import { renderToStaticMarkup } from "react-dom/server";
2
+ import { afterEach, beforeAll, describe, expect, it, vi } from "vitest";
3
+ import { LiveControls } from "./LiveControls";
4
+
5
+ // The bootstrap script guards on a window global, so it runs once for the file.
6
+ beforeAll(() => {
7
+ const host = document.createElement("div");
8
+ host.innerHTML = renderToStaticMarkup(<LiveControls site="shop" />);
9
+ document.body.appendChild(host);
10
+ const script = host.querySelector('script[type="module"]');
11
+ new Function(script?.textContent ?? "")();
12
+ });
13
+
14
+ afterEach(() => {
15
+ vi.restoreAllMocks();
16
+ });
17
+
18
+ function press(target: EventTarget, init: KeyboardEventInit): KeyboardEvent {
19
+ const e = new KeyboardEvent("keydown", {
20
+ bubbles: true,
21
+ cancelable: true,
22
+ composed: true,
23
+ ...init,
24
+ });
25
+ target.dispatchEvent(e);
26
+ return e;
27
+ }
28
+
29
+ describe('LiveControls "." shortcut', () => {
30
+ it("Ctrl+. outside a field opens the editor chooser in a new tab", () => {
31
+ const open = vi.spyOn(window, "open").mockImplementation(() => null);
32
+ expect(press(document.body, { key: ".", ctrlKey: true }).defaultPrevented).toBe(true);
33
+ const url = new URL(open.mock.calls[0][0] as string);
34
+ expect(url.origin + url.pathname).toBe("https://studio.decocms.com/choose-editor");
35
+ expect(url.searchParams.get("site")).toBe("shop");
36
+ });
37
+
38
+ it("leaves typing in a field alone", () => {
39
+ const open = vi.spyOn(window, "open").mockImplementation(() => null);
40
+ const input = document.createElement("input");
41
+ document.body.appendChild(input);
42
+ expect(press(input, { key: "." }).defaultPrevented).toBe(false);
43
+ expect(open).not.toHaveBeenCalled();
44
+ input.remove();
45
+ });
46
+
47
+ it("leaves typing in a field inside a shadow root alone (a third-party widget's form)", () => {
48
+ const open = vi.spyOn(window, "open").mockImplementation(() => null);
49
+ const host = document.createElement("div");
50
+ document.body.appendChild(host);
51
+ const input = document.createElement("input");
52
+ host.attachShadow({ mode: "open" }).appendChild(input);
53
+ // The document sees the host as the target; the email's "." must still type.
54
+ expect(press(input, { key: "." }).defaultPrevented).toBe(false);
55
+ expect(open).not.toHaveBeenCalled();
56
+ host.remove();
57
+ });
58
+ });
@@ -13,7 +13,10 @@ interface LiveControlsProps {
13
13
  * Mirrors production behavior (apps/website/components/_Controls.tsx):
14
14
  * 1. Injects __DECO_STATE for the admin to read
15
15
  * 2. Listens for postMessage events from the admin (inject scripts, scroll, rerender)
16
- * 3. "." opens admin in same tab, Ctrl/Cmd+"." opens in new tab, Ctrl+Shift+E also works
16
+ * 3. "." opens admin in same tab, Ctrl/Cmd+"." opens in new tab, Ctrl+Shift+E also works.
17
+ * Keys typed in a field are left alone, also in a field inside a shadow root
18
+ * (a third-party widget's form): its events reach the document with the host
19
+ * as their target, so the field is read from the event's composed path.
17
20
  */
18
21
  export function LiveControls({ site, page, flags }: LiveControlsProps) {
19
22
  // Keep `window.LIVE.page` in sync with the CURRENT page across SPA
@@ -103,7 +106,7 @@ function LiveControlsScript() {
103
106
 
104
107
  if (window.self === window.top) {
105
108
  document.addEventListener("keydown", function(e) {
106
- var t = e.target;
109
+ var t = e.composedPath ? e.composedPath()[0] : e.target;
107
110
  if (t && (t.tagName === "INPUT" || t.tagName === "TEXTAREA" || t.tagName === "SELECT" || t.isContentEditable)) return;
108
111
  if (e.defaultPrevented) return;
109
112