fix(filters): preserve regex meaning when changing operators

Swap wrapped and raw regex sources only when both compile identically after saving. Leave literal, glob, multiline and Unicode-sensitive comparisons unchanged rather than inventing non-equivalent conversions.
This commit is contained in:
翅膀 committed 2026-10-17 07:47:03 +08:00
1 parent 2717ab49dc
commit 54b2944dd4
5 files changed
+129 -8

No files matched your search

+11 -3
View File
@@ -5,6 +5,7 @@ import { FILTER_SCOPE_EVENTS, blankNode, isBlankNode, nodeKey } from "~/composab
import { FILTER_TYPES, FILTER_OPS } from "~/types";
import type { FilterOp, FilterType } from "~/types";
import {
convertValuesForOp,
fieldPathOptions,
isArrayFieldPath,
validateFilterValue,
@@ -58,17 +59,24 @@ function validateValue(value: string): string | null {
return validateFilterValue(props.node.leaf.type, props.node.leaf.op, value);
}
/** Switch the operator, swapping `/x/` ↔ `x` when moving to or from `regex`. */
function changeOp(next: FilterOp): void {
const { leaf } = props.node;
leaf.values = convertValuesForOp(leaf.values, leaf.op, next);
leaf.op = next;
}
function setType(value: string): void {
if ((FILTER_TYPES as readonly string[]).includes(value)) {
props.node.leaf.type = value as FilterType;
// Keyword filters have no operator; a hidden `exists` would drop their values.
if (value === "keyword") props.node.leaf.op = "eq";
if (value === "keyword") changeOp("eq");
}
}
function setOp(value: string): void {
if (FILTER_OPS.includes(value as FilterOp)) {
props.node.leaf.op = value as FilterOp;
changeOp(value as FilterOp);
// `exists` hides the values input: its unadded text must not block saving.
if (!showValues.value) props.node.leaf.draft = "";
}
@@ -78,7 +86,7 @@ function setPath(value: string | string[]): void {
props.node.leaf.path = typeof value === "string" ? value : "";
// Paths ending on an array compare the whole array; `contains` matches an element.
if (isArrayFieldPath(props.node.leaf.path) && props.node.leaf.op === "eq") {
props.node.leaf.op = "contains";
changeOp("contains");
}
}