@worm-print-platform/canvas 0.0.0-stage → 1.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +427 -2
- package/dist/browser-bundle.d.ts +14 -0
- package/dist/canvas.css +526 -0
- package/dist/canvas.js +1163 -0
- package/dist/core/default-template.d.ts +9 -0
- package/dist/core/element-definitions.d.ts +74 -0
- package/dist/core/format.d.ts +122 -0
- package/dist/core/h.d.ts +22 -0
- package/dist/core/icons.d.ts +10 -0
- package/dist/core/plugin.d.ts +40 -0
- package/dist/core/store.d.ts +163 -0
- package/dist/host-contract.d.ts +120 -0
- package/dist/index.cjs +894 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.js +9175 -0
- package/dist/print-designer.d.ts +251 -0
- package/dist/shell/binding.d.ts +21 -0
- package/dist/shell/canvas.d.ts +61 -0
- package/dist/shell/code.d.ts +11 -0
- package/dist/shell/color-picker.d.ts +44 -0
- package/dist/shell/controls.d.ts +146 -0
- package/dist/shell/crop-anchor.d.ts +29 -0
- package/dist/shell/elements.d.ts +30 -0
- package/dist/shell/expression-editor.d.ts +33 -0
- package/dist/shell/flow-list.d.ts +12 -0
- package/dist/shell/font-select.d.ts +24 -0
- package/dist/shell/format-bar.d.ts +15 -0
- package/dist/shell/help-content/advanced-settings.d.ts +12 -0
- package/dist/shell/help-content/changelog.d.ts +12 -0
- package/dist/shell/help-content/faq.d.ts +12 -0
- package/dist/shell/help-content/features.d.ts +12 -0
- package/dist/shell/help-content/getting-started.d.ts +12 -0
- package/dist/shell/help-content/index.d.ts +13 -0
- package/dist/shell/help-content/shortcuts.d.ts +12 -0
- package/dist/shell/help-modal.d.ts +19 -0
- package/dist/shell/page-extras.d.ts +11 -0
- package/dist/shell/panels.d.ts +20 -0
- package/dist/shell/plugin-ui.d.ts +13 -0
- package/dist/shell/prop-group.d.ts +11 -0
- package/dist/shell/property-panel.d.ts +15 -0
- package/dist/shell/property-search.d.ts +32 -0
- package/dist/shell/ruler.d.ts +29 -0
- package/dist/shell/table-props.d.ts +17 -0
- package/dist/shell/table.d.ts +22 -0
- package/dist/shell/toolbar-overflow.d.ts +28 -0
- package/dist/shell/toolbar.d.ts +8 -0
- package/dist/shell/watermark.d.ts +11 -0
- package/dist/vue3/PrintDesigner.d.ts +127 -0
- package/dist/vue3/PrintHtmlPreview.d.ts +89 -0
- package/dist/vue3/index.cjs +82 -0
- package/dist/vue3/index.d.ts +29 -0
- package/dist/vue3/index.js +460 -0
- package/dist/vue3/previewStyle.d.ts +8 -0
- package/dist/vue3/toolbarOverflow.d.ts +51 -0
- package/package.json +78 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 worm <liu_long_qqcom@qq.com>
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,428 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @worm-print-platform/canvas
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
> **设计器权威实现**:Vue 3 / Vue 2.7 / React / jQuery / 无框架宿主统一消费本包(宿主示例见仓库 `demos/`)。
|
|
4
|
+
> 样式、图标与主题令牌的单一来源在 `core/src/styles`,本包从共享源派生;
|
|
5
|
+
> 元素能力位(core)与元素定义(本包)走统一注册表,内置 11 类元素与第三方插件同权。
|
|
6
|
+
> 宿主接入契约见 [「宿主契约」](#宿主契约) 与 `src/host-contract.ts`;Vue 3 宿主可用 `/vue3` 兼容门面沿用原组件写法。
|
|
7
|
+
|
|
8
|
+
打印模板可视化设计器的 **通用宿主适配形态**:任意框架、或完全没有框架的宿主都能直接接入,
|
|
9
|
+
实现为零框架原生 Custom Element + Shadow DOM,**不依赖 Vue / React 等任何框架运行时**,
|
|
10
|
+
作为设计器的权威实现,同时向 Vue 3、Vue 2、React、jQuery 及任意无框架宿主提供同一套能力。
|
|
11
|
+
|
|
12
|
+
- 功能与布局与 Vue 3 版对齐,逐项状态见 [PARITY.md](./PARITY.md)
|
|
13
|
+
- 样式与图标从 **`core/src/styles` 共享源**派生(`gen-styles.mjs` 剥 `[data-v-*]` 后补主题令牌、
|
|
14
|
+
`gen-icons.mjs` 读共享 `icon-manifest.json`);`gen-styles.mjs --check` 做漂移检测
|
|
15
|
+
- 运行时体积:`dist/index.js` 368.28 kB(gzip 106.68 kB),零框架依赖
|
|
16
|
+
- 浏览器直挂形态:`dist/canvas.js`(UMD,core 与码制库已内联)+ `dist/canvas.css`,见下节
|
|
17
|
+
|
|
18
|
+
## 安装
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
npm i @worm-print-platform/canvas @worm-print-platform/core
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
`canvas` 与主线同号发布正式版,安装直接取 `latest`(工作区为 `1.5.0` 待发),不需要 `@next`;`core` 装同版本即可,出纸链路也要直接用它。本包现为设计器的**权威实现**,技术主站 `demo/` 与四个宿主示例工程均已接入;`/vue3` 门面与 `@worm-vue3-print/canvas` 是 Vue 3 兼容轨。注意 `plugin-kit` 是本包的运行时依赖,1.5.0 起六包同号同批发版,安装时不要只锁 `core` + `canvas` 两个版本号。
|
|
25
|
+
|
|
26
|
+
本页包含三种 NPM 宿主的最小接入示例:[Vue 3](#vue-3) · [Vue 2.7](#vue-27) · [React 18](#react-18);无打包器宿主见下文「浏览器直挂」。
|
|
27
|
+
|
|
28
|
+
四种宿主形态各自的依赖与插件(本包零框架运行时,宿主框架按需装):
|
|
29
|
+
|
|
30
|
+
| 宿主 | 安装 | 框架插件 |
|
|
31
|
+
|---|---|---|
|
|
32
|
+
| Vue 3 | `npm i @worm-print-platform/canvas @worm-print-platform/core vue` | `@vitejs/plugin-vue`,须配 `isCustomElement` |
|
|
33
|
+
| Vue 2.7 | `npm i @worm-print-platform/canvas @worm-print-platform/core vue@2.7` | `@vitejs/plugin-vue2` |
|
|
34
|
+
| React 18 | `npm i @worm-print-platform/canvas @worm-print-platform/core react react-dom` | `@vitejs/plugin-react`,无需自定义元素配置 |
|
|
35
|
+
| jQuery / 无打包器 | 取本包 `dist/canvas.js` + `dist/canvas.css` 直挂 | 无,见下节 |
|
|
36
|
+
|
|
37
|
+
## 浏览器直挂(jQuery / 无打包器宿主)
|
|
38
|
+
|
|
39
|
+
`vite.bundle.config.ts` 额外产出两个文件,子路径 `@worm-print-platform/canvas/browser` 指向它们:
|
|
40
|
+
|
|
41
|
+
- `dist/canvas.js`(499.07 kB / gzip 154.83 kB):UMD,把 `@worm-print-platform/core` 与条形码、二维码库
|
|
42
|
+
全部内联,并挂出全局 `window.WormPrintCanvas`。除了本包 `index` 的那套 API
|
|
43
|
+
(`registerAll` / `createDefaultTemplate` / `DesignerStore` / `PrintDesignerElement`),
|
|
44
|
+
还一并挂出出纸要用的 `renderHtmlPages` / `browserCodeRenderer` / `PrintClient` / `WormPrintError`,
|
|
45
|
+
以及宿主自绘模板缩略图所需的纸张几何工具 `getPaperDimensions` / `isContinuousPaperSize` / `getByPath`
|
|
46
|
+
—— 直挂宿主拿不到裸包名 `@worm-print-platform/core`,没有这些入口就无法在同一个 `<script>` 里
|
|
47
|
+
完成「浏览器渲染 → 打印 / 静默打印」
|
|
48
|
+
- `dist/canvas.css`:即 `src/styles/designer.css`(由 `gen-styles.mjs` 从 core 共享源派生,含 `:host` 块级基线)。
|
|
49
|
+
Shadow DOM 形态用不到它——样式由组件内联注入;只有把设计器渲染进 **light DOM** 时才需要引它,
|
|
50
|
+
公开子路径为 `@worm-print-platform/canvas/style.css`(打包器 `import '@worm-print-platform/canvas/style.css'`,
|
|
51
|
+
无打包器则复制成文件后 `<link>`),不要深路径引 `dist/`
|
|
52
|
+
|
|
53
|
+
```html
|
|
54
|
+
<link rel="stylesheet" href="/vendor/worm-print-canvas.css" />
|
|
55
|
+
<script src="/vendor/worm-print-canvas.js"></script>
|
|
56
|
+
<script>
|
|
57
|
+
WormPrintCanvas.registerAll()
|
|
58
|
+
const el = document.querySelector('print-designer')
|
|
59
|
+
el.fields = fields // 数组/对象只能赋 property
|
|
60
|
+
el.template = templateJson
|
|
61
|
+
el.uploadImage = file => uploadToServer(file)
|
|
62
|
+
el.addEventListener('save', e => persist(e.detail))
|
|
63
|
+
const out = await WormPrintCanvas.renderHtmlPages(
|
|
64
|
+
JSON.parse(el.getTemplateJson()), data, location.origin, WormPrintCanvas.browserCodeRenderer)
|
|
65
|
+
</script>
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
两个硬约束:自定义元素默认 `display:inline`,宿主必须给它块级宽高,否则设计器整体塌陷;
|
|
69
|
+
`fields` 没有 getter,读回要看 `el.store.fields`。
|
|
70
|
+
|
|
71
|
+
完整可运行的直挂宿主示例见 `demos/apps/jquery`(含 `sync-assets.mjs` 如何从包 dist 取这两个文件、
|
|
72
|
+
以及 `/render-api` 透传的 40 行静态服务器);Vue 3 / Vue 2 / React 三种 NPM 接入形态见下文
|
|
73
|
+
「在 Vue 3 / Vue 2 / React 中使用」,可运行工程在 `demos/apps/vue3`、`demos/apps/vue2`、`demos/apps/react`。
|
|
74
|
+
|
|
75
|
+
## 使用
|
|
76
|
+
|
|
77
|
+
```js
|
|
78
|
+
import { registerAll } from '@worm-print-platform/canvas'
|
|
79
|
+
registerAll() // 注册 <print-designer>,按需调用,不自动污染全局
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
```html
|
|
83
|
+
<print-designer id="d" style="height:100%"></print-designer>
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
```js
|
|
87
|
+
const d = document.getElementById('d')
|
|
88
|
+
d.fields = [{ fieldKey: 'orderNo', fieldLabel: '单号' }] // 也接受 JSON 字符串
|
|
89
|
+
d.template = templateJson
|
|
90
|
+
d.addEventListener('save', e => save(e.detail)) // e.detail 为模板 JSON
|
|
91
|
+
d.addEventListener('preview', e => preview(e.detail))
|
|
92
|
+
d.getTemplateJson()
|
|
93
|
+
d.validateTemplate() // 拼版/多页配置校验,宿主旁路导出前调用
|
|
94
|
+
d.loadTemplate(json)
|
|
95
|
+
d.save()
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
出纸链路不需要设计器,直接走 `@worm-print-platform/core`(浏览器渲染 / print-render 服务端 /
|
|
99
|
+
桌面静默打印),与 canvas(Vue 3)宿主完全一致。
|
|
100
|
+
|
|
101
|
+
## 宿主契约
|
|
102
|
+
|
|
103
|
+
`<print-designer>` 的接入面是**版本化**的,定义在 `src/host-contract.ts`,宿主不必再从散文里抄:
|
|
104
|
+
|
|
105
|
+
```ts
|
|
106
|
+
import {
|
|
107
|
+
PRINT_DESIGNER_CONTRACT_VERSION, getHostContract,
|
|
108
|
+
PRINT_DESIGNER_PARTS, PRINT_DESIGNER_THEME_TOKENS,
|
|
109
|
+
} from '@worm-print-platform/canvas'
|
|
110
|
+
import type { PrintDesignerEventMap, PrintDesignerCustomEvent } from '@worm-print-platform/canvas'
|
|
111
|
+
|
|
112
|
+
// 运行时自检(自定义元素的静态属性与实例方法都能读)
|
|
113
|
+
PrintDesignerElement.contractVersion === PRINT_DESIGNER_CONTRACT_VERSION
|
|
114
|
+
el.getHostContract() === getHostContract()
|
|
115
|
+
|
|
116
|
+
// 事件 detail 有类型:不要把 dblclick-element 的 detail 当 JSON 解析
|
|
117
|
+
el.addEventListener('save', (e) => {
|
|
118
|
+
const ev = e as PrintDesignerCustomEvent<'save'> // ev.detail: string
|
|
119
|
+
})
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
| 面 | 内容 |
|
|
123
|
+
|---|---|
|
|
124
|
+
| attribute | `template` · `fields` · `name` · `fonts` · `design-data`(另有 `is-edit` / `show-help` 两个布尔开关) |
|
|
125
|
+
| property | `template` · `fields` · `fonts` · `designData` · `isEdit` · `showHelp` · `uploadImage` · `uploadDesignBackground` |
|
|
126
|
+
| 方法 | `getTemplateJson()` · `loadTemplate()` · `save()` · `validateTemplate()` · `getGeometryWarnings()` |
|
|
127
|
+
| 事件(`bubbles + composed`) | `save` / `preview` → 模板 JSON 字符串;`help` → 无载荷;`dblclick-element` → 元素 id 字符串;`dblclick-cell` → `{ elementId, r, c, cellId? }` |
|
|
128
|
+
|
|
129
|
+
`getTemplateJson()` 在自定义元素上返回**字符串**(`/vue3` 门面转回对象);它绕过拼版校验,
|
|
130
|
+
宿主旁路导出前要先调 `validateTemplate()`。事件全部 `bubbles: true, composed: true`,可穿透 Shadow DOM。
|
|
131
|
+
|
|
132
|
+
**定制外观**:外壳 8 个 `::part` 挂钩(`container` / `toolbar` / `format-bar` / `left-panel` /
|
|
133
|
+
`canvas-area` / `property-panel` / `status-bar` / `page-tabs`)允许有限定制:
|
|
134
|
+
|
|
135
|
+
```css
|
|
136
|
+
print-designer::part(toolbar) { border-bottom-width: 2px }
|
|
137
|
+
print-designer { --pd-accent: #0a7d4b } /* 主题令牌,完整清单见 core/styles/theme.css */
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
主题令牌有 52 个 `--pd-*`,权威定义在 `@worm-print-platform/core/styles/theme.css`;不要写死颜色到
|
|
141
|
+
Shadow DOM 内部选择器。契约版本遵守语义化:删/改 attribute、方法或事件形状升 MAJOR,
|
|
142
|
+
新增可选项升 MINOR;`src/__tests__/host-contract.spec.ts` 会把声明面与运行时逐项对拍,漂移即红。
|
|
143
|
+
|
|
144
|
+
**扩展元素**:内置 11 类与第三方插件走同一张注册表,见下节与「统一元素注册表」。
|
|
145
|
+
|
|
146
|
+
## 统一元素注册表
|
|
147
|
+
|
|
148
|
+
内置元素与插件**同权**:`registerElementDefinition()` 可覆盖内置的素材卡标签/图标/属性分组/双击字段,
|
|
149
|
+
`registerElementRenderer()` 可替换画布内容渲染;注销句柄只撤销自己那一版,内置类型会自动回落基线。
|
|
150
|
+
core 的 `registerElementCapabilities()` 则统一六处类型白名单(页眉页脚放行、旋转、文字溢出、
|
|
151
|
+
元素级边框、绑定声明、出厂默认值),插件在 `defineElementPlugin({ kinds: [{ capabilities }] })` 里
|
|
152
|
+
声明即自动生效:
|
|
153
|
+
|
|
154
|
+
```ts
|
|
155
|
+
import { registerElementDefinition, registerElementRenderer } from '@worm-print-platform/canvas'
|
|
156
|
+
|
|
157
|
+
const release = registerElementDefinition({
|
|
158
|
+
type: 'signature', label: '手写签名', icon: 'PenLine',
|
|
159
|
+
material: true, propertyGroup: 'general', dblEditableField: 'src', group: '扩展',
|
|
160
|
+
})
|
|
161
|
+
registerElementRenderer('signature', (el) => { /* 返回 HTML 字符串或 Node */ })
|
|
162
|
+
// release() 后素材卡与渲染一并回到内置基线
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
两处注册表都挂在 `globalThis` 固定 Symbol 上,与 core / plugin-kit 一致,避免多打包副本漂移;
|
|
166
|
+
注册态变化会通知已挂载的设计器即时重绘。
|
|
167
|
+
|
|
168
|
+
## 自定义元素插件(图表等)
|
|
169
|
+
|
|
170
|
+
设计器只保留通用机制,具体元素由独立插件包提供。宿主 `import` 一次插件入口即可让素材卡、
|
|
171
|
+
画布、属性台「自定义属性」页签与出纸同时生效;元素语义来自 `@worm-print-platform/core/designer`
|
|
172
|
+
的插件描述符,设计器 UI 来自 `@worm-print-platform/plugin-kit` 的框架无关 `ElementUI` 契约,
|
|
173
|
+
一份 UI 同时服务本包与 Vue 3 轨的 canvas。
|
|
174
|
+
|
|
175
|
+
```js
|
|
176
|
+
import { registerAll } from '@worm-print-platform/canvas'
|
|
177
|
+
import '@worm-print-platform/echarts/ui' // 官方图表插件(/ui 入口);换成自己的插件包同理
|
|
178
|
+
registerAll()
|
|
179
|
+
|
|
180
|
+
const d = document.querySelector('print-designer')
|
|
181
|
+
d.fields = [{ fieldKey: 'trend', fieldLabel: '趋势数据', fieldType: 'list', sortOrder: 1 }]
|
|
182
|
+
// 设计态示例数据:单个业务对象、对象数组或 design-data attribute 的 JSON 串都可以;
|
|
183
|
+
// 单对象自动归一成数组信封。未注入时插件回落到 core 的 DEFAULT_DEMO_DATA。
|
|
184
|
+
d.designData = { trend: [{ name: '一月', value: 12 }, { name: '二月', value: 28 }] }
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
`data`(数组信封)会传给插件的画布与属性台渲染函数,`fields` 传给属性台渲染函数,
|
|
188
|
+
图表类插件据此按真实业务数据画预览、按业务字段生成数据源下拉。未注册 UI 的插件不会出现在素材台。
|
|
189
|
+
写自己的插件见 `packages/plugin-kit/README.md`。
|
|
190
|
+
|
|
191
|
+
## Vue 3 兼容门面(`/vue3`)
|
|
192
|
+
|
|
193
|
+
原本消费 `@worm-vue3-print/canvas` 的 Vue 3 宿主,可以只改依赖与 import 就把设计器实现换成
|
|
194
|
+
零框架的本包,模板、事件回调、`ref` 方法调用与业务逻辑一行不动:
|
|
195
|
+
|
|
196
|
+
```diff
|
|
197
|
+
- "@worm-vue3-print/canvas": "..."
|
|
198
|
+
+ "@worm-print-platform/canvas": "..."
|
|
199
|
+
```
|
|
200
|
+
```diff
|
|
201
|
+
- import { PrintDesigner, PrintHtmlPreview, ... } from '@worm-vue3-print/canvas'
|
|
202
|
+
- import type { TemplateData } from '@worm-vue3-print/canvas'
|
|
203
|
+
+ import { PrintDesigner, PrintHtmlPreview, ... } from '@worm-print-platform/canvas/vue3'
|
|
204
|
+
+ import type { TemplateData } from '@worm-print-platform/canvas/vue3'
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
可选扩展包同步换:旧 `echarts` / `echarts-common` 插件 → `@worm-print-platform/echarts`(UI 入口为 `/ui`)。
|
|
208
|
+
monorepo 源码消费的宿主还需把 Vite `resolve.alias` 与 `tsconfig.paths`
|
|
209
|
+
里的旧 canvas 条目指向 `packages/canvas/src/vue3/index.ts`,并补 `canvas` / `plugin-kit` 两条。
|
|
210
|
+
|
|
211
|
+
`vue` 是本子路径的 **optional peer**:不用 `/vue3` 时主入口仍是零框架、零 Vue 依赖。
|
|
212
|
+
设计器样式由元素自己注入 Shadow DOM,预览组件样式由门面幂等注入,宿主都不需要额外引 CSS。
|
|
213
|
+
|
|
214
|
+
### 门面负责的契约转换
|
|
215
|
+
|
|
216
|
+
| Vue 组件侧(Vue 3 轨口径) | Custom Element 侧 | 门面动作 |
|
|
217
|
+
|---|---|---|
|
|
218
|
+
| `:initial-template` `:fields` `:fonts` 与函数型 `:upload-image` | `el.template` `el.fields` `el.fonts` `el.uploadImage` | 显式赋 property,绕开 `setAttribute` 把对象 / 函数字符串化 |
|
|
219
|
+
| `@save="onSave"` 首参是 JSON 字符串 | `CustomEvent('save')`,json 在 `detail` | 解 `detail` 再 emit |
|
|
220
|
+
| `@preview` | `CustomEvent('preview')` | 转无参 emit |
|
|
221
|
+
| `ref.getTemplateJson()` 返回对象 | `getTemplateJson()` 返回 JSON 字符串 | `JSON.parse` |
|
|
222
|
+
| `ref.validateTemplate()` / `getGeometryWarnings()` | 同名同语义 | 直接代理 |
|
|
223
|
+
|
|
224
|
+
props 变化会重新注入(与 Vue 3 轨的 `watch` 口径一致);卸载时摘除事件监听。
|
|
225
|
+
|
|
226
|
+
### 已提供的导出
|
|
227
|
+
|
|
228
|
+
`PrintDesigner`、`PrintHtmlPreview`、`renderHtmlPages`、`browserCodeRenderer`、
|
|
229
|
+
`createDefaultTemplate`、`toRuntimePool`、`DEFAULT_DEMO_DATA`、`getDemoData`、
|
|
230
|
+
`provideToolbarOverflow` / `useToolbarOverflow` / `emptyToolbarOverflow` /
|
|
231
|
+
`createOverflowUnitDirective` / `TOOLBAR_OVERFLOW` / `moreSlotId`,
|
|
232
|
+
以及 `@worm-print-platform/core/designer` 的全量类型与 `PrintFontDeclaration`。
|
|
233
|
+
`InstanceType<typeof PrintDesigner>` / `InstanceType<typeof PrintHtmlPreview>` 含 expose 方法,
|
|
234
|
+
TS 宿主沿用原写法即可通过 `vue-tsc`。
|
|
235
|
+
|
|
236
|
+
### 能力边界(换包前请确认)
|
|
237
|
+
|
|
238
|
+
- **Vue 专属元素扩展注册表不提供**:`registerElementExtension` / `resolveElementPropertyTab`
|
|
239
|
+
等属于 Vue 3 轨 canvas 的 Vue 组件契约。自定义元素改走 `@worm-print-platform/plugin-kit`
|
|
240
|
+
(`defineElementUI` / `registerElementUI`),官方图表用 `@worm-print-platform/echarts/ui`,见上一节。
|
|
241
|
+
- `initialElements`:本包没有独立元素池,门面在未提供 `initialTemplate` 时把它并入
|
|
242
|
+
当前模板第一页的 `elements`;与 Vue 3 轨的「单独覆盖元素层」略有差别。
|
|
243
|
+
- **样式、图标与主题令牌的单一来源在 `core/src/styles`**:`gen-styles.mjs` / `gen-icons.mjs`
|
|
244
|
+
从共享源派生;Vue 3 轨反向消费同一份主题令牌,本包不依赖其构建产物。因此本仓构建顺序是
|
|
245
|
+
core → plugin-kit → canvas,与 Vue 3 轨互不阻塞。
|
|
246
|
+
- **本包是设计器权威实现**,`/vue3` 门面与 `@worm-vue3-print/canvas` 是 Vue 3 兼容轨(沿用原组件写法,
|
|
247
|
+
不是新的正式方案);新接入的宿主统一用本包主入口。
|
|
248
|
+
|
|
249
|
+
## 在 Vue 3 / Vue 2 / React 中使用
|
|
250
|
+
|
|
251
|
+
三种宿主消费的是**同一个 `<print-designer>` 自定义元素**,差异只有两处:编译期要不要放行未知标签、
|
|
252
|
+
对象入参在哪一帧赋 property。下面代码取自 `demos/apps/{vue3,vue2,react}` 三个可运行工程,
|
|
253
|
+
已剥掉示例库与弹层等 demo 噪音。
|
|
254
|
+
|
|
255
|
+
### Vue 3
|
|
256
|
+
|
|
257
|
+
> Vue 3 项目的生产首选仍是 `@worm-vue3-print/canvas`(功能面更全);本节面向已定用本包形态、
|
|
258
|
+
> 或要与 Vue 2 / React 共用同一套注入代码的场景。
|
|
259
|
+
|
|
260
|
+
```js
|
|
261
|
+
// vite.config.js —— SFC 编译期就要声明 print-* 是自定义元素,否则被当未注册组件
|
|
262
|
+
import vue from '@vitejs/plugin-vue'
|
|
263
|
+
|
|
264
|
+
export default defineConfig({
|
|
265
|
+
plugins: [vue({
|
|
266
|
+
template: { compilerOptions: { isCustomElement: tag => tag.startsWith('print-') } },
|
|
267
|
+
})],
|
|
268
|
+
})
|
|
269
|
+
```
|
|
270
|
+
|
|
271
|
+
```js
|
|
272
|
+
// main.js
|
|
273
|
+
import { createApp } from 'vue'
|
|
274
|
+
import { registerAll } from '@worm-print-platform/canvas'
|
|
275
|
+
|
|
276
|
+
registerAll() // 全应用调一次,不自动污染全局
|
|
277
|
+
createApp(App).mount('#app')
|
|
278
|
+
```
|
|
279
|
+
|
|
280
|
+
```vue
|
|
281
|
+
<template>
|
|
282
|
+
<print-designer ref="designerRef" is-edit="true" show-help="true"
|
|
283
|
+
@save="onSave" @preview="onPreview" @dblclick-cell="onDblclickCell" />
|
|
284
|
+
</template>
|
|
285
|
+
|
|
286
|
+
<script setup>
|
|
287
|
+
import { onMounted, ref } from 'vue'
|
|
288
|
+
|
|
289
|
+
const designerRef = ref(null)
|
|
290
|
+
|
|
291
|
+
/** 对象/函数入参只能赋 property(模板绑定会走 setAttribute 被字符串化) */
|
|
292
|
+
function syncDesigner() {
|
|
293
|
+
const el = designerRef.value
|
|
294
|
+
el.fields = fields.value
|
|
295
|
+
el.template = template.value
|
|
296
|
+
el.fonts = fontsOf(template.value) // 可选:字体声明;demo 的 fontsOf() 取各页 /fonts 并按 family 去重
|
|
297
|
+
}
|
|
298
|
+
const onSave = e => persist(JSON.parse(e.detail))
|
|
299
|
+
|
|
300
|
+
onMounted(() => {
|
|
301
|
+
const el = designerRef.value
|
|
302
|
+
el.uploadImage = uploadToServer
|
|
303
|
+
el.uploadDesignBackground = uploadToServer
|
|
304
|
+
syncDesigner()
|
|
305
|
+
})
|
|
306
|
+
</script>
|
|
307
|
+
```
|
|
308
|
+
|
|
309
|
+
- **runtime-only 构建下 `app.config.compilerOptions.isCustomElement` 不生效**,配置必须落在 `@vitejs/plugin-vue` 里;
|
|
310
|
+
只有自带运行时编译器的宿主才用 `app.config` 那条。
|
|
311
|
+
|
|
312
|
+
### Vue 2.7
|
|
313
|
+
|
|
314
|
+
```js
|
|
315
|
+
// main.js
|
|
316
|
+
import Vue from 'vue'
|
|
317
|
+
import { registerAll } from '@worm-print-platform/canvas'
|
|
318
|
+
|
|
319
|
+
registerAll()
|
|
320
|
+
// 必须声明,否则报 Unknown custom element、画面是个空壳
|
|
321
|
+
Vue.config.ignoredElements = ['print-designer']
|
|
322
|
+
|
|
323
|
+
new Vue({ render: h => h(App) }).$mount('#app')
|
|
324
|
+
```
|
|
325
|
+
|
|
326
|
+
```vue
|
|
327
|
+
<template>
|
|
328
|
+
<print-designer ref="designer" is-edit="true" show-help="true"
|
|
329
|
+
@save="onSave" @preview="onPreview" @dblclick-cell="onDblclickCell" />
|
|
330
|
+
</template>
|
|
331
|
+
|
|
332
|
+
<script>
|
|
333
|
+
export default {
|
|
334
|
+
data: () => ({ fields: [], templateData: null }),
|
|
335
|
+
mounted() {
|
|
336
|
+
const el = this.$refs.designer
|
|
337
|
+
el.uploadImage = this.uploadImage // 函数只能赋 property
|
|
338
|
+
el.uploadDesignBackground = this.uploadImage
|
|
339
|
+
this.syncDesigner()
|
|
340
|
+
},
|
|
341
|
+
methods: {
|
|
342
|
+
syncDesigner() {
|
|
343
|
+
const el = this.$refs.designer
|
|
344
|
+
if (!el) return
|
|
345
|
+
el.fields = this.fields // 数组只能赋 property
|
|
346
|
+
el.template = this.templateData // 对象同上
|
|
347
|
+
},
|
|
348
|
+
onSave(e) { persist(JSON.parse(e.detail)) }, // e.detail 为模板 JSON 字符串
|
|
349
|
+
},
|
|
350
|
+
}
|
|
351
|
+
</script>
|
|
352
|
+
```
|
|
353
|
+
|
|
354
|
+
- **`structuredClone` 会抛 `DataCloneError`**:Vue 2 把 `data` 里的对象深度响应式化成 Proxy,
|
|
355
|
+
模板/数据深拷贝统一用 `JSON.parse(JSON.stringify(...))`。
|
|
356
|
+
- 宿主页面是**单根节点**(Vue 2 不支持多根 fragment,预览层与弹层都得写在根节点内);
|
|
357
|
+
`v-if` 控制的预览 iframe 要 `$nextTick()` + `requestAnimationFrame()` 两层才拿得到节点。
|
|
358
|
+
- Vue 2 侧专属坑的完整清单见 `demos/apps/vue2/README.md`(共 7 条)。
|
|
359
|
+
|
|
360
|
+
### React 18
|
|
361
|
+
|
|
362
|
+
React 没有 `isCustomElement` 这类编译配置:未知标签 `print-designer` 直接按原生元素渲染,
|
|
363
|
+
所以只剩运行期两条约定。
|
|
364
|
+
|
|
365
|
+
```jsx
|
|
366
|
+
// main.jsx
|
|
367
|
+
import { registerAll } from '@worm-print-platform/canvas'
|
|
368
|
+
|
|
369
|
+
registerAll() // 一次即可;设计器样式在组件 Shadow DOM 内,宿主不必引 css
|
|
370
|
+
```
|
|
371
|
+
|
|
372
|
+
```jsx
|
|
373
|
+
// App.jsx
|
|
374
|
+
import { useEffect, useRef, useState } from 'react'
|
|
375
|
+
|
|
376
|
+
export default function App() {
|
|
377
|
+
const designerRef = useRef(null)
|
|
378
|
+
const [fields, setFields] = useState([])
|
|
379
|
+
const [templateData, setTemplateData] = useState(null)
|
|
380
|
+
|
|
381
|
+
function syncDesigner(nextFields, nextTemplate) {
|
|
382
|
+
const el = designerRef.current
|
|
383
|
+
if (!el) return
|
|
384
|
+
el.fields = nextFields // JSX 里 template={obj} 会被字符串化成 [object Object]
|
|
385
|
+
el.template = nextTemplate
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
useEffect(() => {
|
|
389
|
+
const el = designerRef.current
|
|
390
|
+
el.uploadImage = uploadToServer // 函数同样只能赋 property
|
|
391
|
+
el.uploadDesignBackground = uploadToServer
|
|
392
|
+
syncDesigner(fields, templateData) // 挂载后统一注入;换模板/换字段后再调一次
|
|
393
|
+
|
|
394
|
+
const onSave = e => persist(JSON.parse(e.detail))
|
|
395
|
+
el.addEventListener('save', onSave) // JSX 的 onSave 不会绑定 CustomEvent
|
|
396
|
+
return () => el.removeEventListener('save', onSave) // StrictMode dev 双跑,漏解绑会重复注册
|
|
397
|
+
}, [])
|
|
398
|
+
|
|
399
|
+
return <print-designer ref={designerRef} is-edit="true" show-help="true" />
|
|
400
|
+
}
|
|
401
|
+
```
|
|
402
|
+
|
|
403
|
+
- 事件只注册一次:处理器要读最新 state 时用 `useRef` 桥接(demo 里的 `printDataRef`),
|
|
404
|
+
把 state 放进 effect 依赖会导致频繁重绑。
|
|
405
|
+
|
|
406
|
+
### 三宿主共通的三条
|
|
407
|
+
|
|
408
|
+
1. **对象与函数只能赋 property**;字符串开关(`is-edit="true"`、`show-help="true"`)写在模板 / JSX 属性上即可。
|
|
409
|
+
`fields` 只有 setter(读回看 `el.store.fields`),`template` 的 getter 返回当前画布 JSON。
|
|
410
|
+
改字段/换模板后要**显式再调一次注入函数**(三个 demo 都在切示例、导入模板、清空、自定义字段四处调用),
|
|
411
|
+
不去 `watch` element 本身。
|
|
412
|
+
2. **元素默认 `display:inline`,宿主必须给块级宽高**,否则设计器整体塌陷:
|
|
413
|
+
`.容器 print-designer { display: block; width: 100%; height: 100% }`。
|
|
414
|
+
3. **事件是 CustomEvent**,载荷在 `event.detail`:`save` / `preview` 是模板 JSON 字符串;
|
|
415
|
+
`dblclick-element` 就是元素 id 字符串本身、`dblclick-cell` 是 `{ elementId, r, c, cellId }` 对象,
|
|
416
|
+
后两者**不要** `JSON.parse`。旁路取模板用 `getTemplateJson()`,它绕过拼版校验,
|
|
417
|
+
保存/导出前要先调 `validateTemplate()`。
|
|
418
|
+
|
|
419
|
+
## 与 canvas 版的边界
|
|
420
|
+
|
|
421
|
+
| | Vue 3 轨(@worm-vue3-print/canvas) | 本包(@worm-print-platform/canvas) |
|
|
422
|
+
|---|---|---|
|
|
423
|
+
| 运行时 | Vue 3.5 | 无框架 |
|
|
424
|
+
| 宿主注入 | `provide/inject`、props | element property / attribute + CustomEvent |
|
|
425
|
+
| 样式 | `@worm-vue3-print/canvas/style.css` | 内置 Shadow DOM(`?inline`),无需引样式文件;light DOM 形态引 `@worm-print-platform/canvas/style.css` |
|
|
426
|
+
| 适用 | Vue 3 项目 | React / Vue 2 / jQuery / 原生 |
|
|
427
|
+
|
|
428
|
+
Vue 3 项目若要沿用原组件形态,可用 `/vue3` 兼容门面或 `@worm-vue3-print/canvas`(消费同一份共享样式与能力表);新接入的宿主统一用本包主入口。
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* @worm-print-platform/canvas v1.5.0
|
|
3
|
+
* Copyright (c) 2026 worm <liu_long_qqcom@qq.com>
|
|
4
|
+
* SPDX-FileCopyrightText: 2026 worm <liu_long_qqcom@qq.com>
|
|
5
|
+
* SPDX-License-Identifier: MIT
|
|
6
|
+
*/
|
|
7
|
+
import { getPaperDimensions, isContinuousPaperSize, getByPath } from '@worm-print-platform/core';
|
|
8
|
+
import { applyPreviewViewZoom, defaultZoomKey, paperWidthPx, resolvePrinterDpi, resolveToolbarOverflow, resolveZoomKey, viewZoomHint, zoomPresets, zoomValueOf } from '@worm-print-platform/core/designer';
|
|
9
|
+
import { renderHtmlPages, browserCodeRenderer } from '@worm-print-platform/core/browser';
|
|
10
|
+
import { PrintClient, WormPrintError } from '@worm-print-platform/core/client';
|
|
11
|
+
export * from './index';
|
|
12
|
+
export { renderHtmlPages, browserCodeRenderer, PrintClient, WormPrintError };
|
|
13
|
+
export { getPaperDimensions, isContinuousPaperSize, getByPath };
|
|
14
|
+
export { applyPreviewViewZoom, defaultZoomKey, paperWidthPx, resolvePrinterDpi, resolveToolbarOverflow, resolveZoomKey, viewZoomHint, zoomPresets, zoomValueOf, };
|