feat(filters): split pasted values without breaking regexes

Accept multiple values separated by commas or lines while retaining commas inside wrapped expressions and raw regex patterns. Pasting replaces the selected range and keeps rejected values as an explicit draft.
This commit is contained in:
翅膀 committed 2026-10-17 06:58:12 +08:00
1 parent d693f70612
commit 00387c3afa
6 files changed
+152 -2

No files matched your search

+44
View File
@@ -47,6 +47,7 @@
@keydown="onKeydown"
@focus="focused = true"
@blur="onBlur"
@paste="onPaste"
/>
<ComboboxTrigger class="filter-combo-trigger" tabindex="-1" :aria-label="label">
<ChevronDown :size="14" />
@@ -126,6 +127,7 @@ import {
ComboboxViewport,
} from "reka-ui";
import type { ComboOption } from "~/utils/filter-catalog";
import { commaEndsValue, splitFilterValues } from "~/utils/filter-catalog";
/**
* Searchable dropdown for the filter editor. Options carry the stored English
@@ -153,6 +155,11 @@ const props = withDefaults(
* the owner can refuse to save it and restore it after a remount.
*/
draft?: string;
/**
* Multi-value fields: a comma adds the typed value. Off for raw regular
* expressions, where a comma belongs to the pattern (`{1,3}`).
*/
splitOnComma?: boolean;
}>(),
{
multiple: false,
@@ -162,6 +169,7 @@ const props = withDefaults(
label: "",
mono: true,
draft: "",
splitOnComma: true,
},
);
@@ -407,10 +415,46 @@ function onKeydown(event: KeyboardEvent): void {
}
return;
}
if (event.key === "," && props.multiple && props.creatable) {
const [start, end] = selectionOf(event);
if (commaEndsValue(draft.value, start, end, props.splitOnComma)) {
event.preventDefault();
commitDraft();
}
return;
}
if (event.key === "Backspace" && props.multiple && !draft.value && values.value.length) {
event.preventDefault();
removeAt(values.value.length - 1);
}
}
/** Selected range of the draft input (the caret when nothing is selected). */
function selectionOf(event: Event): [number, number] {
const input = event.target as HTMLInputElement;
const start = input.selectionStart ?? draft.value.length;
return [start, input.selectionEnd ?? start];
}
function onPaste(event: ClipboardEvent): void {
if (!props.multiple || !props.creatable) return;
const text = event.clipboardData?.getData("text") ?? "";
const [start, end] = selectionOf(event);
// The pasted text replaces the selection, exactly as a native paste would.
const combined = draft.value.slice(0, start) + text + draft.value.slice(end);
const parts = splitFilterValues(combined, props.splitOnComma);
// Nothing to separate: the browser pastes into the draft as usual.
if (parts.length <= 1 && (parts[0] ?? "") === combined.trim()) return;
event.preventDefault();
const next = [...values.value];
const rejected: string[] = [];
for (const part of parts) {
if (props.validate?.(part)) rejected.push(part);
else if (!next.includes(part)) next.push(part);
}
if (next.length !== values.value.length) setValues(next);
draft.value = rejected.join(", ");
typing.value = rejected.length > 0;
commitError.value = rejected.length ? (props.validate?.(rejected[0]!) ?? null) : null;
}
</script>
+1
View File
@@ -164,6 +164,7 @@ async function remove(): Promise<void> {
multiple
creatable
:validate="validateValue"
:split-on-comma="node.leaf.op !== 'regex'"
:draft="node.leaf.draft"
:placeholder="t('routeEditor.valuesPlaceholder')"
:label="t('routeEditor.values')"
+54
View File
@@ -1161,6 +1161,60 @@ function isWrappedRegex(value: string): boolean {
return value.length >= 2 && value.startsWith("/") && value.endsWith("/");
}
/**
* Length of the `/regex/` that `text` starts with: up to the first closing
* `/` followed by a comma or the end of the text, or -1 while it is still
* open. Escaped slashes (`\/`) and slashes in a character class (`[/,]`)
* never close it.
*/
function wrappedRegexLength(text: string): number {
if (!text.startsWith("/")) return -1;
let inClass = false;
for (let i = 1; i < text.length; i++) {
const char = text[i];
if (char === "\\") i++;
else if (inClass) inClass = char !== "]";
else if (char === "[") inClass = true;
else if (char === "/" && /^\s*(?:,|$)/.test(text.slice(i + 1))) return i + 1;
}
return -1;
}
/**
* Split typed or pasted text into match values: one per line and, unless
* `commas` is off (the raw `regex` operator), one per comma. A `/regex/`
* keeps its own commas (`/v{1,3}/`, `/[/,]/`).
*/
export function splitFilterValues(text: string, commas = true): string[] {
const out: string[] = [];
for (const line of text.split(/\r?\n/)) {
if (!commas) {
out.push(line);
continue;
}
let rest = line.trimStart();
while (rest) {
const regexLength = wrappedRegexLength(rest);
const token = regexLength > 0 ? rest.slice(0, regexLength) : rest.split(",", 1)[0]!;
out.push(token);
rest = rest.slice(token.length).replace(/^\s*,/, "").trimStart();
}
}
return out.map((v) => v.trim()).filter(Boolean);
}
/**
* Whether a comma typed over `text[start, end)` ends the value being typed —
* only when typed at its end (no selection) and outside an open `/regex/`.
* Anywhere else the comma is part of the text; raw patterns (`commas` off)
* keep every comma.
*/
export function commaEndsValue(text: string, start: number, end: number, commas = true): boolean {
if (!commas || start !== end || text.slice(end).trim()) return false;
const value = text.slice(0, start).trim();
return !value.startsWith("/") || wrappedRegexLength(value) === value.length;
}
function isGlob(value: string): boolean {
return value.includes("*") || value.includes("?");
}