@open-artifact/design-core 0.2.2 → 0.9.1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@open-artifact/design-core",
3
- "version": "0.2.2",
3
+ "version": "0.9.1",
4
4
  "description": "The Design artifact model: canvas.json, merging, view context and project checks.",
5
5
  "type": "module",
6
6
  "exports": {
@@ -16,7 +16,7 @@
16
16
  "NOTICE"
17
17
  ],
18
18
  "dependencies": {
19
- "@open-artifact/dc": "0.2.2"
19
+ "@open-artifact/dc": "0.9.1"
20
20
  },
21
21
  "license": "Apache-2.0",
22
22
  "author": {
package/src/canvas.d.ts CHANGED
@@ -80,12 +80,25 @@ export type NoteKind = 'title1' | 'sticky' | 'rect' | 'oval' | 'pen' | 'line' |
80
80
  export declare const NOTE_COLORS: readonly ["gray", "red", "orange", "green", "teal", "blue", "purple", "pink"];
81
81
  export type NoteColor = (typeof NOTE_COLORS)[number];
82
82
  /** Text sizes of notes. */
83
+ export type ArrowHead = 'none' | 'open' | 'filled';
83
84
  export type NoteSize = number | 's' | 'm' | 'l' | 'xl' | 'xxl';
85
+ /** Shared by the canvas renderer and the layout checker. */
86
+ export declare function noteFontSize(size: unknown, kind?: NoteKind): number;
84
87
  /** A canvas note: a title over several artboards, a sticky, or a drawn shape. */
85
88
  export interface CanvasNote {
86
89
  x: number;
87
90
  y: number;
88
- text: string;
91
+ text?: string;
92
+ title?: string;
93
+ linkKind?: 'slides' | 'motion' | 'doc' | 'design';
94
+ alt?: string;
95
+ css?: Record<string, string>;
96
+ rotate?: number;
97
+ flipX?: boolean;
98
+ flipY?: boolean;
99
+ headStart?: ArrowHead;
100
+ headEnd?: ArrowHead;
101
+ d?: string;
89
102
  kind?: NoteKind;
90
103
  w?: number;
91
104
  h?: number;
package/src/canvas.js CHANGED
@@ -27,6 +27,13 @@ export const ID_PATTERN = /^[A-Za-z0-9_-]{1,40}$/;
27
27
  export const DS_FOLDER_PATTERN = /^[a-z0-9][a-z0-9_-]{0,63}$/;
28
28
  /** Named note colors. */
29
29
  export const NOTE_COLORS = ['gray', 'red', 'orange', 'green', 'teal', 'blue', 'purple', 'pink'];
30
+ /** Shared by the canvas renderer and the layout checker. */
31
+ export function noteFontSize(size, kind) {
32
+ if (typeof size === 'number' && Number.isFinite(size))
33
+ return Math.max(8, Math.min(400, Math.round(size)));
34
+ const sizes = { s: 14, m: 24, l: 40, xl: 72, xxl: 120 };
35
+ return typeof size === 'string' && Object.hasOwn(sizes, size) ? sizes[size] : kind === 'title1' ? 72 : 14;
36
+ }
30
37
  /** An empty index, as the editor creates it. */
31
38
  export function emptyCanvas(title, now = new Date()) {
32
39
  return {
@@ -63,8 +70,11 @@ export function normalizeCanvas(input) {
63
70
  w: clampSize(entry.w, 880),
64
71
  h: clampSize(entry.h, 560),
65
72
  };
66
- if (Array.isArray(entry.guides))
67
- boards[path].guides = entry.guides.slice(0, MAX_GUIDES);
73
+ if (Array.isArray(entry.guides)) {
74
+ boards[path].guides = entry.guides.filter(g => isRecord(g) && ['grid', 'rows', 'columns'].includes(String(g.kind))).slice(0, MAX_GUIDES);
75
+ }
76
+ else
77
+ delete boards[path].guides;
68
78
  }
69
79
  }
70
80
  const seen = new Set();
@@ -89,7 +99,34 @@ export function normalizeCanvas(input) {
89
99
  continue;
90
100
  const { groups: rawGroups, ...rest } = note;
91
101
  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 } : {}) };
