Improve local filetree view state and sidebar performance
This commit is contained in:
@@ -174,6 +174,57 @@ impl BufferStore {
|
||||
}
|
||||
}
|
||||
|
||||
/// 本地文件 rename/move 后重绑打开的 Markdown buffer。
|
||||
pub fn rekey_local_folder_markdown(
|
||||
&self,
|
||||
workspace_id: &str,
|
||||
root_uri: &str,
|
||||
previous_relative_path: &str,
|
||||
previous_document_id: &str,
|
||||
next_relative_path: &str,
|
||||
next_document_id: &str,
|
||||
) -> Option<DocumentBuffer> {
|
||||
let previous_path = build_local_folder_workspace_path(
|
||||
workspace_id,
|
||||
root_uri,
|
||||
previous_relative_path,
|
||||
previous_document_id,
|
||||
);
|
||||
let next_path = build_local_folder_workspace_path(
|
||||
workspace_id,
|
||||
root_uri,
|
||||
next_relative_path,
|
||||
next_document_id,
|
||||
);
|
||||
let previous_key = BufferKey::from_workspace_path(&previous_path);
|
||||
let next_key = BufferKey::from_workspace_path(&next_path);
|
||||
let mut inner = self.inner.write().expect("BufferStore lock");
|
||||
let mut buffer = inner.buffers.remove(&previous_key)?;
|
||||
buffer.workspace_path = next_path;
|
||||
inner.buffers.insert(next_key, buffer.clone());
|
||||
Some(buffer)
|
||||
}
|
||||
|
||||
/// 本地文件 delete/archive/purge 后标记打开的 Markdown buffer 已删除。
|
||||
pub fn mark_local_folder_markdown_deleted(
|
||||
&self,
|
||||
workspace_id: &str,
|
||||
root_uri: &str,
|
||||
relative_path: &str,
|
||||
document_id: &str,
|
||||
) -> Option<DocumentBuffer> {
|
||||
let path =
|
||||
build_local_folder_workspace_path(workspace_id, root_uri, relative_path, document_id);
|
||||
let key = BufferKey::from_workspace_path(&path);
|
||||
let mut inner = self.inner.write().expect("BufferStore lock");
|
||||
if let Some(buf) = inner.buffers.get_mut(&key) {
|
||||
buf.mark_deleted();
|
||||
Some(buf.clone())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
/// 获取或创建 buffer 时设置 file_version 和 base_content_hash(从 aggregate 加载后调用)。
|
||||
pub fn init_buffer(
|
||||
&self,
|
||||
@@ -449,4 +500,70 @@ mod tests {
|
||||
assert_eq!(buf.file_version.as_deref(), Some("v2"));
|
||||
assert_eq!(buf.base_content_hash.as_deref(), Some("sha256:saved"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn document_buffer_rekeys_after_local_file_operation_rename() {
|
||||
let store = BufferStore::new();
|
||||
let root_uri = "file:///tmp/mnote-buffer-rekey";
|
||||
let old_path = build_local_folder_workspace_path(
|
||||
"local:test",
|
||||
root_uri,
|
||||
"docs/Old.md",
|
||||
"local-md:docs~2FOld.md",
|
||||
);
|
||||
store.init_buffer(&old_path, Some("v1".into()), Some("sha256:old".into()));
|
||||
|
||||
let rekeyed = store
|
||||
.rekey_local_folder_markdown(
|
||||
"local:test",
|
||||
root_uri,
|
||||
"docs/Old.md",
|
||||
"local-md:docs~2FOld.md",
|
||||
"docs/New.md",
|
||||
"local-md:docs~2FNew.md",
|
||||
)
|
||||
.expect("buffer should be rekeyed");
|
||||
|
||||
assert_eq!(rekeyed.workspace_path.relative_path, "docs/New.md");
|
||||
assert_eq!(
|
||||
rekeyed
|
||||
.workspace_path
|
||||
.object_identity
|
||||
.document_id
|
||||
.as_deref(),
|
||||
Some("local-md:docs~2FNew.md")
|
||||
);
|
||||
assert!(store.get_by_path(&old_path).is_none());
|
||||
let next_path = build_local_folder_workspace_path(
|
||||
"local:test",
|
||||
root_uri,
|
||||
"docs/New.md",
|
||||
"local-md:docs~2FNew.md",
|
||||
);
|
||||
assert!(store.get_by_path(&next_path).is_some());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn document_buffer_marks_deleted_after_local_file_operation_archive() {
|
||||
let store = BufferStore::new();
|
||||
let root_uri = "file:///tmp/mnote-buffer-delete";
|
||||
let path = build_local_folder_workspace_path(
|
||||
"local:test",
|
||||
root_uri,
|
||||
"docs/Delete.md",
|
||||
"local-md:docs~2FDelete.md",
|
||||
);
|
||||
store.get_or_create(&path);
|
||||
|
||||
let deleted = store
|
||||
.mark_local_folder_markdown_deleted(
|
||||
"local:test",
|
||||
root_uri,
|
||||
"docs/Delete.md",
|
||||
"local-md:docs~2FDelete.md",
|
||||
)
|
||||
.expect("buffer should be marked deleted");
|
||||
|
||||
assert_eq!(deleted.dirty_state, DocBufferDirtyState::Deleted);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user