@open-document/core 0.10.0 → 0.11.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.
@@ -0,0 +1,156 @@
1
+ /**
2
+ * The element panel's Format section, shared by the viewer and the dev server:
3
+ * which style keys it may write, which design tokens each one offers, and what
4
+ * a value has to look like before it is spliced into a document's source.
5
+ */
6
+
7
+ export const STYLE_PROPS = [
8
+ 'fontFamily',
9
+ 'fontSize',
10
+ 'fontWeight',
11
+ 'fontStyle',
12
+ 'textAlign',
13
+ 'lineHeight',
14
+ 'letterSpacing',
15
+ 'color',
16
+ 'background',
17
+ ] as const;
18
+
19
+ export type StyleProp = (typeof STYLE_PROPS)[number];
20
+
21
+ /** `null` takes the key off the element, so the value comes from wherever it did before. */
22
+ export type StyleValue = string | number | null;
23
+ export type StyleChanges = Partial<Record<StyleProp, StyleValue>>;
24
+
25
+ /** What the source says about one key: a literal, a design token, or code the panel can only show. */
26
+ export type StyleSource =
27
+ | { kind: 'literal'; value: string | number }
28
+ | { kind: 'token'; token: string }
29
+ | { kind: 'code'; code: string };
30
+
31
+ /** `from` names the shared style object the key comes from, when it is not the element's own. */
32
+ export type StyleOrigin = StyleSource & { from?: string };
33
+
34
+ export type StyleInfo =
35
+ | { editable: true; props: Partial<Record<StyleProp, StyleOrigin>> }
36
+ | { editable: false; reason: string; props?: undefined };
37
+
38
+ export function isStyleProp(value: unknown): value is StyleProp {
39
+ return typeof value === 'string' && (STYLE_PROPS as readonly string[]).includes(value);
40
+ }
41
+
42
+ export type TokenGroup = 'font' | 'size' | 'color' | 'leading';
43
+
44
+ /** The `--od-*` variables a document's design const defines, by what they can stand for. */
45
+ export const TOKENS: Record<TokenGroup, Array<{ token: string; label: string }>> = {
46
+ font: [
47
+ { token: 'font-heading', label: 'Heading' },
48
+ { token: 'font-body', label: 'Body' },
49
+ { token: 'font-mono', label: 'Mono' },
50
+ ],
51
+ size: [
52
+ { token: 'size-title', label: 'Title' },
53
+ { token: 'size-h1', label: 'H1' },
54
+ { token: 'size-h2', label: 'H2' },
55
+ { token: 'size-h3', label: 'H3' },
56
+ { token: 'size-body', label: 'Body' },
57
+ { token: 'size-caption', label: 'Caption' },
58
+ ],
59
+ color: [
60
+ { token: 'text', label: 'Text' },
61
+ { token: 'muted', label: 'Muted' },
62
+ { token: 'accent', label: 'Accent' },
63
+ { token: 'rule', label: 'Rule' },
64
+ { token: 'bg', label: 'Background' },
65
+ ],
66
+ leading: [{ token: 'leading', label: 'Leading' }],
67
+ };
68
+
69
+ export const PROP_TOKENS: Partial<Record<StyleProp, TokenGroup>> = {
70
+ fontFamily: 'font',
71
+ fontSize: 'size',
72
+ color: 'color',
73
+ background: 'color',
74
+ lineHeight: 'leading',
75
+ };
76
+
77
+ const TOKEN_VALUE = /^var\(\s*--od-([a-z0-9-]+)\s*\)$/;
78
+
79
+ /** `var(--od-accent)` → `accent`; anything else is not a token reference. */
80
+ export function tokenOf(value: string): string | null {
81
+ return TOKEN_VALUE.exec(value.trim())?.[1] ?? null;
82
+ }
83
+
84
+ export function tokenValue(token: string): string {
85
+ return `var(--od-${token})`;
86
+ }
87
+
88
+ function isToken(prop: StyleProp, value: string): boolean {
89
+ const group = PROP_TOKENS[prop];
90
+ const token = tokenOf(value);
91
+ return group !== undefined && token !== null && TOKENS[group].some((t) => t.token === token);
92
+ }
93
+
94
+ const LENGTH = /^-?\d+(\.\d+)?(px|em|rem)$/;
95
+ const HEX = /^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/;
96
+ // Everything written is a quoted string literal, so nothing here can become
97
+ // code; this keeps out what would end a declaration in exported CSS instead.
98
+ const PLAIN = /^[^;{}<>\\\n\r]{1,200}$/;
99
+
100
+ /** Whether the panel may write this value for this key — the server's check, run again in the viewer. */
101
+ export function isValidStyleValue(prop: StyleProp, value: StyleValue): boolean {
102
+ if (value === null) return true;
103
+ if (typeof value === 'number') {
104
+ if (!Number.isFinite(value)) return false;
105
+ if (prop === 'fontWeight') return value >= 100 && value <= 900 && value % 100 === 0;
106
+ if (prop === 'lineHeight') return value > 0 && value <= 10;
107
+ return false;
108
+ }
109
+ if (!PLAIN.test(value)) return false;
110
+ if (isToken(prop, value)) return true;
111
+ switch (prop) {
112
+ case 'fontFamily':
113
+ return tokenOf(value) === null;
114
+ case 'fontSize':
115
+ return LENGTH.test(value) && !value.startsWith('-');
116
+ case 'letterSpacing':
117
+ return LENGTH.test(value) || value === 'normal';
118
+ case 'fontWeight':
119
+ return value === 'normal' || value === 'bold';
120
+ case 'fontStyle':
121
+ return value === 'normal' || value === 'italic';
122
+ case 'textAlign':
123
+ return ['left', 'center', 'right', 'justify', 'start', 'end'].includes(value);
124
+ case 'lineHeight':
125
+ return value === 'normal';
126
+ case 'color':
127
+ case 'background':
128
+ return HEX.test(value) || value === 'transparent';
129
+ }
130
+ }
131
+
132
+ /** `fontSize` → `font-size`, for `style.setProperty`. */
133
+ export function cssName(prop: StyleProp): string {
134
+ return prop.replace(/[A-Z]/g, (char) => `-${char.toLowerCase()}`);
135
+ }
136
+
137
+ /** A value as CSS, the way React would render it onto the element. */
138
+ export function cssValue(prop: StyleProp, value: string | number): string {
139
+ return typeof value === 'number' && prop !== 'fontWeight' && prop !== 'lineHeight'
140
+ ? `${value}px`
141
+ : String(value);
142
+ }
143
+
144
+ /** `rgb(29, 78, 216)` → `#1d4ed8`; a translucent or unparseable colour is `null`. */
145
+ export function rgbToHex(value: string): string | null {
146
+ const match = /^rgba?\(\s*(\d+)[\s,]+(\d+)[\s,]+(\d+)(?:[\s,/]+([\d.]+%?))?\s*\)$/.exec(value);
147
+ if (!match) return HEX.test(value) ? value.toLowerCase() : null;
148
+ const alpha = match[4];
149
+ if (alpha !== undefined) {
150
+ const a = alpha.endsWith('%') ? Number.parseFloat(alpha) / 100 : Number.parseFloat(alpha);
151
+ if (a < 1) return null;
152
+ }
153
+ return `#${[match[1], match[2], match[3]]
154
+ .map((part) => Number(part).toString(16).padStart(2, '0'))
155
+ .join('')}`;
156
+ }
@@ -204,6 +204,7 @@ export function Doc() {
204
204
  const [selection, setSelection] = useState<PageSelection>({ kind: 'all' });
205
205
  const [customRange, setCustomRange] = useState('');
206
206
  const [designOpen, setDesignOpen] = useState(false);
207
+ const closeDesign = useCallback(() => setDesignOpen(false), []);
207
208
  const [editing, setEditingState] = useState(readEditing);
208
209
  // The dev server reloads every open viewer when a document is added or
209
210
  // removed anywhere in the workspace. Edit mode is per tab and survives that.
@@ -858,6 +859,7 @@ export function Doc() {
858
859
  docId={docId}
859
860
  containerRef={scrollRef}
860
861
  panelHidden={designOpen}
862
+ onPick={closeDesign}
861
863
  quiet={cardShown}
862
864
  onExit={() => setEditing(false)}
863
865
  exitRef={leaveEditRef}