From ae5e526b2e30f6410ba48836fc4812e4c8a22830 Mon Sep 17 00:00:00 2001 From: Skyler Lehmkuhl Date: Thu, 5 Dec 2024 16:54:45 -0500 Subject: [PATCH] Add menu option to import image --- src/main.js | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/src/main.js b/src/main.js index b90d315..14aac31 100644 --- a/src/main.js +++ b/src/main.js @@ -4,7 +4,7 @@ import { Bezier } from "/bezier.js"; import { Quadtree } from './quadtree.js'; import { createNewFileDialog, showNewFileDialog, closeDialog } from './newfile.js'; import { titleCase, getMousePositionFraction, getKeyframesSurrounding, invertPixels, lerpColor, lerp, camelToWords } from './utils.js'; -const { writeTextFile: writeTextFile, readTextFile: readTextFile, writeFile: writeFile }= window.__TAURI__.fs; +const { writeTextFile: writeTextFile, readTextFile: readTextFile, writeFile: writeFile, readFile: readFile }= window.__TAURI__.fs; const { open: openFileDialog, save: saveFileDialog, @@ -1918,6 +1918,25 @@ async function open() { } } +async function importImage() { + const path = await openFileDialog({ + multiple: false, + directory: false, + filters: [ + { + name: 'Image files', + extensions: ['png', 'gif', 'avif', 'jpg', 'jpeg'], + }, + ], + defaultPath: await documentDir(), + }); + if (path) { + const dataURL = await convertToDataURL(path); + actions.addImageObject.create(50, 50, dataURL, 0, context.activeObject) + } + +} + async function quit() { if (undoStack.length) { if (await confirmDialog("Are you sure you want to quit?", {title: 'Really quit?', kind: "warning"})) { @@ -2928,6 +2947,11 @@ async function updateMenu() { enabled: true, action: open, }, + { + text: 'Import Image...', + enabled: true, + action: importImage, + }, { text: 'Quit', enabled: true, @@ -3097,13 +3121,13 @@ const panes = { async function convertToDataURL(filePath) { try { // Read the image file as a binary file (buffer) - const binaryData = await readBinaryFile(filePath); + const binaryData = await readFile(filePath); const mimeType = getMimeType(filePath); if (!mimeType) { throw new Error('Unsupported image type'); } - const base64Data = binaryData.toString('base64'); + const base64Data = btoa(String.fromCharCode(...binaryData)) const dataURL = `data:${mimeType};base64,${base64Data}`; return dataURL;