refactor: share editor content layout helpers

This commit is contained in:
lix-2026
2026-05-25 09:17:48 +08:00
parent c3b1aea30e
commit 563e64e5de
6 changed files with 46 additions and 41 deletions
@@ -8,7 +8,10 @@
//! - `hover_anchor_transform`
//! - `block_menu_layout`(参数化版)
use crate::editor_runtime::block_hover_state::{BlockMenuLayout, HoveredBlockState};
use crate::editor_runtime::{
block_hover_state::{BlockMenuLayout, HoveredBlockState},
content_layout::content_text_left,
};
/// 多行块的最小高度阈值,超过此值视作多行。
const HANDLE_MULTILINE_HEIGHT: f64 = 40.0;
@@ -16,29 +19,6 @@ const HANDLE_MULTILINE_HEIGHT: f64 = 40.0;
/// handle shell 菜单左侧最小限制。
const HANDLE_MENU_LEFT_LIMIT: f64 = -320.0;
/// 内容列水平内边距(与 lib.rs 中同名常量同值)。
const CONTENT_COLUMN_HORIZONTAL_PADDING: f64 = 48.0;
/// 内容列最大宽度(与 lib.rs 中同名常量同值)。
const CONTENT_COLUMN_MAX_WIDTH: f64 = 708.0;
/// 根据 stage 宽度计算实际内容列宽。
fn content_column_width(stage_rect_width: f64) -> f64 {
let available = (stage_rect_width - (CONTENT_COLUMN_HORIZONTAL_PADDING * 2.0)).max(0.0);
available.min(CONTENT_COLUMN_MAX_WIDTH)
}
/// 内容列左侧相对于 stage 的偏移。
fn content_column_left(stage_rect_width: f64) -> f64 {
let column_width = content_column_width(stage_rect_width);
((stage_rect_width - column_width) / 2.0).max(0.0)
}
/// 内容文本区左侧相对于 stage 的偏移(列左内边距后)。
fn content_text_left(stage_rect_width: f64) -> f64 {
content_column_left(stage_rect_width) + CONTENT_COLUMN_HORIZONTAL_PADDING
}
/// 计算 handle shell 锚点在 stage 内的垂直位置。
pub(crate) fn hover_anchor_top(block: &HoveredBlockState) -> f64 {
if block.height > HANDLE_MULTILINE_HEIGHT {
@@ -0,0 +1,22 @@
/// 内容列最大宽度。
pub(crate) const CONTENT_COLUMN_MAX_WIDTH: f64 = 708.0;
/// 内容列水平内边距。
pub(crate) const CONTENT_COLUMN_HORIZONTAL_PADDING: f64 = 48.0;
/// 根据 stage 宽度计算实际内容列宽。
pub(crate) fn content_column_width(stage_rect_width: f64) -> f64 {
let available = (stage_rect_width - (CONTENT_COLUMN_HORIZONTAL_PADDING * 2.0)).max(0.0);
available.min(CONTENT_COLUMN_MAX_WIDTH)
}
/// 内容列左侧相对于 stage 的偏移。
pub(crate) fn content_column_left(stage_rect_width: f64) -> f64 {
let column_width = content_column_width(stage_rect_width);
((stage_rect_width - column_width) / 2.0).max(0.0)
}
/// 内容文本区左侧相对于 stage 的偏移。
pub(crate) fn content_text_left(stage_rect_width: f64) -> f64 {
content_column_left(stage_rect_width) + CONTENT_COLUMN_HORIZONTAL_PADDING
}
@@ -6,6 +6,7 @@ pub(crate) mod block_menu_document;
pub(crate) mod block_menu_overlay;
pub(crate) mod block_menu_legacy_html;
pub(crate) mod command_sync;
pub(crate) mod content_layout;
pub(crate) mod dom_events;
pub(crate) mod dom_selection;
pub(crate) mod editor_focus;
+1 -16
View File
@@ -40,6 +40,7 @@ use editor_runtime::block_hover_state::{
use editor_runtime::command_sync::{
read_editor_snapshot, sync_editor_outputs, sync_persisted_editor_command,
};
use editor_runtime::content_layout::{content_column_left, content_text_left};
use editor_runtime::dom_events::{event_target_matches_selector, target_element};
use editor_runtime::dom_selection::{
block_index_from_selection, runtime_block_id_from_index, selection_payload,
@@ -66,8 +67,6 @@ use editor_runtime::persistence::persisted_document_storage_key;
const EDITOR_STAGE_SELECTOR: &str = "[data-testid=\"mnote-leptos-tiptap-editor-stage\"]";
const EDITOR_ROOT_SELECTOR: &str = ".editor-surface .ProseMirror";
const HANDLE_SHELL_SELECTOR: &str = ".block-handle-shell";
const CONTENT_COLUMN_MAX_WIDTH: f64 = 708.0;
const CONTENT_COLUMN_HORIZONTAL_PADDING: f64 = 48.0;
const HANDLE_STAGE_PADDING_LEFT: f64 = 18.0;
const HANDLE_TRIGGER_WIDTH: f64 = 22.0;
const HANDLE_TRIGGER_GAP: f64 = 4.0;
@@ -4809,20 +4808,6 @@ pub(crate) fn editor_stage_element() -> Option<Element> {
})
}
fn content_column_width(stage_rect_width: f64) -> f64 {
let available = (stage_rect_width - (CONTENT_COLUMN_HORIZONTAL_PADDING * 2.0)).max(0.0);
available.min(CONTENT_COLUMN_MAX_WIDTH)
}
fn content_column_left(stage_rect_width: f64) -> f64 {
let column_width = content_column_width(stage_rect_width);
((stage_rect_width - column_width) / 2.0).max(0.0)
}
fn content_text_left(stage_rect_width: f64) -> f64 {
content_column_left(stage_rect_width) + CONTENT_COLUMN_HORIZONTAL_PADDING
}
fn handle_lane_left(stage_rect_width: f64) -> f64 {
(content_text_left(stage_rect_width) + HANDLE_TEXT_ALIGN_OFFSET
- HANDLE_TRIGGER_WIDTH