Файли
openidea/src/features/plugins/views/viewStore.ts
T
Arnaud Ritti abdbc39d0d feat(plugins): let a command ask the user with a picker, an input box or a confirmation (#1779)
Plugin API 1.2 adds `ui.pick`, `ui.pick-many`, `ui.input`, `ui.confirm` and `views.reveal`. The dialogs show in the window whose command or event asked and block that call until the user answers; the wait is added to the call's deadline, so it doesn't use up the time budget. They need no capability and return `invalid` without a window (events without a client, rendering, editor providers). Prompts are withdrawn when the call is cancelled or the plugin stops, and reach remote windows through the dispatcher (`plugin_answer_prompt`). `views.reveal` brings one of the plugin's declared views to the front. The Rust SDK and its mock gain `pick`, `pick_many`, `input`, `confirm` and `reveal_view`. All components are rebuilt for the WIT change.

Closes #1768

Co-authored-by: arnaud-ritti <[email protected]>
2026-11-06 23:18:30 +08:00

43 рядки
1.7 KiB
TypeScript

/**
* Client state of plugin views: a revision per (plugin, view) that bumps when
* the plugin asks for a refresh (`viewChanged`), and the messages its code
* sends to the script of an `html` body (`viewMessage`).
*/
import { create } from "zustand";
export const viewKey = (pluginId: string, view: string) => `${pluginId}/${view}`;
/** The tool window a plugin's declared view is shown in. */
export const pluginToolWindowId = (pluginId: string, view: string) => `plugin.${pluginId}.${view}`;
export const useViewRevisions = create<{ revisions: Record<string, number> }>(() => ({ revisions: {} }));
/** The plugin's view content changed: rendered views fetch it again. */
export function bumpView(pluginId: string, view: string) {
const key = viewKey(pluginId, view);
useViewRevisions.setState((s) => ({ revisions: { ...s.revisions, [key]: (s.revisions[key] ?? 0) + 1 } }));
}
export function useViewRevision(pluginId: string, view: string): number {
return useViewRevisions((s) => s.revisions[viewKey(pluginId, view)] ?? 0);
}
type MessageListener = (message: string) => void;
const messageListeners = new Map<string, Set<MessageListener>>();
/** Delivers a plugin's message to the frames of its view. */
export function deliverViewMessage(pluginId: string, view: string, message: string) {
for (const l of messageListeners.get(viewKey(pluginId, view)) ?? []) l(message);
}
export function onViewMessage(pluginId: string, view: string, listener: MessageListener): () => void {
const key = viewKey(pluginId, view);
const set = messageListeners.get(key) ?? new Set();
set.add(listener);
messageListeners.set(key, set);
return () => {
set.delete(listener);
if (!set.size) messageListeners.delete(key);
};
}