Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:art: Hide the proxy host and port inputs for automatic proxy modes

Jeffrey Chen7524bf10217f
diff --git a/app/src/config/tabs/appTab.test.ts b/app/src/config/tabs/appTab.test.ts
index 03c0c83156..49d416a43d 100644
--- a/app/src/config/tabs/appTab.test.ts
+++ b/app/src/config/tabs/appTab.test.ts
@@ -13,7 +13,7 @@ type Invoke = (channel: string, data: {cmd: string; enabled?: boolean}) => Promi
type Slot = {key: string; html: () => string; afterMount: (root: unknown) => Promise<void>};
const getAccessibilitySlot = (browser: boolean, mobile: boolean, invoke?: Invoke, slotKey = "accessibilitySupport",
- switches: string[] = []): Slot | undefined => {
+ switches: string[] = [], scheme = "http"): Slot | undefined => {
const source = readFileSync(resolve(process.cwd(), "src/config/tabs/appTab.ts"), "utf8");
const processed = parse(source, {BROWSER: browser, MOBILE: mobile}, false, true);
const code = transpileModule(processed, {compilerOptions: {module: ModuleKind.CommonJS}}).outputText;
@@ -24,7 +24,7 @@ const getAccessibilitySlot = (browser: boolean, mobile: boolean, invoke?: Invoke
console: {warn: (): void => undefined},
window: {siyuan: {
languages: new Proxy({}, {get: (_, key) => String(key)}),
- config: {system: {isMicrosoftStore: true, networkProxy: {host: 'user:"<&@host', port: "7890", scheme: "http"}}},
+ config: {system: {isMicrosoftStore: true, networkProxy: {host: 'user:"<&@host', port: "7890", scheme}}},
}},
require: (name: string) => {
if (name === "electron") {
@@ -80,6 +80,71 @@ test("network proxy renders escaped values in every build target", () => {
}
});
+test("network proxy keeps its layout space and hides only the manual inputs", () => {
+ const manual = getAccessibilitySlot(true, false, undefined, "networkProxy");
+ assert.ok(manual);
+ assert.ok(manual.html().includes('<div id="networkProxyManual" class="fn__flex fn__flex-1">'));
+ assert.equal(manual.html().includes('class="b3-text-field fn__flex-1 fn__none"'), false);
+
+ for (const scheme of ["system", ""]) {
+ const automatic = getAccessibilitySlot(true, false, undefined, "networkProxy", [], scheme);
+ assert.ok(automatic);
+ assert.ok(automatic.html().includes('<div id="networkProxyManual" class="fn__flex fn__flex-1">'));
+ assert.equal(automatic.html().match(/class="b3-text-field fn__flex-1 fn__none"/g)?.length, 2);
+ }
+});
+
+const proxyControls = (scheme: string) => {
+ let change: () => Promise<void>;
+ const schemeElement = {
+ value: scheme,
+ addEventListener: (_: string, listener: () => Promise<void>) => { change = listener; },
+ };
+ const hidden: Record<string, boolean[]> = {host: [], port: []};
+ const field = (name: string) => ({
+ value: "",
+ classList: {
+ toggle: (className: string, force?: boolean): void => {
+ assert.equal(className, "fn__none");
+ hidden[name].push(Boolean(force));
+ },
+ },
+ });
+ const host = field("host");
+ const port = field("port");
+ const root = {
+ querySelector: (selector: string) => {
+ if (selector === "#networkProxyScheme") {
+ return schemeElement;
+ }
+ if (selector === "#networkProxyHost") {
+ return host;
+ }
+ if (selector === "#networkProxyPort") {
+ return port;
+ }
+ return {addEventListener: (): void => undefined};
+ },
+ };
+ return {root, schemeElement, hidden, change: () => change()};
+};
+
+test("network proxy hides host and port inputs unless a manual scheme is selected", async () => {
+ const cases = [["http", false], ["https", false], ["socks5", false], ["system", true], ["", true]] as const;
+ for (const [scheme, hidden] of cases) {
+ const slot = getAccessibilitySlot(true, false, undefined, "networkProxy");
+ const view = proxyControls(scheme);
+ await slot.afterMount(view.root);
+ assert.deepEqual(view.hidden, {host: [hidden], port: [hidden]}, `scheme ${scheme}`);
+ }
+ const slot = getAccessibilitySlot(true, false, undefined, "networkProxy");
+ const view = proxyControls("http");
+ await slot.afterMount(view.root);
+ view.schemeElement.value = "system";
+ await view.change();
+ assert.deepEqual(view.hidden, {host: [false, true], port: [false, true]});
+});
+
const controls = () => {
let change: () => Promise<void>;
const input = {
diff --git a/app/src/config/tabs/appTab.ts b/app/src/config/tabs/appTab.ts
index 59e146bc9f..e171bba9e7 100644
--- a/app/src/config/tabs/appTab.ts
+++ b/app/src/config/tabs/appTab.ts
@@ -343,8 +343,12 @@ const mountLinuxInputMethodSetting = async (root: HTMLElement) => {
};
/// #endif
+// 直接连接和使用系统代理时不使用主机与端口,由界面自行取值
+const isManualProxyScheme = (scheme: string) => "" !== scheme && "system" !== scheme;
+
const genNetworkProxyHtml = (): string => {
const proxy = window.siyuan.config.system.networkProxy;
+ const inputClass = `b3-text-field fn__flex-1${isManualProxyScheme(proxy.scheme || "") ? "" : " fn__none"}`;
return `<div class="b3-label config-item">
${genConfigItemName(window.siyuan.languages.networkProxy)}
<div class="b3-label__text">
@@ -359,9 +363,11 @@ const genNetworkProxyHtml = (): string => {
<option value="http" ${proxy.scheme === "http" ? "selected" : ""}>HTTP</option>
</select>
<span class="fn__space"></span>
- <input spellcheck="false" id="networkProxyHost" placeholder="user:pass@IP" class="b3-text-field fn__flex-1" value="${escapeHtmlTextAndAttr(proxy.host)}"/>
- <span class="fn__space"></span>
- <input id="networkProxyPort" placeholder="Port" class="b3-text-field fn__flex-1" value="${escapeHtmlTextAndAttr(proxy.port)}" type="number"/>
+ <div id="networkProxyManual" class="fn__flex fn__flex-1">
+ <input spellcheck="false" id="networkProxyHost" placeholder="user:pass@IP" class="${inputClass}" value="${escapeHtmlTextAndAttr(proxy.host)}"/>
+ <span class="fn__space"></span>
+ <input id="networkProxyPort" placeholder="Port" class="${inputClass}" value="${escapeHtmlTextAndAttr(proxy.port)}" type="number"/>
+ </div>
<span class="fn__space"></span>
<button id="networkProxyConfirm" class="b3-button fn__size200 b3-button--outline">${window.siyuan.languages.confirm}</button>
</div>
@@ -372,13 +378,14 @@ const mountNetworkProxy = (root: HTMLElement) => {
const schemeElement = root.querySelector("#networkProxyScheme") as HTMLSelectElement;
const hostElement = root.querySelector("#networkProxyHost") as HTMLInputElement;
const portElement = root.querySelector("#networkProxyPort") as HTMLInputElement;
- const updateInputs = () => {
- const disabled = schemeElement.value === "" || schemeElement.value === "system";
- hostElement.disabled = disabled;
- portElement.disabled = disabled;
+ // 容器始终占据剩余空间,隐藏输入框后确定按钮仍保持右对齐
+ const updateManualVisibility = () => {
+ const hidden = !isManualProxyScheme(schemeElement.value);
+ hostElement.classList.toggle("fn__none", hidden);
+ portElement.classList.toggle("fn__none", hidden);
};
- schemeElement.addEventListener("change", updateInputs);
- updateInputs();
+ schemeElement.addEventListener("change", updateManualVisibility);
+ updateManualVisibility();
root.querySelector("#networkProxyConfirm")?.addEventListener("click", () => {
const scheme = schemeElement.value as Config.TSystemNetworkProxyScheme;
const host = hostElement.value;