Файли
openidea/src/features/plugins/cloneSource.tsx
T
arookieofc-ai[bot]таarookieofc-ai 67c00a986a feat(plugins): add clone sources to the Clone Repository dialog (#7948)
[[contributes.cloneSources]] (API 1.21) adds an entry to the Get from VCS source list whose required web page lists repositories and hands the chosen one to the dialog with {"op": "clone-url", "url": …}. The page is served and messaged like other plugin web UI under the key cloneSources/<id>; the dialog fills its URL and directory and git only runs what the user confirms. The standalone Get from VCS window registers only this contribution point. plugin_web_page and plugin_view_event now also run in projectless windows (outside_restricted); Restricted Mode still runs no plugin UI or code. Docs list the point under API 1.21.

Fixes #5572

Co-authored-by: arookieofc-ai <[email protected]>
2026-12-04 20:46:11 +08:00

153 рядки
5.3 KiB
TypeScript

/**
* Clone sources contributed by plugins (`[[contributes.cloneSources]]`).
*
* The declaration's page is a web view of the plugin: it lists repositories and
* posts the one the user picks. The dialog's own `registerCloneSource` handles
* everything else — ordering, the disabled state while cloning, double-click to
* clone right away — so the dialog itself needs to know nothing about plugins.
*/
import type { ComponentType } from "react";
import type { CloneSourceProps } from "@/workbench/contributions";
import { notifications, registerCloneSource } from "@/workbench/contributions";
import { tx } from "@/platform/i18n";
import { errorText, refreshPlugins, syncPluginsAcrossWindows } from "./store";
import { pluginsBackend } from "./backend";
import { defineUiPoint } from "./projection";
import { syncPluginProjection } from "./themeRuntime";
import { pluginIconName } from "./pluginIcon";
import styles from "./cloneSource.module.css";
import { WebView } from "./views/WebView";
import { bumpView, deliverViewMessage } from "./views/viewStore";
/** What the page declares (`[[contributes.cloneSources]]`), already validated by the host. */
export interface CloneSourceDecl {
id: string;
label: string;
/** An IDE icon name, or a package SVG (`icons/gitee.svg`). */
icon?: string;
web: string;
}
/** The repository a page asked for, or null when the message is the plugin's own. */
export function clonePickFromMessage(text: string): { url: string; clone: boolean } | null {
let parsed: unknown;
try {
parsed = JSON.parse(text);
} catch {
return null;
}
if (!parsed || typeof parsed !== "object") return null;
const message = parsed as { op?: unknown; url?: unknown; clone?: unknown };
if (message.op !== "clone-url" || typeof message.url !== "string") return null;
const url = message.url.trim();
// A single line and no spaces: the dialog's field takes a clone URL, not a command.
if (!url || /\s/.test(url)) return null;
return { url, clone: message.clone === true };
}
/** The pane: the plugin's page, with its `clone-url` taken off the message stream. */
export function PluginCloneSource({
pluginId,
view,
label,
disabled,
onUrl,
onClone,
}: CloneSourceProps & { pluginId: string; view: string; label: string }) {
return (
<div className={styles.pane} aria-disabled={disabled || undefined} inert={disabled || undefined}>
<WebView
pluginId={pluginId}
view={view}
label={label}
header="none"
onText={(text) => {
const picked = clonePickFromMessage(text);
if (!picked) return false;
if (picked.clone) onClone(picked.url);
else onUrl(picked.url);
return true;
}}
/>
</div>
);
}
/**
* Registers a plugin's source with the dialog, icon first (a package SVG is an
* asset of the plugin, so it arrives asynchronously), and returns the disposer.
*/
export function projectCloneSource(pluginId: string, decl: CloneSourceDecl): () => void {
let disposed = false;
let undo: (() => void) | undefined;
const component: ComponentType<CloneSourceProps> = (props) => (
<PluginCloneSource pluginId={pluginId} view={`cloneSources/${decl.id}`} label={decl.label} {...props} />
);
const register = (icon: string) => {
if (disposed) return;
undo = registerCloneSource({
// Namespaced: two plugins may both call theirs "gitee".
id: `plugin:${pluginId}/${decl.id}`,
label: decl.label,
icon,
component,
});
};
const icon = decl.icon?.trim();
if (icon?.endsWith(".svg")) {
void pluginsBackend()
.assets(pluginId, `cloneSourceIcon/${decl.id}`)
.then((assets) => {
const svg = assets.find(([id]) => id === `cloneSourceIcon/${decl.id}`)?.[1];
register(svg ? pluginIconName(pluginId, { light: svg, dark: null }) : "branch");
})
.catch((e: unknown) => {
notifications().notify({
group: "plugins",
level: "warning",
title: tx`Could not load the ${decl.label} icon`,
body: tx`plugin ${pluginId}: ${errorText(e)}`,
});
register("branch");
});
} else {
register(icon || "branch");
}
return () => {
disposed = true;
undo?.();
};
}
/** Registers the client side of `cloneSources`; shared by the workbench and its standalone window. */
export function defineCloneSourceUiPoint(): () => void {
return defineUiPoint<CloneSourceDecl>("cloneSources", {
title: "Clone Sources",
project: ({ pluginId, decl }) => projectCloneSource(pluginId, decl),
});
}
/**
* Registers plugin clone sources in a window that is not the workbench (the
* standalone Get from VCS window): only this point, not every plugin surface.
*/
export function startCloneSourcePlugins(): () => void {
const undefine = defineCloneSourceUiPoint();
const stopProjection = syncPluginProjection();
const stopWindowSync = syncPluginsAcrossWindows();
const stopEvents = pluginsBackend().onEvent((e) => {
if (e.type === "commandsChanged") void refreshPlugins();
else if (e.type === "viewChanged") bumpView(e.pluginId, e.view);
else if (e.type === "viewMessage") deliverViewMessage(e.pluginId, e.view, e.message);
});
void refreshPlugins();
return () => {
stopEvents();
stopWindowSync();
stopProjection();
undefine();
};
}