aboutsummaryrefslogtreecommitdiff
path: root/src/client/views/smartdraw/DrawingFillHandler.tsx
diff options
context:
space:
mode:
Diffstat (limited to 'src/client/views/smartdraw/DrawingFillHandler.tsx')
-rw-r--r--src/client/views/smartdraw/DrawingFillHandler.tsx79
1 files changed, 79 insertions, 0 deletions
diff --git a/src/client/views/smartdraw/DrawingFillHandler.tsx b/src/client/views/smartdraw/DrawingFillHandler.tsx
new file mode 100644
index 000000000..d0a840883
--- /dev/null
+++ b/src/client/views/smartdraw/DrawingFillHandler.tsx
@@ -0,0 +1,79 @@
+import { imageUrlToBase64 } from '../../../ClientUtils';
+import { Doc, StrListCast } from '../../../fields/Doc';
+import { DocData } from '../../../fields/DocSymbols';
+import { DocCast, ImageCast } from '../../../fields/Types';
+import { ImageField } from '../../../fields/URLField';
+import { Upload } from '../../../server/SharedMediaTypes';
+import { gptDescribeImage } from '../../apis/gpt/GPT';
+import { Docs } from '../../documents/Documents';
+import { Networking } from '../../Network';
+import { DocumentView, DocumentViewInternal } from '../nodes/DocumentView';
+import { OpenWhere } from '../nodes/OpenWhere';
+import { AspectRatioLimits, FireflyDimensionsMap, FireflyImageDimensions, FireflyStylePresets } from './FireflyConstants';
+
+const DashDropboxId = '2m86iveqdr9vzsa';
+export class DrawingFillHandler {
+ static drawingToImage = async (drawing: Doc, strength: number, user_prompt: string, styleDoc?: Doc) => {
+ const docData = drawing[DocData];
+ const tags = StrListCast(docData.tags).map(tag => tag.slice(1));
+ const styles = tags.filter(tag => FireflyStylePresets.has(tag));
+ const styleDocs = !Doc.Links(drawing).length
+ ? styleDoc && !tags.length
+ ? [styleDoc]
+ : []
+ : Doc.Links(drawing)
+ .map(link => Doc.getOppositeAnchor(link, drawing))
+ .map(anchor => anchor && DocCast(anchor.embedContainer));
+ const styleUrl = await DocumentView.GetDocImage(styleDocs.filter(doc => doc?.data instanceof ImageField).lastElement())?.then(styleImg => {
+ const hrefParts = ImageCast(styleImg).url.href.split('.');
+ return `${hrefParts.slice(0, -1).join('.')}_o.${hrefParts.lastElement()}`;
+ });
+ DocumentView.GetDocImage(drawing)?.then(imageField => {
+ if (imageField) {
+ const aspectRatio = (drawing.width as number) / (drawing.height as number);
+ let dims: { width: number; height: number };
+ if (aspectRatio > AspectRatioLimits[FireflyImageDimensions.Widescreen]) {
+ dims = FireflyDimensionsMap[FireflyImageDimensions.Widescreen];
+ } else if (aspectRatio > AspectRatioLimits[FireflyImageDimensions.Landscape]) {
+ dims = FireflyDimensionsMap[FireflyImageDimensions.Landscape];
+ } else if (aspectRatio < AspectRatioLimits[FireflyImageDimensions.Portrait]) {
+ dims = FireflyDimensionsMap[FireflyImageDimensions.Portrait];
+ } else {
+ dims = FireflyDimensionsMap[FireflyImageDimensions.Square];
+ }
+ const { href } = ImageCast(imageField).url;
+ const hrefParts = href.split('.');
+ const structureUrl = `${hrefParts.slice(0, -1).join('.')}_o.${hrefParts.lastElement()}`;
+ return imageUrlToBase64(structureUrl)
+ .then(gptDescribeImage)
+ .then((prompt, newPrompt = user_prompt || prompt) =>
+ Networking.PostToServer('/queryFireflyImageFromStructure', { prompt: `${newPrompt}`, width: dims.width, height: dims.height, structure: structureUrl, strength, presets: styles, styleUrl })
+ .then((infos: Upload.ImageInformation[]) => {
+ const genratedDocs = DocCast(drawing.ai_firefly_generatedDocs) ?? Docs.Create.MasonryDocument([], { _width: 400, _height: 400 });
+ drawing[DocData].ai_firefly_generatedDocs = genratedDocs;
+ infos.map(info =>
+ Doc.AddDocToList(
+ genratedDocs,
+ undefined,
+ Docs.Create.ImageDocument(info.accessPaths.agnostic.client, {
+ ai: 'firefly',
+ title: newPrompt,
+ ai_firefly_prompt: newPrompt,
+ _width: 500,
+ data_nativeWidth: info.nativeWidth,
+ data_nativeHeight: info.nativeHeight,
+ })
+ )
+ );
+ if (!DocumentView.getFirstDocumentView(genratedDocs)) DocumentViewInternal.addDocTabFunc(genratedDocs, OpenWhere.addRight);
+ })
+ .catch(e => {
+ if (e.toString().includes('Dropbox') && confirm('Create image failed. Try authorizing DropBox?\r\n' + e.toString().replace(/^[^"]*/, ''))) {
+ window.open(`https://www.dropbox.com/oauth2/authorize?client_id=${DashDropboxId}&response_type=code&token_access_type=offline&redirect_uri=http://localhost:1050/refreshDropbox`, '_blank')?.focus();
+ } else alert(e.toString());
+ })
+ ); // prettier-ignore:q
+ }
+ });
+ };
+}