Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/api-index.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ The full surface is documented in [`javascript-reference.md`](./javascript-refer
| `HOOKS` | `typeof HOOKS` *(typed hook-name constants)* | Stable |
| `hooks` | `wp.hooks` bridge | Stable |
| `saveSession` | `() => void` | Stable |
| `registerWindowAction` / `unregisterWindowAction` / `listWindowActions` | `( def: WindowActionDef ) => void` *(rows in every window's ⋯ menu; `label`/`icon`/`isVisible` may be per-window functions)* | Experimental |
| `registerWindowAction` / `unregisterWindowAction` / `listWindowActions` | `( def: WindowActionDef ) => void` *(rows in every window's ⋯ menu, as verbs or checkboxes; `label`/`icon`/`isVisible`/`checked` may be per-window functions)* | Experimental |
| [`electron`](./desktop-host.md) | `ElectronAdapterApi` *(set a window free into a real OS window; published by the Electron Adapter extension, absent in a browser)* | Experimental |

### HTTP & UI primitives — must-know
Expand Down
59 changes: 51 additions & 8 deletions docs/examples/window-action.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,46 @@ wp.os.registerWindowAction( {
whatever it depends on — a capability, a connection, the page the window
has navigated to — without your plugin re-registering anything.

## A checkbox row for a per-window preference

A verb runs and the menu closes. A **checkbox** reports a setting the
window either has or does not, and stays open when clicked so the user
watches the tick land:

```js
const KEY = 'my-plugin/show-gridlines';

wp.os.registerWindowAction( {
id: 'my-plugin/show-gridlines',
label: 'Show gridlines',
checkable: true,
checked: () => localStorage.getItem( KEY ) === '1',
isVisible: ( win ) => win.id === 'my-plugin-canvas',
onSelect: () => {
const next = localStorage.getItem( KEY ) === '1' ? '0' : '1';
localStorage.setItem( KEY, next );
repaintCanvas();
},
owner: 'my-plugin-shell',
} );
```

`checked` is **asked, never told**. It runs on every menu open, so you
persist the value and repaint nothing — and the row cannot disagree
with your plugin for longer than one open, however the value changed
(a second window, a settings panel, a REST response landing late).
This is how the built-in Corkboard's "Show pins" works.

`closeOnSelect` overrides the defaults in either direction: `false` on
a verb keeps the menu up, `true` on a checkbox dismisses it after the
flip.

**Checkbox or relabelling verb?** Use the relabelling `label` above
when the two states are two *places* the window can be — the row names
the move. Use a checkbox when they are one setting: a tick says "there
is a thing here, and it is currently off", which a label reading "Show
gridlines" alone cannot.

## Ordering

`order` sorts your row against other plugins' rows; the built-in items
Expand All @@ -82,16 +122,19 @@ order: 60, // earlier than most

## What the framework guarantees

- **The menu closes before `onSelect` runs**, so a handler that opens a
dialog or navigates is not competing with a still-painted popover.
- **The menu closes before `onSelect` runs** for a verb row, so a
handler that opens a dialog or navigates is not competing with a
still-painted popover. A checkbox instead flips its tick
optimistically and leaves the menu open.
- **A throwing resolver or handler is contained.** A row whose `label`
or `isVisible` throws simply does not appear; a handler that throws is
logged. The ⋯ menu is shared surface — one plugin's bug must not cost
the user their "Reload".
or `isVisible` throws simply does not appear; a `checked` that throws
paints unchecked rather than dropping the row; a handler that throws
is logged. The ⋯ menu is shared surface — one plugin's bug must not
cost the user their "Reload".
- **Registration is validated loudly.** A bad `id`, a missing
`onSelect`, a non-function `isVisible` throws a `RegistrationError`
naming the field, at registration time, rather than silently painting
nothing.
`onSelect`, a non-function `isVisible`, or `checkable` without
`checked` throws a `RegistrationError` naming the field, at
registration time, rather than silently painting nothing.

## Deciding when the menu opens

Expand Down
2 changes: 2 additions & 0 deletions docs/hooks-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -3762,6 +3762,8 @@ An interactive PixiJS map of post links — every public post type participates

The window and icon are titled **Corkboard** — a thing you can have on a desk, rather than the name of the data structure behind it. The module directory, window id, REST routes, and every hook below keep the `content_graph` slug.

Nodes are drawn as **discs coloured by post type**, using the same palette the relationship satellites use so a focused post and the bubbles fanned around it read as one system. The original **pin** look — the post type's Dashicon glyph as the node body — is one row away in the window's ⋯ menu ("Show pins"), and the choice persists in `localStorage` under `desktop-mode/corkboard-node-style`. Either way the focused node reveals its glyph, so focus never costs the user the type information. The row is an ordinary checkbox registered through the public [`registerWindowAction`](./javascript-reference.md#wposregisterwindowaction--experimental) surface — see [`examples/window-action.md`](./examples/window-action.md) to add your own.

### `openstation_content_graph_user_can_use` — Experimental (filter)

```php
Expand Down
46 changes: 44 additions & 2 deletions docs/javascript-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -7350,12 +7350,54 @@ wp.os.registerWindowAction( {
| `icon` | `string \| ( win ) => string` | Dashicons class. Optional; also re-read per open. |
| `order` | `number` | Sort order among registered rows. Default `100`. |
| `isVisible` | `( win ) => boolean` | Optional. Re-read per open; omit to show everywhere. |
| `onSelect` | `( win ) => void` | Required. The menu is closed before it is called. |
| `checkable` | `boolean` | Paint the row as a checkbox instead of a verb. Requires `checked`. |
| `checked` | `( win ) => boolean` | Required with `checkable`. Re-read per open. |
| `closeOnSelect` | `boolean` | Whether selecting closes the menu. Defaults to `true` for a verb, `false` for a checkbox. |
| `onSelect` | `( win ) => void` | Required. For a verb the menu is closed before it is called; for a checkbox the tick flips first and the menu stays open. |
| `owner` | `string` | Script handle, for live unregistration on deactivation. See below. |

Also: `wp.os.unregisterWindowAction( id )` and
`wp.os.listWindowActions()`.

**Checkbox rows.** Set `checkable` with a `checked` reader and the row
reports a preference instead of running a verb:

```js
wp.os.registerWindowAction( {
id: 'my-plugin/show-pins',
label: 'Show pins',
checkable: true,
checked: () => localStorage.getItem( 'my-plugin/pins' ) === '1',
isVisible: ( win ) => win.id === 'my-plugin-board',
onSelect: () => {
const next = localStorage.getItem( 'my-plugin/pins' ) === '1' ? '0' : '1';
localStorage.setItem( 'my-plugin/pins', next );
repaint();
},
} );
```

`checked` is asked on every open, never told — so persist the value
and repaint nothing; the row cannot go stale for longer than one open,
however the value changed (another window, a settings panel, a REST
response landing late). The shell flips the tick optimistically on
click and leaves the menu open, so the user sees it land and can flip
it back without reopening. A `checked` that throws paints the row
unchecked rather than dropping it: losing the indicator is recoverable,
losing the row is not.

Registering `checkable` without `checked` throws — a checkbox nobody
can ask renders permanently unticked, which reads as broken
persistence in your plugin rather than a missing field here.

**Checkbox or relabelling verb?** Both express a two-state thing, and
they are not interchangeable. Use a relabelling `label` function when
the two states are two *places* the window can be ("Send to your Mac"
/ "Bring back into OpenStation") — the row names the move. Use a
checkbox when they are one setting the window either has or does not:
a tick says "there is a thing here, and it is currently off", which a
label reading "Show pins" alone cannot.

**Making `owner` mean something.** Pair it with the PHP opt-in
[`openstation_register_window_action_script( 'my-plugin-shell' )`](./hooks-reference.md#openstation_register_window_action_script-handle--experimental-php-function)
and pass the same handle. That puts your script in the live-refresh
Expand All @@ -7368,7 +7410,7 @@ inert: the row stays until the next page reload. That is deliberate
backwards-compat, the same bargain commands and title-bar buttons
offer, but it does mean `owner` alone is not the whole opt-in.

**Why `label` / `icon` / `isVisible` may be functions.** They are read
**Why `label` / `icon` / `isVisible` / `checked` may be functions.** They are read
fresh every time the menu opens, not once at registration. That is what
lets one row express a toggle whose meaning depends on state — "Send to
your Mac" becoming "Bring back into OpenStation" for the same window —
Expand Down
79 changes: 77 additions & 2 deletions src/content-graph/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@
*/

import { __, sprintf } from '../i18n';
import { registerWindowAction } from '../window-actions/registry';
import { fetchGraph, fetchPostDetail, fetchPostTypes, getConfig } from './rest';
import { renderToolbar } from './toolbar';
import { renderPanel } from './panel';
import { GraphScene } from './scene';
import { GraphScene, type NodeStyle } from './scene';
import type { SatelliteRef } from './satellites';
import type { DesktopApiLike } from './pixi-types';
import type { GraphNode, GroupFacet } from './types';
Expand All @@ -40,6 +41,74 @@ declare global {

const WINDOW_ID = 'desktop-mode-content-graph';

/**
* Web-storage key for the node-body preference. Frozen `desktop-mode`
* prefix — see AGENTS.md ("`desktop_mode_*` values are frozen").
*/
const NODE_STYLE_KEY = 'desktop-mode/corkboard-node-style';

/**
* Every live Corkboard scene. The ⋯ menu row is registered once at
* module load — not per render — because the row's identity is "the
* Corkboard's node style", not "this particular mount's node style".
* Registering per render would replace the entry (same id) on every
* reopen and leave the toggle reaching for a scene that had already
* been destroyed.
*/
const liveScenes = new Set< GraphScene >();

/** Session + across-session node-body preference. */
let nodeStyle: NodeStyle = readNodeStyle();

function readNodeStyle(): NodeStyle {
try {
return window.localStorage.getItem( NODE_STYLE_KEY ) === 'icon'
? 'icon'
: 'disc';
} catch {
// Private-mode / blocked storage — fall back to the default.
return 'disc';
}
}

function writeNodeStyle( style: NodeStyle ): void {
try {
window.localStorage.setItem( NODE_STYLE_KEY, style );
} catch {
// Non-fatal: the preference just doesn't survive the session.
}
}

/**
* "Show pins" in the Corkboard's ⋯ menu — the way back to the
* dashicon-glyph nodes the window shipped with. Registered at module
* load, which happens the first time the window opens (the bundle is
* lazy-loaded by the native-window sync); the menu repaints its plugin
* rows on every open, and while open on every registry change, so the
* row lands in the menu of the window that just loaded this bundle.
*
* Registered once for the window kind rather than per mount: the
* preference belongs to "the Corkboard", not to one instance of it.
* Re-registering per render would replace the entry (same id) on every
* reopen and leave `checked` closing over a scene already destroyed.
*/
registerWindowAction( {
id: 'desktop-mode/corkboard-pins',
label: __( 'Show pins' ),
checkable: true,
checked: () => nodeStyle === 'icon',
isVisible: ( win ) =>
( ( win.config as { baseId?: string } ).baseId ?? win.id ) ===
WINDOW_ID,
onSelect: () => {
nodeStyle = nodeStyle === 'icon' ? 'disc' : 'icon';
writeNodeStyle( nodeStyle );
for ( const scene of liveScenes ) {
scene.setNodeStyle( nodeStyle );
}
},
} );

interface ActiveState {
abort: () => void;
}
Expand Down Expand Up @@ -230,14 +299,17 @@ async function renderContentGraph( body: HTMLElement ): Promise< ActiveState > {
},
handleSatelliteClick,
cfg.postTypes,
nodeStyle,
);
liveScenes.add( scene );

try {
await scene.mount( desktopApi );
} catch ( err ) {
stageHost.textContent = __( 'Could not initialise the graph renderer.' );
// eslint-disable-next-line no-console
console.warn( '[content-graph] scene mount failed', err );
liveScenes.delete( scene );
return { abort: () => {} };
}

Expand All @@ -261,7 +333,10 @@ async function renderContentGraph( body: HTMLElement ): Promise< ActiveState > {
aborted = true;
toolbar.destroy();
panel.destroy();
scene?.destroy();
if ( scene ) {
liveScenes.delete( scene );
scene.destroy();
}
scene = null;
},
};
Expand Down
Loading