Files
magpie/internal/gui/assets/plugins/redact/settings.js
T
yetone d1fa254b1d gui: masked words and Never mask are a list, a row per word (noahhrreion on Discord)
noahhrreion: "希望脱敏词选项可以做成真正的列表,在一个小输入框里面不方便管理多个脱敏词".
Settings → Privacy's Masked words was one comma-separated box. It is now a
list (wordList in plugins/redact/settings.js): each word is its own row,
edited in place (Enter or leaving the row saves, Escape puts it back,
emptied is removed), × removes it, and the last row adds. Its sibling,
Never mask under the mask actions, had the same box and gets the same list.

Stored values are unchanged: redactWords and the allow list are still
string arrays, saved through the same savePrefs / settings/redact-allow
calls, and nothing is rewritten on open (the new test checks that opening
the page saves nothing). Typed or pasted text splits as the old box did
(, , and newlines, trimmed, empties dropped), so a pasted "a, b" is two
rows. The Hide eye still shows every row as dots. New strings are in the
redact plugin's i18n for zh, zh-TW, ja and de; the two old "separated by
commas" hints are gone from the core i18n. The site's masking docs (en, zh,
ja) say a row each.

Run (on mote via mote-run, Chromium and WebKit):
- new redact-words-list.test.cjs: 5 languages × 1100/400px × both engines,
  draw, add with Enter, paste-split, edit, Escape, ×, empty-and-Tab, reload;
  with i18n-keys 24/24 ok
- redact-words-hidden, any-case, whole, mask-actions, redact-route,
  redact-kinds, redact-shape, settings-search, settings-sections,
  settings-lines-wrap, settings-hints, settings-groups, readable-focus,
  masking-records, mask-alert: 378/378 ok
- go test ./internal/gui/ -run 'TestCoreScriptBudget|TestSettings': ok
  (app.js shrank; no budget raised)
- screenshots light/dark, 1100/400px, both engines, looked at

Broken by hand: without wordFocusBack (focus kept across a redraw) the new
test goes red (waitForFunction timeout after the first add). Dropping the
blur guard during a redraw (wordsRedrawing / isConnected) stays green in
both Playwright engines, which fire no blur on removal; it is kept as a
guard for engines that do, with no test that fails without it.

Not tried: the real app on WebKitGTK / Windows WebView2; no Go files changed.
2026-11-04 12:38:10 +08:00

