feat(filter-editor): integrate searchable condition inputs

Replace chip-only values with context-aware bilingual inputs and a compact condition grid. Preserve drafts across tree changes, block saves and tests until drafts are resolved, and keep keyword filters free of hidden operators.
This commit is contained in:
翅膀 committed 2026-10-17 06:29:41 +08:00
1 parent 2ebce8495d
commit d693f70612
10 files changed
+557 -190

No files matched your search

+66 -6
View File
@@ -1,11 +1,22 @@
import { toRaw } from "vue";
import type { ComputedRef, InjectionKey } from "vue";
import type { Filter, FilterNode, FilterOp, FilterType } from "~/types";
/** Exact event names the edited route filters on (see `scopeEvents`). */
export const FILTER_SCOPE_EVENTS: InjectionKey<ComputedRef<string[]>> = Symbol("filter-scope");
export interface LeafForm {
type: FilterType;
path: string;
op: FilterOp;
values: string[];
exclude: boolean;
/**
* Text typed into the values input but not added yet (editor-only, never
* saved). It lives on the leaf so it follows the condition when the input
* remounts (a NOT unwrapped, the root wrapped by an inserted fragment).
*/
draft: string;
}
export type NodeKind = "leaf" | "all" | "any" | "not";
@@ -17,8 +28,22 @@ export interface NodeForm {
child: NodeForm | null;
}
const nodeKeys = new WeakMap<object, number>();
let lastNodeKey = 0;
/** Stable render key of a node form (list indexes shift when siblings are removed). */
export function nodeKey(nf: NodeForm): number {
const raw = toRaw(nf);
let key = nodeKeys.get(raw);
if (key === undefined) {
key = ++lastNodeKey;
nodeKeys.set(raw, key);
}
return key;
}
export function blankLeafForm(): LeafForm {
return { type: "event", path: "", op: "eq", values: [], exclude: false };
return { type: "event", path: "", op: "eq", values: [], exclude: false, draft: "" };
}
export function blankNode(kind: NodeKind): NodeForm {
@@ -42,6 +67,7 @@ function leafToForm(f: Filter): LeafForm {
op: f.op ?? "eq",
values: matchToValues(f.match),
exclude: !!f.exclude,
draft: "",
};
}
@@ -60,12 +86,14 @@ export function nodeToForm(node: FilterNode): NodeForm {
export function formToLeaf(lf: LeafForm): Filter | null {
const values = lf.values.map((v) => v.trim()).filter((v) => v.length > 0);
if (lf.op !== "exists" && values.length === 0) return null;
// Keyword filters ignore the operator on the server, so never store one.
const op: FilterOp = lf.type === "keyword" ? "eq" : lf.op;
if (op !== "exists" && values.length === 0) return null;
if (lf.type === "field" && lf.path.trim().length === 0) return null;
const filter: Filter = { type: lf.type };
if (lf.type === "field") filter.path = lf.path.trim();
if (lf.op !== "eq") filter.op = lf.op;
if (lf.op !== "exists") filter.match = values.length === 1 ? values[0] : values;
if (op !== "eq") filter.op = op;
if (op !== "exists") filter.match = values.length === 1 ? values[0] : values;
if (lf.exclude) filter.exclude = true;
return filter;
}
@@ -116,6 +144,29 @@ export function nodeFormToRouteFilters(nf: NodeForm): { filters: Filter[]; ast?:
return { filters: [], ast: ast ?? undefined };
}
/**
* Exact event names a route positively filters on (negated conditions,
* `not` groups, globs and regexes are skipped) — the context the filter
* editor uses to rank action and field suggestions.
*/
export function scopeEvents(nf: NodeForm): string[] {
const out = new Set<string>();
const walk = (n: NodeForm): void => {
if (n.kind === "leaf") {
const { type, op, exclude, values } = n.leaf;
if (type !== "event" || exclude || (op !== "eq" && op !== "in")) return;
for (const v of values) {
const value = v.trim();
if (/^[a-z0-9_]+$/i.test(value)) out.add(value.toLowerCase());
}
return;
}
if (n.kind === "all" || n.kind === "any") n.children.forEach(walk);
};
walk(nf);
return [...out];
}
/**
* Whether a node holds nothing worth confirming before it is removed: an
* untouched condition (default operator, no path, no values, not negated) or
@@ -124,13 +175,22 @@ export function nodeFormToRouteFilters(nf: NodeForm): { filters: Filter[]; ast?:
*/
export function isBlankNode(nf: NodeForm): boolean {
if (nf.kind === "leaf") {
const { exclude, op, path, values } = nf.leaf;
return !exclude && op === "eq" && !path.trim() && values.every((v) => !v.trim());
const { draft, exclude, op, path, values } = nf.leaf;
return (
!exclude && op === "eq" && !path.trim() && values.every((v) => !v.trim()) && !draft.trim()
);
}
if (nf.kind === "all" || nf.kind === "any") return nf.children.every(isBlankNode);
return !nf.child || isBlankNode(nf.child);
}
/** Whether a condition of the tree still holds typed text that was not added. */
export function hasPendingDraft(nf: NodeForm): boolean {
if (nf.kind === "leaf") return !!nf.leaf.draft.trim();
if (nf.kind === "all" || nf.kind === "any") return nf.children.some(hasPendingDraft);
return !!nf.child && hasPendingDraft(nf.child);
}
export function collectEvents(node: FilterNode): string[] {
const out: string[] = [];
const walk = (n: FilterNode): void => {
+16
View File
@@ -261,7 +261,14 @@ const en: Dict = {
"routeEditor.addGroup": "Add group",
"routeEditor.addNot": "Add not",
"routeEditor.remove": "Remove",
"routeEditor.removeCondition": "Delete condition",
"routeEditor.removeGroup": "Delete condition group",
"routeEditor.removeTarget": "Remove target",
"routeEditor.path": "Field path",
"routeEditor.values": "Values",
"routeEditor.notHint": "Negate this condition",
"routeEditor.errDraft": "Add (press Enter) or clear the filter value still being typed",
"routeEditor.errInvalidValue": 'Invalid value "{value}": {reason}',
"routeEditor.op": "Operator",
"routeEditor.pathPlaceholder": "payload.path",
"routeEditor.valuesPlaceholder": "Type a value and press Enter",
@@ -272,6 +279,7 @@ const en: Dict = {
"routeEditor.testMatched": "Matches",
"routeEditor.testNotMatched": "Does not match",
"routeEditor.testInvalidJson": "Invalid JSON",
"routeEditor.testEventInvalid": "Fix or clear the event type first",
"routeEditor.fragments": "Fragments",
"routeEditor.fragmentsPresets": "Presets",
"routeEditor.fragmentsEmpty": "No fragments yet",
@@ -685,7 +693,14 @@ const zh: Dict = {
"routeEditor.addGroup": "添加分组",
"routeEditor.addNot": "添加非",
"routeEditor.remove": "删除",
"routeEditor.removeCondition": "删除条件",
"routeEditor.removeGroup": "删除条件组",
"routeEditor.removeTarget": "删除目标",
"routeEditor.path": "字段路径",
"routeEditor.values": "匹配值",
"routeEditor.notHint": "对该条件取反",
"routeEditor.errDraft": "请先确认(回车)或清除尚未添加的过滤值",
"routeEditor.errInvalidValue": "无效的值“{value}”:{reason}",
"routeEditor.op": "操作符",
"routeEditor.pathPlaceholder": "payload.path",
"routeEditor.valuesPlaceholder": "输入值后回车",
@@ -696,6 +711,7 @@ const zh: Dict = {
"routeEditor.testMatched": "匹配",
"routeEditor.testNotMatched": "不匹配",
"routeEditor.testInvalidJson": "无效的 JSON",
"routeEditor.testEventInvalid": "请先修正或清空事件类型",
"routeEditor.fragments": "过滤器片段",
"routeEditor.fragmentsPresets": "预设",
"routeEditor.fragmentsEmpty": "暂无片段",