Skip to content

Commit

Permalink
Merge pull request #90 from center-for-threat-informed-defense/AF-26_…
Browse files Browse the repository at this point in the history
…auto_save

AF-26 Autosave
  • Loading branch information
mikecarenzo committed Jul 24, 2023
2 parents f2bb214 + c101c85 commit eb2b02d
Show file tree
Hide file tree
Showing 22 changed files with 253 additions and 172 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ export default defineComponent({
*/
onCanvasClick(e: PointerEvent, x: number, y: number) {
this.execute(new Page.UnselectDescendants(this.editor.page));
this.execute(new App.SetEditorPointerLocation(this.ctx, this.editor.id, x, y));
this.execute(new App.SetEditorPointerLocation(this.ctx, x, y));
if (e.button === MouseClick.Right) {
this.openContextMenu(x, y);
}
Expand Down Expand Up @@ -366,7 +366,7 @@ export default defineComponent({
this.view = { x, y, k, w, h };
this.execute(
new App.SetEditorViewParams(
this.ctx, this.editor.id, { ...this.view }
this.ctx, { ...this.view }
)
);
}
Expand All @@ -383,7 +383,7 @@ export default defineComponent({
// Configure view parameters
this.execute(
new App.SetEditorViewParams(
this.ctx, this.editor.id, { ...this.view }
this.ctx, { ...this.view }
)
);
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { AppCommand } from "../AppCommand";
import { ApplicationStore } from "@/store/StoreTypes";

export class ClearPageRecoveryBank extends AppCommand {

/**
* Clears the application's page recovery bank.
* @param context
* The application context.
*/
constructor(context: ApplicationStore) {
super(context);
}


/**
* Executes the command.
*/
public execute(): void {
for(let id of this._context.recoveryBank.pages.keys()) {
// Clear everything except the active page
if(id === this._context.activePage.id) {
continue;
}
this._context.recoveryBank.withdrawEditor(id);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,6 @@ export class LoadFile extends AppCommand {
* Executes the command.
*/
public execute(): void {
// NOTE: For now, only one page will be loaded at a time.
this._context.pages.clear();
this._context.pages.set(this._editor.id, this._editor);
this._context.activePage = this._editor;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +1,21 @@
import { AppCommand } from "../AppCommand";
import { ApplicationStore } from "@/store/StoreTypes";
import { Browser } from "@/assets/scripts/Browser";
import { PageEditor } from "@/store/PageEditor";

export class PublishPageToDevice extends AppCommand {

/**
* The page's editor.
*/
private _editor: PageEditor;


/**
* Publishes a page to the user's file system.
* @param context
* The application context.
* @param id
* The id of the page.
*/
constructor(context: ApplicationStore, id: string) {
constructor(context: ApplicationStore) {
super(context);
let editor = context.pages.get(id);
if(!editor) {
throw new Error(`Page '${ id }' not found.`);
} else if(!editor.isValid()) {
throw new Error(`Page '${ id }' is not valid.`);
let editor = context.activePage;
if(!editor.isValid()) {
throw new Error(`Page '${ editor.id }' is not valid.`);
} else if(!this._context.publisher) {
throw new Error(`App is not configured with a publisher.`);
} else {
this._editor = editor;
}
}

Expand All @@ -37,9 +24,10 @@ export class PublishPageToDevice extends AppCommand {
* Executes the command.
*/
public execute(): void {
let editor = this._context.activePage;
Browser.downloadTextFile(
this._editor.page.props.toString(),
this._context.publisher!.publish(this._editor.page),
editor.page.props.toString(),
this._context.publisher!.publish(editor.page),
"json"
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,35 @@ import { AppCommand } from "../AppCommand";
import { ApplicationStore } from "@/store/StoreTypes";
import { Browser } from "@/assets/scripts/Browser";
import { PageImage } from "@/assets/scripts/BlockDiagram/PageImage";
import { PageEditor } from "@/store/PageEditor";

export class SavePageImageToDevice extends AppCommand {

/**
* The page's editor.
*/
private _editor: PageEditor;


/**
* Saves a page as an image to the user's file system.
* @param context
* The application context.
* @param id
* The id of the page.
*/
constructor(context: ApplicationStore, id: string) {
constructor(context: ApplicationStore) {
super(context);
let editor = context.pages.get(id);
if(!editor) {
throw new Error(`Page '${ id }' not found.`);
} else {
this._editor = editor;
}
}


/**
* Executes the command.
*/
public execute(): void {
let editor = this._context.activePage;
let d = this._context.settings.view.diagram;
let e = this._context.settings.file.image_export;
let image = new PageImage(
this._editor.page,
editor.page,
e.padding,
d.display_grid,
d.display_shadows,
d.display_debug_mode
);
Browser.downloadImageFile(
this._editor.page.props.toString(),
editor.page.props.toString(),
image.capture()
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,32 @@ import Configuration from "@/assets/builder.config";
import { AppCommand } from "../AppCommand";
import { ApplicationStore } from "@/store/StoreTypes";
import { Browser } from "@/assets/scripts/Browser";
import { PageEditor } from "@/store/PageEditor";

export class SavePageToDevice extends AppCommand {

/**
* The page's editor.
*/
private _editor: PageEditor;


/**
* Saves a page to the user's file system.
* @param context
* The application context.
* @param id
* The id of the page.
*/
constructor(context: ApplicationStore, id: string) {
constructor(context: ApplicationStore) {
super(context);
let editor = context.pages.get(id);
if(!editor) {
throw new Error(`Page '${ id }' not found.`);
} else {
this._editor = editor;
}
}


/**
* Executes the command.
*/
public execute(): void {
let editor = this._context.activePage;
// Download page
Browser.downloadTextFile(
this._editor.page.props.toString(),
this._editor.toFile(),
editor.page.props.toString(),
editor.toFile(),
Configuration.file_type_extension
);
// Withdraw progress from recovery bank
this._context.recoveryBank.withdrawEditor(editor.id);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,10 @@ import { AppCommand } from "../AppCommand";
import { ApplicationStore } from "@/store/StoreTypes";
import { Browser } from "@/assets/scripts/Browser";
import { PageImage } from "@/assets/scripts/BlockDiagram/PageImage";
import { PageEditor } from "@/store/PageEditor";
import { DiagramObjectModel } from "@/assets/scripts/BlockDiagram";

export class SaveSelectionImageToDevice extends AppCommand {

/**
* The page's editor.
*/
private _editor: PageEditor;

/**
* The objects to capture.
*/
Expand All @@ -22,17 +16,10 @@ export class SaveSelectionImageToDevice extends AppCommand {
* Saves a page's selection as an image to the user's file system.
* @param context
* The application context.
* @param id
* The id of the page.
*/
constructor(context: ApplicationStore, id: string) {
constructor(context: ApplicationStore) {
super(context);
let editor = context.pages.get(id);
if(!editor) {
throw new Error(`Page '${ id }' not found.`);
} else {
this._editor = editor;
}
let editor = context.activePage;
this._objects = [...editor.page.getSubtree(o => o.isSelected())];
}

Expand All @@ -41,17 +28,18 @@ export class SaveSelectionImageToDevice extends AppCommand {
* Executes the command.
*/
public execute(): void {
let editor = this._context.activePage;
let d = this._context.settings.view.diagram;
let e = this._context.settings.file.image_export;
let image = new PageImage(
this._editor.page,
editor.page,
e.padding,
d.display_grid,
d.display_shadows,
d.display_debug_mode
);
Browser.downloadImageFile(
this._editor.page.props.toString(),
editor.page.props.toString(),
image.capture(this._objects)
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
import { AppCommand } from "../AppCommand";
import { ApplicationStore } from "@/store/StoreTypes";
import { PageEditor } from "@/store/PageEditor";

export class SetEditorPointerLocation extends AppCommand {

/**
* The editor.
*/
private _editor: PageEditor;

/**
* The pointer's x location.
*/
Expand All @@ -24,21 +18,13 @@ export class SetEditorPointerLocation extends AppCommand {
* Sets a page editor's pointer location.
* @param context
* The application context.
* @param id
* The id of the page.
* @param x
* The pointer's x location.
* @param y
* The pointer's y location.
*/
constructor(context: ApplicationStore, id: string, x: number, y: number) {
constructor(context: ApplicationStore, x: number, y: number) {
super(context);
let editor = context.pages.get(id);
if(!editor) {
throw new Error(`Page '${ id }' not found.`);
} else {
this._editor = editor;
}
this._x = x;
this._y = y;
}
Expand All @@ -48,7 +34,7 @@ export class SetEditorPointerLocation extends AppCommand {
* Executes the command.
*/
public execute(): void {
this._editor.pointer.value = { x: this._x, y: this._y };
this._context.activePage.pointer.value = { x: this._x, y: this._y };
}

}
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import { AppCommand } from "../AppCommand";
import { ApplicationStore } from "@/store/StoreTypes";
import { EditorViewParameters, PageEditor } from "@/store/PageEditor";
import { EditorViewParameters } from "@/store/PageEditor";

export class SetEditorViewParams extends AppCommand {

/**
* The editor.
*/
private _editor: PageEditor;

/**
* The new front-end view parameters.
*/
Expand All @@ -19,19 +14,11 @@ export class SetEditorViewParams extends AppCommand {
* Sets a page editor's front-end view parameters.
* @param context
* The application context.
* @param id
* The id of the page.
* @param params
* The new front-end view parameters.
*/
constructor(context: ApplicationStore, id: string, params: EditorViewParameters) {
constructor(context: ApplicationStore, params: EditorViewParameters) {
super(context);
let editor = context.pages.get(id);
if(!editor) {
throw new Error(`Page '${ id }' not found.`);
} else {
this._editor = editor;
}
this._params = params;
}

Expand All @@ -40,7 +27,7 @@ export class SetEditorViewParams extends AppCommand {
* Executes the command.
*/
public execute(): void {
this._editor.view = this._params;
this._context.activePage.view = this._params;
}

}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./ClearPageRecoveryBank";
export * from "./CopySelectedChildren";
export * from "./LoadFile";
export * from "./LoadSettings";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,7 @@ export class MoveCameraToObjects extends PageCommand {
`Objects must originate from the same root.`
);
}
let editor = context.pages.get(page.id);
if(!editor) {
throw new Error(
`Objects must be managed by an editor.`
);
}
let editor = context.activePage;
super(page.id);
this._editor = editor;
// Calculate bounding box
Expand Down
Loading

0 comments on commit eb2b02d

Please sign in to comment.