fix editor page transform true source

This commit is contained in:
lix-2026
2026-04-30 22:45:43 +08:00
parent c7cfe85d87
commit 015ad5bedd
6 changed files with 278 additions and 37 deletions
+206 -24
View File
@@ -12,9 +12,10 @@ use serde::{Deserialize, Serialize};
use serde_json::{json, Map, Value};
use std::{any::Any, cell::Cell, collections::HashMap, fmt::Display};
use wasm_bindgen::{closure::Closure, prelude::*, JsCast, JsValue};
use wasm_bindgen_futures::{spawn_local, JsFuture};
use web_sys::{
window, CustomEvent, CustomEventInit, DragEvent, Element, Event, EventTarget, HtmlElement,
MouseEvent, Node, Storage, WheelEvent,
MouseEvent, Node, RequestInit, RequestMode, Response, Storage, WheelEvent,
};
const EDITOR_STAGE_SELECTOR: &str = "#editor-stage";
@@ -4034,6 +4035,126 @@ fn normalized_page_block_title(text: &str) -> String {
}
}
#[derive(Debug, Deserialize)]
struct TreeCommandHttpResponse {
result: Option<TreeCommandResult>,
message: Option<String>,
}
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
struct TreeCommandResult {
document_id: Option<String>,
workspace_id: Option<String>,
title: Option<String>,
parent_id: Option<String>,
}
fn dispatch_tree_local_command(body: &Value, result: &Value) {
let Some(win) = window() else {
return;
};
let Ok(detail) = serde_wasm_bindgen::to_value(&json!({
"body": body,
"result": result,
})) else {
return;
};
let init = CustomEventInit::new();
init.set_detail(&detail);
if let Ok(event) = CustomEvent::new_with_event_init_dict("tree:local-command", &init) {
let _ = win.dispatch_event(&event);
}
}
async fn create_child_page_via_tree_command(
parent_id: String,
workspace_id: Option<String>,
title: String,
) -> Result<TreeCommandResult, String> {
let body = json!({
"action": "create",
"workspaceId": workspace_id,
"parentId": parent_id,
"title": title,
"accessScope": "private",
"content": [],
});
let body_string =
serde_json::to_string(&body).map_err(|err| format!("序列化创建子页面请求失败:{err}"))?;
let request_init = RequestInit::new();
request_init.set_method("POST");
request_init.set_mode(RequestMode::SameOrigin);
request_init.set_body(&JsValue::from_str(&body_string));
let headers = js_sys::Object::new();
js_sys::Reflect::set(
&headers,
&JsValue::from_str("content-type"),
&JsValue::from_str("application/json"),
)
.map_err(|_| "设置创建子页面请求头失败".to_string())?;
js_sys::Reflect::set(
request_init.as_ref(),
&JsValue::from_str("headers"),
&headers,
)
.map_err(|_| "设置创建子页面请求头失败".to_string())?;
let win = window().ok_or_else(|| "当前浏览器窗口不可用".to_string())?;
let response_value =
JsFuture::from(win.fetch_with_str_and_init("/api/tree/commands", &request_init))
.await
.map_err(|err| format!("创建子页面请求失败:{err:?}"))?;
let response: Response = response_value
.dyn_into()
.map_err(|_| "创建子页面响应类型错误".to_string())?;
let status = response.status();
let json_value = JsFuture::from(
response
.json()
.map_err(|_| "读取创建子页面响应失败".to_string())?,
)
.await
.map_err(|err| format!("解析创建子页面响应失败:{err:?}"))?;
let parsed: TreeCommandHttpResponse = serde_wasm_bindgen::from_value(json_value.clone())
.map_err(|err| format!("创建子页面响应 JSON 不符合预期:{err}"))?;
if !response.ok() {
return Err(parsed
.message
.unwrap_or_else(|| format!("创建子页面失败:HTTP {status}")));
}
let result = parsed
.result
.ok_or_else(|| "创建子页面响应缺少 result".to_string())?;
let document_id = result
.document_id
.as_deref()
.map(str::trim)
.filter(|value| !value.is_empty())
.ok_or_else(|| "创建子页面响应缺少 documentId".to_string())?;
if document_id == parent_id {
return Err("创建子页面返回了当前页面 id".to_string());
}
if let Some(created_parent_id) = result
.parent_id
.as_deref()
.map(str::trim)
.filter(|value| !value.is_empty())
{
if created_parent_id != parent_id {
return Err(format!(
"创建子页面父节点不符合预期:期望 {parent_id},实际 {created_parent_id}"
));
}
}
dispatch_tree_local_command(
&body,
&serde_wasm_bindgen::from_value(json_value).unwrap_or(Value::Null)["result"],
);
Ok(result)
}
fn block_plain_text_from_index(
editor: TiptapEditorHandle,
block_index: usize,
@@ -4093,9 +4214,14 @@ fn top_level_block_is_page(editor: TiptapEditorHandle, block_index: usize) -> bo
.unwrap_or(false)
}
fn page_reference_paragraph_node(page_id: &str, title: &str) -> Value {
fn page_reference_paragraph_node(page_id: &str, workspace_id: Option<&str>, title: &str) -> Value {
let href = if page_id.trim().is_empty() {
"#".to_string()
} else if let Some(workspace_id) = workspace_id
.map(str::trim)
.filter(|value| !value.is_empty())
{
format!("/documents/{page_id}?workspaceId={workspace_id}")
} else {
format!("/documents/{page_id}")
};
@@ -4118,6 +4244,7 @@ fn replace_block_with_page_reference(
editor: TiptapEditorHandle,
block_index: usize,
page_id: &str,
workspace_id: Option<&str>,
title: &str,
) -> Result<(), String> {
let mut document = editor
@@ -4127,7 +4254,7 @@ fn replace_block_with_page_reference(
if block_index >= content.len() {
return Err(format!("找不到第 {} 个块", block_index + 1));
}
content[block_index] = page_reference_paragraph_node(page_id, title);
content[block_index] = page_reference_paragraph_node(page_id, workspace_id, title);
let next_payload = document
.get("content")
.and_then(Value::as_array)
@@ -4163,28 +4290,59 @@ fn run_block_turn_into_page_action(
return;
}
};
let page_id = document_id
.get_untracked()
.unwrap_or_else(|| format!("page-block-{}", block_index + 1));
match replace_block_with_page_reference(editor, block_index, &page_id, &page_title) {
Ok(()) => {
set_block_menu_open.set(false);
set_block_menu_anchor.set(None);
set_block_turn_into_open.set(false);
sync_persisted_editor_command(
editor,
&runtime_persisted_identity(document_id, workspace_id),
set_dirty_count,
set_html_output,
set_document_json,
set_json_output,
title,
set_command_feedback,
format!("已转换为页面:{page_title}"),
);
let Some(parent_document_id) = document_id.get_untracked() else {
set_command_feedback.set("转换为页面失败:当前页面缺少 documentId".to_string());
return;
};
let workspace_id_value = workspace_id.get_untracked();
set_command_feedback.set(format!("正在创建子页面:{page_title}"));
spawn_local(async move {
match create_child_page_via_tree_command(
parent_document_id.clone(),
workspace_id_value.clone(),
page_title.clone(),
)
.await
{
Ok(created) => {
let Some(child_page_id) = created.document_id.as_deref() else {
set_command_feedback.set("转换为页面失败:创建响应缺少子页面 id".to_string());
return;
};
let effective_workspace_id = created
.workspace_id
.as_deref()
.or(workspace_id_value.as_deref());
let effective_title = created.title.as_deref().unwrap_or(&page_title);
match replace_block_with_page_reference(
editor,
block_index,
child_page_id,
effective_workspace_id,
effective_title,
) {
Ok(()) => {
set_block_menu_open.set(false);
set_block_menu_anchor.set(None);
set_block_turn_into_open.set(false);
sync_persisted_editor_command(
editor,
&runtime_persisted_identity(document_id, workspace_id),
set_dirty_count,
set_html_output,
set_document_json,
set_json_output,
title,
set_command_feedback,
format!("已转换为子页面:{effective_title}"),
);
}
Err(err) => set_command_feedback.set(err),
}
}
Err(err) => set_command_feedback.set(err),
}
Err(err) => set_command_feedback.set(err),
}
});
}
fn apply_feedback_result<E>(setter: WriteSignal<String>, result: Result<&'static str, E>)
@@ -4931,6 +5089,30 @@ fn App(mount_options: MountOptions) -> impl IntoView {
on_cleanup(move || drop(keydown_handle));
}
{
let click_handle = window_event_listener(ev::click, move |event: MouseEvent| {
let Some(element) = event.target().and_then(target_element) else {
return;
};
let Some(anchor) = element.closest("a.mnote-page-block-link").ok().flatten() else {
return;
};
let Some(href) = anchor
.get_attribute("href")
.map(|value| value.trim().to_string())
.filter(|value| !value.is_empty() && value != "#")
else {
return;
};
event.prevent_default();
event.stop_propagation();
if let Some(win) = window() {
let _ = win.location().assign(&href);
}
});
on_cleanup(move || drop(click_handle));
}
let has_task_schema = move || {
let snapshot = json_output.get();
snapshot.contains("\"taskList\"") && snapshot.contains("\"taskItem\"")