@open-document/core 0.9.1 → 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.
- package/dist/{build-B0GJXy8I.js → build-BHFGY8eF.js} +1 -1
- package/dist/{check-CuDIWsPT.js → check-UEBmDfT_.js} +3 -3
- package/dist/cli/bin.js +10 -6
- package/dist/{config-CAZ7Z_va.js → config-B_pdNbYg.js} +1166 -591
- package/dist/{context-PWAh9qbf.js → context-BM9_-u9P.js} +1 -1
- package/dist/{dev-CYqppXDI.js → dev-DX3QMIRi.js} +1 -1
- package/dist/{diagram-CCaUUnRe.js → diagram-BK4jTZr4.js} +146 -146
- package/dist/diff-DayvbXfX.js +44 -0
- package/dist/{export-1JtO2OMC.js → export-BPsXj1i6.js} +3 -3
- package/dist/{import-CsrdBOZ8.js → import-71wlAsrd.js} +2 -2
- package/dist/index.d.ts +53 -2
- package/dist/index.js +457 -18
- package/dist/ops/index.d.ts +72 -17
- package/dist/ops/index.js +3 -3
- package/dist/{ops-B2y9BMpk.js → ops-BUo67DaP.js} +642 -124
- package/dist/{preview-DW3PM-JV.js → preview-D3m1-gUL.js} +1 -1
- package/dist/{sdk-BqU6mwD_.d.ts → sdk-CpDEGRIo.d.ts} +2 -0
- package/dist/vite/index.js +1 -1
- package/package.json +2 -2
- package/skills/doc-authoring/SKILL.md +9 -4
- package/skills/doc-authoring/references/pagination.md +1 -1
- package/skills/doc-authoring/references/tables-and-charts.md +18 -1
- package/src/app/components/browser/browser-ui.tsx +105 -0
- package/src/app/components/chart.tsx +239 -0
- package/src/app/components/command-palette.tsx +284 -0
- package/src/app/components/design-panel/design-panel.tsx +22 -20
- package/src/app/components/design-panel/design-provider.tsx +4 -2
- package/src/app/components/diagram.tsx +11 -1
- package/src/app/components/doc-assets.tsx +6 -5
- package/src/app/components/doc-search.tsx +12 -10
- package/src/app/components/doc-sidebar.tsx +24 -18
- package/src/app/components/flow-page.tsx +2 -0
- package/src/app/components/inspector/format-section.tsx +434 -0
- package/src/app/components/inspector/inspector.tsx +310 -89
- package/src/app/components/inspector/use-style-edits.ts +210 -0
- package/src/app/components/language-toggle.tsx +38 -0
- package/src/app/components/palette-items.ts +128 -0
- package/src/app/components/panel/edit-save-card.tsx +4 -2
- package/src/app/components/panel/fields.tsx +100 -9
- package/src/app/components/panel/save-card.tsx +18 -10
- package/src/app/components/sidebar/folder-item.tsx +10 -8
- package/src/app/components/sidebar/icon-picker.tsx +3 -1
- package/src/app/components/sidebar/sidebar.tsx +44 -13
- package/src/app/components/theme-toggle.tsx +5 -3
- package/src/app/components/ui/menu.tsx +5 -0
- package/src/app/lib/agent-bridge.ts +8 -2
- package/src/app/lib/chart.test.ts +108 -0
- package/src/app/lib/chart.ts +343 -0
- package/src/app/lib/doc-preview.tsx +0 -10
- package/src/app/lib/docs.ts +3 -0
- package/src/app/lib/docx/from-dom.ts +11 -11
- package/src/app/lib/docx/ooxml.test.ts +25 -9
- package/src/app/lib/docx/ooxml.ts +58 -37
- package/src/app/lib/export-docx.ts +13 -8
- package/src/app/lib/flow.ts +1 -1
- package/src/app/lib/i18n-ja/browser.ts +88 -0
- package/src/app/lib/i18n-ja/doc.ts +65 -0
- package/src/app/lib/i18n-ja/index.ts +5 -0
- package/src/app/lib/i18n-ja/panels.ts +151 -0
- package/src/app/lib/i18n-ko/browser.ts +87 -0
- package/src/app/lib/i18n-ko/doc.ts +65 -0
- package/src/app/lib/i18n-ko/index.ts +5 -0
- package/src/app/lib/i18n-ko/panels.ts +148 -0
- package/src/app/lib/i18n-zh-cn/browser.ts +84 -0
- package/src/app/lib/i18n-zh-cn/doc.ts +64 -0
- package/src/app/lib/i18n-zh-cn/index.ts +5 -0
- package/src/app/lib/i18n-zh-cn/panels.ts +143 -0
- package/src/app/lib/i18n-zh-tw/browser.ts +84 -0
- package/src/app/lib/i18n-zh-tw/doc.ts +64 -0
- package/src/app/lib/i18n-zh-tw/index.ts +5 -0
- package/src/app/lib/i18n-zh-tw/panels.ts +144 -0
- package/src/app/lib/i18n.test.ts +24 -0
- package/src/app/lib/i18n.tsx +109 -0
- package/src/app/lib/inspector/format.ts +156 -0
- package/src/app/lib/outline.ts +23 -0
- package/src/app/lib/page-range.test.ts +10 -1
- package/src/app/lib/page-range.ts +26 -0
- package/src/app/lib/sdk.ts +2 -0
- package/src/app/main.tsx +4 -1
- package/src/app/routes/assets.tsx +193 -132
- package/src/app/routes/doc.tsx +668 -310
- package/src/app/routes/home-shell.tsx +115 -88
- package/src/app/routes/home.tsx +200 -67
- package/src/app/routes/themes.tsx +42 -35
- package/src/app/styles.css +84 -5
- package/src/app/virtual.d.ts +1 -0
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
export const PANELS: Record<string, string> = {
|
|
2
|
+
Design: '設計',
|
|
3
|
+
'This document has no design const yet — saving writes one':
|
|
4
|
+
'這份文件還沒有 design 常數,儲存時會寫入一個',
|
|
5
|
+
Draft: '草稿',
|
|
6
|
+
'Unsaved changes': '未儲存的變更',
|
|
7
|
+
'Shuffle preset': '隨機套用預設組合',
|
|
8
|
+
'Reset to defaults': '重設為預設值',
|
|
9
|
+
'Close design panel (D)': '關閉設計面板 (D)',
|
|
10
|
+
Palette: '色彩',
|
|
11
|
+
Fonts: '字型',
|
|
12
|
+
'Type scale': '字級',
|
|
13
|
+
Page: '頁面',
|
|
14
|
+
Sheet: '紙張',
|
|
15
|
+
Text: '文字',
|
|
16
|
+
Muted: '次要文字',
|
|
17
|
+
Accent: '強調色',
|
|
18
|
+
Rule: '線條',
|
|
19
|
+
Heading: '標題',
|
|
20
|
+
Body: '內文',
|
|
21
|
+
Mono: '等寬',
|
|
22
|
+
Title: '大標題',
|
|
23
|
+
Caption: '圖說',
|
|
24
|
+
'System sans': '系統無襯線體',
|
|
25
|
+
'Georgia serif': 'Georgia 襯線體',
|
|
26
|
+
'Times serif': 'Times 襯線體',
|
|
27
|
+
'Body under 12px prints below 9pt — hard to read on paper.':
|
|
28
|
+
'內文小於 12px 時印出來不到 9pt,在紙上不易閱讀。',
|
|
29
|
+
Margin: '邊界',
|
|
30
|
+
Leading: '行高',
|
|
31
|
+
Radius: '圓角',
|
|
32
|
+
'Failed to save': '儲存失敗',
|
|
33
|
+
'{label} swatch': '{label}色票',
|
|
34
|
+
'Custom (from source)': '自訂(來自原始碼)',
|
|
35
|
+
Undo: '復原',
|
|
36
|
+
Redo: '重做',
|
|
37
|
+
Saved: '已儲存',
|
|
38
|
+
'{count} unsaved change': '{count} 項未儲存的變更',
|
|
39
|
+
'{count} unsaved changes': '{count} 項未儲存的變更',
|
|
40
|
+
Discard: '捨棄',
|
|
41
|
+
Save: '儲存',
|
|
42
|
+
Saving: '儲存中',
|
|
43
|
+
'Not everything was saved': '有部分變更未能儲存',
|
|
44
|
+
'Find in document': '在文件中尋找',
|
|
45
|
+
Find: '尋找',
|
|
46
|
+
'Previous match': '上一個符合項目',
|
|
47
|
+
'Next match': '下一個符合項目',
|
|
48
|
+
'Close search': '關閉搜尋',
|
|
49
|
+
'Text formatting': '文字格式',
|
|
50
|
+
'Link address': '連結網址',
|
|
51
|
+
'https://… or #section': 'https://… 或 #章節',
|
|
52
|
+
'Only web, mail, phone, or in-document addresses': '只接受網址、電子郵件、電話或文件內的位置',
|
|
53
|
+
'Apply link': '套用連結',
|
|
54
|
+
Apply: '套用',
|
|
55
|
+
'Remove link': '移除連結',
|
|
56
|
+
Bold: '粗體',
|
|
57
|
+
Italic: '斜體',
|
|
58
|
+
Code: '程式碼',
|
|
59
|
+
'Already bold — this element’s own style sets it': '已經是粗體:這個元素本身的樣式就設定了粗體',
|
|
60
|
+
Link: '連結',
|
|
61
|
+
'Clear formatting': '清除格式',
|
|
62
|
+
'Select words to format': '請先選取要套用格式的文字',
|
|
63
|
+
'This text is passed in as a string — formatting needs text written in the document':
|
|
64
|
+
'這段文字是以字串傳入的,只有直接寫在文件中的文字才能套用格式',
|
|
65
|
+
'The text on the page does not line up with its source, so it cannot be edited here.':
|
|
66
|
+
'頁面上的文字與原始碼對不上,因此無法在這裡編輯。',
|
|
67
|
+
'Discarded unsaved edits': '已捨棄未儲存的編輯',
|
|
68
|
+
'Save failed': '儲存失敗',
|
|
69
|
+
'Saved to source': '已儲存至原始碼',
|
|
70
|
+
'{failed} of {total} not saved — {reason}': '{total} 項中有 {failed} 項未儲存:{reason}',
|
|
71
|
+
refused: '遭到拒絕',
|
|
72
|
+
'The document reloaded with new text here — {count} unsaved edit was lost':
|
|
73
|
+
'文件已重新載入,這裡的文字已變更,{count} 項未儲存的編輯已遺失',
|
|
74
|
+
'The document reloaded with new text here — {count} unsaved edits were lost':
|
|
75
|
+
'文件已重新載入,這裡的文字已變更,{count} 項未儲存的編輯已遺失',
|
|
76
|
+
'Type inside one run of text — the markup between runs stays as written.':
|
|
77
|
+
'請在同一段文字內輸入,段落之間的標記會維持原樣。',
|
|
78
|
+
'source text does not match this element': '原始碼中的文字與這個元素不符',
|
|
79
|
+
'could not read source': '無法讀取原始碼',
|
|
80
|
+
'This text cannot be edited here.': '這段文字無法在這裡編輯。',
|
|
81
|
+
'Marked in source — run /apply-comments': '已在原始碼中標記,請執行 /apply-comments',
|
|
82
|
+
'Could not add comment': '無法新增註解',
|
|
83
|
+
'Enter to keep · Shift+Enter for a new line · Esc to revert':
|
|
84
|
+
'Enter 保留 · Shift+Enter 換行 · Esc 還原',
|
|
85
|
+
'Double-click or Enter to edit text · Esc to deselect':
|
|
86
|
+
'按兩下或按 Enter 編輯文字 · Esc 取消選取',
|
|
87
|
+
'Click to select · double-click to edit · Esc to leave': '按一下選取 · 按兩下編輯 · Esc 離開',
|
|
88
|
+
Element: '元素',
|
|
89
|
+
Close: '關閉',
|
|
90
|
+
'Not editable here.': '無法在這裡編輯。',
|
|
91
|
+
'Comment for the agent': '給 agent 的註解',
|
|
92
|
+
'make this bold, shorten to one line…': '改成粗體、縮短成一行…',
|
|
93
|
+
'Mark comment': '標記註解',
|
|
94
|
+
Typography: '字體排印',
|
|
95
|
+
Color: '顏色',
|
|
96
|
+
Font: '字型',
|
|
97
|
+
Size: '大小',
|
|
98
|
+
'Size in pixels': '大小(像素)',
|
|
99
|
+
Weight: '粗細',
|
|
100
|
+
'Line height': '行高',
|
|
101
|
+
'Letter spacing in pixels': '字距(像素)',
|
|
102
|
+
Fill: '填色',
|
|
103
|
+
Background: '背景',
|
|
104
|
+
None: '無',
|
|
105
|
+
Inherited: '繼承',
|
|
106
|
+
'From {name}': '來自 {name}',
|
|
107
|
+
'from {name}': '來自 {name}',
|
|
108
|
+
'set in code: {code}': '由程式碼設定:{code}',
|
|
109
|
+
Custom: '自訂',
|
|
110
|
+
'Align left': '靠左對齊',
|
|
111
|
+
'Align center': '置中對齊',
|
|
112
|
+
'Align right': '靠右對齊',
|
|
113
|
+
Justify: '左右對齊',
|
|
114
|
+
'This is a component — its styles are set inside it, not here.':
|
|
115
|
+
'這是元件,它的樣式在元件內部設定,不在這裡。',
|
|
116
|
+
'This element’s style is not written as an object.': '這個元素的 style 不是以物件撰寫的。',
|
|
117
|
+
'source does not match this element': '原始碼與這個元素不符',
|
|
118
|
+
'element not found': '找不到元素',
|
|
119
|
+
'Light 300': '細 300',
|
|
120
|
+
'Regular 400': '一般 400',
|
|
121
|
+
'Medium 500': '中等 500',
|
|
122
|
+
'Semibold 600': '半粗 600',
|
|
123
|
+
'Bold 700': '粗 700',
|
|
124
|
+
'Extrabold 800': '特粗 800',
|
|
125
|
+
'Heading {level}': '標題 {level}',
|
|
126
|
+
Paragraph: '段落',
|
|
127
|
+
List: '清單',
|
|
128
|
+
Table: '表格',
|
|
129
|
+
Image: '圖片',
|
|
130
|
+
Box: '區塊',
|
|
131
|
+
Alignment: '對齊',
|
|
132
|
+
Emphasis: '強調',
|
|
133
|
+
'Edit on page': '在頁面上編輯',
|
|
134
|
+
Source: '原始碼',
|
|
135
|
+
Location: '位置',
|
|
136
|
+
'Letter spacing': '字距',
|
|
137
|
+
'Text color': '文字顏色',
|
|
138
|
+
'{label} token': '{label} token',
|
|
139
|
+
'Inherited font': '繼承的字型',
|
|
140
|
+
'{name} font': '{name}字型',
|
|
141
|
+
'Custom font': '自訂字型',
|
|
142
|
+
Done: '完成',
|
|
143
|
+
'{count} elements print from this line of source': '這一行原始碼印出 {count} 個元素',
|
|
144
|
+
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { DICTIONARIES } from './i18n';
|
|
3
|
+
|
|
4
|
+
const placeholders = (text: string) => [...text.matchAll(/\{(\w+)\}/g)].map((m) => m[1]).sort();
|
|
5
|
+
|
|
6
|
+
const translated = Object.entries(DICTIONARIES).filter(([locale]) => locale !== 'en');
|
|
7
|
+
const reference = Object.keys(DICTIONARIES['zh-TW']).sort();
|
|
8
|
+
|
|
9
|
+
describe.each(translated)('%s dictionary', (_locale, dictionary) => {
|
|
10
|
+
it('translates exactly the strings the others do', () => {
|
|
11
|
+
expect(Object.keys(dictionary).sort()).toEqual(reference);
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
it('keeps every placeholder of the English key, and no others', () => {
|
|
15
|
+
const broken = Object.entries(dictionary).filter(
|
|
16
|
+
([en, text]) => placeholders(en).join() !== placeholders(text).join(),
|
|
17
|
+
);
|
|
18
|
+
expect(broken).toEqual([]);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it('has no empty translations', () => {
|
|
22
|
+
expect(Object.entries(dictionary).filter(([, text]) => text.trim() === '')).toEqual([]);
|
|
23
|
+
});
|
|
24
|
+
});
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createContext,
|
|
3
|
+
type ReactNode,
|
|
4
|
+
useCallback,
|
|
5
|
+
useContext,
|
|
6
|
+
useEffect,
|
|
7
|
+
useMemo,
|
|
8
|
+
useRef,
|
|
9
|
+
useState,
|
|
10
|
+
} from 'react';
|
|
11
|
+
import { JA } from './i18n-ja';
|
|
12
|
+
import { KO } from './i18n-ko';
|
|
13
|
+
import { ZH_CN } from './i18n-zh-cn';
|
|
14
|
+
import { ZH_TW } from './i18n-zh-tw';
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* The viewer chrome in English, Chinese, Japanese or Korean. The English text is the
|
|
18
|
+
* key, so a string with no translation yet still reads — in English — instead
|
|
19
|
+
* of showing a key. Only the chrome is translated: what prints on the page is
|
|
20
|
+
* the document's, and `meta.labels` already decides what a figure is called.
|
|
21
|
+
*/
|
|
22
|
+
export const LOCALES = [
|
|
23
|
+
{ value: 'en', label: 'English' },
|
|
24
|
+
{ value: 'zh-TW', label: '繁體中文' },
|
|
25
|
+
{ value: 'zh-CN', label: '简体中文' },
|
|
26
|
+
{ value: 'ja', label: '日本語' },
|
|
27
|
+
{ value: 'ko', label: '한국어' },
|
|
28
|
+
] as const;
|
|
29
|
+
export type Locale = (typeof LOCALES)[number]['value'];
|
|
30
|
+
|
|
31
|
+
const STORAGE = 'open-doc:locale';
|
|
32
|
+
export const DICTIONARIES: Record<Locale, Record<string, string>> = {
|
|
33
|
+
en: {},
|
|
34
|
+
'zh-TW': ZH_TW,
|
|
35
|
+
'zh-CN': ZH_CN,
|
|
36
|
+
ja: JA,
|
|
37
|
+
ko: KO,
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
const isLocale = (value: string | null): value is Locale =>
|
|
41
|
+
LOCALES.some((entry) => entry.value === value);
|
|
42
|
+
|
|
43
|
+
/** The reader's browser language, mapped onto a locale we have; Chinese splits by script. */
|
|
44
|
+
function browserLocale(language: string): Locale {
|
|
45
|
+
if (/^zh-(TW|HK|MO)\b|^zh-Hant/i.test(language)) return 'zh-TW';
|
|
46
|
+
if (/^zh/i.test(language)) return 'zh-CN';
|
|
47
|
+
if (/^ja/i.test(language)) return 'ja';
|
|
48
|
+
if (/^ko/i.test(language)) return 'ko';
|
|
49
|
+
return 'en';
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function initialLocale(): Locale {
|
|
53
|
+
try {
|
|
54
|
+
const stored = localStorage.getItem(STORAGE);
|
|
55
|
+
if (isLocale(stored)) return stored;
|
|
56
|
+
} catch {}
|
|
57
|
+
return typeof navigator === 'undefined' ? 'en' : browserLocale(navigator.language);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export type Translate = (text: string, vars?: Record<string, string | number>) => string;
|
|
61
|
+
|
|
62
|
+
function translate(locale: Locale, text: string, vars?: Record<string, string | number>): string {
|
|
63
|
+
const template = DICTIONARIES[locale][text] ?? text;
|
|
64
|
+
if (!vars) return template;
|
|
65
|
+
return template.replace(/\{(\w+)\}/g, (whole, name: string) =>
|
|
66
|
+
name in vars ? String(vars[name]) : whole,
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
type LocaleState = { locale: Locale; setLocale: (locale: Locale) => void; t: Translate };
|
|
71
|
+
|
|
72
|
+
const LocaleContext = createContext<LocaleState>({
|
|
73
|
+
locale: 'en',
|
|
74
|
+
setLocale: () => {},
|
|
75
|
+
t: (text, vars) => translate('en', text, vars),
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
export function LocaleProvider({ children }: { children: ReactNode }) {
|
|
79
|
+
const [locale, setLocaleState] = useState<Locale>(initialLocale);
|
|
80
|
+
|
|
81
|
+
useEffect(() => {
|
|
82
|
+
document.documentElement.lang = locale;
|
|
83
|
+
}, [locale]);
|
|
84
|
+
|
|
85
|
+
const setLocale = useCallback((next: Locale) => {
|
|
86
|
+
setLocaleState(next);
|
|
87
|
+
try {
|
|
88
|
+
localStorage.setItem(STORAGE, next);
|
|
89
|
+
} catch {}
|
|
90
|
+
}, []);
|
|
91
|
+
|
|
92
|
+
// `t` keeps one identity for the app's lifetime and reads the locale when
|
|
93
|
+
// called. Components still re-render on a switch — the context value changes —
|
|
94
|
+
// but an effect that lists `t` does not re-run, so switching language never
|
|
95
|
+
// tears down an edit in progress or refetches what the inspector holds.
|
|
96
|
+
const localeRef = useRef(locale);
|
|
97
|
+
localeRef.current = locale;
|
|
98
|
+
const t = useCallback<Translate>((text, vars) => translate(localeRef.current, text, vars), []);
|
|
99
|
+
const value = useMemo(() => ({ locale, setLocale, t }), [locale, setLocale, t]);
|
|
100
|
+
return <LocaleContext.Provider value={value}>{children}</LocaleContext.Provider>;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export function useLocale(): LocaleState {
|
|
104
|
+
return useContext(LocaleContext);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export function useT(): Translate {
|
|
108
|
+
return useContext(LocaleContext).t;
|
|
109
|
+
}
|
|
@@ -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
|
+
}
|
package/src/app/lib/outline.ts
CHANGED
|
@@ -58,6 +58,29 @@ export function collectOutline(root: ParentNode): OutlineEntry[] {
|
|
|
58
58
|
return entries;
|
|
59
59
|
}
|
|
60
60
|
|
|
61
|
+
/**
|
|
62
|
+
* A PDF's bookmarks and heading tags are built from the accessibility tree,
|
|
63
|
+
* which knows nothing of the outline's rules: it would list the cover title,
|
|
64
|
+
* "Contents", every h4, and the long text of a heading the author shortened.
|
|
65
|
+
* This marks the copy so the tree says what the outline says — every listed
|
|
66
|
+
* heading as a heading at its outline level and with its outline text, every
|
|
67
|
+
* other heading as plain text.
|
|
68
|
+
*/
|
|
69
|
+
export function exposeOutline(root: ParentNode): void {
|
|
70
|
+
const listed = new Set(collectOutline(root).map((entry) => entry.id));
|
|
71
|
+
for (const el of Array.from(
|
|
72
|
+
root.querySelectorAll<HTMLElement>('h1, h2, h3, h4, h5, h6, [data-od-heading]'),
|
|
73
|
+
)) {
|
|
74
|
+
if (el.id && listed.has(el.id)) {
|
|
75
|
+
el.setAttribute('role', 'heading');
|
|
76
|
+
el.setAttribute('aria-level', String(levelOf(el)));
|
|
77
|
+
el.setAttribute('aria-label', textOf(el));
|
|
78
|
+
} else {
|
|
79
|
+
el.setAttribute('role', 'none');
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
61
84
|
// Shared through globalThis for the same reason as the page context: the
|
|
62
85
|
// viewer writes the outline from the source copy of this module, while a
|
|
63
86
|
// document's `<TableOfContents>` reads it from the published bundle.
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
11
|
import { describe, expect, it } from 'vitest';
|
|
12
|
-
import { describeSelection, parseRange, resolveSelection } from './page-range';
|
|
12
|
+
import { describeSelection, formatPages, parseRange, resolveSelection } from './page-range';
|
|
13
13
|
|
|
14
14
|
describe('parseRange', () => {
|
|
15
15
|
it('reads single pages, one-based', () => {
|
|
@@ -93,3 +93,12 @@ describe('describeSelection', () => {
|
|
|
93
93
|
});
|
|
94
94
|
});
|
|
95
95
|
});
|
|
96
|
+
|
|
97
|
+
describe('formatPages', () => {
|
|
98
|
+
it('writes runs back the way a reader types them', () => {
|
|
99
|
+
expect(formatPages([0, 1, 2, 5])).toBe('1–3, 6');
|
|
100
|
+
expect(formatPages([4])).toBe('5');
|
|
101
|
+
expect(formatPages([0, 2, 4])).toBe('1, 3, 5');
|
|
102
|
+
expect(formatPages([])).toBe('');
|
|
103
|
+
});
|
|
104
|
+
});
|
|
@@ -85,6 +85,32 @@ export function describeSelection(
|
|
|
85
85
|
return { count: pages?.length ?? 0, valid: pages !== null };
|
|
86
86
|
}
|
|
87
87
|
|
|
88
|
+
/**
|
|
89
|
+
* Zero-based indices as the reader would write them back: `[0,1,2,5]` →
|
|
90
|
+
* `1–3, 6`. Shown under the range field so a typo is caught before the
|
|
91
|
+
* download, not after.
|
|
92
|
+
*/
|
|
93
|
+
export function formatPages(indices: number[]): string {
|
|
94
|
+
const runs: string[] = [];
|
|
95
|
+
let start: number | undefined;
|
|
96
|
+
let prev: number | undefined;
|
|
97
|
+
const flush = () => {
|
|
98
|
+
if (start === undefined || prev === undefined) return;
|
|
99
|
+
runs.push(start === prev ? `${start + 1}` : `${start + 1}–${prev + 1}`);
|
|
100
|
+
};
|
|
101
|
+
for (const index of indices) {
|
|
102
|
+
if (prev !== undefined && index === prev + 1) {
|
|
103
|
+
prev = index;
|
|
104
|
+
continue;
|
|
105
|
+
}
|
|
106
|
+
flush();
|
|
107
|
+
start = index;
|
|
108
|
+
prev = index;
|
|
109
|
+
}
|
|
110
|
+
flush();
|
|
111
|
+
return runs.join(', ');
|
|
112
|
+
}
|
|
113
|
+
|
|
88
114
|
function clamp(value: number, min: number, max: number): number {
|
|
89
115
|
return Math.min(max, Math.max(min, value));
|
|
90
116
|
}
|
package/src/app/lib/sdk.ts
CHANGED
|
@@ -55,6 +55,8 @@ export type DocPage = ComponentType;
|
|
|
55
55
|
export type FlowSection = {
|
|
56
56
|
readonly __odFlow: true;
|
|
57
57
|
blocks: ReactNode[];
|
|
58
|
+
/** Rendered on every page the section expands into, in the top margin band. */
|
|
59
|
+
header?: ComponentType;
|
|
58
60
|
/** Rendered on every page the section expands into. */
|
|
59
61
|
footer?: ComponentType;
|
|
60
62
|
/** Page padding override in px; defaults to the design's `margin`. */
|
package/src/app/main.tsx
CHANGED
|
@@ -2,13 +2,16 @@ import { ThemeProvider } from 'next-themes';
|
|
|
2
2
|
import { StrictMode } from 'react';
|
|
3
3
|
import { createRoot } from 'react-dom/client';
|
|
4
4
|
import { App } from './app';
|
|
5
|
+
import { LocaleProvider } from './lib/i18n';
|
|
5
6
|
import './styles.css';
|
|
6
7
|
|
|
7
8
|
// biome-ignore lint/style/noNonNullAssertion: #root is guaranteed by index.html
|
|
8
9
|
createRoot(document.getElementById('root')!).render(
|
|
9
10
|
<StrictMode>
|
|
10
11
|
<ThemeProvider attribute="class" defaultTheme="system" enableSystem disableTransitionOnChange>
|
|
11
|
-
<
|
|
12
|
+
<LocaleProvider>
|
|
13
|
+
<App />
|
|
14
|
+
</LocaleProvider>
|
|
12
15
|
</ThemeProvider>
|
|
13
16
|
</StrictMode>,
|
|
14
17
|
);
|