From b78d104728c6af280e653d8e92291619b9cc0003 Mon Sep 17 00:00:00 2001 From: funjia <13429193897@163.com> Date: Sun, 4 Oct 2026 01:14:04 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=99=9A=E7=BA=BF=E8=BE=B9=E6=A1=86?= =?UTF-8?q?=E6=94=B9=E4=B8=BA=E3=80=8C=E7=BA=B8=E4=B8=8A=E6=9C=89=E4=B8=9C?= =?UTF-8?q?=E8=A5=BF=E6=89=8D=E7=94=BB=E3=80=8D=EF=BC=88=E5=8E=9F=20Web=20?= =?UTF-8?q?=E7=89=88=20+=20=E6=8F=92=E4=BB=B6=E7=89=88=E9=83=BD=E6=94=B9?= =?UTF-8?q?=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 用户口径:**没内容时不要打印虚线,有内容时要打印**。 原来只有「空格」满足这条,另有两种状态违反: - 双击「隐藏(不打印)」的格子:纸上什么都没有,虚线照印; - 模板选了但内容全空(最典型是 CustomTag 把所有行的文字都清空): 纸上什么都没有,虚线照印 —— 在纸上看它和空格一模一样。 改成把规则统一成一句话:**边框 = 这一格纸上真的会印出东西**。 - 空格 / 双击隐藏:由格子状态直接判定(LabelCell 新增 blank-on-paper 类,由页面算好传进来 —— 只有页面知道模板的内容规则); - 模板内容全空:交给模板自己判。两块代码各自在模板注册表里声明 `hasContent`(插件在 templates/registry.ts,原 Web 版在 Main.vue 的 templates 数组里)。 为什么不能让模板「什么都不声明」靠通用扫描:CustomTag 的行是 `{ text, size, bold, color }`,通用扫描扫到 `color: '#000000'` 就认为有内容了, 于是文字全清空的标签仍被判定成有内容。所以 Error / CustomTag 显式声明 hasContent;未声明的模板退回通用兜底 —— 兜底刻意偏「宽松」:多印一个空框 看得见、好排查;把有内容的标签判成空的,用户会以为东西丢了。 另外规格类模板标记 `alwaysPrints`:它们带左侧固定信息区(店铺名 / 电话 / 内置二维码图片),那部分不来自 config,所以「字段被清空」的规格标签仍有内容、 边框必须留 —— 否则用户清空字段后会发现边框莫名其妙没了。 验证(Playwright + 本机 Edge,真实打印管线): - 插件版 5 项边框断言 + 上一轮的 13 项打印断言,全过; - 原 Web 版 5 项边框断言全过; - 两版都生成 PDF 用浏览器 PDF 视图渲染确认纸面:只有真正有内容的格子 留下边框与内容,隐藏格 / 内容全空格在纸上是空白。 - 插件产物已升版本发布为 funjia.print-tag@1.0.2。 --- plugin/plugin.config.json | 2 +- plugin/src/components/LabelCell.vue | 20 +++++++ plugin/src/pages/PrintTagPage.vue | 42 ++++++++++++++- plugin/src/templates/registry.ts | 73 +++++++++++++++++++++++-- src/components/LabelCell.vue | 23 +++++++- src/components/Main.vue | 84 ++++++++++++++++++++++++----- 6 files changed, 225 insertions(+), 19 deletions(-) diff --git a/plugin/plugin.config.json b/plugin/plugin.config.json index 9811b9a..1d64898 100644 --- a/plugin/plugin.config.json +++ b/plugin/plugin.config.json @@ -1,7 +1,7 @@ { "id": "funjia.print-tag", "name": "标签打印", - "version": "1.0.1", + "version": "1.0.2", "kind": "iife", "artifact": "PrintTagBundle.iife.js", "style": "PrintTagBundle.css", diff --git a/plugin/src/components/LabelCell.vue b/plugin/src/components/LabelCell.vue index 82b79fc..0bd52ca 100644 --- a/plugin/src/components/LabelCell.vue +++ b/plugin/src/components/LabelCell.vue @@ -6,6 +6,7 @@ 'pt-cell--placeholder': !item?.name, 'pt-cell--selected': selected, 'pt-cell--hidden': !!item?.hidden, + 'pt-cell--blank-on-paper': blankOnPaper, }" :title="!item?.name ? '空格:单击选中后可编辑' : item.hidden ? '已隐藏(双击恢复显示)' : '双击隐藏内容'" @click="emit('select')" @@ -32,6 +33,13 @@ defineProps({ index: { type: Number, required: true }, item: { type: Object, default: null }, selected: { type: Boolean, default: false }, + /** + * 这一格**打印出来纸上不会有任何东西**(空格 / 双击隐藏 / 模板选了但内容全空)。 + * + * 由页面算好传进来,而不是这里自己推:只有页面知道模板的内容规则 + * (见 `PrintTagPage.vue` 的 `isBlankOnPaper`)。LabelCell 保持「只管外观」。 + */ + blankOnPaper: { type: Boolean, default: false }, }); const emit = defineEmits(["select", "toggle-hide"]); @@ -125,6 +133,18 @@ const emit = defineEmits(["select", "toggle-hide"]); /* ====== 打印 ====== */ @media print { + /** + * 虚线边框的含义是「**这一格纸上有东西**」。所以: + * + * - 空格(`.pt-cell--placeholder`)不画 —— 下面第一条; + * - 双击隐藏、以及「模板选了但内容全空」(`.pt-cell--blank-on-paper`)同样不画 —— + * 这两种在纸上看和空格完全一样,印个空框出来纯属干扰。 + * - 真正有内容的格子保留边框。 + */ + .pt-cell--blank-on-paper { + border-color: transparent !important; + } + /** * ⚠️ 选择器必须写成 `.x, .x *` 两段 —— 序号角标里的数字在一个 `` 里, * 而 `visibility` 是**可被子元素覆盖的继承属性**:只隐父 div 的话, diff --git a/plugin/src/pages/PrintTagPage.vue b/plugin/src/pages/PrintTagPage.vue index ce3b3f7..90471c6 100644 --- a/plugin/src/pages/PrintTagPage.vue +++ b/plugin/src/pages/PrintTagPage.vue @@ -207,6 +207,7 @@ :index="i" :item="slot" :selected="selectedIndex === i" + :blank-on-paper="isBlankOnPaper(slot)" @select="onCellSelect(i)" @toggle-hide="toggleHidden(i)" > @@ -234,7 +235,14 @@ import Input from "../components/Input.vue"; import Select from "../components/Select.vue"; import DynamicForm from "../components/DynamicForm.vue"; import LabelCell from "../components/LabelCell.vue"; -import { TEMPLATE_COMPONENTS, TEMPLATE_FIELDS, TEMPLATE_OPTIONS } from "../templates/registry"; +import { + TEMPLATE_HAS_CONTENT, + TEMPLATE_COMPONENTS, + TEMPLATE_FIELDS, + TEMPLATE_MAP, + TEMPLATE_OPTIONS, + hasVisibleContent, +} from "../templates/registry"; /* ================================================================== */ /* 宿主上下文 */ @@ -613,6 +621,33 @@ const onIssueOneSn = async () => { } }; +/** + * 这一格**打印出来纸上会不会有东西**。 + * + * 用途只有一个:决定要不要画那圈虚线边框。规则统一成一句话 —— + * **边框代表「这一格纸上有东西」**,所以纸上什么都没有的格子不该有边框: + * + * | 情况 | 纸上 | 边框 | + * | --- | --- | --- | + * | 空格(没选模板) | 无 | 不画 | + * | 双击隐藏 | 无 | 不画 | + * | 模板选了但内容全空(如 CustomTag 所有行都空) | 无 | 不画 | + * | 有内容 | 有 | 画 | + * + * 前两种是「按状态」判定的(`name` / `hidden`),第三种要看模板: + * **规格类模板即使字段全空,左侧固定信息区(店铺名 / 电话 / 内置二维码)照印**, + * 所以它们直接短路成「有内容」—— 否则用户把字段清空后会发现边框莫名其妙没了。 + */ +const isBlankOnPaper = (slot: Slot): boolean => { + if (!slot.name) return true; // 空格 + if (slot.hidden) return true; // 双击隐藏:内容不打印,纸上看和空格一样 + const tpl = TEMPLATE_MAP.get(slot.name); + if (tpl?.alwaysPrints) return false; // 规格类一定有左侧固定信息区 + // 模板自己说了算(见 TagTemplate.hasContent),没声明则走通用兜底 + const hasContent = TEMPLATE_HAS_CONTENT.get(slot.name) ?? hasVisibleContent; + return !hasContent(slot.config ?? {}); +}; + /* ================================================================== */ /* 单格编辑 */ /* ================================================================== */ @@ -752,6 +787,11 @@ const applyPrintStyles = () => { .pt-cell--placeholder, .pt-cell--placeholder *, .pt-hide, .pt-hide * { visibility: hidden !important; } + /* 虚线边框 = 「这一格纸上有东西」。纸上什么都没有的格子(空格 / 双击隐藏 / + 模板内容全空)不画边框 —— 纸上看它和空格一模一样,印个空框只是干扰。 + 判定见 isBlankOnPaper()。 */ + .pt-cell--blank-on-paper { border-color: transparent !important; } + /* 标签纸钉在纸边偏移处:@page 边距已经是 0,位置只能自己给 */ .pt-paper { position: absolute; diff --git a/plugin/src/templates/registry.ts b/plugin/src/templates/registry.ts index 15c9d43..5f8e61b 100644 --- a/plugin/src/templates/registry.ts +++ b/plugin/src/templates/registry.ts @@ -48,8 +48,35 @@ export interface TagTemplate { formData: Record; /** 仅 SpecTag 使用:右侧规格行按此顺序取值 */ fields?: string[]; + /** + * 这个模板**即使字段全空,纸上也一定会印出东西**。 + * + * 规格类模板走 `TagShell` 时会画左侧固定信息区(店铺名 / 电话 / 内置二维码图片), + * 那部分不来自 `config`,所以「config 是空的」并不等于「纸上没东西」。 + * + * 这个标记只服务一件事:**决定要不要给格子印虚线边框** + * (规则见 `PrintTagPage.vue` 的 `isBlankOnPaper`)。漏标会让 + * 「字段清空的规格标签」被当成空格、连边框一起丢掉。 + */ + alwaysPrints?: boolean; + /** + * 这份 config 在纸上会不会留下东西。**省略时退回通用的 `hasVisibleContent`**。 + * + * 为什么不能全靠通用判定:`CustomTag` 的行是 `{ text, size, bold, color }`, + * 通用判定扫到 `color: "#000000"` 就认为「有内容」了 —— 于是文字全清空的 + * 标签仍被判成有内容、纸上印出一个空框。`size` 之类同理。 + * **只有模板自己知道哪些字段是"给人看的"**,所以让它自己说。 + * + * 新加模板时不写这个也不算错(会走通用判定),但那种判定偏宽松 —— + * 宁可多印一个框也不该把有内容的标签判成空的,这是刻意选的偏向。 + */ + hasContent?: (config: Record) => boolean; } +/** 把若干字段当作「给人看的文字」来判断有没有内容 */ +const anyTextFilled = (keys: string[]) => (config: Record) => + keys.some((key) => String(config?.[key] ?? "").trim() !== ""); + /* ------------------------------------------------------------------ */ /* 复用的选项表 */ /* ------------------------------------------------------------------ */ @@ -96,8 +123,10 @@ const OS_OPTIONS = [ ]; /** 台式机(带独显)三兄弟:CPU / 内存 / 硬盘 / 显卡 */ -const desktopWithGpu = (cpu: string, memory: string, hardDisk: string, graphicsCard: string): Pick => ({ +const desktopWithGpu = (cpu: string, memory: string, hardDisk: string, graphicsCard: string): Pick => ({ fields: ["cpu", "memory", "hardDisk", "graphicsCard"], + // 走 TagShell 且带左侧固定信息区 → 字段全空也仍有墨 + alwaysPrints: true, formSchema: { formItems: [ { type: "input", label: "CPU", key: "cpu", required: true }, @@ -110,8 +139,9 @@ const desktopWithGpu = (cpu: string, memory: string, hardDisk: string, graphicsC }); /** 台式机(核显):CPU / 内存 / 硬盘 / 操作系统 */ -const desktopWithOs = (cpu: string, memory: string, hardDisk: string, system: string): Pick => ({ +const desktopWithOs = (cpu: string, memory: string, hardDisk: string, system: string): Pick => ({ fields: ["cpu", "memory", "hardDisk", "system"], + alwaysPrints: true, formSchema: { formItems: [ { type: "input", label: "CPU", key: "cpu", required: true }, @@ -124,8 +154,9 @@ const desktopWithOs = (cpu: string, memory: string, hardDisk: string, system: st }); /** 一体机:标题 / CPU / 内存 / 硬盘 */ -const allInOne = (name: string, cpu: string, memory: string, hardDisk: string): Pick => ({ +const allInOne = (name: string, cpu: string, memory: string, hardDisk: string): Pick => ({ fields: ["name", "cpu", "memory", "hardDisk"], + alwaysPrints: true, formSchema: { formItems: [ { type: "input", label: "标题", key: "name", required: true }, @@ -186,6 +217,7 @@ export const TEMPLATES: TagTemplate[] = [ label: "HardDisk", component: SpecTag, fields: ["system", "partition", "hardDisk", "brand"], + alwaysPrints: true, formSchema: { formItems: [ { type: "select", label: "系统", key: "system", options: [ @@ -221,6 +253,8 @@ export const TEMPLATES: TagTemplate[] = [ { label: "Error", component: ErrorTag, + // 只显示标题 / 说明 / 时间,三个都空就是纸上没东西 + hasContent: anyTextFilled(["title", "reason", "time"]), formSchema: { formItems: [ { type: "select", label: "标题", key: "title", options: [ @@ -238,6 +272,11 @@ export const TEMPLATES: TagTemplate[] = [ { label: "CustomTag", component: CustomTag, + // 只有「文字非空的那些行」会显示,颜色 / 字号不算内容 + hasContent: (config) => + ((config?.lines ?? []) as Array<{ text?: string }>).some( + (line) => String(line?.text ?? "").trim() !== "" + ), formSchema: { formItems: [ { @@ -274,8 +313,36 @@ export const TEMPLATE_OPTIONS = TEMPLATES.map(({ label, formSchema, formData }) formData, })); +/** 模板标识 → 模板定义(要查 `alwaysPrints` / `hasContent` 这类元信息时用它) */ +export const TEMPLATE_MAP = new Map(TEMPLATES.map((t) => [t.label, t])); + /** 模板标识 → 渲染组件 */ export const TEMPLATE_COMPONENTS = new Map(TEMPLATES.map((t) => [t.label, t.component])); /** 模板标识 → 右侧字段顺序 */ export const TEMPLATE_FIELDS = new Map(TEMPLATES.map((t) => [t.label, t.fields ?? []])); + +/** 模板标识 → 判定「这份 config 在纸上有没有东西」的函数(已含通用兜底) */ +export const TEMPLATE_HAS_CONTENT = new Map) => boolean>( + TEMPLATES.map((t) => [t.label, t.hasContent ?? hasVisibleContent]) +); + +/** + * 通用兜底:扫一遍 config,只要有**非空白的字符串**就认为有内容,数组逐项下钻。 + * + * 只在模板没声明 `hasContent` 时用。它偏宽松 —— 像 `CustomTag` 的 + * `color: "#000000"`、`size: 4` 这种「不是给人看的」字段会被算成内容, + * 所以有意义的模板请自己声明 `hasContent`。 + * + * 偏向「宽松」是**故意**的:多印一个空框看得见、好排查; + * 把有内容的标签判成空的,用户会以为东西丢了。 + */ +export function hasVisibleContent(value: unknown): boolean { + if (typeof value === "string") return value.trim() !== ""; + if (Array.isArray(value)) return value.some((item) => hasVisibleContent(item)); + if (value && typeof value === "object") { + return Object.values(value as Record).some((item) => hasVisibleContent(item)); + } + // 数字 / 布尔不需要「填」,一律不算内容,否则 `size: 4` 会被误判 + return false; +} diff --git a/src/components/LabelCell.vue b/src/components/LabelCell.vue index 8d2377f..7a9c4f1 100644 --- a/src/components/LabelCell.vue +++ b/src/components/LabelCell.vue @@ -4,7 +4,8 @@ border: !!item?.name, placeholder: !item?.name, selected, - hidden: !!item?.hidden + hidden: !!item?.hidden, + 'blank-on-paper': blankOnPaper }" :title="!item?.name ? '空格:单击选中后可编辑' : (item.hidden ? '已隐藏(双击恢复显示)' : '双击隐藏内容')" @click="emit('select')" @@ -22,7 +23,14 @@ defineProps({ index: { type: Number, required: true }, item: { type: Object, default: null }, - selected: { type: Boolean, default: false } + selected: { type: Boolean, default: false }, + /** + * 这一格**打印出来纸上不会有任何东西**(空格 / 双击隐藏 / 模板选了但内容全空)。 + * + * 由页面算好传进来,而不是这里自己推:只有页面知道模板的内容规则 + * (见 Main.vue 的 isBlankOnPaper)。本组件保持「只管外观」。 + */ + blankOnPaper: { type: Boolean, default: false } }); const emit = defineEmits(['select', 'toggle-hide']); @@ -163,6 +171,17 @@ const emit = defineEmits(['select', 'toggle-hide']); } @media print { + /** + * 虚线边框的含义是「**这一格纸上有东西**」: + * - 空格(.placeholder)不画; + * - 双击隐藏、以及「模板选了但内容全空」(.blank-on-paper)同样不画 —— + * 这两种在纸上看和空格一模一样,印个空框出来纯属干扰; + * - 真正有内容的格子保留边框。 + */ + .blank-on-paper { + border-color: transparent !important; + } + /** * 序号角标与空格引导均不打印。 * diff --git a/src/components/Main.vue b/src/components/Main.vue index 524f475..10d53f8 100644 --- a/src/components/Main.vue +++ b/src/components/Main.vue @@ -89,7 +89,8 @@
+ :selected="selectedIndex === i" :blank-on-paper="isBlankOnPaper(slot)" + @select="onCellSelect(i)" @toggle-hide="toggleHidden(i)">
@@ -123,72 +124,89 @@ import LabelCell from './LabelCell.vue'; const clone = (obj) => JSON.parse(JSON.stringify(obj)); // 模板注册表:新增模板只需在数组中加一项 +// +// `alwaysPrints` / `hasContent` 只服务一件事:**决定打印时给不给格子画虚线边框** +// (规则见下面 isBlankOnPaper 的注释)。新增模板时建议一并写清楚,漏写会走通用兜底。 const templates = [ { label: 'I5_10400F_16G_256G_1060', component: I5_10400F_16G_256G_1060, formSchema: formSchema_I510400F, - formData: initialValues_I510400F + formData: initialValues_I510400F, + // 带左侧固定信息区(店铺名 / 电话 / 二维码),字段全空也仍有墨 + alwaysPrints: true }, { label: 'I5_10400F_16G_256G_2060', component: I5_10400F_16G_512G_2060, formSchema: formSchema_I510400F2060, - formData: initialValues_I510400F2060 + formData: initialValues_I510400F2060, + alwaysPrints: true }, { label: 'I5_9400F_16G_256G_1060', component: I5_9400F_16G_256G_1060, formSchema: formSchema_I59400F, - formData: initialValues_I59400F + formData: initialValues_I59400F, + alwaysPrints: true }, { label: 'I5_4590_8G_120G', component: I5_4590_8G_120G, formSchema: formSchema_I54590, - formData: initialValues_I54590 + formData: initialValues_I54590, + alwaysPrints: true }, { label: 'I3_4130_8G_120G', component: I3_4130_8G_120G, formSchema: formSchema_I34130, - formData: initialValues_I34130 + formData: initialValues_I34130, + alwaysPrints: true }, { label: 'G3260_8G_120G', component: G3260_8G_120G, formSchema: formSchema_G3260, - formData: initialValues_G3260 + formData: initialValues_G3260, + alwaysPrints: true }, { label: '24寸一体机_I5_3代_8G_256G', component: All_in_one_24_8G_256G, formSchema: formSchema_24AllInOne, - formData: initialValues_24AllInOne + formData: initialValues_24AllInOne, + alwaysPrints: true }, { label: '22寸一体机_4G_120G', component: All_in_one_22_4G_120G, formSchema: formSchema_22AllInOne, - formData: initialValues_22AllInOne + formData: initialValues_22AllInOne, + alwaysPrints: true }, { label: 'HardDisk', component: HardDisk, formSchema: formSchema_HardDisk, - formData: initialValues_HardDisk + formData: initialValues_HardDisk, + alwaysPrints: true }, { label: 'Error', component: Error, formSchema: formSchema_Error, - formData: initialValues_Error + formData: initialValues_Error, + // 只显示标题 / 说明 / 时间,三个都空就是纸上没东西 + hasContent: (c) => [c?.title, c?.reason, c?.time].some((v) => String(v ?? '').trim() !== '') }, { label: 'CustomTag', component: CustomTag, formSchema: formSchema_CustomTag, - formData: initialValues_CustomTag + formData: initialValues_CustomTag, + // 只有「文字非空的那些行」会显示,颜色 / 字号不算内容 + hasContent: (c) => (c?.lines || []).some((line) => String(line?.text ?? '').trim() !== '') } ]; @@ -197,6 +215,48 @@ const people = reactive( templates.map(({ label, formSchema, formData }) => ({ label, formSchema, formData })) ); const templateMap = new Map(templates.map(({ label, component }) => [label, component])); +const templateByLabel = new Map(templates.map((t) => [t.label, t])); + +/** + * 通用兜底:扫一遍 config,只要有**非空白的字符串**就认为有内容,数组逐项下钻。 + * + * 只在模板没声明 `hasContent` 时用。它偏宽松 —— 像 CustomTag 的 `color: '#000000'` + * 会被算成内容,所以有意义的模板请自己声明 `hasContent`。 + * 偏向「宽松」是故意的:多印一个空框看得见、好排查;把有内容的标签判成空的, + * 用户会以为东西丢了。 + */ +const hasVisibleContent = (value) => { + if (typeof value === 'string') return value.trim() !== ''; + if (Array.isArray(value)) return value.some(hasVisibleContent); + if (value && typeof value === 'object') return Object.values(value).some(hasVisibleContent); + return false; // 数字 / 布尔不需要「填」,否则 size: 4 会被误判 +}; + +/** + * 这一格**打印出来纸上会不会有东西**。 + * + * 用途只有一个:决定要不要画那圈虚线边框。规则一句话 —— + * **边框代表「这一格纸上有东西」**,所以纸上什么都没有的格子不该有边框: + * + * | 情况 | 纸上 | 边框 | + * | --- | --- | --- | + * | 空格(没选模板) | 无 | 不画 | + * | 双击隐藏 | 无 | 不画 | + * | 模板选了但内容全空(如自定义文字所有行都空) | 无 | 不画 | + * | 有内容 | 有 | 画 | + * + * 前两种按状态就能判(name / hidden),第三种要看模板:规格类模板**即使字段全空, + * 左侧固定信息区(店铺名 / 电话 / 内置二维码)照印**,所以直接短路成「有内容」—— + * 否则用户把字段清空后会发现边框莫名其妙没了。 + */ +const isBlankOnPaper = (slot) => { + if (!slot?.name) return true; // 空格 + if (slot.hidden) return true; // 双击隐藏:内容不打印,纸上看和空格一样 + const tpl = templateByLabel.get(slot.name); + if (tpl?.alwaysPrints) return false; + const hasContent = tpl?.hasContent || hasVisibleContent; + return !hasContent(slot.config || {}); +}; // ---------- 固定 44 个格子(每格可单独编辑) ---------- const TOTAL = 44;