refactor: remove unused block document helpers
This commit is contained in:
@@ -2,69 +2,6 @@
|
||||
|
||||
use serde_json::{json, Map, Value};
|
||||
|
||||
use crate::editor_runtime::block_hover_state::DropPlacement;
|
||||
|
||||
fn document_content_mut(document: &mut Value) -> Result<&mut Vec<Value>, String> {
|
||||
document
|
||||
.get_mut("content")
|
||||
.and_then(Value::as_array_mut)
|
||||
.ok_or_else(|| "文档 JSON 缺少顶层 content 数组".to_string())
|
||||
}
|
||||
|
||||
pub(crate) fn duplicate_top_level_block(document: &mut Value, index: usize) -> Result<(), String> {
|
||||
let content = document_content_mut(document)?;
|
||||
let block = content
|
||||
.get(index)
|
||||
.cloned()
|
||||
.ok_or_else(|| format!("找不到第 {index} 个块"))?;
|
||||
content.insert(index + 1, block);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn delete_top_level_block(document: &mut Value, index: usize) -> Result<(), String> {
|
||||
let content = document_content_mut(document)?;
|
||||
if index >= content.len() {
|
||||
return Err(format!("找不到第 {index} 个块"));
|
||||
}
|
||||
|
||||
if content.len() == 1 {
|
||||
content[0] = json!({"type": "paragraph"});
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
content.remove(index);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn reorder_top_level_block(
|
||||
document: &mut Value,
|
||||
source: usize,
|
||||
target: usize,
|
||||
placement: DropPlacement,
|
||||
) -> Result<(), String> {
|
||||
let content = document_content_mut(document)?;
|
||||
if source >= content.len() || target >= content.len() {
|
||||
return Err("拖拽目标超出当前顶层块范围".to_string());
|
||||
}
|
||||
|
||||
let block = content.remove(source);
|
||||
let mut insert_at = match placement {
|
||||
DropPlacement::Before => target,
|
||||
DropPlacement::After => target + 1,
|
||||
};
|
||||
|
||||
if source < insert_at {
|
||||
insert_at = insert_at.saturating_sub(1);
|
||||
}
|
||||
|
||||
if insert_at > content.len() {
|
||||
insert_at = content.len();
|
||||
}
|
||||
|
||||
content.insert(insert_at, block);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn node_with_attrs(
|
||||
type_name: &str,
|
||||
attrs: Option<Map<String, Value>>,
|
||||
@@ -115,49 +52,3 @@ pub(crate) fn mindmap_paragraph_node(next_mindmap_id: impl FnOnce() -> String) -
|
||||
attrs.insert("projectionVersion".to_string(), json!(1));
|
||||
node_with_attrs("paragraph", Some(attrs), Vec::new())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use serde_json::json;
|
||||
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn delete_only_top_level_block_keeps_empty_paragraph() {
|
||||
let mut document = json!({
|
||||
"type": "doc",
|
||||
"content": [
|
||||
{"type": "paragraph", "content": [{"type": "text", "text": "A"}]}
|
||||
]
|
||||
});
|
||||
|
||||
delete_top_level_block(&mut document, 0).expect("delete block");
|
||||
|
||||
assert_eq!(
|
||||
document,
|
||||
json!({"type": "doc", "content": [{"type": "paragraph"}]})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reorder_top_level_block_adjusts_target_after_source_removal() {
|
||||
let mut document = json!({
|
||||
"type": "doc",
|
||||
"content": [
|
||||
{"type": "paragraph", "attrs": {"id": "a"}},
|
||||
{"type": "paragraph", "attrs": {"id": "b"}},
|
||||
{"type": "paragraph", "attrs": {"id": "c"}}
|
||||
]
|
||||
});
|
||||
|
||||
reorder_top_level_block(&mut document, 0, 2, DropPlacement::After).expect("reorder block");
|
||||
|
||||
let ids: Vec<_> = document["content"]
|
||||
.as_array()
|
||||
.expect("content")
|
||||
.iter()
|
||||
.map(|node| node["attrs"]["id"].as_str().unwrap_or_default())
|
||||
.collect();
|
||||
assert_eq!(ids, vec!["b", "c", "a"]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user