@worm-print-platform/canvas 1.5.0 → 1.5.2
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/README.md +112 -359
- package/dist/browser-bundle.d.ts +1 -1
- package/dist/canvas.css +1 -1
- package/dist/canvas.js +84 -81
- package/dist/core/default-template.d.ts +1 -1
- package/dist/core/element-definitions.d.ts +1 -1
- package/dist/core/format.d.ts +1 -1
- package/dist/core/h.d.ts +1 -1
- package/dist/core/icons.d.ts +1 -1
- package/dist/core/plugin.d.ts +1 -1
- package/dist/core/store.d.ts +1 -1
- package/dist/host-contract.d.ts +7 -3
- package/dist/index.cjs +14 -11
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1289 -1242
- package/dist/print-designer.d.ts +7 -3
- package/dist/shell/binding.d.ts +1 -1
- package/dist/shell/canvas.d.ts +1 -1
- package/dist/shell/code.d.ts +1 -1
- package/dist/shell/color-picker.d.ts +1 -1
- package/dist/shell/controls.d.ts +4 -1
- package/dist/shell/crop-anchor.d.ts +1 -1
- package/dist/shell/elements.d.ts +1 -1
- package/dist/shell/expression-editor.d.ts +1 -1
- package/dist/shell/flow-list.d.ts +1 -1
- package/dist/shell/font-select.d.ts +1 -1
- package/dist/shell/format-bar.d.ts +1 -1
- package/dist/shell/help-content/advanced-settings.d.ts +1 -1
- package/dist/shell/help-content/changelog.d.ts +1 -1
- package/dist/shell/help-content/faq.d.ts +1 -1
- package/dist/shell/help-content/features.d.ts +1 -1
- package/dist/shell/help-content/getting-started.d.ts +1 -1
- package/dist/shell/help-content/index.d.ts +1 -1
- package/dist/shell/help-content/shortcuts.d.ts +1 -1
- package/dist/shell/help-modal.d.ts +1 -1
- package/dist/shell/page-extras.d.ts +1 -1
- package/dist/shell/panels.d.ts +1 -1
- package/dist/shell/plugin-ui.d.ts +1 -1
- package/dist/shell/prop-group.d.ts +1 -1
- package/dist/shell/property-panel.d.ts +1 -1
- package/dist/shell/property-search.d.ts +1 -1
- package/dist/shell/ruler.d.ts +1 -1
- package/dist/shell/table-props.d.ts +1 -1
- package/dist/shell/table.d.ts +2 -2
- package/dist/shell/toolbar-overflow.d.ts +1 -1
- package/dist/shell/toolbar.d.ts +1 -1
- package/dist/shell/watermark.d.ts +1 -1
- package/dist/vue3/PrintDesigner.d.ts +23 -1
- package/dist/vue3/PrintHtmlPreview.d.ts +1 -1
- package/dist/vue3/index.cjs +2 -2
- package/dist/vue3/index.d.ts +1 -1
- package/dist/vue3/index.js +121 -111
- package/dist/vue3/previewStyle.d.ts +1 -1
- package/dist/vue3/toolbarOverflow.d.ts +1 -1
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -1,428 +1,181 @@
|
|
|
1
|
-
|
|
1
|
+
<p align="center">
|
|
2
|
+
<strong>@worm-print-platform/canvas</strong><br>
|
|
3
|
+
<em>打印模板设计器权威实现(零框架 Custom Elements)</em>
|
|
4
|
+
</p>
|
|
2
5
|
|
|
3
|
-
>
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
6
|
+
<p align="center">
|
|
7
|
+
<img src="https://img.shields.io/badge/npm-v1.5.2-384BFF" alt="npm">
|
|
8
|
+
<img src="https://img.shields.io/badge/license-MIT-9A9A9A" alt="license">
|
|
9
|
+
<a href="/vue3">
|
|
10
|
+
<img src="https://img.shields.io/badge/vue-optional%20peer-42b883" alt="vue peer">
|
|
11
|
+
</a>
|
|
12
|
+
</p>
|
|
7
13
|
|
|
8
|
-
|
|
9
|
-
实现为零框架原生 Custom Element + Shadow DOM,**不依赖 Vue / React 等任何框架运行时**,
|
|
10
|
-
作为设计器的权威实现,同时向 Vue 3、Vue 2、React、jQuery 及任意无框架宿主提供同一套能力。
|
|
14
|
+
> monorepo 根:[worm-print-platform](https://github.com/worm-longliu/worm-print-platform) · 在线示例:[GitHub Pages](https://worm-longliu.github.io/worm-print-platform/) · 设计器能力验收清单:[PARITY.md](./PARITY.md)
|
|
11
15
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
-
|
|
16
|
-
-
|
|
16
|
+
打印模板可视化设计器的**权威实现**:原生 Custom Element + Shadow DOM,**不依赖 Vue / React 等任何框架运行时**。Vue 3 / Vue 2.7 / React / jQuery / 原生 JS 等任意宿主统一消费本包,样式 / 图标 / 主题令牌从 `@worm-print-platform/core/src/styles` 共享源派生,元素能力与定义走统一注册表(内置 11 类与第三方插件同权)。
|
|
17
|
+
|
|
18
|
+
- 运行时体积:`dist/index.js` 约 368 kB(gzip 106 kB),零框架依赖
|
|
19
|
+
- 浏览器直挂形态:`dist/canvas.js`(UMD,core + 码制库内联)+ `dist/canvas.css`
|
|
20
|
+
- `vue` 是可选 peer,不用 `/vue3` 时主入口零 Vue 依赖
|
|
17
21
|
|
|
18
22
|
## 安装
|
|
19
23
|
|
|
20
24
|
```bash
|
|
25
|
+
# NPM 宿主
|
|
21
26
|
npm i @worm-print-platform/canvas @worm-print-platform/core
|
|
22
27
|
```
|
|
23
28
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
本页包含三种 NPM 宿主的最小接入示例:[Vue 3](#vue-3) · [Vue 2.7](#vue-27) · [React 18](#react-18);无打包器宿主见下文「浏览器直挂」。
|
|
29
|
+
宿主框架依赖(主入口零框架运行时,宿主框架按需装):
|
|
27
30
|
|
|
28
|
-
|
|
31
|
+
| 宿主 | 安装 |
|
|
32
|
+
| --- | --- |
|
|
33
|
+
| Vue 3 | `npm i @worm-print-platform/canvas @worm-print-platform/core vue`(可选 `/vue3` 门面) |
|
|
34
|
+
| Vue 2.7 | `npm i @worm-print-platform/canvas @worm-print-platform/core vue@2.7` |
|
|
35
|
+
| React 18 | `npm i @worm-print-platform/canvas @worm-print-platform/core react react-dom` |
|
|
36
|
+
| jQuery / 无打包器 | 取 `dist/canvas.js` + `dist/canvas.css` 直挂(见下节) |
|
|
29
37
|
|
|
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
|
-
## 使用
|
|
38
|
+
## 快速示例(原生 Custom Element)
|
|
76
39
|
|
|
77
40
|
```js
|
|
78
41
|
import { registerAll } from '@worm-print-platform/canvas'
|
|
79
|
-
registerAll() // 注册 <print-designer
|
|
42
|
+
registerAll() // 注册 <print-designer>,不自动污染全局
|
|
80
43
|
```
|
|
81
44
|
|
|
82
45
|
```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() 后素材卡与渲染一并回到内置基线
|
|
46
|
+
<print-designer id="d" style="display:block;height:100%"></print-designer>
|
|
163
47
|
```
|
|
164
48
|
|
|
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
49
|
```js
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
//
|
|
183
|
-
//
|
|
184
|
-
|
|
50
|
+
const el = document.getElementById('d')
|
|
51
|
+
el.fields = [{ fieldKey: 'orderNo', fieldLabel: '单号' }]
|
|
52
|
+
el.template = templateJson
|
|
53
|
+
el.uploadImage = file => uploadToServer(file)
|
|
54
|
+
el.addEventListener('save', e => persist(e.detail))
|
|
55
|
+
el.addEventListener('preview', e => preview(e.detail))
|
|
56
|
+
el.getTemplateJson() // 返回 JSON 字符串
|
|
57
|
+
el.validateTemplate() // 拼版/多页配置校验,保存前必须调
|
|
58
|
+
el.loadTemplate(json)
|
|
185
59
|
```
|
|
186
60
|
|
|
187
|
-
|
|
188
|
-
图表类插件据此按真实业务数据画预览、按业务字段生成数据源下拉。未注册 UI 的插件不会出现在素材台。
|
|
189
|
-
写自己的插件见 `packages/plugin-kit/README.md`。
|
|
61
|
+
> 完整宿主接入代码见根 README「快速开始」章节与各框架文档:[docs/vue3.md](../../docs/vue3.md) · [docs/vue2.md](../../docs/vue2.md) · [docs/react.md](../../docs/react.md) · [docs/jquery.md](../../docs/jquery.md)。可运行工程:`demos/apps/{vue3,vue2,react,jquery}`。
|
|
190
62
|
|
|
191
|
-
##
|
|
63
|
+
## /vue3 兼容门面
|
|
192
64
|
|
|
193
|
-
|
|
194
|
-
零框架的本包,模板、事件回调、`ref` 方法调用与业务逻辑一行不动:
|
|
65
|
+
Vue 3 宿主用 `/vue3` 门面即可沿用组件写法(props / 事件 / `ref`),底层复用零框架的本包,模板、事件回调、`ref` 方法调用一行不动。原本消费 `@worm-vue3-print/canvas` 的项目只需改依赖名:
|
|
195
66
|
|
|
196
67
|
```diff
|
|
197
68
|
- "@worm-vue3-print/canvas": "..."
|
|
198
69
|
+ "@worm-print-platform/canvas": "..."
|
|
199
70
|
```
|
|
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
71
|
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
`vue` 是本子路径的 **optional peer**:不用 `/vue3` 时主入口仍是零框架、零 Vue 依赖。
|
|
212
|
-
设计器样式由元素自己注入 Shadow DOM,预览组件样式由门面幂等注入,宿主都不需要额外引 CSS。
|
|
72
|
+
```ts
|
|
73
|
+
import { PrintDesigner, PrintHtmlPreview } from '@worm-print-platform/canvas/vue3'
|
|
74
|
+
import type { TemplateData } from '@worm-print-platform/canvas/vue3'
|
|
75
|
+
```
|
|
213
76
|
|
|
214
|
-
|
|
77
|
+
门面负责的契约转换:
|
|
215
78
|
|
|
216
|
-
| Vue
|
|
217
|
-
|
|
218
|
-
| `:initial-template` `:fields`
|
|
79
|
+
| Vue 组件侧 | Custom Element 侧 | 门面动作 |
|
|
80
|
+
| --- | --- | --- |
|
|
81
|
+
| `:initial-template` / `:fields` / 函数型 `:upload-image` | `el.template` / `el.fields` / `el.uploadImage` | 显式赋 property,绕开 attribute 字符串化 |
|
|
219
82
|
| `@save="onSave"` 首参是 JSON 字符串 | `CustomEvent('save')`,json 在 `detail` | 解 `detail` 再 emit |
|
|
220
|
-
|
|
|
221
|
-
| `ref.getTemplateJson()` 返回对象 | `getTemplateJson()` 返回 JSON 字符串 | `JSON.parse` |
|
|
222
|
-
| `ref.validateTemplate()` / `getGeometryWarnings()` | 同名同语义 | 直接代理 |
|
|
83
|
+
| `ref.getTemplateJson()` 返回对象 | 返回 JSON 字符串 | `JSON.parse` |
|
|
223
84
|
|
|
224
|
-
|
|
85
|
+
详细迁移说明见 [docs/migration-worm-vue3-print.md](../../docs/migration-worm-vue3-print.md)。
|
|
225
86
|
|
|
226
|
-
|
|
87
|
+
## 浏览器直挂(jQuery / 无打包器宿主)
|
|
227
88
|
|
|
228
|
-
`
|
|
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`。
|
|
89
|
+
`vite.bundle.config.ts` 产出 UMD bundle(`@worm-print-platform/canvas/browser` 子路径):
|
|
235
90
|
|
|
236
|
-
|
|
91
|
+
```html
|
|
92
|
+
<link rel="stylesheet" href="/vendor/worm-print-canvas.css" />
|
|
93
|
+
<script src="/vendor/worm-print-canvas.js"></script>
|
|
94
|
+
<script>
|
|
95
|
+
WormPrintCanvas.registerAll()
|
|
96
|
+
const el = document.querySelector('print-designer')
|
|
97
|
+
el.fields = fields
|
|
98
|
+
el.template = templateJson
|
|
99
|
+
el.addEventListener('save', e => persist(e.detail))
|
|
100
|
+
</script>
|
|
101
|
+
```
|
|
237
102
|
|
|
238
|
-
|
|
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
|
-
不是新的正式方案);新接入的宿主统一用本包主入口。
|
|
103
|
+
两个硬约束:自定义元素默认 `display:inline`,宿主必须给它块级宽高;`fields` 只有 setter,读回看 `el.store.fields`。
|
|
248
104
|
|
|
249
|
-
##
|
|
105
|
+
## 宿主契约
|
|
250
106
|
|
|
251
|
-
|
|
252
|
-
对象入参在哪一帧赋 property。下面代码取自 `demos/apps/{vue3,vue2,react}` 三个可运行工程,
|
|
253
|
-
已剥掉示例库与弹层等 demo 噪音。
|
|
107
|
+
`<print-designer>` 的接入面是**版本化**的,定义在 `src/host-contract.ts`:
|
|
254
108
|
|
|
255
|
-
|
|
109
|
+
| 面 | 内容 |
|
|
110
|
+
| --- | --- |
|
|
111
|
+
| attribute | `template` · `fields` · `name` · `fonts` · `design-data`(另 `is-edit` / `show-help` / `show-preview` / `show-save` 四个布尔开关) |
|
|
112
|
+
| property | `template` · `fields` · `fonts` · `designData` · `isEdit` · `showHelp` · `showPreview` · `showSave` · `uploadImage` · `uploadDesignBackground` |
|
|
113
|
+
| 方法 | `getTemplateJson()` · `loadTemplate()` · `save()` · `validateTemplate()` · `getGeometryWarnings()` |
|
|
114
|
+
| 事件(`bubbles + composed`) | `save` / `preview` → 模板 JSON 字符串;`help` → 无载荷;`dblclick-element` → 元素 id;`dblclick-cell` → `{ elementId, r, c, cellId? }` |
|
|
256
115
|
|
|
257
|
-
|
|
258
|
-
> 或要与 Vue 2 / React 共用同一套注入代码的场景。
|
|
116
|
+
自定义外观:外壳 8 个 `::part` 挂钩 + 52 个 `--pd-*` 主题令牌(权威定义在 `@worm-print-platform/core/styles/theme.css`)。契约版本遵守语义化,`src/__tests__/host-contract.spec.ts` 做漂移检测。
|
|
259
117
|
|
|
260
|
-
|
|
261
|
-
// vite.config.js —— SFC 编译期就要声明 print-* 是自定义元素,否则被当未注册组件
|
|
262
|
-
import vue from '@vitejs/plugin-vue'
|
|
118
|
+
`showPreview` / `showSave` 控制工具栏「预览 / 保存」按钮显隐,默认显示,置 `false` 隐藏(Vue 3 门面同名 props)。隐藏只影响按钮出图:`save()` 方法、`save` / `preview` 事件照常可用,宿主可接管打印与预览业务逻辑。
|
|
263
119
|
|
|
264
|
-
|
|
265
|
-
plugins: [vue({
|
|
266
|
-
template: { compilerOptions: { isCustomElement: tag => tag.startsWith('print-') } },
|
|
267
|
-
})],
|
|
268
|
-
})
|
|
269
|
-
```
|
|
120
|
+
## 统一元素注册表
|
|
270
121
|
|
|
271
|
-
|
|
272
|
-
// main.js
|
|
273
|
-
import { createApp } from 'vue'
|
|
274
|
-
import { registerAll } from '@worm-print-platform/canvas'
|
|
122
|
+
内置元素与第三方插件**同权**:
|
|
275
123
|
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
```
|
|
124
|
+
```ts
|
|
125
|
+
import { registerElementDefinition, registerElementRenderer } from '@worm-print-platform/canvas'
|
|
279
126
|
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
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()
|
|
127
|
+
const release = registerElementDefinition({
|
|
128
|
+
type: 'signature', label: '手写签名', icon: 'PenLine',
|
|
129
|
+
material: true, propertyGroup: 'general', dblEditableField: 'src', group: '扩展',
|
|
305
130
|
})
|
|
306
|
-
|
|
131
|
+
registerElementRenderer('signature', (el) => { /* 返回 HTML 字符串或 Node */ })
|
|
132
|
+
// release() 后回到内置基线
|
|
307
133
|
```
|
|
308
134
|
|
|
309
|
-
|
|
310
|
-
只有自带运行时编译器的宿主才用 `app.config` 那条。
|
|
135
|
+
## 自定义元素插件(图表等)
|
|
311
136
|
|
|
312
|
-
|
|
137
|
+
设计器只保留通用机制,具体元素由插件包提供:
|
|
313
138
|
|
|
314
|
-
```
|
|
315
|
-
// main.js
|
|
316
|
-
import Vue from 'vue'
|
|
139
|
+
```ts
|
|
317
140
|
import { registerAll } from '@worm-print-platform/canvas'
|
|
318
|
-
|
|
141
|
+
import '@worm-print-platform/echarts/ui' // 官方图表插件;换自定义插件同理
|
|
319
142
|
registerAll()
|
|
320
|
-
// 必须声明,否则报 Unknown custom element、画面是个空壳
|
|
321
|
-
Vue.config.ignoredElements = ['print-designer']
|
|
322
143
|
|
|
323
|
-
|
|
144
|
+
const el = document.querySelector('print-designer')
|
|
145
|
+
el.fields = [{ fieldKey: 'trend', fieldLabel: '趋势数据', fieldType: 'list' }]
|
|
146
|
+
el.designData = { trend: [{ name: '一月', value: 12 }, { name: '二月', value: 28 }] }
|
|
324
147
|
```
|
|
325
148
|
|
|
326
|
-
|
|
327
|
-
<template>
|
|
328
|
-
<print-designer ref="designer" is-edit="true" show-help="true"
|
|
329
|
-
@save="onSave" @preview="onPreview" @dblclick-cell="onDblclickCell" />
|
|
330
|
-
</template>
|
|
149
|
+
插件写自己的元素见 [`@worm-print-platform/plugin-kit`](../plugin-kit/README.md)。
|
|
331
150
|
|
|
332
|
-
|
|
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
|
-
```
|
|
151
|
+
## 两种接入形态的边界
|
|
353
152
|
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
153
|
+
| | `/vue3` 门面 | 本包主入口 |
|
|
154
|
+
| --- | --- | --- |
|
|
155
|
+
| 运行时 | Vue 3(可选 peer) | 无框架 |
|
|
156
|
+
| 宿主注入 | props / 事件 / `ref` | element property / attribute + CustomEvent |
|
|
157
|
+
| 样式 | 门面幂等注入,无需引样式文件 | 内置 Shadow DOM(`?inline`);light DOM 形态引 `@worm-print-platform/canvas/style.css` |
|
|
158
|
+
| 适用 | 沿用组件写法的 Vue 3 项目 | Vue 3 / Vue 2 / React / jQuery / 原生 |
|
|
359
159
|
|
|
360
|
-
|
|
160
|
+
## 样式生成
|
|
361
161
|
|
|
362
|
-
|
|
363
|
-
所以只剩运行期两条约定。
|
|
162
|
+
样式 / 图标 / 主题令牌的单一来源在 `core/src/styles`,本包从共享源派生(`gen-styles.mjs` 剥 `[data-v-*]` 后补主题令牌,`gen-icons.mjs` 读共享 `icon-manifest.json`);`gen-styles.mjs --check` 做漂移检测,不要手改派生产物。
|
|
364
163
|
|
|
365
|
-
|
|
366
|
-
// main.jsx
|
|
367
|
-
import { registerAll } from '@worm-print-platform/canvas'
|
|
368
|
-
|
|
369
|
-
registerAll() // 一次即可;设计器样式在组件 Shadow DOM 内,宿主不必引 css
|
|
370
|
-
```
|
|
164
|
+
## 开发
|
|
371
165
|
|
|
372
|
-
```
|
|
373
|
-
|
|
374
|
-
|
|
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
|
-
}
|
|
166
|
+
```bash
|
|
167
|
+
npm run build # styles + vite build + tsc
|
|
168
|
+
npm test # vitest(含 host-contract 漂移检测)
|
|
401
169
|
```
|
|
402
170
|
|
|
403
|
-
|
|
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()`。
|
|
171
|
+
## 相关链接
|
|
418
172
|
|
|
419
|
-
|
|
173
|
+
- [根 README 架构分层](https://github.com/worm-longliu/worm-print-platform/blob/main/README.md#架构与分层)
|
|
174
|
+
- [设计器内核(core)](../core/README.md)
|
|
175
|
+
- [插件 UI 契约(plugin-kit)](../plugin-kit/README.md)
|
|
176
|
+
- [ECharts 图表插件](../echarts/README.md)
|
|
177
|
+
- [宿主契约源码](src/host-contract.ts) · [验收清单](PARITY.md)
|
|
420
178
|
|
|
421
|
-
|
|
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 / 原生 |
|
|
179
|
+
## License
|
|
427
180
|
|
|
428
|
-
|
|
181
|
+
[MIT](../../LICENSE)
|
package/dist/browser-bundle.d.ts
CHANGED