diff options
author | bobzel <zzzman@gmail.com> | 2025-08-14 13:02:53 -0400 |
---|---|---|
committer | bobzel <zzzman@gmail.com> | 2025-08-14 13:02:53 -0400 |
commit | cabd82bea8eb4c69f60c8b5d4f987cf484123ecd (patch) | |
tree | 00430ea11b9eaa0dc032a8f7941849bbb9e0bf44 /src/client/views/nodes/ImageBox.tsx | |
parent | 467f1c2543626a50d48c84669cd408571260f147 (diff) |
moved background removal code into ImageUtils
Diffstat (limited to 'src/client/views/nodes/ImageBox.tsx')
-rw-r--r-- | src/client/views/nodes/ImageBox.tsx | 61 |
1 files changed, 14 insertions, 47 deletions
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)); } }; |