feat: consolidate local-first mnote web runtime

This commit is contained in:
lix-2026
2026-05-28 22:01:44 +08:00
parent 7354807ee9
commit 39b9a0183a
154 changed files with 13591 additions and 12728 deletions
@@ -1,8 +1,11 @@
use crate::document_buffer_store::BufferStore;
use crate::routes::{local_workspace_id_from_root_uri, refresh_local_search_index_for_path};
use crate::routes::{
local_markdown_conflict_detection_key, local_workspace_id_from_root_uri,
refresh_local_search_index_for_path,
};
use notify::event::ModifyKind;
use notify::{Config, Event, EventKind, RecommendedWatcher, RecursiveMode, Watcher};
use serde_json::{json, Value};
use serde_json::{Value, json};
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicUsize, Ordering};
@@ -212,6 +215,12 @@ fn spawn_local_folder_watcher(
&root_uri_for_task,
&relative_path,
);
let mut observed_file_version: Option<String> = None;
let mut buffer_file_version: Option<String> = None;
let mut last_write_intent_id: Option<String> = None;
let mut last_save_operation_id: Option<String> = None;
let mut self_write_echo = false;
let event_kind = format!("{:?}", event.kind);
let document_id = if is_markdown_path(&path) {
let document_id =
format!("local-md:{}", encode_local_id_segment(&relative_path));
@@ -226,10 +235,40 @@ fn spawn_local_folder_watcher(
&relative_path,
&document_id,
);
buffer_store_for_task.mark_external_modified(
&ws_path,
Some("external-editor".into()),
);
if event_kind.contains("Remove") {
if let Some(buffer) =
buffer_store_for_task.mark_local_folder_markdown_deleted(
&ws_id,
&root_uri_for_task,
&relative_path,
&document_id,
)
{
buffer_file_version = buffer.file_version.clone();
last_write_intent_id =
buffer.last_write_intent_id.clone();
last_save_operation_id =
buffer.last_save_operation_id.clone();
}
} else {
observed_file_version =
local_markdown_conflict_detection_key(&document_id, &path)
.ok();
if let Some(outcome) = buffer_store_for_task
.mark_external_modified_if_version_changed_with_outcome(
&ws_path,
observed_file_version.clone(),
Some("external-editor".into()),
)
{
buffer_file_version = outcome.buffer.file_version.clone();
last_write_intent_id =
outcome.buffer.last_write_intent_id.clone();
last_save_operation_id =
outcome.buffer.last_save_operation_id.clone();
self_write_echo = outcome.self_write_echo;
}
}
}
document_id
} else {
@@ -241,8 +280,13 @@ fn spawn_local_folder_watcher(
"rootUri": root_uri_for_task,
"relativePath": relative_path,
"documentId": document_id,
"eventKind": format!("{:?}", event.kind),
"eventKind": event_kind,
"revision": event_revision(&path),
"observedFileVersion": observed_file_version,
"bufferFileVersion": buffer_file_version,
"lastWriteIntentId": last_write_intent_id,
"lastSaveOperationId": last_save_operation_id,
"selfWriteEcho": self_write_echo,
});
let _ = sender_for_task.send(payload);
}
@@ -375,12 +419,12 @@ fn system_time_ms(time: SystemTime) -> u128 {
#[cfg(test)]
mod tests {
use super::{
is_local_search_index_path, refresh_local_search_index_for_event, should_emit_event_kind,
LocalFolderWatcherRegistry,
LocalFolderWatcherRegistry, is_local_search_index_path,
refresh_local_search_index_for_event, should_emit_event_kind,
};
use crate::document_buffer_store::BufferStore;
use notify::event::{AccessKind, CreateKind, DataChange, ModifyKind};
use notify::EventKind;
use notify::event::{AccessKind, CreateKind, DataChange, ModifyKind};
fn test_root(name: &str) -> std::path::PathBuf {
let root = std::env::temp_dir().join(format!(