102
+ const x = finite(note.x, 0);
103
+ const y = finite(note.y, 0);
104
+ const item = { ...rest, x, y, ...(groups ? { groups } : {}) };
105
+ if (isRecord(item.css))
106
+ item.css = Object.fromEntries(Object.entries(item.css).filter(([, value]) => typeof value === 'string'));
107
+ else
108
+ delete item.css;
109
+ if (!Number.isFinite(item.rotate))
110
+ delete item.rotate;
111
+ if (item.points !== undefined) {
112
+ if (Array.isArray(item.points))
113
+ item.points = item.points.filter(p => Array.isArray(p) && p.length === 2 && p.every(Number.isFinite));
114
+ else
115
+ delete item.points;
116
+ }
117
+ for (const key of ['title', 'href', 'alt', 'd'])
118
+ if (typeof item[key] !== 'string')
119
+ delete item[key];
120
+ if (typeof item.text !== 'string')
121
+ delete item.text;
122
+ if ((item.kind === 'line' || item.kind === 'arrow') && Number.isFinite(note.x2) && Number.isFinite(note.y2) && !item.points?.length) {
123
+ item.points = [[0, 0], [Number(note.x2) - x, Number(note.y2) - y]];
124
+ }
125
+ if (item.kind === 'line' || item.kind === 'arrow') {
126
+ delete item.x2;
127
+ delete item.y2;
128
+ }
129
+ notes[id] = item;
93
130
  }
94
131
  }
95
132
  let launch;
@@ -128,7 +165,32 @@ export function parseCanvas(text) {
128
165
  }
129
166
  /** Serializes the index the way it is stored: two-space JSON with a trailing newline. */
130
167
  export function serializeCanvas(canvas) {
131
- return `${JSON.stringify(canvas, null, 2)}\n`;
168
+ const notes = Object.fromEntries(Object.entries(canvas.notes).map(([id, note]) => {
169
+ if (note.kind === 'pen' && note.points?.length) {
170
+ let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;
171
+ for (const [x, y] of note.points) {
172
+ minX = Math.min(minX, x);
173
+ minY = Math.min(minY, y);
174
+ maxX = Math.max(maxX, x);
175
+ maxY = Math.max(maxY, y);
176
+ }
177
+ const points = note.points.map(([x, y]) => [x - minX, y - minY]);
178
+ const w = Math.max(1, maxX - minX), h = Math.max(1, maxY - minY);
179
+ const nodes = Array.isArray(note.nodes) ? note.nodes.map(n => isRecord(n) ? { ...n, x: Number(n.x) - minX, y: Number(n.y) - minY } : n) : undefined;
180
+ const d = points.map(([x, y], i) => `${i ? 'L' : 'M'}${x} ${y}`).join(' ') + (note.closed ? ' Z' : '');
181
+ const width = typeof note.stroke === 'number' ? note.stroke : 3;
182
+ const css = { ...note.css, ...(note.stroke ? { 'stroke-width': width + 'px' } : {}),
183
+ ...(note.dash === 'dashed' ? { 'stroke-dasharray': `${3 * width} ${2.5 * width}` } : note.dash === 'dotted' ? { 'stroke-dasharray': `0.01 ${2 * width}` } : {}) };
184
+ return [id, { ...note, x: note.x + minX, y: note.y + minY, w, h, d, points, ...(nodes ? { nodes } : {}), ...(Object.keys(css).length ? { css } : {}) }];
185
+ }
186
+ if ((note.kind === 'line' || note.kind === 'arrow') && note.points?.length === 2) {
187
+ const [first, last] = note.points;
188
+ const { points, ...line } = note;
189
+ return [id, { ...line, x: note.x + first[0], y: note.y + first[1], x2: note.x + last[0], y2: note.y + last[1] }];
190
+ }
191
+ return [id, note];
192
+ }));
193
+ return `${JSON.stringify({ ...canvas, notes }, null, 2)}\n`;
132
194
  }
133
195
  /* -------------------------------------------------------------------------- */
134
196
  /* Pages */
package/src/check.js CHANGED
@@ -3,7 +3,7 @@
3
3
  * files, then every artboard file. Paths are relative to `project/`.
