mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-12-05 11:15:01 +03:00
feat(editor): drag table rows and columns by their axis pills
Port the BlockSuite rowDrag and columnDrag gestures to the Simple Table axis pills, in Page mode and in an edited Edgeless note. - A click on a pill opens the axis menu as before. Like the BlockSuite pill click, the press and the release must be on the same pill, and the pointer must not move more than 10 px along the axis. - A longer drag closes the menu and hides the pills and add bars. It tints the dragged row or column and shows a preview of its cells that follows the pointer. A 5 px indicator marks the target gap. The gap rule is the same as getTargetIndexByDraggingOffset. - The release writes one fractional order key through the new PageDocSession::move_table_axis_to, in one undo step. Cell text, colors and column widths stay with the row or column. - A release at the start gap writes nothing. Escape stops the drag and writes nothing. - move_table_axis now uses move_table_axis_to.
This commit is contained in:
1 parent
9312cb424b
commit
91d9e00dd9
10 files changed
+1039
-70
No files matched your search
@@ -5,7 +5,7 @@
|
||||
|
||||
use y_octo::{Any, SessionValue, TextDeltaOp, TypePath, TypeTarget};
|
||||
|
||||
use crate::{DocError, DocResult};
|
||||
use crate::{DocError, DocResult, read::TableData};
|
||||
|
||||
use super::{
|
||||
PageCommit, PageDocSession,
|
||||
@@ -337,38 +337,45 @@ impl PageDocSession {
|
||||
let Some(table) = self.read_table_block(block_id)? else {
|
||||
return Ok(None);
|
||||
};
|
||||
let orders: Vec<(&str, &str)> = if is_row {
|
||||
table
|
||||
.rows
|
||||
.iter()
|
||||
.map(|r| (r.row_id.as_str(), r.order.as_str()))
|
||||
.collect()
|
||||
} else {
|
||||
table
|
||||
.columns
|
||||
.iter()
|
||||
.map(|c| (c.column_id.as_str(), c.order.as_str()))
|
||||
.collect()
|
||||
};
|
||||
let Some(idx) = orders.iter().position(|(id, _)| *id == axis_id) else {
|
||||
let Some(index) = table_axis_orders(&table, is_row)
|
||||
.iter()
|
||||
.position(|(id, _)| *id == axis_id)
|
||||
else {
|
||||
return Ok(None);
|
||||
};
|
||||
let (prev_order, next_order) = if earlier {
|
||||
if idx == 0 {
|
||||
return Ok(None);
|
||||
}
|
||||
let prev = idx.checked_sub(2).map(|i| orders[i].1);
|
||||
let next = Some(orders[idx - 1].1);
|
||||
(prev, next)
|
||||
let gap = if earlier {
|
||||
index.saturating_sub(1)
|
||||
} else {
|
||||
if idx + 1 >= orders.len() {
|
||||
return Ok(None);
|
||||
}
|
||||
let prev = Some(orders[idx + 1].1);
|
||||
let next = orders.get(idx + 2).map(|(_, o)| *o);
|
||||
(prev, next)
|
||||
index + 2
|
||||
};
|
||||
let new_order = self.next_jittered_order_key(prev_order, next_order)?;
|
||||
self.move_table_axis_to(block_id, is_row, axis_id, gap, now_ms)
|
||||
}
|
||||
|
||||
/// Move a table row (`is_row == true`) or column (`is_row == false`) into `gap` of the
|
||||
/// visible order (`0` is before the first, `len` is after the last), like BlockSuite
|
||||
/// `moveRow` / `moveColumn`: one fractional `order` write in one undo step. A gap beside
|
||||
/// the axis or past the end writes nothing.
|
||||
pub fn move_table_axis_to(
|
||||
&mut self,
|
||||
block_id: &str,
|
||||
is_row: bool,
|
||||
axis_id: &str,
|
||||
gap: usize,
|
||||
now_ms: u64,
|
||||
) -> DocResult<PageCommit> {
|
||||
let Some(table) = self.read_table_block(block_id)? else {
|
||||
return Ok(None);
|
||||
};
|
||||
let orders = table_axis_orders(&table, is_row);
|
||||
let Some(index) = orders.iter().position(|(id, _)| *id == axis_id) else {
|
||||
return Ok(None);
|
||||
};
|
||||
if gap > orders.len() || gap == index || gap == index + 1 {
|
||||
return Ok(None);
|
||||
}
|
||||
let prev = gap.checked_sub(1).map(|i| orders[i].1);
|
||||
let next = orders.get(gap).map(|(_, order)| *order);
|
||||
let new_order = self.next_jittered_order_key(prev, next)?;
|
||||
self.set_table_axis_field(
|
||||
block_id,
|
||||
is_row,
|
||||
@@ -460,6 +467,23 @@ impl PageDocSession {
|
||||
}
|
||||
}
|
||||
|
||||
/// `(id, order)` of every row (`is_row == true`) or column in visible order.
|
||||
fn table_axis_orders(table: &TableData, is_row: bool) -> Vec<(&str, &str)> {
|
||||
if is_row {
|
||||
table
|
||||
.rows
|
||||
.iter()
|
||||
.map(|r| (r.row_id.as_str(), r.order.as_str()))
|
||||
.collect()
|
||||
} else {
|
||||
table
|
||||
.columns
|
||||
.iter()
|
||||
.map(|c| (c.column_id.as_str(), c.order.as_str()))
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use y_octo::{Any, SessionValue, TextDeltaOp, TextInsert, TypePath, TypeTarget};
|
||||
@@ -704,4 +728,69 @@ mod tests {
|
||||
assert!(crate::fractional_index::validate_order_key(&pair[1].order).is_ok());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn move_table_axis_to_writes_one_order_per_real_move_and_undoes_in_one_step() {
|
||||
let ids = |page: &PageDocSession, is_row: bool| -> Vec<String> {
|
||||
let table = page.read_table_block("tbl").unwrap().unwrap();
|
||||
if is_row {
|
||||
table.rows.into_iter().map(|r| r.row_id).collect()
|
||||
} else {
|
||||
table.columns.into_iter().map(|c| c.column_id).collect()
|
||||
}
|
||||
};
|
||||
for (is_row, axis, gap, expected) in [
|
||||
(true, "row-0", 3, Some(["row-1", "row-2", "row-0", "row-3"])),
|
||||
(true, "row-3", 0, Some(["row-3", "row-0", "row-1", "row-2"])),
|
||||
(true, "row-1", 4, Some(["row-0", "row-2", "row-3", "row-1"])),
|
||||
(
|
||||
false,
|
||||
"col-2",
|
||||
1,
|
||||
Some(["col-0", "col-2", "col-1", "col-3"]),
|
||||
),
|
||||
(true, "row-1", 1, None),
|
||||
(true, "row-1", 2, None),
|
||||
(false, "col-0", 5, None),
|
||||
(false, "missing", 0, None),
|
||||
] {
|
||||
let mut page = PageDocSession::new("p-move", 7).unwrap();
|
||||
page.add_block("page", BlockSpec::Page { title: "T".into() }, None, None, 1)
|
||||
.unwrap();
|
||||
page.add_block(
|
||||
"note",
|
||||
BlockSpec::Note { xywh: None },
|
||||
Some("page"),
|
||||
None,
|
||||
2,
|
||||
)
|
||||
.unwrap();
|
||||
let rows = (0..4)
|
||||
.map(|r| (0..4).map(|c| format!("{r}{c}")).collect())
|
||||
.collect();
|
||||
page.add_block("tbl", BlockSpec::Table { rows }, Some("note"), None, 3)
|
||||
.unwrap();
|
||||
page.stop_capturing().unwrap();
|
||||
let before = page.read_table_block("tbl").unwrap().unwrap();
|
||||
let commit = page
|
||||
.move_table_axis_to("tbl", is_row, axis, gap, 10)
|
||||
.unwrap();
|
||||
let case = format!("{axis} -> {gap}");
|
||||
let Some(expected) = expected else {
|
||||
assert!(commit.is_none(), "{case}: zero writes");
|
||||
assert_eq!(page.read_table_block("tbl").unwrap().unwrap(), before);
|
||||
continue;
|
||||
};
|
||||
assert!(commit.is_some(), "{case}");
|
||||
assert_eq!(ids(&page, is_row), expected, "{case}");
|
||||
let after = page.read_table_block("tbl").unwrap().unwrap();
|
||||
assert_eq!(after.cells, before.cells, "{case}: cells follow their ids");
|
||||
page.undo().unwrap();
|
||||
assert_eq!(
|
||||
page.read_table_block("tbl").unwrap().unwrap(),
|
||||
before,
|
||||
"{case}: one undo step"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -125,8 +125,8 @@ pub use slash::{
|
||||
slash_rows_for_entries, slash_rows_for_query, slash_submenu_items, slash_surface_refs,
|
||||
};
|
||||
pub use table_cell::{
|
||||
InlineTableCell, TableAddDrag, TableAddDragKind, TableAxisMenu, TableCellDragMode,
|
||||
TableColResizeDrag, TableHoverState,
|
||||
InlineTableCell, TableAddDrag, TableAddDragKind, TableAxisDrag, TableAxisMenu,
|
||||
TableCellDragMode, TableColResizeDrag, TableHoverState,
|
||||
};
|
||||
pub use toolbar::{
|
||||
ActiveSelection, FormatAction, FormatMoreAction, LinkBar, LinkEditField, MarkState,
|
||||
|
||||
@@ -29,6 +29,22 @@ pub struct TableColResizeDrag {
|
||||
pub preview_width: f32,
|
||||
}
|
||||
|
||||
/// Press on a Simple Table three-dot axis pill. A release within 10 px opens the axis menu;
|
||||
/// further movement drags the row or column to the gap under the pointer (BlockSuite
|
||||
/// `rowDrag` / `columnDrag`).
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct TableAxisDrag {
|
||||
pub axis: TableAxisMenu,
|
||||
/// Visible index of the dragged row or column.
|
||||
pub source: usize,
|
||||
pub start_screen: (f32, f32),
|
||||
pub pointer_screen: (f32, f32),
|
||||
/// Whether the pointer has moved more than 10 px along the axis.
|
||||
pub active: bool,
|
||||
/// Gap (`0..=len`) the release moves the axis into; `None` keeps it in place.
|
||||
pub drop_gap: Option<usize>,
|
||||
}
|
||||
|
||||
/// Kind of Simple Table hover add-bar drag (right bar = columns, bottom bar = rows, corner = both).
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum TableAddDragKind {
|
||||
@@ -101,6 +117,7 @@ pub struct InlineTableCell {
|
||||
pub drag_mode: TableCellDragMode,
|
||||
pub axis_menu: Option<TableAxisMenu>,
|
||||
pub col_resize_drag: Option<TableColResizeDrag>,
|
||||
pub axis_drag: Option<TableAxisDrag>,
|
||||
}
|
||||
|
||||
impl Deref for InlineTableCell {
|
||||
@@ -136,6 +153,7 @@ impl InlineTableCell {
|
||||
drag_mode: TableCellDragMode::Character,
|
||||
axis_menu: None,
|
||||
col_resize_drag: None,
|
||||
axis_drag: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -86,10 +86,9 @@ impl PlaygroundView {
|
||||
return;
|
||||
}
|
||||
if self.overlays.table_hover.add_drag.is_some()
|
||||
|| self
|
||||
.table_cell
|
||||
.as_ref()
|
||||
.is_some_and(|c| c.dragging || c.col_resize_drag.is_some())
|
||||
|| self.table_cell.as_ref().is_some_and(|c| {
|
||||
c.dragging || c.col_resize_drag.is_some() || c.axis_drag.is_some()
|
||||
})
|
||||
{
|
||||
self.table_cell_mouse_move(event, window, cx);
|
||||
return;
|
||||
@@ -102,10 +101,9 @@ impl PlaygroundView {
|
||||
&& !self.overlays.marquee_active()
|
||||
{
|
||||
if self.overlays.table_hover.add_drag.is_some()
|
||||
|| self
|
||||
.table_cell
|
||||
.as_ref()
|
||||
.is_some_and(|c| c.dragging || c.col_resize_drag.is_some())
|
||||
|| self.table_cell.as_ref().is_some_and(|c| {
|
||||
c.dragging || c.col_resize_drag.is_some() || c.axis_drag.is_some()
|
||||
})
|
||||
{
|
||||
self.table_cell_mouse_move(event, window, cx);
|
||||
return;
|
||||
@@ -166,6 +164,7 @@ impl PlaygroundView {
|
||||
if self.mouse_image_resize().is_some() {
|
||||
self.finish_image_resize_drag(window, cx);
|
||||
}
|
||||
self.finish_table_axis_drag(window, cx);
|
||||
self.finish_table_add_drag(window, cx);
|
||||
self.finish_table_col_resize_drag(window, cx);
|
||||
if let Some(cell) = self.table_cell.as_mut() {
|
||||
|
||||
@@ -0,0 +1,816 @@
|
||||
//! Simple Table row and column reorder by dragging a three-dot axis pill, after BlockSuite
|
||||
//! `rowDrag` / `columnDrag`: a release within 10 px opens the axis menu, a longer drag shows a
|
||||
//! preview of the axis under the pointer and an indicator on the target gap, and the release
|
||||
//! moves the axis there in one undo step.
|
||||
|
||||
use blocksuite_editor::overlays::{TableAxisDrag, TableAxisMenu};
|
||||
use blocksuite_playground_doc::read::TableData;
|
||||
use ui::{
|
||||
AnyElement, BoxShadow, ContentMask, Context, ScaledPixels, TransformationMatrix, Window, div,
|
||||
hsla, point, prelude::*, px, size,
|
||||
};
|
||||
|
||||
use super::TablePaintFrame;
|
||||
use crate::{
|
||||
PlaygroundView,
|
||||
block_flow::{FlowBlock, FlowRect, FlowTextLayout, TableCellHit},
|
||||
theme::Palette,
|
||||
};
|
||||
|
||||
/// Screen distance along the axis that turns a pill press into a drag.
|
||||
const DRAG_THRESHOLD: f32 = 10.0;
|
||||
|
||||
/// Edges of the rows or columns of a laid-out table along one axis, in block-local flow units.
|
||||
struct AxisEdges {
|
||||
/// The start edge of every row or column in visible order, then the end edge of the last.
|
||||
/// Columns of a right-to-left table run leftward, so their edges are negated `x` values.
|
||||
offsets: Vec<f32>,
|
||||
/// `-1.0` when the logical order runs against the physical axis.
|
||||
sign: f32,
|
||||
}
|
||||
|
||||
impl AxisEdges {
|
||||
fn of(cells: &[TableCellHit], table: &TableData, axis: TableAxisMenu) -> Option<Self> {
|
||||
let rects: Vec<FlowRect> = match axis {
|
||||
TableAxisMenu::Row => {
|
||||
let column = &table.columns.first()?.column_id;
|
||||
table
|
||||
.rows
|
||||
.iter()
|
||||
.map(|row| axis_cell(cells, &row.row_id, column))
|
||||
.collect::<Option<_>>()?
|
||||
}
|
||||
TableAxisMenu::Column => {
|
||||
let row = &table.rows.first()?.row_id;
|
||||
table
|
||||
.columns
|
||||
.iter()
|
||||
.map(|column| axis_cell(cells, row, &column.column_id))
|
||||
.collect::<Option<_>>()?
|
||||
}
|
||||
};
|
||||
let last = *rects.last()?;
|
||||
if axis == TableAxisMenu::Row {
|
||||
let mut offsets: Vec<f32> = rects.iter().map(|r| r.y).collect();
|
||||
offsets.push(last.bottom());
|
||||
return Some(Self { offsets, sign: 1.0 });
|
||||
}
|
||||
let sign = if rects.len() > 1 && rects[1].x < rects[0].x {
|
||||
-1.0
|
||||
} else {
|
||||
1.0
|
||||
};
|
||||
let start = |r: &FlowRect| if sign > 0.0 { r.x } else { -r.right() };
|
||||
let mut offsets: Vec<f32> = rects.iter().map(start).collect();
|
||||
offsets.push(if sign > 0.0 { last.right() } else { -last.x });
|
||||
Some(Self { offsets, sign })
|
||||
}
|
||||
|
||||
/// Gap (`0..=len`) the axis at `source` moves into when its band starts at `band_start`,
|
||||
/// like BlockSuite `getTargetIndexByDraggingOffset`: the band must pass the center of a
|
||||
/// row or column to take its place. `None` keeps the axis in place.
|
||||
fn drop_gap(&self, source: usize, band_start: f32) -> Option<usize> {
|
||||
let o = &self.offsets;
|
||||
let start = *o.get(source)?;
|
||||
let band_end = band_start + (o.get(source + 1)? - start);
|
||||
let center = |i: usize| (o[i] + o[i + 1]) * 0.5;
|
||||
if band_start > start {
|
||||
(source + 1..o.len() - 1)
|
||||
.rev()
|
||||
.find(|&i| band_end > center(i))
|
||||
.map(|i| i + 1)
|
||||
} else {
|
||||
(0..source).find(|&i| band_start < center(i))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn axis_cell(cells: &[TableCellHit], row: &str, column: &str) -> Option<FlowRect> {
|
||||
cells
|
||||
.iter()
|
||||
.find(|c| c.row_id == row && c.column_id == column)
|
||||
.map(|c| c.rect)
|
||||
}
|
||||
|
||||
impl PlaygroundView {
|
||||
/// Start a press on the axis pill of `axis` through the cell at `row_id` × `column_id`.
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub(super) fn begin_table_axis_drag(
|
||||
&mut self,
|
||||
block_id: String,
|
||||
row_id: String,
|
||||
column_id: String,
|
||||
axis: TableAxisMenu,
|
||||
position: (f32, f32),
|
||||
window: &mut Window,
|
||||
cx: &mut Context<Self>,
|
||||
) {
|
||||
let same_cell = self.table_cell.as_ref().is_some_and(|c| {
|
||||
c.block_id == block_id && c.row_id == row_id && c.column_id == column_id
|
||||
});
|
||||
if !same_cell {
|
||||
self.focus_table_cell(block_id, row_id, column_id, None, window, cx);
|
||||
}
|
||||
let Some(source) = self.table_cell.as_ref().and_then(|cell| {
|
||||
let table = self.focused_table(cell)?;
|
||||
match axis {
|
||||
TableAxisMenu::Row => table.rows.iter().position(|r| r.row_id == cell.row_id),
|
||||
TableAxisMenu::Column => table
|
||||
.columns
|
||||
.iter()
|
||||
.position(|c| c.column_id == cell.column_id),
|
||||
}
|
||||
}) else {
|
||||
return;
|
||||
};
|
||||
if let Some(cell) = self.table_cell.as_mut() {
|
||||
cell.axis_drag = Some(TableAxisDrag {
|
||||
axis,
|
||||
source,
|
||||
start_screen: position,
|
||||
pointer_screen: position,
|
||||
active: false,
|
||||
drop_gap: None,
|
||||
});
|
||||
}
|
||||
cx.notify();
|
||||
}
|
||||
|
||||
/// Follow the pointer of a pill press: past the threshold, close the axis menu and track the
|
||||
/// gap the dragged axis would move into. Writes nothing.
|
||||
pub(super) fn table_axis_drag_move(&mut self, position: (f32, f32), cx: &mut Context<Self>) {
|
||||
let note = self.active_note_table_target();
|
||||
let scale = note.as_ref().map_or(1.0, |(_, t)| t.scale.max(0.05));
|
||||
let layout = match note {
|
||||
Some((layout, _)) => Some(layout),
|
||||
None => self.flow_layout(),
|
||||
};
|
||||
let Some(cell) = self.table_cell.as_ref() else {
|
||||
return;
|
||||
};
|
||||
let edges = layout
|
||||
.as_ref()
|
||||
.zip(self.focused_table(cell))
|
||||
.and_then(|(l, table)| {
|
||||
let drag = cell.axis_drag.as_ref()?;
|
||||
AxisEdges::of(&l.block(&cell.block_id)?.body.table_cells, table, drag.axis)
|
||||
});
|
||||
let Some(cell) = self.table_cell.as_mut() else {
|
||||
return;
|
||||
};
|
||||
let Some(drag) = cell.axis_drag.as_mut() else {
|
||||
return;
|
||||
};
|
||||
drag.pointer_screen = position;
|
||||
let along = match drag.axis {
|
||||
TableAxisMenu::Row => position.1 - drag.start_screen.1,
|
||||
TableAxisMenu::Column => position.0 - drag.start_screen.0,
|
||||
};
|
||||
if !drag.active && along.abs() <= DRAG_THRESHOLD {
|
||||
return;
|
||||
}
|
||||
if !drag.active {
|
||||
drag.active = true;
|
||||
cell.axis_menu = None;
|
||||
cell.area = None;
|
||||
}
|
||||
drag.drop_gap = edges.and_then(|e| {
|
||||
let start = *e.offsets.get(drag.source)?;
|
||||
e.drop_gap(drag.source, start + e.sign * along / scale)
|
||||
});
|
||||
cx.notify();
|
||||
}
|
||||
|
||||
/// Toggle the axis menu on a click of the pill of `axis`, like the BlockSuite pill `click`:
|
||||
/// only a press and release on the same pill that never passed the drag threshold opens it.
|
||||
/// The pill's click runs before the window's release ends the press.
|
||||
pub(super) fn click_table_axis_pill(
|
||||
&mut self,
|
||||
axis: TableAxisMenu,
|
||||
window: &mut Window,
|
||||
cx: &mut Context<Self>,
|
||||
) {
|
||||
let Some((block_id, row_id, column_id)) = self.table_cell.as_ref().and_then(|c| {
|
||||
c.axis_drag
|
||||
.as_ref()
|
||||
.filter(|d| d.axis == axis && !d.active)
|
||||
.map(|_| (c.block_id.clone(), c.row_id.clone(), c.column_id.clone()))
|
||||
}) else {
|
||||
return;
|
||||
};
|
||||
self.overlays.table_axis_scroll.set(0.0);
|
||||
self.open_table_axis_menu_for(block_id, row_id, column_id, axis, window, cx);
|
||||
}
|
||||
|
||||
/// End a pill press. A drag moves its axis into the tracked gap in one undo step; a press
|
||||
/// that never passed the threshold only ends.
|
||||
pub(crate) fn finish_table_axis_drag(&mut self, window: &mut Window, cx: &mut Context<Self>) {
|
||||
let Some((block_id, row_id, column_id, drag)) = self.table_cell.as_mut().and_then(|c| {
|
||||
let drag = c.axis_drag.take()?;
|
||||
Some((
|
||||
c.block_id.clone(),
|
||||
c.row_id.clone(),
|
||||
c.column_id.clone(),
|
||||
drag,
|
||||
))
|
||||
}) else {
|
||||
return;
|
||||
};
|
||||
if !drag.active {
|
||||
return;
|
||||
}
|
||||
cx.notify();
|
||||
let Some(gap) = drag.drop_gap.filter(|_| !self.table_is_readonly()) else {
|
||||
return;
|
||||
};
|
||||
let is_row = drag.axis == TableAxisMenu::Row;
|
||||
let axis_id = if is_row { row_id } else { column_id };
|
||||
let now_ms = self.now_ms(cx) as u64;
|
||||
let res = self.page_mut().and_then(|page| {
|
||||
page.stop_capturing()?;
|
||||
let commit = page.move_table_axis_to(&block_id, is_row, &axis_id, gap, now_ms)?;
|
||||
page.stop_capturing()?;
|
||||
Ok(commit)
|
||||
});
|
||||
match res {
|
||||
Ok(Some(commit)) => {
|
||||
self.did_table_cell_commit(&block_id, commit);
|
||||
window.notify_text_input_changed(&self.editor_focus);
|
||||
}
|
||||
Ok(None) => {}
|
||||
Err(err) => eprintln!("table move axis: {err}"),
|
||||
}
|
||||
}
|
||||
|
||||
/// Drop a pill press without writing anything. Returns whether one was active.
|
||||
pub(super) fn cancel_table_axis_drag(&mut self, cx: &mut Context<Self>) -> bool {
|
||||
let cancelled = self
|
||||
.table_cell
|
||||
.as_mut()
|
||||
.is_some_and(|c| c.axis_drag.take().is_some());
|
||||
if cancelled {
|
||||
cx.notify();
|
||||
}
|
||||
cancelled
|
||||
}
|
||||
|
||||
/// Whether a pill drag of `block_id` is past the threshold (the pills and add bars hide).
|
||||
pub(super) fn table_axis_dragging(&self, block_id: &str) -> bool {
|
||||
self.table_cell.as_ref().is_some_and(|c| {
|
||||
c.block_id == block_id && c.axis_drag.as_ref().is_some_and(|d| d.active)
|
||||
})
|
||||
}
|
||||
|
||||
/// Source highlight, pointer-following preview, and target gap indicator of an active pill
|
||||
/// drag on `block`, mapped through `frame`.
|
||||
pub(super) fn table_axis_drag_elements(
|
||||
&self,
|
||||
block: &FlowBlock,
|
||||
frame: TablePaintFrame,
|
||||
palette: &Palette,
|
||||
) -> Vec<AnyElement> {
|
||||
let mut out = Vec::new();
|
||||
let Some((cell, drag)) = self.table_cell.as_ref().and_then(|c| {
|
||||
let drag = c.axis_drag.as_ref().filter(|d| d.active)?;
|
||||
(c.block_id == block.block_id).then_some((c, drag))
|
||||
}) else {
|
||||
return out;
|
||||
};
|
||||
let cells = &block.body.table_cells;
|
||||
let Some(table) = self.focused_table(cell) else {
|
||||
return out;
|
||||
};
|
||||
let Some(edges) = AxisEdges::of(cells, table, drag.axis) else {
|
||||
return out;
|
||||
};
|
||||
let scale = match frame {
|
||||
TablePaintFrame::Page(_) => 1.0,
|
||||
TablePaintFrame::Note { transform, .. } => transform.scale.max(0.05),
|
||||
};
|
||||
let row = drag.axis == TableAxisMenu::Row;
|
||||
let source: Vec<&TableCellHit> = cells
|
||||
.iter()
|
||||
.filter(|c| match row {
|
||||
true => c.row_id == cell.row_id,
|
||||
false => c.column_id == cell.column_id,
|
||||
})
|
||||
.collect();
|
||||
let Some(first) = cells.first().map(|c| c.rect) else {
|
||||
return out;
|
||||
};
|
||||
let table_box = cells.iter().fold(
|
||||
(first.x, first.y, first.right(), first.bottom()),
|
||||
|(l, t, r, b), c| {
|
||||
(
|
||||
l.min(c.rect.x),
|
||||
t.min(c.rect.y),
|
||||
r.max(c.rect.right()),
|
||||
b.max(c.rect.bottom()),
|
||||
)
|
||||
},
|
||||
);
|
||||
let scroll_x = self.overlays.code_scroll_x(&block.block_id);
|
||||
let (dx, dy) = (block.x - scroll_x, block.y);
|
||||
let (vis_left, vis_right) = (block.x, block.x + block.body.width);
|
||||
let clip = |r: FlowRect| {
|
||||
let r = r.translated(dx, dy);
|
||||
let (l, rt) = (r.x.max(vis_left), r.right().min(vis_right));
|
||||
(rt > l).then(|| frame.map(FlowRect::new(l, r.y, rt - l, r.h)))
|
||||
};
|
||||
let at = |m: FlowRect| {
|
||||
div()
|
||||
.absolute()
|
||||
.left(px(m.x))
|
||||
.top(px(m.y))
|
||||
.w(px(m.w))
|
||||
.h(px(m.h))
|
||||
};
|
||||
|
||||
let tint = palette.brand.opacity(0.12);
|
||||
for hit in &source {
|
||||
if let Some(m) = clip(hit.rect) {
|
||||
out.push(at(m).bg(tint).into_any_element());
|
||||
}
|
||||
}
|
||||
|
||||
// The indicator goes over the preview so the target gap stays visible under it.
|
||||
let indicator = drag.drop_gap.and_then(|gap| {
|
||||
let b = edges.sign * edges.offsets.get(gap)?;
|
||||
let (l, t, r, bottom) = table_box;
|
||||
let line = match row {
|
||||
true => FlowRect::new(l - 1.0, b - 2.0, r - l + 2.0, 5.0),
|
||||
false => FlowRect::new(b - 2.0, t - 1.0, 5.0, bottom - t + 2.0),
|
||||
};
|
||||
let color = palette
|
||||
.theme
|
||||
.css_var("--affine-v2-table-indicator-activated")
|
||||
.unwrap_or(palette.brand);
|
||||
Some(
|
||||
at(clip(line)?)
|
||||
.bg(color)
|
||||
.snapshot("overlay.table_hover.drag_indicator", Some("root.canvas"))
|
||||
.into_any_element(),
|
||||
)
|
||||
});
|
||||
|
||||
let along = match row {
|
||||
true => drag.pointer_screen.1 - drag.start_screen.1,
|
||||
false => drag.pointer_screen.0 - drag.start_screen.0,
|
||||
} / scale;
|
||||
let Some(band) = source.iter().map(|c| c.rect).reduce(|a, c| {
|
||||
let (l, t) = (a.x.min(c.x), a.y.min(c.y));
|
||||
FlowRect::new(
|
||||
l,
|
||||
t,
|
||||
a.right().max(c.right()) - l,
|
||||
a.bottom().max(c.bottom()) - t,
|
||||
)
|
||||
}) else {
|
||||
return out;
|
||||
};
|
||||
let separator = palette
|
||||
.theme
|
||||
.css_var("--affine-v2-layer-insideBorder-border")
|
||||
.unwrap_or(palette.border);
|
||||
let pieces: Vec<(FlowRect, (f32, f32), FlowTextLayout)> = source
|
||||
.iter()
|
||||
.map(|hit| {
|
||||
let local = hit.rect.translated(-band.x, -band.y);
|
||||
let text = (
|
||||
hit.text_origin.0 - hit.rect.x,
|
||||
hit.text_origin.1 - hit.rect.y,
|
||||
);
|
||||
(local, text, hit.text.clone())
|
||||
})
|
||||
.collect();
|
||||
let moved = match row {
|
||||
true => band.translated(0.0, along),
|
||||
false => band.translated(along, 0.0),
|
||||
};
|
||||
let screen = frame.map(moved.translated(dx, dy));
|
||||
out.push(
|
||||
at(screen)
|
||||
.opacity(0.8)
|
||||
.bg(palette
|
||||
.theme
|
||||
.css_var("--affine-v2-layer-background-primary")
|
||||
.unwrap_or(palette.panel))
|
||||
.shadow(
|
||||
vec![BoxShadow {
|
||||
color: hsla(0.0, 0.0, 0.0, 0.1),
|
||||
offset: point(px(0.0), px(0.0)),
|
||||
blur_radius: px(10.0),
|
||||
spread_radius: px(0.0),
|
||||
inset: false,
|
||||
}]
|
||||
.into(),
|
||||
)
|
||||
.snapshot("overlay.table_hover.drag_preview", Some("root.canvas"))
|
||||
.child(
|
||||
ui::canvas(
|
||||
|_, _, _| (),
|
||||
move |bounds, _, window, cx| {
|
||||
let paint = |window: &mut Window, cx: &mut ui::App| {
|
||||
for (index, (rect, text, layout)) in pieces.iter().enumerate() {
|
||||
let cell_bounds = rect.bounds(point(px(0.0), px(0.0)));
|
||||
if index > 0 {
|
||||
let edge = match row {
|
||||
true => FlowRect::new(rect.x, rect.y, 1.0, rect.h),
|
||||
false => FlowRect::new(rect.x, rect.y, rect.w, 1.0),
|
||||
};
|
||||
window.paint_quad(ui::fill(
|
||||
edge.bounds(point(px(0.0), px(0.0))),
|
||||
separator,
|
||||
));
|
||||
}
|
||||
let origin = point(px(rect.x + text.0), px(rect.y + text.1));
|
||||
window.with_local_paint_mask(
|
||||
ContentMask {
|
||||
bounds: cell_bounds,
|
||||
},
|
||||
|window| match layout {
|
||||
FlowTextLayout::Rich(rich) => {
|
||||
rich.paint_at(origin, &[], &[], None, window, cx)
|
||||
}
|
||||
FlowTextLayout::Plain(plain) => {
|
||||
plain.paint(origin, window, cx)
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
};
|
||||
let sf = window.scale_factor();
|
||||
let transform = TransformationMatrix::unit()
|
||||
.translate(point(
|
||||
ScaledPixels(f32::from(bounds.origin.x) * sf),
|
||||
ScaledPixels(f32::from(bounds.origin.y) * sf),
|
||||
))
|
||||
.scale(size(scale, scale));
|
||||
window.with_paint_transformation(transform, |w| paint(w, cx));
|
||||
},
|
||||
)
|
||||
.size_full(),
|
||||
)
|
||||
.into_any_element(),
|
||||
);
|
||||
out.extend(indicator);
|
||||
out
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use blocksuite_playground_doc::{read::TableData, schema::BlockSpec};
|
||||
|
||||
use super::AxisEdges;
|
||||
use crate::{
|
||||
HOME_PAGE_ID, PlaygroundMode, PlaygroundOptions, PlaygroundTheme,
|
||||
overlays::tests::{Session, element, open_with_options},
|
||||
room_client::DocState,
|
||||
};
|
||||
|
||||
const MENU: &str = "overlay.table_toolbar.axis_menu";
|
||||
const PREVIEW: &str = "overlay.table_hover.drag_preview";
|
||||
const INDICATOR: &str = "overlay.table_hover.drag_indicator";
|
||||
const CASES: [(PlaygroundMode, PlaygroundTheme); 3] = [
|
||||
(PlaygroundMode::Page, PlaygroundTheme::Light),
|
||||
(PlaygroundMode::Page, PlaygroundTheme::Dark),
|
||||
(PlaygroundMode::Edgeless, PlaygroundTheme::Light),
|
||||
];
|
||||
|
||||
#[test]
|
||||
fn drop_gap_follows_block_suite_band_centers() {
|
||||
// Four 40-unit rows; the dragged band must pass a neighbour's center to take its place.
|
||||
let edges = AxisEdges {
|
||||
offsets: vec![0.0, 40.0, 80.0, 120.0, 160.0],
|
||||
sign: 1.0,
|
||||
};
|
||||
for (source, band_start, gap) in [
|
||||
(0, 0.0, None),
|
||||
(0, 19.0, None),
|
||||
(0, 21.0, Some(2)),
|
||||
(0, 61.0, Some(3)),
|
||||
(0, 200.0, Some(4)),
|
||||
(3, 120.0, None),
|
||||
(3, 101.0, None),
|
||||
(3, 99.0, Some(2)),
|
||||
(3, -50.0, Some(0)),
|
||||
(1, 30.0, None),
|
||||
(1, 10.0, Some(0)),
|
||||
] {
|
||||
assert_eq!(
|
||||
edges.drop_gap(source, band_start),
|
||||
gap,
|
||||
"{source} at {band_start}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// A 4 × 3 table `t` (cell text `r<row>c<col>`) in the note `n`; in Edgeless the note is
|
||||
/// edited at zoom 0.75.
|
||||
fn open_table(mode: PlaygroundMode, theme: PlaygroundTheme) -> Session {
|
||||
let session = open_with_options(
|
||||
1280,
|
||||
800,
|
||||
PlaygroundOptions {
|
||||
mode,
|
||||
theme,
|
||||
..Default::default()
|
||||
},
|
||||
);
|
||||
let mut state = DocState::new("table-axis-drag", 61).unwrap();
|
||||
state
|
||||
.workspace
|
||||
.create_page(HOME_PAGE_ID, "Axis drag", 1)
|
||||
.unwrap();
|
||||
let page = state.workspace.page_mut(HOME_PAGE_ID).unwrap();
|
||||
let rows = (0..4)
|
||||
.map(|r| (0..3).map(|c| format!("r{r}c{c}")).collect())
|
||||
.collect();
|
||||
for (id, spec, parent) in [
|
||||
(
|
||||
"root",
|
||||
BlockSpec::Page {
|
||||
title: "Axis drag".into(),
|
||||
},
|
||||
None,
|
||||
),
|
||||
(
|
||||
"n",
|
||||
BlockSpec::Note {
|
||||
xywh: Some("[0,0,800,600]".into()),
|
||||
},
|
||||
Some("root"),
|
||||
),
|
||||
("t", BlockSpec::Table { rows }, Some("n")),
|
||||
] {
|
||||
page.add_block(id, spec, parent, None, 2).unwrap();
|
||||
}
|
||||
page.set_table_column_width("t", "col-0", 200.0, 3).unwrap();
|
||||
session.view(|v, cx| v.set_document(state, cx));
|
||||
session.settle();
|
||||
if mode == PlaygroundMode::Edgeless {
|
||||
session.app.update(|cx| {
|
||||
assert!(
|
||||
session
|
||||
.window
|
||||
.update(cx, |v, w, cx| v.edgeless_begin_inline_edit("n", w, cx))
|
||||
.unwrap()
|
||||
);
|
||||
});
|
||||
session.view(|v, cx| v.set_edgeless_zoom(0.75, cx));
|
||||
session.settle();
|
||||
}
|
||||
session
|
||||
}
|
||||
|
||||
/// Screen center of the cell at `row` × `col` and the screen size of one flow unit.
|
||||
fn cell_center(session: &Session, row: usize, col: usize) -> ((f32, f32), f32) {
|
||||
session.view(|v, _| {
|
||||
let index = row * 3 + col;
|
||||
if let Some((layout, t)) = v.active_note_table_target() {
|
||||
let block = layout.block("t").unwrap();
|
||||
let cell = &block.body.table_cells[index];
|
||||
let r = t.rect_to_screen(&cell.rect.translated(block.x, block.y));
|
||||
return ((r.x + r.w * 0.5, r.y + r.h * 0.5), t.scale);
|
||||
}
|
||||
let layout = v.flow_layout().unwrap();
|
||||
let block = layout.block("t").unwrap();
|
||||
let rect = block.body.table_cells[index].rect;
|
||||
let geometry = v.current_page_geometry(v.window_size.0);
|
||||
let x = geometry.column_x + block.x + rect.x + rect.w * 0.5;
|
||||
let y = v.topbar_height() + block.y + rect.y + rect.h * 0.5 - v.scroll_y;
|
||||
((x, y), 1.0)
|
||||
})
|
||||
}
|
||||
|
||||
fn table(session: &Session) -> TableData {
|
||||
session.view(|v, _| v.page().unwrap().read_table_block("t").unwrap().unwrap())
|
||||
}
|
||||
|
||||
/// Cell text in visible order, one string per row.
|
||||
fn grid(table: &TableData) -> Vec<String> {
|
||||
table
|
||||
.rows
|
||||
.iter()
|
||||
.map(|r| {
|
||||
table
|
||||
.columns
|
||||
.iter()
|
||||
.map(|c| {
|
||||
table.cells[&(r.row_id.clone(), c.column_id.clone())]
|
||||
.text
|
||||
.plain
|
||||
.as_str()
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
.join(" ")
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Focus the cell at `row` × `col` and return the screen center of the pill `pill`.
|
||||
fn pill(session: &Session, row: usize, col: usize, pill: &str) -> (f32, f32) {
|
||||
let ((x, y), _) = cell_center(session, row, col);
|
||||
session.press(x, y, 1);
|
||||
session.release(x, y);
|
||||
session.pointer(x, y, false);
|
||||
let b = element(&session.settle(), &format!("overlay.table_hover.{pill}")).bounds;
|
||||
(b.x + b.width * 0.5, b.y + b.height * 0.5)
|
||||
}
|
||||
|
||||
fn save_evidence(session: &Session, filename: &str) {
|
||||
let Some(dir) = std::env::var_os("TABLE_EVIDENCE_DIR") else {
|
||||
return;
|
||||
};
|
||||
let dir = std::path::PathBuf::from(dir);
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
session.app.update(|cx| {
|
||||
session
|
||||
.window
|
||||
.update(cx, |_, window, _| {
|
||||
let image = window.render_to_image().unwrap();
|
||||
image.save(dir.join(filename)).unwrap();
|
||||
})
|
||||
.unwrap();
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn row_pill_click_opens_the_menu_and_drag_reorders_rows_in_one_undo_step() {
|
||||
for (mode, theme) in CASES {
|
||||
let session = open_table(mode, theme);
|
||||
let shot = |name: &str| {
|
||||
let file = format!("{name}-{mode:?}-{theme:?}.png").to_lowercase();
|
||||
save_evidence(&session, &file);
|
||||
};
|
||||
let before = table(&session);
|
||||
let (px, py) = pill(&session, 0, 1, "row_pill");
|
||||
shot("row-before");
|
||||
let written = session.written();
|
||||
session.press(px, py, 1);
|
||||
session.pointer(px, py + 6.0, true);
|
||||
session.release(px, py + 6.0);
|
||||
let frame = session.settle();
|
||||
assert!(
|
||||
frame.elements.contains_key(MENU),
|
||||
"{mode:?}: a click opens the menu"
|
||||
);
|
||||
assert_eq!(
|
||||
session.written(),
|
||||
written,
|
||||
"{mode:?}: a click writes nothing"
|
||||
);
|
||||
|
||||
// Two rows down: the band passes the centers of rows 1 and 2.
|
||||
let ((_, top), unit) = cell_center(&session, 0, 1);
|
||||
let ((_, third), _) = cell_center(&session, 2, 1);
|
||||
let target = py + (third - top) + 4.0 * unit;
|
||||
session.press(px, py, 1);
|
||||
session.pointer(px, py + 12.0, true);
|
||||
session.pointer(px, target, true);
|
||||
let frame = session.frame();
|
||||
shot("row-dragging");
|
||||
for id in [PREVIEW, INDICATOR] {
|
||||
assert!(frame.elements.contains_key(id), "{mode:?}: {id}");
|
||||
}
|
||||
assert!(
|
||||
!frame.elements.contains_key(MENU),
|
||||
"{mode:?}: the drag closes the menu"
|
||||
);
|
||||
assert!(!frame.elements.contains_key("overlay.table_hover.row_pill"));
|
||||
let ((_, gap_y), _) = cell_center(&session, 3, 1);
|
||||
let line = element(&frame, INDICATOR).bounds;
|
||||
let row_h = gap_y - third;
|
||||
let boundary = gap_y - row_h * 0.5;
|
||||
assert!(
|
||||
(line.y + line.height * 0.5 - boundary).abs() <= 3.0 * unit,
|
||||
"{mode:?}: the indicator sits on the gap below row 2"
|
||||
);
|
||||
assert_eq!(
|
||||
session.written(),
|
||||
written,
|
||||
"{mode:?}: 0 writes before release"
|
||||
);
|
||||
session.release(px, target);
|
||||
session.settle();
|
||||
shot("row-after");
|
||||
let after = table(&session);
|
||||
assert_eq!(
|
||||
grid(&after),
|
||||
[
|
||||
"r1c0 r1c1 r1c2",
|
||||
"r2c0 r2c1 r2c2",
|
||||
"r0c0 r0c1 r0c2",
|
||||
"r3c0 r3c1 r3c2"
|
||||
],
|
||||
"{mode:?}"
|
||||
);
|
||||
assert_eq!(
|
||||
after.cells, before.cells,
|
||||
"{mode:?}: cells follow their rows"
|
||||
);
|
||||
session.undo();
|
||||
assert_eq!(table(&session), before, "{mode:?}: one undo step");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn column_pill_drag_moves_width_and_cells_and_cancels_without_writes() {
|
||||
for (mode, theme) in CASES {
|
||||
let session = open_table(mode, theme);
|
||||
let shot = |name: &str| {
|
||||
let file = format!("{name}-{mode:?}-{theme:?}.png").to_lowercase();
|
||||
save_evidence(&session, &file);
|
||||
};
|
||||
let before = table(&session);
|
||||
let (px, py) = pill(&session, 1, 0, "col_pill");
|
||||
shot("column-before");
|
||||
let written = session.written();
|
||||
|
||||
// Out past the threshold and back: the release keeps the column in place.
|
||||
session.press(px, py, 1);
|
||||
session.pointer(px + 30.0, py, true);
|
||||
session.pointer(px, py, true);
|
||||
assert!(!session.frame().elements.contains_key(INDICATOR));
|
||||
session.release(px, py);
|
||||
session.settle();
|
||||
assert_eq!(
|
||||
session.written(),
|
||||
written,
|
||||
"{mode:?}: in place writes nothing"
|
||||
);
|
||||
assert!(!session.frame().elements.contains_key(MENU));
|
||||
|
||||
// Escape in the middle of a drag drops it.
|
||||
session.press(px, py, 1);
|
||||
session.pointer(px + 200.0, py, true);
|
||||
assert!(session.frame().elements.contains_key(INDICATOR));
|
||||
session.key("escape");
|
||||
assert!(
|
||||
session.view(|v, _| v.active_table_cell().is_some_and(|c| c.axis_drag.is_none()))
|
||||
);
|
||||
assert!(!session.frame().elements.contains_key(PREVIEW));
|
||||
session.release(px + 200.0, py);
|
||||
session.settle();
|
||||
assert_eq!(
|
||||
session.written(),
|
||||
written,
|
||||
"{mode:?}: Escape writes nothing"
|
||||
);
|
||||
|
||||
// Past the center of the next column: column 0 moves after column 1. The pill
|
||||
// follows the hovered cell, so hover the first column again.
|
||||
let (px, py) = pill(&session, 1, 0, "col_pill");
|
||||
let ((x0, _), _) = cell_center(&session, 1, 0);
|
||||
let ((x1, _), _) = cell_center(&session, 1, 1);
|
||||
let target = px + (x1 - x0);
|
||||
session.press(px, py, 1);
|
||||
session.pointer(target, py, true);
|
||||
session.frame();
|
||||
shot("column-dragging");
|
||||
session.release(target, py);
|
||||
session.settle();
|
||||
shot("column-after");
|
||||
let after = table(&session);
|
||||
let ids: Vec<&str> = after.columns.iter().map(|c| c.column_id.as_str()).collect();
|
||||
assert_eq!(ids, ["col-1", "col-0", "col-2"], "{mode:?}");
|
||||
assert_eq!(
|
||||
after.columns[1].width,
|
||||
Some(200.0),
|
||||
"{mode:?}: the width follows"
|
||||
);
|
||||
assert_eq!(grid(&after)[0], "r0c1 r0c0 r0c2", "{mode:?}");
|
||||
session.undo();
|
||||
assert_eq!(table(&session), before, "{mode:?}: one undo step");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pill_menu_opens_only_on_a_release_over_the_pressed_pill() {
|
||||
for mode in [PlaygroundMode::Page, PlaygroundMode::Edgeless] {
|
||||
for (id, dx, dy, opens) in [
|
||||
("row_pill", 0.0, 0.0, true),
|
||||
("col_pill", 0.0, 0.0, true),
|
||||
("row_pill", 100.0, 0.0, false),
|
||||
("col_pill", 0.0, 100.0, false),
|
||||
] {
|
||||
let session = open_table(mode, PlaygroundTheme::Light);
|
||||
let (px, py) = pill(&session, 1, 1, id);
|
||||
let written = session.written();
|
||||
session.press(px, py, 1);
|
||||
session.pointer(px + dx, py + dy, true);
|
||||
session.release(px + dx, py + dy);
|
||||
let frame = session.settle();
|
||||
let case = format!("{mode:?} {id} moved by ({dx}, {dy})");
|
||||
assert_eq!(frame.elements.contains_key(MENU), opens, "{case}");
|
||||
assert_eq!(session.written(), written, "{case}: zero writes");
|
||||
assert!(
|
||||
session
|
||||
.view(|v, _| v.active_table_cell().is_some_and(|c| c.axis_drag.is_none())),
|
||||
"{case}: the release ends the press"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
//! In-place live `Y.Text` editing glue for `affine:table` cells on [`PlaygroundView`].
|
||||
|
||||
mod area;
|
||||
mod axis_drag;
|
||||
mod navigation;
|
||||
mod paint;
|
||||
mod pointer;
|
||||
|
||||
@@ -21,6 +21,9 @@ impl PlaygroundView {
|
||||
return false;
|
||||
};
|
||||
let key = e.keystroke.key.as_str();
|
||||
if key == "escape" && self.cancel_table_axis_drag(cx) {
|
||||
return true;
|
||||
}
|
||||
let m = e.keystroke.modifiers;
|
||||
let cmd = m.secondary(cx);
|
||||
if composing || (e.prefer_character_input && key != "escape") {
|
||||
|
||||
@@ -125,6 +125,7 @@ impl PlaygroundView {
|
||||
};
|
||||
let block_id = block.block_id.as_str();
|
||||
out.extend(self.table_axis_preview_in(block, frame, width, height, palette));
|
||||
out.extend(self.table_axis_drag_elements(block, frame, palette));
|
||||
if self.table_is_readonly() || block.body.table_cells.is_empty() {
|
||||
return out;
|
||||
}
|
||||
@@ -173,6 +174,10 @@ impl PlaygroundView {
|
||||
.css_var("--affine-v2-table-indicator-activated")
|
||||
.unwrap_or(palette.brand);
|
||||
|
||||
if self.table_axis_dragging(block_id) {
|
||||
return out;
|
||||
}
|
||||
|
||||
// Column resize dividers on the first row's right borders.
|
||||
let first_row = cells[0].row_id.clone();
|
||||
for hit in cells.iter().filter(|c| c.row_id == first_row) {
|
||||
@@ -357,10 +362,17 @@ impl PlaygroundView {
|
||||
.justify_center()
|
||||
.gap(px(2.5))
|
||||
.cursor_pointer()
|
||||
.on_mouse_down(MouseButton::Left, |_, _, cx| cx.stop_propagation())
|
||||
.on_mouse_down(
|
||||
MouseButton::Left,
|
||||
cx.listener(move |this, ev: &MouseDownEvent, win, cx| {
|
||||
cx.stop_propagation();
|
||||
let (b, r, c) = (bid.clone(), rid.clone(), cid.clone());
|
||||
let at = (f32::from(ev.position.x), f32::from(ev.position.y));
|
||||
this.begin_table_axis_drag(b, r, c, axis, at, win, cx);
|
||||
}),
|
||||
)
|
||||
.on_click(cx.listener(move |this, _, win, cx| {
|
||||
let (b, r, c) = (bid.clone(), rid.clone(), cid.clone());
|
||||
this.open_table_axis_menu_for(b, r, c, axis, win, cx);
|
||||
this.click_table_axis_pill(axis, win, cx);
|
||||
}))
|
||||
.snapshot(
|
||||
format_args!("overlay.table_hover.{snap_suffix}"),
|
||||
|
||||
@@ -434,8 +434,8 @@ impl PlaygroundView {
|
||||
.reveal_table_cell_x(&cell.block_id, cell_left, cell_right, caret_x, code);
|
||||
}
|
||||
|
||||
/// Extend the active table cell's text selection, update live column-resize preview, or
|
||||
/// update Simple Table hover add-bar drag preview (0 CRDT writes until release).
|
||||
/// Extend the active table cell's text selection, or update the live preview of an axis pill
|
||||
/// drag, a column resize, or a hover add-bar drag (0 CRDT writes until release).
|
||||
pub(crate) fn table_cell_mouse_move(
|
||||
&mut self,
|
||||
event: &MouseMoveEvent,
|
||||
@@ -443,6 +443,7 @@ impl PlaygroundView {
|
||||
cx: &mut Context<Self>,
|
||||
) {
|
||||
if event.pressed_button != Some(MouseButton::Left) {
|
||||
self.finish_table_axis_drag(window, cx);
|
||||
self.finish_table_add_drag(window, cx);
|
||||
self.finish_table_col_resize_drag(window, cx);
|
||||
if let Some(cell) = self.table_cell.as_mut() {
|
||||
@@ -450,6 +451,15 @@ impl PlaygroundView {
|
||||
}
|
||||
return;
|
||||
}
|
||||
if self
|
||||
.table_cell
|
||||
.as_ref()
|
||||
.is_some_and(|c| c.axis_drag.is_some())
|
||||
{
|
||||
let position = (f32::from(event.position.x), f32::from(event.position.y));
|
||||
self.table_axis_drag_move(position, cx);
|
||||
return;
|
||||
}
|
||||
// Drag distances are measured in flow units: an Active Note divides out the camera.
|
||||
let scale = self
|
||||
.active_note_table_target()
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
//! Simple Table (`affine:table`) desktop hover affordances (16 px add-column/add-row bars and
|
||||
//! corner drag-to-add, 28 × 16 / 16 × 28 three-dot axis pills, 5 px `ew-resize` column dividers,
|
||||
//! and axis menu triggers).
|
||||
//! corner drag-to-add, 28 × 16 / 16 × 28 three-dot axis pills that open the axis menu or drag
|
||||
//! their row or column, 5 px `ew-resize` column dividers, and axis menu triggers).
|
||||
|
||||
use ui::{
|
||||
AnyElement, CursorStyle, FontWeight, MouseButton, MouseDownEvent, SharedString, div,
|
||||
prelude::*, px,
|
||||
};
|
||||
|
||||
use super::TablePaintFrame;
|
||||
use crate::{
|
||||
PlaygroundView,
|
||||
block_flow::{FlowLayout, FlowRect, PageViewport},
|
||||
@@ -47,6 +48,7 @@ impl PlaygroundView {
|
||||
let hover = &self.overlays.table_hover;
|
||||
let pinned = focused_cell.filter(|c| c.axis_menu.is_some());
|
||||
let active_block_id = pinned
|
||||
.or_else(|| focused_cell.filter(|c| c.axis_drag.is_some()))
|
||||
.map(|c| c.block_id.as_str())
|
||||
.or_else(|| hover.add_drag.as_ref().map(|d| d.block_id.as_str()))
|
||||
.or(hover.block_id.as_deref())
|
||||
@@ -275,8 +277,12 @@ impl PlaygroundView {
|
||||
)
|
||||
};
|
||||
|
||||
let dragging_axis = self.table_axis_dragging(active_block_id);
|
||||
let active = hover.add_drag.as_ref().map(|d| d.kind).or_else(|| {
|
||||
if pinned.is_some() || hover.block_id.as_deref() != Some(active_block_id) {
|
||||
if dragging_axis
|
||||
|| pinned.is_some()
|
||||
|| hover.block_id.as_deref() != Some(active_block_id)
|
||||
{
|
||||
return None;
|
||||
}
|
||||
let (x, y) = hover.pointer?;
|
||||
@@ -405,7 +411,8 @@ impl PlaygroundView {
|
||||
let mut col_pill_anchor = FlowRect::new(table_screen_left, table_screen_top, 28.0, 16.0);
|
||||
let mut row_pill_anchor = FlowRect::new(table_screen_left, table_screen_top, 16.0, 28.0);
|
||||
|
||||
if let (Some(rid), Some(cid)) = (target_row_id, target_col_id)
|
||||
if !dragging_axis
|
||||
&& let (Some(rid), Some(cid)) = (target_row_id, target_col_id)
|
||||
&& let Some(cell_hit) = block
|
||||
.body
|
||||
.table_cells
|
||||
@@ -439,17 +446,23 @@ impl PlaygroundView {
|
||||
.justify_center()
|
||||
.gap(px(2.5))
|
||||
.cursor_pointer()
|
||||
.on_mouse_down(MouseButton::Left, |_, _, cx| cx.stop_propagation())
|
||||
.on_click(cx.listener(move |this, _, win, cx| {
|
||||
this.overlays.table_axis_scroll.set(0.0);
|
||||
this.open_table_axis_menu_for(
|
||||
bid_c.clone(),
|
||||
rid_c.clone(),
|
||||
cid_c.clone(),
|
||||
TableAxisMenu::Column,
|
||||
win,
|
||||
cx,
|
||||
);
|
||||
.on_mouse_down(
|
||||
MouseButton::Left,
|
||||
cx.listener(move |this, ev: &MouseDownEvent, win, cx| {
|
||||
cx.stop_propagation();
|
||||
this.begin_table_axis_drag(
|
||||
bid_c.clone(),
|
||||
rid_c.clone(),
|
||||
cid_c.clone(),
|
||||
TableAxisMenu::Column,
|
||||
(f32::from(ev.position.x), f32::from(ev.position.y)),
|
||||
win,
|
||||
cx,
|
||||
);
|
||||
}),
|
||||
)
|
||||
.on_click(cx.listener(|this, _, win, cx| {
|
||||
this.click_table_axis_pill(TableAxisMenu::Column, win, cx);
|
||||
}))
|
||||
.child(
|
||||
div()
|
||||
@@ -491,17 +504,23 @@ impl PlaygroundView {
|
||||
.items_center()
|
||||
.justify_center()
|
||||
.cursor_pointer()
|
||||
.on_mouse_down(MouseButton::Left, |_, _, cx| cx.stop_propagation())
|
||||
.on_click(cx.listener(move |this, _, win, cx| {
|
||||
this.overlays.table_axis_scroll.set(0.0);
|
||||
this.open_table_axis_menu_for(
|
||||
bid_r.clone(),
|
||||
rid_r.clone(),
|
||||
cid_r.clone(),
|
||||
TableAxisMenu::Row,
|
||||
win,
|
||||
cx,
|
||||
);
|
||||
.on_mouse_down(
|
||||
MouseButton::Left,
|
||||
cx.listener(move |this, ev: &MouseDownEvent, win, cx| {
|
||||
cx.stop_propagation();
|
||||
this.begin_table_axis_drag(
|
||||
bid_r.clone(),
|
||||
rid_r.clone(),
|
||||
cid_r.clone(),
|
||||
TableAxisMenu::Row,
|
||||
(f32::from(ev.position.x), f32::from(ev.position.y)),
|
||||
win,
|
||||
cx,
|
||||
);
|
||||
}),
|
||||
)
|
||||
.on_click(cx.listener(|this, _, win, cx| {
|
||||
this.click_table_axis_pill(TableAxisMenu::Row, win, cx);
|
||||
}))
|
||||
.child(
|
||||
div()
|
||||
@@ -533,6 +552,8 @@ impl PlaygroundView {
|
||||
cx,
|
||||
));
|
||||
}
|
||||
layer =
|
||||
layer.children(self.table_axis_drag_elements(block, TablePaintFrame::Page(v), palette));
|
||||
Some(layer.into_any_element())
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user