-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use browser fs to import/export whole scene
- Loading branch information
1 parent
d861d46
commit 866511e
Showing
12 changed files
with
147 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { RoughGenerator } from "roughjs/bin/generator"; | ||
import { fileOpen } from "browser-fs-access"; | ||
import { Command } from "./types"; | ||
import { | ||
ZagyCanvasElement, | ||
isZagyPortable, | ||
isLine, | ||
isRect, | ||
isText, | ||
isImage, | ||
isHanddrawn, | ||
} from "@/types/general"; | ||
import { useStore } from "@/store/index"; | ||
import { | ||
generateCacheLineElement, | ||
generateCacheRectElement, | ||
generateCachedHandDrawnElement, | ||
generateTextElement, | ||
} from "@/utils/canvas/generateElement"; | ||
|
||
/** | ||
* future version should support loading scene from Z+ as well | ||
*/ | ||
export class ActionOpenScene extends Command { | ||
public async execute() { | ||
const { setSelectedElements, setElements, setPosition, zoomLevel } = useStore.getState(); | ||
try { | ||
const blob = await fileOpen({ | ||
// List of allowed file extensions (with leading '.'), defaults to `''`. | ||
extensions: [".zagydraw"], | ||
// Textual description for file dialog , defaults to `''`. | ||
description: "Open Zagydraw", | ||
// By specifying an ID, the user agent can remember different directories for different IDs. | ||
id: "zagydraw", | ||
}); | ||
const text = await (await blob.handle?.getFile())?.text(); | ||
if (text) { | ||
const portable = JSON.parse(text); | ||
isZagyPortable(portable); | ||
const roughGenerator = new RoughGenerator(); | ||
const elsToPush: ZagyCanvasElement[] = []; | ||
for (const el of portable.elements) { | ||
if (isRect(el)) { | ||
elsToPush.push( | ||
generateCacheRectElement( | ||
roughGenerator, | ||
[el.x, el.y], | ||
[el.endX, el.endY], | ||
zoomLevel, | ||
el.options, | ||
), | ||
); | ||
} else if (isLine(el)) { | ||
elsToPush.push( | ||
generateCacheLineElement( | ||
roughGenerator, | ||
el.point1, | ||
el.point2, | ||
zoomLevel, | ||
el.options, | ||
), | ||
); | ||
} else if (isText(el)) { | ||
elsToPush.push( | ||
generateTextElement(el.text.join("\n"), [el.x, el.y], el.options), | ||
); | ||
} else if (isHanddrawn(el)) { | ||
elsToPush.push( | ||
generateCachedHandDrawnElement(el.paths, zoomLevel, el.options), | ||
); | ||
} else if (isImage(el)) { | ||
// generateImageElement(); | ||
} | ||
|
||
setPosition({ x: 0, y: 0 }); | ||
setSelectedElements(() => []); | ||
setElements(() => [...elsToPush]); | ||
} | ||
} | ||
} catch (e) { | ||
console.log("🪵 [copySelected.ts:15] ~ token ~ \x1b[0;32me\x1b[0m = ", e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters