forked from mxx1111/mdlook
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.rs
More file actions
382 lines (345 loc) · 12.9 KB
/
Copy pathlib.rs
File metadata and controls
382 lines (345 loc) · 12.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
use std::fs;
use std::path::{Path, PathBuf};
use std::sync::Mutex;
use std::time::UNIX_EPOCH;
use serde::Serialize;
use tauri::menu::{MenuBuilder, MenuItemBuilder, SubmenuBuilder};
use tauri::{Emitter, Manager, State};
use tauri_plugin_clipboard_manager::ClipboardExt;
#[cfg(target_os = "macos")]
mod macos_fn_key {
use std::mem;
use std::sync::OnceLock;
use objc2::runtime::{AnyClass, AnyObject, Imp, Sel};
use objc2::{msg_send, sel};
use objc2_app_kit::NSEvent;
type FlagsChangedFn = unsafe extern "C-unwind" fn(&AnyObject, Sel, &NSEvent);
static ORIGINAL_FLAGS_CHANGED: OnceLock<Imp> = OnceLock::new();
/// Tao's macOS view consumes every `flagsChanged:` event but only handles
/// Shift, Control, Option and Command. The standalone Fn/Globe event uses
/// key code 63, so it never reaches AppKit's responder chain and macOS
/// Dictation cannot see the user's configured Fn shortcut.
unsafe extern "C-unwind" fn flags_changed(this: &AnyObject, selector: Sel, event: &NSEvent) {
if event.keyCode() == 63 {
let superclass: &AnyClass = unsafe { msg_send![this, superclass] };
let _: () = unsafe { msg_send![super(this, superclass), flagsChanged: event] };
return;
}
let original = ORIGINAL_FLAGS_CHANGED
.get()
.expect("Tao flagsChanged implementation was not installed");
let original: FlagsChangedFn = unsafe { mem::transmute(*original) };
unsafe { original(this, selector, event) };
}
pub fn install() -> Result<(), String> {
let tao_view =
AnyClass::get(c"TaoView").ok_or_else(|| "TaoView class is unavailable".to_string())?;
let method = tao_view
.instance_method(sel!(flagsChanged:))
.ok_or_else(|| "TaoView flagsChanged: method is unavailable".to_string())?;
let original = method.implementation();
ORIGINAL_FLAGS_CHANGED
.set(original)
.map_err(|_| "TaoView flagsChanged: patch was installed twice".to_string())?;
let replacement: Imp =
unsafe { mem::transmute::<FlagsChangedFn, Imp>(flags_changed as FlagsChangedFn) };
unsafe { method.set_implementation(replacement) };
Ok(())
}
}
/// A node in the local markdown file tree.
#[derive(Serialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct FileNode {
pub name: String,
pub path: String,
pub is_dir: bool,
pub children: Vec<FileNode>,
}
/// Pending file path captured from a "open with mdlook" / file-association launch
/// before the frontend is ready to receive it.
#[derive(Default)]
struct PendingFile(Mutex<Option<String>>);
fn is_markdown(path: &Path) -> bool {
matches!(
path.extension().and_then(|e| e.to_str()).map(|s| s.to_lowercase()),
Some(ref e) if e == "md" || e == "markdown" || e == "mdown" || e == "markdn"
)
}
/// Recursively build a tree of directories and markdown files under `dir`.
/// Hidden entries (starting with '.') and common noise dirs are skipped.
fn build_tree(dir: &Path, depth: usize) -> Vec<FileNode> {
if depth > 12 {
return Vec::new();
}
let mut dirs: Vec<FileNode> = Vec::new();
let mut files: Vec<FileNode> = Vec::new();
let entries = match fs::read_dir(dir) {
Ok(e) => e,
Err(_) => return Vec::new(),
};
for entry in entries.flatten() {
let path = entry.path();
let name = entry.file_name().to_string_lossy().to_string();
if name.starts_with('.') || name == "node_modules" || name == "target" {
continue;
}
if path.is_dir() {
let children = build_tree(&path, depth + 1);
// Only keep directories that contain markdown somewhere.
if !children.is_empty() {
dirs.push(FileNode {
name,
path: path.to_string_lossy().to_string(),
is_dir: true,
children,
});
}
} else if is_markdown(&path) {
files.push(FileNode {
name,
path: path.to_string_lossy().to_string(),
is_dir: false,
children: Vec::new(),
});
}
}
dirs.sort_by(|a, b| a.name.to_lowercase().cmp(&b.name.to_lowercase()));
files.sort_by(|a, b| a.name.to_lowercase().cmp(&b.name.to_lowercase()));
dirs.extend(files);
dirs
}
/// Read a markdown folder into a file tree (directories + .md files only).
#[tauri::command]
fn read_md_tree(path: String) -> Result<Vec<FileNode>, String> {
let p = PathBuf::from(&path);
if !p.is_dir() {
return Err(format!("不是有效的文件夹:{path}"));
}
Ok(build_tree(&p, 0))
}
/// Read a UTF-8 text file.
#[tauri::command]
fn read_text_file(path: String) -> Result<String, String> {
fs::read_to_string(&path).map_err(|e| format!("读取失败:{e}"))
}
/// Write a UTF-8 text file (used to save edits back to the original file).
#[tauri::command]
fn write_text_file(path: String, content: String) -> Result<(), String> {
fs::write(&path, content).map_err(|e| format!("写入失败:{e}"))
}
/// Write a binary file, creating parent directories as needed.
#[tauri::command]
fn write_binary_file(path: String, bytes: Vec<u8>) -> Result<(), String> {
let p = PathBuf::from(&path);
if let Some(parent) = p.parent() {
fs::create_dir_all(parent).map_err(|e| format!("创建目录失败:{e}"))?;
}
fs::write(&p, bytes).map_err(|e| format!("写入失败:{e}"))
}
/// Return the file modified timestamp in milliseconds since Unix epoch.
#[tauri::command]
fn file_modified_millis(path: String) -> Result<u128, String> {
let modified = fs::metadata(&path)
.map_err(|e| format!("读取文件信息失败:{e}"))?
.modified()
.map_err(|e| format!("读取修改时间失败:{e}"))?;
modified
.duration_since(UNIX_EPOCH)
.map(|d| d.as_millis())
.map_err(|e| format!("修改时间异常:{e}"))
}
/// Take (and clear) the pending file path captured at launch, if any.
#[tauri::command]
fn take_pending_file(state: State<'_, PendingFile>) -> Option<String> {
state.0.lock().ok().and_then(|mut g| g.take())
}
/// Write rich HTML (with a plain-text fallback) to the system clipboard.
/// Needed because WKWebView blocks `navigator.clipboard.write` for rich content,
/// which breaks the "一键复制到公众号" flow in the desktop app.
#[tauri::command]
fn copy_html(app: tauri::AppHandle, html: String, text: Option<String>) -> Result<(), String> {
app.clipboard()
.write_html(html, text)
.map_err(|e| e.to_string())
}
/// Write plain text to the system clipboard.
#[tauri::command]
fn copy_text(app: tauri::AppHandle, text: String) -> Result<(), String> {
app.clipboard().write_text(text).map_err(|e| e.to_string())
}
/// Resolve plain CLI-argument paths (Windows/Linux launch) to the first markdown file.
fn first_markdown_arg(args: &[String]) -> Option<String> {
for raw in args {
let pb = PathBuf::from(raw);
if pb.is_file() && is_markdown(&pb) {
return Some(pb.to_string_lossy().to_string());
}
}
None
}
/// Resolve file-open URLs (macOS "open with" / double-click) to the first markdown file.
/// Uses `to_file_path()` so percent-encoded non-ASCII paths (e.g. 中文目录) decode correctly.
fn first_markdown_url(urls: &[tauri::Url]) -> Option<String> {
for url in urls {
if let Ok(pb) = url.to_file_path() {
if pb.is_file() && is_markdown(&pb) {
return Some(pb.to_string_lossy().to_string());
}
}
}
None
}
fn build_menu(app: &tauri::AppHandle) -> tauri::Result<tauri::menu::Menu<tauri::Wry>> {
let app_menu = SubmenuBuilder::new(app, "mdlook")
.about(None)
.separator()
.hide()
.hide_others()
.separator()
.quit()
.build()?;
let file_menu = SubmenuBuilder::new(app, "文件")
.item(
&MenuItemBuilder::with_id("new-draft", "新建草稿")
.accelerator("CmdOrCtrl+N")
.build(app)?,
)
.separator()
.item(
&MenuItemBuilder::with_id("open-file", "打开文件…")
.accelerator("CmdOrCtrl+O")
.build(app)?,
)
.item(
&MenuItemBuilder::with_id("open-folder", "打开文件夹…")
.accelerator("CmdOrCtrl+Shift+O")
.build(app)?,
)
.separator()
.item(
&MenuItemBuilder::with_id("save", "保存")
.accelerator("CmdOrCtrl+S")
.build(app)?,
)
.item(
&MenuItemBuilder::with_id("save-as", "另存为…")
.accelerator("CmdOrCtrl+Shift+S")
.build(app)?,
)
.build()?;
let export_menu = SubmenuBuilder::new(app, "导出")
.item(&MenuItemBuilder::with_id("export-md", "Markdown 文件").build(app)?)
.separator()
.item(&MenuItemBuilder::with_id("export-html", "HTML").build(app)?)
.item(&MenuItemBuilder::with_id("export-pure-html", "无样式 HTML").build(app)?)
.separator()
.item(&MenuItemBuilder::with_id("export-pdf", "PDF").build(app)?)
.item(&MenuItemBuilder::with_id("export-png", "PNG 图片").build(app)?)
.build()?;
let edit_menu = SubmenuBuilder::new(app, "编辑")
.undo()
.redo()
.separator()
.cut()
.copy()
.paste()
.select_all()
.separator()
.item(
&MenuItemBuilder::with_id("copy-to-wechat", "复制到公众号")
.accelerator("CmdOrCtrl+Shift+C")
.build(app)?,
)
.build()?;
let view_menu = SubmenuBuilder::new(app, "视图")
.item(
&MenuItemBuilder::with_id("cycle-view", "切换视图")
.accelerator("CmdOrCtrl+Shift+P")
.build(app)?,
)
.item(&MenuItemBuilder::with_id("toggle-style-panel", "样式面板").build(app)?)
.item(&MenuItemBuilder::with_id("open-history", "版本历史").build(app)?)
.separator()
.fullscreen()
.build()?;
MenuBuilder::new(app)
.item(&app_menu)
.item(&file_menu)
.item(&export_menu)
.item(&edit_menu)
.item(&view_menu)
.build()
}
fn emit_menu_command(app: &tauri::AppHandle, command: &str) {
let is_supported = matches!(
command,
"new-draft"
| "open-file"
| "open-folder"
| "save"
| "save-as"
| "copy-to-wechat"
| "cycle-view"
| "toggle-style-panel"
| "open-history"
| "export-md"
| "export-html"
| "export-pure-html"
| "export-pdf"
| "export-png"
);
if is_supported {
let _ = app.emit("mdlook://menu-command", command);
}
}
#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
tauri::Builder::default()
.plugin(tauri_plugin_dialog::init())
.plugin(tauri_plugin_clipboard_manager::init())
.menu(build_menu)
.on_menu_event(|app, event| {
emit_menu_command(app, event.id().as_ref());
})
.manage(PendingFile::default())
.invoke_handler(tauri::generate_handler![
read_md_tree,
read_text_file,
write_text_file,
write_binary_file,
file_modified_millis,
take_pending_file,
copy_html,
copy_text
])
.setup(|app| {
#[cfg(target_os = "macos")]
macos_fn_key::install().map_err(std::io::Error::other)?;
// On Windows/Linux the opened file arrives as a CLI argument.
let args: Vec<String> = std::env::args().skip(1).collect();
if let Some(p) = first_markdown_arg(&args) {
if let Some(state) = app.try_state::<PendingFile>() {
*state.0.lock().unwrap() = Some(p);
}
}
Ok(())
})
.build(tauri::generate_context!())
.expect("error while building mdlook")
.run(|app_handle, event| {
// macOS delivers "open with" / double-click via the Opened event.
if let tauri::RunEvent::Opened { urls } = event {
if let Some(p) = first_markdown_url(&urls) {
if let Some(state) = app_handle.try_state::<PendingFile>() {
*state.0.lock().unwrap() = Some(p.clone());
}
// Bring the window to the front and notify the frontend.
if let Some(win) = app_handle.get_webview_window("main") {
let _ = win.set_focus();
}
let _ = app_handle.emit("mdlook://open-file", p);
}
}
});
}