aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorSophie Zhang <sophie_zhang@brown.edu>2023-08-17 17:52:12 -0400
committerSophie Zhang <sophie_zhang@brown.edu>2023-08-17 17:52:12 -0400
commit592e1a1b1000157db77bd812b8debfbcc45844f9 (patch)
treefb9cfd5bf4fa4f3696f07878267aa2d3fffba420 /src
parentd3ecace5f03233e5d5ab2354c3f482418287ca9a (diff)
revert
Diffstat (limited to 'src')
-rw-r--r--src/client/views/nodes/ImageBox.tsx4
-rw-r--r--src/client/views/nodes/generativeFill/GenerativeFill.tsx115
-rw-r--r--src/client/views/nodes/generativeFill/generativeFillUtils/ImageHandler.ts2
3 files changed, 46 insertions, 75 deletions
diff --git a/src/client/views/nodes/ImageBox.tsx b/src/client/views/nodes/ImageBox.tsx
index 352e0fbdc..603994016 100644
--- a/src/client/views/nodes/ImageBox.tsx
+++ b/src/client/views/nodes/ImageBox.tsx
@@ -252,8 +252,8 @@ export class ImageBox extends ViewBoxAnnotatableComponent<ViewBoxAnnotatableProp
funcs.push({
description: 'Open Image Editor',
event: action(() => {
- MainView.Instance.imageEditorOpen = true;
- MainView.Instance.imageEditorSource = this.choosePath(field.url);
+ MainView.Instance.setImageEditorOpen(true);
+ MainView.Instance.setImageEditorSource(this.choosePath(field.url));
MainView.Instance.addDoc = this.props.addDocument;
MainView.Instance.imageRootDoc = this.rootDoc;
}),
diff --git a/src/client/views/nodes/generativeFill/GenerativeFill.tsx b/src/client/views/nodes/generativeFill/GenerativeFill.tsx
index 99068d647..f136982bc 100644
--- a/src/client/views/nodes/generativeFill/GenerativeFill.tsx
+++ b/src/client/views/nodes/generativeFill/GenerativeFill.tsx
@@ -3,7 +3,7 @@ import React = require('react');
import { useEffect, useRef, useState } from 'react';
import { APISuccess, ImageUtility } from './generativeFillUtils/ImageHandler';
import { BrushHandler } from './generativeFillUtils/BrushHandler';
-import { Box, Checkbox, FormControlLabel, IconButton, Slider, TextField } from '@mui/material';
+import { Box, IconButton, Slider, TextField } from '@mui/material';
import { CursorData, Point } from './generativeFillUtils/generativeFillInterfaces';
import { activeColor, canvasSize, eraserColor, freeformRenderSize, newCollectionSize, offsetDistanceY, offsetX } from './generativeFillUtils/generativeFillConstants';
import { PointerHandler } from './generativeFillUtils/PointerHandler';
@@ -19,7 +19,6 @@ import { Cast, DocCast, NumCast } from '../../../../fields/Types';
import { CollectionDockingView } from '../../collections/CollectionDockingView';
import { OpenWhere, OpenWhereMod } from '../DocumentView';
import { Oval } from 'react-loader-spinner';
-import { CheckBox } from '../../search/CheckBox';
/**
* For images not 1024x1024 fill in the rest in solid black, or a
@@ -73,7 +72,6 @@ const GenerativeFill = ({ imageEditorOpen, imageEditorSource, imageRootDoc, addD
const [input, setInput] = useState('');
const [loading, setLoading] = useState(false);
const [saveLoading, setSaveLoading] = useState(false);
- const [isNewCollection, setIsNewCollection] = useState(false);
// the current image in the main canvas
const currImg = useRef<HTMLImageElement | null>(null);
// the unedited version of each generation (parent)
@@ -83,6 +81,9 @@ const GenerativeFill = ({ imageEditorOpen, imageEditorSource, imageRootDoc, addD
// stores redo stack
const redoStack = useRef<string[]>([]);
+ // early stage properly, likely will get rid of
+ const freeformPosition = useRef<number[]>([0, 0]);
+
// which urls were already saved to canvas
const savedSrcs = useRef<Set<string>>(new Set());
@@ -90,6 +91,7 @@ const GenerativeFill = ({ imageEditorOpen, imageEditorSource, imageRootDoc, addD
const newCollectionRef = useRef<Doc | null>(null);
const parentDoc = useRef<Doc | null>(null);
const childrenDocs = useRef<Doc[]>([]);
+ const addToExistingCollection = useRef<boolean>(false);
// Undo and Redo
const handleUndo = () => {
@@ -109,13 +111,10 @@ const GenerativeFill = ({ imageEditorOpen, imageEditorSource, imageRootDoc, addD
};
const handleRedo = () => {
- const ctx = ImageUtility.getCanvasContext(canvasRef);
- if (!ctx || !currImg.current || !canvasRef.current) return;
-
+ // TODO: handle undo as well
const target = redoStack.current[redoStack.current.length - 1];
if (!target) {
} else {
- undoStack.current = [...undoStack.current, canvasRef.current?.toDataURL()];
const img = new Image();
img.src = target;
ImageUtility.drawImgToCanvas(img, canvasRef);
@@ -185,10 +184,13 @@ const GenerativeFill = ({ imageEditorOpen, imageEditorSource, imageRootDoc, addD
console.log('first load');
if (!imageEditorSource || imageEditorSource === '') return;
const img = new Image();
+ console.log('source', imageEditorSource);
img.src = imageEditorSource;
+ console.log('drawing image');
ImageUtility.drawImgToCanvas(img, canvasRef);
currImg.current = img;
originalImg.current = img;
+ freeformPosition.current = [0, 0];
return () => {
console.log('cleanup');
@@ -197,6 +199,7 @@ const GenerativeFill = ({ imageEditorOpen, imageEditorSource, imageRootDoc, addD
childrenDocs.current = [];
currImg.current = null;
originalImg.current = null;
+ freeformPosition.current = [0, 0];
undoStack.current = [];
redoStack.current = [];
};
@@ -265,25 +268,24 @@ const GenerativeFill = ({ imageEditorOpen, imageEditorSource, imageRootDoc, addD
// create first image
if (!newCollectionRef.current) {
- if (!isNewCollection) {
- // newcollection should stay null
- } else {
- if (!(originalImg.current && imageRootDoc)) return;
- // create new collection and add it to the view
- newCollectionRef.current = Docs.Create.FreeformDocument([], {
- x: NumCast(imageRootDoc.x) + NumCast(imageRootDoc._width) + offsetX,
- y: NumCast(imageRootDoc.y),
- _width: newCollectionSize,
- _height: newCollectionSize,
- title: 'Image edit collection',
- });
- DocUtils.MakeLink(imageRootDoc, newCollectionRef.current, { link_relationship: 'Image Edit Version History', link_displayLine: false });
- // add the doc to the main freeform
- addDoc?.(newCollectionRef.current);
- await createNewImgDoc(originalImg.current, true);
+ if (addToExistingCollection.current) {
}
+ if (!(originalImg.current && imageRootDoc)) return;
+ console.log('creating first image');
+ // create new collection and add it to the view
+ newCollectionRef.current = Docs.Create.FreeformDocument([], {
+ x: NumCast(imageRootDoc.x) + NumCast(imageRootDoc._width) + offsetX,
+ y: NumCast(imageRootDoc.y),
+ _width: newCollectionSize,
+ _height: newCollectionSize,
+ title: 'Image edit collection',
+ });
+ DocUtils.MakeLink(imageRootDoc, newCollectionRef.current, { link_relationship: 'Image Edit Version History', link_displayLine: false });
+ // add the doc to the main freeform
+ // addDoc?.(newCollectionRef.current);
+ await createNewImgDoc(originalImg.current, true);
} else {
- // parentDoc.current = childrenDocs.current[childrenDocs.current.length - 1];
+ parentDoc.current = childrenDocs.current[childrenDocs.current.length - 1];
childrenDocs.current = [];
}
@@ -292,24 +294,12 @@ const GenerativeFill = ({ imageEditorOpen, imageEditorSource, imageRootDoc, addD
const { urls } = res as APISuccess;
const image = new Image();
image.src = urls[0];
-
- // need to crop images agh
- // save to dash
-
- // const imageRes = await Promise.all(
- // urls.map(async url => {
- // const newImg = new Image();
- // newImg.src = url;
- // await onSave(newImg);
- // })
- // );
-
- // map each url to [url, imgDoc]
setEdits(urls);
-
ImageUtility.drawImgToCanvas(image, canvasRef);
currImg.current = image;
- // onSave(currImg.current);
+ onSave();
+ freeformPosition.current[0] += 1;
+ freeformPosition.current[1] = 0;
} catch (err) {
console.log(err);
}
@@ -336,7 +326,7 @@ const GenerativeFill = ({ imageEditorOpen, imageEditorSource, imageRootDoc, addD
// creates a new image document and returns its reference
const createNewImgDoc = async (img: HTMLImageElement, firstDoc: boolean): Promise<Doc | undefined> => {
- if (!imageRootDoc) return;
+ if (!newCollectionRef.current || !imageRootDoc) return;
const src = img.src;
const [result] = await Networking.PostToServer('/uploadRemoteImage', { sources: [src] });
const source = Utils.prepend(result.accessPaths.agnostic.client);
@@ -344,6 +334,7 @@ const GenerativeFill = ({ imageEditorOpen, imageEditorSource, imageRootDoc, addD
if (firstDoc) {
const x = 0;
const initialY = 0;
+ console.log('first doc');
const newImg = Docs.Create.ImageDocument(source, {
x: x,
@@ -354,19 +345,16 @@ const GenerativeFill = ({ imageEditorOpen, imageEditorSource, imageRootDoc, addD
data_nativeHeight: result.nativeHeight,
});
- if (isNewCollection && newCollectionRef.current) {
- Doc.AddDocToList(newCollectionRef.current, undefined, newImg);
- } else {
- addDoc?.(newImg);
- }
-
+ Doc.AddDocToList(newCollectionRef.current, undefined, newImg);
parentDoc.current = newImg;
return newImg;
} else {
if (!parentDoc.current) return;
const x = NumCast(parentDoc.current.x) + freeformRenderSize + offsetX;
// dummy position
+ console.log('creating child elements');
const initialY = 0;
+
const newImg = Docs.Create.ImageDocument(source, {
x: x,
y: initialY,
@@ -377,26 +365,21 @@ const GenerativeFill = ({ imageEditorOpen, imageEditorSource, imageRootDoc, addD
});
childrenDocs.current.push(newImg);
-
- if (isNewCollection && newCollectionRef.current) {
- Doc.AddDocToList(newCollectionRef.current, undefined, newImg);
- } else {
- addDoc?.(newImg);
- }
-
DocUtils.MakeLink(parentDoc.current, newImg, { link_relationship: 'Image Edit', link_displayLine: true });
adjustImgPositions();
+
+ Doc.AddDocToList(newCollectionRef.current, undefined, newImg);
return newImg;
}
};
// need to maybe call on every img click, not just when the save btn is clicked
- const onSave = async (img: HTMLImageElement) => {
+ const onSave = async () => {
setSaveLoading(true);
- // if (!currImg.current || !originalImg.current || !imageRootDoc) return;
+ if (!currImg.current || !originalImg.current || !imageRootDoc) return;
try {
console.log('creating another image');
- await createNewImgDoc(img, false);
+ await createNewImgDoc(currImg.current, false);
} catch (err) {
console.log(err);
}
@@ -406,7 +389,7 @@ const GenerativeFill = ({ imageEditorOpen, imageEditorSource, imageRootDoc, addD
const handleViewClose = () => {
if (newCollectionRef.current) {
newCollectionRef.current.fitContentOnce = true;
- // CollectionDockingView.AddSplit(newCollectionRef.current, OpenWhereMod.right);
+ CollectionDockingView.AddSplit(newCollectionRef.current, OpenWhereMod.right);
}
MainView.Instance.setImageEditorOpen(false);
MainView.Instance.setImageEditorSource('');
@@ -418,19 +401,6 @@ const GenerativeFill = ({ imageEditorOpen, imageEditorSource, imageRootDoc, addD
<div className="generativeFillControls">
<h1>AI Image Editor</h1>
<div style={{ display: 'flex', alignItems: 'center', gap: '1.5rem' }}>
- <FormControlLabel
- control={
- <Checkbox
- checked={isNewCollection}
- onChange={e => {
- setIsNewCollection(prev => !prev);
- }}
- />
- }
- label={'Create New Collection'}
- labelPlacement="end"
- sx={{ whiteSpace: 'nowrap' }}
- />
<Buttons canvasRef={canvasRef} currImg={currImg} getEdit={getEdit} loading={loading} onReset={handleReset} />
<IconButton onClick={handleViewClose}>
<BsX color={activeColor} />
@@ -523,14 +493,13 @@ const GenerativeFill = ({ imageEditorOpen, imageEditorSource, imageRootDoc, addD
height={100}
src={edit}
onClick={async () => {
+ // if (savedSrcs.current.has(edit)) return;
const img = new Image();
img.src = edit;
ImageUtility.drawImgToCanvas(img, canvasRef);
currImg.current = img;
savedSrcs.current.add(edit);
- undoStack.current = [];
- redoStack.current = [];
- await onSave(img);
+ await onSave();
}}
/>
))}
diff --git a/src/client/views/nodes/generativeFill/generativeFillUtils/ImageHandler.ts b/src/client/views/nodes/generativeFill/generativeFillUtils/ImageHandler.ts
index 45cf7196b..1954ab3fb 100644
--- a/src/client/views/nodes/generativeFill/generativeFillUtils/ImageHandler.ts
+++ b/src/client/views/nodes/generativeFill/generativeFillUtils/ImageHandler.ts
@@ -101,7 +101,9 @@ export class ImageUtility {
ctx.clearRect(0, 0, canvasSize, canvasSize);
ctx.drawImage(img, 0, 0, width, height);
} else {
+ console.log('loading image');
img.onload = () => {
+ console.log('loaded');
const ctx = this.getCanvasContext(canvasRef);
if (!ctx) return;
ctx.globalCompositeOperation = 'source-over';