Keep library links working when the project folder is moved or cloned (#2132)

Fixes #2122

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
NetterSilverandClaude Opus 5.5 authored and GitHub committed 2026-11-13 01:48:30 +01:00
1 parent 39acc36438
commit 7b34dbab00
13 files changed
+201 -10

No files matched your search

+1 -1
View File
@@ -41,7 +41,7 @@ use open::append_pages;
pub(crate) use open::open;
use open::{import, open_recent, revert};
pub(crate) use save::save_ends_preview;
pub use save::{FileSnapshot, file_bytes, thumbnail};
pub use save::{FileSnapshot, file_bytes, file_bytes_at, thumbnail};
use save::{color_profile, mark_saved, save, save_as};
pub use version::MAX_AUTOSAVES;
pub(crate) use version::{add_version, keep_in_history, read_version, unique_id};
+3 -1
View File
@@ -75,7 +75,9 @@ pub fn load_bytes(bytes: &[u8], name_hint: &str) -> Result<(Document, Vec<String
pub fn load_path(path: &str, variables: Option<&Value>) -> Result<(figcraft_format::Loaded, bool)> {
let bytes = read(path)?;
let sibling = if variables.is_none() { sibling_variables(path) } else { None };
load_with(&bytes, path, variables.or(sibling.as_ref()), &rest_images(path))
let (mut loaded, rest) = load_with(&bytes, path, variables.or(sibling.as_ref()), &rest_images(path))?;
crate::library::resolve_links(&mut loaded.doc, path);
Ok((loaded, rest))
}
/// Load from bytes. Returns the document with its notes (repairs of a native file, or import
+15 -1
View File
@@ -42,6 +42,11 @@ pub fn file_bytes(ds: &DocState) -> Result<Vec<u8>> {
FileSnapshot::of(ds).encode()
}
/// [`file_bytes`] for the file at `path` (library links are stored relative to its folder too).
pub fn file_bytes_at(ds: &DocState, path: &str) -> Result<Vec<u8>> {
FileSnapshot::of(ds).encode_at(Some(path))
}
/// A document's file as it is at one moment, so the slow part of saving it (the thumbnail and the
/// encoding) can run on another thread while editing goes on: [`encode`](Self::encode) there, then
/// [`save`](Self::save) the bytes back on the session's thread.
@@ -73,8 +78,17 @@ impl FileSnapshot {
/// The file's bytes: content, thumbnail and version history.
pub fn encode(&self) -> Result<Vec<u8>> {
self.encode_at(self.path.as_deref())
}
/// [`encode`](Self::encode) for the file at `path`: library links also say where their
/// files are from its folder.
fn encode_at(&self, path: Option<&str>) -> Result<Vec<u8>> {
let mut doc = (*self.doc).clone();
doc.current_page = Some(self.page);
if let Some(path) = path {
crate::library::relate_links(&mut doc, path);
}
let thumb = thumbnail(&doc);
figcraft_format::save_bytes_with_history(&doc, thumb.as_deref(), &self.versions, &self.branches)
.map_err(|e| EngineError::Other(e.to_string()))
@@ -177,7 +191,7 @@ fn save_to(s: &mut Session, path: Option<String>) -> Result<Value> {
{
Arc::make_mut(&mut snapshot.doc).name = file_stem(path);
}
let bytes = snapshot.encode()?;
let bytes = snapshot.encode_at(path.as_deref().or(snapshot.path.as_deref()))?;
let Some(path) = path else {
// The download can still be refused: the caller marks the file saved once it was delivered.
let name = format!("{}.figcraft", ds.doc.name);
+7 -1
View File
@@ -646,7 +646,13 @@ fn add(s: &mut Session, p: &Value) -> Result<Value> {
l.path = path.clone();
l.name = m.name.clone();
}
None => libs.enabled.push(LibraryLink { id: m.id.clone(), name: m.name.clone(), path: path.clone(), version: m.version }),
None => libs.enabled.push(LibraryLink {
id: m.id.clone(),
name: m.name.clone(),
path: path.clone(),
version: m.version,
relative: String::new(),
}),
}
}
let mut out = assets_json(&lib);