diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/client/views/nodes/scrapbook/ScrapbookBox.tsx | 69 |
1 files changed, 42 insertions, 27 deletions
diff --git a/src/client/views/nodes/scrapbook/ScrapbookBox.tsx b/src/client/views/nodes/scrapbook/ScrapbookBox.tsx index 593c0ac63..d67f9b7a2 100644 --- a/src/client/views/nodes/scrapbook/ScrapbookBox.tsx +++ b/src/client/views/nodes/scrapbook/ScrapbookBox.tsx @@ -3,16 +3,19 @@ import * as React from 'react'; import { Doc, DocListCast, StrListCast } from '../../../../fields/Doc'; import { List } from '../../../../fields/List'; import { emptyFunction } from '../../../../Utils'; +import axios from 'axios'; import { Docs } from '../../../documents/Documents'; import { DocumentType } from '../../../documents/DocumentTypes'; import { CollectionView } from '../../collections/CollectionView'; import { ViewBoxAnnotatableComponent } from '../../DocComponent'; +import { AspectRatioLimits } from '../../smartdraw/FireflyConstants'; import { DocumentView } from '../DocumentView'; import { FieldView, FieldViewProps } from '../FieldView'; import { DragManager } from '../../../util/DragManager'; import { RTFCast, StrCast, toList } from '../../../../fields/Types'; import { undoable } from '../../../util/UndoManager'; import ReactLoading from 'react-loading'; +import { NumCast } from '../../../../fields/Types'; import { ScrapbookItemConfig, ScrapbookPreset } from './ScrapbookPreset'; import { ImageBox } from '../ImageBox'; import { FireflyImageDimensions } from '../../smartdraw/FireflyConstants'; @@ -431,39 +434,49 @@ export class ScrapbookBox extends ViewBoxAnnotatableComponent<FieldViewProps>() } - async generateAiImageCorrect(prompt? : string) { - this.loading = true; - - if(!prompt){ - prompt = 'A serene mountain landscape at sunrise, ultra-wide, pastel sky, abstract, scrapbook background'; - } - const dimensions = FireflyImageDimensions.Square; - SmartDrawHandler.CreateWithFirefly(prompt, dimensions) - .then(action(doc => { + + @action + async generateAiImageCorrect(prompt?: string) { + this.loading = true; + try { + // 1) Default to regenPrompt if none provided + if (!prompt) prompt = this.regenPrompt; + + // 2) Measure the scrapbook’s current size + const w = NumCast(this.layoutDoc._width, 1); + const h = NumCast(this.layoutDoc._height, 1); + const ratio = w / h; + + // 3) Pick the Firefly preset that best matches the aspect ratio + let preset = FireflyImageDimensions.Square; + if (ratio > AspectRatioLimits[FireflyImageDimensions.Widescreen]) { + preset = FireflyImageDimensions.Widescreen; + } else if (ratio > AspectRatioLimits[FireflyImageDimensions.Landscape]) { + preset = FireflyImageDimensions.Landscape; + } else if (ratio < AspectRatioLimits[FireflyImageDimensions.Portrait]) { + preset = FireflyImageDimensions.Portrait; + } + + // 4) Call exactly the same CreateWithFirefly that ImageBox uses + const doc = await SmartDrawHandler.CreateWithFirefly(prompt, preset); + if (doc instanceof Doc) { + // 5) Hook it into your state + this.imgDoc = doc; const imgField = ImageCast(doc.data); - if (imgField?.url.href) { - this.src = imgField.url.href; - const url = new ImageField(this.src); - this.imgDoc = Docs.Create.ImageDocument(url, { title: 'Generated Background', _width: 1792, _height: 2304, - _nativeWidth: 1792, _nativeHeight: 1792 - }, ); - } else { - alert('Image URL missing.'); - this.src = ''; - } - + this.src = imgField?.url.href ?? ''; } else { alert('Failed to generate document.'); + this.src = ''; } - })) - .catch(e => { + } catch (e) { alert(`Generation error: ${e}`); - }) - .finally(action(() => { - this.loading = false; - })); - } + } finally { + runInAction(() => { + this.loading = false; + }); + } + } childRejectDrop = (de: DragManager.DropEvent, subView?: DocumentView) => { return true; // disable dropping documents onto any child of the scrapbook. @@ -541,6 +554,8 @@ export class ScrapbookBox extends ViewBoxAnnotatableComponent<FieldViewProps>() return false; }; + + @computed get regenPrompt() { const slots = DocListCast(this.dataDoc[this.fieldKey]); |