Files
AFFiNE/packages/frontend/client/tests/seeded_linked_doc_persistence.rs
T
DarkSky 438c5cff16 feat(editor,hosts): add create-linked-doc conversion with subdoc persistence
Page FormatToolbar `Create Linked Doc` opens the `Create linked doc` title
dialog. The dialog shows the first block as the title when that block is a
heading. `Confirm` moves the whole blocks that the selection touches, with
their subtrees and all their properties, into a new linked page. Code
text, table and database cells, marks, and inline references stay intact.
It puts an `affine:embed-linked-doc` card in their place. A blank title
gives `Untitled`. `Cancel`, `Escape`, or a press outside the dialog write
nothing.

The Edgeless `More` menu adds `Turn into linked doc` for one unlocked
note. This puts an `affine:embed-synced-doc` card with the frame, index,
and connectors of the note in its place. The menu also adds `Create linked
doc` for other unlocked selections. This moves the items, selected notes
included, into a new Edgeless document in their paint order. Then it puts
a 364 x 390 `vertical` card at their center. Both actions use the same
title dialog. Each conversion is one undo step in the source document. The
open-document group of the menu comes before the clipboard group. A menu
that does not fit scrolls its rows with the wheel and does not cover the
toolbar or the creation toolbar. `PageDocSession::export_block_tree`
exports a block subtree with every property for these copies and for
Edgeless copy and clone.

`WorkspaceDocDirectory::seed_created_doc` gives the complete Yjs V1 state
of a new document to the host, so that the content persists. The desktop
host replaces the initial blocks of the store with the seed blocks through
`PageDocSession::replace_blocks_update_v1`. It queues the change for the
connected session. The desktop directory keeps the snapshot of each loaded
document, and of a seeded document that the store does not have yet, so
synced cards show the content after a refresh or a reopen. The client
directory queues the seed with the pending updates of the workspace
database (`enqueue_pending_update`). The mobile host keeps the seed as the
cached CRDT state. The mobile bridge adds a document that the shell
created to the workspace root before it merges the content. New mobile
document IDs come from the intent, or from a random value, for each
workspace. They never reuse the ID of a document in the trash.
2026-12-05 01:01:17 +09:00

104 lines
3.0 KiB
Rust

//! A linked document that `StoreBackedDocDirectory::seed_created_doc` seeds is listed in the
//! workspace and keeps its blocks after the session flushes to SQLite and the store reopens.
#![cfg(not(target_family = "wasm"))]
use affine_client::{
EditorDocMode, HostKind, SqliteWorkspaceStore, StoreBackedDocDirectory, WorkspaceDocDirectory,
};
use affine_store::directory::DocId;
use blocksuite_playground_doc::{
schema::{BlockSpec, ParagraphType},
session::PageDocSession,
};
const WORKSPACE: &str = "ws-seeded-linked-doc";
fn seed(doc_id: &str) -> Vec<u8> {
let mut page = PageDocSession::new(doc_id, 9).unwrap();
page.add_block(
"seed-root",
BlockSpec::Page {
title: "Moved".into(),
},
None,
None,
1,
)
.unwrap();
page.add_block(
"seed-note",
BlockSpec::Note { xywh: None },
Some("seed-root"),
None,
2,
)
.unwrap();
page.add_block(
"seed-p",
BlockSpec::Paragraph {
paragraph_type: ParagraphType::Text,
text: "Moved body".into(),
},
Some("seed-note"),
None,
3,
)
.unwrap();
page.encode_update_v1().unwrap()
}
#[tokio::test]
async fn seeded_linked_doc_is_listed_and_keeps_its_blocks_after_sqlite_reopen() {
let dir = std::env::temp_dir().join(format!("affine-client-seeded-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
let store = SqliteWorkspaceStore::open(&dir, "workspace", "local", WORKSPACE)
.await
.unwrap();
let mut session = store
.load_workspace_session("user", 1, HostKind::Native, "", "", 1_000)
.await
.unwrap();
let mut directory =
StoreBackedDocDirectory::from_shared(session.directory().shared_directory(), 1_001);
let doc = directory
.create_linked_doc("Moved", EditorDocMode::Page, None)
.unwrap();
directory
.seed_created_doc(&doc.id, seed(doc.id.as_str()))
.unwrap();
store
.flush_session_mutations(&mut session, None, 1_002)
.await
.unwrap();
drop(session);
store.close().await;
let store = SqliteWorkspaceStore::open(&dir, "workspace", "local", WORKSPACE)
.await
.unwrap();
let mut reopened = store
.load_workspace_session("user", 2, HostKind::Native, "", "", 1_003)
.await
.unwrap();
let doc_id = DocId::new(doc.id.as_str()).unwrap();
let listed = reopened
.directory()
.lock_directory()
.get_doc(&doc_id)
.unwrap();
assert_eq!(listed.map(|entry| entry.title), Some("Moved".to_owned()));
store
.open_or_hydrate_document(&mut reopened, doc.id.as_str(), 1_004)
.await
.unwrap();
let snapshot = reopened.doc_snapshot(doc.id.as_str()).unwrap();
assert_eq!(
snapshot.root.as_ref().map(|root| root.id.as_str()),
Some("seed-root")
);
assert!(snapshot.block("seed-p").is_some());
store.close().await;
let _ = std::fs::remove_dir_all(&dir);
}