дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-12-06 18:17:15 +02:00
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]>
Цей коміт міститься в:
1 джерело
896ec3b924
коміт
67c00a986a
21 files changed
+480
-14
No files matched your search
@@ -0,0 +1,96 @@
|
||||
import { afterEach, beforeEach, expect, it, vi } from "vitest";
|
||||
import type { PluginInfo, UiContribution } from "@/bindings";
|
||||
import { useCloneSources } from "@/workbench/contributions";
|
||||
import { createMemoryPluginsBackend, setPluginsBackend } from "../backend";
|
||||
import { clonePickFromMessage, startCloneSourcePlugins } from "../cloneSource";
|
||||
import { clearUiContributions, syncUiContributions } from "../projection";
|
||||
import { defineViewUiPoints } from "../uiPoints";
|
||||
|
||||
const plugins = [{ id: "gitee", status: "inactive" }] as PluginInfo[];
|
||||
const source = (extra: Record<string, unknown> = {}): UiContribution => ({
|
||||
pluginId: "gitee",
|
||||
point: "cloneSources",
|
||||
key: "cloneSources/gitee",
|
||||
decl: { id: "gitee", label: "Gitee", web: "clone/index.html", ...extra },
|
||||
});
|
||||
const registered = () => useCloneSources.getState().sources.get("plugin:gitee/gitee");
|
||||
let stop: () => void;
|
||||
|
||||
beforeEach(() => {
|
||||
clearUiContributions();
|
||||
stop = defineViewUiPoints();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
clearUiContributions();
|
||||
stop();
|
||||
setPluginsBackend(null);
|
||||
});
|
||||
|
||||
it("reads the repository a page picked", () => {
|
||||
expect(clonePickFromMessage('{"op":"clone-url","url":"https://gitee.com/acme/widgets.git"}')).toEqual({
|
||||
url: "https://gitee.com/acme/widgets.git",
|
||||
clone: false,
|
||||
});
|
||||
expect(clonePickFromMessage('{"op":"clone-url","url":" [email protected]:acme/widgets.git "}')).toEqual({
|
||||
url: "[email protected]:acme/widgets.git",
|
||||
clone: false,
|
||||
});
|
||||
// A double click asks the dialog to clone right away, like the built-in sources.
|
||||
expect(clonePickFromMessage('{"op":"clone-url","url":"https://gitee.com/a/b.git","clone":true}')).toEqual({
|
||||
url: "https://gitee.com/a/b.git",
|
||||
clone: true,
|
||||
});
|
||||
});
|
||||
|
||||
it("leaves everything else to the plugin", () => {
|
||||
for (const text of [
|
||||
"",
|
||||
"not json",
|
||||
"[]",
|
||||
"null",
|
||||
'{"op":"other","url":"https://gitee.com/a/b.git"}',
|
||||
'{"op":"clone-url"}',
|
||||
'{"op":"clone-url","url":42}',
|
||||
'{"op":"clone-url","url":" "}',
|
||||
// A URL with a space is not a clone URL: the dialog's field takes one token.
|
||||
'{"op":"clone-url","url":"https://gitee.com/a b"}',
|
||||
]) {
|
||||
expect(clonePickFromMessage(text), text).toBeNull();
|
||||
}
|
||||
});
|
||||
|
||||
it("registers the source with the dialog as the plugin declared it", () => {
|
||||
syncUiContributions([source({ icon: "branch" })], plugins);
|
||||
expect(registered()).toMatchObject({ id: "plugin:gitee/gitee", label: "Gitee", icon: "branch" });
|
||||
});
|
||||
|
||||
it("falls back to a generic icon, and disposes with the plugin", () => {
|
||||
syncUiContributions([source()], plugins);
|
||||
expect(registered()?.icon).toBe("branch");
|
||||
syncUiContributions([], plugins);
|
||||
expect(registered()).toBeUndefined();
|
||||
});
|
||||
|
||||
it("shows a package SVG as the source's icon", async () => {
|
||||
setPluginsBackend({
|
||||
...createMemoryPluginsBackend([]),
|
||||
assets: async () => [["cloneSourceIcon/gitee", '<svg xmlns="http://www.w3.org/2000/svg"><rect/></svg>']],
|
||||
});
|
||||
syncUiContributions([source({ icon: "icon.svg" })], plugins);
|
||||
// The asset arrives asynchronously; the source is registered once it does.
|
||||
await vi.waitFor(() => expect(registered()?.icon).toContain("pluginIcon/gitee"));
|
||||
});
|
||||
|
||||
it("loads only clone sources in the standalone Get from VCS window", async () => {
|
||||
stop();
|
||||
stop = () => {};
|
||||
setPluginsBackend({
|
||||
...createMemoryPluginsBackend([]),
|
||||
list: async () => ({ plugins, activeTheme: null, ui: [source({ icon: "branch" })] }),
|
||||
});
|
||||
const stopStandalone = startCloneSourcePlugins();
|
||||
await vi.waitFor(() => expect(registered()).toMatchObject({ id: "plugin:gitee/gitee", label: "Gitee" }));
|
||||
stopStandalone();
|
||||
expect(registered()).toBeUndefined();
|
||||
});
|
||||
@@ -0,0 +1,10 @@
|
||||
.pane {
|
||||
height: 100%;
|
||||
min-width: 0;
|
||||
}
|
||||
|
||||
/* Cloning: the page stays visible but takes no input, like the built-in sources. */
|
||||
.pane[aria-disabled="true"] {
|
||||
opacity: 0.6;
|
||||
pointer-events: none;
|
||||
}
|
||||
@@ -0,0 +1,152 @@
|
||||
/**
|
||||
* 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();
|
||||
};
|
||||
}
|
||||
@@ -90,6 +90,7 @@ const TEXT_KEYS: Record<string, readonly string[]> = {
|
||||
iconThemes: ["label"],
|
||||
themes: ["name"],
|
||||
secrets: ["label", "description"],
|
||||
cloneSources: ["label"],
|
||||
};
|
||||
|
||||
function localizeDecl(decl: unknown, keys: readonly string[], t: PluginTranslate): unknown {
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
* - `editorPanels`: a web page at the right edge of every editor group, following its text editor;
|
||||
* - `overlays`: a web page floating over the workbench that never takes the focus;
|
||||
* - `dialogs`: a web page in a modal, resizable dialog that `views.reveal(id)` opens;
|
||||
* - `cloneSources`: an entry of the Clone Repository dialog's source list, whose page picks a repository;
|
||||
* - `fileIcons`: icons for the files a plugin knows, whatever icon theme is in use;
|
||||
* - `fileTemplates`: entries of the New menu;
|
||||
* - `commitChecks`: before-commit checks listed in Commit Options, run by the plugin's code.
|
||||
@@ -84,6 +85,7 @@ import { pluginServiceProvider } from "./views/serviceProvider";
|
||||
import { WebView } from "./views/WebView";
|
||||
import { registerLogActions } from "./views/logActions";
|
||||
import { projectSettings, type SettingsDecl } from "./settings";
|
||||
import { defineCloneSourceUiPoint } from "./cloneSource";
|
||||
|
||||
// --- menus ------------------------------------------------------------------------
|
||||
|
||||
@@ -624,6 +626,7 @@ export function defineViewUiPoints(): () => void {
|
||||
title: "Tool Windows",
|
||||
project: ({ pluginId, decl }, { index }) => projectView(pluginId, decl, index),
|
||||
}),
|
||||
defineCloneSourceUiPoint(),
|
||||
defineUiPoint<CustomEditorDecl>("customEditors", {
|
||||
title: "Editors",
|
||||
project: ({ pluginId, decl }) => {
|
||||
|
||||
@@ -304,6 +304,11 @@ export interface WebViewProps {
|
||||
pluginDialog?: FrameDialog | null;
|
||||
/** A custom run-configuration form (`[[contributes.runConfigTypes]]`). */
|
||||
runConfig?: FrameRunConfig | null;
|
||||
/**
|
||||
* Text the page posted. Returning true means it was handled here and does not
|
||||
* reach the plugin's code (the Clone dialog's sources use it for `clone-url`).
|
||||
*/
|
||||
onText?: (text: string) => boolean;
|
||||
}
|
||||
|
||||
export function WebView({
|
||||
@@ -318,6 +323,7 @@ export function WebView({
|
||||
overlay = null,
|
||||
pluginDialog = null,
|
||||
runConfig = null,
|
||||
onText,
|
||||
}: WebViewProps) {
|
||||
const plugin = usePlugins((s) => s.plugins.find((p) => p.id === pluginId));
|
||||
const page = usePageUrl(pluginId, view, plugin);
|
||||
@@ -337,6 +343,10 @@ export function WebView({
|
||||
anyActionRef.current = anyAction;
|
||||
}, [anyAction]);
|
||||
const frame = useRef<HTMLIFrameElement>(null);
|
||||
const onTextRef = useRef(onText);
|
||||
useEffect(() => {
|
||||
onTextRef.current = onText;
|
||||
}, [onText]);
|
||||
|
||||
const post = useCallback((message: object, transfer: Transferable[] = []) => {
|
||||
frame.current?.contentWindow?.postMessage({ tag: FRAME_TAG, ...message }, "*", transfer);
|
||||
@@ -607,6 +617,7 @@ export function WebView({
|
||||
(err: unknown) => post({ reply: id, error: errorText(err) }),
|
||||
);
|
||||
} else if (typeof m.text === "string") {
|
||||
if (onTextRef.current?.(m.text)) return;
|
||||
if (handleLogTabMessage(m.text)) return;
|
||||
if (handleContentTabMessage(m.text)) return;
|
||||
if (handleDiffMessage(m.text)) return;
|
||||
|
||||
Reference in new issue
Заблокувати користувача