From cabd82bea8eb4c69f60c8b5d4f987cf484123ecd Mon Sep 17 00:00:00 2001
From: bobzel <zzzman@gmail.com>
Date: Thu, 14 Aug 2025 13:02:53 -0400
Subject: moved background removal code into ImageUtils

---
 src/client/views/nodes/ImageBox.tsx | 61 +++++++++----------------------------
 1 file changed, 14 insertions(+), 47 deletions(-)

(limited to 'src/client/views/nodes/ImageBox.tsx')

diff --git a/src/client/views/nodes/ImageBox.tsx b/src/client/views/nodes/ImageBox.tsx
index cc747eb32..c4a731078 100644
--- a/src/client/views/nodes/ImageBox.tsx
+++ b/src/client/views/nodes/ImageBox.tsx
@@ -46,6 +46,7 @@ import { FieldView, FieldViewProps } from './FieldView';
 import { FocusViewOptions } from './FocusViewOptions';
 import './ImageBox.scss';
 import { OpenWhere } from './OpenWhere';
+import { ImageUtils } from '../../util/Import & Export/ImageUtils';
 
 const DefaultPath = '/assets/unknown-file-icon-hi.png';
 export class ImageEditorData {
@@ -388,56 +389,22 @@ export class ImageBox extends ViewBoxAnnotatableComponent<FieldViewProps>() {
         this._props.bringToFront?.(cropping);
         return cropping;
     };
-
-    static _worker?: Worker;
-    static removeImgBackground = (doc: Doc, addDoc: (doc: Doc | Doc[], annotationKey?: string) => boolean, docImgPath: string) => {
-        ImageEditorData.AddDoc = addDoc;
-        ImageEditorData.RootDoc = doc;
-        if (ImageBox._worker) return ImageBox._worker;
-        const worker = new Worker('/image.worker.js', { type: 'module' });
-        worker.onmessage = async (event: MessageEvent) => {
-            const { success, result, error } = event.data;
-            if (success) {
-                const blobToDataURL = (blob: any) => {
-                    return new Promise<string | ArrayBuffer | null>((resolve, reject) => {
-                        const reader = new FileReader();
-                        reader.onload = () => resolve(reader.result);
-                        reader.onerror = error => reject(error);
-                        reader.readAsDataURL(blob);
-                    });
-                };
-                blobToDataURL(result).then(durl => {
-                    ClientUtils.convertDataUri(durl as string, doc[Id] + '_noBgd').then(url => {
-                        const width = NumCast(doc._width) || 1;
-                        const height = NumCast(doc._height);
-                        const imageSnapshot = Docs.Create.ImageDocument(url, {
-                            _nativeWidth: Doc.NativeWidth(doc),
-                            _nativeHeight: Doc.NativeHeight(doc),
-                            x: NumCast(doc.x) + width,
-                            y: NumCast(doc.y),
-                            _width: 150,
-                            _height: (height / width) * 150,
-                            title: 'bgremoved:' + doc.title,
-                        });
-                        Doc.SetNativeWidth(imageSnapshot[DocData], Doc.NativeWidth(doc));
-                        Doc.SetNativeHeight(imageSnapshot[DocData], Doc.NativeHeight(doc));
-                        addDoc?.(imageSnapshot);
-                    });
-                });
-            } else {
-                console.error('Error in background removal:', error);
-            }
-            // worker.terminate();
-        };
-        worker.onerror = (e: ErrorEvent) => console.error('Worker failed:', e); //  worker.terminate();
-
-        worker.postMessage({ imagePath: docImgPath });
-        return worker;
-    };
     removeBackground = () => {
         const field = ImageCast(this.dataDoc[this.fieldKey]);
         if (field && this._props.addDocument) {
-            ImageBox.removeImgBackground(this.rootDoc, this._props.addDocument, this.choosePath(field.url));
+            const doc = this.Document;
+            const width = NumCast(doc._width) || 1;
+            const height = NumCast(doc._height);
+            const options ={
+                _nativeWidth: Doc.NativeWidth(doc),
+                _nativeHeight: Doc.NativeHeight(doc),
+                x: NumCast(doc.x) + width,
+                y: NumCast(doc.y),
+                _width: 150,
+                _height: (height / width) * 150,
+                title: 'bgremoved:' + doc.title,
+            }
+            ImageUtils.removeImgBackground(options, this._props.addDocument, this.Document[Id], Doc.NativeWidth(doc), Doc.NativeHeight(doc), this.choosePath(field.url));
         }
     };
 
-- 
cgit v1.2.3-70-g09d2

