Files
WebHooker/app/composables/useFilterNode.ts
T
翅膀 d693f70612 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.
2026-10-17 06:29:41 +08:00

254 lines
8.1 KiB
TypeScript

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";
export interface NodeForm {
kind: NodeKind;
leaf: LeafForm;
children: 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, draft: "" };
}
export function blankNode(kind: NodeKind): NodeForm {
return {
kind,
leaf: blankLeafForm(),
children: kind === "all" || kind === "any" ? [blankNode("leaf")] : [],
child: kind === "not" ? blankNode("leaf") : null,
};
}
function matchToValues(match: string | string[] | undefined): string[] {
if (match === undefined || match === null) return [];
return Array.isArray(match) ? [...match] : [match];
}
function leafToForm(f: Filter): LeafForm {
return {
type: f.type,
path: f.path ?? "",
op: f.op ?? "eq",
values: matchToValues(f.match),
exclude: !!f.exclude,
draft: "",
};
}
export function nodeToForm(node: FilterNode): NodeForm {
if ("all" in node) {
return { kind: "all", leaf: blankLeafForm(), children: node.all.map(nodeToForm), child: null };
}
if ("any" in node) {
return { kind: "any", leaf: blankLeafForm(), children: node.any.map(nodeToForm), child: null };
}
if ("not" in node) {
return { kind: "not", leaf: blankLeafForm(), children: [], child: nodeToForm(node.not) };
}
return { kind: "leaf", leaf: leafToForm(node), children: [], child: null };
}
export function formToLeaf(lf: LeafForm): Filter | null {
const values = lf.values.map((v) => v.trim()).filter((v) => v.length > 0);
// 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 (op !== "eq") filter.op = op;
if (op !== "exists") filter.match = values.length === 1 ? values[0] : values;
if (lf.exclude) filter.exclude = true;
return filter;
}
export function formToNode(nf: NodeForm): FilterNode | null {
if (nf.kind === "leaf") return formToLeaf(nf.leaf);
if (nf.kind === "all" || nf.kind === "any") {
const children = nf.children.map(formToNode).filter((c): c is FilterNode => c !== null);
if (children.length === 0) return null;
return nf.kind === "all" ? { all: children } : { any: children };
}
if (nf.kind === "not") {
const child = nf.child ? formToNode(nf.child) : null;
if (!child) return null;
return { not: child };
}
return null;
}
export function isTrivialNode(nf: NodeForm): boolean {
if (nf.kind === "leaf") {
return nf.leaf.type !== "field" && nf.leaf.op === "eq" && !nf.leaf.exclude;
}
if (nf.kind === "all") return nf.children.every(isTrivialNode);
return false;
}
export function flattenLeaves(nf: NodeForm): Filter[] {
const out: Filter[] = [];
const walk = (n: NodeForm): void => {
if (n.kind === "leaf") {
const f = formToLeaf(n.leaf);
if (f) out.push(f);
return;
}
if (n.kind === "all" || n.kind === "any") n.children.forEach(walk);
else if (n.kind === "not" && n.child) walk(n.child);
};
walk(nf);
return out;
}
export function nodeFormToRouteFilters(nf: NodeForm): { filters: Filter[]; ast?: FilterNode } {
if (isTrivialNode(nf)) {
return { filters: flattenLeaves(nf) };
}
const ast = formToNode(nf);
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
* groups made only of those. A changed operator counts as content, since
* e.g. `exists` is a complete condition without values.
*/
export function isBlankNode(nf: NodeForm): boolean {
if (nf.kind === "leaf") {
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 => {
if ("all" in n) {
n.all.forEach(walk);
return;
}
if ("any" in n) {
n.any.forEach(walk);
return;
}
if ("not" in n) {
walk(n.not);
return;
}
if (n.type === "event") {
const m = n.match;
if (Array.isArray(m)) out.push(...m);
else if (typeof m === "string" && m) out.push(m);
}
};
walk(node);
return out;
}
export function describeLeaf(f: Filter, t: (key: string) => string): string {
const label = f.type === "field" ? (f.path ?? f.type) : t("filter." + f.type);
const op = f.op ?? "eq";
const match = f.match;
const values = match === undefined ? [] : Array.isArray(match) ? match : [match];
const value = values.map((v) => JSON.stringify(v)).join(` ${t("filterNode.or")} `) || "\u2205";
let base: string;
if (f.type === "keyword") {
base = `${label} ${t("filterNode.matches")} ${value}`;
} else if (op === "exists") {
base = `${label} ${t("filterOp.exists")}`;
} else if (op === "eq") {
base = `${label} ${t("filterNode.is")} ${value}`;
} else {
base = `${label} ${t("filterOp." + op)} ${value}`;
}
return f.exclude ? `${t("filterNode.not")} (${base})` : base;
}
export function describeNode(node: FilterNode, t: (key: string) => string): string {
if ("all" in node) {
return node.all
.map((c) => describeNode(c, t))
.filter((s) => s.length)
.join(` ${t("filterNode.and")} `);
}
if ("any" in node) {
const parts = node.any.map((c) => describeNode(c, t)).filter((s) => s.length);
return parts.length ? `(${parts.join(` ${t("filterNode.or")} `)})` : "";
}
if ("not" in node) {
return `${t("filterNode.not")} (${describeNode(node.not, t)})`;
}
return describeLeaf(node, t);
}