@open-artifact/design-core 0.0.0-stage → 0.2.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 +202 -0
- package/NOTICE +5 -0
- package/README.md +5 -2
- package/package.json +36 -4
- package/src/canvas.d.ts +201 -0
- package/src/canvas.js +214 -0
- package/src/check.d.ts +27 -0
- package/src/check.js +234 -0
- package/src/index.d.ts +10 -0
- package/src/index.js +10 -0
- package/src/merge.d.ts +22 -0
- package/src/merge.js +112 -0
- package/src/note-groups.d.ts +115 -0
- package/src/note-groups.js +285 -0
- package/src/view-context.d.ts +91 -0
- package/src/view-context.js +134 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/NOTICE
ADDED
package/README.md
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @open-artifact/design-core
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The Design artifact model: canvas.json, merging, view context and project checks.
|
|
4
|
+
|
|
5
|
+
Part of [Open Artifact](https://github.com/JimLiu/open-artifact): slide decks, design canvases and whiteboards an agent makes
|
|
6
|
+
and you view, edit and comment on in one local page. Source: [`packages/design-core`](https://github.com/JimLiu/open-artifact/tree/main/packages/design-core).
|
package/package.json
CHANGED
|
@@ -1,6 +1,38 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@open-artifact/design-core",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "The Design artifact model: canvas.json, merging, view context and project checks.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": {
|
|
8
|
+
"types": "./src/index.d.ts",
|
|
9
|
+
"default": "./src/index.js"
|
|
10
|
+
},
|
|
11
|
+
"./package.json": "./package.json"
|
|
12
|
+
},
|
|
13
|
+
"files": [
|
|
14
|
+
"src",
|
|
15
|
+
"LICENSE",
|
|
16
|
+
"NOTICE"
|
|
17
|
+
],
|
|
18
|
+
"dependencies": {
|
|
19
|
+
"@open-artifact/dc": "0.2.0"
|
|
20
|
+
},
|
|
21
|
+
"license": "Apache-2.0",
|
|
22
|
+
"author": {
|
|
23
|
+
"name": "Jim Liu 宝玉",
|
|
24
|
+
"url": "https://github.com/JimLiu"
|
|
25
|
+
},
|
|
26
|
+
"homepage": "https://github.com/JimLiu/open-artifact#readme",
|
|
27
|
+
"repository": {
|
|
28
|
+
"type": "git",
|
|
29
|
+
"url": "git+https://github.com/JimLiu/open-artifact.git",
|
|
30
|
+
"directory": "packages/design-core"
|
|
31
|
+
},
|
|
32
|
+
"bugs": {
|
|
33
|
+
"url": "https://github.com/JimLiu/open-artifact/issues"
|
|
34
|
+
},
|
|
35
|
+
"publishConfig": {
|
|
36
|
+
"access": "public"
|
|
37
|
+
}
|
|
38
|
+
}
|
package/src/canvas.d.ts
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `project/canvas.json`, the index of a Design artifact.
|
|
3
|
+
*
|
|
4
|
+
* It places every artboard (a `.dc.html` file under `project/`) on an
|
|
5
|
+
* infinite canvas, orders them back to front, groups them into pages, and
|
|
6
|
+
* holds the canvas notes. Readers keep every key they do not know: the file
|
|
7
|
+
* is shared by the editor, agents and future versions, so a normalize/save
|
|
8
|
+
* cycle must never drop data.
|
|
9
|
+
*/
|
|
10
|
+
/** Current index version. */
|
|
11
|
+
export declare const CANVAS_VERSION = 3;
|
|
12
|
+
/** Size limits of an artboard frame, CSS pixels. */
|
|
13
|
+
export declare const MIN_BOARD_SIZE = 40;
|
|
14
|
+
export declare const MAX_BOARD_SIZE = 8000;
|
|
15
|
+
/** Gap between frames in a row, and between rows. */
|
|
16
|
+
export declare const ROW_GAP = 80;
|
|
17
|
+
export declare const COLUMN_GAP = 120;
|
|
18
|
+
/** Most pages, notes, guides per board. */
|
|
19
|
+
export declare const MAX_PAGES = 40;
|
|
20
|
+
export declare const MAX_NOTES = 999;
|
|
21
|
+
export declare const MAX_GUIDES = 6;
|
|
22
|
+
/** How deep note groups may nest (see `note-groups.ts`). */
|
|
23
|
+
export declare const MAX_NOTE_GROUPS = 8;
|
|
24
|
+
/** Id grammar of pages and notes. */
|
|
25
|
+
export declare const ID_PATTERN: RegExp;
|
|
26
|
+
/** Folder grammar of an installed design system (`project/ds/<folder>/`). */
|
|
27
|
+
export declare const DS_FOLDER_PATTERN: RegExp;
|
|
28
|
+
/** What opens first. */
|
|
29
|
+
export type Launch = {
|
|
30
|
+
view: 'canvas';
|
|
31
|
+
page?: string;
|
|
32
|
+
} | {
|
|
33
|
+
view: 'focused';
|
|
34
|
+
file: string;
|
|
35
|
+
};
|
|
36
|
+
/** A page of the canvas. */
|
|
37
|
+
export interface CanvasPage {
|
|
38
|
+
id: string;
|
|
39
|
+
name: string;
|
|
40
|
+
}
|
|
41
|
+
/** A layout guide drawn over an artboard (never in Play or exports). */
|
|
42
|
+
export interface BoardGuide {
|
|
43
|
+
kind: 'columns' | 'rows' | 'grid';
|
|
44
|
+
count?: number | 'auto';
|
|
45
|
+
gutter?: number;
|
|
46
|
+
margin?: number;
|
|
47
|
+
align?: 'stretch' | 'start' | 'center' | 'end';
|
|
48
|
+
size?: number;
|
|
49
|
+
offset?: number;
|
|
50
|
+
color?: string;
|
|
51
|
+
hidden?: boolean;
|
|
52
|
+
[key: string]: unknown;
|
|
53
|
+
}
|
|
54
|
+
/** One artboard's frame on the canvas, keyed by its path under `project/`. */
|
|
55
|
+
export interface BoardEntry {
|
|
56
|
+
x: number;
|
|
57
|
+
y: number;
|
|
58
|
+
w: number;
|
|
59
|
+
h: number;
|
|
60
|
+
title?: string;
|
|
61
|
+
/** Page id; none = the first page. */
|
|
62
|
+
page?: string;
|
|
63
|
+
/** `"fill"`: a page that fills the window in the focused view. */
|
|
64
|
+
expand?: 'fill';
|
|
65
|
+
/** `"flow"`: PDF export paginates. */
|
|
66
|
+
print?: 'flow' | 'fixed';
|
|
67
|
+
paper?: 'letter' | 'a4';
|
|
68
|
+
/** Has working controls: gets the Play button. */
|
|
69
|
+
is_interactive?: boolean;
|
|
70
|
+
/** Drawn without a frame (editor-set). */
|
|
71
|
+
frameless?: boolean;
|
|
72
|
+
guides?: BoardGuide[];
|
|
73
|
+
/** Corner radius, px. */
|
|
74
|
+
radius?: number;
|
|
75
|
+
[key: string]: unknown;
|
|
76
|
+
}
|
|
77
|
+
/** Note kinds. No kind = a sticky. */
|
|
78
|
+
export type NoteKind = 'title1' | 'sticky' | 'rect' | 'oval' | 'pen' | 'line' | 'arrow' | 'image' | 'link';
|
|
79
|
+
/** Named note colors. */
|
|
80
|
+
export declare const NOTE_COLORS: readonly ["gray", "red", "orange", "green", "teal", "blue", "purple", "pink"];
|
|
81
|
+
export type NoteColor = (typeof NOTE_COLORS)[number];
|
|
82
|
+
/** Text sizes of notes. */
|
|
83
|
+
export type NoteSize = number | 's' | 'm' | 'l' | 'xl' | 'xxl';
|
|
84
|
+
/** A canvas note: a title over several artboards, a sticky, or a drawn shape. */
|
|
85
|
+
export interface CanvasNote {
|
|
86
|
+
x: number;
|
|
87
|
+
y: number;
|
|
88
|
+
text: string;
|
|
89
|
+
kind?: NoteKind;
|
|
90
|
+
w?: number;
|
|
91
|
+
h?: number;
|
|
92
|
+
maxW?: number;
|
|
93
|
+
maxH?: number;
|
|
94
|
+
size?: NoteSize;
|
|
95
|
+
bold?: boolean;
|
|
96
|
+
italic?: boolean;
|
|
97
|
+
page?: string;
|
|
98
|
+
color?: NoteColor | string;
|
|
99
|
+
fill?: NoteColor | string;
|
|
100
|
+
/** Pen / line / arrow points, relative to `x`,`y`. */
|
|
101
|
+
points?: [number, number][];
|
|
102
|
+
/** Pen notes: stroke weight in px (default 3). */
|
|
103
|
+
stroke?: number;
|
|
104
|
+
/** Pen notes: dash pattern (default solid). */
|
|
105
|
+
dash?: 'dashed' | 'dotted';
|
|
106
|
+
/** Image notes: the uploaded blob url. */
|
|
107
|
+
src?: string;
|
|
108
|
+
/** Link notes: the target. */
|
|
109
|
+
href?: string;
|
|
110
|
+
/** Groups this note belongs to, innermost first (see `note-groups.ts`). */
|
|
111
|
+
groups?: string[];
|
|
112
|
+
[key: string]: unknown;
|
|
113
|
+
}
|
|
114
|
+
/** A design system installed into this canvas (tokens copied to `project/ds/<namespace>/`). */
|
|
115
|
+
export interface DesignSystemRecord {
|
|
116
|
+
title: string;
|
|
117
|
+
/** The folder under `project/ds/`. */
|
|
118
|
+
namespace: string;
|
|
119
|
+
/** Where it came from (an artifact address). */
|
|
120
|
+
artifact: string;
|
|
121
|
+
version: string | null;
|
|
122
|
+
copiedAt: string;
|
|
123
|
+
[key: string]: unknown;
|
|
124
|
+
}
|
|
125
|
+
/** The whole index. */
|
|
126
|
+
export interface Canvas {
|
|
127
|
+
v: number;
|
|
128
|
+
createdOnFiles?: {
|
|
129
|
+
v: number;
|
|
130
|
+
at: string;
|
|
131
|
+
};
|
|
132
|
+
title?: string;
|
|
133
|
+
launch?: Launch;
|
|
134
|
+
pages: CanvasPage[];
|
|
135
|
+
boards: Record<string, BoardEntry>;
|
|
136
|
+
/** Board paths, back to front. */
|
|
137
|
+
order: string[];
|
|
138
|
+
notes: Record<string, CanvasNote>;
|
|
139
|
+
designSystems: DesignSystemRecord[];
|
|
140
|
+
[key: string]: unknown;
|
|
141
|
+
}
|
|
142
|
+
/** An empty index, as the editor creates it. */
|
|
143
|
+
export declare function emptyCanvas(title?: string, now?: Date): Canvas;
|
|
144
|
+
/**
|
|
145
|
+
* Normalizes parsed JSON into a {@link Canvas}: fills missing collections,
|
|
146
|
+
* clamps sizes, drops entries that cannot be drawn, and puts every board in
|
|
147
|
+
* `order` exactly once. Unknown keys survive.
|
|
148
|
+
*/
|
|
149
|
+
export declare function normalizeCanvas(input: unknown): Canvas;
|
|
150
|
+
/** The cleaned `groups` of a stored note: unique valid ids, at most `MAX_NOTE_GROUPS`. */
|
|
151
|
+
export declare function normalizeNoteGroups(value: unknown): string[] | undefined;
|
|
152
|
+
/** Parses canvas.json text; invalid JSON yields an empty canvas and the error. */
|
|
153
|
+
export declare function parseCanvas(text: string): {
|
|
154
|
+
canvas: Canvas;
|
|
155
|
+
error: string | null;
|
|
156
|
+
};
|
|
157
|
+
/** Serializes the index the way it is stored: two-space JSON with a trailing newline. */
|
|
158
|
+
export declare function serializeCanvas(canvas: Canvas): string;
|
|
159
|
+
/** The page id an entry belongs to (`null` on a canvas without pages). */
|
|
160
|
+
export declare function pageOf(canvas: Canvas, item: {
|
|
161
|
+
page?: string;
|
|
162
|
+
}): string | null;
|
|
163
|
+
/** Board paths on a page, back to front. `null` = all (canvas without pages). */
|
|
164
|
+
export declare function boardsOnPage(canvas: Canvas, page: string | null): string[];
|
|
165
|
+
/** Note ids on a page. */
|
|
166
|
+
export declare function notesOnPage(canvas: Canvas, page: string | null): string[];
|
|
167
|
+
/** A fresh id not used by `taken`. */
|
|
168
|
+
export declare function freshId(prefix: string, taken: Iterable<string>): string;
|
|
169
|
+
/** An axis-aligned rectangle in canvas pixels. */
|
|
170
|
+
export interface Rect {
|
|
171
|
+
x: number;
|
|
172
|
+
y: number;
|
|
173
|
+
w: number;
|
|
174
|
+
h: number;
|
|
175
|
+
}
|
|
176
|
+
/** The union of rectangles, or `null` for none. */
|
|
177
|
+
export declare function unionRect(rects: readonly Rect[]): Rect | null;
|
|
178
|
+
/** True when two rectangles overlap (touching edges do not). */
|
|
179
|
+
export declare function intersects(a: Rect, b: Rect): boolean;
|
|
180
|
+
/**
|
|
181
|
+
* Where a new frame of the given size goes: to the right of the last row's
|
|
182
|
+
* rightmost frame (with {@link ROW_GAP}), or at the origin on an empty page.
|
|
183
|
+
*/
|
|
184
|
+
export declare function placeNewBoard(canvas: Canvas, page: string | null, size: {
|
|
185
|
+
w: number;
|
|
186
|
+
h: number;
|
|
187
|
+
}): {
|
|
188
|
+
x: number;
|
|
189
|
+
y: number;
|
|
190
|
+
};
|
|
191
|
+
/**
|
|
192
|
+
* Lays frames out in rows (left to right, wrapping at `maxRowWidth`), with
|
|
193
|
+
* the standard gaps. Returns new positions by path; sizes are unchanged.
|
|
194
|
+
*/
|
|
195
|
+
export declare function layoutRows(canvas: Canvas, paths: readonly string[], maxRowWidth?: number, origin?: {
|
|
196
|
+
x: number;
|
|
197
|
+
y: number;
|
|
198
|
+
}): Record<string, {
|
|
199
|
+
x: number;
|
|
200
|
+
y: number;
|
|
201
|
+
}>;
|
package/src/canvas.js
ADDED
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `project/canvas.json`, the index of a Design artifact.
|
|
3
|
+
*
|
|
4
|
+
* It places every artboard (a `.dc.html` file under `project/`) on an
|
|
5
|
+
* infinite canvas, orders them back to front, groups them into pages, and
|
|
6
|
+
* holds the canvas notes. Readers keep every key they do not know: the file
|
|
7
|
+
* is shared by the editor, agents and future versions, so a normalize/save
|
|
8
|
+
* cycle must never drop data.
|
|
9
|
+
*/
|
|
10
|
+
/** Current index version. */
|
|
11
|
+
export const CANVAS_VERSION = 3;
|
|
12
|
+
/** Size limits of an artboard frame, CSS pixels. */
|
|
13
|
+
export const MIN_BOARD_SIZE = 40;
|
|
14
|
+
export const MAX_BOARD_SIZE = 8000;
|
|
15
|
+
/** Gap between frames in a row, and between rows. */
|
|
16
|
+
export const ROW_GAP = 80;
|
|
17
|
+
export const COLUMN_GAP = 120;
|
|
18
|
+
/** Most pages, notes, guides per board. */
|
|
19
|
+
export const MAX_PAGES = 40;
|
|
20
|
+
export const MAX_NOTES = 999;
|
|
21
|
+
export const MAX_GUIDES = 6;
|
|
22
|
+
/** How deep note groups may nest (see `note-groups.ts`). */
|
|
23
|
+
export const MAX_NOTE_GROUPS = 8;
|
|
24
|
+
/** Id grammar of pages and notes. */
|
|
25
|
+
export const ID_PATTERN = /^[A-Za-z0-9_-]{1,40}$/;
|
|
26
|
+
/** Folder grammar of an installed design system (`project/ds/<folder>/`). */
|
|
27
|
+
export const DS_FOLDER_PATTERN = /^[a-z0-9][a-z0-9_-]{0,63}$/;
|
|
28
|
+
/** Named note colors. */
|
|
29
|
+
export const NOTE_COLORS = ['gray', 'red', 'orange', 'green', 'teal', 'blue', 'purple', 'pink'];
|
|
30
|
+
/** An empty index, as the editor creates it. */
|
|
31
|
+
export function emptyCanvas(title, now = new Date()) {
|
|
32
|
+
return {
|
|
33
|
+
v: CANVAS_VERSION,
|
|
34
|
+
createdOnFiles: { v: 1, at: now.toISOString().replace(/\.\d{3}Z$/, 'Z') },
|
|
35
|
+
...(title ? { title } : {}),
|
|
36
|
+
launch: { view: 'canvas' },
|
|
37
|
+
pages: [],
|
|
38
|
+
boards: {},
|
|
39
|
+
order: [],
|
|
40
|
+
notes: {},
|
|
41
|
+
designSystems: [],
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
const isRecord = (value) => Boolean(value) && typeof value === 'object' && !Array.isArray(value);
|
|
45
|
+
const finite = (value, fallback) => typeof value === 'number' && Number.isFinite(value) ? value : fallback;
|
|
46
|
+
const clampSize = (value, fallback) => Math.min(MAX_BOARD_SIZE, Math.max(MIN_BOARD_SIZE, Math.round(finite(value, fallback))));
|
|
47
|
+
/**
|
|
48
|
+
* Normalizes parsed JSON into a {@link Canvas}: fills missing collections,
|
|
49
|
+
* clamps sizes, drops entries that cannot be drawn, and puts every board in
|
|
50
|
+
* `order` exactly once. Unknown keys survive.
|
|
51
|
+
*/
|
|
52
|
+
export function normalizeCanvas(input) {
|
|
53
|
+
const raw = isRecord(input) ? input : {};
|
|
54
|
+
const boards = {};
|
|
55
|
+
if (isRecord(raw.boards)) {
|
|
56
|
+
for (const [path, entry] of Object.entries(raw.boards)) {
|
|
57
|
+
if (!isRecord(entry))
|
|
58
|
+
continue;
|
|
59
|
+
boards[path] = {
|
|
60
|
+
...entry,
|
|
61
|
+
x: finite(entry.x, 0),
|
|
62
|
+
y: finite(entry.y, 0),
|
|
63
|
+
w: clampSize(entry.w, 880),
|
|
64
|
+
h: clampSize(entry.h, 560),
|
|
65
|
+
};
|
|
66
|
+
if (Array.isArray(entry.guides))
|
|
67
|
+
boards[path].guides = entry.guides.slice(0, MAX_GUIDES);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
const seen = new Set();
|
|
71
|
+
const order = [];
|
|
72
|
+
for (const path of Array.isArray(raw.order) ? raw.order : []) {
|
|
73
|
+
if (typeof path === 'string' && boards[path] && !seen.has(path)) {
|
|
74
|
+
seen.add(path);
|
|
75
|
+
order.push(path);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
for (const path of Object.keys(boards))
|
|
79
|
+
if (!seen.has(path))
|
|
80
|
+
order.push(path);
|
|
81
|
+
const pages = (Array.isArray(raw.pages) ? raw.pages : [])
|
|
82
|
+
.filter((page) => isRecord(page) && typeof page.id === 'string' && ID_PATTERN.test(page.id))
|
|
83
|
+
.map((page) => ({ ...page, name: typeof page.name === 'string' ? page.name : page.id }))
|
|
84
|
+
.slice(0, MAX_PAGES);
|
|
85
|
+
const notes = {};
|
|
86
|
+
if (isRecord(raw.notes)) {
|
|
87
|
+
for (const [id, note] of Object.entries(raw.notes)) {
|
|
88
|
+
if (!isRecord(note) || Object.keys(notes).length >= MAX_NOTES)
|
|
89
|
+
continue;
|
|
90
|
+
const { groups: rawGroups, ...rest } = note;
|
|
91
|
+
const groups = normalizeNoteGroups(rawGroups);
|
|
92
|
+
notes[id] = { ...rest, x: finite(note.x, 0), y: finite(note.y, 0), text: typeof note.text === 'string' ? note.text : '', ...(groups ? { groups } : {}) };
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
let launch;
|
|
96
|
+
if (isRecord(raw.launch)) {
|
|
97
|
+
if (raw.launch.view === 'focused' && typeof raw.launch.file === 'string')
|
|
98
|
+
launch = { view: 'focused', file: raw.launch.file };
|
|
99
|
+
else
|
|
100
|
+
launch = typeof raw.launch.page === 'string' ? { view: 'canvas', page: raw.launch.page } : { view: 'canvas' };
|
|
101
|
+
}
|
|
102
|
+
return {
|
|
103
|
+
...raw,
|
|
104
|
+
v: finite(raw.v, CANVAS_VERSION),
|
|
105
|
+
...(launch ? { launch } : {}),
|
|
106
|
+
pages,
|
|
107
|
+
boards,
|
|
108
|
+
order,
|
|
109
|
+
notes,
|
|
110
|
+
designSystems: Array.isArray(raw.designSystems) ? raw.designSystems.filter(isRecord) : [],
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
/** The cleaned `groups` of a stored note: unique valid ids, at most `MAX_NOTE_GROUPS`. */
|
|
114
|
+
export function normalizeNoteGroups(value) {
|
|
115
|
+
if (!Array.isArray(value))
|
|
116
|
+
return undefined;
|
|
117
|
+
const out = [...new Set(value.filter((group) => typeof group === 'string' && ID_PATTERN.test(group)))].slice(0, MAX_NOTE_GROUPS);
|
|
118
|
+
return out.length ? out : undefined;
|
|
119
|
+
}
|
|
120
|
+
/** Parses canvas.json text; invalid JSON yields an empty canvas and the error. */
|
|
121
|
+
export function parseCanvas(text) {
|
|
122
|
+
try {
|
|
123
|
+
return { canvas: normalizeCanvas(JSON.parse(text)), error: null };
|
|
124
|
+
}
|
|
125
|
+
catch (error) {
|
|
126
|
+
return { canvas: normalizeCanvas({}), error: error instanceof Error ? error.message : String(error) };
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
/** Serializes the index the way it is stored: two-space JSON with a trailing newline. */
|
|
130
|
+
export function serializeCanvas(canvas) {
|
|
131
|
+
return `${JSON.stringify(canvas, null, 2)}\n`;
|
|
132
|
+
}
|
|
133
|
+
/* -------------------------------------------------------------------------- */
|
|
134
|
+
/* Pages */
|
|
135
|
+
/* -------------------------------------------------------------------------- */
|
|
136
|
+
/** The page id an entry belongs to (`null` on a canvas without pages). */
|
|
137
|
+
export function pageOf(canvas, item) {
|
|
138
|
+
if (!canvas.pages.length)
|
|
139
|
+
return null;
|
|
140
|
+
const first = canvas.pages[0].id;
|
|
141
|
+
return item.page && canvas.pages.some((page) => page.id === item.page) ? item.page : first;
|
|
142
|
+
}
|
|
143
|
+
/** Board paths on a page, back to front. `null` = all (canvas without pages). */
|
|
144
|
+
export function boardsOnPage(canvas, page) {
|
|
145
|
+
return canvas.order.filter((path) => page === null || pageOf(canvas, canvas.boards[path]) === page);
|
|
146
|
+
}
|
|
147
|
+
/** Note ids on a page. */
|
|
148
|
+
export function notesOnPage(canvas, page) {
|
|
149
|
+
return Object.keys(canvas.notes).filter((id) => page === null || pageOf(canvas, canvas.notes[id]) === page);
|
|
150
|
+
}
|
|
151
|
+
/** A fresh id not used by `taken`. */
|
|
152
|
+
export function freshId(prefix, taken) {
|
|
153
|
+
const used = new Set(taken);
|
|
154
|
+
for (let i = 1;; i++) {
|
|
155
|
+
const id = `${prefix}${i}`;
|
|
156
|
+
if (!used.has(id))
|
|
157
|
+
return id;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
/** The union of rectangles, or `null` for none. */
|
|
161
|
+
export function unionRect(rects) {
|
|
162
|
+
if (!rects.length)
|
|
163
|
+
return null;
|
|
164
|
+
const x = Math.min(...rects.map((r) => r.x));
|
|
165
|
+
const y = Math.min(...rects.map((r) => r.y));
|
|
166
|
+
const right = Math.max(...rects.map((r) => r.x + r.w));
|
|
167
|
+
const bottom = Math.max(...rects.map((r) => r.y + r.h));
|
|
168
|
+
return { x, y, w: right - x, h: bottom - y };
|
|
169
|
+
}
|
|
170
|
+
/** True when two rectangles overlap (touching edges do not). */
|
|
171
|
+
export function intersects(a, b) {
|
|
172
|
+
return a.x < b.x + b.w && b.x < a.x + a.w && a.y < b.y + b.h && b.y < a.y + a.h;
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Where a new frame of the given size goes: to the right of the last row's
|
|
176
|
+
* rightmost frame (with {@link ROW_GAP}), or at the origin on an empty page.
|
|
177
|
+
*/
|
|
178
|
+
export function placeNewBoard(canvas, page, size) {
|
|
179
|
+
const rects = boardsOnPage(canvas, page).map((path) => canvas.boards[path]);
|
|
180
|
+
if (!rects.length)
|
|
181
|
+
return { x: 0, y: 0 };
|
|
182
|
+
const lastRowTop = Math.max(...rects.map((r) => r.y));
|
|
183
|
+
const row = rects.filter((r) => r.y === lastRowTop);
|
|
184
|
+
const right = Math.max(...row.map((r) => r.x + r.w));
|
|
185
|
+
let spot = { x: right + ROW_GAP, y: lastRowTop };
|
|
186
|
+
// Never on top of something else.
|
|
187
|
+
while (rects.some((r) => intersects(r, { ...spot, ...size })))
|
|
188
|
+
spot = { x: spot.x + ROW_GAP + size.w, y: spot.y };
|
|
189
|
+
return spot;
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Lays frames out in rows (left to right, wrapping at `maxRowWidth`), with
|
|
193
|
+
* the standard gaps. Returns new positions by path; sizes are unchanged.
|
|
194
|
+
*/
|
|
195
|
+
export function layoutRows(canvas, paths, maxRowWidth = 4000, origin = { x: 0, y: 0 }) {
|
|
196
|
+
const out = {};
|
|
197
|
+
let x = origin.x;
|
|
198
|
+
let y = origin.y;
|
|
199
|
+
let rowHeight = 0;
|
|
200
|
+
for (const path of paths) {
|
|
201
|
+
const board = canvas.boards[path];
|
|
202
|
+
if (!board)
|
|
203
|
+
continue;
|
|
204
|
+
if (x > origin.x && x + board.w - origin.x > maxRowWidth) {
|
|
205
|
+
x = origin.x;
|
|
206
|
+
y += rowHeight + COLUMN_GAP;
|
|
207
|
+
rowHeight = 0;
|
|
208
|
+
}
|
|
209
|
+
out[path] = { x, y };
|
|
210
|
+
x += board.w + ROW_GAP;
|
|
211
|
+
rowHeight = Math.max(rowHeight, board.h);
|
|
212
|
+
}
|
|
213
|
+
return out;
|
|
214
|
+
}
|