From 80c4e34efd5ce34dfa4c3f0d1e55fd2ecf9d6a4d Mon Sep 17 00:00:00 2001
From: eleanor-park <eleanor_park@brown.edu>
Date: Sun, 8 Dec 2024 00:59:21 -0500
Subject: frontend and image creation working for drawingToImage()

---
 src/client/views/smartdraw/DrawingFillHandler.tsx | 60 ++++++++++++-----------
 1 file changed, 31 insertions(+), 29 deletions(-)

(limited to 'src/client/views/smartdraw/DrawingFillHandler.tsx')

diff --git a/src/client/views/smartdraw/DrawingFillHandler.tsx b/src/client/views/smartdraw/DrawingFillHandler.tsx
index 508ee557d..c3c762181 100644
--- a/src/client/views/smartdraw/DrawingFillHandler.tsx
+++ b/src/client/views/smartdraw/DrawingFillHandler.tsx
@@ -12,37 +12,39 @@ import { ImageUtility } from '../nodes/imageEditor/imageEditorUtils/ImageHandler
 import { OpenWhere } from '../nodes/OpenWhere';
 import { ObservableReactComponent } from '../ObservableReactComponent';
 
-@observer
-export class DrawingFillHandler extends ObservableReactComponent<object> {
-    static Instance: DrawingFillHandler;
-
-    constructor(props: object) {
-        super(props);
-        makeObservable(this);
-        DrawingFillHandler.Instance = this;
-    }
-
-    @action
-    drawingToImage = async (drawing: Doc, prompt: string) => {
+export class DrawingFillHandler {
+    static drawingToImage = async (drawing: Doc, prompt: string) => {
         const imageField = await DocumentView.GetDocImage(drawing);
         if (!imageField) return;
-        const image = new Image();
-        image.src = imageField.url?.href;
-        await new Promise<void>((resolve, reject) => {
-            image.onload = () => resolve();
-            image.onerror = () => reject(new Error('Error loading image'));
-        });
+        const { href } = ImageCast(imageField).url;
+        const hrefParts = href.split('.');
+        const hrefComplete = `${hrefParts[0]}_o.${hrefParts[1]}`;
+        try {
+            const response = await fetch(hrefComplete);
+            const blob: Blob = await response.blob();
+            const strength: number = 100;
+            const img = await Networking.PostToServer('/queryFireflyImage', { prompt, blob, strength });
+            DocumentViewInternal.addDocTabFunc(Docs.Create.ImageDocument(img, {}), OpenWhere.addRight);
+            // Networking.PostToServer('/queryFireflyImage', { prompt, blob, strength }).then(img => DocumentViewInternal.addDocTabFunc(Docs.Create.ImageDocument(img, {}), OpenWhere.addRight));
+        } catch (error) {
+            console.error('Error fetching image:', error);
+            return;
+        }
 
-        const canvas = document.createElement('canvas');
-        canvas.width = image.width;
-        canvas.height = image.height;
-        const ctx = canvas.getContext('2d');
-        if (!ctx) return;
-        ctx.globalCompositeOperation = 'source-over';
-        ctx.clearRect(0, 0, image.width, image.height);
-        ctx.drawImage(image, 0, 0);
-        const blob: Blob = await ImageUtility.canvasToBlob(canvas);
-        const strength: number = 100;
-        Networking.PostToServer('/queryFireflyImage', { prompt, blob, strength }).then(img => DocumentViewInternal.addDocTabFunc(Docs.Create.ImageDocument(img, {}), OpenWhere.addRight));
+        // const image = new Image();
+        // image.src = imageField.url?.href;
+        // // image.onload = async () => {
+        //     const canvas = document.createElement('canvas');
+        //     canvas.width = image.width;
+        //     canvas.height = image.height;
+        //     const ctx = canvas.getContext('2d');
+        //     if (!ctx) return;
+        //     ctx.globalCompositeOperation = 'source-over';
+        //     ctx.clearRect(0, 0, image.width, image.height);
+        //     ctx.drawImage(image, 0, 0);
+        // const blob: Blob = await ImageUtility.canvasToBlob(canvas);
+        // const strength: number = 100;
+        // Networking.PostToServer('/queryFireflyImage', { prompt, blob, strength }).then(img => DocumentViewInternal.addDocTabFunc(Docs.Create.ImageDocument(img, {}), OpenWhere.addRight));
+        // };
     };
 }
-- 
cgit v1.2.3-70-g09d2

