refactor: move selection payload helpers

This commit is contained in:
lix-2026
2026-05-25 05:34:16 +08:00
parent e6ab9268b6
commit ec27e08e8b
5 changed files with 87 additions and 76 deletions
+1 -67
View File
@@ -36,7 +36,7 @@ use editor_runtime::command_sync::{
read_editor_snapshot, sync_editor_outputs, sync_persisted_editor_command,
};
use editor_runtime::dom_selection::{
selection_summary,
runtime_block_id_from_index, selection_payload, selection_summary, SelectionPayload,
};
use editor_runtime::editor_focus::{active_editor_stage, body_has_focus, schedule_editor_focus};
use editor_runtime::overlays::{
@@ -3088,16 +3088,6 @@ struct StatePayload {
read_only: bool,
}
#[derive(Clone, Debug, Serialize)]
#[serde(rename_all = "camelCase")]
struct SelectionPayload {
selection: TiptapSelectionState,
summary: String,
editor_focused: bool,
current_block_index: Option<usize>,
current_block_id: Option<String>,
}
#[derive(Clone, Debug, Serialize)]
#[serde(rename_all = "camelCase")]
struct ChangeMetaPayload {
@@ -3539,48 +3529,6 @@ fn runtime_event_target() -> Option<EventTarget> {
.map(|body| body.into())
}
fn block_id_from_element(block: &Element) -> Option<String> {
block
.get_attribute("data-block-id")
.map(|value| value.trim().to_string())
.filter(|value| !value.is_empty())
}
fn block_id_from_json_node(node: &Value) -> Option<String> {
node.get("attrs")
.and_then(|attrs| attrs.get("blockId"))
.and_then(Value::as_str)
.map(str::trim)
.filter(|value| !value.is_empty())
.map(str::to_string)
}
fn runtime_block_id_from_index(index: usize) -> Option<String> {
editor_root_element()
.and_then(|root| root.children().item(index as u32))
.and_then(|block| block_id_from_element(&block))
.or_else(|| {
editor_root_element()
.and_then(|root| {
js_sys::Reflect::get(root.as_ref(), &JsValue::from_str("editor")).ok()
})
.and_then(|editor_value| {
js_sys::Reflect::get(&editor_value, &JsValue::from_str("getJSON"))
.ok()
.and_then(|callback| callback.dyn_into::<js_sys::Function>().ok())
.and_then(|callback| callback.call0(&editor_value).ok())
})
.and_then(|value| serde_wasm_bindgen::from_value::<Value>(value).ok())
.and_then(|document| {
document
.get("content")
.and_then(Value::as_array)
.and_then(|content| content.get(index))
.and_then(block_id_from_json_node)
})
})
}
fn current_runtime_block_info() -> CurrentBlockInfo {
hovered_block_from_selection()
.and_then(|block| {
@@ -5860,20 +5808,6 @@ fn current_block_info_from_index(index: Option<usize>) -> CurrentBlockInfo {
})
}
fn selection_payload(
selection: &TiptapSelectionState,
editor_focused: bool,
current_block_index: Option<usize>,
) -> SelectionPayload {
SelectionPayload {
selection: selection.clone(),
summary: selection_summary(selection),
editor_focused,
current_block_index,
current_block_id: current_block_index.and_then(runtime_block_id_from_index),
}
}
fn state_payload(
document_id: Option<String>,
workspace_id: Option<String>,