dsh-obsidian-memo 0.16.3
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 +21 -0
- package/README.en.md +270 -0
- package/README.md +272 -0
- package/cordis.patch.yml +10 -0
- package/docs/ARCHITECTURE.md +88 -0
- package/docs/AUDIT-0.16.md +74 -0
- package/docs/CHANGELOG.md +120 -0
- package/docs/CONTRACT.md +544 -0
- package/docs/GUIDE.md +177 -0
- package/docs/KNOWN_ISSUES.md +113 -0
- package/icon.svg +7 -0
- package/lib/ai.js +161 -0
- package/lib/capture.js +175 -0
- package/lib/client.js +6994 -0
- package/lib/config-picker.js +29 -0
- package/lib/filename.js +33 -0
- package/lib/files.js +88 -0
- package/lib/host/ai-jobs.js +118 -0
- package/lib/host/configuration.js +151 -0
- package/lib/host/dispatch.js +32 -0
- package/lib/host/environment.js +24 -0
- package/lib/host/folders.js +77 -0
- package/lib/host/images.js +116 -0
- package/lib/host/mutations.js +123 -0
- package/lib/host/note-content.js +44 -0
- package/lib/host/notes.js +148 -0
- package/lib/host/rpc.js +82 -0
- package/lib/host/scanning.js +67 -0
- package/lib/host/session.js +67 -0
- package/lib/host/state.js +68 -0
- package/lib/host/vault.js +38 -0
- package/lib/host/writes.js +21 -0
- package/lib/index.js +88 -0
- package/lib/notes.js +454 -0
- package/lib/obsidian.js +93 -0
- package/lib/scan.js +269 -0
- package/lib/settings.js +228 -0
- package/lib/shared.js +309 -0
- package/lib/trash.js +205 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +76 -0
- package/scripts/benchmark.mjs +58 -0
- package/scripts/build-client.mjs +39 -0
- package/src/client/ai/use-comments.js +75 -0
- package/src/client/ai/use-job-notifications.js +53 -0
- package/src/client/ai/use-project-dialog.js +58 -0
- package/src/client/capture/composer.js +266 -0
- package/src/client/capture/images.js +64 -0
- package/src/client/capture/keyboard.js +34 -0
- package/src/client/components/dialogs.js +67 -0
- package/src/client/components/feedback.js +50 -0
- package/src/client/components/icons.js +160 -0
- package/src/client/components/use-toasts.js +58 -0
- package/src/client/constants.js +127 -0
- package/src/client/data/state.js +116 -0
- package/src/client/data/use-notes-data.js +137 -0
- package/src/client/data/visibility.js +14 -0
- package/src/client/drag/drop-actions.js +47 -0
- package/src/client/drag/motion.js +84 -0
- package/src/client/drag/overlay.js +33 -0
- package/src/client/drag/use-card-drag.js +149 -0
- package/src/client/i18n/en.js +219 -0
- package/src/client/i18n/index.js +11 -0
- package/src/client/i18n/zh.js +219 -0
- package/src/client/images/cache.js +55 -0
- package/src/client/images/components.js +121 -0
- package/src/client/images/queue.js +106 -0
- package/src/client/index.js +40 -0
- package/src/client/panel/header.js +146 -0
- package/src/client/panel/use-shortcuts.js +79 -0
- package/src/client/panel.js +424 -0
- package/src/client/preferences.js +198 -0
- package/src/client/react.js +8 -0
- package/src/client/reader/blocks.js +51 -0
- package/src/client/reader/note-drawer.js +270 -0
- package/src/client/reader/use-dock-offset.js +23 -0
- package/src/client/reader/use-drawer-layout.js +96 -0
- package/src/client/reader/use-note-editor.js +230 -0
- package/src/client/reader/use-pinned-note.js +41 -0
- package/src/client/runtime.js +30 -0
- package/src/client/settings/ai-budget-row.js +65 -0
- package/src/client/settings/ai-budget.js +42 -0
- package/src/client/settings/callouts.js +21 -0
- package/src/client/settings/config-file.js +74 -0
- package/src/client/settings/image-processing.js +31 -0
- package/src/client/settings/official-page.js +32 -0
- package/src/client/settings/paths.js +131 -0
- package/src/client/settings/settings-drawer.js +244 -0
- package/src/client/settings/style-select.js +58 -0
- package/src/client/settings/trash.js +30 -0
- package/src/client/settings/use-settings-draft.js +207 -0
- package/src/client/styles/base.js +63 -0
- package/src/client/styles/capture.js +94 -0
- package/src/client/styles/index.js +26 -0
- package/src/client/styles/overrides.js +116 -0
- package/src/client/styles/reader.js +66 -0
- package/src/client/styles/settings.js +118 -0
- package/src/client/styles/tokens.js +29 -0
- package/src/client/styles/wall.js +83 -0
- package/src/client/testing.js +139 -0
- package/src/client/utils.js +66 -0
- package/src/client/wall/model.js +158 -0
- package/src/client/wall/note-card.js +201 -0
- package/src/client/wall/use-note-actions.js +107 -0
- package/src/client/wall/use-subfolders.js +42 -0
- package/src/client/wall/use-wall-content.js +135 -0
- package/src/client/wall/use-wall-filters.js +92 -0
- package/src/client/wall/use-wall-paging.js +55 -0
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
/** 持有抽屉读取、草稿、保存与任务写入;拒绝迟到回调覆盖新会话。 */
|
|
2
|
+
import { clearStoredPinnedNote } from '../preferences.js';
|
|
3
|
+
import { React } from '../react.js';
|
|
4
|
+
import { callRpc } from '../runtime.js';
|
|
5
|
+
import { isAbortError } from '../utils.js';
|
|
6
|
+
|
|
7
|
+
function useNoteEditor(options) {
|
|
8
|
+
const { tRef, aliveRef, controllersRef, request, refreshNow, replaceNote, replaceRenamedNote,
|
|
9
|
+
pushToast, pushErrorToast, onOpen, onClose } = options;
|
|
10
|
+
const [noteDrawer, setNoteDrawer] = React.useState(null); // { path, summary, full, loading, error }
|
|
11
|
+
const noteDrawerRef = React.useRef(null);
|
|
12
|
+
const drawerAbortRef = React.useRef(null);
|
|
13
|
+
const [editing, setEditing] = React.useState(false);
|
|
14
|
+
const editingRef = React.useRef(false);
|
|
15
|
+
const [bodyDraft, setBodyDraft] = React.useState('');
|
|
16
|
+
const bodyDraftRef = React.useRef('');
|
|
17
|
+
const [titleDraft, setTitleDraft] = React.useState('');
|
|
18
|
+
const [savingNote, setSavingNote] = React.useState(false);
|
|
19
|
+
const savingNoteRef = React.useRef(false);
|
|
20
|
+
const [taskBusy, setTaskBusy] = React.useState(false);
|
|
21
|
+
const taskBusyRef = React.useRef(false);
|
|
22
|
+
const editorSessionRef = React.useRef(0);
|
|
23
|
+
function updateDrawer(value) {
|
|
24
|
+
const next = typeof value === 'function' ? value(noteDrawerRef.current) : value;
|
|
25
|
+
noteDrawerRef.current = next;
|
|
26
|
+
setNoteDrawer(next);
|
|
27
|
+
}
|
|
28
|
+
React.useEffect(() => () => {
|
|
29
|
+
if (drawerAbortRef.current) drawerAbortRef.current.abort();
|
|
30
|
+
}, []);
|
|
31
|
+
function closeNoteDrawer() {
|
|
32
|
+
// noteDrawerRef 可能为空(首次打开前/关闭后),判空后再读 summary(0.13 修复)。
|
|
33
|
+
editorSessionRef.current += 1;
|
|
34
|
+
const closing = noteDrawerRef.current;
|
|
35
|
+
if (editingRef.current && closing) {
|
|
36
|
+
const full = closing.full;
|
|
37
|
+
const original = full && typeof full.body === 'string' ? full.body : '';
|
|
38
|
+
const summaryName = closing.summary && typeof closing.summary.name === 'string' ? closing.summary.name : '';
|
|
39
|
+
const originalName = (full && (full.name || full.title)) || summaryName || '';
|
|
40
|
+
if (bodyDraftRef.current !== original || titleDraft.trim() !== originalName) {
|
|
41
|
+
pushToast({ text: tRef.current('drawer.editDiscarded'), kind: 'info' });
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
if (drawerAbortRef.current) {
|
|
45
|
+
drawerAbortRef.current.abort();
|
|
46
|
+
drawerAbortRef.current = null;
|
|
47
|
+
}
|
|
48
|
+
onClose();
|
|
49
|
+
editingRef.current = false;
|
|
50
|
+
bodyDraftRef.current = '';
|
|
51
|
+
savingNoteRef.current = false;
|
|
52
|
+
setEditing(false);
|
|
53
|
+
setBodyDraft('');
|
|
54
|
+
setSavingNote(false);
|
|
55
|
+
// 0.13.4:主动关闭后不再记住这篇(锁定态重进时改为选当前筛选第一篇)。
|
|
56
|
+
clearStoredPinnedNote();
|
|
57
|
+
updateDrawer(null);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
async function loadNote(path) {
|
|
61
|
+
if (typeof path !== 'string' || path === '') return;
|
|
62
|
+
if (!noteDrawerRef.current || noteDrawerRef.current.path !== path) return;
|
|
63
|
+
updateDrawer((current) => (current && current.path === path
|
|
64
|
+
? Object.assign({}, current, { loading: true, error: null })
|
|
65
|
+
: current));
|
|
66
|
+
if (drawerAbortRef.current) drawerAbortRef.current.abort();
|
|
67
|
+
const controller = new AbortController();
|
|
68
|
+
drawerAbortRef.current = controller;
|
|
69
|
+
controllersRef.current.add(controller);
|
|
70
|
+
try {
|
|
71
|
+
const full = await callRpc('note', { path: path }, controller.signal);
|
|
72
|
+
if (!aliveRef.current || controller.signal.aborted || drawerAbortRef.current !== controller || editingRef.current) return;
|
|
73
|
+
updateDrawer((current) => {
|
|
74
|
+
if (!current || current.path !== path) return current;
|
|
75
|
+
return Object.assign({}, current, {
|
|
76
|
+
full: full && typeof full === 'object' ? full : null,
|
|
77
|
+
summary: full && typeof full === 'object' ? full : current.summary,
|
|
78
|
+
loading: false,
|
|
79
|
+
error: null,
|
|
80
|
+
});
|
|
81
|
+
});
|
|
82
|
+
} catch (error) {
|
|
83
|
+
if (isAbortError(error)) return;
|
|
84
|
+
if (!aliveRef.current || controller.signal.aborted || drawerAbortRef.current !== controller) return;
|
|
85
|
+
updateDrawer((current) => {
|
|
86
|
+
if (!current || current.path !== path) return current;
|
|
87
|
+
return Object.assign({}, current, {
|
|
88
|
+
loading: false,
|
|
89
|
+
error: error && error.message ? error.message : String(error),
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
} finally {
|
|
93
|
+
controllersRef.current.delete(controller);
|
|
94
|
+
if (drawerAbortRef.current === controller) drawerAbortRef.current = null;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function openNoteDrawer(note) {
|
|
99
|
+
if (!note || typeof note.path !== 'string') return;
|
|
100
|
+
editorSessionRef.current += 1;
|
|
101
|
+
onOpen();
|
|
102
|
+
editingRef.current = false;
|
|
103
|
+
bodyDraftRef.current = '';
|
|
104
|
+
savingNoteRef.current = false;
|
|
105
|
+
setEditing(false);
|
|
106
|
+
setBodyDraft('');
|
|
107
|
+
setSavingNote(false);
|
|
108
|
+
updateDrawer({ path: note.path, summary: note, full: null, loading: true, error: null });
|
|
109
|
+
loadNote(note.path);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function handleBodyChange(value) {
|
|
113
|
+
bodyDraftRef.current = value;
|
|
114
|
+
setBodyDraft(value);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function startEditing() {
|
|
118
|
+
if (taskBusyRef.current || savingNoteRef.current) return;
|
|
119
|
+
const current = noteDrawerRef.current;
|
|
120
|
+
if (!current || !current.full) return;
|
|
121
|
+
if (typeof current.full.body !== 'string') return;
|
|
122
|
+
if (drawerAbortRef.current) drawerAbortRef.current.abort();
|
|
123
|
+
updateDrawer(Object.assign({}, current, { loading: false }));
|
|
124
|
+
bodyDraftRef.current = current.full.body;
|
|
125
|
+
setBodyDraft(current.full.body);
|
|
126
|
+
setTitleDraft(current.full.name || current.full.title || '');
|
|
127
|
+
editingRef.current = true;
|
|
128
|
+
setEditing(true);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
async function toggleTask(sourceLine, checked) {
|
|
132
|
+
const current = noteDrawerRef.current;
|
|
133
|
+
if (!current || !current.full || editingRef.current || taskBusyRef.current) return;
|
|
134
|
+
taskBusyRef.current = true;
|
|
135
|
+
setTaskBusy(true);
|
|
136
|
+
try {
|
|
137
|
+
const result = await request('toggleTask', { path: current.path, sourceLine, checked, expectedBody: current.full.body });
|
|
138
|
+
if (result.note) replaceNote(result.note);
|
|
139
|
+
await loadNote(current.path);
|
|
140
|
+
await refreshNow(false);
|
|
141
|
+
} catch (error) {
|
|
142
|
+
if (!isAbortError(error)) pushErrorToast(error);
|
|
143
|
+
await loadNote(current.path);
|
|
144
|
+
} finally {
|
|
145
|
+
taskBusyRef.current = false;
|
|
146
|
+
if (aliveRef.current) setTaskBusy(false);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function cancelEditing() {
|
|
151
|
+
if (savingNoteRef.current) return;
|
|
152
|
+
const current = noteDrawerRef.current;
|
|
153
|
+
const full = current && current.full;
|
|
154
|
+
const original = full && typeof full.body === 'string' ? full.body : '';
|
|
155
|
+
const summaryName = current && current.summary && typeof current.summary.name === 'string' ? current.summary.name : '';
|
|
156
|
+
const originalName = (full && (full.name || full.title)) || summaryName || '';
|
|
157
|
+
if (bodyDraftRef.current !== original || titleDraft.trim() !== originalName) {
|
|
158
|
+
pushToast({ text: tRef.current('drawer.editDiscarded'), kind: 'info' });
|
|
159
|
+
}
|
|
160
|
+
editingRef.current = false;
|
|
161
|
+
bodyDraftRef.current = '';
|
|
162
|
+
setEditing(false);
|
|
163
|
+
setBodyDraft('');
|
|
164
|
+
if (current) loadNote(current.path);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
function saveEditing() {
|
|
168
|
+
const current = noteDrawerRef.current;
|
|
169
|
+
if (!current || savingNoteRef.current) return;
|
|
170
|
+
const path = current.path;
|
|
171
|
+
const session = editorSessionRef.current;
|
|
172
|
+
const body = bodyDraftRef.current;
|
|
173
|
+
const original = current.full && typeof current.full.body === 'string' ? current.full.body : '';
|
|
174
|
+
const summaryName = current.summary && typeof current.summary.name === 'string' ? current.summary.name : '';
|
|
175
|
+
const originalName = (current.full && (current.full.name || current.full.title)) || summaryName || '';
|
|
176
|
+
const name = titleDraft.trim();
|
|
177
|
+
if (!name) {
|
|
178
|
+
pushErrorToast(new Error('便签文件名不能为空'));
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
if (body === original && name === originalName) {
|
|
182
|
+
editingRef.current = false;
|
|
183
|
+
bodyDraftRef.current = '';
|
|
184
|
+
setEditing(false);
|
|
185
|
+
setBodyDraft('');
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
savingNoteRef.current = true;
|
|
189
|
+
setSavingNote(true);
|
|
190
|
+
request('saveNote', { path: path, body: body, name: name, expectedRevision: current.full && current.full.contentRevision })
|
|
191
|
+
.then((value) => {
|
|
192
|
+
if (!aliveRef.current) return;
|
|
193
|
+
const savedNote = value && value.note;
|
|
194
|
+
if (savedNote) {
|
|
195
|
+
if (typeof savedNote.path === 'string' && savedNote.path !== path) replaceRenamedNote(path, savedNote);
|
|
196
|
+
else replaceNote(savedNote);
|
|
197
|
+
}
|
|
198
|
+
// 用户在保存途中切换/关闭了抽屉:不要动新抽屉的编辑态。
|
|
199
|
+
const stillOpen = Boolean(editorSessionRef.current === session && noteDrawerRef.current && noteDrawerRef.current.path === path);
|
|
200
|
+
if (stillOpen) {
|
|
201
|
+
if (savedNote && savedNote.path !== path) {
|
|
202
|
+
noteDrawerRef.current = { path: savedNote.path, summary: savedNote, full: null, loading: true, error: null };
|
|
203
|
+
updateDrawer(noteDrawerRef.current);
|
|
204
|
+
}
|
|
205
|
+
editingRef.current = false;
|
|
206
|
+
bodyDraftRef.current = '';
|
|
207
|
+
setEditing(false);
|
|
208
|
+
setBodyDraft('');
|
|
209
|
+
setTitleDraft(savedNote ? savedNote.name : name);
|
|
210
|
+
loadNote(savedNote ? savedNote.path : path);
|
|
211
|
+
pushToast({ text: tRef.current('drawer.saved'), kind: 'success' });
|
|
212
|
+
}
|
|
213
|
+
return refreshNow(false);
|
|
214
|
+
})
|
|
215
|
+
.catch((error) => {
|
|
216
|
+
if (aliveRef.current && !isAbortError(error)) pushErrorToast(error);
|
|
217
|
+
})
|
|
218
|
+
.then(() => {
|
|
219
|
+
if (editorSessionRef.current !== session) return;
|
|
220
|
+
savingNoteRef.current = false;
|
|
221
|
+
if (aliveRef.current) setSavingNote(false);
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
return { noteDrawer, noteDrawerRef, editing, editingRef, bodyDraft, titleDraft, setTitleDraft,
|
|
226
|
+
savingNote, savingNoteRef, taskBusy, closeNoteDrawer, loadNote, openNoteDrawer,
|
|
227
|
+
handleBodyChange, startEditing, toggleTask, cancelEditing, saveEditing };
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
export { useNoteEditor };
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/** 记忆并恢复钉住的便签,编辑和保存期间保留当前草稿。 */
|
|
2
|
+
import { readStoredPinnedNote, storePinnedNote } from '../preferences.js';
|
|
3
|
+
import { React } from '../react.js';
|
|
4
|
+
|
|
5
|
+
function usePinnedNote({ closeNoteDrawer, editing, editingRef, noteDrawer, noteDrawerRef, openNoteDrawer, pinned, pinnedUserClosedRef, savingNoteRef, sortedNotes, view }) {
|
|
6
|
+
React.useEffect(() => {
|
|
7
|
+
if (!pinned) return;
|
|
8
|
+
if (noteDrawer && typeof noteDrawer.path === 'string' && noteDrawer.path !== '') storePinnedNote(noteDrawer.path);
|
|
9
|
+
}, [pinned, noteDrawer && noteDrawer.path]);
|
|
10
|
+
|
|
11
|
+
React.useEffect(() => {
|
|
12
|
+
if (!pinned || !view.loaded || pinnedUserClosedRef.current) return;
|
|
13
|
+
if (noteDrawerRef.current) return;
|
|
14
|
+
if (sortedNotes.length === 0) return;
|
|
15
|
+
const stored = readStoredPinnedNote();
|
|
16
|
+
// 「当前筛选里最前的一篇」按当前排序结果算(默认创建时间新→旧)。
|
|
17
|
+
const target = (stored !== '' && sortedNotes.find((note) => note.path === stored)) || sortedNotes[0];
|
|
18
|
+
if (target) openNoteDrawer(target);
|
|
19
|
+
}, [pinned, view.loaded, sortedNotes]);
|
|
20
|
+
|
|
21
|
+
React.useEffect(() => {
|
|
22
|
+
if (!view.loaded) return;
|
|
23
|
+
// 编辑中不切走/不关闭:日期筛选变化等触发的自动切换会重置草稿;等保存/取消后再跟随。
|
|
24
|
+
if (editingRef.current) return;
|
|
25
|
+
// 保存请求还在飞(尤其改名):等乐观更新/refresh 落地后再判断,避免误关抽屉。
|
|
26
|
+
if (savingNoteRef.current) return;
|
|
27
|
+
const current = noteDrawerRef.current;
|
|
28
|
+
if (!current) return;
|
|
29
|
+
if (sortedNotes.some((note) => note.path === current.path)) return;
|
|
30
|
+
if (pinned) {
|
|
31
|
+
const next = sortedNotes[0];
|
|
32
|
+
if (next && next.path !== current.path) openNoteDrawer(next);
|
|
33
|
+
else if (!next) closeNoteDrawer();
|
|
34
|
+
} else {
|
|
35
|
+
closeNoteDrawer();
|
|
36
|
+
}
|
|
37
|
+
}, [sortedNotes, pinned, view.loaded, editing]);
|
|
38
|
+
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export { usePinnedNote };
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/** 持有当前宿主连接与翻译函数,集中处理 RPC 信封和错误。 */
|
|
2
|
+
import { CHANNEL } from './constants.js';
|
|
3
|
+
import { isAbortError, makeError } from './utils.js';
|
|
4
|
+
|
|
5
|
+
let runtime = null;
|
|
6
|
+
|
|
7
|
+
async function callRpc(endpoint, payload, signal) {
|
|
8
|
+
if (!runtime || !runtime.connection || !runtime.connection.rpc || typeof runtime.connection.rpc.call !== 'function') {
|
|
9
|
+
throw makeError('offline', runtime && runtime.t ? runtime.t('error.offline') : 'Connection unavailable');
|
|
10
|
+
}
|
|
11
|
+
let result;
|
|
12
|
+
try {
|
|
13
|
+
result = await runtime.connection.rpc.call(CHANNEL, endpoint, payload, signal);
|
|
14
|
+
} catch (error) {
|
|
15
|
+
if (isAbortError(error)) throw error;
|
|
16
|
+
throw makeError('transport', error && error.message ? error.message : String(error));
|
|
17
|
+
}
|
|
18
|
+
if (result && result.ok === true) return result.value;
|
|
19
|
+
const failure = result && result.error ? result.error : null;
|
|
20
|
+
throw makeError(
|
|
21
|
+
failure && typeof failure.code === 'string' ? failure.code : 'failed',
|
|
22
|
+
failure && typeof failure.message === 'string' && failure.message !== ''
|
|
23
|
+
? failure.message
|
|
24
|
+
: (runtime && runtime.t ? runtime.t('error.failed') : 'Request failed'),
|
|
25
|
+
);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function setRuntime(value) { runtime = value; }
|
|
29
|
+
|
|
30
|
+
export { runtime, callRpc, setRuntime };
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/** AI 预算字段展示与输入。 */
|
|
2
|
+
import { Fragment, h } from '../react.js';
|
|
3
|
+
import { AI_EFFORTS, AI_MAX_TOKENS_MAX, AI_MAX_TOKENS_MIN, AI_PRESET_IDS, patchAiBudget, readAiBudget } from './ai-budget.js';
|
|
4
|
+
|
|
5
|
+
function AiBudgetRow(props) {
|
|
6
|
+
const { t, ai, disabled, onChange } = props;
|
|
7
|
+
const value = readAiBudget(ai);
|
|
8
|
+
const presetDetail = (id) => t('settings.aiPresetDetail.' + id);
|
|
9
|
+
const commitTokens = (event) => {
|
|
10
|
+
const raw = String(event.target.value).trim();
|
|
11
|
+
const parsed = raw === '' ? NaN : Number(raw);
|
|
12
|
+
if (!Number.isFinite(parsed)) {
|
|
13
|
+
// 空/非法输入:恢复当前值,不写脏数据。
|
|
14
|
+
event.target.value = String(value.maxTokens);
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
const next = patchAiBudget(value, { maxTokens: parsed });
|
|
18
|
+
onChange(next);
|
|
19
|
+
event.target.value = String(next.maxTokens);
|
|
20
|
+
};
|
|
21
|
+
return h('div', { className: 'SN_aiBudgetRow', 'data-sn-ai-budget': '' },
|
|
22
|
+
h('span', { className: 'SN_aiBudgetLabel', title: t('settings.aiBudgetHint') }, t('settings.aiBudget')),
|
|
23
|
+
h('select', {
|
|
24
|
+
className: 'SN_select',
|
|
25
|
+
value: value.preset,
|
|
26
|
+
disabled: disabled,
|
|
27
|
+
'aria-label': t('settings.aiBudget'),
|
|
28
|
+
title: presetDetail(value.preset),
|
|
29
|
+
onChange: (event) => onChange(patchAiBudget(value, { preset: event.target.value })),
|
|
30
|
+
}, AI_PRESET_IDS.map((id) => h('option', { key: id, value: id, title: presetDetail(id) }, t('settings.aiPreset.' + id)))),
|
|
31
|
+
value.preset === 'custom'
|
|
32
|
+
? h(Fragment, null,
|
|
33
|
+
h('select', {
|
|
34
|
+
className: 'SN_select',
|
|
35
|
+
value: value.reasoningEffort,
|
|
36
|
+
disabled: disabled,
|
|
37
|
+
'aria-label': t('settings.aiEffort'),
|
|
38
|
+
title: t('settings.aiEffort'),
|
|
39
|
+
onChange: (event) => onChange(patchAiBudget(value, { reasoningEffort: event.target.value })),
|
|
40
|
+
}, AI_EFFORTS.map((id) => h('option', { key: id, value: id }, t('settings.aiEffort.' + id)))),
|
|
41
|
+
h('input', {
|
|
42
|
+
type: 'number',
|
|
43
|
+
className: 'SN_aiTokens',
|
|
44
|
+
defaultValue: String(value.maxTokens),
|
|
45
|
+
min: AI_MAX_TOKENS_MIN,
|
|
46
|
+
max: AI_MAX_TOKENS_MAX,
|
|
47
|
+
step: 256,
|
|
48
|
+
disabled: disabled,
|
|
49
|
+
'aria-label': t('settings.aiMaxTokens'),
|
|
50
|
+
title: t('settings.aiMaxTokens'),
|
|
51
|
+
onBlur: commitTokens,
|
|
52
|
+
onKeyDown: (event) => {
|
|
53
|
+
if (event.key === 'Enter') {
|
|
54
|
+
event.preventDefault();
|
|
55
|
+
commitTokens(event);
|
|
56
|
+
}
|
|
57
|
+
},
|
|
58
|
+
}),
|
|
59
|
+
h('span', { className: 'SN_aiUnit' }, t('settings.aiTokensUnit')),
|
|
60
|
+
)
|
|
61
|
+
: null,
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export { AiBudgetRow };
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/** AI 预算预设与规范化纯函数;与 Host 常量保持一致。 */
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
const AI_PRESETS = {
|
|
5
|
+
low: { reasoningEffort: 'off', maxTokens: 2048 },
|
|
6
|
+
medium: { reasoningEffort: 'low', maxTokens: 4096 },
|
|
7
|
+
high: { reasoningEffort: 'high', maxTokens: 8192 },
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
const AI_PRESET_IDS = ['low', 'medium', 'high', 'custom'];
|
|
11
|
+
|
|
12
|
+
const AI_EFFORTS = ['off', 'low', 'high', 'max'];
|
|
13
|
+
|
|
14
|
+
const AI_MAX_TOKENS_MIN = 256;
|
|
15
|
+
|
|
16
|
+
const AI_MAX_TOKENS_MAX = 65536;
|
|
17
|
+
|
|
18
|
+
const DEFAULT_AI = { preset: 'high', reasoningEffort: 'high', maxTokens: 8192 };
|
|
19
|
+
|
|
20
|
+
function readAiBudget(ai) {
|
|
21
|
+
const source = ai && typeof ai === 'object' ? ai : {};
|
|
22
|
+
const preset = AI_PRESET_IDS.indexOf(source.preset) >= 0 ? source.preset : DEFAULT_AI.preset;
|
|
23
|
+
// 非自定义档一律按预设映射,保证界面选哪档、实际请求就是哪档。
|
|
24
|
+
if (preset !== 'custom') return { preset: preset, reasoningEffort: AI_PRESETS[preset].reasoningEffort, maxTokens: AI_PRESETS[preset].maxTokens };
|
|
25
|
+
const reasoningEffort = AI_EFFORTS.indexOf(source.reasoningEffort) >= 0 ? source.reasoningEffort : DEFAULT_AI.reasoningEffort;
|
|
26
|
+
const raw = Number(source.maxTokens);
|
|
27
|
+
const maxTokens = Number.isFinite(raw)
|
|
28
|
+
? Math.min(AI_MAX_TOKENS_MAX, Math.max(AI_MAX_TOKENS_MIN, Math.round(raw)))
|
|
29
|
+
: DEFAULT_AI.maxTokens;
|
|
30
|
+
return { preset: 'custom', reasoningEffort: reasoningEffort, maxTokens: maxTokens };
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function patchAiBudget(ai, patch) {
|
|
34
|
+
const current = readAiBudget(ai);
|
|
35
|
+
const next = Object.assign({}, current, patch && typeof patch === 'object' ? patch : {});
|
|
36
|
+
if (patch && AI_PRESET_IDS.indexOf(patch.preset) >= 0 && patch.preset !== 'custom') {
|
|
37
|
+
Object.assign(next, AI_PRESETS[patch.preset]);
|
|
38
|
+
}
|
|
39
|
+
return readAiBudget(next);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export { AI_PRESETS, AI_PRESET_IDS, AI_EFFORTS, AI_MAX_TOKENS_MIN, AI_MAX_TOKENS_MAX, DEFAULT_AI, readAiBudget, patchAiBudget };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/** 评论 callout 类型和中英文名称。 */
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
const CALLOUT_IDS = [
|
|
5
|
+
'note', 'abstract', 'info', 'todo', 'tip', 'success', 'question',
|
|
6
|
+
'warning', 'failure', 'danger', 'bug', 'example', 'quote',
|
|
7
|
+
];
|
|
8
|
+
|
|
9
|
+
const CALLOUT_ZH = {
|
|
10
|
+
note: '笔记', abstract: '摘要', info: '信息', todo: '待办', tip: '提示',
|
|
11
|
+
success: '成功', question: '问题', warning: '警告', failure: '失败',
|
|
12
|
+
danger: '危险', bug: '缺陷', example: '示例', quote: '引用',
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
const CALLOUT_EN = {
|
|
16
|
+
note: 'Note', abstract: 'Abstract', info: 'Info', todo: 'Todo', tip: 'Tip',
|
|
17
|
+
success: 'Success', question: 'Question', warning: 'Warning', failure: 'Failure',
|
|
18
|
+
danger: 'Danger', bug: 'Bug', example: 'Example', quote: 'Quote',
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export { CALLOUT_IDS, CALLOUT_ZH, CALLOUT_EN };
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/** 打开、加载、重载与另存配置文件的设置组件。 */
|
|
2
|
+
import { React, h } from '../react.js';
|
|
3
|
+
import { joinPath } from '../utils.js';
|
|
4
|
+
|
|
5
|
+
function ConfigFileSettings({ t, config, disabled, request, onSettings }) {
|
|
6
|
+
const [path, setPath] = React.useState('');
|
|
7
|
+
const [mode, setMode] = React.useState('');
|
|
8
|
+
const [busy, setBusy] = React.useState(false);
|
|
9
|
+
const [error, setError] = React.useState('');
|
|
10
|
+
const [message, setMessage] = React.useState('');
|
|
11
|
+
const accept = (result, action) => {
|
|
12
|
+
if (result.settings && onSettings) onSettings(result.settings, action);
|
|
13
|
+
if (action === 'load' || action === 'relocate') { setMode(''); setPath(''); }
|
|
14
|
+
if (action !== 'open' && action !== 'reveal') setMessage(t('settings.configDone'));
|
|
15
|
+
};
|
|
16
|
+
const run = (action) => {
|
|
17
|
+
if (busy || disabled) return;
|
|
18
|
+
setBusy(true); setError(''); setMessage('');
|
|
19
|
+
request('configFile', { action, path }).then((result) => accept(result, action))
|
|
20
|
+
.catch((cause) => setError(cause.message || String(cause))).finally(() => setBusy(false));
|
|
21
|
+
};
|
|
22
|
+
const loadConfig = () => {
|
|
23
|
+
if (busy || disabled || config?.pinned) return;
|
|
24
|
+
setBusy(true); setError(''); setMessage('');
|
|
25
|
+
request('chooseConfigFile', {}).then((result) => {
|
|
26
|
+
if (!result.path) return;
|
|
27
|
+
return request('configFile', { action: 'load', path: result.path }).then((loaded) => accept(loaded, 'load'));
|
|
28
|
+
}).catch((cause) => setError(cause.message || String(cause))).finally(() => setBusy(false));
|
|
29
|
+
};
|
|
30
|
+
const chooseDirectory = () => {
|
|
31
|
+
if (busy || disabled) return;
|
|
32
|
+
setBusy(true); setError('');
|
|
33
|
+
request('chooseConfigDirectory', {}).then((result) => {
|
|
34
|
+
if (!result.path) return;
|
|
35
|
+
setPath(joinPath(result.path, 'config.json'));
|
|
36
|
+
}).catch((cause) => setError(cause.message || String(cause))).finally(() => setBusy(false));
|
|
37
|
+
};
|
|
38
|
+
const showMode = (action) => {
|
|
39
|
+
setError(''); setMessage(''); setMode(action); setPath('');
|
|
40
|
+
};
|
|
41
|
+
const blocked = busy || disabled;
|
|
42
|
+
return h('section', { className: 'SN_drawerSection' },
|
|
43
|
+
h('h3', { className: 'SN_drawerSectionTitle' }, t('settings.config')),
|
|
44
|
+
h('code', { className: 'SN_configPath' }, config?.path || t('panel.loading')),
|
|
45
|
+
config?.error ? h('p', { className: 'SN_settingsError', role: 'alert' }, config.error) : null,
|
|
46
|
+
h('p', { className: 'SN_fieldHint' }, t(config?.pinned ? 'settings.configPinned' : 'settings.configHint')),
|
|
47
|
+
h('div', { className: 'SN_configActions' },
|
|
48
|
+
h('button', { type: 'button', className: 'SN_ghostBtn', disabled: blocked, onClick: () => run('reveal') }, t('settings.openConfig')),
|
|
49
|
+
h('button', { type: 'button', className: 'SN_ghostBtn', disabled: blocked || config?.pinned,
|
|
50
|
+
onClick: loadConfig, title: t(config?.pinned ? 'settings.configPinned' : 'settings.configLoadHint') }, t('settings.loadConfig')),
|
|
51
|
+
h('select', { className: 'SN_ghostBtn', value: '', disabled: blocked, 'aria-label': t('settings.configActions'), onChange: (event) => {
|
|
52
|
+
const action = event.target.value;
|
|
53
|
+
if (action === 'openFile') { setMode(''); run('open'); }
|
|
54
|
+
else if (action === 'reload') { setMode(''); run('reload'); }
|
|
55
|
+
else if (action === 'relocate') showMode('relocate');
|
|
56
|
+
} },
|
|
57
|
+
h('option', { value: '' }, t('settings.configActions')),
|
|
58
|
+
h('option', { value: 'openFile' }, t('settings.openConfigFile')),
|
|
59
|
+
h('option', { value: 'reload' }, t('settings.reloadConfig')),
|
|
60
|
+
!config?.pinned ? h('option', { value: 'relocate' }, t('settings.relocateConfig')) : null)),
|
|
61
|
+
!config?.pinned && mode === 'relocate' ? h('div', { className: 'SN_configEditor' },
|
|
62
|
+
h('p', { className: 'SN_fieldHint' }, t('settings.configCopyHint')),
|
|
63
|
+
h('div', { className: 'SN_configPathRow' },
|
|
64
|
+
h('input', { type: 'text', className: 'SN_textInput', value: path, placeholder: t('settings.configPath'),
|
|
65
|
+
'aria-label': t('settings.configPath'), disabled: blocked, onChange: (event) => setPath(event.target.value) }),
|
|
66
|
+
h('button', { type: 'button', className: 'SN_ghostBtn', disabled: blocked, onClick: chooseDirectory }, t('settings.pick'))),
|
|
67
|
+
h('div', { className: 'SN_pathActions' },
|
|
68
|
+
h('button', { type: 'button', className: 'SN_ghostBtn', disabled: blocked, onClick: () => { setMode(''); setPath(''); } }, t('settings.cancel')),
|
|
69
|
+
h('button', { type: 'button', className: 'SN_primaryBtn', disabled: blocked || !path.trim(), onClick: () => run(mode) }, t('settings.relocateConfig')))) : null,
|
|
70
|
+
error ? h('p', { className: 'SN_settingsError', role: 'alert' }, error) : null,
|
|
71
|
+
message ? h('p', { className: 'SN_fieldHint', role: 'status' }, message) : null);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export { ConfigFileSettings };
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/** 图片压缩与尺寸设置视图。 */
|
|
2
|
+
import { readImageCompression } from '../capture/images.js';
|
|
3
|
+
import { h } from '../react.js';
|
|
4
|
+
|
|
5
|
+
function ImageProcessingSettings({ t, value, disabled, onChange, children }) {
|
|
6
|
+
const config = readImageCompression(value);
|
|
7
|
+
const numeric = (field, min, max, label) => h('label', { className: 'SN_field' },
|
|
8
|
+
h('span', { className: 'SN_fieldLabel' }, t(label)),
|
|
9
|
+
h('input', { type: 'number', className: 'SN_textInput', key: field + ':' + config[field],
|
|
10
|
+
defaultValue: config[field], min, max, step: 1, disabled: disabled || !config.enabled,
|
|
11
|
+
'aria-label': t(label), onBlur: (event) => {
|
|
12
|
+
const raw = event.target.value.trim();
|
|
13
|
+
const next = readImageCompression({ ...config, [field]: raw === '' ? config[field] : Number(raw) });
|
|
14
|
+
onChange(next); event.target.value = String(next[field]);
|
|
15
|
+
}, onKeyDown: (event) => { if (event.key === 'Enter') { event.preventDefault(); event.target.blur(); } },
|
|
16
|
+
}));
|
|
17
|
+
return h('div', { className: 'SN_drawerSection', 'data-sn-image-processing': '' },
|
|
18
|
+
h('div', { className: 'SN_drawerSectionHead' }, h('h3', { className: 'SN_drawerSectionTitle' }, t('settings.images'))),
|
|
19
|
+
children,
|
|
20
|
+
h('label', { className: 'SN_imageSettingToggle' },
|
|
21
|
+
h('input', { type: 'checkbox', checked: config.enabled, disabled,
|
|
22
|
+
onChange: (event) => onChange({ ...config, enabled: event.target.checked }) }),
|
|
23
|
+
h('span', null, t('settings.compressWebp'))),
|
|
24
|
+
h('p', { className: 'SN_drawerSectionHint' }, t('settings.imagesHint')),
|
|
25
|
+
h('div', { className: 'SN_imageSettingNumbers' },
|
|
26
|
+
numeric('quality', 1, 100, 'settings.imageQuality'),
|
|
27
|
+
numeric('maxDimension', 256, 8192, 'settings.imageResolution')),
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export { ImageProcessingSettings };
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/** 官方设置页复用配置轮询、草稿 hook 和设置组件。 */
|
|
2
|
+
import { useNotesData } from '../data/use-notes-data.js';
|
|
3
|
+
import { React, h } from '../react.js';
|
|
4
|
+
import { runtime } from '../runtime.js';
|
|
5
|
+
import { SettingsDrawer } from './settings-drawer.js';
|
|
6
|
+
import { useSettingsDraft } from './use-settings-draft.js';
|
|
7
|
+
|
|
8
|
+
function OfficialSettingsPage() {
|
|
9
|
+
const t = (runtime && runtime.t) || ((key) => key);
|
|
10
|
+
const tRef = React.useRef(t); tRef.current = t;
|
|
11
|
+
const panelRef = React.useRef(null);
|
|
12
|
+
const [message, setMessage] = React.useState('');
|
|
13
|
+
const [error, setError] = React.useState('');
|
|
14
|
+
const data = useNotesData({ panelRef, settingsOnly: true, onState() {}, onVisible() {}, onError: (cause) => setError(cause.message || String(cause)) });
|
|
15
|
+
const controls = useSettingsDraft({ ...data, t, tRef, panelRef, onOpen() {},
|
|
16
|
+
pushToast: (toast) => { setMessage(toast.text); setError(''); }, pushErrorToast: (cause) => setError(cause.message || String(cause)) });
|
|
17
|
+
React.useEffect(() => {
|
|
18
|
+
if (data.view.settings && !controls.settingsOpen && !controls.savingSettings) controls.openSettings();
|
|
19
|
+
}, [data.view.settings, controls.settingsOpen, controls.savingSettings]);
|
|
20
|
+
return h('div', { className: 'SN_panel SN_officialSettings', ref: panelRef },
|
|
21
|
+
error ? h('p', { className: 'SN_settingsError', role: 'alert' }, error) : null,
|
|
22
|
+
message ? h('p', { className: 'SN_fieldHint', role: 'status' }, message) : null,
|
|
23
|
+
!data.view.loaded ? h('p', null, t('panel.loading')) : null,
|
|
24
|
+
controls.settingsDraft ? h(SettingsDrawer, { t, draft: controls.settingsDraft, inline: true,
|
|
25
|
+
picking: controls.picking, saving: controls.savingSettings, request: data.request,
|
|
26
|
+
initialTab: controls.settingsTab, onConfigSettings: controls.acceptConfigSettings,
|
|
27
|
+
onPatch: controls.patchDraft, onPick: controls.systemPick, onRemoveArchive: controls.removeArchiveTarget,
|
|
28
|
+
onPatchEmoji: controls.patchEmoji, onRemoveEmoji: controls.removeEmoji, onAddEmoji: controls.addEmoji,
|
|
29
|
+
onSave: controls.saveSettingsDraft, onCancel: controls.closeSettings }) : null);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export { OfficialSettingsPage };
|