mirror of
https://github.com/storytold/wordcraft.git
synced 2026-12-03 22:40:27 +00:00
Merge pull request #4207
This commit is contained in:
@@ -12675,10 +12675,12 @@ fn outline_level_changes_are_undoable() {
|
||||
assert!(s.run("outline.level", &json!({"level": 12})).is_err());
|
||||
assert!(s.run("outline.level", &json!({})).is_err());
|
||||
// Every outline command survives junk parameters in Outline view.
|
||||
for spec in cmd::registry().all().iter().filter(|c| c.id.starts_with("outline.")) {
|
||||
let reg = cmd::registry();
|
||||
for spec in reg.all().iter().filter(|c| c.id.starts_with("outline.")) {
|
||||
for j in [json!(null), json!({"block": 99999, "level": "x", "value": 5}), json!({"block": -1, "level": 0})] {
|
||||
let mut s = outline_doc();
|
||||
let _ = s.run(spec.id, &j);
|
||||
let r = s.run(spec.id, &j);
|
||||
assert!(!caught_panic(®, &r), "{} panicked on {j}: {r:?}", spec.id);
|
||||
let _ = s.layout();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -133,6 +133,7 @@ impl WordApp {
|
||||
user_origin: false,
|
||||
read_aloud_error: None,
|
||||
autosave_path: None,
|
||||
read_from: None,
|
||||
saves_in_flight: Vec::new(),
|
||||
fallback_chooser: false,
|
||||
font_generation: wordcraft_fonts::FontDb::global().generation(),
|
||||
|
||||
@@ -176,7 +176,11 @@ impl WordApp {
|
||||
return Err(e.into());
|
||||
}
|
||||
if id == "file.autosave" {
|
||||
return self.set_autosave(¶ms);
|
||||
let r = self.set_autosave(¶ms);
|
||||
if let Err(e) = &r {
|
||||
self.status(e.clone());
|
||||
}
|
||||
return r;
|
||||
}
|
||||
if let Some(r) = table_pen::toggle(self, id, ¶ms) {
|
||||
return r;
|
||||
@@ -326,13 +330,8 @@ impl WordApp {
|
||||
self.recovery_seen = true;
|
||||
self.sync_document();
|
||||
}
|
||||
// An explicit save to the document's own file lets AutoSave keep writing there.
|
||||
if let Ok(v) = &r
|
||||
&& matches!(id, "file.save" | "file.saveAs")
|
||||
&& v.get("saved").and_then(Value::as_bool) == Some(true)
|
||||
&& v.get("path").and_then(Value::as_str).map(std::path::Path::new) == self.session.path.as_deref()
|
||||
{
|
||||
self.autosave_path = self.session.path.clone();
|
||||
if let Ok(v) = &r {
|
||||
self.note_file_on_disk(id, ¶ms, v);
|
||||
}
|
||||
// A Markdown file edited in the formatted view was written out again, not kept as it was.
|
||||
if let Ok(v) = &r
|
||||
@@ -349,6 +348,9 @@ impl WordApp {
|
||||
if self.autosave_path.as_ref() == Some(&old) {
|
||||
self.autosave_path = self.session.path.clone();
|
||||
}
|
||||
if self.read_from.as_ref() == Some(&old) {
|
||||
self.read_from = self.session.path.clone();
|
||||
}
|
||||
let old = old.to_string_lossy();
|
||||
self.ui.recent.retain(|r| *r != old);
|
||||
}
|
||||
@@ -434,6 +436,25 @@ impl WordApp {
|
||||
r
|
||||
}
|
||||
|
||||
/// Which file on disk the document is in, after `file.save`, `file.saveAs` or `file.open`.
|
||||
fn note_file_on_disk(&mut self, id: &str, params: &Value, v: &Value) {
|
||||
// An explicit save to the document's own file lets AutoSave keep writing there.
|
||||
if matches!(id, "file.save" | "file.saveAs")
|
||||
&& v.get("saved").and_then(Value::as_bool) == Some(true)
|
||||
&& v.get("path").and_then(Value::as_str).map(std::path::Path::new) == self.session.path.as_deref()
|
||||
{
|
||||
self.autosave_path = self.session.path.clone();
|
||||
self.read_from = self.session.path.clone();
|
||||
}
|
||||
// A file read from disk may have AutoSave turned on for it; one opened from bytes only
|
||||
// names a file it never read.
|
||||
// `file.open` without a path only asks for the picker, and `data` that isn't a string is
|
||||
// ignored (the file is read from disk).
|
||||
if id == "file.open" && params.get("path").and_then(Value::as_str).is_some() && v.get("alreadyOpen").and_then(Value::as_bool) == Some(false) {
|
||||
self.read_from = if params.get("data").and_then(Value::as_str).is_some() { None } else { self.session.path.clone() };
|
||||
}
|
||||
}
|
||||
|
||||
fn after_command(&mut self, id: &str) {
|
||||
self.canvas.glide.restart(craft::platform::clock::monotonic_ms());
|
||||
if id == "view.outline" {
|
||||
|
||||
@@ -684,13 +684,31 @@ impl WordApp {
|
||||
self.autosave && self.autosave_block().is_none()
|
||||
}
|
||||
|
||||
/// What the AutoSave switch can't lift: [`Self::autosave_block`], less a file read from disk
|
||||
/// here, which the switch turns on (#3307). A document opened from bytes stays blocked.
|
||||
pub fn autosave_switch_block(&self) -> Option<AutoSaveBlock> {
|
||||
self.autosave_block().filter(|b| *b != AutoSaveBlock::NotSavedHere || self.read_from != self.session.path)
|
||||
}
|
||||
|
||||
/// `file.autosave` (the AutoSave switch, and scripts): `value` turns AutoSave on or off;
|
||||
/// without it, it toggles what the switch shows. Turning it on for a document AutoSave can't
|
||||
/// cover is an error that says why, instead of a switch that is on but saves nothing (#196).
|
||||
/// Turning it on for a file only opened here is the go-ahead a save gives (#3307), when the
|
||||
/// document was read from that file; turning it off withdraws the go-ahead for this document.
|
||||
pub(super) fn set_autosave(&mut self, params: &Value) -> Result<Value, String> {
|
||||
let on = params.get("value").and_then(Value::as_bool).unwrap_or(!self.autosaves());
|
||||
if on && let Some(block) = self.autosave_block() {
|
||||
return Err(block.reason());
|
||||
let on = match params.get("value") {
|
||||
None => !self.autosaves(),
|
||||
Some(Value::Bool(on)) => *on,
|
||||
Some(_) => return Err("`value` must be true or false".into()),
|
||||
};
|
||||
if on && self.autosave_block().is_some() {
|
||||
if let Some(block) = self.autosave_switch_block() {
|
||||
return Err(block.reason());
|
||||
}
|
||||
self.autosave_path = self.session.path.clone();
|
||||
}
|
||||
if !on {
|
||||
self.autosave_path = None;
|
||||
}
|
||||
self.autosave = on;
|
||||
self.session.autosave = on;
|
||||
|
||||
@@ -76,16 +76,43 @@ fn new_app() -> WordApp {
|
||||
WordApp::new(wordcraft_engine::Session::new(wordcraft_doc::Document::new()), crate::Services::default())
|
||||
}
|
||||
|
||||
/// A saved .docx (so AutoSave can cover it) in the harness.
|
||||
fn autosave_harness() -> (egui_kittest::Harness<'static, WordApp>, std::path::PathBuf) {
|
||||
let dir = std::env::temp_dir().join(format!("wordcraft-ui-autosave-label-{}", std::process::id()));
|
||||
/// How [`autosave_harness`] has the saved file in its window.
|
||||
#[derive(Debug, PartialEq)]
|
||||
enum Reopen {
|
||||
/// The window that saved it.
|
||||
No,
|
||||
/// Opened in a fresh window, read from disk.
|
||||
FromDisk,
|
||||
/// Opened in a fresh window from its bytes, which only names the file.
|
||||
FromBytes,
|
||||
}
|
||||
|
||||
/// A saved .docx (so AutoSave can cover it), or the same file opened in a fresh window as
|
||||
/// `reopen` says, with the title bar drawn over it. `files` answers a later Save As pick.
|
||||
fn autosave_harness(
|
||||
reopen: Reopen,
|
||||
files: &std::sync::Arc<craft::host::ScriptedFiles>,
|
||||
) -> (egui_kittest::Harness<'static, WordApp>, std::path::PathBuf) {
|
||||
let dir = std::env::temp_dir().join(format!("wordcraft-ui-autosave-label-{}-{reopen:?}", std::process::id()));
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let path = dir.join("draft.docx");
|
||||
let mut a = new_app();
|
||||
let mut a = WordApp::new(wordcraft_engine::Session::new(wordcraft_doc::Document::new()), crate::Services::default())
|
||||
.with_host(std::sync::Arc::new(craft::host::Host::builder(crate::APP).files(files.clone()).build()));
|
||||
a.run("text.insert", json!({"text": "Hello"})).unwrap();
|
||||
a.run("file.save", json!({"path": path.to_string_lossy()})).unwrap();
|
||||
assert!(a.autosaves(), "a saved .docx is covered by AutoSave");
|
||||
if reopen != Reopen::No {
|
||||
a = WordApp::new(wordcraft_engine::Session::new(wordcraft_doc::Document::new()), crate::Services::default())
|
||||
.with_host(std::sync::Arc::new(craft::host::Host::builder(crate::APP).files(files.clone()).build()));
|
||||
if reopen == Reopen::FromBytes {
|
||||
let data = wordcraft_engine::cmd::insert::base64_encode(&std::fs::read(&path).unwrap());
|
||||
a.execute("file.open", json!({"path": path.to_string_lossy(), "data": data})).unwrap();
|
||||
} else {
|
||||
a.run("file.open", json!({"path": path.to_string_lossy()})).unwrap();
|
||||
}
|
||||
assert!(!a.autosaves(), "an opened file waits for the user's go-ahead");
|
||||
}
|
||||
let h = harness(a);
|
||||
(h, dir)
|
||||
}
|
||||
@@ -93,7 +120,8 @@ fn autosave_harness() -> (egui_kittest::Harness<'static, WordApp>, std::path::Pa
|
||||
/// #3370: clicking the AutoSave switch label toggles AutoSave on and off.
|
||||
#[test]
|
||||
fn clicking_the_autosave_label_toggles_autosave() {
|
||||
let (mut h, dir) = autosave_harness();
|
||||
let files = std::sync::Arc::new(craft::host::ScriptedFiles::new());
|
||||
let (mut h, dir) = autosave_harness(Reopen::No, &files);
|
||||
assert!(h.state().autosaves());
|
||||
h.get_by_label("AutoSave").click();
|
||||
crate::test_harness::frames(&mut h, 2);
|
||||
@@ -104,6 +132,35 @@ fn clicking_the_autosave_label_toggles_autosave() {
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
/// #3307: a file opened, not saved, in this window had the switch greyed out, and clicking it
|
||||
/// asked for Save As. Clicking it now turns AutoSave on for that file.
|
||||
#[test]
|
||||
fn clicking_autosave_on_an_opened_file_turns_it_on() {
|
||||
let files = std::sync::Arc::new(craft::host::ScriptedFiles::new());
|
||||
let (mut h, dir) = autosave_harness(Reopen::FromDisk, &files);
|
||||
h.get_by_label("AutoSave").click();
|
||||
crate::test_harness::frames(&mut h, 2);
|
||||
assert!(h.state().autosaves(), "the switch turned AutoSave on");
|
||||
assert!(h.state().dialog_host.is_empty() && h.state().file_dialog.is_none(), "without Save As");
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
/// A document opened from bytes only names its file, so the switch can't hand that file to
|
||||
/// AutoSave: it shows the block and clicking it asks for Save As, after which AutoSave is on.
|
||||
#[test]
|
||||
fn clicking_autosave_on_a_document_opened_from_bytes_asks_for_save_as() {
|
||||
let files = std::sync::Arc::new(craft::host::ScriptedFiles::new());
|
||||
let (mut h, dir) = autosave_harness(Reopen::FromBytes, &files);
|
||||
assert_eq!(h.state().autosave_switch_block(), Some(crate::AutoSaveBlock::NotSavedHere));
|
||||
let copy = dir.join("copy.docx");
|
||||
files.queue(craft::host::Picked::Paths(vec![copy.clone()]));
|
||||
h.get_by_label("AutoSave").click();
|
||||
crate::test_harness::frames(&mut h, 2);
|
||||
assert!(copy.is_file(), "Save As saved a copy");
|
||||
assert!(h.state().autosaves(), "AutoSave covers the copy");
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
/// #701: the arrow beside Undo lists the session's undo steps, newest first; clicking the third
|
||||
/// row undoes three steps at once.
|
||||
#[test]
|
||||
@@ -744,8 +801,11 @@ fn the_autosave_knob_sits_right_when_on_and_mirrors_right_to_left() {
|
||||
a.ui.language = "en".into();
|
||||
// Mark the document as saved here without touching disk (`session.dirty` stays false).
|
||||
let path = std::path::PathBuf::from("draft.docx");
|
||||
// A save to the document's own file records both (`note_file_on_disk`): the file is read
|
||||
// from disk, so the switch can be turned on again after it has been turned off.
|
||||
a.session.path = Some(path.clone());
|
||||
a.autosave_path = Some(path);
|
||||
a.autosave_path = Some(path.clone());
|
||||
a.read_from = Some(path);
|
||||
|
||||
let mut h = harness(a);
|
||||
// Arabic UI labels draw even when built without `CRAFT_FONTS_DIR` (embedded Arabic faces).
|
||||
|
||||
@@ -104,7 +104,8 @@ fn leading(app: &mut WordApp, ui: &mut Ui) -> Option<egui::Rect> {
|
||||
/// The AutoSave switch: off and with the reason as its tooltip when AutoSave can't cover the document (#196,
|
||||
/// #176); a click then offers Save As where that helps. Returns the rect it took.
|
||||
fn autosave(app: &mut WordApp, ui: &mut Ui) -> egui::Rect {
|
||||
let block = app.autosave_block();
|
||||
// A file read from disk here is off, not blocked: the switch turns it on (#3307).
|
||||
let block = app.autosave_switch_block();
|
||||
let on = app.autosaves() && block.is_none();
|
||||
let tip = block.as_ref().map_or_else(|| tl!("AutoSave saves every change to the file (needs a saved document)").to_string(), |b| b.reason());
|
||||
// The small switch, as the old title bar drew it.
|
||||
|
||||
@@ -20,6 +20,8 @@ use crate::{WordApp, panes};
|
||||
pub(crate) struct Parked {
|
||||
/// [`WordApp`]'s `autosave_path`: the file AutoSave writes this document to.
|
||||
autosave_path: Option<std::path::PathBuf>,
|
||||
/// [`WordApp`]'s `read_from`: the file on disk this document was read from or saved to.
|
||||
read_from: Option<std::path::PathBuf>,
|
||||
/// The comment panel's selection and the comment and reply text typed in this document and not
|
||||
/// yet written to it.
|
||||
comments: craft::review::ui::CommentsPanelState,
|
||||
@@ -102,7 +104,11 @@ impl WordApp {
|
||||
if from == to {
|
||||
return false;
|
||||
}
|
||||
let leaving = Parked { autosave_path: self.autosave_path.take(), comments: std::mem::take(&mut self.canvas.comments) };
|
||||
let leaving = Parked {
|
||||
autosave_path: self.autosave_path.take(),
|
||||
read_from: self.read_from.take(),
|
||||
comments: std::mem::take(&mut self.canvas.comments),
|
||||
};
|
||||
if self.session.is_open(from) {
|
||||
self.parked.insert(from, leaving);
|
||||
}
|
||||
@@ -110,6 +116,7 @@ impl WordApp {
|
||||
let seen = back.is_some();
|
||||
let back = back.unwrap_or_default();
|
||||
self.autosave_path = back.autosave_path;
|
||||
self.read_from = back.read_from;
|
||||
self.canvas.comments = back.comments;
|
||||
let session = &self.session;
|
||||
self.parked.retain(|d, _| session.is_open(*d));
|
||||
|
||||
@@ -509,6 +509,9 @@ pub struct WordApp {
|
||||
/// there. A file that was merely opened isn't rewritten until the user saves it: saving drops
|
||||
/// whatever WordCraft can't represent (content controls, charts, macros…).
|
||||
autosave_path: Option<std::path::PathBuf>,
|
||||
/// The file on disk the document was read from or saved to. A document opened from bytes
|
||||
/// (`file.open` with `data`) only names a file, so the AutoSave switch can't cover it.
|
||||
read_from: Option<std::path::PathBuf>,
|
||||
/// Web: saves the browser's picker hasn't finished yet (#1315).
|
||||
saves_in_flight: Vec<SaveInFlight>,
|
||||
/// The Save Changes prompt this window is running and the command it holds back.
|
||||
|
||||
@@ -20,7 +20,7 @@ pub enum AutoSaveBlock {
|
||||
Unsaved,
|
||||
/// A file in a format that can't keep everything (Markdown, plain text, HTML…), by file name.
|
||||
Format { name: String },
|
||||
/// A file that was opened but not yet saved in this session.
|
||||
/// A file that was opened but not yet saved in this session; the switch shows off and lifts it.
|
||||
NotSavedHere,
|
||||
}
|
||||
|
||||
|
||||
@@ -3247,6 +3247,99 @@ fn autosave_is_unavailable_in_the_browser() {
|
||||
assert!(err.contains("browser"), "{err}");
|
||||
}
|
||||
|
||||
/// #3307: an opened file isn't rewritten until the user says so, but the switch says so
|
||||
/// without Save As: turning it on lets AutoSave write the file.
|
||||
#[test]
|
||||
fn turning_autosave_on_covers_a_file_only_opened_here() {
|
||||
let dir = scratch("autosave-opened");
|
||||
let path = dir.join("report.docx");
|
||||
let mut a = typed();
|
||||
a.run("file.save", json!({"path": path.to_string_lossy()})).unwrap();
|
||||
let mut b = app();
|
||||
b.run("file.open", json!({"path": path.to_string_lossy()})).unwrap();
|
||||
assert_eq!(b.autosave_block(), Some(AutoSaveBlock::NotSavedHere));
|
||||
assert_eq!(b.autosave_switch_block(), None, "the switch lifts it itself");
|
||||
b.run("text.insert", json!({"text": "Opened "})).unwrap();
|
||||
b.autosave_tick(monotonic_ms() + 10_000.0);
|
||||
assert!(b.session.dirty, "opening alone never rewrites the file");
|
||||
assert!(b.run("file.autosave", json!({"value": "false"})).is_err());
|
||||
assert!(!b.autosaves(), "a value that isn't a bool grants nothing");
|
||||
assert_eq!(b.run("file.autosave", json!({})).unwrap(), json!({"value": true}));
|
||||
assert!(b.autosaves());
|
||||
b.autosave_tick(monotonic_ms() + 20_000.0);
|
||||
assert!(!b.session.dirty, "AutoSave wrote the opened file");
|
||||
assert!(b.run("file.autosave", json!({"value": false})).is_ok());
|
||||
assert!(!b.autosaves());
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
/// `file.open` with `data` only names the file: the switch mustn't let AutoSave write a file of
|
||||
/// that name, which may hold something else entirely.
|
||||
#[test]
|
||||
fn the_autosave_switch_refuses_a_document_opened_from_bytes() {
|
||||
let dir = scratch("autosave-bytes");
|
||||
let path = dir.join("other.docx");
|
||||
let mut a = typed();
|
||||
a.run("file.save", json!({"path": path.to_string_lossy()})).unwrap();
|
||||
let on_disk = std::fs::read(&path).unwrap();
|
||||
let files = Arc::new(ScriptedFiles::new());
|
||||
let mut b = app_with_files(files.clone());
|
||||
let data = wordcraft_engine::cmd::insert::base64_encode(&on_disk);
|
||||
b.execute("file.open", json!({"path": path.to_string_lossy(), "data": data})).unwrap();
|
||||
assert_eq!(b.autosave_block(), Some(AutoSaveBlock::NotSavedHere));
|
||||
assert_eq!(b.autosave_switch_block(), Some(AutoSaveBlock::NotSavedHere), "the switch shows the block");
|
||||
assert!(b.run("file.autosave", json!({"value": true})).is_err());
|
||||
b.status = None;
|
||||
let err = b.run("file.autosave", json!({})).unwrap_err();
|
||||
assert_eq!(status_text(&b), Some(err.as_str()), "the status bar says why");
|
||||
b.run("text.insert", json!({"text": "Bytes "})).unwrap();
|
||||
b.autosave_tick(monotonic_ms() + 10_000.0);
|
||||
assert!(b.session.dirty);
|
||||
assert_eq!(std::fs::read(&path).unwrap(), on_disk, "the named file is untouched");
|
||||
// Open without a path only asks for the picker: it isn't a read of the shown file.
|
||||
script_pick(&files, None);
|
||||
b.run("file.open", json!({"path": null})).unwrap();
|
||||
assert!(b.run("file.autosave", json!({"value": true})).is_err(), "the picker request grants nothing");
|
||||
// A `data` that isn't a string is ignored and the file is read from disk.
|
||||
let mut c = app();
|
||||
c.run("file.open", json!({"path": path.to_string_lossy(), "data": null})).unwrap();
|
||||
assert!(c.run("file.autosave", json!({"value": true})).is_ok(), "read from disk");
|
||||
// Saving is still the way: then AutoSave covers the file.
|
||||
b.run("file.save", json!({})).unwrap();
|
||||
assert!(b.autosaves());
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
/// AutoSave turned off in one document stays off there when another document turns it on: the
|
||||
/// preference is shared, but each document's go-ahead is its own.
|
||||
#[test]
|
||||
fn autosave_turned_off_stays_off_when_another_document_turns_it_on() {
|
||||
let dir = scratch("autosave-two");
|
||||
let (first_path, second_path) = (dir.join("first.docx"), dir.join("second.docx"));
|
||||
let mut c = typed();
|
||||
c.run("file.save", json!({"path": second_path.to_string_lossy()})).unwrap();
|
||||
let mut a = typed();
|
||||
a.run("file.save", json!({"path": first_path.to_string_lossy()})).unwrap();
|
||||
let first = a.session.document_id();
|
||||
let saved = std::fs::read(&first_path).unwrap();
|
||||
assert!(a.autosaves());
|
||||
a.run("file.autosave", json!({"value": false})).unwrap();
|
||||
a.run("file.open", json!({"path": second_path.to_string_lossy()})).unwrap();
|
||||
assert_ne!(a.session.document_id(), first, "the second file opens beside the first");
|
||||
a.run("file.autosave", json!({"value": true})).unwrap();
|
||||
assert!(a.autosaves());
|
||||
a.show_document(first).unwrap();
|
||||
assert!(!a.autosaves(), "the first document's switch is still off");
|
||||
a.run("text.insert", json!({"text": "Later "})).unwrap();
|
||||
a.autosave_tick(monotonic_ms() + 10_000.0);
|
||||
assert!(a.session.dirty);
|
||||
assert_eq!(std::fs::read(&first_path).unwrap(), saved, "the first file is untouched");
|
||||
// Turned back on, it covers the file it was saved to without another save.
|
||||
a.run("file.autosave", json!({"value": true})).unwrap();
|
||||
assert!(a.autosaves());
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
/// Cancelling the Open picker (Mod+O) left the document as it was but turned AutoSave off.
|
||||
#[test]
|
||||
fn cancelling_open_keeps_autosave() {
|
||||
|
||||
@@ -186,6 +186,7 @@ impl WordApp {
|
||||
clipboard_published: _,
|
||||
// Per document: the shown document's, swapped with `parked` on a document switch.
|
||||
autosave_path: _,
|
||||
read_from: _,
|
||||
parked: _,
|
||||
// Per window.
|
||||
ui,
|
||||
|
||||
Reference in New Issue
Block a user