Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
leowrites committed Nov 30, 2024
1 parent ac4bea6 commit ab4089d
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 41 deletions.
52 changes: 52 additions & 0 deletions memory-viz/src/memory_model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@ import {
Rect,
Primitive,
Style,
DisplaySettings,
Size,
} from "./types";
import { isArrayOfType } from "./typeguards";
import { RoughSVG } from "roughjs/bin/svg";
import { Config, Options } from "roughjs/bin/core";
import type * as fsType from "fs";
import type * as CSS from "csstype";
import { getSize } from "./automate";

// Dynamic import of Node fs module
let fs: typeof fsType | undefined;
Expand Down Expand Up @@ -950,4 +953,53 @@ export class MemoryModel {

return sizes_arr;
}

/**
* Return the dimension of the canvas.
* @param configuration: The configuration of the canvas.
* @param snapshotObjects: The objects that will be on the canvas.
*/
static getCanvasDimensions(
configuration: Partial<DisplaySettings>,
snapshotObjects: DrawnEntity[]
): Size {
// Dynamically determining the width of the canvas, in case one has not been provided.
const size: Size = {
width: configuration.width,
height: configuration.height,
};
if (!configuration.hasOwnProperty("width")) {
let rightmost_obj;
let rightmost_edge = 0;

for (const obj of snapshotObjects) {
const width = getSize(obj).width;
const curr_edge = obj.x + width;
if (curr_edge > rightmost_edge) {
rightmost_edge = curr_edge;
rightmost_obj = obj;
}
}
size.width = rightmost_edge + 100;
}

// Dynamically determining the height of the canvas, in case one has not been provided.
if (!configuration.hasOwnProperty("height")) {
let downmost_obj = snapshotObjects[0];
let downmost_edge = 0;

for (const obj of snapshotObjects) {
const height = getSize(obj).height;
const curr_edge = obj.y + height;

if (curr_edge > downmost_edge) {
downmost_obj = obj;
downmost_edge = obj.y + height;
}
}

size.height = downmost_edge + 100;
}
return size;
}
}
44 changes: 3 additions & 41 deletions memory-viz/src/user_functions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { MemoryModel } from "./memory_model";
import { drawAutomated, getSize } from "./automate";
import { drawAutomated } from "./automate";
import { DrawnEntity, DisplaySettings } from "./types";
import type * as fsType from "fs";
export * from "./types";
Expand Down Expand Up @@ -59,7 +59,8 @@ function draw(
const isArrayOfArrays = Array.isArray(objs) && Array.isArray(objs[0]);

const processSnapshot = (snapshotObjects: DrawnEntity[]) => {
getCanvasDimensions(configuration, snapshotObjects);
({ width: configuration.width, height: configuration.height } =
MemoryModel.getCanvasDimensions(configuration, snapshotObjects));
const model = new MemoryModel({
width: configuration.width,
height: configuration.height,
Expand Down Expand Up @@ -88,43 +89,4 @@ function draw(
: processSnapshot(snapshotObjects);
}

function getCanvasDimensions(
configuration: Partial<DisplaySettings>,
snapshotObjects: DrawnEntity[]
): void {
// Dynamically determining the width of the canvas, in case one has not been provided.
if (!configuration.hasOwnProperty("width")) {
let rightmost_obj;
let rightmost_edge = 0;

for (const obj of snapshotObjects) {
const width = getSize(obj).width;
const curr_edge = obj.x + width;
if (curr_edge > rightmost_edge) {
rightmost_edge = curr_edge;
rightmost_obj = obj;
}
}
configuration.width = rightmost_edge + 100;
}

// Dynamically determining the height of the canvas, in case one has not been provided.
if (!configuration.hasOwnProperty("height")) {
let downmost_obj = snapshotObjects[0];
let downmost_edge = 0;

for (const obj of snapshotObjects) {
const height = getSize(obj).height;
const curr_edge = obj.y + height;

if (curr_edge > downmost_edge) {
downmost_obj = obj;
downmost_edge = obj.y + height;
}
}

configuration.height = downmost_edge + 100;
}
}

export { draw };

0 comments on commit ab4089d

Please sign in to comment.