T21a round 1 fixes

- Enter over a selection drops it and splits at its start, one undo step
- undo and redo keep where the caret was and put it back
- a format unwraps only its own markers, so italic inside bold keeps the bold
- keys into a merged table cell land on the cell that shows
- undo rebuilds a block whose kind needs other fields, so styling comes back
- tests activate their window, as focus events need
- undo bookkeeping moves to blocks/edit.rs
This commit is contained in:
ZacharyZhang-NY committed 2026-09-26 03:49:27 -04:00
1 parent 6a9ce8a2c3
commit f3e2c6c27f
10 files changed
+330 -81

No files matched your search

+82 -10
View File
@@ -1,6 +1,6 @@
use std::collections::HashMap;
use gpui::{Context, Window};
use gpui::{App, Context, Focusable, Window};
use super::{Block, BlockData, BlockEditor, BlockEvent, BlockKind, Snapshot};
@@ -17,13 +17,13 @@ impl BlockEditor {
self.turn_into(key, BlockKind::Paragraph, window, cx);
return;
}
let caret = field.read(cx).cursor();
let tail = field.read(cx).text()[caret..].to_string();
log::info!("block editor: block {key} splits at {caret}");
let selection = field.read(cx).selection();
let tail = field.read(cx).text()[selection.end..].to_string();
log::info!("block editor: block {key} splits over {selection:?}");
self.before_change(cx);
field.update(cx, |field, cx| {
let end = field.text().len();
field.select(caret..end, cx);
field.select(selection.start..end, cx);
field.insert("", cx);
});
let key = self.fresh_key();
@@ -73,29 +73,39 @@ impl BlockEditor {
pub(crate) fn undo(&mut self, window: &mut Window, cx: &mut Context<Self>) {
self.settle(true, cx);
if let Some(previous) = self.history.undo(self.current.clone()) {
if let Some(previous) = self.history.undo(self.now(cx)) {
log::info!("block editor: undo");
self.restore(previous, window, cx);
}
}
pub(crate) fn redo(&mut self, window: &mut Window, cx: &mut Context<Self>) {
if let Some(next) = self.history.redo(self.current.clone()) {
if let Some(next) = self.history.redo(self.now(cx)) {
log::info!("block editor: redo");
self.restore(next, window, cx);
}
}
/// Puts the document back as `snapshot` holds it, keeping fields whose blocks stay.
/// The current document with the caret where it is now.
fn now(&self, cx: &Context<Self>) -> Snapshot {
let mut now = self.current.clone();
now.caret = self.snapshot(cx).caret;
now
}
/// Puts the document back as `snapshot` holds it, keeping fields whose blocks stay, and the caret where it was.
fn restore(&mut self, snapshot: Snapshot, window: &mut Window, cx: &mut Context<Self>) {
let mut kept: HashMap<u64, Block> = self
.blocks
.drain(..)
.map(|block| (block.key, block))
.collect();
for (key, data) in &snapshot.0 {
for (key, data) in &snapshot.blocks {
let block = match kept.remove(key) {
Some(mut block) if block.fields.len() == data.texts.len() => {
Some(mut block)
if block.fields.len() == data.texts.len()
&& block.kind.same_fields(&data.kind) =>
{
block.kind = data.kind.clone();
for (field, text) in block.fields.iter().zip(&data.texts) {
if field.read(cx).text() != text {
@@ -108,8 +118,70 @@ impl BlockEditor {
};
self.blocks.push(block);
}
if let Some((key, field, selection)) = snapshot.caret.clone()
&& let Some(block) = self.blocks.iter().find(|block| block.key == key)
&& let Some(field) = block.fields.get(field)
{
field.update(cx, |field, cx| field.select(selection, cx));
window.focus(&field.focus_handle(cx));
}
self.current = snapshot;
cx.emit(BlockEvent::Changed);
cx.notify();
}
pub(super) fn snapshot(&self, cx: &App) -> Snapshot {
let caret = self.caret.and_then(|(key, field)| {
let block = self.blocks.iter().find(|block| block.key == key)?;
let selection = block.fields.get(field)?.read(cx).selection();
Some((key, field, selection))
});
Snapshot {
caret,
blocks: self
.blocks
.iter()
.map(|block| {
let texts = block
.fields
.iter()
.map(|field| field.read(cx).text().to_string())
.collect();
(
block.key,
BlockData {
kind: block.kind.clone(),
texts,
},
)
})
.collect(),
}
}
/// Records what changed as one undo step, a burst of typing as one, and tells the owner.
pub(super) fn settle(&mut self, typing: bool, cx: &mut Context<Self>) {
let now = self.snapshot(cx);
if now.blocks != self.current.blocks {
let before = std::mem::replace(&mut self.current, now);
self.history.record(before, typing);
cx.emit(BlockEvent::Changed);
cx.notify();
}
}
/// Records the document before a change to its blocks.
pub(crate) fn before_change(&mut self, cx: &mut Context<Self>) {
self.settle(true, cx);
let mut before = self.current.clone();
before.caret = self.snapshot(cx).caret;
self.history.record(before, false);
}
/// After a change to the blocks: the new state is current.
pub(crate) fn after_change(&mut self, cx: &mut Context<Self>) {
self.current = self.snapshot(cx);
cx.emit(BlockEvent::Changed);
cx.notify();
}
}
+8 -6
View File
@@ -1,6 +1,6 @@
use gpui::{Context, Window};
use super::{BlockData, BlockEditor, BlockKind};
use super::{BlockData, BlockEditor, BlockKind, table::shown_cell};
use crate::documents::modes::at_edge;
/// A new empty paragraph.
@@ -43,12 +43,13 @@ impl BlockEditor {
BlockKind::Image(_) | BlockKind::Video(_) | BlockKind::Embed(_) => {
self.insert(Some(key), paragraph(), window, cx);
}
BlockKind::Table { columns, .. } => {
BlockKind::Table { columns, merged } => {
let below = field + columns;
if below >= self.blocks[self.index(key)].fields.len() {
self.add_row(key, window, cx);
}
self.focus_field(key, below, None, window, cx);
let shown = shown_cell(&merged, columns, below);
self.focus_field(key, shown, None, window, cx);
}
_ => return false,
}
@@ -95,14 +96,15 @@ impl BlockEditor {
if !at_edge(block.fields[field].read(cx), down) {
return false;
}
if let BlockKind::Table { columns, .. } = block.kind {
if let BlockKind::Table { columns, merged } = &block.kind {
let row = if down {
Some(field + columns)
} else {
field.checked_sub(columns)
field.checked_sub(*columns)
};
if let Some(row) = row.filter(|row| *row < block.fields.len()) {
self.focus_field(key, row, None, window, cx);
let shown = shown_cell(merged, *columns, row);
self.focus_field(key, shown, None, window, cx);
return true;
}
}
+10
View File
@@ -150,6 +150,16 @@ impl BlockKind {
}
}
/// Whether a field made for this kind serves `other` unchanged: the same highlighter and hint.
pub(crate) fn same_fields(&self, other: &Self) -> bool {
std::mem::discriminant(self) == std::mem::discriminant(other)
&& match (self, other) {
(Self::Heading(one), Self::Heading(two)) => one == two,
(Self::Synced(one), Self::Synced(two)) => one == two,
_ => true,
}
}
/// Prose: Enter splits it, Backspace at its start turns it back into text or joins it up.
pub fn is_prose(&self) -> bool {
matches!(
+25 -55
View File
@@ -61,9 +61,12 @@ pub(crate) struct Block {
_changes: Vec<Subscription>,
}
/// The whole document, as undo keeps it.
#[derive(Clone, Debug, PartialEq)]
struct Snapshot(Vec<(u64, BlockData)>);
/// The whole document as undo keeps it, and where the caret was: block, field and selection.
#[derive(Clone, Debug)]
struct Snapshot {
blocks: Vec<(u64, BlockData)>,
caret: Option<(u64, usize, std::ops::Range<usize>)>,
}
/// A document of blocks, Notion-like: prose, lists and to-dos, quotes and callouts, toggles, code, math, diagrams, dividers, tables, media, columns and synced blocks. Markdown at a paragraph's start changes its kind; `/` offers every kind; `@` and `:` suggest people and emoji; blocks drag by their handle. Undo covers text and structure alike.
pub struct BlockEditor {
@@ -71,6 +74,8 @@ pub struct BlockEditor {
next: u64,
history: History<Snapshot>,
current: Snapshot,
/// The field that last held focus, as block and field.
caret: Option<(u64, usize)>,
pub(crate) people: Vec<SharedString>,
pub(crate) diagram: Option<RenderDiagram>,
_synced: Subscription,
@@ -85,7 +90,11 @@ impl BlockEditor {
blocks: Vec::new(),
next: 0,
history: History::default(),
current: Snapshot(Vec::new()),
current: Snapshot {
blocks: Vec::new(),
caret: None,
},
caret: None,
people: Vec::new(),
diagram: None,
_synced: synced,
@@ -114,34 +123,12 @@ impl BlockEditor {
/// The document as it stands.
pub fn blocks(&self, cx: &App) -> Vec<BlockData> {
self.snapshot(cx)
.0
.blocks
.into_iter()
.map(|(_, data)| data)
.collect()
}
fn snapshot(&self, cx: &App) -> Snapshot {
Snapshot(
self.blocks
.iter()
.map(|block| {
let texts = block
.fields
.iter()
.map(|field| field.read(cx).text().to_string())
.collect();
(
block.key,
BlockData {
kind: block.kind.clone(),
texts,
},
)
})
.collect(),
)
}
/// A field for `kind`: inline markdown styled as typed, or code colored as code.
fn field(
kind: &BlockKind,
@@ -300,9 +287,16 @@ impl BlockEditor {
}
self.settle(true, cx);
}
InputEvent::Focus if field.read(cx).is_empty() => {
let hint = self.blocks[ix].kind.placeholder();
field.update(cx, |field, cx| field.set_placeholder(hint, cx));
InputEvent::Focus => {
self.caret = self.blocks[ix]
.fields
.iter()
.position(|each| each == field)
.map(|at| (key, at));
if field.read(cx).is_empty() {
let hint = self.blocks[ix].kind.placeholder();
field.update(cx, |field, cx| field.set_placeholder(hint, cx));
}
}
InputEvent::Blur if self.blocks[ix].kind == BlockKind::Paragraph => {
field.update(cx, |field, cx| field.set_placeholder("", cx));
@@ -332,30 +326,6 @@ impl BlockEditor {
}
}
/// Records what changed as one undo step, a burst of typing as one, and tells the owner.
fn settle(&mut self, typing: bool, cx: &mut Context<Self>) {
let now = self.snapshot(cx);
if now != self.current {
let before = std::mem::replace(&mut self.current, now);
self.history.record(before, typing);
cx.emit(BlockEvent::Changed);
cx.notify();
}
}
/// Records the document before a change to its blocks.
pub(crate) fn before_change(&mut self, cx: &mut Context<Self>) {
self.settle(true, cx);
self.history.record(self.current.clone(), false);
}
/// After a change to the blocks: the new state is current.
pub(crate) fn after_change(&mut self, cx: &mut Context<Self>) {
self.current = self.snapshot(cx);
cx.emit(BlockEvent::Changed);
cx.notify();
}
pub(crate) fn focus_field(
&self,
key: u64,
@@ -453,7 +423,7 @@ impl BlockEditor {
pub fn duplicate(&mut self, key: u64, window: &mut Window, cx: &mut Context<Self>) {
let ix = self.index(key);
let data = self.snapshot(cx).0[ix].1.clone();
let data = self.snapshot(cx).blocks[ix].1.clone();
self.insert(Some(key), data, window, cx);
}
+9
View File
@@ -25,6 +25,15 @@ pub(crate) fn span(merged: &[(usize, usize)], columns: usize, row: usize, column
.count()
}
/// The cell that shows for `cell`: itself, or the one a merge folds it into.
pub(crate) fn shown_cell(merged: &[(usize, usize)], columns: usize, cell: usize) -> usize {
let (row, mut column) = (cell / columns, cell % columns);
while column > 0 && merged.contains(&(row, column)) {
column -= 1;
}
row * columns + column
}
/// Merges after `column` is removed: its own go, the ones right of it move left.
pub(crate) fn without_column(merged: &[(usize, usize)], column: usize) -> Vec<(usize, usize)> {
merged