@worm-print-platform/plugin-kit 0.0.0-stage → 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 CHANGED
@@ -1,3 +1,121 @@
1
- # Temporary Holding Version
1
+ <p align="center">
2
+ <strong>@worm-print-platform/plugin-kit</strong><br>
3
+ <em>插件设计器 UI 契约与注册表(框架无关)</em>
4
+ </p>
2
5
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
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
+ </p>
10
+
11
+ > monorepo 根:[worm-print-platform](https://github.com/worm-longliu/worm-print-platform)
12
+
13
+ 自定义元素的**框架无关设计器 UI 契约与注册表**。插件作者只写一份 DOM 渲染能力(返回 HTML 字符串或 DOM 节点),即可同时被两类宿主消费:
14
+
15
+ | 宿主 | 消费方式 |
16
+ | --- | --- |
17
+ | `@worm-print-platform/canvas` | 零框架原生挂载:素材卡、画布、属性台「自定义属性」页签直接使用 UI |
18
+ | `@worm-print-platform/canvas/vue3` | Vue 3 门面:复用同一份 UI 契约 |
19
+
20
+ ## 与 core 插件描述符的关系
21
+
22
+ - **元素语义**(kind 类型、标签、图标、默认值、绑定、出图、校验)由 `@worm-print-platform/core/designer` 的 `defineElementPlugin` + `registerElementPlugin` 声明。
23
+ - **设计器 UI**(画布渲染什么样、属性台怎么改)由本包的 `defineElementUI` + `registerElementUI` 声明。
24
+ - 两者用元素 `type` 关联:宿主只展示「core 注册表里有 kind 且 plugin-kit 里有 UI」的元素,避免出现无法渲染的素材卡。
25
+
26
+ ## 安装
27
+
28
+ ```bash
29
+ npm i @worm-print-platform/plugin-kit
30
+ ```
31
+
32
+ ## 契约与使用
33
+
34
+ ```ts
35
+ import { defineElementUI, registerElementUI } from '@worm-print-platform/plugin-kit'
36
+
37
+ export const MY_ELEMENT_UI = defineElementUI({
38
+ types: ['myElement', 'myElementBold'],
39
+ renderCanvas: ({ element }) => `<svg data-type="${element.printElementType.type}"></svg>`,
40
+ renderProperty: ({ element, update }) => {
41
+ const form = document.createElement('form')
42
+ form.className = 'pd-form'
43
+ const input = document.createElement('input')
44
+ input.className = 'pd-input'
45
+ input.value = String(element.options.myText ?? '')
46
+ input.addEventListener('change', () => update({ myText: input.value }))
47
+ form.appendChild(input)
48
+ return form
49
+ },
50
+ hasProperties: element => element.options.myText !== undefined,
51
+ group: '我的元素',
52
+ searchKeywords: ['我的元素', 'myElement'],
53
+ styles: '.my-element-row { display: flex; gap: 6px; }',
54
+ })
55
+
56
+ registerElementUI(MY_ELEMENT_UI) // 包入口 import 即注册
57
+ ```
58
+
59
+ ### 渲染结果
60
+
61
+ `renderCanvas` / `renderProperty` 返回以下三种形态之一:
62
+
63
+ | 形态 | 宿主行为 |
64
+ | --- | --- |
65
+ | `string` | 作为 HTML 注入宿主容器(插件负责保证内容安全、可信) |
66
+ | `Node` | 直接挂载 |
67
+ | `{ node, dispose? }` | 挂载前者的内容,宿主重绘/卸载时调用 `dispose` 释放监听或实例 |
68
+
69
+ ### 设计态上下文:`data` 与 `fields`
70
+
71
+ | 字段 | 来源 | 用途 |
72
+ | --- | --- | --- |
73
+ | `data` | 宿主注入的设计态示例数据;未注入时回落 core 的 `DEFAULT_DEMO_DATA` | 画布预览按真实业务数据出图(图表类插件的柱子/扇区由此而来) |
74
+ | `fields` | 宿主的业务字段清单(`fieldKey` / `fieldLabel` / `fieldType`) | 生成「数据源 / 类目 / 数值字段」下拉 |
75
+
76
+ ### 属性写回
77
+
78
+ 属性上下文只提供 `update(partial)` / `repaint()`(`data` / `fields` 只读):
79
+
80
+ - `update(partial)`:宿主把补丁合并进 `element.options`、记一条历史并重绘。
81
+ - `repaint()`:只请求重绘,不写数据、不记历史。
82
+ - 插件不得绕过宿主直接改 store。
83
+
84
+ ## 注册表 API
85
+
86
+ | API | 说明 |
87
+ | --- | --- |
88
+ | `defineElementUI(ui)` | 校验并返回描述符(不注册) |
89
+ | `registerElementUI(ui)` | 注册;同 type 后注册者覆盖,返回只注销自己这一版的句柄 |
90
+ | `getElementUI(type)` | 按元素类型取 UI |
91
+ | `getElementUIEntries()` | `{ type, ui }[]`,供宿主生成素材卡/属性页签 |
92
+ | `subscribeElementUIRegistry(fn)` | 订阅注册态变化,宿主据此刷新素材台/属性台 |
93
+ | `clearElementUIs()` | 清空(单测/热重载) |
94
+
95
+ 注册态挂在 `globalThis` 的固定 `Symbol` 上,与 core 元素插件注册表同一约定,避免多打包副本漂移。
96
+
97
+ ## 边界
98
+
99
+ - 本包纯 TS,不依赖 Vue / React / 任何 UI 组件库;DOM 操作只在渲染函数被宿主调用时发生。
100
+ - 插件 UI 用样式串注入宿主样式作用域,不提供独立 CSS 文件。
101
+ - 出纸仍走 `@worm-print-platform/core` 的通用渲染协议,本包不负责。
102
+
103
+ ## 开发
104
+
105
+ ```bash
106
+ npm run build # tsup
107
+ npm test # vitest
108
+ ```
109
+
110
+ ## 参考实现
111
+
112
+ - 官方图表插件:[`@worm-print-platform/echarts`](../echarts/README.md) — 用 `/kit` 提供框架无关纯 TS 能力,`/ui` 提供 plugin-kit 适配层
113
+
114
+ ## 相关链接
115
+
116
+ - [根 README](https://github.com/worm-longliu/worm-print-platform/blob/main/README.md)
117
+ - [插件化自定义元素章节](https://github.com/worm-longliu/worm-print-platform/blob/main/README.md#插件化自定义元素)
118
+
119
+ ## License
120
+
121
+ [MIT](../../LICENSE)
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
+ });
@@ -0,0 +1,100 @@
1
+ /*!
2
+ * @worm-print-platform/plugin-kit v1.5.2
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 };
@@ -0,0 +1,100 @@
1
+ /*!
2
+ * @worm-print-platform/plugin-kit v1.5.2
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": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "1.5.2",
4
+ "type": "module",
5
+ "description": "打印模板自定义元素的框架无关设计器 UI 契约与注册表:插件写一份 DOM 渲染能力,canvas(原生)与 /vue3(门面)同时消费",
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.2"
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
+ }