@worm-print-platform/plugin-kit 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/README.md +106 -2
- package/dist/index.cjs +140 -0
- package/dist/index.d.cts +100 -0
- package/dist/index.d.ts +100 -0
- package/dist/index.js +105 -0
- package/package.json +45 -4
package/README.md
CHANGED
|
@@ -1,3 +1,107 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @worm-print-platform/plugin-kit
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
打印模板自定义元素的**框架无关设计器 UI 契约与注册表**。
|
|
4
|
+
|
|
5
|
+
插件作者只写一份 DOM 渲染能力(返回 HTML 字符串或 DOM 节点),即可同时被两类宿主消费:
|
|
6
|
+
|
|
7
|
+
| 宿主 | 消费方式 |
|
|
8
|
+
| --- | --- |
|
|
9
|
+
| `@worm-print-platform/canvas` | 零框架原生挂载:素材卡、画布、属性台「自定义属性」页签直接使用 UI |
|
|
10
|
+
| `@worm-vue3-print/canvas` | Vue 3 桥接:`useElementExtensionRegistry` 自动生成画布组件与属性页签组件 |
|
|
11
|
+
|
|
12
|
+
> 状态:随工作区主线开发,**尚未发布到 npm**;通过 workspace / `file:` 依赖接入。
|
|
13
|
+
|
|
14
|
+
## 与 core 插件描述符的关系
|
|
15
|
+
|
|
16
|
+
- **元素语义**(kind 类型、标签、图标、专属属性元数据、默认值、绑定、出图、校验、AI/MCP 可见性)仍由
|
|
17
|
+
`@worm-print-platform/core/designer` 的 `defineElementPlugin` + `registerElementPlugin` 声明。
|
|
18
|
+
- **设计器 UI**(画布长什么样、属性台怎么改)由本包的 `defineElementUI` + `registerElementUI` 声明。
|
|
19
|
+
- 两者用元素 `type` 关联:宿主只展示「core 注册表里有 kind 且 plugin-kit 里有 UI」的元素,避免出现无法渲染的素材卡。
|
|
20
|
+
|
|
21
|
+
## 契约
|
|
22
|
+
|
|
23
|
+
```ts
|
|
24
|
+
import { defineElementUI, registerElementUI } from '@worm-print-platform/plugin-kit'
|
|
25
|
+
|
|
26
|
+
export const MY_ELEMENT_UI = defineElementUI({
|
|
27
|
+
types: ['myElement', 'myElementBold'],
|
|
28
|
+
renderCanvas: ({ element }) => `<svg data-type="${element.printElementType.type}"></svg>`,
|
|
29
|
+
renderProperty: ({ element, update }) => {
|
|
30
|
+
const form = document.createElement('form')
|
|
31
|
+
form.className = 'pd-form'
|
|
32
|
+
// …原生 DOM 构建,控件类名与 canvas / common 的 .pd-* 一致即可复用两侧样式
|
|
33
|
+
const input = document.createElement('input')
|
|
34
|
+
input.className = 'pd-input'
|
|
35
|
+
input.value = String(element.options.myText ?? '')
|
|
36
|
+
input.addEventListener('change', () => update({ myText: input.value }))
|
|
37
|
+
form.appendChild(input)
|
|
38
|
+
return form
|
|
39
|
+
},
|
|
40
|
+
hasProperties: element => element.options.myText !== undefined,
|
|
41
|
+
// 素材卡分组:core 描述符的 designer.materialCard.group 优先,缺省回落到这里
|
|
42
|
+
group: '我的元素',
|
|
43
|
+
searchKeywords: ['我的元素', 'myElement'],
|
|
44
|
+
styles: '.my-element-row { display: flex; gap: 6px; }',
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
// 包入口 import 即注册;返回注销句柄
|
|
48
|
+
registerElementUI(MY_ELEMENT_UI)
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
### 渲染结果
|
|
52
|
+
|
|
53
|
+
`renderCanvas` / `renderProperty` 返回以下三种形态之一:
|
|
54
|
+
|
|
55
|
+
| 形态 | 宿主行为 |
|
|
56
|
+
| --- | --- |
|
|
57
|
+
| `string` | 作为 HTML 注入宿主容器(插件负责保证内容安全、可信) |
|
|
58
|
+
| `Node` | 直接挂载 |
|
|
59
|
+
| `{ node, dispose? }` | 挂载前者的内容,宿主重绘/卸载时调用 `dispose` 释放监听或实例 |
|
|
60
|
+
|
|
61
|
+
### 设计态上下文:`data` 与 `fields`
|
|
62
|
+
|
|
63
|
+
两个渲染函数都会收到 `data`(数组信封,`data[0]` 是数据根对象);属性台还额外收到 `fields`:
|
|
64
|
+
|
|
65
|
+
| 字段 | 来源 | 用途 |
|
|
66
|
+
| --- | --- | --- |
|
|
67
|
+
| `data` | 宿主注入的设计态示例数据;未注入时回落 core 的 `DEFAULT_DEMO_DATA` | 画布预览按真实业务数据出图(图表类插件的柱子/扇区由此而来) |
|
|
68
|
+
| `fields` | 宿主的业务字段清单(`fieldKey` / `fieldLabel` / `fieldType`) | 生成「数据源 / 类目 / 数值字段」下拉,不必从 demo 数据的键里猜 |
|
|
69
|
+
|
|
70
|
+
`@worm-print-platform/canvas` 宿主用 `el.designData = {...}`(或 `design-data` attribute 的 JSON 串)注入示例数据,
|
|
71
|
+
单对象会自动归一成 `[对象]`;`fields` 来自 `el.fields`。未注入时 `fields` 为空数组,插件应回落 `data[0]` 的键。
|
|
72
|
+
Vue 3 的 `@worm-vue3-print/canvas` 属性台透传宿主的 `fields`,画布组件则透传宿主的 `printData`。
|
|
73
|
+
|
|
74
|
+
### 属性写回
|
|
75
|
+
|
|
76
|
+
属性上下文只提供 `update(partial)` / `repaint()`(`data` / `fields` 是只读上下文):
|
|
77
|
+
|
|
78
|
+
- `update(partial)`:宿主把补丁合并进 `element.options`、记一条历史并重绘(canvas 由桥接组件 emit `change`)。
|
|
79
|
+
- `repaint()`:只请求重绘,不写数据、不记历史。
|
|
80
|
+
- 插件不得绕过宿主直接改 store;嵌套字段(如 `chartOption`)由插件归一后整体写回。
|
|
81
|
+
|
|
82
|
+
### 事件建议
|
|
83
|
+
|
|
84
|
+
- **common** 的属性台是整树重绘模型,文本输入用 `change`(失焦/回车提交)而不是 `input`,否则每次提交都会重建 DOM、丢失焦点。
|
|
85
|
+
- **canvas** 的桥接组件对「自身 update 引起的变化」不做重渲染,对撤销/外部程序化修改会重渲染,因此同一份 UI 在两侧都可连续输入。
|
|
86
|
+
|
|
87
|
+
## 注册表 API
|
|
88
|
+
|
|
89
|
+
| API | 说明 |
|
|
90
|
+
| --- | --- |
|
|
91
|
+
| `defineElementUI(ui)` | 校验并返回描述符(不注册) |
|
|
92
|
+
| `registerElementUI(ui)` | 注册;同 type 后注册者覆盖,返回只注销自己这一版的句柄 |
|
|
93
|
+
| `getElementUI(type)` | 按元素类型取 UI |
|
|
94
|
+
| `getElementUIEntries()` | `{ type, ui }[]`,供宿主生成素材卡/属性页签 |
|
|
95
|
+
| `getElementUIs()` | 去重后的 UI 列表(一份 UI 覆盖多 type 时只返回一次) |
|
|
96
|
+
| `getElementUITypes()` | 已注册类型 |
|
|
97
|
+
| `subscribeElementUIRegistry(fn)` | 订阅注册态变化,宿主据此刷新素材台/属性台 |
|
|
98
|
+
| `clearElementUIs()` | 清空(单测/热重载) |
|
|
99
|
+
|
|
100
|
+
注册态挂在 `globalThis` 的固定 `Symbol` 上,与 core 元素插件注册表同一约定,避免 monorepo / 多打包副本
|
|
101
|
+
出现「注册在这份、查询在那份」的漂移。因此 common 的 UMD 直挂形态与插件的 ESM 形态可以共享同一份注册态。
|
|
102
|
+
|
|
103
|
+
## 边界
|
|
104
|
+
|
|
105
|
+
- 本包纯 TS,不依赖 Vue / React / 任何 UI 组件库;DOM 操作只在渲染函数被宿主调用时发生。
|
|
106
|
+
- 插件 UI 用样式串注入宿主样式作用域(common 的 Shadow DOM / canvas 的 `document.head`),不提供独立 CSS 文件。
|
|
107
|
+
- 宿主与插件包都不负责出纸:打印仍走 core 的通用渲染协议(`render.html` / `createRenderer` / `executorBundle`)。
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
TOOLBAR_OVERFLOW_KEY: () => TOOLBAR_OVERFLOW_KEY,
|
|
24
|
+
clearElementUIs: () => clearElementUIs,
|
|
25
|
+
defineElementUI: () => defineElementUI,
|
|
26
|
+
getElementUI: () => getElementUI,
|
|
27
|
+
getElementUIEntries: () => getElementUIEntries,
|
|
28
|
+
getElementUITypes: () => getElementUITypes,
|
|
29
|
+
getElementUIs: () => getElementUIs,
|
|
30
|
+
registerElementUI: () => registerElementUI,
|
|
31
|
+
subscribeElementUIRegistry: () => subscribeElementUIRegistry
|
|
32
|
+
});
|
|
33
|
+
module.exports = __toCommonJS(index_exports);
|
|
34
|
+
|
|
35
|
+
// src/registry.ts
|
|
36
|
+
var REGISTRY_KEY = /* @__PURE__ */ Symbol.for("worm-print-platform.element-ui-registry.v1");
|
|
37
|
+
var host = globalThis;
|
|
38
|
+
function state() {
|
|
39
|
+
const existing = host[REGISTRY_KEY];
|
|
40
|
+
if (existing) return existing;
|
|
41
|
+
const created = { entries: /* @__PURE__ */ new Map(), listeners: /* @__PURE__ */ new Set(), nextSeq: 0 };
|
|
42
|
+
host[REGISTRY_KEY] = created;
|
|
43
|
+
return created;
|
|
44
|
+
}
|
|
45
|
+
function notify() {
|
|
46
|
+
for (const listener of [...state().listeners]) {
|
|
47
|
+
try {
|
|
48
|
+
listener();
|
|
49
|
+
} catch {
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
function assertElementUI(ui) {
|
|
54
|
+
if (!ui || typeof ui !== "object") throw new Error("registerElementUI: \u9700\u8981 ElementUI \u5BF9\u8C61");
|
|
55
|
+
if (!Array.isArray(ui.types) || ui.types.length === 0) {
|
|
56
|
+
throw new Error("registerElementUI: types \u5FC5\u987B\u662F\u975E\u7A7A\u6570\u7EC4");
|
|
57
|
+
}
|
|
58
|
+
for (const type of ui.types) {
|
|
59
|
+
if (!type || typeof type !== "string") {
|
|
60
|
+
throw new Error("registerElementUI: types \u5185\u6BCF\u9879\u5FC5\u987B\u662F\u975E\u7A7A\u5B57\u7B26\u4E32");
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
if (typeof ui.renderCanvas !== "function") {
|
|
64
|
+
throw new Error(`registerElementUI: ${ui.types.join(",")} \u7F3A\u5C11 renderCanvas`);
|
|
65
|
+
}
|
|
66
|
+
if (ui.renderProperty !== void 0 && typeof ui.renderProperty !== "function") {
|
|
67
|
+
throw new Error(`registerElementUI: ${ui.types.join(",")} \u7684 renderProperty \u5FC5\u987B\u662F\u51FD\u6570`);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
function defineElementUI(ui) {
|
|
71
|
+
assertElementUI(ui);
|
|
72
|
+
return ui;
|
|
73
|
+
}
|
|
74
|
+
function registerElementUI(ui) {
|
|
75
|
+
assertElementUI(ui);
|
|
76
|
+
const s = state();
|
|
77
|
+
const seq = s.nextSeq++;
|
|
78
|
+
for (const type of ui.types) s.entries.set(type, { ui, seq });
|
|
79
|
+
notify();
|
|
80
|
+
return () => {
|
|
81
|
+
let removed = false;
|
|
82
|
+
for (const type of ui.types) {
|
|
83
|
+
const current = s.entries.get(type);
|
|
84
|
+
if (current?.ui !== ui) continue;
|
|
85
|
+
s.entries.delete(type);
|
|
86
|
+
removed = true;
|
|
87
|
+
}
|
|
88
|
+
if (removed) notify();
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
function getElementUI(type) {
|
|
92
|
+
if (!type) return void 0;
|
|
93
|
+
return state().entries.get(type)?.ui;
|
|
94
|
+
}
|
|
95
|
+
function getElementUIEntries() {
|
|
96
|
+
const entries = [...state().entries.entries()];
|
|
97
|
+
entries.sort((a, b) => a[1].seq - b[1].seq);
|
|
98
|
+
return entries.map(([type, entry]) => ({ type, ui: entry.ui }));
|
|
99
|
+
}
|
|
100
|
+
function getElementUIs() {
|
|
101
|
+
const seen = /* @__PURE__ */ new Set();
|
|
102
|
+
const out = [];
|
|
103
|
+
for (const { ui } of getElementUIEntries()) {
|
|
104
|
+
if (seen.has(ui)) continue;
|
|
105
|
+
seen.add(ui);
|
|
106
|
+
out.push(ui);
|
|
107
|
+
}
|
|
108
|
+
return out;
|
|
109
|
+
}
|
|
110
|
+
function getElementUITypes() {
|
|
111
|
+
return getElementUIEntries().map((entry) => entry.type);
|
|
112
|
+
}
|
|
113
|
+
function subscribeElementUIRegistry(listener) {
|
|
114
|
+
const s = state();
|
|
115
|
+
s.listeners.add(listener);
|
|
116
|
+
return () => {
|
|
117
|
+
s.listeners.delete(listener);
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
function clearElementUIs() {
|
|
121
|
+
const s = state();
|
|
122
|
+
if (s.entries.size === 0 && s.listeners.size === 0) return;
|
|
123
|
+
s.entries.clear();
|
|
124
|
+
notify();
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// src/toolbar-overflow-key.ts
|
|
128
|
+
var TOOLBAR_OVERFLOW_KEY = /* @__PURE__ */ Symbol.for("worm-print-platform.toolbar-overflow.v1");
|
|
129
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
130
|
+
0 && (module.exports = {
|
|
131
|
+
TOOLBAR_OVERFLOW_KEY,
|
|
132
|
+
clearElementUIs,
|
|
133
|
+
defineElementUI,
|
|
134
|
+
getElementUI,
|
|
135
|
+
getElementUIEntries,
|
|
136
|
+
getElementUITypes,
|
|
137
|
+
getElementUIs,
|
|
138
|
+
registerElementUI,
|
|
139
|
+
subscribeElementUIRegistry
|
|
140
|
+
});
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* @worm-print-platform/plugin-kit 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 { RuntimeElement, PrintBusinessField, ElementOptions } from '@worm-print-platform/core/designer';
|
|
8
|
+
|
|
9
|
+
/** 插件产出的可视内容:HTML 字符串或真实 DOM 节点 */
|
|
10
|
+
type ElementUINode = string | Node;
|
|
11
|
+
/** 带生命周期的渲染结果:宿主在重绘/卸载时调用 dispose 释放监听、图表实例等资源 */
|
|
12
|
+
interface ElementUIResultObject {
|
|
13
|
+
node: ElementUINode;
|
|
14
|
+
dispose?: () => void;
|
|
15
|
+
}
|
|
16
|
+
type ElementUIRenderResult = ElementUINode | ElementUIResultObject;
|
|
17
|
+
/** 画布内渲染上下文(与 canvas 扩展组件的 props 同口径) */
|
|
18
|
+
interface ElementUICanvasContext {
|
|
19
|
+
element: RuntimeElement;
|
|
20
|
+
/** 设计态示例数据:数组信封,data[0] 即数据根对象(与 canvas 扩展组件一致) */
|
|
21
|
+
data?: readonly Record<string, unknown>[];
|
|
22
|
+
/** 设计态为 true;宿主未来做只读预览时可传 false */
|
|
23
|
+
designMode?: boolean;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* 属性台「自定义属性」渲染上下文。
|
|
27
|
+
* 插件只通过 update 写回,宿主负责写入模板、记历史与重绘画布/属性台。
|
|
28
|
+
*/
|
|
29
|
+
interface ElementUIPropertyContext {
|
|
30
|
+
element: RuntimeElement;
|
|
31
|
+
/** 设计态示例数据:数组信封,data[0] 即数据根对象 */
|
|
32
|
+
data?: readonly Record<string, unknown>[];
|
|
33
|
+
/**
|
|
34
|
+
* 宿主的业务字段清单(fieldKey / fieldLabel / fieldType)。
|
|
35
|
+
* 图表类插件据此生成「数据源 / 类目 / 数值字段」下拉,不必再从 demo 数据的键里猜;
|
|
36
|
+
* 未注入时为 undefined 或空数组,插件应自行回落到 data[0] 的键。
|
|
37
|
+
*/
|
|
38
|
+
fields?: readonly PrintBusinessField[];
|
|
39
|
+
/** 把补丁合并进 element.options 并通知宿主(宿主保证同步生效并记一条历史) */
|
|
40
|
+
update(partial: Partial<ElementOptions>): void;
|
|
41
|
+
/** 仅请求宿主重绘,不写数据、不记历史(面板内部态变化用) */
|
|
42
|
+
repaint(): void;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* 一份插件设计器 UI,可覆盖同一插件的多个元素类型。
|
|
46
|
+
* 同一 type 后注册者覆盖前者;注册与注销都是幂等的。
|
|
47
|
+
*/
|
|
48
|
+
interface ElementUI {
|
|
49
|
+
/** 覆盖的元素类型,必须与 core 插件注册表里的 kind.type 对齐 */
|
|
50
|
+
readonly types: readonly string[];
|
|
51
|
+
/** 画布内渲染:返回 HTML 字符串或 DOM 节点 */
|
|
52
|
+
renderCanvas(ctx: ElementUICanvasContext): ElementUIRenderResult;
|
|
53
|
+
/** 属性台「自定义属性」内容;不提供则该元素没有自定义属性页签 */
|
|
54
|
+
renderProperty?(ctx: ElementUIPropertyContext): ElementUIRenderResult;
|
|
55
|
+
/** 实例级判据:返回 false 时不显示自定义属性页签;缺省视为有 renderProperty 即显示 */
|
|
56
|
+
hasProperties?(element: RuntimeElement): boolean;
|
|
57
|
+
/** 素材卡分组(与 core kind.designer.materialCard.group 同义);宿主优先取 core,缺省回落到这里 */
|
|
58
|
+
readonly group?: string;
|
|
59
|
+
/** 属性搜索关键词;缺省由宿主按元素标签/类型名兜底 */
|
|
60
|
+
readonly searchKeywords?: readonly string[];
|
|
61
|
+
/** 宿主注入到自身样式作用域:common 注入 Shadow DOM,canvas 注入 document.head */
|
|
62
|
+
readonly styles?: string;
|
|
63
|
+
}
|
|
64
|
+
/** 类型→UI 的解析视图(供宿主遍历注册态) */
|
|
65
|
+
interface ElementUIEntry {
|
|
66
|
+
type: string;
|
|
67
|
+
ui: ElementUI;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** 描述符恒等函数:给插件作者提供类型检查与可读性,不做隐式注册 */
|
|
71
|
+
declare function defineElementUI(ui: ElementUI): ElementUI;
|
|
72
|
+
/**
|
|
73
|
+
* 注册一份插件 UI(同 type 后注册者覆盖,便于热更新)。
|
|
74
|
+
* 返回注销句柄:只会注销「自己这一版」占用的类型,不误删后来覆盖它的新 UI。
|
|
75
|
+
*/
|
|
76
|
+
declare function registerElementUI(ui: ElementUI): () => void;
|
|
77
|
+
/** 按元素类型取插件 UI;未注册返回 undefined */
|
|
78
|
+
declare function getElementUI(type?: string | null): ElementUI | undefined;
|
|
79
|
+
/** 当前注册态的类型→UI 条目(后注册覆盖、按注册顺序) */
|
|
80
|
+
declare function getElementUIEntries(): ElementUIEntry[];
|
|
81
|
+
/** 已注册的插件 UI 列表(同一份 UI 覆盖多个 type 时只返回一次,按注册顺序) */
|
|
82
|
+
declare function getElementUIs(): ElementUI[];
|
|
83
|
+
/** 已注册的元素类型列表(按注册顺序去重) */
|
|
84
|
+
declare function getElementUITypes(): string[];
|
|
85
|
+
/** 订阅注册态变化;返回取消订阅句柄。宿主可在注册后即时刷新素材台/画布。 */
|
|
86
|
+
declare function subscribeElementUIRegistry(listener: () => void): () => void;
|
|
87
|
+
/** 清空注册态(仅供单测与宿主热重载使用) */
|
|
88
|
+
declare function clearElementUIs(): void;
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* 顶栏「更多」收纳能力的跨实现共享注入键。
|
|
92
|
+
*
|
|
93
|
+
* canvas(Vue 3 composable)与 common(/vue3 兼容门面)各自维护一份同源实现,
|
|
94
|
+
* 但 provide/inject 必须用同一个 key:两处若各自 Symbol(),宿主 App.vue 与
|
|
95
|
+
* DemoTopbarMore.vue 分别 import 不同实现时会静默拿不到上下文。
|
|
96
|
+
* 用 Symbol.for 保证即使被打包进两个 bundle 也是同一个全局 symbol。
|
|
97
|
+
*/
|
|
98
|
+
declare const TOOLBAR_OVERFLOW_KEY: symbol;
|
|
99
|
+
|
|
100
|
+
export { type ElementUI, type ElementUICanvasContext, type ElementUIEntry, type ElementUINode, type ElementUIPropertyContext, type ElementUIRenderResult, type ElementUIResultObject, TOOLBAR_OVERFLOW_KEY, clearElementUIs, defineElementUI, getElementUI, getElementUIEntries, getElementUITypes, getElementUIs, registerElementUI, subscribeElementUIRegistry };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* @worm-print-platform/plugin-kit 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 { RuntimeElement, PrintBusinessField, ElementOptions } from '@worm-print-platform/core/designer';
|
|
8
|
+
|
|
9
|
+
/** 插件产出的可视内容:HTML 字符串或真实 DOM 节点 */
|
|
10
|
+
type ElementUINode = string | Node;
|
|
11
|
+
/** 带生命周期的渲染结果:宿主在重绘/卸载时调用 dispose 释放监听、图表实例等资源 */
|
|
12
|
+
interface ElementUIResultObject {
|
|
13
|
+
node: ElementUINode;
|
|
14
|
+
dispose?: () => void;
|
|
15
|
+
}
|
|
16
|
+
type ElementUIRenderResult = ElementUINode | ElementUIResultObject;
|
|
17
|
+
/** 画布内渲染上下文(与 canvas 扩展组件的 props 同口径) */
|
|
18
|
+
interface ElementUICanvasContext {
|
|
19
|
+
element: RuntimeElement;
|
|
20
|
+
/** 设计态示例数据:数组信封,data[0] 即数据根对象(与 canvas 扩展组件一致) */
|
|
21
|
+
data?: readonly Record<string, unknown>[];
|
|
22
|
+
/** 设计态为 true;宿主未来做只读预览时可传 false */
|
|
23
|
+
designMode?: boolean;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* 属性台「自定义属性」渲染上下文。
|
|
27
|
+
* 插件只通过 update 写回,宿主负责写入模板、记历史与重绘画布/属性台。
|
|
28
|
+
*/
|
|
29
|
+
interface ElementUIPropertyContext {
|
|
30
|
+
element: RuntimeElement;
|
|
31
|
+
/** 设计态示例数据:数组信封,data[0] 即数据根对象 */
|
|
32
|
+
data?: readonly Record<string, unknown>[];
|
|
33
|
+
/**
|
|
34
|
+
* 宿主的业务字段清单(fieldKey / fieldLabel / fieldType)。
|
|
35
|
+
* 图表类插件据此生成「数据源 / 类目 / 数值字段」下拉,不必再从 demo 数据的键里猜;
|
|
36
|
+
* 未注入时为 undefined 或空数组,插件应自行回落到 data[0] 的键。
|
|
37
|
+
*/
|
|
38
|
+
fields?: readonly PrintBusinessField[];
|
|
39
|
+
/** 把补丁合并进 element.options 并通知宿主(宿主保证同步生效并记一条历史) */
|
|
40
|
+
update(partial: Partial<ElementOptions>): void;
|
|
41
|
+
/** 仅请求宿主重绘,不写数据、不记历史(面板内部态变化用) */
|
|
42
|
+
repaint(): void;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* 一份插件设计器 UI,可覆盖同一插件的多个元素类型。
|
|
46
|
+
* 同一 type 后注册者覆盖前者;注册与注销都是幂等的。
|
|
47
|
+
*/
|
|
48
|
+
interface ElementUI {
|
|
49
|
+
/** 覆盖的元素类型,必须与 core 插件注册表里的 kind.type 对齐 */
|
|
50
|
+
readonly types: readonly string[];
|
|
51
|
+
/** 画布内渲染:返回 HTML 字符串或 DOM 节点 */
|
|
52
|
+
renderCanvas(ctx: ElementUICanvasContext): ElementUIRenderResult;
|
|
53
|
+
/** 属性台「自定义属性」内容;不提供则该元素没有自定义属性页签 */
|
|
54
|
+
renderProperty?(ctx: ElementUIPropertyContext): ElementUIRenderResult;
|
|
55
|
+
/** 实例级判据:返回 false 时不显示自定义属性页签;缺省视为有 renderProperty 即显示 */
|
|
56
|
+
hasProperties?(element: RuntimeElement): boolean;
|
|
57
|
+
/** 素材卡分组(与 core kind.designer.materialCard.group 同义);宿主优先取 core,缺省回落到这里 */
|
|
58
|
+
readonly group?: string;
|
|
59
|
+
/** 属性搜索关键词;缺省由宿主按元素标签/类型名兜底 */
|
|
60
|
+
readonly searchKeywords?: readonly string[];
|
|
61
|
+
/** 宿主注入到自身样式作用域:common 注入 Shadow DOM,canvas 注入 document.head */
|
|
62
|
+
readonly styles?: string;
|
|
63
|
+
}
|
|
64
|
+
/** 类型→UI 的解析视图(供宿主遍历注册态) */
|
|
65
|
+
interface ElementUIEntry {
|
|
66
|
+
type: string;
|
|
67
|
+
ui: ElementUI;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** 描述符恒等函数:给插件作者提供类型检查与可读性,不做隐式注册 */
|
|
71
|
+
declare function defineElementUI(ui: ElementUI): ElementUI;
|
|
72
|
+
/**
|
|
73
|
+
* 注册一份插件 UI(同 type 后注册者覆盖,便于热更新)。
|
|
74
|
+
* 返回注销句柄:只会注销「自己这一版」占用的类型,不误删后来覆盖它的新 UI。
|
|
75
|
+
*/
|
|
76
|
+
declare function registerElementUI(ui: ElementUI): () => void;
|
|
77
|
+
/** 按元素类型取插件 UI;未注册返回 undefined */
|
|
78
|
+
declare function getElementUI(type?: string | null): ElementUI | undefined;
|
|
79
|
+
/** 当前注册态的类型→UI 条目(后注册覆盖、按注册顺序) */
|
|
80
|
+
declare function getElementUIEntries(): ElementUIEntry[];
|
|
81
|
+
/** 已注册的插件 UI 列表(同一份 UI 覆盖多个 type 时只返回一次,按注册顺序) */
|
|
82
|
+
declare function getElementUIs(): ElementUI[];
|
|
83
|
+
/** 已注册的元素类型列表(按注册顺序去重) */
|
|
84
|
+
declare function getElementUITypes(): string[];
|
|
85
|
+
/** 订阅注册态变化;返回取消订阅句柄。宿主可在注册后即时刷新素材台/画布。 */
|
|
86
|
+
declare function subscribeElementUIRegistry(listener: () => void): () => void;
|
|
87
|
+
/** 清空注册态(仅供单测与宿主热重载使用) */
|
|
88
|
+
declare function clearElementUIs(): void;
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* 顶栏「更多」收纳能力的跨实现共享注入键。
|
|
92
|
+
*
|
|
93
|
+
* canvas(Vue 3 composable)与 common(/vue3 兼容门面)各自维护一份同源实现,
|
|
94
|
+
* 但 provide/inject 必须用同一个 key:两处若各自 Symbol(),宿主 App.vue 与
|
|
95
|
+
* DemoTopbarMore.vue 分别 import 不同实现时会静默拿不到上下文。
|
|
96
|
+
* 用 Symbol.for 保证即使被打包进两个 bundle 也是同一个全局 symbol。
|
|
97
|
+
*/
|
|
98
|
+
declare const TOOLBAR_OVERFLOW_KEY: symbol;
|
|
99
|
+
|
|
100
|
+
export { type ElementUI, type ElementUICanvasContext, type ElementUIEntry, type ElementUINode, type ElementUIPropertyContext, type ElementUIRenderResult, type ElementUIResultObject, TOOLBAR_OVERFLOW_KEY, clearElementUIs, defineElementUI, getElementUI, getElementUIEntries, getElementUITypes, getElementUIs, registerElementUI, subscribeElementUIRegistry };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
// src/registry.ts
|
|
2
|
+
var REGISTRY_KEY = /* @__PURE__ */ Symbol.for("worm-print-platform.element-ui-registry.v1");
|
|
3
|
+
var host = globalThis;
|
|
4
|
+
function state() {
|
|
5
|
+
const existing = host[REGISTRY_KEY];
|
|
6
|
+
if (existing) return existing;
|
|
7
|
+
const created = { entries: /* @__PURE__ */ new Map(), listeners: /* @__PURE__ */ new Set(), nextSeq: 0 };
|
|
8
|
+
host[REGISTRY_KEY] = created;
|
|
9
|
+
return created;
|
|
10
|
+
}
|
|
11
|
+
function notify() {
|
|
12
|
+
for (const listener of [...state().listeners]) {
|
|
13
|
+
try {
|
|
14
|
+
listener();
|
|
15
|
+
} catch {
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
function assertElementUI(ui) {
|
|
20
|
+
if (!ui || typeof ui !== "object") throw new Error("registerElementUI: \u9700\u8981 ElementUI \u5BF9\u8C61");
|
|
21
|
+
if (!Array.isArray(ui.types) || ui.types.length === 0) {
|
|
22
|
+
throw new Error("registerElementUI: types \u5FC5\u987B\u662F\u975E\u7A7A\u6570\u7EC4");
|
|
23
|
+
}
|
|
24
|
+
for (const type of ui.types) {
|
|
25
|
+
if (!type || typeof type !== "string") {
|
|
26
|
+
throw new Error("registerElementUI: types \u5185\u6BCF\u9879\u5FC5\u987B\u662F\u975E\u7A7A\u5B57\u7B26\u4E32");
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
if (typeof ui.renderCanvas !== "function") {
|
|
30
|
+
throw new Error(`registerElementUI: ${ui.types.join(",")} \u7F3A\u5C11 renderCanvas`);
|
|
31
|
+
}
|
|
32
|
+
if (ui.renderProperty !== void 0 && typeof ui.renderProperty !== "function") {
|
|
33
|
+
throw new Error(`registerElementUI: ${ui.types.join(",")} \u7684 renderProperty \u5FC5\u987B\u662F\u51FD\u6570`);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
function defineElementUI(ui) {
|
|
37
|
+
assertElementUI(ui);
|
|
38
|
+
return ui;
|
|
39
|
+
}
|
|
40
|
+
function registerElementUI(ui) {
|
|
41
|
+
assertElementUI(ui);
|
|
42
|
+
const s = state();
|
|
43
|
+
const seq = s.nextSeq++;
|
|
44
|
+
for (const type of ui.types) s.entries.set(type, { ui, seq });
|
|
45
|
+
notify();
|
|
46
|
+
return () => {
|
|
47
|
+
let removed = false;
|
|
48
|
+
for (const type of ui.types) {
|
|
49
|
+
const current = s.entries.get(type);
|
|
50
|
+
if (current?.ui !== ui) continue;
|
|
51
|
+
s.entries.delete(type);
|
|
52
|
+
removed = true;
|
|
53
|
+
}
|
|
54
|
+
if (removed) notify();
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
function getElementUI(type) {
|
|
58
|
+
if (!type) return void 0;
|
|
59
|
+
return state().entries.get(type)?.ui;
|
|
60
|
+
}
|
|
61
|
+
function getElementUIEntries() {
|
|
62
|
+
const entries = [...state().entries.entries()];
|
|
63
|
+
entries.sort((a, b) => a[1].seq - b[1].seq);
|
|
64
|
+
return entries.map(([type, entry]) => ({ type, ui: entry.ui }));
|
|
65
|
+
}
|
|
66
|
+
function getElementUIs() {
|
|
67
|
+
const seen = /* @__PURE__ */ new Set();
|
|
68
|
+
const out = [];
|
|
69
|
+
for (const { ui } of getElementUIEntries()) {
|
|
70
|
+
if (seen.has(ui)) continue;
|
|
71
|
+
seen.add(ui);
|
|
72
|
+
out.push(ui);
|
|
73
|
+
}
|
|
74
|
+
return out;
|
|
75
|
+
}
|
|
76
|
+
function getElementUITypes() {
|
|
77
|
+
return getElementUIEntries().map((entry) => entry.type);
|
|
78
|
+
}
|
|
79
|
+
function subscribeElementUIRegistry(listener) {
|
|
80
|
+
const s = state();
|
|
81
|
+
s.listeners.add(listener);
|
|
82
|
+
return () => {
|
|
83
|
+
s.listeners.delete(listener);
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
function clearElementUIs() {
|
|
87
|
+
const s = state();
|
|
88
|
+
if (s.entries.size === 0 && s.listeners.size === 0) return;
|
|
89
|
+
s.entries.clear();
|
|
90
|
+
notify();
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// src/toolbar-overflow-key.ts
|
|
94
|
+
var TOOLBAR_OVERFLOW_KEY = /* @__PURE__ */ Symbol.for("worm-print-platform.toolbar-overflow.v1");
|
|
95
|
+
export {
|
|
96
|
+
TOOLBAR_OVERFLOW_KEY,
|
|
97
|
+
clearElementUIs,
|
|
98
|
+
defineElementUI,
|
|
99
|
+
getElementUI,
|
|
100
|
+
getElementUIEntries,
|
|
101
|
+
getElementUITypes,
|
|
102
|
+
getElementUIs,
|
|
103
|
+
registerElementUI,
|
|
104
|
+
subscribeElementUIRegistry
|
|
105
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,47 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@worm-print-platform/plugin-kit",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"description": "
|
|
6
|
-
|
|
3
|
+
"version": "1.5.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "【尚未发布】打印模板自定义元素的框架无关设计器 UI 契约与注册表:插件写一份 DOM 渲染能力,common(原生)与 canvas(Vue 桥接)同时消费",
|
|
6
|
+
"main": "./dist/index.cjs",
|
|
7
|
+
"module": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.js",
|
|
13
|
+
"require": "./dist/index.cjs"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist"
|
|
18
|
+
],
|
|
19
|
+
"license": "MIT",
|
|
20
|
+
"author": "worm <liu_long_qqcom@qq.com>",
|
|
21
|
+
"repository": {
|
|
22
|
+
"type": "git",
|
|
23
|
+
"url": "git+https://github.com/worm-longliu/worm-print-platform.git"
|
|
24
|
+
},
|
|
25
|
+
"keywords": [
|
|
26
|
+
"print",
|
|
27
|
+
"template",
|
|
28
|
+
"designer",
|
|
29
|
+
"plugin",
|
|
30
|
+
"framework-agnostic"
|
|
31
|
+
],
|
|
32
|
+
"scripts": {
|
|
33
|
+
"dev": "tsup --watch",
|
|
34
|
+
"build": "tsup && node ../../scripts/inject-license-banner.mjs dist",
|
|
35
|
+
"test": "vitest run",
|
|
36
|
+
"test:watch": "vitest"
|
|
37
|
+
},
|
|
38
|
+
"dependencies": {
|
|
39
|
+
"@worm-print-platform/core": "^1.5.0"
|
|
40
|
+
},
|
|
41
|
+
"devDependencies": {
|
|
42
|
+
"happy-dom": "^20.10.6",
|
|
43
|
+
"tsup": "^8.0.0",
|
|
44
|
+
"typescript": "~5.9.3",
|
|
45
|
+
"vitest": "^1.6.0"
|
|
46
|
+
}
|
|
47
|
+
}
|