@mica-js/document 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +12 -2
- package/dist/canvas/index.d.ts +223 -0
- package/dist/canvas/index.js +570 -0
- package/dist/canvas/index.js.map +1 -0
- package/dist/errors-7cnIjhnV.js +69 -0
- package/dist/errors-7cnIjhnV.js.map +1 -0
- package/dist/index.d.ts +182 -0
- package/dist/index.js +3 -0
- package/dist/schematic/index.d.ts +931 -0
- package/dist/schematic/index.js +2845 -0
- package/dist/schematic/index.js.map +1 -0
- package/dist/type-name-B6Vkwj77.js +154 -0
- package/dist/type-name-B6Vkwj77.js.map +1 -0
- package/dist/validate-Cz-ZbbEr.js +232 -0
- package/dist/validate-Cz-ZbbEr.js.map +1 -0
- package/package.json +45 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 DigitalEcho
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,13 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @mica-js/document
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Mica 文档契约:`.sch` 原理图、`.cnv` 画布、话题投递与共享格式模型。本包只承载纯数据契约与文档操作,不包含编辑器状态、交互或渲染。
|
|
4
|
+
|
|
5
|
+
## 主要入口
|
|
6
|
+
|
|
7
|
+
- `@mica-js/document`:话题投递(`TopicInbox`、投递类型与默认值)、payload 类型、资源引用、诊断类型和 `SchematicError`。
|
|
8
|
+
- `@mica-js/document/schematic`:原理图解析与序列化、器件元数据解析、器件符号、几何、网表推导及文档编辑命令。`device.json` 的结构与注册标准化见 [格式说明](../../docs/formats/device.md) 和 [Draft 2020-12 Schema](../../docs/formats/device.schema.json)。
|
|
9
|
+
- `@mica-js/document/canvas`:画布解析与序列化、widget 实例、绑定校验及文档编辑命令。
|
|
10
|
+
|
|
11
|
+
## 依赖边界
|
|
12
|
+
|
|
13
|
+
本包只从 `@mica-js/core` 引入 `TimePs` 类型;不依赖仿真器或编辑器。
|
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Applies one command to a copy of `document`.
|
|
3
|
+
*
|
|
4
|
+
* @throws {SchematicError} when the command is invalid; `diagnostics` carries every problem found
|
|
5
|
+
* and the input document is never mutated.
|
|
6
|
+
*/
|
|
7
|
+
export declare function applyCanvasCommand(document: CanvasDocument, command: CanvasCommand): CanvasDocument;
|
|
8
|
+
|
|
9
|
+
/** @throws {SchematicError} when any diagnostic is an error. */
|
|
10
|
+
export declare function assertCanvasBindings(document: CanvasDocument, options?: ValidateCanvasBindingsOptions): void;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Schema version of the `.cnv` canvas contract (see x-botlab/mica-js#67).
|
|
14
|
+
*
|
|
15
|
+
* A canvas is an independent 2D plane: it records widget instances with their placement, property
|
|
16
|
+
* overrides and port-to-symbol bindings. Electrical topology stays in the `.sch` the canvas binds
|
|
17
|
+
* (see x-botlab/mica-js#6); the two coordinate spaces are unrelated.
|
|
18
|
+
*/
|
|
19
|
+
export declare const CANVAS_VERSION = 1;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* What the binding validator needs to know about widgets and the bound schematic.
|
|
23
|
+
*
|
|
24
|
+
* A host implements this over its widget registry and schematic model, so the validator itself
|
|
25
|
+
* depends on neither package and never owns a document.
|
|
26
|
+
*/
|
|
27
|
+
export declare interface CanvasBindingCatalog {
|
|
28
|
+
/** Widget definition for `"<package>:<widget>"`; `undefined` when unknown. */
|
|
29
|
+
widgetDefinition(ref: string): {
|
|
30
|
+
readonly ports: Readonly<Record<string, CanvasPortDescriptor>>;
|
|
31
|
+
readonly propertyKeys: readonly string[];
|
|
32
|
+
} | undefined;
|
|
33
|
+
/** True when the `.cnv` `schematic` path is loaded. */
|
|
34
|
+
hasSchematic(path: string): boolean;
|
|
35
|
+
/** True when the bound schematic contains this symbol id. */
|
|
36
|
+
hasSymbol(symbolId: string): boolean;
|
|
37
|
+
/** Device port declaration; `undefined` when the symbol has no such port. */
|
|
38
|
+
symbolPort(symbolId: string, port: string): CanvasPortDescriptor | undefined;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** Endpoint of a widget port: a port of one symbol instance in the bound schematic. */
|
|
42
|
+
export declare interface CanvasBindingTarget {
|
|
43
|
+
readonly symbol: string;
|
|
44
|
+
readonly port: string;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* One edit to a `.cnv` document. Every command describes document content only, so hosts can
|
|
49
|
+
* journal them for undo and persistence.
|
|
50
|
+
*/
|
|
51
|
+
export declare type CanvasCommand = {
|
|
52
|
+
readonly type: 'add-instance';
|
|
53
|
+
readonly instance: CanvasWidgetInstance;
|
|
54
|
+
readonly z: number;
|
|
55
|
+
} | {
|
|
56
|
+
readonly type: 'remove-instance';
|
|
57
|
+
readonly instanceId: string;
|
|
58
|
+
} | {
|
|
59
|
+
readonly type: 'set-placement';
|
|
60
|
+
readonly instanceId: string;
|
|
61
|
+
readonly placement: WidgetPlacement;
|
|
62
|
+
} | {
|
|
63
|
+
readonly type: 'set-z';
|
|
64
|
+
readonly instanceId: string;
|
|
65
|
+
readonly z: number;
|
|
66
|
+
} | {
|
|
67
|
+
readonly type: 'set-property';
|
|
68
|
+
readonly instanceId: string;
|
|
69
|
+
readonly key: string;
|
|
70
|
+
readonly value: unknown;
|
|
71
|
+
} | {
|
|
72
|
+
readonly type: 'unset-property';
|
|
73
|
+
readonly instanceId: string;
|
|
74
|
+
readonly key: string;
|
|
75
|
+
} | {
|
|
76
|
+
readonly type: 'set-binding';
|
|
77
|
+
readonly instanceId: string;
|
|
78
|
+
readonly port: string;
|
|
79
|
+
readonly target: CanvasBindingTarget | null;
|
|
80
|
+
} | {
|
|
81
|
+
readonly type: 'set-schematic';
|
|
82
|
+
readonly schematic: string | null;
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
/** Root of a `.cnv` file; the project-root relative path is the canvas identity. */
|
|
86
|
+
export declare interface CanvasDocument {
|
|
87
|
+
readonly version: typeof CANVAS_VERSION;
|
|
88
|
+
/** Project-root relative POSIX path of the `.sch` the canvas draws and simulates against. */
|
|
89
|
+
readonly schematic?: string;
|
|
90
|
+
/** Widget instances in drawing order; later instances paint above earlier ones. */
|
|
91
|
+
readonly widgets: readonly CanvasWidgetInstance[];
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** One endpoint a binding can reach; widget and device ports share this shape. */
|
|
95
|
+
export declare interface CanvasPortDescriptor {
|
|
96
|
+
readonly direction: 'input' | 'output';
|
|
97
|
+
readonly type: PayloadType;
|
|
98
|
+
/**
|
|
99
|
+
* How publishes become deliveries (RFC 0017). Omitted means the payload type decides; the two
|
|
100
|
+
* are orthogonal, so any combination of `delivery` and `type` is valid.
|
|
101
|
+
*/
|
|
102
|
+
readonly delivery?: DeliveryKind;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** One widget placed on a canvas; the order in `widgets` is the drawing order (#67). */
|
|
106
|
+
export declare interface CanvasWidgetInstance extends WidgetPlacement {
|
|
107
|
+
/** Stable identity: UUID v4, unique within the document. */
|
|
108
|
+
readonly id: string;
|
|
109
|
+
/** Widget reference `"<package>:<widget>"`; built-ins use the reserved `builtin:` namespace. */
|
|
110
|
+
readonly widget: string;
|
|
111
|
+
/** Property overrides keyed by the widget definition; values follow that definition (#7). */
|
|
112
|
+
readonly properties?: Readonly<Record<string, unknown>>;
|
|
113
|
+
/** Port key → endpoint; only bound ports take part in messaging (#10). */
|
|
114
|
+
readonly bindings?: Readonly<Record<string, CanvasBindingTarget>>;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** Delivery a port gets when it omits the optional `delivery` field (RFC 0017 default table). */
|
|
118
|
+
export declare function defaultDelivery(type: PayloadType): DeliveryKind;
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Delivery semantics of one topic port (RFC 0017).
|
|
122
|
+
*
|
|
123
|
+
* `level` treats the payload as the current value of the port, so only the last delivery of a step
|
|
124
|
+
* takes effect; `message` treats every publish as one record that must all arrive in order.
|
|
125
|
+
*/
|
|
126
|
+
export declare const DELIVERY_KINDS: readonly ["level", "message"];
|
|
127
|
+
|
|
128
|
+
export declare type DeliveryKind = (typeof DELIVERY_KINDS)[number];
|
|
129
|
+
|
|
130
|
+
export declare interface Diagnostic {
|
|
131
|
+
readonly severity: DiagnosticSeverity;
|
|
132
|
+
readonly code: DiagnosticCode;
|
|
133
|
+
/** Human readable English message; the `code` is the stable contract. */
|
|
134
|
+
readonly message: string;
|
|
135
|
+
/** JSON Pointer to the offending member, e.g. `/sheet/symbols/0/id`. */
|
|
136
|
+
readonly pointer?: string;
|
|
137
|
+
readonly entity?: DiagnosticEntity;
|
|
138
|
+
readonly related?: readonly DiagnosticRelated[];
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
export declare type DiagnosticCode = 'json/invalid' | 'json/not-object' | 'schema/unsupported-version' | 'schema/missing-field' | 'schema/invalid-type' | 'schema/invalid-value' | 'schema/unknown-field' | 'id/duplicate' | 'device/invalid-reference' | 'device/unresolved' | 'device/invalid-package' | 'device/duplicate-package' | 'device/reserved-namespace' | 'device/duplicate-device' | 'device/invalid-description' | 'device/unknown-pin' | 'device/no-symbol' | 'wire/invalid-points' | 'wire/not-orthogonal' | 'junction/unknown-wire' | 'junction/duplicate-wire' | 'junction/off-wire' | 'symbol/missing-net' | 'symbol/unsupported-field' | 'symbol/designator-duplicate' | 'net/naming-conflict' | 'canvas/unknown-symbol' | 'canvas/unknown-widget' | 'canvas/unknown-property' | 'canvas/unknown-port' | 'canvas/unknown-schematic' | 'canvas/unknown-target-port' | 'topic/type-mismatch' | 'type/unknown' | 'edit/unknown-entity' | 'edit/invalid-command';
|
|
142
|
+
|
|
143
|
+
export declare interface DiagnosticEntity {
|
|
144
|
+
readonly kind: 'symbol' | 'wire' | 'junction';
|
|
145
|
+
readonly id: string;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
export declare interface DiagnosticRelated {
|
|
149
|
+
readonly pointer?: string;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
export declare type DiagnosticSeverity = 'error' | 'warning';
|
|
153
|
+
|
|
154
|
+
/** Payload types are nominal for now: only an exact match is assignable. */
|
|
155
|
+
export declare function isAssignable(from: PayloadType, to: PayloadType): boolean;
|
|
156
|
+
|
|
157
|
+
/** @throws {SchematicError} when the input does not match the `.cnv` contract. */
|
|
158
|
+
export declare function parseCanvas(input: string | unknown): CanvasDocument;
|
|
159
|
+
|
|
160
|
+
/** Resolves a written `delivery` value, or `undefined` when it is not one of the two kinds. */
|
|
161
|
+
export declare function parseDeliveryKind(value: unknown): DeliveryKind | undefined;
|
|
162
|
+
|
|
163
|
+
/** Resolves a written payload type name, or `undefined` when it names no known type. */
|
|
164
|
+
export declare function parsePayloadType(value: string): PayloadType | undefined;
|
|
165
|
+
|
|
166
|
+
/** Payload type names of the Mica value model (see #10). */
|
|
167
|
+
export declare const PAYLOAD_TYPES: readonly ["boolean", "number", "string", "vec2d", "vec3d", "bytes", "bytes<rgb565>", "bytes<segments>"];
|
|
168
|
+
|
|
169
|
+
export declare type PayloadType = (typeof PAYLOAD_TYPES)[number];
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Declared delivery wins; the payload type decides when the port leaves it out.
|
|
173
|
+
*
|
|
174
|
+
* `declared` is `unknown` on purpose: the value arrives from a written descriptor, so an
|
|
175
|
+
* unparsable value falls back to the default instead of failing here. Validating the written value
|
|
176
|
+
* belongs to the loader that reads the descriptor.
|
|
177
|
+
*/
|
|
178
|
+
export declare function resolveDelivery(declared: unknown, type: PayloadType): DeliveryKind;
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Thrown when input violates the schematic format contract.
|
|
182
|
+
* `diagnostics` carries every problem found, so callers can report them all at once.
|
|
183
|
+
*/
|
|
184
|
+
export declare class SchematicError extends Error {
|
|
185
|
+
readonly diagnostics: readonly Diagnostic[];
|
|
186
|
+
constructor(diagnostics: readonly Diagnostic[]);
|
|
187
|
+
/** Code of the first diagnostic, for coarse programmatic handling. */
|
|
188
|
+
get code(): DiagnosticCode | undefined;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Serializes with a fixed field order and 2-space indentation, matching the example in #67.
|
|
193
|
+
* `properties` and `bindings` are written whenever the document carries them, even when empty, so
|
|
194
|
+
* a parse / serialize round trip keeps the original shape.
|
|
195
|
+
*/
|
|
196
|
+
export declare function serializeCanvas(document: CanvasDocument): string;
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Collects binding diagnostics; never throws.
|
|
200
|
+
*
|
|
201
|
+
* Without a `catalog` the checks that need the widget registry and the schematic are skipped, so a
|
|
202
|
+
* host that has not loaded them can call this safely. `schematic` is optional on the document: when
|
|
203
|
+
* absent, only widget-side facts are checked.
|
|
204
|
+
*/
|
|
205
|
+
export declare function validateCanvasBindings(document: CanvasDocument, options?: ValidateCanvasBindingsOptions): readonly Diagnostic[];
|
|
206
|
+
|
|
207
|
+
export declare interface ValidateCanvasBindingsOptions {
|
|
208
|
+
readonly catalog?: CanvasBindingCatalog;
|
|
209
|
+
/** Skip widget/property/port checks; useful for partial validation. Default `false`. */
|
|
210
|
+
readonly skipWidgets?: boolean;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/** Placement and size of one widget instance, in logical units. */
|
|
214
|
+
export declare interface WidgetPlacement {
|
|
215
|
+
readonly x: number;
|
|
216
|
+
readonly y: number;
|
|
217
|
+
readonly width: number;
|
|
218
|
+
readonly height: number;
|
|
219
|
+
/** Clockwise rotation in degrees around the instance center. */
|
|
220
|
+
readonly rotation: number;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
export { }
|