merong-docs 0.1.11 → 1.0.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/OVERVIEW.md +19 -19
- package/OVERVIEW.zh-CN.md +19 -22
- package/README.md +77 -77
- package/README.zh-CN.md +81 -81
- package/client/android-file-share.ts +25 -25
- package/client/controls.tsx +163 -163
- package/client/document-panel.tsx +166 -166
- package/client/external-file.tsx +78 -78
- package/client/file-reader.tsx +43 -43
- package/client/markdown-panel.tsx +737 -737
- package/client/markdown-scroll.ts +122 -122
- package/client/messages.ts +224 -224
- package/client/native-contacts.ts +145 -145
- package/client/page-render-queue.ts +12 -12
- package/client/page-selection.tsx +227 -227
- package/client/page-tools.tsx +72 -72
- package/client/pdf-geometry.ts +96 -96
- package/client/pdf-panel.tsx +477 -477
- package/client/reader-find.tsx +190 -190
- package/client/reader-notice.tsx +46 -46
- package/client/settings.tsx +143 -143
- package/client/syntax.tsx +55 -55
- package/client/viewport-geometry.ts +61 -61
- package/client/web.ts +462 -462
- package/client/worksheet-panel.tsx +631 -631
- package/client/zoom-controls.tsx +140 -140
- package/index.client.tsx +36 -36
- package/index.server.ts +73 -73
- package/node_modules/markdown-it/bin/markdown-it.mjs +0 -0
- package/node_modules/yaml/bin.mjs +0 -0
- package/npm-shrinkwrap.json +2 -2
- package/package.json +1 -1
- package/scripts/prepare-office.mjs +20 -20
- package/server/engine.ts +89 -89
- package/server/external-file.ts +42 -42
- package/server/generated/document-worker.ts +1 -1
- package/server/generated/rust-reader_bg.wasm +0 -0
- package/server/markdown-index.ts +305 -305
- package/server/markdown-links.ts +51 -51
- package/server/markdown-page.ts +120 -120
- package/server/office-fonts.cjs +824 -824
- package/server/office-location.ts +41 -41
- package/server/office-preview.ts +186 -186
- package/server/office-worker.cjs +49 -49
- package/server/office-xml.ts +79 -79
- package/server/pdf-engine.ts +168 -168
- package/server/pdf-text-geometry.ts +60 -60
- package/server/rust-reader.ts +40 -40
- package/server/structured-document.ts +69 -69
- package/server/text-search.ts +15 -15
- package/server/worker-entry.ts +42 -42
- package/server/worker-protocol.ts +38 -38
- package/server/worksheet.ts +67 -67
- package/server/workspace-file.ts +63 -63
- package/shared/contracts.ts +187 -187
- package/shared/file-limits.ts +5 -5
- package/shared/markdown-constants.ts +4 -4
- package/shared/markdown-math.ts +198 -198
- package/shared/markdown-references.ts +83 -83
- package/shared/markdown-source-location.ts +8 -8
- package/shared/reader-settings.ts +15 -15
- package/shared/reader-shortcuts.ts +132 -132
- package/vendor/office2pdf/office2pdf_bg.wasm +2 -2
package/client/settings.tsx
CHANGED
|
@@ -1,143 +1,143 @@
|
|
|
1
|
-
import { Fragment, useState } from "react";
|
|
2
|
-
import { useSettings, useTranslation } from "@getpaseo/plugin/client";
|
|
3
|
-
import { SettingsAction, SettingsCard, SettingsInput, SettingsSection, SettingsSelect } from "@getpaseo/plugin/client/ui";
|
|
4
|
-
import { readerSettings } from "../shared/reader-settings";
|
|
5
|
-
import { allowedShortcut, conflictingShortcut, defaultModifiers, keyboardShortcuts, modifierChoices, normalizeShortcut,
|
|
6
|
-
type KeyboardShortcut, type ModifiedShortcut, type ReaderShortcuts } from "../shared/reader-shortcuts";
|
|
7
|
-
import { messages } from "./messages";
|
|
8
|
-
|
|
9
|
-
const findActions = ["find", "closeFind", "nextMatch", "previousMatch"] as const;
|
|
10
|
-
const zoomActions = ["zoomIn", "zoomOut", "resetZoom", "wheelZoom"] as const;
|
|
11
|
-
const navigationActions = ["forcePan", "panUp", "panDown", "panLeft", "panRight", "scrollPageUp", "scrollPageDown", "closeCellMenu"] as const;
|
|
12
|
-
const presets: Record<KeyboardShortcut, readonly string[]> = {
|
|
13
|
-
find: ["Ctrl/Cmd+Shift+F", "F3"],
|
|
14
|
-
closeFind: ["Ctrl/Cmd+Escape"],
|
|
15
|
-
nextMatch: ["F3", "Ctrl/Cmd+ArrowDown"],
|
|
16
|
-
previousMatch: ["Shift+F3", "Ctrl/Cmd+ArrowUp"],
|
|
17
|
-
zoomIn: ["Alt+ArrowUp", "Ctrl/Cmd+ArrowUp"],
|
|
18
|
-
zoomOut: ["Alt+ArrowDown", "Ctrl/Cmd+ArrowDown"],
|
|
19
|
-
resetZoom: ["Alt+0", "Home"],
|
|
20
|
-
panUp: ["W", "K"], panDown: ["S", "J"], panLeft: ["A", "H"], panRight: ["D", "L"],
|
|
21
|
-
scrollPageUp: ["Shift+ArrowUp", "Ctrl/Cmd+ArrowUp"],
|
|
22
|
-
scrollPageDown: ["Shift+ArrowDown", "Ctrl/Cmd+ArrowDown"],
|
|
23
|
-
closeCellMenu: ["Shift+Escape", "Backspace"],
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
function shortcutLabel(value: string) {
|
|
27
|
-
return value.replace(/Ctrl\/Cmd/g, "Ctrl / ⌘").replace(/Cmd/g, "⌘").replace(/Alt/g, "Alt / Option")
|
|
28
|
-
.replace(/Plus/g, "+").replace(/Arrow(Up|Down|Left|Right)/g, (_, direction: "Up" | "Down" | "Left" | "Right") =>
|
|
29
|
-
({ Up: "↑", Down: "↓", Left: "←", Right: "→" })[direction]);
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
function isModifier(action: KeyboardShortcut | ModifiedShortcut): action is ModifiedShortcut {
|
|
33
|
-
return action === "forcePan" || action === "wheelZoom";
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
export function ReaderSettings() {
|
|
37
|
-
const settings = useSettings(readerSettings);
|
|
38
|
-
const { t } = useTranslation(messages);
|
|
39
|
-
const [custom, setCustom] = useState<{ action: KeyboardShortcut; value: string }>();
|
|
40
|
-
const [problem, setProblem] = useState<{ action: KeyboardShortcut | ModifiedShortcut; text: string }>();
|
|
41
|
-
const ready = settings.status === "ready";
|
|
42
|
-
const error = settings.saveError || settings.status === "invalid" || settings.status === "error";
|
|
43
|
-
const shortcuts = ready ? settings.values.shortcuts : {};
|
|
44
|
-
const disabled = !ready || settings.saving;
|
|
45
|
-
|
|
46
|
-
async function recover() {
|
|
47
|
-
if (settings.status === "invalid") await settings.reset();
|
|
48
|
-
else await settings.reload();
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
async function saveShortcut(action: KeyboardShortcut | ModifiedShortcut, value: string) {
|
|
52
|
-
if (settings.status !== "ready" || settings.saving) return;
|
|
53
|
-
const next: ReaderShortcuts = { ...settings.values.shortcuts };
|
|
54
|
-
if (value === "default") delete next[action];
|
|
55
|
-
else next[action] = value;
|
|
56
|
-
const conflict = conflictingShortcut(action, next);
|
|
57
|
-
if (conflict) {
|
|
58
|
-
setProblem({ action, text: t("shortcutConflict", { action: t(conflict) }) });
|
|
59
|
-
return;
|
|
60
|
-
}
|
|
61
|
-
setProblem(undefined);
|
|
62
|
-
if (await settings.save({ ...settings.values, shortcuts: next }, settings.revision)) setCustom(undefined);
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
function shortcutRow(action: KeyboardShortcut | ModifiedShortcut) {
|
|
66
|
-
const modifier = isModifier(action);
|
|
67
|
-
const defaults: readonly string[] = modifier ? [defaultModifiers[action]] : keyboardShortcuts[action];
|
|
68
|
-
const choices: readonly string[] = !modifier ? presets[action]
|
|
69
|
-
: action === "forcePan" ? ["Space", ...modifierChoices] : modifierChoices;
|
|
70
|
-
const selected = shortcuts[action] ?? "default";
|
|
71
|
-
const values = new Set(choices.filter((key) => !defaults.includes(key)));
|
|
72
|
-
if (selected !== "default" && selected !== "none") values.add(selected);
|
|
73
|
-
const options = [
|
|
74
|
-
{ value: "default", label: t("shortcutDefault", { shortcut: defaults.map(shortcutLabel).join(", ") }) },
|
|
75
|
-
...Array.from(values, (value) => ({ value, label: shortcutLabel(value) })),
|
|
76
|
-
{ value: "none", label: t("shortcutNone") },
|
|
77
|
-
...(!modifier ? [{ value: "custom", label: t("shortcutCustom") }] : []),
|
|
78
|
-
];
|
|
79
|
-
return (
|
|
80
|
-
<Fragment key={action}>
|
|
81
|
-
<SettingsSelect label={t(action)} testID={`merong-shortcut-${action}`}
|
|
82
|
-
hint={action === "forcePan" ? t("forcePanHint") : action === "wheelZoom" ? t("wheelZoomHint") : undefined}
|
|
83
|
-
error={problem?.action === action ? problem.text : undefined}
|
|
84
|
-
value={custom?.action === action ? "custom" : selected} options={options} disabled={disabled}
|
|
85
|
-
onValueChange={(value) => {
|
|
86
|
-
if (value === "custom" && !modifier) {
|
|
87
|
-
setProblem(undefined);
|
|
88
|
-
setCustom({ action, value: selected === "default" || selected === "none" ? "" : selected });
|
|
89
|
-
} else void saveShortcut(action, value);
|
|
90
|
-
}} />
|
|
91
|
-
{custom?.action === action && (
|
|
92
|
-
<>
|
|
93
|
-
<SettingsInput label={t("editShortcut", { action: t(action) })} initialValue={custom.value}
|
|
94
|
-
hint={t("shortcutExample")} placeholder="Ctrl/Cmd+Shift+K" disabled={disabled}
|
|
95
|
-
onChangeText={(value) => {
|
|
96
|
-
setCustom({ ...custom, value });
|
|
97
|
-
setProblem(undefined);
|
|
98
|
-
}} />
|
|
99
|
-
<SettingsAction label={t("shortcutCustom")} actionLabel={t("saveShortcut")} disabled={disabled} onPress={() => {
|
|
100
|
-
const key = normalizeShortcut(custom.value);
|
|
101
|
-
if (!key || !allowedShortcut(custom.action, key)) {
|
|
102
|
-
setProblem({ action: custom.action, text: t("invalidShortcut") });
|
|
103
|
-
} else void saveShortcut(custom.action, key);
|
|
104
|
-
}} />
|
|
105
|
-
<SettingsAction label={t("shortcutCustom")} actionLabel={t("cancelShortcut")} onPress={() => {
|
|
106
|
-
setCustom(undefined);
|
|
107
|
-
setProblem(undefined);
|
|
108
|
-
}} />
|
|
109
|
-
</>
|
|
110
|
-
)}
|
|
111
|
-
</Fragment>
|
|
112
|
-
);
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
return (
|
|
116
|
-
<>
|
|
117
|
-
{error && (
|
|
118
|
-
<SettingsSection title={t("readerSettings")}>
|
|
119
|
-
<SettingsCard>
|
|
120
|
-
<SettingsAction label={t("readerSettingsError")}
|
|
121
|
-
actionLabel={t(settings.status === "invalid" ? "resetReaderSettings" : "reloadReaderSettings")}
|
|
122
|
-
disabled={disabled} onPress={() => { void recover(); }} />
|
|
123
|
-
</SettingsCard>
|
|
124
|
-
</SettingsSection>
|
|
125
|
-
)}
|
|
126
|
-
<SettingsSection title={t("keyboardSettings")} info={t("keyboardSettingsHint")}>
|
|
127
|
-
<SettingsCard>
|
|
128
|
-
<SettingsAction label={t("keyboardSettings")} hint={t("keyboardSettingsHint")}
|
|
129
|
-
actionLabel={t("resetShortcuts")} disabled={disabled || !Object.keys(shortcuts).length}
|
|
130
|
-
onPress={() => {
|
|
131
|
-
if (settings.status !== "ready" || settings.saving) return;
|
|
132
|
-
setCustom(undefined);
|
|
133
|
-
setProblem(undefined);
|
|
134
|
-
void settings.save({ ...settings.values, shortcuts: {} }, settings.revision);
|
|
135
|
-
}} />
|
|
136
|
-
</SettingsCard>
|
|
137
|
-
<SettingsCard>{findActions.map(shortcutRow)}</SettingsCard>
|
|
138
|
-
<SettingsCard>{zoomActions.map(shortcutRow)}</SettingsCard>
|
|
139
|
-
<SettingsCard>{navigationActions.map(shortcutRow)}</SettingsCard>
|
|
140
|
-
</SettingsSection>
|
|
141
|
-
</>
|
|
142
|
-
);
|
|
143
|
-
}
|
|
1
|
+
import { Fragment, useState } from "react";
|
|
2
|
+
import { useSettings, useTranslation } from "@getpaseo/plugin/client";
|
|
3
|
+
import { SettingsAction, SettingsCard, SettingsInput, SettingsSection, SettingsSelect } from "@getpaseo/plugin/client/ui";
|
|
4
|
+
import { readerSettings } from "../shared/reader-settings";
|
|
5
|
+
import { allowedShortcut, conflictingShortcut, defaultModifiers, keyboardShortcuts, modifierChoices, normalizeShortcut,
|
|
6
|
+
type KeyboardShortcut, type ModifiedShortcut, type ReaderShortcuts } from "../shared/reader-shortcuts";
|
|
7
|
+
import { messages } from "./messages";
|
|
8
|
+
|
|
9
|
+
const findActions = ["find", "closeFind", "nextMatch", "previousMatch"] as const;
|
|
10
|
+
const zoomActions = ["zoomIn", "zoomOut", "resetZoom", "wheelZoom"] as const;
|
|
11
|
+
const navigationActions = ["forcePan", "panUp", "panDown", "panLeft", "panRight", "scrollPageUp", "scrollPageDown", "closeCellMenu"] as const;
|
|
12
|
+
const presets: Record<KeyboardShortcut, readonly string[]> = {
|
|
13
|
+
find: ["Ctrl/Cmd+Shift+F", "F3"],
|
|
14
|
+
closeFind: ["Ctrl/Cmd+Escape"],
|
|
15
|
+
nextMatch: ["F3", "Ctrl/Cmd+ArrowDown"],
|
|
16
|
+
previousMatch: ["Shift+F3", "Ctrl/Cmd+ArrowUp"],
|
|
17
|
+
zoomIn: ["Alt+ArrowUp", "Ctrl/Cmd+ArrowUp"],
|
|
18
|
+
zoomOut: ["Alt+ArrowDown", "Ctrl/Cmd+ArrowDown"],
|
|
19
|
+
resetZoom: ["Alt+0", "Home"],
|
|
20
|
+
panUp: ["W", "K"], panDown: ["S", "J"], panLeft: ["A", "H"], panRight: ["D", "L"],
|
|
21
|
+
scrollPageUp: ["Shift+ArrowUp", "Ctrl/Cmd+ArrowUp"],
|
|
22
|
+
scrollPageDown: ["Shift+ArrowDown", "Ctrl/Cmd+ArrowDown"],
|
|
23
|
+
closeCellMenu: ["Shift+Escape", "Backspace"],
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
function shortcutLabel(value: string) {
|
|
27
|
+
return value.replace(/Ctrl\/Cmd/g, "Ctrl / ⌘").replace(/Cmd/g, "⌘").replace(/Alt/g, "Alt / Option")
|
|
28
|
+
.replace(/Plus/g, "+").replace(/Arrow(Up|Down|Left|Right)/g, (_, direction: "Up" | "Down" | "Left" | "Right") =>
|
|
29
|
+
({ Up: "↑", Down: "↓", Left: "←", Right: "→" })[direction]);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function isModifier(action: KeyboardShortcut | ModifiedShortcut): action is ModifiedShortcut {
|
|
33
|
+
return action === "forcePan" || action === "wheelZoom";
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function ReaderSettings() {
|
|
37
|
+
const settings = useSettings(readerSettings);
|
|
38
|
+
const { t } = useTranslation(messages);
|
|
39
|
+
const [custom, setCustom] = useState<{ action: KeyboardShortcut; value: string }>();
|
|
40
|
+
const [problem, setProblem] = useState<{ action: KeyboardShortcut | ModifiedShortcut; text: string }>();
|
|
41
|
+
const ready = settings.status === "ready";
|
|
42
|
+
const error = settings.saveError || settings.status === "invalid" || settings.status === "error";
|
|
43
|
+
const shortcuts = ready ? settings.values.shortcuts : {};
|
|
44
|
+
const disabled = !ready || settings.saving;
|
|
45
|
+
|
|
46
|
+
async function recover() {
|
|
47
|
+
if (settings.status === "invalid") await settings.reset();
|
|
48
|
+
else await settings.reload();
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
async function saveShortcut(action: KeyboardShortcut | ModifiedShortcut, value: string) {
|
|
52
|
+
if (settings.status !== "ready" || settings.saving) return;
|
|
53
|
+
const next: ReaderShortcuts = { ...settings.values.shortcuts };
|
|
54
|
+
if (value === "default") delete next[action];
|
|
55
|
+
else next[action] = value;
|
|
56
|
+
const conflict = conflictingShortcut(action, next);
|
|
57
|
+
if (conflict) {
|
|
58
|
+
setProblem({ action, text: t("shortcutConflict", { action: t(conflict) }) });
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
setProblem(undefined);
|
|
62
|
+
if (await settings.save({ ...settings.values, shortcuts: next }, settings.revision)) setCustom(undefined);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function shortcutRow(action: KeyboardShortcut | ModifiedShortcut) {
|
|
66
|
+
const modifier = isModifier(action);
|
|
67
|
+
const defaults: readonly string[] = modifier ? [defaultModifiers[action]] : keyboardShortcuts[action];
|
|
68
|
+
const choices: readonly string[] = !modifier ? presets[action]
|
|
69
|
+
: action === "forcePan" ? ["Space", ...modifierChoices] : modifierChoices;
|
|
70
|
+
const selected = shortcuts[action] ?? "default";
|
|
71
|
+
const values = new Set(choices.filter((key) => !defaults.includes(key)));
|
|
72
|
+
if (selected !== "default" && selected !== "none") values.add(selected);
|
|
73
|
+
const options = [
|
|
74
|
+
{ value: "default", label: t("shortcutDefault", { shortcut: defaults.map(shortcutLabel).join(", ") }) },
|
|
75
|
+
...Array.from(values, (value) => ({ value, label: shortcutLabel(value) })),
|
|
76
|
+
{ value: "none", label: t("shortcutNone") },
|
|
77
|
+
...(!modifier ? [{ value: "custom", label: t("shortcutCustom") }] : []),
|
|
78
|
+
];
|
|
79
|
+
return (
|
|
80
|
+
<Fragment key={action}>
|
|
81
|
+
<SettingsSelect label={t(action)} testID={`merong-shortcut-${action}`}
|
|
82
|
+
hint={action === "forcePan" ? t("forcePanHint") : action === "wheelZoom" ? t("wheelZoomHint") : undefined}
|
|
83
|
+
error={problem?.action === action ? problem.text : undefined}
|
|
84
|
+
value={custom?.action === action ? "custom" : selected} options={options} disabled={disabled}
|
|
85
|
+
onValueChange={(value) => {
|
|
86
|
+
if (value === "custom" && !modifier) {
|
|
87
|
+
setProblem(undefined);
|
|
88
|
+
setCustom({ action, value: selected === "default" || selected === "none" ? "" : selected });
|
|
89
|
+
} else void saveShortcut(action, value);
|
|
90
|
+
}} />
|
|
91
|
+
{custom?.action === action && (
|
|
92
|
+
<>
|
|
93
|
+
<SettingsInput label={t("editShortcut", { action: t(action) })} initialValue={custom.value}
|
|
94
|
+
hint={t("shortcutExample")} placeholder="Ctrl/Cmd+Shift+K" disabled={disabled}
|
|
95
|
+
onChangeText={(value) => {
|
|
96
|
+
setCustom({ ...custom, value });
|
|
97
|
+
setProblem(undefined);
|
|
98
|
+
}} />
|
|
99
|
+
<SettingsAction label={t("shortcutCustom")} actionLabel={t("saveShortcut")} disabled={disabled} onPress={() => {
|
|
100
|
+
const key = normalizeShortcut(custom.value);
|
|
101
|
+
if (!key || !allowedShortcut(custom.action, key)) {
|
|
102
|
+
setProblem({ action: custom.action, text: t("invalidShortcut") });
|
|
103
|
+
} else void saveShortcut(custom.action, key);
|
|
104
|
+
}} />
|
|
105
|
+
<SettingsAction label={t("shortcutCustom")} actionLabel={t("cancelShortcut")} onPress={() => {
|
|
106
|
+
setCustom(undefined);
|
|
107
|
+
setProblem(undefined);
|
|
108
|
+
}} />
|
|
109
|
+
</>
|
|
110
|
+
)}
|
|
111
|
+
</Fragment>
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
return (
|
|
116
|
+
<>
|
|
117
|
+
{error && (
|
|
118
|
+
<SettingsSection title={t("readerSettings")}>
|
|
119
|
+
<SettingsCard>
|
|
120
|
+
<SettingsAction label={t("readerSettingsError")}
|
|
121
|
+
actionLabel={t(settings.status === "invalid" ? "resetReaderSettings" : "reloadReaderSettings")}
|
|
122
|
+
disabled={disabled} onPress={() => { void recover(); }} />
|
|
123
|
+
</SettingsCard>
|
|
124
|
+
</SettingsSection>
|
|
125
|
+
)}
|
|
126
|
+
<SettingsSection title={t("keyboardSettings")} info={t("keyboardSettingsHint")}>
|
|
127
|
+
<SettingsCard>
|
|
128
|
+
<SettingsAction label={t("keyboardSettings")} hint={t("keyboardSettingsHint")}
|
|
129
|
+
actionLabel={t("resetShortcuts")} disabled={disabled || !Object.keys(shortcuts).length}
|
|
130
|
+
onPress={() => {
|
|
131
|
+
if (settings.status !== "ready" || settings.saving) return;
|
|
132
|
+
setCustom(undefined);
|
|
133
|
+
setProblem(undefined);
|
|
134
|
+
void settings.save({ ...settings.values, shortcuts: {} }, settings.revision);
|
|
135
|
+
}} />
|
|
136
|
+
</SettingsCard>
|
|
137
|
+
<SettingsCard>{findActions.map(shortcutRow)}</SettingsCard>
|
|
138
|
+
<SettingsCard>{zoomActions.map(shortcutRow)}</SettingsCard>
|
|
139
|
+
<SettingsCard>{navigationActions.map(shortcutRow)}</SettingsCard>
|
|
140
|
+
</SettingsSection>
|
|
141
|
+
</>
|
|
142
|
+
);
|
|
143
|
+
}
|
package/client/syntax.tsx
CHANGED
|
@@ -1,55 +1,55 @@
|
|
|
1
|
-
import { memo, useMemo } from "react";
|
|
2
|
-
import { Text, type TextStyle } from "react-native";
|
|
3
|
-
import { refractor } from "refractor/core";
|
|
4
|
-
import markdown from "refractor/markdown";
|
|
5
|
-
import type { Theme } from "./controls";
|
|
6
|
-
|
|
7
|
-
refractor.register(markdown);
|
|
8
|
-
|
|
9
|
-
type SyntaxNode = ReturnType<typeof refractor.highlight>["children"][number];
|
|
10
|
-
|
|
11
|
-
function markdownTokens(source: string): SyntaxNode[] | undefined {
|
|
12
|
-
if (source.length > 32_768) return;
|
|
13
|
-
try { return refractor.highlight(source, "markdown").children; } catch { return undefined; }
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
function sourceTokenStyle(classes: unknown, colors: Theme["colors"], hostPalette: boolean): TextStyle | undefined {
|
|
17
|
-
if (!Array.isArray(classes)) return;
|
|
18
|
-
const kind = classes[1];
|
|
19
|
-
if (typeof kind !== "string") return;
|
|
20
|
-
if (hostPalette) {
|
|
21
|
-
const sourceColor = kind === "title" ? "heading" : kind === "url" ? "link" : kind === "code" ? "string"
|
|
22
|
-
: kind === "prolog" ? "comment" : kind === "class-name" ? "class" : kind === "attr-value" ? "string" : kind;
|
|
23
|
-
return {
|
|
24
|
-
color: `var(--colors-syntax-${sourceColor}, ${colors.foreground})`,
|
|
25
|
-
...(kind === "italic" ? { fontStyle: "italic" } : kind === "bold" ? { fontWeight: "700" } : {}),
|
|
26
|
-
};
|
|
27
|
-
}
|
|
28
|
-
if (kind === "comment" || kind === "punctuation" || kind === "prolog") return { color: colors.foregroundMuted };
|
|
29
|
-
if (kind === "string" || kind === "inserted" || kind === "attr-value") return { color: colors.statusSuccess };
|
|
30
|
-
if (kind === "number" || kind === "constant" || kind === "boolean") return { color: colors.statusWarning };
|
|
31
|
-
if (kind === "deleted") return { color: colors.statusDanger };
|
|
32
|
-
if (kind === "italic") return { fontStyle: "italic" };
|
|
33
|
-
if (kind === "bold" || kind === "title") return { fontWeight: "700", color: colors.accent };
|
|
34
|
-
if (kind === "keyword" || kind === "tag" || kind === "url" || kind === "function" ||
|
|
35
|
-
kind === "class-name" || kind === "property") return { color: colors.accent };
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
function renderSyntax(nodes: readonly SyntaxNode[], colors: Theme["colors"], hostPalette: boolean): React.ReactNode {
|
|
39
|
-
return nodes.map((node, index) => {
|
|
40
|
-
if (node.type === "text") return node.value;
|
|
41
|
-
if (node.type === "element") {
|
|
42
|
-
return <Text key={index} style={sourceTokenStyle(node.properties.className, colors, hostPalette)}>
|
|
43
|
-
{renderSyntax(node.children, colors, hostPalette)}
|
|
44
|
-
</Text>;
|
|
45
|
-
}
|
|
46
|
-
return null;
|
|
47
|
-
});
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
export const Syntax = memo(function Syntax({ source, theme, hostPalette = false }: {
|
|
51
|
-
source: string; theme: Theme; hostPalette?: boolean;
|
|
52
|
-
}) {
|
|
53
|
-
const tokens = useMemo(() => markdownTokens(source), [source]);
|
|
54
|
-
return tokens ? renderSyntax(tokens, theme.colors, hostPalette) : source;
|
|
55
|
-
});
|
|
1
|
+
import { memo, useMemo } from "react";
|
|
2
|
+
import { Text, type TextStyle } from "react-native";
|
|
3
|
+
import { refractor } from "refractor/core";
|
|
4
|
+
import markdown from "refractor/markdown";
|
|
5
|
+
import type { Theme } from "./controls";
|
|
6
|
+
|
|
7
|
+
refractor.register(markdown);
|
|
8
|
+
|
|
9
|
+
type SyntaxNode = ReturnType<typeof refractor.highlight>["children"][number];
|
|
10
|
+
|
|
11
|
+
function markdownTokens(source: string): SyntaxNode[] | undefined {
|
|
12
|
+
if (source.length > 32_768) return;
|
|
13
|
+
try { return refractor.highlight(source, "markdown").children; } catch { return undefined; }
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function sourceTokenStyle(classes: unknown, colors: Theme["colors"], hostPalette: boolean): TextStyle | undefined {
|
|
17
|
+
if (!Array.isArray(classes)) return;
|
|
18
|
+
const kind = classes[1];
|
|
19
|
+
if (typeof kind !== "string") return;
|
|
20
|
+
if (hostPalette) {
|
|
21
|
+
const sourceColor = kind === "title" ? "heading" : kind === "url" ? "link" : kind === "code" ? "string"
|
|
22
|
+
: kind === "prolog" ? "comment" : kind === "class-name" ? "class" : kind === "attr-value" ? "string" : kind;
|
|
23
|
+
return {
|
|
24
|
+
color: `var(--colors-syntax-${sourceColor}, ${colors.foreground})`,
|
|
25
|
+
...(kind === "italic" ? { fontStyle: "italic" } : kind === "bold" ? { fontWeight: "700" } : {}),
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
if (kind === "comment" || kind === "punctuation" || kind === "prolog") return { color: colors.foregroundMuted };
|
|
29
|
+
if (kind === "string" || kind === "inserted" || kind === "attr-value") return { color: colors.statusSuccess };
|
|
30
|
+
if (kind === "number" || kind === "constant" || kind === "boolean") return { color: colors.statusWarning };
|
|
31
|
+
if (kind === "deleted") return { color: colors.statusDanger };
|
|
32
|
+
if (kind === "italic") return { fontStyle: "italic" };
|
|
33
|
+
if (kind === "bold" || kind === "title") return { fontWeight: "700", color: colors.accent };
|
|
34
|
+
if (kind === "keyword" || kind === "tag" || kind === "url" || kind === "function" ||
|
|
35
|
+
kind === "class-name" || kind === "property") return { color: colors.accent };
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function renderSyntax(nodes: readonly SyntaxNode[], colors: Theme["colors"], hostPalette: boolean): React.ReactNode {
|
|
39
|
+
return nodes.map((node, index) => {
|
|
40
|
+
if (node.type === "text") return node.value;
|
|
41
|
+
if (node.type === "element") {
|
|
42
|
+
return <Text key={index} style={sourceTokenStyle(node.properties.className, colors, hostPalette)}>
|
|
43
|
+
{renderSyntax(node.children, colors, hostPalette)}
|
|
44
|
+
</Text>;
|
|
45
|
+
}
|
|
46
|
+
return null;
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export const Syntax = memo(function Syntax({ source, theme, hostPalette = false }: {
|
|
51
|
+
source: string; theme: Theme; hostPalette?: boolean;
|
|
52
|
+
}) {
|
|
53
|
+
const tokens = useMemo(() => markdownTokens(source), [source]);
|
|
54
|
+
return tokens ? renderSyntax(tokens, theme.colors, hostPalette) : source;
|
|
55
|
+
});
|
|
@@ -1,61 +1,61 @@
|
|
|
1
|
-
export interface Point { x: number; y: number }
|
|
2
|
-
export interface Size { width: number; height: number }
|
|
3
|
-
export interface Position extends Point { scale: number }
|
|
4
|
-
export interface Contact extends Point { id: number }
|
|
5
|
-
|
|
6
|
-
export const minZoomPercent = 25;
|
|
7
|
-
export const maxZoomPercent = 400;
|
|
8
|
-
|
|
9
|
-
export const clamp = (value: number, minimum: number, maximum: number) =>
|
|
10
|
-
Math.max(minimum, Math.min(maximum, value));
|
|
11
|
-
|
|
12
|
-
export function boundZoom(scale: number) {
|
|
13
|
-
return clamp(scale, minZoomPercent / 100, maxZoomPercent / 100);
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export function constrainGrid(position: Position, viewport: Size, content: Size): Position {
|
|
17
|
-
return {
|
|
18
|
-
scale: position.scale,
|
|
19
|
-
x: clamp(position.x, Math.min(0, viewport.width - content.width * position.scale), 0),
|
|
20
|
-
y: clamp(position.y, Math.min(0, viewport.height - content.height * position.scale), 0),
|
|
21
|
-
};
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export function zoomAround(position: Position, scale: number, anchor: Point, target = anchor): Position {
|
|
25
|
-
const ratio = scale / position.scale;
|
|
26
|
-
return {
|
|
27
|
-
scale,
|
|
28
|
-
x: target.x - (anchor.x - position.x) * ratio,
|
|
29
|
-
y: target.y - (anchor.y - position.y) * ratio,
|
|
30
|
-
};
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
export function zoomGridScroll(scroll: Position, scale: number, anchor: Point, viewport: Size, content: Size, frozen: Size) {
|
|
34
|
-
const inset = (zoom: number) => ({
|
|
35
|
-
x: Math.min(frozen.width * zoom, viewport.width / 3),
|
|
36
|
-
y: Math.min(frozen.height * zoom, viewport.height / 3),
|
|
37
|
-
});
|
|
38
|
-
const before = inset(scroll.scale);
|
|
39
|
-
const after = inset(scale);
|
|
40
|
-
const position = zoomAround({ x: -scroll.x, y: -scroll.y, scale: scroll.scale }, scale,
|
|
41
|
-
{ x: Math.max(0, anchor.x - before.x), y: Math.max(0, anchor.y - before.y) },
|
|
42
|
-
{ x: Math.max(0, anchor.x - after.x), y: Math.max(0, anchor.y - after.y) });
|
|
43
|
-
const bounds = constrainGrid(position, { width: viewport.width - after.x, height: viewport.height - after.y }, content);
|
|
44
|
-
return { x: Math.abs(bounds.x), y: Math.abs(bounds.y), scale };
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
function midpoint([first, second = first]: Contact[]): Point {
|
|
48
|
-
return { x: (first.x + second.x) / 2, y: (first.y + second.y) / 2 };
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
function distance([first, second]: Contact[]): number {
|
|
52
|
-
return second ? Math.max(1, Math.hypot(first.x - second.x, first.y - second.y)) : 1;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
export function panAndPinch(position: Position, previous: Contact[], next: Contact[]): Position {
|
|
56
|
-
if (!previous.length || previous.length !== next.length || previous.some((value, index) => value.id !== next[index].id)) {
|
|
57
|
-
return position;
|
|
58
|
-
}
|
|
59
|
-
const scale = boundZoom(position.scale * distance(next) / distance(previous));
|
|
60
|
-
return zoomAround(position, scale, midpoint(previous), midpoint(next));
|
|
61
|
-
}
|
|
1
|
+
export interface Point { x: number; y: number }
|
|
2
|
+
export interface Size { width: number; height: number }
|
|
3
|
+
export interface Position extends Point { scale: number }
|
|
4
|
+
export interface Contact extends Point { id: number }
|
|
5
|
+
|
|
6
|
+
export const minZoomPercent = 25;
|
|
7
|
+
export const maxZoomPercent = 400;
|
|
8
|
+
|
|
9
|
+
export const clamp = (value: number, minimum: number, maximum: number) =>
|
|
10
|
+
Math.max(minimum, Math.min(maximum, value));
|
|
11
|
+
|
|
12
|
+
export function boundZoom(scale: number) {
|
|
13
|
+
return clamp(scale, minZoomPercent / 100, maxZoomPercent / 100);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function constrainGrid(position: Position, viewport: Size, content: Size): Position {
|
|
17
|
+
return {
|
|
18
|
+
scale: position.scale,
|
|
19
|
+
x: clamp(position.x, Math.min(0, viewport.width - content.width * position.scale), 0),
|
|
20
|
+
y: clamp(position.y, Math.min(0, viewport.height - content.height * position.scale), 0),
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function zoomAround(position: Position, scale: number, anchor: Point, target = anchor): Position {
|
|
25
|
+
const ratio = scale / position.scale;
|
|
26
|
+
return {
|
|
27
|
+
scale,
|
|
28
|
+
x: target.x - (anchor.x - position.x) * ratio,
|
|
29
|
+
y: target.y - (anchor.y - position.y) * ratio,
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function zoomGridScroll(scroll: Position, scale: number, anchor: Point, viewport: Size, content: Size, frozen: Size) {
|
|
34
|
+
const inset = (zoom: number) => ({
|
|
35
|
+
x: Math.min(frozen.width * zoom, viewport.width / 3),
|
|
36
|
+
y: Math.min(frozen.height * zoom, viewport.height / 3),
|
|
37
|
+
});
|
|
38
|
+
const before = inset(scroll.scale);
|
|
39
|
+
const after = inset(scale);
|
|
40
|
+
const position = zoomAround({ x: -scroll.x, y: -scroll.y, scale: scroll.scale }, scale,
|
|
41
|
+
{ x: Math.max(0, anchor.x - before.x), y: Math.max(0, anchor.y - before.y) },
|
|
42
|
+
{ x: Math.max(0, anchor.x - after.x), y: Math.max(0, anchor.y - after.y) });
|
|
43
|
+
const bounds = constrainGrid(position, { width: viewport.width - after.x, height: viewport.height - after.y }, content);
|
|
44
|
+
return { x: Math.abs(bounds.x), y: Math.abs(bounds.y), scale };
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function midpoint([first, second = first]: Contact[]): Point {
|
|
48
|
+
return { x: (first.x + second.x) / 2, y: (first.y + second.y) / 2 };
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function distance([first, second]: Contact[]): number {
|
|
52
|
+
return second ? Math.max(1, Math.hypot(first.x - second.x, first.y - second.y)) : 1;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function panAndPinch(position: Position, previous: Contact[], next: Contact[]): Position {
|
|
56
|
+
if (!previous.length || previous.length !== next.length || previous.some((value, index) => value.id !== next[index].id)) {
|
|
57
|
+
return position;
|
|
58
|
+
}
|
|
59
|
+
const scale = boundZoom(position.scale * distance(next) / distance(previous));
|
|
60
|
+
return zoomAround(position, scale, midpoint(previous), midpoint(next));
|
|
61
|
+
}
|