Merge pull request #2962 from storytold/fix/2119-workspace-recovered-files

M9: web: a workspace publishes every recovered file, not only the one in front (#2119, part)
This commit is contained in:
BFlat
2026-12-03 09:54:37 +07:00
committed by GitHub
2 changed files with 42 additions and 25 deletions
+9 -2
View File
@@ -276,9 +276,16 @@ impl eframe::App for WebShell {
}
figcraft_ui_egui::windows::guard(&mut self.fig, ctx, |fig| fig.background_tick(ctx));
if let Some(workspace) = &mut self.workspace {
let front = self.fig.session.ds().ok().map(|d| d.uid);
if let Some(params) = workspace.step(front, self.fig.session.live.iter()) {
let s = &self.fig.session;
// `collab.publish` publishes the file in front: bring the chosen one there for it (a
// recovered file may be behind), then put back the one you had.
if let Some((uid, params)) = workspace.step(s.docs.iter().map(|d| d.uid), s.live.iter())
&& let Some(index) = s.docs.iter().position(|d| d.uid == uid)
{
let front = s.active;
self.fig.run("doc.switch", serde_json::json!({ "index": index }));
self.fig.run("collab.publish", params);
self.fig.run("doc.switch", serde_json::json!({ "index": front }));
}
}
// The host hears when unsaved changes appear or go away (crate::host).
+33 -23
View File
@@ -1,8 +1,8 @@
//! Workspace mode: a page served beside its own `figcraft-server` that says
//! `<meta name="figcraft-workspace" content="on">` keeps every file on that server. Each document
//! that comes to the front without being on a server already (a new design, a file opened from the
//! computer, one kept from a stopped page) is published there once, as File ▸ Publish to server…
//! does, so it joins the team list and every edit is kept as it happens. Testable without a
//! `<meta name="figcraft-workspace" content="on">` keeps every file on that server. Each open
//! document that isn't on a server already (a new design, a file opened from the computer, each one
//! kept from a stopped page, in front or not) is published there once, as File ▸ Publish to
//! server… does, so it joins the team list and every edit is kept as it happens. Testable without a
//! browser (like [`crate::host_publish`]).
use std::collections::BTreeSet;
@@ -13,8 +13,8 @@ use serde_json::{Value, json};
/// The page's server and the documents already decided on.
pub struct Workspace {
server: String,
/// Every document seen in front: each is published at most once, so a failed or refused
/// publish isn't retried and a file you leave isn't put back.
/// Every document decided on: each is published at most once, so a failed or refused publish
/// isn't retried and a file you leave isn't put back.
seen: BTreeSet<u64>,
}
@@ -23,15 +23,12 @@ impl Workspace {
Workspace { server, seen: BTreeSet::new() }
}
/// After a frame: the `collab.publish` parameters for the document in front (`front`, its
/// `DocState::uid`), when it is new to the workspace and no session syncs it (`sessions`).
pub fn step<'a>(&mut self, front: Option<u64>, sessions: impl IntoIterator<Item = &'a Live>) -> Option<Value> {
let uid = front?;
if !self.seen.insert(uid) {
return None;
}
let on_server = sessions.into_iter().any(|l| l.doc_uid == Some(uid));
(!on_server).then(|| json!({ "server": self.server }))
/// After a frame: the next open document (`docs`, their `DocState::uid`s) new to the workspace
/// that no session syncs (`sessions`), and the `collab.publish` parameters for it. One a frame.
pub fn step<'a>(&mut self, docs: impl IntoIterator<Item = u64>, sessions: impl IntoIterator<Item = &'a Live>) -> Option<(u64, Value)> {
let synced: BTreeSet<u64> = sessions.into_iter().filter_map(|l| l.doc_uid).collect();
let uid = docs.into_iter().find(|&uid| self.seen.insert(uid) && !synced.contains(&uid))?;
Some((uid, json!({ "server": self.server })))
}
}
@@ -50,22 +47,35 @@ mod tests {
l
}
fn published(uid: u64) -> Option<(u64, serde_json::Value)> {
Some((uid, json!({"server": SERVER})))
}
#[test]
fn a_new_document_is_published_once() {
let mut w = Workspace::new(SERVER.into());
assert_eq!(w.step(Some(1), &[]), Some(json!({"server": SERVER})));
assert_eq!(w.step(Some(1), &[]), None, "the publish is under way, or failed: not again");
assert_eq!(w.step(None, &[]), None, "the home screen");
assert!(w.step(Some(2), &[]).is_some(), "the next new design");
assert_eq!(w.step(Some(1), &[]), None, "back to the first");
assert_eq!(w.step([1], &[]), published(1));
assert_eq!(w.step([1], &[]), None, "the publish is under way, or failed: not again");
assert_eq!(w.step([], &[]), None, "the home screen");
assert_eq!(w.step([1, 2], &[]), published(2), "the next new design");
assert_eq!(w.step([2, 1], &[]), None, "back to the first");
}
#[test]
fn every_recovered_document_is_published_not_only_the_one_in_front() {
let mut w = Workspace::new(SERVER.into());
let open = [5, 6, 7];
let mut done: Vec<u64> = std::iter::from_fn(|| w.step(open, &[]).map(|(uid, _)| uid)).collect();
done.sort();
assert_eq!(done, open);
}
#[test]
fn a_file_on_a_server_already_is_left_alone() {
let mut w = Workspace::new(SERVER.into());
let joined = syncing(3);
assert_eq!(w.step(Some(3), [&joined]), None, "opened from the team list");
assert_eq!(w.step(Some(3), &[]), None, "and after leaving its session it isn't published as a copy");
assert!(w.step(Some(4), [&joined]).is_some(), "another session's file doesn't count");
assert_eq!(w.step([3], [&joined]), None, "opened from the team list");
assert_eq!(w.step([3], &[]), None, "and after leaving its session it isn't published as a copy");
assert_eq!(w.step([3, 4], [&joined]), published(4), "another session's file doesn't count");
}
}