1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
import { imageUrlToBase64 } from '../../../ClientUtils';
import { Doc } from '../../../fields/Doc';
import { DocData } from '../../../fields/DocSymbols';
import { DocCast, ImageCast } from '../../../fields/Types';
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) => {
const docData = drawing[DocData];
const tags: string[] = ((docData?.tags as unknown as string[]) ?? []).map(tag => tag.slice(1)) ?? [];
const styles = tags.filter(tag => FireflyStylePresets.has(tag));
const styleDocs = Doc.Links(drawing)
.map(link => Doc.getOppositeAnchor(link, drawing))
.map(anchor => anchor && DocCast(anchor.embedContainer));
const styleRef = styleDocs.filter(doc => doc !== undefined && doc.type === 'image').lastElement();
let styleUrl: string | undefined;
if (styleRef) {
const styleImg = await DocumentView.GetDocImage(styleRef);
if (styleImg) {
const { href } = ImageCast(styleImg).url;
const hrefParts = href.split('.');
styleUrl = `${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 =>
Networking.PostToServer('/queryFireflyImageFromStructure', { prompt: `${user_prompt || prompt}`, width: dims.width, height: dims.height, structure: structureUrl, strength, presets: styles, styleUrl })
.then((info: Upload.ImageInformation) =>
DocumentViewInternal.addDocTabFunc(
Docs.Create.ImageDocument(info.accessPaths.agnostic.client, {
ai: 'firefly',
title: user_prompt || prompt,
ai_firefly_prompt: user_prompt || prompt,
_width: 500,
data_nativeWidth: info.nativeWidth,
data_nativeHeight: info.nativeHeight,
}),
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();
}
})
); // prettier-ignore:q
}
});
};
}
|