feat(anatomy): view.canvas op on the loaded sheet; drawings read once at load
This commit is contained in:
1 parent
1ec9316a73
commit
842797b440
5 files changed
+168
No files matched your search
@@ -7,6 +7,8 @@ use serde_json::Value;
|
||||
#[cfg(feature = "ts")]
|
||||
use ts_rs::TS;
|
||||
|
||||
use crate::anatomy::compose::{ComposeError, asset_id_of_view, compose_canvas};
|
||||
use crate::anatomy::svg::serialize::serialize;
|
||||
use crate::domain::archive::build_sheet_snapshot;
|
||||
use crate::domain::editor::{
|
||||
self, EditorError, LeafClick, MoveDirection, NewReportDraft, NewReportStamp, VertebraAction,
|
||||
@@ -215,6 +217,7 @@ pub fn on_sheet(sheet: &LoadedSheet, op: &str, input: &str) -> Option<Outcome> {
|
||||
"draft.restore" => draft_restore(sheet, input),
|
||||
"archive.import" => archive_import(sheet, input),
|
||||
"archive.snapshot" => archive_snapshot(sheet, input),
|
||||
"view.canvas" => view_canvas(sheet, input),
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
@@ -339,3 +342,47 @@ fn archive_snapshot(sheet: &LoadedSheet, input: &str) -> Outcome {
|
||||
let input: SnapshotInput = parse_input(input)?;
|
||||
to_value(&build_sheet_snapshot(sheet, &input.report))
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Anatomy (R3)
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[cfg_attr(feature = "ts", derive(TS), ts(export))]
|
||||
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
||||
pub struct CanvasViewInput {
|
||||
pub view_id: String,
|
||||
}
|
||||
|
||||
/// The editor canvas of one view, as SVG text the browser parses.
|
||||
#[derive(serde::Serialize)]
|
||||
#[cfg_attr(feature = "ts", derive(TS), ts(export))]
|
||||
pub struct CanvasView {
|
||||
pub svg: String,
|
||||
}
|
||||
|
||||
/// A composed view larger than this is refused (plan R3 C2: output bounded).
|
||||
pub const MAX_COMPOSED_BYTES: usize = 8 * 1024 * 1024;
|
||||
|
||||
fn view_canvas(sheet: &LoadedSheet, input: &str) -> Outcome {
|
||||
let input: CanvasViewInput = parse_input(input)?;
|
||||
let document = &sheet.document;
|
||||
if !document.views.iter().any(|v| v.id() == input.view_id) {
|
||||
return Err(Failure::new("unknown_view", &input.view_id));
|
||||
}
|
||||
let asset_id = asset_id_of_view(document, &input.view_id)
|
||||
.ok_or_else(|| Failure::new("no_asset", &input.view_id))?;
|
||||
let drawing = match sheet.drawing(asset_id) {
|
||||
None => return Err(Failure::new("no_asset", asset_id)),
|
||||
Some(Err(reason)) => return Err(Failure::new("svg_rejected", reason)),
|
||||
Some(Ok(drawing)) => drawing,
|
||||
};
|
||||
let canvas = compose_canvas(document, &input.view_id, drawing).map_err(|e| match e {
|
||||
ComposeError::UnknownView => Failure::new("unknown_view", &input.view_id),
|
||||
ComposeError::InvalidCharacter => Failure::new("invalid_character", &input.view_id),
|
||||
})?;
|
||||
let svg = serialize(&canvas);
|
||||
if svg.len() > MAX_COMPOSED_BYTES {
|
||||
return Err(Failure::new("svg_rejected", "too_large"));
|
||||
}
|
||||
to_value(&CanvasView { svg })
|
||||
}
|
||||
Reference in new issue
Block a user