Repository navigation
Feat/unify command palettes - #11
Merged
Merged
Conversation
…tration - Added IframeCommandBridge to subscribe to commands from focused iframe windows. - Commands are re-registered as slash-commands in the shell palette, allowing for dynamic updates. - Introduced unregisterByOwner function to evict commands when focus shifts between windows. - Enhanced AiAssistant to eagerly display commands without requiring user input. - Implemented keyboard navigation improvements for command selection. - Added sorting functionality for commands to prioritize iframe commands. - Updated types to support harvested commands from iframes.
Collaborator
Author
There was a problem hiding this comment.
We need to refactor this and ship it into smaller and meaningful pieces as one of the next steps.
Collaborator
Author
There was a problem hiding this comment.
Same thing, this ai-assistant.ts is a monster. It's actually the Command Palette, not sure if a renaming is worthy, as its also acting as a AI assistant.
epeicher
approved these changes
Apr 24, 2026
Collaborator
There was a problem hiding this comment.
This is great @AllTerrainDeveloper! I have tested it, and it works very well 🙌 LGTM!
Collaborator
Author
|
I have cleaned up the logs and a some dead code :) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
feat/iframe-command-bridge — Harvest
@wordpress/commandsinto the shell paletteSummary
Demo.CM.mov
Replaces the "press Cmd+K twice to escape the native palette" workaround with a proper bridge: the shell palette hijacks Cmd+K inside chromeless iframes, harvests the focused window's
wp.data.select('core/commands')registry, and re-publishes every command (context-scoped statics and dynamic loader hooks like "Duplicate block") as first-class entries in our own palette. Selecting one runs inside the source iframe via postMessage, or opens a new desktop window when the command is a pure navigation.One palette. One keystroke. Contextual to the focused window. The native WP palette is never seen.
Motivation
Before this PR we had two disconnected palettes:
@wordpress/commandsUI — only visible while focus was inside a Gutenberg / site-editor iframe.Cmd+K fired whichever one owned focus first, and the user had to double-tap to escalate from one to the other. Worse, every command the WP editor exposed ("toggle distraction-free", "duplicate block", pattern insertions) was stranded inside its iframe and invisible to the desktop shell.
The fix is architecturally invasive enough that we couldn't ship it as a small follow-up — it touches the chromeless bridge, the palette UI, the command registry, and adds a new cross-frame protocol. Grouping it into one PR keeps the story readable.
What changed
New cross-frame protocol
wp-desktop-commands-subscribewp-desktop-commands-unsubscribewp-desktop-commands-invokewp-desktop-commands-listwp-desktop-bridge-readyAll messages are
window.location.origin-gated on both ends.Iframe side (
includes/render.php).commands-command-menuthat might render via a non-keyboard path.wp.element.createRoot. It's the only way to legally calluseSelect+ each loader'shook— Gutenberg loaders are React hooks and can't run outside a render. We went through a classic setState-inside-useEffect loop en route to a ref-based bucket (see "Gotchas"). Results cover tier-2 (getCommands(true)) + tier-3 (getCommandLoaders(true)). Tier-1 (global nav) is deliberately skipped — the dock already has those.Function.prototype.toString()+ regex forlocation.href = '…'/.assign('…')/.replace('…'). An earlier dry-run sandbox silently failed to intercept the href setter (Location.prototype.hrefis non-configurable) and every nav callback actually navigated the iframe → infinite window spawning. The static scan is now the only safe path.wp.element.renderToStringbefore crossing postMessage (structured clone refuses React elements). Parent injects the SVG viainnerHTMLin the palette row.__wpdCommandCallbacks): loader-returned commands don't exist ingetCommands(), so invoke needs its own lookup keyed on command name, rebuilt every merge so closures stay fresh.Parent side (
src/commands/iframe-bridge.ts)IframeCommandBridgeclass; installed fromdesktop.ts.iframe:<windowId>owner, subscribe the new one.onFocused.runthat opens a new desktop window viamanager.open({ id: deriveWindowId(url), ... })instead of navigating the current iframe.runthat postswp-desktop-commands-invokeback to the source iframe.Command registry (
src/commands.ts)owner?: stringfield (already existed) + newunregisterByOwner(owner)helper for bulk eviction.eager?: booleanfield:true⇒ surfaces before the user types/. Harvested iframe commands auto-set it. Built-in commands stay slash-only.listEagerCommands()export for the palette.iconSvg?: stringfield for inline SVG glyphs.Palette UX (
src/ai-assistant.ts,assets/css/ai-assistant.css)/→ slash-only commands (disjoint from eager — the two surfaces don't overlap).:hoverstyling while arrow keys drive selection; a realmousemoveclears it.:has()).core/block-editor/action-duplicate) are ugly and not user-facing.Docs
docs/architecture.md— new "Command palette bridge (Cmd+K, hijacked)" section: names the approach as a hack, walks through both sides, calls out caveats.docs/javascript-reference.md— rewrittenwp-desktop-commands-listpayload (no more dry-invoke; static regex + React-mounted loader harvest), addediconSvg/eager/ownertoregisterCommanddocs with the disjoint eager/slash behavior spelled out.Gotchas worth knowing (for future-us)
Location.prototype.hrefis non-configurable. AnObject.definePropertyshim silently fails — no throw, no sentinel. Any code that relies on intercepting navigation via this property should use a static approach (regex on the callback's source) or assume the navigation will happen.getCommands(). Invoke must consult the harvester's own cache first.admin_footer). The parent can sendsubscribearbitrarily earlier. Fix: iframe postswp-desktop-bridge-readyonce listeners exist; parent replays subscribe if the source is the currently focused window.Testing
npm run build— green, bundle grew by ~4 kB gzipped.npm test— 364 / 364 passing.php -l includes/render.php— clean.Manual QA:
/→ eager commands vanish, the built-in/openis the only entry.Breaking changes
None for end users. Plugin-facing: the
eagerflag defaults tofalse, so commands registered viawp.desktop.registerCommand()are now slash-only unless they opt in. No existing caller relies on the previous "always visible on empty input" behavior — the palette only recently shipped that surface.Follow-ups (not in this PR)
core/block-editor'suseCommandLoaderhooks that depend on selection state beyond what we currently surface.docs/examples/showingeager: truevs. default.