4
4
  */
5
5
  import { checkDcFile, isBoardPath } from '@open-artifact/dc';
6
- import { CANVAS_VERSION, DS_FOLDER_PATTERN, ID_PATTERN, MAX_BOARD_SIZE, MAX_GUIDES, MAX_NOTES, MAX_NOTE_GROUPS, MAX_PAGES, MIN_BOARD_SIZE, intersects, pageOf, } from "./canvas.js";
6
+ import { CANVAS_VERSION, DS_FOLDER_PATTERN, ID_PATTERN, MAX_BOARD_SIZE, MAX_GUIDES, MAX_NOTES, MAX_NOTE_GROUPS, MAX_PAGES, MIN_BOARD_SIZE, intersects, noteFontSize, pageOf, } from "./canvas.js";
7
7
  /** Most files a design holds (under `project/`). */
8
8
  export const MAX_PROJECT_FILES = 512;
9
9
  /** Most bytes a design holds: its files and uploads together. */
@@ -132,11 +132,30 @@ export function checkDesignProject(files, options = {}) {
132
132
  const note = notes[id];
133
133
  if (!ID_PATTERN.test(id))
134
134
  add('canvas.json', 'error', `Note id "${id}" must match [A-Za-z0-9_-]{1,40}.`);
135
- if (typeof note?.x !== 'number' || typeof note?.y !== 'number' || typeof note?.text !== 'string') {
136
- add('canvas.json', 'error', `Note "${id}" needs numeric x, y and a text.`);
135
+ if (typeof note?.x !== 'number' || typeof note?.y !== 'number') {
136
+ add('canvas.json', 'error', `Note "${id}" needs numeric x and y.`);
137
+ continue;
138
+ }
139
+ if ((!note.kind || note.kind === 'sticky' || note.kind === 'title1') && typeof note.text !== 'string')
140
+ add('canvas.json', 'error', `Note "${id}" needs text.`);
141
+ if (note.kind === 'link' && (typeof note.href !== 'string' || !note.href.trim()))
142
+ add('canvas.json', 'error', `Link card "${id}" needs an href.`);
143
+ if (note.page !== undefined && !pageIds.has(String(note.page)))
144
+ add('canvas.json', 'error', `Note "${id}" names unknown page "${String(note.page)}".`);
145
+ if (note.kind === 'title1' && noteFontSize(note.size, 'title1') >= 72 && typeof note.text === 'string') {
146
+ if (note.maxW === undefined)
147
+ add('canvas.json', 'warning', `Title note "${id}" should set maxW (its row's width).`);
148
+ const width = typeof note.maxW === 'number' && note.maxW > 0 ? note.maxW : Math.max(1, note.text.length) * noteFontSize(note.size, 'title1');
149
+ const page = note.page ?? pages[0]?.id;
150
+ const x = note.x, y = note.y;
151
+ const inColumn = entries.filter(([, b]) => (b.page ?? pages[0]?.id) === page && Number(b.x) < x + width && Number(b.x) + Number(b.w) > x);
152
+ const below = inColumn.filter(([, b]) => Number(b.y) >= y && Number(b.y) - y < 223).sort((a, b) => Number(a[1].y) - Number(b[1].y));
153
+ const over = inColumn.filter(([, b]) => Number(b.y) < y && Number(b.y) + Number(b.h) > y);
154
+ if (below.length)
155
+ add('canvas.json', 'error', `Title "${id}" is ${Number(below[0][1].y) - y} px above ${below[0][0]}. Leave 223 px above its row (y ≤ ${Number(below[0][1].y) - 223}).`);
156
+ if (over.length)
157
+ add('canvas.json', 'error', `Title "${id}" sits over ${over[0][0]}. Move it at least 223 px above its row.`);
137
158
  }
138
- if (note?.kind === 'title1' && note.maxW === undefined)
139
- add('canvas.json', 'warning', `Title note "${id}" should set maxW (its row's width).`);
140
159
  if (note?.groups !== undefined) {
141
160
  const groups = note.groups;
142
161
  if (!Array.isArray(groups) || groups.some((group) => typeof group !== 'string' || !ID_PATTERN.test(group))) {