v0.37.1
This commit is contained in:
1 parent
78dbb31487
commit
f48843e934
83 files changed
+7304
-1203
No files matched your search
@@ -135,7 +135,7 @@ window.rustProxyChoicePicker = (() => {
|
||||
remove.addEventListener("click", () => { const box = checks.get(id); box.checked = false; box.dispatchEvent(new Event("change", { bubbles: true })); });
|
||||
tag.append(text);
|
||||
if (index > 0 && options.markFirst && options.makeFirst) {
|
||||
const promote = el("button", "★", "acl-picker-promote"); promote.type = "button"; promote.title = t(options.makeFirst); promote.setAttribute("aria-label", `${t(options.makeFirst)}: ${item?.title || id}`);
|
||||
const promote = el("button", "★", "acl-picker-promote"); promote.type = "button"; promote.dataset.uiIconName = "none"; promote.title = t(options.makeFirst); promote.setAttribute("aria-label", `${t(options.makeFirst)}: ${item?.title || id}`);
|
||||
promote.addEventListener("click", () => { order.splice(order.indexOf(id), 1); order.unshift(id); sync(); root.dispatchEvent(new Event("change", { bubbles: true })); });
|
||||
tag.append(promote);
|
||||
}
|
||||
@@ -170,3 +170,73 @@ window.rustProxyChoicePicker = (() => {
|
||||
}
|
||||
return { create, read: root => root?.read?.() || [] };
|
||||
})();
|
||||
|
||||
window.rustProxyChipInput = (() => {
|
||||
const CIDR = /^(?:(?:25[0-5]|2[0-4]\d|1?\d?\d)(?:\.(?:25[0-5]|2[0-4]\d|1?\d?\d)){3}(?:\/(?:3[0-2]|[12]?\d))?|[0-9a-fA-F:]*:[0-9a-fA-F:.]*(?:\/(?:12[0-8]|1[01]\d|[1-9]?\d))?)$/;
|
||||
function create(options = {}) {
|
||||
const box = el("div", undefined, `host-chip-editor ${options.className || ""}`.trim()), input = document.createElement("input");
|
||||
input.type = "text"; input.maxLength = options.maxLength || 128; input.placeholder = options.placeholder || ""; input.spellcheck = false;
|
||||
if (options.label) input.setAttribute("aria-label", options.label);
|
||||
const values = [], limit = options.limit || 64;
|
||||
const changed = () => box.dispatchEvent(new Event("change", { bubbles: true }));
|
||||
const normalize = raw => { const value = String(raw || "").trim(); if (!value) return null; if (options.validate) { const result = options.validate(value); if (result === false) throw fieldError(input, options.invalidMessage || t("invalidValue")); if (typeof result === "string") return result; } return value; };
|
||||
const sync = () => {
|
||||
box.querySelectorAll(".host-chip").forEach(node => node.remove());
|
||||
for (const value of values) {
|
||||
const chip = el("span", undefined, "host-chip"); chip.append(el("span", value));
|
||||
const remove = el("button", "×"); remove.type = "button"; remove.setAttribute("aria-label", `${t("remove")} ${value}`);
|
||||
remove.addEventListener("click", () => { values.splice(values.indexOf(value), 1); sync(); changed(); });
|
||||
chip.append(remove); box.insertBefore(chip, input);
|
||||
}
|
||||
};
|
||||
const add = (raw, notify = true) => { const value = normalize(raw); if (!value || values.includes(value)) return; if (values.length >= limit) throw fieldError(input, options.limitMessage || t("aclPickerLimit")); values.push(value); sync(); if (notify) changed(); };
|
||||
const consume = raw => { for (const part of String(raw).split(/[\s,;]+/).filter(Boolean)) add(part); input.value = ""; };
|
||||
const guarded = raw => { try { consume(raw); } catch (error) { toast(error.message, true); } };
|
||||
input.addEventListener("keydown", event => { if (event.key === "Enter" || event.key === ",") { event.preventDefault(); guarded(input.value); } });
|
||||
input.addEventListener("blur", () => { if (input.value.trim()) guarded(input.value); });
|
||||
input.addEventListener("paste", event => { const value = event.clipboardData?.getData("text") || ""; if (!/[\s,;]/.test(value.trim())) return; event.preventDefault(); guarded(value); });
|
||||
box.append(input);
|
||||
for (const value of options.values || []) { try { add(value, false); } catch { } }
|
||||
box.read = () => { if (input.value.trim()) consume(input.value); return [...values]; };
|
||||
box.set = list => { values.length = 0; for (const value of list || []) { try { add(value, false); } catch { } } sync(); };
|
||||
box.input = input;
|
||||
return box;
|
||||
}
|
||||
const validCidr = value => CIDR.test(value);
|
||||
return { create, validCidr };
|
||||
})();
|
||||
|
||||
window.rustProxyCountryPicker = (() => {
|
||||
const CODES = "AD AE AF AG AI AL AM AO AQ AR AS AT AU AW AX AZ BA BB BD BE BF BG BH BI BJ BL BM BN BO BQ BR BS BT BV BW BY BZ CA CC CD CF CG CH CI CK CL CM CN CO CR CU CV CW CX CY CZ DE DJ DK DM DO DZ EC EE EG EH ER ES ET FI FJ FK FM FO FR GA GB GD GE GF GG GH GI GL GM GN GP GQ GR GS GT GU GW GY HK HM HN HR HT HU ID IE IL IM IN IO IQ IR IS IT JE JM JO JP KE KG KH KI KM KN KP KR KW KY KZ LA LB LC LI LK LR LS LT LU LV LY MA MC MD ME MF MG MH MK ML MM MN MO MP MQ MR MS MT MU MV MW MX MY MZ NA NC NE NF NG NI NL NO NP NR NU NZ OM PA PE PF PG PH PK PL PM PN PR PS PT PW PY QA RE RO RS RU RW SA SB SC SD SE SG SH SI SJ SK SL SM SN SO SR SS ST SV SX SY SZ TC TD TF TG TH TJ TK TL TM TN TO TR TT TV TW TZ UA UG UM US UY UZ VA VC VE VG VI VN VU WF WS XK YE YT ZA ZM ZW".split(" ");
|
||||
const nameOf = code => { try { return new Intl.DisplayNames([document.documentElement.lang || "en"], { type: "region" }).of(code) || code; } catch { return code; } };
|
||||
const parse = text => [...new Set(String(text || "").split(/[\s,;]+/).map(value => value.trim().toUpperCase()).filter(Boolean))];
|
||||
function attach(textarea, options = {}) {
|
||||
if (!textarea || textarea.dataset.countryPicker) return null;
|
||||
textarea.dataset.countryPicker = "1"; textarea.hidden = true;
|
||||
const host = el("div", undefined, "country-picker-host"); textarea.after(host);
|
||||
let picker = null, writing = false;
|
||||
const build = () => {
|
||||
const items = CODES.map(code => ({ id: code, title: `${code} · ${nameOf(code)}`, search: nameOf(code) }));
|
||||
picker = window.rustProxyChoicePicker.create({
|
||||
className: "country-picker", selected: parse(textarea.value), items, limit: options.limit || 250,
|
||||
labels: { choose: "countryPickerChoose", selected: "countryPickerSelected", none: "countryPickerNone", search: "countryPickerSearch", addHint: "countryPickerAddHint", empty: "countryPickerEmpty" },
|
||||
custom: raw => {
|
||||
const text = String(raw || "").trim(); if (!text) return null;
|
||||
const code = text.toUpperCase();
|
||||
if (/^[A-Z]{2}$/.test(code)) return { id: code, title: `${code} · ${nameOf(code)}` };
|
||||
const query = text.toLocaleLowerCase(), matches = CODES.filter(item => nameOf(item).toLocaleLowerCase().includes(query));
|
||||
const exact = matches.find(item => nameOf(item).toLocaleLowerCase() === query) || (matches.length === 1 ? matches[0] : null);
|
||||
return exact ? { id: exact, title: `${exact} · ${nameOf(exact)}` } : null;
|
||||
},
|
||||
});
|
||||
picker.addEventListener("choicechange", event => { if (writing) return; const value = (event.detail || []).join("\n"); if (value === textarea.value) return; textarea.value = value; textarea.dispatchEvent(new Event("input", { bubbles: true })); });
|
||||
host.replaceChildren(picker);
|
||||
picker.setDisabled(textarea.disabled);
|
||||
};
|
||||
const refresh = () => { writing = true; try { build(); } finally { writing = false; } };
|
||||
refresh();
|
||||
new MutationObserver(() => picker?.setDisabled(textarea.disabled)).observe(textarea, { attributes: true, attributeFilter: ["disabled"] });
|
||||
return { refresh, host };
|
||||
}
|
||||
return { attach, codes: CODES, nameOf };
|
||||
})();
|
||||
Reference in new issue
Block a user