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;