feat: Refactor template handling and brand management
- Removed schema_json from article templates and related queries. - Updated brand service to include website field in requests and responses. - Simplified template wizard view by eliminating unused schema fields and related logic. - Added tests for ark text format handling. - Created migrations for dropping schema_json and adding website to brands.
This commit is contained in:
@@ -14,9 +14,8 @@ import type {
|
||||
TemplateAssistCompetitor,
|
||||
TemplateAnalyzeResult,
|
||||
TemplateOutlineNode,
|
||||
TemplateSchemaField,
|
||||
} from "@geo/shared-types";
|
||||
import { computed, reactive, ref, watch } from "vue";
|
||||
import { computed, ref, watch } from "vue";
|
||||
import { useI18n } from "vue-i18n";
|
||||
import { useRoute, useRouter } from "vue-router";
|
||||
|
||||
@@ -154,7 +153,6 @@ let outlineNodeSeed = 0;
|
||||
|
||||
const currentStep = ref(0);
|
||||
const articleLocale = ref("zh-CN");
|
||||
const fieldValues = reactive<Record<string, JsonValue>>({});
|
||||
const currentArticleId = ref<number | null>(null);
|
||||
const restoringDraft = ref(false);
|
||||
const savingDraft = ref(false);
|
||||
@@ -259,7 +257,6 @@ const templateCardConfig = computed<TemplateCardConfig>(() =>
|
||||
);
|
||||
const wizardConfig = computed<TemplateWizardConfig | null>(() => templateCardConfig.value.wizard ?? null);
|
||||
const isResearchReportTemplate = computed(() => templateDetail.value?.template_key === "research_report");
|
||||
const wizardManagedFields = computed(() => wizardConfig.value?.managed_fields ?? []);
|
||||
const templateMeta = computed(() => {
|
||||
const fallback = getTemplateMeta(templateDetail.value?.template_key ?? "unknown");
|
||||
return {
|
||||
@@ -272,20 +269,6 @@ const selectedBrand = computed(() =>
|
||||
brandsQuery.data.value?.find((brand) => brand.id === selectedBrandId.value) ?? null,
|
||||
);
|
||||
|
||||
const schemaFields = computed<TemplateSchemaField[]>(() => {
|
||||
return templateDetailQuery.data.value?.schema_json?.fields ?? [];
|
||||
});
|
||||
|
||||
const visibleSchemaFields = computed(() =>
|
||||
schemaFields.value.filter((field) =>
|
||||
!isWizardManagedField(
|
||||
field.name,
|
||||
templateDetail.value?.template_key,
|
||||
wizardManagedFields.value,
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
const keywordOptions = computed(() =>
|
||||
(keywordsQuery.data.value ?? []).map((item) => ({
|
||||
label: item.name,
|
||||
@@ -378,17 +361,7 @@ const structureStepCopy = computed<TemplateStepConfig>(() => wizardConfig.value?
|
||||
const generateStepCopy = computed<TemplateStepConfig>(() => wizardConfig.value?.steps?.generate ?? {});
|
||||
const brandCardCopy = computed<TemplateCardCopy>(() => wizardConfig.value?.basic?.cards?.brand ?? {});
|
||||
const keywordCardCopy = computed<TemplateCardCopy>(() => wizardConfig.value?.basic?.cards?.keywords ?? {});
|
||||
const fieldsCardCopy = computed<TemplateCardCopy>(() => wizardConfig.value?.basic?.cards?.template_fields ?? {});
|
||||
const competitorsCardCopy = computed<TemplateCardCopy>(() => wizardConfig.value?.basic?.cards?.competitors ?? {});
|
||||
const hasTemplateFieldsCardConfig = computed(
|
||||
() => !isResearchReportTemplate.value && Boolean(wizardConfig.value?.basic?.cards?.template_fields),
|
||||
);
|
||||
const editableSchemaFields = computed<TemplateSchemaField[]>(() =>
|
||||
hasTemplateFieldsCardConfig.value ? visibleSchemaFields.value : [],
|
||||
);
|
||||
const showTemplateFieldsCard = computed(
|
||||
() => editableSchemaFields.value.length > 0 && fieldsCardCopy.value.visible !== false,
|
||||
);
|
||||
const titleCardCopy = computed<TemplateCardCopy>(() => wizardConfig.value?.structure?.cards?.choose_title ?? {});
|
||||
const outlineCardCopy = computed<TemplateCardCopy>(() => wizardConfig.value?.structure?.cards?.outline ?? {});
|
||||
const previewCardCopy = computed<TemplateCardCopy>(() => wizardConfig.value?.structure?.cards?.preview ?? {});
|
||||
@@ -436,7 +409,7 @@ const assistHeading = computed(() =>
|
||||
: t("templates.wizard.assist.outlineTitle"),
|
||||
);
|
||||
|
||||
function resetWizardState(fields: TemplateSchemaField[], detail: NonNullable<typeof templateDetail.value>): void {
|
||||
function resetWizardState(detail: NonNullable<typeof templateDetail.value>): void {
|
||||
currentArticleId.value = draftArticleId.value;
|
||||
currentStep.value = 0;
|
||||
articleLocale.value = "zh-CN";
|
||||
@@ -455,20 +428,6 @@ function resetWizardState(fields: TemplateSchemaField[], detail: NonNullable<typ
|
||||
customOutlineInput.value = "";
|
||||
generatedOutline.value = [];
|
||||
|
||||
for (const key of Object.keys(fieldValues)) {
|
||||
delete fieldValues[key];
|
||||
}
|
||||
|
||||
for (const field of fields) {
|
||||
if (field.default !== undefined) {
|
||||
fieldValues[field.name] = field.default;
|
||||
} else if (field.type === "number") {
|
||||
fieldValues[field.name] = null;
|
||||
} else {
|
||||
fieldValues[field.name] = "";
|
||||
}
|
||||
}
|
||||
|
||||
const defaults = buildOutlineOptions(
|
||||
templateCardConfig.value.wizard?.outline,
|
||||
detail.template_key,
|
||||
@@ -527,26 +486,17 @@ function applyDraftArticleState(detail: ArticleDetail): void {
|
||||
outlineSections.value = stringListFromUnknown(draftState.outline_sections) ?? outlineSections.value;
|
||||
generatedOutline.value = decorateOutlineNodes(parseOutlineNodes(draftState.generated_outline));
|
||||
|
||||
const savedFields = asRecord(draftState.field_values);
|
||||
if (savedFields) {
|
||||
for (const field of schemaFields.value) {
|
||||
if (Object.prototype.hasOwnProperty.call(savedFields, field.name)) {
|
||||
fieldValues[field.name] = savedFields[field.name] as JsonValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
restoringDraft.value = false;
|
||||
}
|
||||
|
||||
watch(
|
||||
[enabled, schemaFields, templateDetail, () => draftArticleQuery.data.value],
|
||||
([isOpen, fields, detail, draftArticle]) => {
|
||||
[enabled, templateDetail, () => draftArticleQuery.data.value],
|
||||
([isOpen, detail, draftArticle]) => {
|
||||
if (!isOpen || !detail) {
|
||||
return;
|
||||
}
|
||||
|
||||
resetWizardState(fields, detail);
|
||||
resetWizardState(detail);
|
||||
if (draftArticle && (draftArticle.generate_status === "draft" || draftArticle.generate_status === "failed")) {
|
||||
applyDraftArticleState(draftArticle);
|
||||
}
|
||||
@@ -599,6 +549,9 @@ watch(selectedBrandId, async (brandId) => {
|
||||
if (brand && !brandName.value.trim()) {
|
||||
brandName.value = brand.name;
|
||||
}
|
||||
if (brand?.website && !officialWebsite.value.trim()) {
|
||||
officialWebsite.value = brand.website;
|
||||
}
|
||||
if (brand?.description && !brandSummary.value.trim()) {
|
||||
brandSummary.value = brand.description;
|
||||
}
|
||||
@@ -633,37 +586,12 @@ function nextOutlineNodeKey(): string {
|
||||
return `outline-${outlineNodeSeed}`;
|
||||
}
|
||||
|
||||
function isWizardManagedField(name: string, templateKey?: string, managedFields: string[] = []): boolean {
|
||||
if (managedFields.includes(name)) {
|
||||
return true;
|
||||
}
|
||||
if (name === "count" || name === "brand" || name === "keyword") {
|
||||
return true;
|
||||
}
|
||||
if (templateKey === "top_x_article" && name === "topic") {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
function renderFieldPlaceholder(field: TemplateSchemaField): string {
|
||||
return field.placeholder || `${t("common.inputPlease")}${field.label}`;
|
||||
}
|
||||
|
||||
function validateBasicInfo(): boolean {
|
||||
if (!normalizedBrandName.value) {
|
||||
message.warning(t("templates.wizard.messages.missingBrand"));
|
||||
return false;
|
||||
}
|
||||
|
||||
for (const field of editableSchemaFields.value) {
|
||||
const value = fieldValues[field.name];
|
||||
if (field.required && (value === null || value === "" || value === undefined)) {
|
||||
message.warning(t("templates.wizard.messages.requiredField", { field: field.label }));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -992,6 +920,7 @@ async function ensureBrandForLibrary(): Promise<number> {
|
||||
|
||||
const created = await brandsApi.create({
|
||||
name: normalizedBrandName.value,
|
||||
website: officialWebsite.value.trim() || null,
|
||||
description: brandSummary.value.trim() || null,
|
||||
});
|
||||
selectedBrandId.value = created.id;
|
||||
@@ -1015,13 +944,6 @@ function buildCompetitorPayload(): TemplateAssistCompetitor[] {
|
||||
|
||||
function buildAssistInputParams(): Record<string, JsonValue> {
|
||||
const payload: Record<string, JsonValue> = {};
|
||||
for (const field of schemaFields.value) {
|
||||
const value = fieldValues[field.name];
|
||||
if (value !== null && value !== "" && value !== undefined) {
|
||||
payload[field.name] = value;
|
||||
}
|
||||
}
|
||||
|
||||
const derivedInputs = wizardConfig.value?.derived_inputs ?? {};
|
||||
for (const [key, config] of Object.entries(derivedInputs)) {
|
||||
const value = resolveDerivedInputValue(key, config);
|
||||
@@ -1052,7 +974,6 @@ function buildPayload(): Record<string, JsonValue> {
|
||||
}))
|
||||
: undefined;
|
||||
const payload: Record<string, JsonValue | undefined> = {
|
||||
...fieldValues,
|
||||
locale: articleLocale.value,
|
||||
title: finalTitle.value || undefined,
|
||||
outline_sections: selectedOutlineLabels.value,
|
||||
@@ -1080,20 +1001,6 @@ function buildPayload(): Record<string, JsonValue> {
|
||||
payload[key] = derivedValue;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
const competitorCount = competitorPayload.length;
|
||||
if (hasSchemaField("brand")) {
|
||||
payload.brand = normalizedBrandName.value;
|
||||
}
|
||||
if (hasSchemaField("keyword") && primaryKeyword.value) {
|
||||
payload.keyword = primaryKeyword.value;
|
||||
}
|
||||
if (hasSchemaField("topic") && !stringValue(fieldValues.topic)) {
|
||||
payload.topic = primaryKeyword.value || normalizedBrandName.value;
|
||||
}
|
||||
if (hasSchemaField("count")) {
|
||||
payload.count = competitorCount > 0 ? competitorCount : defaultFieldNumber("count", 5);
|
||||
}
|
||||
}
|
||||
|
||||
return Object.fromEntries(
|
||||
@@ -1105,7 +1012,6 @@ function buildDraftState(): Record<string, JsonValue> {
|
||||
return {
|
||||
current_step: currentStep.value,
|
||||
locale: articleLocale.value,
|
||||
field_values: { ...fieldValues },
|
||||
selected_brand_id: selectedBrandId.value,
|
||||
brand_name: brandName.value.trim(),
|
||||
official_website: officialWebsite.value.trim(),
|
||||
@@ -1138,23 +1044,6 @@ function buildDraftState(): Record<string, JsonValue> {
|
||||
};
|
||||
}
|
||||
|
||||
function hasSchemaField(name: string): boolean {
|
||||
return schemaFields.value.some((field) => field.name === name);
|
||||
}
|
||||
|
||||
function defaultFieldNumber(name: string, fallback: number): number {
|
||||
const field = schemaFields.value.find((item) => item.name === name);
|
||||
if (!field) {
|
||||
return fallback;
|
||||
}
|
||||
const numeric = Number(field.default);
|
||||
return Number.isFinite(numeric) && numeric > 0 ? numeric : fallback;
|
||||
}
|
||||
|
||||
function stringValue(value: JsonValue | undefined): string {
|
||||
return typeof value === "string" ? value.trim() : "";
|
||||
}
|
||||
|
||||
function handleGenerate(): void {
|
||||
if (!validateBasicInfo() || !validateStructure() || !validateGeneratedOutline()) {
|
||||
return;
|
||||
@@ -1814,7 +1703,7 @@ function resolveDerivedInputValue(
|
||||
if (typeof config.fallback_number === "number") {
|
||||
return config.fallback_number;
|
||||
}
|
||||
return hasSchemaField(fieldName) ? defaultFieldNumber(fieldName, 0) || undefined : undefined;
|
||||
return undefined;
|
||||
}
|
||||
default:
|
||||
return undefined;
|
||||
@@ -2207,48 +2096,6 @@ function onStructureDragEnd(): void {
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div v-if="showTemplateFieldsCard" class="wizard-card">
|
||||
<div class="wizard-card__header">
|
||||
<div>
|
||||
<h3>{{ fieldsCardCopy.title || t("templates.wizard.sections.templateFields") }}</h3>
|
||||
<p>{{ fieldsCardCopy.hint || t("templates.wizard.hints.templateFields") }}</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="field-grid">
|
||||
<div v-for="field in editableSchemaFields" :key="field.name" class="field-item">
|
||||
<label :class="{ 'required-asterisk': field.required }">{{ field.label }}</label>
|
||||
<a-input
|
||||
v-if="field.type === 'text'"
|
||||
v-model:value="fieldValues[field.name]"
|
||||
:placeholder="renderFieldPlaceholder(field)"
|
||||
/>
|
||||
<a-input-number
|
||||
v-else-if="field.type === 'number'"
|
||||
v-model:value="fieldValues[field.name]"
|
||||
:placeholder="renderFieldPlaceholder(field)"
|
||||
style="width: 100%"
|
||||
:min="1"
|
||||
/>
|
||||
<a-select
|
||||
v-else-if="field.type === 'select'"
|
||||
v-model:value="fieldValues[field.name]"
|
||||
:options="(field.options || []).map((option) =>
|
||||
typeof option === 'object'
|
||||
? option
|
||||
: { label: String(option), value: option }
|
||||
)"
|
||||
:placeholder="renderFieldPlaceholder(field)"
|
||||
/>
|
||||
<a-input
|
||||
v-else
|
||||
v-model:value="fieldValues[field.name]"
|
||||
:placeholder="renderFieldPlaceholder(field)"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div v-if="showCompetitorsCard" class="wizard-card">
|
||||
<div class="wizard-card__header">
|
||||
<div>
|
||||
|
||||
Reference in New Issue
Block a user