@worm-print-platform/canvas 1.5.0 → 1.5.3
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 +124 -355
- package/dist/browser-bundle.d.ts +1 -1
- package/dist/canvas.css +2 -2
- package/dist/canvas.js +119 -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 +24 -8
- package/dist/index.cjs +49 -11
- package/dist/index.d.ts +2 -2
- package/dist/index.js +1918 -1678
- package/dist/print-designer.d.ts +32 -5
- 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/theme-tokens.d.ts +21 -0
- package/dist/version.d.ts +8 -0
- package/dist/vue3/PrintDesigner.d.ts +46 -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 +153 -133
- package/dist/vue3/previewStyle.d.ts +1 -1
- package/dist/vue3/toolbarOverflow.d.ts +1 -1
- package/package.json +5 -5
package/README.md
CHANGED
|
@@ -1,428 +1,197 @@
|
|
|
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.3-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>
|
|
46
|
+
<print-designer id="d" style="display:block;height:100%"></print-designer>
|
|
84
47
|
```
|
|
85
48
|
|
|
86
49
|
```js
|
|
87
|
-
const
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
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)
|
|
96
59
|
```
|
|
97
60
|
|
|
98
|
-
|
|
99
|
-
桌面静默打印),与 canvas(Vue 3)宿主完全一致。
|
|
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}`。
|
|
100
62
|
|
|
101
|
-
##
|
|
63
|
+
## /vue3 兼容门面
|
|
102
64
|
|
|
103
|
-
|
|
65
|
+
Vue 3 宿主用 `/vue3` 门面即可沿用组件写法(props / 事件 / `ref`),底层复用零框架的本包,模板、事件回调、`ref` 方法调用一行不动。原本消费 `@worm-vue3-print/canvas` 的项目只需改依赖名:
|
|
104
66
|
|
|
105
|
-
```
|
|
106
|
-
|
|
107
|
-
|
|
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 */
|
|
67
|
+
```diff
|
|
68
|
+
- "@worm-vue3-print/canvas": "..."
|
|
69
|
+
+ "@worm-print-platform/canvas": "..."
|
|
138
70
|
```
|
|
139
71
|
|
|
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
72
|
```ts
|
|
155
|
-
import {
|
|
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() 后素材卡与渲染一并回到内置基线
|
|
73
|
+
import { PrintDesigner, PrintHtmlPreview } from '@worm-print-platform/canvas/vue3'
|
|
74
|
+
import type { TemplateData } from '@worm-print-platform/canvas/vue3'
|
|
163
75
|
```
|
|
164
76
|
|
|
165
|
-
|
|
166
|
-
注册态变化会通知已挂载的设计器即时重绘。
|
|
167
|
-
|
|
168
|
-
## 自定义元素插件(图表等)
|
|
77
|
+
门面负责的契约转换:
|
|
169
78
|
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
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
|
-
```
|
|
79
|
+
| Vue 组件侧 | Custom Element 侧 | 门面动作 |
|
|
80
|
+
| --- | --- | --- |
|
|
81
|
+
| `:initial-template` / `:fields` / 函数型 `:upload-image` | `el.template` / `el.fields` / `el.uploadImage` | 显式赋 property,绕开 attribute 字符串化 |
|
|
82
|
+
| `@save="onSave"` 首参是 JSON 字符串 | `CustomEvent('save')`,json 在 `detail` | 解 `detail` 再 emit |
|
|
83
|
+
| `ref.getTemplateJson()` 返回对象 | 返回 JSON 字符串 | `JSON.parse` |
|
|
186
84
|
|
|
187
|
-
|
|
188
|
-
图表类插件据此按真实业务数据画预览、按业务字段生成数据源下拉。未注册 UI 的插件不会出现在素材台。
|
|
189
|
-
写自己的插件见 `packages/plugin-kit/README.md`。
|
|
85
|
+
详细迁移说明见 [docs/migration-worm-vue3-print.md](../../docs/migration-worm-vue3-print.md)。
|
|
190
86
|
|
|
191
|
-
##
|
|
87
|
+
## 浏览器直挂(jQuery / 无打包器宿主)
|
|
192
88
|
|
|
193
|
-
|
|
194
|
-
零框架的本包,模板、事件回调、`ref` 方法调用与业务逻辑一行不动:
|
|
89
|
+
`vite.bundle.config.ts` 产出 UMD bundle(`@worm-print-platform/canvas/browser` 子路径):
|
|
195
90
|
|
|
196
|
-
```
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
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>
|
|
205
101
|
```
|
|
206
102
|
|
|
207
|
-
|
|
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()` | 同名同语义 | 直接代理 |
|
|
103
|
+
两个硬约束:自定义元素默认 `display:inline`,宿主必须给它块级宽高;`fields` 只有 setter,读回看 `el.store.fields`。
|
|
223
104
|
|
|
224
|
-
|
|
105
|
+
## 宿主契约
|
|
225
106
|
|
|
226
|
-
|
|
107
|
+
`<print-designer>` 的接入面是**版本化**的,定义在 `src/host-contract.ts`:
|
|
227
108
|
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
`
|
|
231
|
-
`
|
|
232
|
-
|
|
233
|
-
`
|
|
234
|
-
TS 宿主沿用原写法即可通过 `vue-tsc`。
|
|
109
|
+
| 面 | 内容 |
|
|
110
|
+
| --- | --- |
|
|
111
|
+
| attribute | `template` · `fields` · `name` · `fonts` · `design-data` · `theme` · `theme-config`(另 `is-edit` / `show-help` / `show-preview` / `show-save` 四个布尔开关) |
|
|
112
|
+
| property | `template` · `fields` · `fonts` · `designData` · `isEdit` · `showHelp` · `showPreview` · `showSave` · `theme` · `themeConfig` · `uploadImage` · `uploadDesignBackground` |
|
|
113
|
+
| 方法 | `getTemplateJson()` · `loadTemplate()` · `save()` · `validateTemplate()` · `getGeometryWarnings()` |
|
|
114
|
+
| 事件(`bubbles + composed`) | `save` / `preview` → 模板 JSON 字符串;`help` → 无载荷;`dblclick-element` → 元素 id;`dblclick-cell` → `{ elementId, r, c, cellId? }` |
|
|
235
115
|
|
|
236
|
-
|
|
116
|
+
自定义外观:外壳 8 个 `::part` 挂钩 + 55 个 `--pd-*` 主题令牌(权威定义在 `@worm-print-platform/core/styles/theme.css`)。契约版本遵守语义化,`src/__tests__/host-contract.spec.ts` 做漂移检测。
|
|
237
117
|
|
|
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
|
-
不是新的正式方案);新接入的宿主统一用本包主入口。
|
|
118
|
+
主题配色有两条通道,且**参数优先于 CSS 令牌**:
|
|
248
119
|
|
|
249
|
-
|
|
120
|
+
- CSS:宿主覆盖 `--pd-*` 变量(注意 Shadow DOM 内 `.designer-container` 的令牌声明会遮蔽外部值);
|
|
121
|
+
- 参数:`theme="auto" | "light" | "dark"` 选预设,`theme-config` 传细粒度色值覆盖。
|
|
250
122
|
|
|
251
|
-
|
|
252
|
-
对象入参在哪一帧赋 property。下面代码取自 `demos/apps/{vue3,vue2,react}` 三个可运行工程,
|
|
253
|
-
已剥掉示例库与弹层等 demo 噪音。
|
|
123
|
+
优先级:`theme-config.overrides` > 显式 `theme="dark"|"light"` > `auto` 跟随系统 `prefers-color-scheme` > CSS 令牌 > 内置浅色。内置暗色预设只改设计器外壳,纸张、画布背景、标尺与手柄保持原值,预览所见即所得不受影响;宿主如需连画布外围一起换色,可显式覆盖 `--pd-canvas-bg`。
|
|
254
124
|
|
|
255
|
-
|
|
125
|
+
```html
|
|
126
|
+
<print-designer
|
|
127
|
+
theme="dark"
|
|
128
|
+
theme-config='{"overrides":{"--pd-bg":"#0f1115","--pd-surface":"#181b21","--pd-text":"#e6e8ee"}}'>
|
|
129
|
+
</print-designer>
|
|
130
|
+
```
|
|
256
131
|
|
|
257
|
-
|
|
258
|
-
> 或要与 Vue 2 / React 共用同一套注入代码的场景。
|
|
132
|
+
Vue 3 门面同名 props:`theme="dark"` + `:theme-config="{ overrides: { ... } }"`。
|
|
259
133
|
|
|
260
|
-
|
|
261
|
-
// vite.config.js —— SFC 编译期就要声明 print-* 是自定义元素,否则被当未注册组件
|
|
262
|
-
import vue from '@vitejs/plugin-vue'
|
|
134
|
+
`showPreview` / `showSave` 控制工具栏「预览 / 保存」按钮显隐,默认显示,置 `false` 隐藏(Vue 3 门面同名 props)。隐藏只影响按钮出图:`save()` 方法、`save` / `preview` 事件照常可用,宿主可接管打印与预览业务逻辑。
|
|
263
135
|
|
|
264
|
-
|
|
265
|
-
plugins: [vue({
|
|
266
|
-
template: { compilerOptions: { isCustomElement: tag => tag.startsWith('print-') } },
|
|
267
|
-
})],
|
|
268
|
-
})
|
|
269
|
-
```
|
|
136
|
+
## 统一元素注册表
|
|
270
137
|
|
|
271
|
-
|
|
272
|
-
// main.js
|
|
273
|
-
import { createApp } from 'vue'
|
|
274
|
-
import { registerAll } from '@worm-print-platform/canvas'
|
|
138
|
+
内置元素与第三方插件**同权**:
|
|
275
139
|
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
```
|
|
140
|
+
```ts
|
|
141
|
+
import { registerElementDefinition, registerElementRenderer } from '@worm-print-platform/canvas'
|
|
279
142
|
|
|
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()
|
|
143
|
+
const release = registerElementDefinition({
|
|
144
|
+
type: 'signature', label: '手写签名', icon: 'PenLine',
|
|
145
|
+
material: true, propertyGroup: 'general', dblEditableField: 'src', group: '扩展',
|
|
305
146
|
})
|
|
306
|
-
|
|
147
|
+
registerElementRenderer('signature', (el) => { /* 返回 HTML 字符串或 Node */ })
|
|
148
|
+
// release() 后回到内置基线
|
|
307
149
|
```
|
|
308
150
|
|
|
309
|
-
|
|
310
|
-
只有自带运行时编译器的宿主才用 `app.config` 那条。
|
|
151
|
+
## 自定义元素插件(图表等)
|
|
311
152
|
|
|
312
|
-
|
|
153
|
+
设计器只保留通用机制,具体元素由插件包提供:
|
|
313
154
|
|
|
314
|
-
```
|
|
315
|
-
// main.js
|
|
316
|
-
import Vue from 'vue'
|
|
155
|
+
```ts
|
|
317
156
|
import { registerAll } from '@worm-print-platform/canvas'
|
|
318
|
-
|
|
157
|
+
import '@worm-print-platform/echarts/ui' // 官方图表插件;换自定义插件同理
|
|
319
158
|
registerAll()
|
|
320
|
-
// 必须声明,否则报 Unknown custom element、画面是个空壳
|
|
321
|
-
Vue.config.ignoredElements = ['print-designer']
|
|
322
159
|
|
|
323
|
-
|
|
160
|
+
const el = document.querySelector('print-designer')
|
|
161
|
+
el.fields = [{ fieldKey: 'trend', fieldLabel: '趋势数据', fieldType: 'list' }]
|
|
162
|
+
el.designData = { trend: [{ name: '一月', value: 12 }, { name: '二月', value: 28 }] }
|
|
324
163
|
```
|
|
325
164
|
|
|
326
|
-
|
|
327
|
-
<template>
|
|
328
|
-
<print-designer ref="designer" is-edit="true" show-help="true"
|
|
329
|
-
@save="onSave" @preview="onPreview" @dblclick-cell="onDblclickCell" />
|
|
330
|
-
</template>
|
|
165
|
+
插件写自己的元素见 [`@worm-print-platform/plugin-kit`](../plugin-kit/README.md)。
|
|
331
166
|
|
|
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
|
-
```
|
|
167
|
+
## 两种接入形态的边界
|
|
353
168
|
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
169
|
+
| | `/vue3` 门面 | 本包主入口 |
|
|
170
|
+
| --- | --- | --- |
|
|
171
|
+
| 运行时 | Vue 3(可选 peer) | 无框架 |
|
|
172
|
+
| 宿主注入 | props / 事件 / `ref` | element property / attribute + CustomEvent |
|
|
173
|
+
| 样式 | 门面幂等注入,无需引样式文件 | 内置 Shadow DOM(`?inline`);light DOM 形态引 `@worm-print-platform/canvas/style.css` |
|
|
174
|
+
| 适用 | 沿用组件写法的 Vue 3 项目 | Vue 3 / Vue 2 / React / jQuery / 原生 |
|
|
359
175
|
|
|
360
|
-
|
|
176
|
+
## 样式生成
|
|
361
177
|
|
|
362
|
-
|
|
363
|
-
所以只剩运行期两条约定。
|
|
178
|
+
样式 / 图标 / 主题令牌的单一来源在 `core/src/styles`,本包从共享源派生(`gen-styles.mjs` 剥 `[data-v-*]` 后补主题令牌,`gen-icons.mjs` 读共享 `icon-manifest.json`);`gen-styles.mjs --check` 做漂移检测,不要手改派生产物。
|
|
364
179
|
|
|
365
|
-
|
|
366
|
-
// main.jsx
|
|
367
|
-
import { registerAll } from '@worm-print-platform/canvas'
|
|
368
|
-
|
|
369
|
-
registerAll() // 一次即可;设计器样式在组件 Shadow DOM 内,宿主不必引 css
|
|
370
|
-
```
|
|
180
|
+
## 开发
|
|
371
181
|
|
|
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
|
-
}
|
|
182
|
+
```bash
|
|
183
|
+
npm run build # styles + vite build + tsc
|
|
184
|
+
npm test # vitest(含 host-contract 漂移检测)
|
|
401
185
|
```
|
|
402
186
|
|
|
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()`。
|
|
187
|
+
## 相关链接
|
|
418
188
|
|
|
419
|
-
|
|
189
|
+
- [根 README 架构分层](https://github.com/worm-longliu/worm-print-platform/blob/main/README.md#架构与分层)
|
|
190
|
+
- [设计器内核(core)](../core/README.md)
|
|
191
|
+
- [插件 UI 契约(plugin-kit)](../plugin-kit/README.md)
|
|
192
|
+
- [ECharts 图表插件](../echarts/README.md)
|
|
193
|
+
- [宿主契约源码](src/host-contract.ts) · [验收清单](PARITY.md)
|
|
420
194
|
|
|
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 / 原生 |
|
|
195
|
+
## License
|
|
427
196
|
|
|
428
|
-
|
|
197
|
+
[MIT](../../LICENSE)
|
package/dist/browser-bundle.d.ts
CHANGED