-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #57 from The-Zagy/import-elements
- Loading branch information
Showing
13 changed files
with
354 additions
and
18 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
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,36 @@ | ||
import { UndoableCommand } from "./types"; | ||
import { useStore } from "@/store/index"; | ||
import { generateImageElement } from "@/utils/canvas/generateElement"; | ||
import { Point, normalizeToGrid } from "@/utils"; | ||
|
||
export class ActionImportElements extends UndoableCommand { | ||
#importedIds: Set<string>; | ||
|
||
constructor(private dataTransfer: DataTransfer, private mouseCoords: Point) { | ||
super(); | ||
this.#importedIds = new Set(); | ||
} | ||
|
||
public execute() { | ||
const { setElements, getPosition } = useStore.getState(); | ||
for (const item of this.dataTransfer.items) { | ||
// if the content is text then we need to check to its structure if we can create ZagyElement from it or not, if not fallback to normal text | ||
// ignore any files that is not image | ||
// TODO branch to every possible item we can create from clipboard | ||
if (item.type.indexOf("image") !== -1) { | ||
const blob = item.getAsFile(); | ||
if (!blob) return; | ||
const norm = normalizeToGrid(getPosition(), this.mouseCoords); | ||
const el = generateImageElement(blob, norm); | ||
this.#importedIds.add(el.id); | ||
setElements((prev) => [...prev, el]); | ||
} | ||
} | ||
return; | ||
} | ||
|
||
public undo() { | ||
const { setElements } = useStore.getState(); | ||
setElements((prev) => prev.filter((i) => !this.#importedIds.has(i.id))); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,4 +1,7 @@ | ||
const CELL_SIZE = 20; | ||
const CACHE_CANVAS_SIZE_THRESHOLD = 10; | ||
|
||
export { CELL_SIZE, CACHE_CANVAS_SIZE_THRESHOLD }; | ||
const PREVIEW_IMAGE_WIDTH = 200; | ||
const PREVIEW_IMAGE_HEIGHT = 200; | ||
|
||
export { CELL_SIZE, CACHE_CANVAS_SIZE_THRESHOLD, PREVIEW_IMAGE_WIDTH, PREVIEW_IMAGE_HEIGHT }; |
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
Oops, something went wrong.