Skip to content

Commit

Permalink
Saving file
Browse files Browse the repository at this point in the history
  • Loading branch information
mateuszmigas committed Mar 6, 2024
1 parent fb9f0eb commit 7a23086
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 18 deletions.
2 changes: 1 addition & 1 deletion apps/web/src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ export const executeCommand = async (
throw new Error(`Command not found: ${name}`);
}

return command.execute(context, params);
return command.execute(context, params as never);
};

13 changes: 10 additions & 3 deletions apps/web/src/commands/saveAsCurrentWorkspace.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
import { downloadAsFile } from "@/utils/html";
import { CommandContext } from "./context";

export const command = {
name: "saveAsCurrentWorkspace",
name: "saveCurrentWorkspaceAsFile",
description: "Save the current workspace as a new file",
execute: async (
context: CommandContext,
_: CommandContext,
payload: {
workspaceId: string;
layerName: string;
format: "jpeg" | "png";
}
) => {
console.log("Saving sheet", context, payload);
const { format } = payload;
const canvas = document.querySelector(
"#canvas-renderer"
) as HTMLCanvasElement;
const data = canvas.toDataURL(`image/${format}`, 1.0);
downloadAsFile(data, `picture.${format}`);
},
} as const;

8 changes: 4 additions & 4 deletions apps/web/src/components/main/appHeaderBar.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { ModeToggle } from "../themeToggle";
import { MenuBar } from "../menu-bar/menuBar";
import { ScrollArea, ScrollBar } from "../ui/scroll-area";
import { IconButton } from "../iconButton";
import { Button } from "../ui/button";
import { cn } from "@/utils/css";
import { useWorkspacesStore } from "@/store";
Expand Down Expand Up @@ -31,8 +30,9 @@ const WorkspaceTab = (props: {
};

export const AppHeaderBar = memo(() => {
const { workspaces, selectedWorkspaceIndex, addWorkspace } =
useWorkspacesStore((state) => state);
const { workspaces, selectedWorkspaceIndex } = useWorkspacesStore(
(state) => state
);

return (
<div className="border-b flex flex-row justify-between items-center px-small gap-big">
Expand All @@ -51,7 +51,7 @@ export const AppHeaderBar = memo(() => {
</div>
<ScrollBar orientation="horizontal" />
</ScrollArea>
<IconButton type="plus" size="medium" onClick={addWorkspace} />
{/* <IconButton type="plus" size="medium" onClick={addWorkspace} /> */}
</div>
<ModeToggle />
</div>
Expand Down
36 changes: 27 additions & 9 deletions apps/web/src/components/menu-bar/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,34 @@ export const menuBarDefinition: MenuItem[] = [
label: "File",
items: [
{
type: "leaf",
label: "Save As",
action: {
onClick: () => {
executeCommand("saveAsCurrentWorkspace", {
workspaceId: "123",
layerName: "test",
});
type: "parent",
label: "Save As...",
items: [
{
type: "leaf",
label: "JPEG Picture",
action: {
onClick: () =>
executeCommand("saveCurrentWorkspaceAsFile", {
workspaceId: "1",
layerName: "1",
format: "jpeg",
}),
},
},
},
{
type: "leaf",
label: "PNG Picture",
action: {
onClick: () =>
executeCommand("saveCurrentWorkspaceAsFile", {
workspaceId: "1",
layerName: "1",
format: "png",
}),
},
},
],
},
{
type: "separator",
Expand Down
6 changes: 5 additions & 1 deletion apps/web/src/hooks/useCanvasRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,25 @@ export const useCanvasRenderer = (

const hostElement = hostElementRef.current;
const canvas = document.createElement("canvas");
canvas.id = "canvas-renderer";
canvas.width = size.width;
canvas.height = size.height;
canvas.style.width = `${size.width}px`;
canvas.style.height = `${size.height}px`;
canvas.className =
"pixelated-canvas bg-gray-100 pointer-events-none origin-top-left shadow-sm";
"pixelated-canvas pointer-events-none origin-top-left shadow-2xl border";
hostElement.appendChild(canvas);
canvasRef.current = canvas;
const offscreenCanvas = canvasRef.current?.transferControlToOffscreen();
const context = offscreenCanvas?.getContext("2d");

if (!context) {
throw new Error("Canvas context is null");
}

contextRef.current = context;
context.fillStyle = "white";
context.fillRect(0, 0, size.width, size.height);

return () => {
hostElement.removeChild(canvas);
Expand Down
7 changes: 7 additions & 0 deletions apps/web/src/utils/html.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export const downloadAsFile = (data: string, filename: string) => {
var anchor = document.createElement("a");
anchor.href = data;
anchor.download = filename;
anchor.click();
};

0 comments on commit 7a23086

Please sign in to comment.