175 lines
8.2 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Settings › Privacy's Instead of masking, send to, and Masked words and
// Never mask as lists, Masked words as dots until Show (the redact
// plugin's part of Settings). Run after app.js, whose helpers it shares
// (el, icon, openPicker, modelOpt, prefWrite, api), and before start.js
// draws Settings. It wraps renderRedact, so app.js carries none of it: the row
// shows while anything is masked, after the masked-words rows.
// redactRouteRow: where a request masking would change goes instead
// (redactRouteTo, jessyoungyu on X): Off masks it as before; a model whose
// provider the user set to Send requests unmasked (maskRouteModels, on
// this computer or the network) gets it as written, so 「百度这家公司怎么样」
// is answered about Baidu, not a placeholder. Picked in the app's menu,
// saved on its own (settings/redact-route).
function redactRouteRow(s) {
const models = s.maskRouteModels || [];
const v = s.redactRouteTo || "";
const b = el("button", "rt-cond on");
b.type = "button";
b.setAttribute("aria-label", t("Model for requests masking would change"));
const m = models.find((x) => x.id === v);
if (v) b.append(icon(m?.icon || "generic"), el("span", "", m ? `${m.name || m.id} · ${m.providerName}` : v));
else b.append(el("span", "", t("Off")));
b.disabled = !models.length && !v;
b.onclick = (ev) => openPicker({ id: "", name: "", fields: [] }, { key: "redactRouteTo", label: "model", value: v, options: [
{ value: "", label: t("Off"), note: t("Masked and sent to the model asked for, as before"), icon: "generic", reset: true },
...models.map(modelOpt)],
onPick: (id) => {
if (id === v) return;
prefWrite(api("settings/redact-route", { model: id }));
} }, b, ev);
const sub = models.length || v
? t("A request masking would change goes as written to this model, which you set to take requests unmasked, so it answers about the real names. If it fails, the request fails: it is never masked and sent elsewhere")
: t("Only a model you set to take requests unmasked can be picked: in the editor of a provider on this computer or your network, turn on Send requests unmasked");
const r = el("div", "row pref"), who = el("div", "who"), val = el("div", "val");
who.append(el("div", "name", t("Instead of masking, send to")), el("div", "sub", sub));
val.append(b);
r.append(who, val);
r.id = "redactRouteRow";
const after = $("#redactWordsWholeRow");
if (after) after.after(r); else $("#redactList").append(r);
}
// wordList: Masked words and Never mask as a list, one row a value
// (noahhrreion on Discord: many words were hard to keep in one small
// box). A row is edited in place and saved on Enter or on leaving it;
// emptied, or its ×, takes it out. The box under them adds one, or
// several split at commas as the one box was, so what is saved is the
// same list of strings. A redraw keeps the box being typed in, and what
// it holds.
let wordsRedrawing = false;
const splitWords = (v) => v.split(/[,,\n]/).map((w) => w.trim()).filter(Boolean);
function wordList(key, list, ph, label, save) {
const box = el("div", "word-list");
box.dataset.list = key;
let cur = list.map((w) => ({ w }));
const put = (next) => {
if (next.map((o) => o.w).join("\n") === cur.map((o) => o.w).join("\n")) return;
cur = next;
save(next.map((o) => o.w));
};
const keys = (i, done, back) => {
i.onkeydown = (e) => {
e.stopPropagation();
if (e.isComposing || e.keyCode === 229) return;
if (e.key === "Enter") done(true);
else if (e.key === "Escape") { i.value = back; i.blur(); }
};
// a box a redraw takes away saves nothing: wordFocusBack puts it back
i.onblur = () => { if (!wordsRedrawing && i.isConnected) done(false); };
};
for (const o of cur) {
const line = el("div", "word-line"), i = input(o.w, "");
i.className = "words";
i.dataset.word = o.w;
i.setAttribute("aria-label", label);
keys(i, (enter) => {
if (enter) return i.blur();
if (i.value.trim() === o.w) return;
const n = cur.indexOf(o);
if (n >= 0) put([...cur.slice(0, n), ...splitWords(i.value).map((w) => ({ w })), ...cur.slice(n + 1)]);
}, o.w);
const x = el("button", "text danger word-x", "×");
x.type = "button";
x.title = t("Remove {word}", { word: o.w });
x.setAttribute("aria-label", x.title);
// pressed, the box being edited keeps its focus and its words
x.onmousedown = (e) => e.preventDefault();
x.onclick = () => put(cur.filter((c) => c !== o));
line.append(i, x);
box.append(line);
}
const line = el("div", "word-line word-new"), add = input("", ph);
add.className = "words word-add";
add.setAttribute("aria-label", label);
const go = el("button", "text", t("Add"));
go.type = "button";
const take = () => {
const v = splitWords(add.value);
add.value = "";
if (v.length) put([...cur, ...v.map((w) => ({ w }))]);
};
keys(add, (enter) => { if (enter || add.value.trim()) take(); }, "");
go.onmousedown = (e) => e.preventDefault();
go.onclick = () => { take(); add.focus(NO_SCROLL); };
line.append(add, go);
box.append(line);
return box;
}
// wordRow: the row a wordList sits under, its words above and the list
// across the row's width
function wordRow(name, sub, list, ...tools) {
const r = el("div", "row pref word-rows"), who = el("div", "who"), val = el("div", "val");
who.append(el("div", "name", name), el("div", "sub", sub));
val.append(...tools);
r.append(who, val, list);
return r;
}
// the box typed in before a redraw, found again after it
function wordFocusOf() {
const i = document.activeElement;
const box = i?.closest?.(".word-list");
if (!box || i.tagName !== "INPUT") return null;
return { key: box.dataset.list, word: i.dataset.word, value: i.value, a: i.selectionStart, b: i.selectionEnd };
}
function wordFocusBack(f) {
if (!f) return;
const box = document.querySelector(`.word-list[data-list="${f.key}"]`);
const i = [...(box?.querySelectorAll("input.words") || [])].find((i) => i.dataset.word === f.word);
if (!i) return;
i.value = f.value;
i.focus(NO_SCROLL);
try { i.setSelectionRange(f.a, f.b); } catch {}
}
// redactWordsHidden: Masked words drawn as dots until Show, as a key is
// (noahhrreion on Discord: a screen share showed the words kept from
// vendors). Still text boxes, so an IME writes 百度; password boxes only
// where the engine has no text security. Show holds until the window
// reloads.
let redactWordsShown = false;
function redactWordsHidden(r) {
const dots = CSS.supports("-webkit-text-security", "disc");
const eye = el("button", "text redact-eye");
eye.type = "button";
const set = () => {
for (const i of r.querySelectorAll("input.words")) {
if (dots) i.classList.toggle("words-hidden", !redactWordsShown);
else i.type = redactWordsShown ? "text" : "password";
}
eye.textContent = t(redactWordsShown ? "Hide" : "Show");
eye.setAttribute("aria-pressed", String(redactWordsShown));
};
eye.onclick = () => { redactWordsShown = !redactWordsShown; set(); };
set();
r.querySelector(".val").append(eye);
}
{
const draw = renderRedact;
renderRedact = function (s, keep) {
const f = wordFocusOf();
wordsRedrawing = true;
try { draw(s, keep); } finally { wordsRedrawing = false; }
const words = wordRow(t("Masked words"), t("Your own words to keep from vendors, one on each row"),
wordList("words", s.redactWords || [], t("names, codenames, hosts"), t("Masked words"), (redactWords) => savePrefs({ ...keep, redactWords })));
const at = $("#redactWordsAnyCaseRow");
if (at) at.before(words); else $("#redactList").append(words);
redactWordsHidden(words);
const acts = $("#maskActList");
if (acts) acts.append(wordRow(t("Never mask"), t("Values that always go as written, each whole and in any case, one on each row: a team address, a test key"),
wordList("allow", s.redactAllow || [], t("ops@company.com, build-01"), t("Never mask"), (allow) => writingPrefs(api("settings/redact-allow", { allow }))
.then((ns) => { prefs = ns; renderSettings(); status(t("Saved"), "ok", 1500); })
.catch(settingsErr))));
wordFocusBack(f);
if (s.redact || s.redactPersonal || (s.redactWords || []).length) redactRouteRow(s);
};
}