Skip to content

Commit 8892129

Browse files
committed
Use document name for saved and exported files
We pass through the document name in the export and save messages. Additionally, we check if the appropriate file suffixes (.graphite and .svg) need to be added before passing it to the frontend.
1 parent 3d7d502 commit 8892129

File tree

5 files changed

+36
-12
lines changed

5 files changed

+36
-12
lines changed

editor/src/consts.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,5 @@ pub const LINE_ROTATE_SNAP_ANGLE: f64 = 15.;
2020
pub const SELECTION_TOLERANCE: f64 = 1.0;
2121

2222
pub const DEFAULT_DOCUMENT_NAME: &str = "Untitled Document";
23+
pub const FILE_SAVE_SUFFIX: &str = ".graphite";
24+
pub const FILE_EXPORT_SUFFIX: &str = ".svg";

editor/src/document/document_file.rs

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
pub use super::layer_panel::*;
2-
use crate::{frontend::layer_panel::*, EditorError};
2+
use crate::{
3+
consts::{FILE_EXPORT_SUFFIX, FILE_SAVE_SUFFIX},
4+
frontend::layer_panel::*,
5+
EditorError,
6+
};
37
use glam::{DAffine2, DVec2};
48
use graphene::{document::Document as InternalDocument, DocumentError, LayerId};
59
use serde::{Deserialize, Serialize};
@@ -282,6 +286,10 @@ impl MessageHandler<DocumentMessage, &InputPreprocessor> for DocumentMessageHand
282286
ExportDocument => {
283287
let bbox = self.document.visible_layers_bounding_box().unwrap_or([DVec2::ZERO, ipp.viewport_size.as_f64()]);
284288
let size = bbox[1] - bbox[0];
289+
let name = match self.name.ends_with(FILE_SAVE_SUFFIX) {
290+
true => self.name.clone().replace(FILE_SAVE_SUFFIX, FILE_EXPORT_SUFFIX),
291+
false => self.name.clone() + FILE_EXPORT_SUFFIX,
292+
};
285293
responses.push_back(
286294
FrontendMessage::ExportDocument {
287295
document: format!(
@@ -293,16 +301,24 @@ impl MessageHandler<DocumentMessage, &InputPreprocessor> for DocumentMessageHand
293301
"\n",
294302
self.document.render_root()
295303
),
304+
name,
305+
}
306+
.into(),
307+
)
308+
}
309+
SaveDocument => {
310+
let name = match self.name.ends_with(FILE_SAVE_SUFFIX) {
311+
true => self.name.clone(),
312+
false => self.name.clone() + FILE_SAVE_SUFFIX,
313+
};
314+
responses.push_back(
315+
FrontendMessage::SaveDocument {
316+
document: self.document.serialize_document(),
317+
name,
296318
}
297319
.into(),
298320
)
299321
}
300-
SaveDocument => responses.push_back(
301-
FrontendMessage::SaveDocument {
302-
document: self.document.serialize_document(),
303-
}
304-
.into(),
305-
),
306322
SetBlendModeForSelectedLayers(blend_mode) => {
307323
let active_document = self;
308324

editor/src/frontend/frontend_message_handler.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ pub enum FrontendMessage {
1818
DisplayConfirmationToCloseAllDocuments,
1919
UpdateCanvas { document: String },
2020
UpdateLayer { path: Vec<LayerId>, data: LayerPanelEntry },
21-
ExportDocument { document: String },
22-
SaveDocument { document: String },
21+
ExportDocument { document: String, name: String },
22+
SaveDocument { document: String, name: String },
2323
EnableTextInput,
2424
DisableTextInput,
2525
UpdateWorkingColors { primary: Color, secondary: Color },

frontend/src/components/panels/Document.vue

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -332,11 +332,13 @@ export default defineComponent({
332332
});
333333
registerResponseHandler(ResponseType.ExportDocument, (responseData: Response) => {
334334
const updateData = responseData as ExportDocument;
335-
if (updateData) this.download("canvas.svg", updateData.document);
335+
if (!updateData) return;
336+
this.download(updateData.name, updateData.document);
336337
});
337338
registerResponseHandler(ResponseType.SaveDocument, (responseData: Response) => {
338339
const saveData = responseData as SaveDocument;
339-
if (saveData) this.download("canvas.graphite", saveData.document);
340+
if (!saveData) return;
341+
this.download(saveData.name, saveData.document);
340342
});
341343
registerResponseHandler(ResponseType.SetActiveTool, (responseData: Response) => {
342344
const toolData = responseData as SetActiveTool;

frontend/src/utilities/response-handler.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,19 +169,23 @@ function newUpdateCanvas(input: any): UpdateCanvas {
169169

170170
export interface ExportDocument {
171171
document: string;
172+
name: string;
172173
}
173-
function newExportDocument(input: any): UpdateCanvas {
174+
function newExportDocument(input: any): ExportDocument {
174175
return {
175176
document: input.document,
177+
name: input.name,
176178
};
177179
}
178180

179181
export interface SaveDocument {
180182
document: string;
183+
name: string;
181184
}
182185
function newSaveDocument(input: any): SaveDocument {
183186
return {
184187
document: input.document,
188+
name: input.name,
185189
};
186190
}
187191

0 commit comments

Comments
 (0)