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

+198 -109
View File
@@ -1,7 +1,15 @@
<script setup lang="ts">
import { Plus, Trash2 } from "lucide-vue-next";
import type { NodeForm } from "~/composables/useFilterNode";
import { blankNode, isBlankNode } from "~/composables/useFilterNode";
import { FILTER_SCOPE_EVENTS, blankNode, isBlankNode, nodeKey } from "~/composables/useFilterNode";
import { FILTER_TYPES, FILTER_OPS } from "~/types";
import type { FilterOp, FilterType } from "~/types";
import {
fieldPathOptions,
isArrayFieldPath,
validateFilterValue,
valueOptions,
} from "~/utils/filter-catalog";
defineOptions({ name: "FilterNodeEditor" });
@@ -12,20 +20,80 @@ const props = withDefaults(defineProps<{ node: NodeForm; depth?: number; deletab
const emit = defineEmits<{ (e: "remove"): void }>();
const { t } = useI18n();
const { t, locale } = useI18n();
const { confirm } = useConfirm();
const scopeEvents = inject(
FILTER_SCOPE_EVENTS,
computed((): string[] => []),
);
const NODE_KINDS = ["all", "any"] as const;
const filterTypeOptions = computed(() =>
const typeOptions = computed(() =>
FILTER_TYPES.map((value) => ({ label: t("filter." + value), value })),
);
const filterOpOptions = computed(() =>
const opOptions = computed(() =>
FILTER_OPS.map((value) => ({ label: t("filterOp." + value), value })),
);
const nodeKindOptions = computed(() =>
NODE_KINDS.map((value) => ({ label: t("filterNode." + value), value })),
const kindOptions = computed(() => [
{ label: t("filterNode.all"), value: "all" },
{ label: t("filterNode.any"), value: "any" },
]);
const pathOptions = computed(() => fieldPathOptions(locale.value, scopeEvents.value));
const valueSuggestions = computed(() =>
valueOptions(
locale.value,
props.node.leaf.type,
props.node.leaf.op,
props.node.leaf.path,
scopeEvents.value,
),
);
const isField = computed(() => props.node.leaf.type === "field");
const showOp = computed(() => props.node.leaf.type !== "keyword");
const showValues = computed(
() => props.node.leaf.type === "keyword" || props.node.leaf.op !== "exists",
);
function validateValue(value: string): string | null {
return validateFilterValue(props.node.leaf.type, props.node.leaf.op, value);
}
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";
}
}
function setOp(value: string): void {
if (FILTER_OPS.includes(value as FilterOp)) {
props.node.leaf.op = value as FilterOp;
// `exists` hides the values input: its unadded text must not block saving.
if (!showValues.value) props.node.leaf.draft = "";
}
}
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";
}
}
function setValues(value: string | string[]): void {
props.node.leaf.values = Array.isArray(value) ? value : value ? [value] : [];
}
function setDraft(value: string): void {
props.node.leaf.draft = value;
}
function setKind(value: string): void {
if (value === "all" || value === "any") props.node.kind = value;
}
function addChild(kind: "leaf" | "all" | "any" | "not"): void {
props.node.children.push(blankNode(kind));
}
@@ -40,6 +108,7 @@ function unwrapNot(): void {
}
async function remove(): Promise<void> {
// Text typed but not added yet (the leaf's draft) counts as content too.
if (!isBlankNode(props.node)) {
const confirmed = await confirm({
title:
@@ -53,99 +122,108 @@ async function remove(): Promise<void> {
<template>
<div class="node-editor" :class="{ 'node-nested': depth > 0 }">
<div v-if="node.kind === 'leaf'" class="leaf-row">
<RcSelect v-model="node.leaf.type" class="node-select" :options="filterTypeOptions" />
<RcInput
v-if="node.leaf.type === 'field'"
v-model="node.leaf.path"
class="input node-path"
:placeholder="t('routeEditor.pathPlaceholder')"
/>
<RcSelect
v-if="node.leaf.type !== 'keyword'"
v-model="node.leaf.op"
class="node-select"
:options="filterOpOptions"
:title="t('routeEditor.op')"
/>
<TagInput
v-if="node.leaf.type === 'keyword' || node.leaf.op !== 'exists'"
v-model="node.leaf.values"
class="node-values"
:placeholder="t('routeEditor.valuesPlaceholder')"
/>
<RcCheckbox
v-model="node.leaf.exclude"
class="node-exclude"
:label="t('routeEditor.not')"
:title="t('routeEditor.not')"
/>
<RcButton
v-if="deletable"
type="button"
variant="destructive"
size="icon"
class="icon-btn danger"
:title="t('routeEditor.remove')"
@click="remove"
>
✕
</RcButton>
<div
v-if="node.kind === 'leaf'"
class="leaf-row"
:class="{
'is-field': isField,
'is-keyword': !showOp,
'no-values': !showValues,
'is-negated': node.leaf.exclude,
}"
>
<div class="leaf-type">
<RcSelect
:model-value="node.leaf.type"
:options="typeOptions"
@update:model-value="setType"
/>
</div>
<div v-if="isField" class="leaf-path">
<FilterCombobox
:model-value="node.leaf.path"
:options="pathOptions"
creatable
:placeholder="t('routeEditor.pathPlaceholder')"
:label="t('routeEditor.path')"
@update:model-value="setPath"
/>
</div>
<div v-if="showOp" class="leaf-op">
<RcSelect
:model-value="node.leaf.op"
:options="opOptions"
:title="t('routeEditor.op')"
@update:model-value="setOp"
/>
</div>
<div v-if="showValues" class="leaf-values">
<FilterCombobox
:model-value="node.leaf.values"
:options="valueSuggestions"
multiple
creatable
:validate="validateValue"
:draft="node.leaf.draft"
:placeholder="t('routeEditor.valuesPlaceholder')"
:label="t('routeEditor.values')"
@update:model-value="setValues"
@update:draft="setDraft"
/>
</div>
<div class="leaf-actions">
<RcToggle
:model-value="node.leaf.exclude"
size="sm"
class="w-auto px-2.5 text-xs font-semibold ring-1 ring-inset ring-border-strong"
:title="t('routeEditor.notHint')"
:aria-label="t('routeEditor.notHint')"
@update:model-value="node.leaf.exclude = $event"
>
{{ t("routeEditor.not") }}
</RcToggle>
<RcButton
v-if="deletable"
type="button"
variant="ghost"
color="error"
size="icon"
class="h-8 w-8 text-muted hover:text-bad"
:title="t('routeEditor.removeCondition')"
:aria-label="t('routeEditor.removeCondition')"
@click="remove"
>
<Trash2 :size="15" />
</RcButton>
</div>
</div>
<div v-else-if="node.kind === 'all' || node.kind === 'any'" class="node-group">
<div class="node-group-head">
<RcSelect v-model="node.kind" class="node-combo" :options="nodeKindOptions" />
<div class="node-actions">
<RcButton
type="button"
variant="ghost"
size="xs"
class="node-add"
@click="addChild('leaf')"
>
+ {{ t("routeEditor.addLeaf") }}
</RcButton>
<RcButton
type="button"
variant="ghost"
size="xs"
class="node-add"
@click="addChild('all')"
>
+ {{ t("routeEditor.addGroup") }}
</RcButton>
<RcButton
type="button"
variant="ghost"
size="xs"
class="node-add"
@click="addChild('not')"
>
+ {{ t("routeEditor.addNot") }}
</RcButton>
<RcButton
v-if="deletable"
type="button"
variant="destructive"
size="icon"
class="icon-btn danger"
:title="t('routeEditor.remove')"
@click="remove"
>
✕
</RcButton>
</div>
<RcToggleGroup
:model-value="node.kind"
:options="kindOptions"
class="node-kind w-auto"
@update:model-value="setKind"
/>
<RcButton
v-if="deletable"
type="button"
variant="ghost"
color="error"
size="icon"
class="ml-auto h-8 w-8 text-muted hover:text-bad"
:title="t('routeEditor.removeGroup')"
:aria-label="t('routeEditor.removeGroup')"
@click="remove"
>
<Trash2 :size="15" />
</RcButton>
</div>
<div class="node-children">
<FilterNodeEditor
v-for="(child, i) in node.children"
:key="i"
:key="nodeKey(child)"
:node="child"
:depth="depth + 1"
deletable
@@ -153,27 +231,38 @@ async function remove(): Promise<void> {
/>
<div v-if="node.children.length === 0" class="node-empty">{{ t("filterNode.empty") }}</div>
</div>
<div class="node-add">
<RcButton type="button" variant="ghost" size="xs" @click="addChild('leaf')">
<Plus :size="13" />{{ t("routeEditor.addLeaf") }}
</RcButton>
<RcButton type="button" variant="ghost" size="xs" @click="addChild('all')">
<Plus :size="13" />{{ t("routeEditor.addGroup") }}
</RcButton>
<RcButton type="button" variant="ghost" size="xs" @click="addChild('not')">
<Plus :size="13" />{{ t("routeEditor.addNot") }}
</RcButton>
</div>
</div>
<div v-else class="node-group node-not">
<div class="node-group-head">
<span class="node-combo node-combo-label">{{ t("filterNode.not") }}</span>
<div class="node-actions">
<RcButton type="button" variant="ghost" size="xs" class="node-add" @click="unwrapNot">
{{ t("filterNode.unwrap") }}
</RcButton>
<RcButton
v-if="deletable"
type="button"
variant="destructive"
size="icon"
class="icon-btn danger"
:title="t('routeEditor.remove')"
@click="remove"
>
✕
</RcButton>
</div>
<span class="node-not-label">{{ t("filterNode.not") }}</span>
<RcButton type="button" variant="ghost" size="xs" class="ml-auto" @click="unwrapNot">
{{ t("filterNode.unwrap") }}
</RcButton>
<RcButton
v-if="deletable"
type="button"
variant="ghost"
color="error"
size="icon"
class="h-8 w-8 text-muted hover:text-bad"
:title="t('routeEditor.removeGroup')"
:aria-label="t('routeEditor.removeGroup')"
@click="remove"
>
<Trash2 :size="15" />
</RcButton>
</div>
<div class="node-children">
<FilterNodeEditor v-if="node.child" :node="node.child" :depth="depth + 1" />
+62 -7
View File
@@ -1,16 +1,20 @@
<script setup lang="ts">
import { computed, nextTick, reactive, ref, watch } from "vue";
import { computed, nextTick, provide, reactive, ref, watch } from "vue";
import { Trash2 } from "lucide-vue-next";
import type { Filter, FilterNode, NamedFragment, Route, RouteTarget, RouteTemplate } from "~/types";
import { FRAGMENT_PRESETS, ROUTE_TEMPLATES } from "~/types";
import type { NodeForm } from "~/composables/useFilterNode";
import {
FILTER_SCOPE_EVENTS,
blankLeafForm,
blankNode,
nodeToForm,
nodeFormToRouteFilters,
formToNode,
hasPendingDraft,
scopeEvents,
} from "~/composables/useFilterNode";
import { eventOptions, validateFilterValue } from "~/utils/filter-catalog";
interface TargetForm {
platform: "discord" | "telegram" | "feishu";
@@ -27,7 +31,7 @@ const props = withDefaults(
const emit = defineEmits<{ (e: "close"): void; (e: "save", route: Route): void }>();
const { t } = useI18n();
const { t, locale } = useI18n();
const { confirm } = useConfirm();
const isEdit = computed(() => props.route != null);
@@ -65,6 +69,11 @@ const form = reactive({
});
const root = ref<NodeForm>(blankNode("all"));
provide(
FILTER_SCOPE_EVENTS,
computed(() => scopeEvents(root.value)),
);
const eventSuggestions = computed(() => eventOptions(locale.value));
const { fragments, load: loadFragments, save: saveFragments } = useFragmentsApi();
const fragmentName = ref("");
@@ -75,6 +84,7 @@ const testEvent = ref("");
const testResult = ref<{ matched: boolean; explanation: string } | null>(null);
const testError = ref("");
const testing = ref(false);
const testEventInput = ref<{ hasInvalidDraft: () => boolean } | null>(null);
function applyTemplate(tmpl: RouteTemplate): void {
form.id = tmpl.id;
@@ -99,11 +109,21 @@ async function removeTarget(target: TargetForm): Promise<void> {
if (index >= 0) form.targets.splice(index, 1);
}
function validateEventName(value: string): string | null {
return /^[a-z0-9_]+$/i.test(value) ? null : "valid.eventName";
}
function validateNode(nf: NodeForm): string | null {
if (nf.kind === "leaf") {
if (nf.leaf.type === "field" && !nf.leaf.path.trim()) return t("routeEditor.errPath");
if (nf.leaf.op !== "exists" && nf.leaf.values.every((v) => !v.trim()))
return t("routeEditor.errValues");
const { type, op, path, values } = nf.leaf;
if (type === "field" && !path.trim()) return t("routeEditor.errPath");
if (type === "keyword" || op !== "exists") {
if (values.every((v) => !v.trim())) return t("routeEditor.errValues");
for (const value of values) {
const err = validateFilterValue(type, op, value);
if (err) return t("routeEditor.errInvalidValue", { value: value.trim(), reason: t(err) });
}
}
return null;
}
if (nf.kind === "all" || nf.kind === "any") {
@@ -118,6 +138,12 @@ function validateNode(nf: NodeForm): string | null {
return t("routeEditor.errGroupEmpty");
}
/** Why the filter tree cannot be used as-is (unconfirmed input or an invalid value). */
function filterTreeError(): string | null {
if (hasPendingDraft(root.value)) return t("routeEditor.errDraft");
return validateNode(root.value);
}
function collect(): Route | null {
filterError.value = "";
targetError.value = "";
@@ -184,6 +210,12 @@ function collect(): Route | null {
}
function validated(): Route | null {
if (hasPendingDraft(root.value)) {
targetError.value = "";
formError.value = "";
filterError.value = t("routeEditor.errDraft");
return null;
}
const route = collect();
if (!route) return null;
if (!/^[a-z0-9][a-z0-9-]*$/.test(route.id)) {
@@ -240,6 +272,15 @@ async function runTest(): Promise<void> {
testError.value = t("routeEditor.testInvalidJson");
return;
}
if (testEventInput.value?.hasInvalidDraft()) {
testError.value = t("routeEditor.testEventInvalid");
return;
}
const problem = filterTreeError();
if (problem) {
testError.value = problem;
return;
}
const node = formToNode(root.value);
if (!node) {
testError.value = t("routeEditor.errAddFilter");
@@ -287,6 +328,11 @@ async function saveAsFragment(): Promise<void> {
return;
}
if (!props.groupId) return;
const problem = filterTreeError();
if (problem) {
fragmentError.value = problem;
return;
}
const node = formToNode(root.value);
if (!node) {
fragmentError.value = t("routeEditor.errAddFilter");
@@ -440,8 +486,17 @@ watch(
<section class="editor-section">
<h3 class="editor-section-title">{{ t("routeEditor.testMatch") }}</h3>
<div class="field">
<RcInput v-model="testEvent" :placeholder="t('routeEditor.testEvent')" />
<div class="field test-event">
<FilterCombobox
ref="testEventInput"
:model-value="testEvent"
:options="eventSuggestions"
creatable
:validate="validateEventName"
:placeholder="t('routeEditor.testEvent')"
:label="t('routeEditor.testEvent')"
@update:model-value="testEvent = typeof $event === 'string' ? $event : ''"
/>
</div>
<div class="field">
<RcTextarea
-15
View File
@@ -1,15 +0,0 @@
<script setup lang="ts">
const props = defineProps<{ modelValue: string[]; placeholder?: string }>();
const emit = defineEmits<{ (e: "update:modelValue", value: string[]): void }>();
</script>
<template>
<RcTagInput
:model-value="props.modelValue"
:placeholder="props.placeholder"
separator=","
unique
size="sm"
@update:model-value="emit('update:modelValue', $event)"
/>
</template>