mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-12-05 11:15:01 +03:00
fix(editor): preserve desktop table controls and use V2 colors
This commit is contained in:
1 parent
ab7ef04aa9
commit
38b9e02f9e
4 files changed
+240
-27
No files matched your search
@@ -0,0 +1,203 @@
|
||||
use std::path::PathBuf;
|
||||
|
||||
use blocksuite_playground_doc::{read::TableData, schema::BlockSpec};
|
||||
|
||||
use crate::{
|
||||
FormFactor, HOME_PAGE_ID, PlaygroundOptions, PlaygroundTheme,
|
||||
overlays::tests::{Session, open_with_options},
|
||||
room_client::DocState,
|
||||
};
|
||||
|
||||
fn open_table(width: u32, height: u32, options: PlaygroundOptions) -> Session {
|
||||
let session = open_with_options(width, height, options);
|
||||
let mut state = DocState::new("table-controls", 42).unwrap();
|
||||
state
|
||||
.workspace
|
||||
.create_page(HOME_PAGE_ID, "Simple table", 1)
|
||||
.unwrap();
|
||||
let page = state.workspace.ensure_page(HOME_PAGE_ID).unwrap();
|
||||
for (id, spec, parent) in [
|
||||
(
|
||||
"root",
|
||||
BlockSpec::Page {
|
||||
title: "Simple table".into(),
|
||||
},
|
||||
None,
|
||||
),
|
||||
("note", BlockSpec::Note { xywh: None }, Some("root")),
|
||||
(
|
||||
"table",
|
||||
BlockSpec::Table {
|
||||
rows: vec![
|
||||
vec!["Item".into(), "Status".into()],
|
||||
vec!["Draft".into(), "Ready".into()],
|
||||
],
|
||||
},
|
||||
Some("note"),
|
||||
),
|
||||
] {
|
||||
page.add_block(id, spec, parent, None, 2).unwrap();
|
||||
}
|
||||
session.view(|view, cx| view.set_document(state, cx));
|
||||
session.settle();
|
||||
session
|
||||
}
|
||||
|
||||
fn save_evidence(session: &Session, filename: &str) {
|
||||
let Some(dir) = std::env::var_os("TABLE_EVIDENCE_DIR") else {
|
||||
return;
|
||||
};
|
||||
let dir = PathBuf::from(dir);
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
session.app.update(|cx| {
|
||||
session
|
||||
.window
|
||||
.update(cx, |_, window, _| {
|
||||
window
|
||||
.render_to_image()
|
||||
.unwrap()
|
||||
.save(dir.join(filename))
|
||||
.unwrap();
|
||||
})
|
||||
.unwrap();
|
||||
});
|
||||
}
|
||||
|
||||
/// Coordinates inside a cell, with an inset from its right and bottom edges.
|
||||
fn cell_point(session: &Session, index: usize, right: f32, bottom: f32) -> (f32, f32) {
|
||||
session.view(|view, _| {
|
||||
let layout = view.flow_layout().unwrap();
|
||||
let block = layout.block("table").unwrap();
|
||||
let rect = block.body.table_cells[index].rect;
|
||||
let geometry = view.current_page_geometry(view.window_size.0);
|
||||
(
|
||||
geometry.column_x + block.x + rect.right() - right,
|
||||
view.topbar_height() + block.y + rect.bottom() - bottom - view.scroll_y,
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn table_controls_follow_host_and_embedding_in_narrow_viewports() {
|
||||
for (width, height, hide_chrome, form_factor, touch) in [
|
||||
(580, 800, true, FormFactor::Desktop, false),
|
||||
(560, 800, true, FormFactor::Desktop, false),
|
||||
(390, 844, false, FormFactor::Desktop, true),
|
||||
(390, 844, true, FormFactor::Phone, true),
|
||||
] {
|
||||
let session = open_table(
|
||||
width,
|
||||
height,
|
||||
PlaygroundOptions {
|
||||
hide_chrome,
|
||||
form_factor,
|
||||
..Default::default()
|
||||
},
|
||||
);
|
||||
let (x, y) = cell_point(&session, 0, 60.0, 18.0);
|
||||
session.press(x, y, 1);
|
||||
session.release(x, y);
|
||||
assert!(session.view(|view, _| view.active_table_cell().is_some()));
|
||||
|
||||
let (x, y) = cell_point(&session, 2, 60.0, 2.0);
|
||||
session.pointer(x, y, false);
|
||||
let frame = session.settle();
|
||||
if width == 580 {
|
||||
save_evidence(&session, "narrow-desktop.png");
|
||||
}
|
||||
assert_eq!(
|
||||
frame.elements.contains_key("overlay.table_toolbar"),
|
||||
touch,
|
||||
"{width}x{height}, {form_factor:?}, hide_chrome={hide_chrome}"
|
||||
);
|
||||
for id in [
|
||||
"overlay.table_hover.row_pill",
|
||||
"overlay.table_hover.col_pill",
|
||||
"overlay.table_col_resize.col-0",
|
||||
"overlay.table_hover.add_row",
|
||||
] {
|
||||
assert_eq!(
|
||||
frame.elements.contains_key(id),
|
||||
!touch,
|
||||
"{width}x{height}: {id}"
|
||||
);
|
||||
}
|
||||
let (x, y) = cell_point(&session, 1, 2.0, 18.0);
|
||||
session.pointer(x, y, false);
|
||||
assert_eq!(
|
||||
session
|
||||
.settle()
|
||||
.elements
|
||||
.contains_key("overlay.table_hover.add_col"),
|
||||
!touch,
|
||||
"{width}x{height}: add column"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
fn table_data(session: &Session) -> TableData {
|
||||
session.view(|view, _| {
|
||||
view.page()
|
||||
.unwrap()
|
||||
.read_table_block("table")
|
||||
.unwrap()
|
||||
.unwrap()
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn table_axis_swatches_write_v2_keys_and_clear_in_one_undo_step() {
|
||||
for (theme, suffix) in [
|
||||
(PlaygroundTheme::Light, "light"),
|
||||
(PlaygroundTheme::Dark, "dark"),
|
||||
] {
|
||||
let session = open_table(
|
||||
580,
|
||||
800,
|
||||
PlaygroundOptions {
|
||||
theme,
|
||||
hide_chrome: true,
|
||||
form_factor: FormFactor::Desktop,
|
||||
..Default::default()
|
||||
},
|
||||
);
|
||||
let set_color = |pill: &str, slug: &str| {
|
||||
let (x, y) = cell_point(&session, 0, 60.0, 18.0);
|
||||
session.pointer(x, y, false);
|
||||
session.click(&session.settle(), &format!("overlay.table_hover.{pill}"));
|
||||
let menu = session.settle();
|
||||
if pill == "row_pill" && slug == "blue" {
|
||||
save_evidence(&session, &format!("palette-{suffix}.png"));
|
||||
}
|
||||
session.click(&menu, &format!("overlay.table_toolbar.axis_menu.bg_{slug}"));
|
||||
session.settle();
|
||||
};
|
||||
let before = table_data(&session);
|
||||
for (pill, is_row) in [("row_pill", true), ("col_pill", false)] {
|
||||
let color = || {
|
||||
let table = table_data(&session);
|
||||
if is_row {
|
||||
table.rows[0].background_color.clone()
|
||||
} else {
|
||||
table.columns[0].background_color.clone()
|
||||
}
|
||||
};
|
||||
for slug in [
|
||||
"blue", "green", "grey", "orange", "purple", "red", "teal", "yellow",
|
||||
] {
|
||||
set_color(pill, slug);
|
||||
let key = format!("var(--affine-v2-table-headerBackground-{slug})");
|
||||
assert_eq!(color().as_deref(), Some(key.as_str()), "{theme:?} {pill}");
|
||||
if is_row && slug == "blue" {
|
||||
save_evidence(&session, &format!("colored-header-{suffix}.png"));
|
||||
}
|
||||
set_color(pill, "default");
|
||||
assert_eq!(color(), None);
|
||||
session.undo();
|
||||
assert_eq!(color().as_deref(), Some(key.as_str()));
|
||||
session.undo();
|
||||
assert_eq!(table_data(&session), before);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,9 @@ mod structure;
|
||||
mod toolbar;
|
||||
mod touch_bar;
|
||||
|
||||
#[cfg(test)]
|
||||
mod controls_tests;
|
||||
|
||||
pub(crate) use pointer::TablePaintFrame;
|
||||
|
||||
use std::rc::Rc;
|
||||
@@ -32,6 +35,10 @@ pub(crate) enum InlineTargetEnd {
|
||||
}
|
||||
|
||||
impl PlaygroundView {
|
||||
pub(crate) fn uses_touch_table_bar(&self, viewport_width: f32) -> bool {
|
||||
self.is_touch_host() || (!self.hide_chrome && viewport_width < 600.0)
|
||||
}
|
||||
|
||||
/// Active in-place table cell editor, if a cell is currently focused.
|
||||
pub fn active_table_cell(&self) -> Option<&InlineTableCell> {
|
||||
self.table_cell.as_ref()
|
||||
|
||||
@@ -206,10 +206,9 @@ impl PlaygroundView {
|
||||
}
|
||||
|
||||
/// Whether the edited Note shows the touch table bar of the Page in place of the desktop
|
||||
/// table controls and the canvas toolbar: a writable table cell is focused on a touch host
|
||||
/// or in a viewport narrower than 600 px.
|
||||
/// table controls and the canvas toolbar when a writable table cell is focused.
|
||||
pub(crate) fn active_note_touch_table_bar(&self, viewport_width: f32) -> bool {
|
||||
(self.is_touch_host() || viewport_width < 600.0)
|
||||
self.uses_touch_table_bar(viewport_width)
|
||||
&& self.table_cell.is_some()
|
||||
&& !self.table_is_readonly()
|
||||
&& self.database_record_sheet.is_none()
|
||||
|
||||
@@ -33,8 +33,7 @@ impl PlaygroundView {
|
||||
if self.table_is_readonly() || self.database_record_sheet.is_some() {
|
||||
return None;
|
||||
}
|
||||
let touch = self.is_touch_host() || viewport_width < 600.0;
|
||||
if touch {
|
||||
if self.uses_touch_table_bar(viewport_width) {
|
||||
let cell = self.table_cell.as_ref()?;
|
||||
let (_, by, hit) =
|
||||
Self::find_table_cell_hit(layout, &cell.block_id, &cell.row_id, &cell.column_id)?;
|
||||
@@ -553,7 +552,7 @@ impl PlaygroundView {
|
||||
) -> AnyElement {
|
||||
let is_row = axis == TableAxisMenu::Row;
|
||||
let menu_w = TOOLBAR_MENU_MIN_W.max(196.0);
|
||||
let menu_h = 264.0 + TOOLBAR_MENU_ROW_H;
|
||||
let menu_h = 292.0 + TOOLBAR_MENU_ROW_H;
|
||||
let left = x.clamp(8.0, (viewport_w - menu_w - 8.0).max(8.0)).round();
|
||||
let top = y.clamp(8.0, (viewport_h - menu_h - 8.0).max(8.0)).round();
|
||||
let hover = palette.hover;
|
||||
@@ -743,32 +742,37 @@ fn table_axis_swatches(
|
||||
palette: &Palette,
|
||||
cx: &ui::Context<PlaygroundView>,
|
||||
) -> ui::Div {
|
||||
const SWATCHES: [(&str, &str, Option<&str>, u32); 6] = [
|
||||
("Default", "default", None, 0xffffff),
|
||||
(
|
||||
"Gray",
|
||||
"gray",
|
||||
Some("var(--affine-background-secondary-color)"),
|
||||
0xefeff0,
|
||||
),
|
||||
("Blue", "blue", Some("var(--affine-tag-blue)"), 0xdbeafe),
|
||||
("Green", "green", Some("var(--affine-tag-green)"), 0xdcfce7),
|
||||
(
|
||||
"Yellow",
|
||||
"yellow",
|
||||
Some("var(--affine-tag-yellow)"),
|
||||
0xfef9c3,
|
||||
),
|
||||
("Red", "red", Some("var(--affine-tag-red)"), 0xfee2e2),
|
||||
const SWATCHES: [(&str, &str); 9] = [
|
||||
("Default", "default"),
|
||||
("Blue", "blue"),
|
||||
("Green", "green"),
|
||||
("Grey", "grey"),
|
||||
("Orange", "orange"),
|
||||
("Purple", "purple"),
|
||||
("Red", "red"),
|
||||
("Teal", "teal"),
|
||||
("Yellow", "yellow"),
|
||||
];
|
||||
SWATCHES.into_iter().fold(
|
||||
let swatches = SWATCHES.into_iter().filter_map(|(name, slug)| {
|
||||
let key = format!("var(--affine-v2-table-headerBackground-{slug})");
|
||||
let value = (slug != "default").then_some(key);
|
||||
let fill = match value.as_deref() {
|
||||
Some(key) => palette.theme.css_color(key)?,
|
||||
None => palette
|
||||
.theme
|
||||
.css_var("--affine-v2-layer-background-primary")?,
|
||||
};
|
||||
Some((name, slug, fill, value))
|
||||
});
|
||||
swatches.fold(
|
||||
div()
|
||||
.flex()
|
||||
.flex_wrap()
|
||||
.items_center()
|
||||
.gap(px(4.0))
|
||||
.px(px(6.0))
|
||||
.py(px(4.0)),
|
||||
|row, (name, slug, css_var, hex)| {
|
||||
|row, (name, slug, fill, value)| {
|
||||
row.child(
|
||||
div()
|
||||
.id(SharedString::from(format!("tbl-bg-{name}")))
|
||||
@@ -776,13 +780,13 @@ fn table_axis_swatches(
|
||||
.w(px(24.0))
|
||||
.h(px(24.0))
|
||||
.rounded(px(4.0))
|
||||
.bg(ui::rgb(hex))
|
||||
.bg(fill)
|
||||
.border_1()
|
||||
.border_color(palette.border)
|
||||
.cursor_pointer()
|
||||
.on_mouse_down(ui::MouseButton::Left, |_, _, cx| cx.stop_propagation())
|
||||
.on_click(cx.listener(move |this, _, win, cx| {
|
||||
let _ = this.table_set_axis_background(is_row, css_var, win, cx);
|
||||
let _ = this.table_set_axis_background(is_row, value.as_deref(), win, cx);
|
||||
}))
|
||||
.snapshot(
|
||||
format_args!("overlay.table_toolbar.axis_menu.bg_{slug}"),
|
||||
|
||||
Reference in new issue
Block a user