diff --git a/plugin/README.md b/plugin/README.md index 432f9ec..8295283 100644 --- a/plugin/README.md +++ b/plugin/README.md @@ -5,7 +5,8 @@ - 插件 id:`funjia.print-tag` - 产物:`dist/PrintTagBundle.iife.js` + `dist/PrintTagBundle.css`(IIFE,自注册) -- 上架位置:`funjia-plugin-repository` 插件仓库的 `repository/plugins/funjia.print-tag/1.0.0/` +- 上架位置:`funjia-plugin-repository` 插件仓库的 `repository/plugins/funjia.print-tag/<版本>/` + (版本号在 `plugin.config.json` 里改,当前见该文件) 仓库根目录那份 `src/` 是**原独立 Web 版**,两者并存、互不影响。 diff --git a/plugin/plugin.config.json b/plugin/plugin.config.json index 0bd094f..9811b9a 100644 --- a/plugin/plugin.config.json +++ b/plugin/plugin.config.json @@ -1,7 +1,7 @@ { "id": "funjia.print-tag", "name": "标签打印", - "version": "1.0.0", + "version": "1.0.1", "kind": "iife", "artifact": "PrintTagBundle.iife.js", "style": "PrintTagBundle.css", diff --git a/plugin/src/components/LabelCell.vue b/plugin/src/components/LabelCell.vue index 89a19c7..82b79fc 100644 --- a/plugin/src/components/LabelCell.vue +++ b/plugin/src/components/LabelCell.vue @@ -125,9 +125,19 @@ const emit = defineEmits(["select", "toggle-hide"]); /* ====== 打印 ====== */ @media print { - /* 序号角标与空格引导均不打印 */ + /** + * ⚠️ 选择器必须写成 `.x, .x *` 两段 —— 序号角标里的数字在一个 `` 里, + * 而 `visibility` 是**可被子元素覆盖的继承属性**:只隐父 div 的话, + * 任何「让标签纸整体可见」的规则(`.pt-paper * { visibility: visible }`) + * 会直接命中那个 span 把它又显出来,序号就印在纸上了。 + * + * 页面里的打印样式(`PrintTagPage.vue` 的 applyPrintStyles)也有同一组规则, + * 那份是给「按 Ctrl+P」等不经过插件按钮的路径兜底的;两份都要留。 + */ .pt-cell__idx, - .pt-cell--placeholder { + .pt-cell__idx *, + .pt-cell--placeholder, + .pt-cell--placeholder * { visibility: hidden !important; border-color: transparent !important; background: transparent !important; @@ -142,6 +152,8 @@ const emit = defineEmits(["select", "toggle-hide"]); background: transparent !important; } + /* 双击隐藏的格子:内容不打印,但仍要占住格子位置 */ + .pt-hide, .pt-hide * { visibility: hidden !important; } diff --git a/plugin/src/pages/PrintTagPage.vue b/plugin/src/pages/PrintTagPage.vue index 8f18d46..ce3b3f7 100644 --- a/plugin/src/pages/PrintTagPage.vue +++ b/plugin/src/pages/PrintTagPage.vue @@ -696,20 +696,70 @@ let printStyle: HTMLStyleElement | null = null; * 原因:`body * { visibility: hidden }` 这种「只留标签纸」的规则如果常驻, * 用户去打印宿主里**任何别的页面**都会是一张白纸 —— 插件 CSS 是全局注入的, * 它分不清「现在打印的是不是我的页面」。临时注入把作用范围钉死在这一次打印上。 + * + * ## 三处必须这么写,改之前先看完 + * + * **① `@page { margin: 0 }` 是「不要页眉页脚」的唯一开关。** + * + * 浏览器会在**页边距那块空白**里画自己的页眉页脚(日期时间 + 标题 / URL + 页码), + * 这是它的打印设置,CSS 里没有 `header: none` 这种东西。把边距设成 0, + * 那块空白没了,页眉页脚自然也就不画了 —— 这是 Chromium/Edge 上唯一可靠的做法。 + * 代价是标签纸在纸上的位置不能再靠 `@page margin` 给,改由 `.pt-paper` 的 + * `top` / `left` 控制:物理结果和原来一模一样(边距设置就是「离纸边的距离」)。 + * + * **② 「不该打印的东西」必须连后代一起隐,而且要写在 `.pt-paper *` 之后。** + * + * `visibility` 是**继承属性,但子元素可以覆盖它**。原来只写 + * `.pt-cell__idx { visibility: hidden }`,父 div 确实隐了,可下面这条 + * `.pt-paper * { visibility: visible !important }` 会**直接命中里面的 `1`** + * 把它又显出来 —— 序号照样印在纸上。所以必须写成 `.x, .x *` 形式。 + * + * 同理,同特异性 + 同 `!important` 时**后写的赢**,所以这一组必须排在 + * `.pt-paper *` 后面,否则完全不起作用(改了没效果、又不报错)。 + * + * **③ 双击隐藏的格子(`.pt-hide`)也走同一条路。** + * + * 否则「隐藏(不打印)」只藏了内容的外层,`.pt-paper *` 又把里面的字放出来, + * 用户以为隐藏了、纸上却印着。 */ const applyPrintStyles = () => { - if (printStyle) document.head.removeChild(printStyle); + // 用 `node.remove()` 而不是 `document.head.removeChild(node)`: + // 节点可能已经不在 head 里了(上一次撤过、或宿主清过 DOM), + // 对已脱离的节点调 removeChild 会抛 NotFoundError,把整段注入**打断在半路** —— + // 症状是「这次打印页眉序号全都没生效」,而没人会往「上次留下的引用」上想。 + // remove() 对已脱离节点是安全的 no-op。 + if (printStyle) printStyle.remove(); + + // 边距 → 标签纸相对纸边的偏移(cm)。防呆:负数按 0 处理,否则会跑到纸外 + const offsetX = Math.max(0, Number(marginHorizontal.value) || 0); + const offsetY = Math.max(0, Number(marginHeight.value) || 0); + printStyle = document.createElement("style"); printStyle.setAttribute("media", "print"); printStyle.textContent = ` - @page { margin: ${marginHeight.value}cm ${marginHorizontal.value}cm 0; } + /* ① 页边距清零 —— 浏览器就不会在边距里画「日期时间 / 标题 / URL / 页码」 */ + @page { margin: 0; } + @media print { html, body { margin: 0 !important; padding: 0 !important; background: #fff !important; } + + /* 先全隐,只留标签纸:否则宿主的侧边栏、顶栏、插件的面板都会跟着打印 */ body * { visibility: hidden !important; } .pt-paper, .pt-paper * { visibility: visible !important; } - /* 用 absolute 钉回纸张原点:不管宿主把内容区摆在哪,标签都从 (0,0) 开始 */ - .pt-paper { position: absolute; left: 0; top: 0; width: 194mm; box-shadow: none !important; } - .pt-cell--placeholder, .pt-cell__idx { visibility: hidden !important; } + + /* ②③ 再把不该打印的连后代一起隐掉 —— 必须在这一组之前先把上面那条写完 */ + .pt-cell__idx, .pt-cell__idx *, + .pt-cell--placeholder, .pt-cell--placeholder *, + .pt-hide, .pt-hide * { visibility: hidden !important; } + + /* 标签纸钉在纸边偏移处:@page 边距已经是 0,位置只能自己给 */ + .pt-paper { + position: absolute; + left: ${offsetX}cm; + top: ${offsetY}cm; + width: 194mm; + box-shadow: none !important; + } } `; document.head.appendChild(printStyle); @@ -717,7 +767,8 @@ const applyPrintStyles = () => { const onAfterPrint = () => { if (printStyle) { - document.head.removeChild(printStyle); + // 同上:remove() 对已脱离的节点安全 + printStyle.remove(); printStyle = null; } }; @@ -734,6 +785,10 @@ const onPrint = () => { /* ================================================================== */ onMounted(async () => { + // beforeprint 覆盖「用户按 Ctrl+P」以及宿主自己的打印入口 —— 那些路径不会经过 + // onPrint(),没有这条监听,打印出来的就是屏幕上的样子(带着宿主的顶栏和时间)。 + // 注册在页面级、卸载时摘掉,所以只影响这一个页面。 + window.addEventListener("beforeprint", applyPrintStyles); window.addEventListener("afterprint", onAfterPrint); if (snEnabled.value) await loadSnMeta(); @@ -742,6 +797,7 @@ onMounted(async () => { }); onBeforeUnmount(() => { + window.removeEventListener("beforeprint", applyPrintStyles); window.removeEventListener("afterprint", onAfterPrint); onAfterPrint(); }); diff --git a/src/components/LabelCell.vue b/src/components/LabelCell.vue index 8bd6c2a..8d2377f 100644 --- a/src/components/LabelCell.vue +++ b/src/components/LabelCell.vue @@ -163,9 +163,18 @@ const emit = defineEmits(['select', 'toggle-hide']); } @media print { - /* 序号角标与空格引导均不打印 */ + /** + * 序号角标与空格引导均不打印。 + * + * ⚠️ 选择器必须写成 `.x, .x *` 两段:序号里的数字在一个 `` 里, + * 而 `visibility` 是**可被子元素覆盖的继承属性**。只隐父 div 的话, + * 让标签纸整体可见的那条 `.page * { visibility: visible }` 会直接命中 + * 那个 span 把它又显出来 —— 序号照样印在纸上,而且这条规则看上去毫无问题。 + */ .id, - .placeholder { + .id *, + .placeholder, + .placeholder * { visibility: hidden !important; border-color: transparent !important; background: transparent !important; @@ -180,6 +189,8 @@ const emit = defineEmits(['select', 'toggle-hide']); background: transparent !important; } + /* 双击隐藏的格子:内容不打印,但仍占住格子位置 */ + .hide, .hide * { visibility: hidden !important; } diff --git a/src/components/Main.vue b/src/components/Main.vue index a7e85ef..524f475 100644 --- a/src/components/Main.vue +++ b/src/components/Main.vue @@ -99,7 +99,7 @@