mirror of
https://github.com/storytold/deckcraft.git
synced 2026-12-01 18:23:52 -06:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2c3a03018d | ||
|
|
8413f81acd | ||
|
|
628777ddad | ||
|
|
655c4d2490 | ||
|
|
6e3f978dd6 | ||
|
|
80cc9b65c8 | ||
|
|
432db38d6a | ||
|
|
e8c207e673 | ||
|
|
cf7d35c955 | ||
|
|
0da7712a29 | ||
|
|
488aca4e7f | ||
|
|
4f959912b4 | ||
|
|
f9a328a147 | ||
|
|
d01e5a1c5e | ||
|
|
fce2a88d31 | ||
|
|
6c068ffc30 | ||
|
|
7a51671ebd | ||
|
|
d5741616c1 | ||
|
|
877e6db673 | ||
|
|
b697455ea7 | ||
|
|
5c179822d9 |
@@ -5,23 +5,23 @@ version: 'v0.0.3'
|
||||
workflows:
|
||||
'.github/workflows/ci.yml':
|
||||
- 'actions/checkout@v7.0.1'
|
||||
- 'step-security/harden-runner@v2.23.0'
|
||||
- 'step-security/harden-runner@v2.22.4'
|
||||
'.github/workflows/freebsd.yml': []
|
||||
'.github/workflows/house-rules.yml': []
|
||||
'.github/workflows/lockfile.yml':
|
||||
- 'actions/checkout@v7.0.1'
|
||||
- 'step-security/harden-runner@v2.23.0'
|
||||
- 'step-security/harden-runner@v2.22.4'
|
||||
'.github/workflows/nightly-dispatch.yml': []
|
||||
'.github/workflows/nightly-macos.yml':
|
||||
- 'actions/checkout@v7.0.1'
|
||||
- 'step-security/harden-runner@v2.23.0'
|
||||
- 'step-security/harden-runner@v2.22.4'
|
||||
'.github/workflows/nightly-sign.yml': []
|
||||
'.github/workflows/nightly.yml': []
|
||||
'.github/workflows/nix.yml': []
|
||||
'.github/workflows/notify-services.yml': []
|
||||
'.github/workflows/packaging-lint.yml':
|
||||
- 'actions/checkout@v7.0.1'
|
||||
- 'step-security/harden-runner@v2.23.0'
|
||||
- 'step-security/harden-runner@v2.22.4'
|
||||
'.github/workflows/release.yml': []
|
||||
'.github/workflows/sonarcloud.yml': []
|
||||
'.github/workflows/windows-arm64.yml': []
|
||||
@@ -31,8 +31,8 @@ dependencies:
|
||||
commit: 'sha1-3d3c42e5aac5ba805825da76410c181273ba90b1'
|
||||
owner_id: 44036562
|
||||
repo_id: 197814629
|
||||
'step-security/harden-runner@v2.23.0':
|
||||
ref: 'v2.23.0'
|
||||
commit: 'sha1-aebdcf6156ab6691adf63218240e476564a1ed4c'
|
||||
'step-security/harden-runner@v2.22.4':
|
||||
ref: 'v2.22.4'
|
||||
commit: 'sha1-1d88384fd173af6ecb012dfd0264a306b711241a'
|
||||
owner_id: 88700172
|
||||
repo_id: 422287306
|
||||
|
||||
@@ -27,7 +27,7 @@ jobs:
|
||||
CRAFT_FONTS_REF: 37827adc41fd165f9a9234ac40f94dbad353aa69
|
||||
steps:
|
||||
- name: Harden runner
|
||||
uses: step-security/harden-runner@v2.23.0
|
||||
uses: step-security/harden-runner@v2.22.4
|
||||
with:
|
||||
egress-policy: audit
|
||||
- name: Check out craft-fonts
|
||||
|
||||
@@ -22,7 +22,7 @@ jobs:
|
||||
timeout-minutes: 10
|
||||
steps:
|
||||
- name: Harden runner
|
||||
uses: step-security/harden-runner@v2.23.0
|
||||
uses: step-security/harden-runner@v2.22.4
|
||||
with:
|
||||
egress-policy: audit
|
||||
- uses: actions/checkout@v7.0.1
|
||||
|
||||
@@ -36,7 +36,7 @@ jobs:
|
||||
RUST_BACKTRACE: 1
|
||||
steps:
|
||||
- name: Harden runner
|
||||
uses: step-security/harden-runner@v2.23.0
|
||||
uses: step-security/harden-runner@v2.22.4
|
||||
with:
|
||||
egress-policy: audit
|
||||
- uses: actions/checkout@v7.0.1
|
||||
|
||||
@@ -28,7 +28,7 @@ jobs:
|
||||
runs-on: windows-latest
|
||||
steps:
|
||||
- name: Harden runner
|
||||
uses: step-security/harden-runner@v2.23.0
|
||||
uses: step-security/harden-runner@v2.22.4
|
||||
with:
|
||||
egress-policy: audit
|
||||
- uses: actions/checkout@v7.0.1
|
||||
@@ -58,7 +58,7 @@ jobs:
|
||||
contents: read
|
||||
steps:
|
||||
- name: Harden runner
|
||||
uses: step-security/harden-runner@v2.23.0
|
||||
uses: step-security/harden-runner@v2.22.4
|
||||
with:
|
||||
egress-policy: audit
|
||||
- uses: actions/checkout@v7.0.1
|
||||
|
||||
@@ -399,7 +399,8 @@ pub fn save_bytes(doc: &Presentation, format: &str) -> Result<Vec<u8>> {
|
||||
"pdf" => deckcraft_pdf::export(doc, &Default::default()).map_err(|e| EngineError::Other(e.to_string())),
|
||||
"gif" => gif_bytes(doc, &Value::Null),
|
||||
"png" | "jpeg" | "jpg" => Err(EngineError::Other("saving a presentation as a single image isn't supported; use file.export".into())),
|
||||
_ => deckcraft_format::save(doc).map_err(|e| EngineError::Other(e.to_string())),
|
||||
"deckcraft" => deckcraft_format::save(doc).map_err(|e| EngineError::Other(e.to_string())),
|
||||
_ => Err(EngineError::Other(format!("unknown save format `{format}`"))),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -493,6 +494,27 @@ pub fn writable_format_for_path(path: &str) -> Option<&'static str> {
|
||||
WRITABLE_EXTENSIONS.contains(&ext.as_str()).then(|| format_for_path(path))
|
||||
}
|
||||
|
||||
/// The format a save path names, or an error listing the file types DeckCraft writes.
|
||||
fn writable_format(path: &str) -> Result<&'static str> {
|
||||
writable_format_for_path(path).ok_or_else(|| {
|
||||
EngineError::Other(format!(
|
||||
"{path}: DeckCraft cannot save this file type; save as .pptx, .potx, .ppsx, .deckcraft, .pdf, .png, .jpg/.jpeg, .gif, .txt, .md or .rtf"
|
||||
))
|
||||
})
|
||||
}
|
||||
|
||||
/// Whether an explicit save format names the same writer as the path. `txt`/`outline` are the
|
||||
/// same writer only for a `.txt` path, and `jpg`/`jpeg` are aliases; every other mismatch would
|
||||
/// write bytes that lie about the file type.
|
||||
fn same_save_format(path: &str, path_format: &str, format: &str) -> bool {
|
||||
if format == path_format {
|
||||
return true;
|
||||
}
|
||||
let lower = path.to_ascii_lowercase();
|
||||
(lower.ends_with(".txt") && matches!((path_format, format), ("outline", "txt") | ("txt", "outline")))
|
||||
|| matches!((path_format, format), ("jpeg", "jpg") | ("jpg", "jpeg"))
|
||||
}
|
||||
|
||||
/// Animated GIF bytes for `doc` with the options in `p` (see [`crate::gif_export::GifOptions::from_params`]).
|
||||
pub fn gif_bytes(doc: &Presentation, p: &Value) -> Result<Vec<u8>> {
|
||||
Ok(crate::gif_export::encode(doc, &crate::gif_export::GifOptions::from_params(p)?)?.bytes)
|
||||
@@ -542,7 +564,13 @@ fn save(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
s.ui_requests.push(crate::UiRequest::PickFile { purpose: "saveAs".into() });
|
||||
return Ok(json!({"needsPath": true}));
|
||||
};
|
||||
let format = str_param(p, "format").unwrap_or(format_for_path(&path));
|
||||
let path_format = writable_format(&path)?;
|
||||
let format = str_param(p, "format").unwrap_or(path_format);
|
||||
if !same_save_format(&path, path_format, format) {
|
||||
return Err(EngineError::Other(format!(
|
||||
"{path}: format `{format}` does not match the file type; save as {path_format} or choose a matching extension"
|
||||
)));
|
||||
}
|
||||
let bytes = save_bytes(&s.doc()?.doc, format)?;
|
||||
write_file(&path, &bytes)?;
|
||||
mark_saved(s, Some(path.clone()))?;
|
||||
@@ -629,7 +657,10 @@ fn color_at(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
|
||||
fn export(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let path = str_param(p, "path").ok_or_else(|| bad("file.export", "missing `path`"))?.to_string();
|
||||
let format = str_param(p, "format").unwrap_or(format_for_path(&path)).to_string();
|
||||
let format = match str_param(p, "format") {
|
||||
Some(f) => f.to_string(),
|
||||
None => writable_format(&path)?.to_string(),
|
||||
};
|
||||
let st = s.doc()?;
|
||||
let scale = finite_scale(f64_or(p, "scale", 2.0));
|
||||
let transparent = bool_or(p, "transparentBackground", false);
|
||||
|
||||
@@ -73,6 +73,19 @@ pub fn content_type(name: &str, bytes: &[u8]) -> &'static str {
|
||||
}
|
||||
}
|
||||
|
||||
/// The audio MIME for a container whose bytes are audio-only, so the PPTX writer stores it as
|
||||
/// sound rather than a movie. Video-bearing containers keep their `video/*` type.
|
||||
fn audio_content_type(ct: &'static str) -> &'static str {
|
||||
match ct {
|
||||
"video/x-ms-wmv" => "audio/x-ms-wma",
|
||||
"video/mp4" => "audio/mp4",
|
||||
"video/webm" => "audio/webm",
|
||||
"video/ogg" => "audio/ogg",
|
||||
"video/quicktime" => "audio/mp4",
|
||||
_ => ct,
|
||||
}
|
||||
}
|
||||
|
||||
/// Natural size of an image in points (96 dpi), fitted inside the slide.
|
||||
fn fitted(s: &Session, bytes: &[u8]) -> (f64, f64) {
|
||||
let (w, h) = image::ImageReader::new(std::io::Cursor::new(bytes))
|
||||
@@ -175,6 +188,10 @@ fn media(s: &mut Session, p: &Value, video: bool) -> Result<Value> {
|
||||
Err(_) if !video && !ct.starts_with("audio/") => return Err(bad(cmd, format!("{name} is not a supported audio file ({ct})"))),
|
||||
_ => {}
|
||||
}
|
||||
// The extension decides the container's MIME even when the probe finds only an audio stream
|
||||
// (#2390): keep it `audio/*` so the writer stores sound, not a movie. The validation above
|
||||
// still uses the original `ct`, because an unreadable file has no audio stream.
|
||||
let ct = if !video && has_audio && !has_video { audio_content_type(ct) } else { ct };
|
||||
let (duration_ms, width, height, playable) = match &info {
|
||||
Ok(i) => {
|
||||
let (w, h) = i.video.as_ref().map(|v| (v.width, v.height)).unwrap_or((0, 0));
|
||||
@@ -244,3 +261,23 @@ pub(super) fn audio(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
pub(super) fn video(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
media(s, p, true)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::audio_content_type;
|
||||
|
||||
#[test]
|
||||
fn audio_content_type_maps_every_audio_only_container() {
|
||||
for (video, audio) in [
|
||||
("video/x-ms-wmv", "audio/x-ms-wma"),
|
||||
("video/mp4", "audio/mp4"),
|
||||
("video/webm", "audio/webm"),
|
||||
("video/ogg", "audio/ogg"),
|
||||
("video/quicktime", "audio/mp4"),
|
||||
] {
|
||||
assert_eq!(audio_content_type(video), audio, "{video}");
|
||||
}
|
||||
assert_eq!(audio_content_type("audio/mpeg"), "audio/mpeg");
|
||||
assert_eq!(audio_content_type("application/octet-stream"), "application/octet-stream");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -182,7 +182,7 @@ mod tests {
|
||||
use deckcraft_model::ShapeId;
|
||||
use serde_json::json;
|
||||
|
||||
use crate::cmd::base64_encode;
|
||||
use crate::cmd::{base64_decode, base64_encode};
|
||||
use crate::{MediaStatus, Session, UiRequest};
|
||||
|
||||
fn wav() -> Vec<u8> {
|
||||
@@ -291,6 +291,72 @@ mod tests {
|
||||
assert!(r["warning"].as_str().is_some_and(|w| w.contains("can't be played")), "{r}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn audio_only_asf_saves_as_sound() {
|
||||
let mut s = Session::with_new();
|
||||
let bytes = include_bytes!("../../../media/tests/fixtures/tone.wma");
|
||||
let r = s.execute("insert.audio", &json!({"name": "tone.asf", "data": base64_encode(bytes)})).expect("insert");
|
||||
assert_eq!(r["contentType"], "audio/x-ms-wma", "{r}");
|
||||
|
||||
let saved = s.execute("file.saveBytes", &json!({"format": "pptx"})).expect("save");
|
||||
let zip = base64_decode(saved["data"].as_str().unwrap()).unwrap();
|
||||
let mut z = zip::ZipArchive::new(std::io::Cursor::new(zip)).unwrap();
|
||||
let names: Vec<String> = (0..z.len()).map(|i| z.by_index(i).unwrap().name().to_string()).collect();
|
||||
assert!(names.iter().any(|n| n == "ppt/media/media1.wma"), "{names:?}");
|
||||
assert!(!names.iter().any(|n| n.ends_with(".wmv")), "{names:?}");
|
||||
|
||||
s.execute("file.openBytes", &json!({"name": "deck.pptx", "data": saved["data"]})).expect("open");
|
||||
let info = s.execute("media.info", &json!({})).expect("info");
|
||||
assert_eq!(info["video"], false, "{info}");
|
||||
assert!(info["durationMs"].as_u64().is_some_and(|ms| ms > 0), "{info}");
|
||||
assert_eq!(info["contentType"], "audio/x-ms-wma", "{info}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_video_container_inserted_as_video_stays_a_movie() {
|
||||
let mut s = Session::with_new();
|
||||
let clip = include_bytes!("../../../media/tests/fixtures/clip-h264.mp4");
|
||||
let r = s.execute("insert.video", &json!({"name": "clip.mp4", "data": base64_encode(clip)})).expect("insert");
|
||||
assert_eq!(r["contentType"], "video/mp4", "{r}");
|
||||
|
||||
let saved = s.execute("file.saveBytes", &json!({"format": "pptx"})).expect("save");
|
||||
s.execute("file.openBytes", &json!({"name": "deck.pptx", "data": saved["data"]})).expect("open");
|
||||
let info = s.execute("media.info", &json!({})).expect("info");
|
||||
assert_eq!(info["video"], true, "{info}");
|
||||
assert_eq!(info["contentType"], "video/mp4", "{info}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn insert_video_still_rejects_an_audio_only_file() {
|
||||
let mut s = Session::with_new();
|
||||
let bytes = include_bytes!("../../../media/tests/fixtures/tone.wma");
|
||||
let err = s.execute("insert.video", &json!({"name": "tone.asf", "data": base64_encode(bytes)})).unwrap_err();
|
||||
assert!(err.to_string().contains("has no video"), "{err}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unreadable_audio_is_still_refused() {
|
||||
let mut s = Session::with_new();
|
||||
let err = s.execute("insert.audio", &json!({"name": "tone.wma", "data": base64_encode(b"not media")})).unwrap_err();
|
||||
assert!(err.to_string().contains("is not a supported audio file"), "{err}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn audio_only_ogv_saves_as_sound() {
|
||||
let mut s = Session::with_new();
|
||||
let _ = s.execute("slide.new", &json!({"layout": "blank"}));
|
||||
let bytes = include_bytes!("../../../media/tests/fixtures/tone.ogg");
|
||||
let r = s.execute("insert.audio", &json!({"name": "tone.ogv", "data": base64_encode(bytes)})).expect("insert");
|
||||
assert_eq!(r["contentType"], "audio/ogg", "{r}");
|
||||
|
||||
let saved = s.execute("file.saveBytes", &json!({"format": "pptx"})).expect("save");
|
||||
let zip = base64_decode(saved["data"].as_str().unwrap()).unwrap();
|
||||
let mut z = zip::ZipArchive::new(std::io::Cursor::new(zip)).unwrap();
|
||||
let names: Vec<String> = (0..z.len()).map(|i| z.by_index(i).unwrap().name().to_string()).collect();
|
||||
assert!(names.iter().any(|n| n == "ppt/media/media1.ogg"), "{names:?}");
|
||||
assert!(!names.iter().any(|n| n.ends_with(".ogv")), "{names:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_vp8_webm_is_playable_and_warns_that_only_the_audio_plays() {
|
||||
let mut s = Session::with_new();
|
||||
|
||||
@@ -330,7 +330,7 @@ fn slide_ids_param(v: &Value, doc: &Presentation, cmd: &str) -> Result<HashSet<S
|
||||
|
||||
/// Keeps sections in step after `moved` landed as a run at `at`: the run joins the section of
|
||||
/// the slide just before it (the first section when it opens the deck), in document order.
|
||||
fn reassign_sections(doc: &mut Presentation, moved: &[SlideId], at: usize) {
|
||||
pub(crate) fn reassign_sections(doc: &mut Presentation, moved: &[SlideId], at: usize) {
|
||||
if doc.sections.is_empty() {
|
||||
return;
|
||||
}
|
||||
@@ -690,6 +690,11 @@ fn section_remove(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let sec = doc.sections.remove(i);
|
||||
if with_slides {
|
||||
doc.slides.retain(|x| !sec.slides.contains(&x.id));
|
||||
// As in slide.delete: the deleted slides leave every custom show and the selection.
|
||||
for show in &mut doc.custom_shows {
|
||||
show.slides.retain(|x| !sec.slides.contains(x));
|
||||
}
|
||||
sel.slides.retain(|x| !sec.slides.contains(x));
|
||||
sel.slide = sel.slide.min(doc.slides.len().saturating_sub(1));
|
||||
} else if let Some(prev) = i.checked_sub(1).and_then(|k| doc.sections.get_mut(k)) {
|
||||
prev.slides.extend(sec.slides);
|
||||
@@ -982,6 +987,29 @@ mod tests {
|
||||
assert_eq!(selected(&s), (0, vec![ids[2]]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn removing_a_section_with_its_slides_prunes_them_from_custom_shows() {
|
||||
let (mut s, ids) = deck(4);
|
||||
s.execute("section.add", &json!({"name": "A", "at": 0})).expect("A");
|
||||
s.execute("section.add", &json!({"name": "B", "at": 2})).expect("B");
|
||||
let show = vec![deckcraft_model::SlideId(ids[0]), deckcraft_model::SlideId(ids[2]), deckcraft_model::SlideId(ids[3])];
|
||||
s.edit(|doc, _| {
|
||||
doc.custom_shows = vec![deckcraft_model::CustomShow { name: "Short".into(), slides: show.clone() }];
|
||||
Ok(())
|
||||
})
|
||||
.expect("set custom show");
|
||||
s.execute("slide.selectSlides", &json!({"ids": [ids[1], ids[3]]})).expect("select");
|
||||
|
||||
s.execute("section.remove", &json!({"index": 1, "slides": true})).expect("remove B and its slides");
|
||||
assert_eq!(order(&s), [ids[0], ids[1]]);
|
||||
let st = s.doc().expect("doc");
|
||||
assert_eq!(st.doc.custom_shows[0].slides, [deckcraft_model::SlideId(ids[0])], "the deleted slides leave the custom show");
|
||||
assert!(st.selection.slides.iter().all(|x| st.doc.slide_index(*x).is_some()), "the selection names only slides of the deck");
|
||||
|
||||
s.execute("edit.undo", &json!({})).expect("undo");
|
||||
assert_eq!(s.doc().expect("doc").doc.custom_shows[0].slides, show);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn duplicate_keeps_section_order_in_step_with_slides() {
|
||||
let mut s = Session::with_new();
|
||||
|
||||
@@ -514,14 +514,24 @@ pub fn accept(doc: &mut Presentation, rev: &Presentation, change: &Change) -> cr
|
||||
let Some(b) = change.revised.and_then(|r| rev.slides.get(r)).cloned() else { return Ok(None) };
|
||||
let sl = adopt(doc, rev, &b, None)?;
|
||||
let at = change.slide.min(doc.slides.len());
|
||||
let id = sl.id;
|
||||
doc.slides.insert(at, Arc::new(sl));
|
||||
// The new slide joins the section of the slide before it, as slide.new does.
|
||||
crate::cmd::slide::reassign_sections(doc, &[id], at);
|
||||
Ok(Some(at))
|
||||
}
|
||||
ChangeKind::Deleted => {
|
||||
if change.slide >= doc.slides.len() || doc.slides.len() <= 1 {
|
||||
return Ok(None);
|
||||
}
|
||||
doc.slides.remove(change.slide);
|
||||
let gone = doc.slides.remove(change.slide).id;
|
||||
// As in slide.delete: the slide leaves its section and every custom show.
|
||||
for sec in &mut doc.sections {
|
||||
sec.slides.retain(|x| *x != gone);
|
||||
}
|
||||
for show in &mut doc.custom_shows {
|
||||
show.slides.retain(|x| *x != gone);
|
||||
}
|
||||
Ok(Some(change.slide.min(doc.slides.len().saturating_sub(1))))
|
||||
}
|
||||
}
|
||||
@@ -811,6 +821,33 @@ mod tests {
|
||||
assert!(s.execute("review.accept", &json!({"all": true})).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn accepted_slides_keep_sections_and_custom_shows_in_step() {
|
||||
let (mut s, rev, _) = decks();
|
||||
let ids = |s: &Session| -> Vec<deckcraft_model::SlideId> { s.doc().unwrap().doc.slides.iter().map(|x| x.id).collect() };
|
||||
let before = ids(&s);
|
||||
assert!(!s.doc().unwrap().doc.sections.is_empty(), "the sample deck has sections");
|
||||
let show: Vec<_> = before.iter().take(3).copied().collect();
|
||||
s.edit(|doc, _| {
|
||||
doc.custom_shows = vec![deckcraft_model::CustomShow { name: "Short".into(), slides: show.clone() }];
|
||||
Ok(())
|
||||
})
|
||||
.unwrap();
|
||||
s.execute("review.compare", &json!({"name": "rev.deckcraft", "data": rev})).unwrap();
|
||||
s.execute("review.accept", &json!({"all": true})).unwrap();
|
||||
|
||||
let st = s.doc().unwrap();
|
||||
let after = ids(&s);
|
||||
let gone = before[1];
|
||||
assert!(!after.contains(&gone), "the revision deleted slide 2");
|
||||
let in_sections: Vec<_> = st.doc.sections.iter().flat_map(|x| x.slides.iter().copied()).collect();
|
||||
assert_eq!(in_sections, after, "every slide is in one section, in deck order, and no section keeps the deleted slide");
|
||||
let new = st.doc.slides.iter().find(|x| super::slide_title(x) == "Brand new").unwrap().id;
|
||||
assert!(st.doc.section_of(new).is_some(), "the inserted slide joins a section");
|
||||
assert!(!st.doc.custom_shows[0].slides.contains(&gone), "the deleted slide leaves the custom show");
|
||||
assert_eq!(st.doc.custom_shows[0].slides.len(), show.len() - 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reject_keeps_the_open_deck_and_accept_all_skips_rejected_revisions() {
|
||||
let (mut s, rev, n) = decks();
|
||||
|
||||
@@ -7803,6 +7803,153 @@ fn save_to_image_path_is_an_error_not_zip_bytes() {
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unsupported_save_paths_are_refused_and_write_nothing() {
|
||||
let dir = std::env::temp_dir().join(format!("deckcraft-save-ext-{}", std::process::id()));
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
for name in ["a.pptm", "a.ppt", "a.pot", "a.potm", "a.pps", "a.ppsm", "a.odp", "a.xml", "a.svg", "a.bmp", "a"] {
|
||||
let mut s = Session::new();
|
||||
sample::open_sample(&mut s).unwrap();
|
||||
let path = dir.join(name).to_string_lossy().to_string();
|
||||
let before = s.doc().unwrap().path.clone();
|
||||
let err = s.execute("file.saveAs", &json!({"path": path})).unwrap_err();
|
||||
assert!(err.to_string().contains("cannot save"), "{name}: {err}");
|
||||
assert!(!std::path::Path::new(&path).exists(), "{name} must not be written");
|
||||
assert_eq!(s.doc().unwrap().path, before, "{name}: the document keeps its path");
|
||||
}
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unsupported_paths_are_refused_even_with_an_explicit_format() {
|
||||
let dir = std::env::temp_dir().join(format!("deckcraft-save-ext-fmt-{}", std::process::id()));
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let mut s = Session::new();
|
||||
sample::open_sample(&mut s).unwrap();
|
||||
let path = dir.join("a.pptm").to_string_lossy().to_string();
|
||||
let err = s.execute("file.saveAs", &json!({"path": path, "format": "pptx"})).unwrap_err();
|
||||
assert!(err.to_string().contains("cannot save"), "{err}");
|
||||
assert!(!std::path::Path::new(&path).exists(), "the path guard wins over the explicit format");
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unsupported_extensionless_save_is_refused_even_with_an_explicit_format() {
|
||||
let dir = std::env::temp_dir().join(format!("deckcraft-save-extless-{}", std::process::id()));
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let mut s = Session::new();
|
||||
sample::open_sample(&mut s).unwrap();
|
||||
let path = dir.join("no-extension").to_string_lossy().to_string();
|
||||
let err = s.execute("file.saveAs", &json!({"path": path, "format": "pptx"})).unwrap_err();
|
||||
assert!(err.to_string().contains("cannot save"), "{err}");
|
||||
assert!(!std::path::Path::new(&path).exists(), "the path guard wins over the explicit format");
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn save_bytes_rejects_an_unknown_format() {
|
||||
let mut s = Session::new();
|
||||
sample::open_sample(&mut s).unwrap();
|
||||
let err = s.execute("file.saveBytes", &json!({"format": "odp"})).unwrap_err();
|
||||
assert!(err.to_string().contains("unknown save format"), "{err}");
|
||||
assert!(s.execute("file.saveBytes", &json!({"format": "deckcraft"})).is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unsupported_export_paths_are_refused() {
|
||||
let dir = std::env::temp_dir().join(format!("deckcraft-export-ext-{}", std::process::id()));
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let mut s = Session::new();
|
||||
sample::open_sample(&mut s).unwrap();
|
||||
let path = dir.join("a.pptm").to_string_lossy().to_string();
|
||||
assert!(s.execute("file.export", &json!({"path": path})).is_err());
|
||||
assert!(!std::path::Path::new(&path).exists());
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn extensionless_export_with_an_explicit_format_still_writes() {
|
||||
let dir = std::env::temp_dir().join(format!("deckcraft-export-fmt-{}", std::process::id()));
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let mut s = Session::new();
|
||||
sample::open_sample(&mut s).unwrap();
|
||||
let path = dir.join("out").to_string_lossy().to_string();
|
||||
s.execute("file.export", &json!({"path": path, "format": "png", "slide": 0})).unwrap();
|
||||
assert!(std::path::Path::new(&path).exists());
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn supported_paths_still_save() {
|
||||
let dir = std::env::temp_dir().join(format!("deckcraft-save-ok-{}", std::process::id()));
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
for (name, magic) in [("a.pptx", b"PK".as_slice()), ("a.deckcraft", b"PK".as_slice()), ("a.txt", b"")] {
|
||||
let mut s = Session::new();
|
||||
sample::open_sample(&mut s).unwrap();
|
||||
let path = dir.join(name).to_string_lossy().to_string();
|
||||
s.execute("file.saveAs", &json!({"path": path})).unwrap();
|
||||
let bytes = std::fs::read(&path).unwrap();
|
||||
assert!(!bytes.is_empty(), "{name}");
|
||||
if !magic.is_empty() {
|
||||
assert!(bytes.starts_with(magic), "{name}: not a ZIP");
|
||||
}
|
||||
}
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn explicit_save_format_must_match_the_path() {
|
||||
let dir = std::env::temp_dir().join(format!("deckcraft-save-mismatch-{}", std::process::id()));
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let mut s = Session::new();
|
||||
sample::open_sample(&mut s).unwrap();
|
||||
let path = dir.join("a.pptx").to_string_lossy().to_string();
|
||||
std::fs::write(&path, b"sentinel").unwrap();
|
||||
let before = s.doc().unwrap().path.clone();
|
||||
let err = s.execute("file.saveAs", &json!({"path": path, "format": "deckcraft"})).unwrap_err();
|
||||
assert!(err.to_string().contains("does not match"), "{err}");
|
||||
assert_eq!(std::fs::read(&path).unwrap(), b"sentinel", "the existing file is untouched");
|
||||
assert_eq!(s.doc().unwrap().path, before, "the document keeps its path");
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn existing_unsupported_destination_is_untouched() {
|
||||
let dir = std::env::temp_dir().join(format!("deckcraft-save-existing-{}", std::process::id()));
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let mut s = Session::new();
|
||||
sample::open_sample(&mut s).unwrap();
|
||||
let path = dir.join("a.pptm").to_string_lossy().to_string();
|
||||
std::fs::write(&path, b"sentinel").unwrap();
|
||||
let before = s.doc().unwrap().path.clone();
|
||||
let err = s.execute("file.saveAs", &json!({"path": path})).unwrap_err();
|
||||
assert!(err.to_string().contains("cannot save"), "{err}");
|
||||
assert_eq!(std::fs::read(&path).unwrap(), b"sentinel", "the existing file is untouched");
|
||||
assert_eq!(s.doc().unwrap().path, before, "the document keeps its path");
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn explicit_txt_format_is_allowed_for_a_txt_path() {
|
||||
let dir = std::env::temp_dir().join(format!("deckcraft-save-txt-alias-{}", std::process::id()));
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let mut s = Session::new();
|
||||
sample::open_sample(&mut s).unwrap();
|
||||
let path = dir.join("outline.txt").to_string_lossy().to_string();
|
||||
s.execute("file.saveAs", &json!({"path": path, "format": "txt"})).unwrap();
|
||||
assert!(!std::fs::read(&path).unwrap().is_empty());
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
/// `shape.connect` glues to a shape inside a group, as PowerPoint does: the connector's
|
||||
/// end is set to the grouped shape and follows it when the group moves. The glue was dropped at
|
||||
/// once (the end stayed `None`) because rerouting only looked at the slide's top-level shapes,
|
||||
|
||||
@@ -125,7 +125,16 @@ pub(crate) struct BackKey {
|
||||
struct Transition {
|
||||
/// The slide left behind, whose shapes Morph matches.
|
||||
from: Option<usize>,
|
||||
/// The time of the key press. A hidden show, which draws nothing, expires the transition
|
||||
/// from it.
|
||||
start: f64,
|
||||
/// Where the drawn transition's clock starts: the frame after the first one (see `progress`),
|
||||
/// because rendering the new slide and the transition's pictures can take longer than the
|
||||
/// whole transition (#1385). `None` until then.
|
||||
clock: Option<f64>,
|
||||
/// The first frame of the transition has been drawn: the new slide and the pictures the
|
||||
/// transition needs exist, so the time they took is not part of the transition.
|
||||
primed: bool,
|
||||
kind: String,
|
||||
option: String,
|
||||
dur: f64,
|
||||
@@ -134,6 +143,20 @@ struct Transition {
|
||||
reverse: bool,
|
||||
}
|
||||
|
||||
impl Transition {
|
||||
/// How far the transition has come at frame time `now`, from 0 to 1. The first frame is the
|
||||
/// one that renders the new slide and the transition's pictures and the second one starts the
|
||||
/// clock, so a slow render never eats the transition's duration (#1385).
|
||||
fn progress(&mut self, now: f64) -> f64 {
|
||||
if !self.primed {
|
||||
self.primed = true;
|
||||
return 0.0;
|
||||
}
|
||||
let clock = *self.clock.get_or_insert(now);
|
||||
((now - clock) / self.dur.max(0.01)).clamp(0.0, 1.0)
|
||||
}
|
||||
}
|
||||
|
||||
/// A copy of the show's clock origins for regressions that must not depend on wall time.
|
||||
#[cfg(test)]
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
@@ -230,7 +253,7 @@ impl Show {
|
||||
.map(|t| (t.kind.clone(), t.option.clone(), t.duration_ms as f64 / 1000.0))
|
||||
.unwrap_or(("none".into(), String::new(), 0.0));
|
||||
self.trans = if kind != "none" && dur > 0.0 && !doc.show.without_animation {
|
||||
Some(Transition { from, start: now, kind, option, dur, reverse: false })
|
||||
Some(Transition { from, start: now, clock: None, primed: false, kind, option, dur, reverse: false })
|
||||
} else {
|
||||
None
|
||||
};
|
||||
@@ -443,7 +466,7 @@ impl Show {
|
||||
// reversed Morph.
|
||||
let reverse = back && kind != "morph";
|
||||
self.trans = if kind != "none" && dur > 0.0 && !doc.show.without_animation {
|
||||
Some(Transition { from, start: now, kind, option, dur, reverse })
|
||||
Some(Transition { from, start: now, clock: None, primed: false, kind, option, dur, reverse })
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
@@ -493,8 +493,8 @@ fn prepare_transition(
|
||||
store: &TextureStore<FrameKey>,
|
||||
frame: &mut PaintFrame,
|
||||
) {
|
||||
let Some(tr) = &show.trans else { return };
|
||||
let p = ((now - tr.start) / tr.dur.max(0.01)).clamp(0.0, 1.0);
|
||||
let Some(tr) = &mut show.trans else { return };
|
||||
let p = tr.progress(now);
|
||||
if p >= 1.0 {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -625,6 +625,29 @@ fn animations_start_after_the_slides_transition() {
|
||||
assert_eq!(show.clock_snapshot().step_start, 10.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_slow_first_frame_does_not_use_up_the_transition() {
|
||||
// #1385: the clock started at the key press, so rendering the new slide for longer than the
|
||||
// transition's duration showed the next slide with no transition at all.
|
||||
let mut s = deckcraft_engine::Session::with_new();
|
||||
s.execute("slide.new", &json!({})).unwrap();
|
||||
s.execute("transition.set", &json!({"kind": "fade", "index": 1, "duration": 500})).unwrap();
|
||||
let doc = s.active().unwrap().doc.clone();
|
||||
let mut show = Show::new(&doc, 0, false, false);
|
||||
assert!(show.apply(ShowAction::Slide(1), &doc, 10.0));
|
||||
let tr = show.trans.as_mut().expect("the fade plays");
|
||||
assert!((tr.dur - 0.5).abs() < 1e-9);
|
||||
// The first frame renders for two seconds; it still shows the start of the transition.
|
||||
assert_eq!(tr.progress(10.0), 0.0);
|
||||
// The frame after it starts the clock.
|
||||
assert_eq!(tr.progress(12.0), 0.0);
|
||||
assert!((tr.progress(12.25) - 0.5).abs() < 1e-9);
|
||||
assert_eq!(tr.progress(12.5), 1.0);
|
||||
assert_eq!(tr.progress(20.0), 1.0);
|
||||
// A hidden show still expires it from the key press.
|
||||
assert_eq!(tr.start, 10.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn going_back_plays_the_left_slides_transition_reversed() {
|
||||
// #1531: Back plays the transition of the slide being left, in reverse.
|
||||
|
||||
Reference in New Issue
Block a user