aboutsummaryrefslogtreecommitdiff
path: root/src/client
diff options
context:
space:
mode:
Diffstat (limited to 'src/client')
-rw-r--r--src/client/apis/google_docs/GooglePhotosClientUtils.ts73
-rw-r--r--src/client/views/MainView.tsx17
-rw-r--r--src/client/views/nodes/FormattedTextBox.tsx1
3 files changed, 43 insertions, 48 deletions
diff --git a/src/client/apis/google_docs/GooglePhotosClientUtils.ts b/src/client/apis/google_docs/GooglePhotosClientUtils.ts
index b95cc98c9..2b72800a9 100644
--- a/src/client/apis/google_docs/GooglePhotosClientUtils.ts
+++ b/src/client/apis/google_docs/GooglePhotosClientUtils.ts
@@ -1,53 +1,42 @@
-import { Album } from "../../../server/apis/google/typings/albums";
-import { PostToServer } from "../../../Utils";
+import { PostToServer, Utils } from "../../../Utils";
import { RouteStore } from "../../../server/RouteStore";
import { ImageField } from "../../../new_fields/URLField";
+import { StrCast, Cast } from "../../../new_fields/Types";
+import { Doc, Opt } from "../../../new_fields/Doc";
+import { Id } from "../../../new_fields/FieldSymbols";
+import requestImageSize = require('../../util/request-image-size');
+import Photos = require('googlephotos');
export namespace GooglePhotosClientUtils {
- export const Create = async (title: string) => {
- let parameters = {
- action: Album.Action.Create,
- body: { album: { title } }
- } as Album.Create;
- return PostToServer(RouteStore.googlePhotosQuery, parameters);
- };
+ export type AlbumReference = { id: string } | { title: string };
+ export const endpoint = () => fetch(Utils.prepend(RouteStore.googlePhotosAccessToken)).then(async response => new Photos(await response.text()));
- export const List = async (options?: Partial<Album.ListOptions>) => {
- let parameters = {
- action: Album.Action.List,
- parameters: {
- pageSize: (options ? options.pageSize : 20) || 20,
- pageToken: (options ? options.pageToken : undefined) || undefined,
- excludeNonAppCreatedData: (options ? options.excludeNonAppCreatedData : false) || false,
- } as Album.ListOptions
- } as Album.List;
- return PostToServer(RouteStore.googlePhotosQuery, parameters);
- };
+ export interface MediaInput {
+ description: string;
+ source: string;
+ }
- export const Get = async (albumId: string) => {
- let parameters = {
- action: Album.Action.Get,
- albumId
- } as Album.Get;
- return PostToServer(RouteStore.googlePhotosQuery, parameters);
- };
-
- export const toDataURL = (field: ImageField | undefined) => {
- if (!field) {
- return undefined;
+ export const UploadMedia = async (sources: Doc[], album?: AlbumReference) => {
+ if (album && "title" in album) {
+ album = (await endpoint()).albums.create(album.title);
+ }
+ const media: MediaInput[] = [];
+ sources.forEach(document => {
+ const data = Cast(Doc.GetProto(document).data, ImageField);
+ const description = StrCast(document.caption);
+ if (!data) {
+ return undefined;
+ }
+ media.push({
+ source: data.url.href,
+ description,
+ } as MediaInput);
+ });
+ if (media.length) {
+ return PostToServer(RouteStore.googlePhotosMediaUpload, { media, album });
}
- const image = document.createElement("img");
- image.src = field.url.href;
- image.width = 200;
- image.height = 200;
- const canvas = document.createElement("canvas");
- canvas.width = image.width;
- canvas.height = image.height;
- const ctx = canvas.getContext("2d")!;
- ctx.drawImage(image, 0, 0);
- const dataUrl = canvas.toDataURL("image/png");
- return dataUrl.replace(/^data:image\/(png|jpg);base64,/, "");
+ return undefined;
};
} \ No newline at end of file
diff --git a/src/client/views/MainView.tsx b/src/client/views/MainView.tsx
index c1c95fc88..6d366216e 100644
--- a/src/client/views/MainView.tsx
+++ b/src/client/views/MainView.tsx
@@ -40,11 +40,10 @@ import { PreviewCursor } from './PreviewCursor';
import { FilterBox } from './search/FilterBox';
import PresModeMenu from './presentationview/PresentationModeMenu';
import { PresBox } from './nodes/PresBox';
-import { docs_v1 } from 'googleapis';
-import { Album } from '../../server/apis/google/typings/albums';
import { GooglePhotosClientUtils } from '../apis/google_docs/GooglePhotosClientUtils';
import { ImageField } from '../../new_fields/URLField';
import { LinkFollowBox } from './linking/LinkFollowBox';
+import { DocumentManager } from '../util/DocumentManager';
@observer
export class MainView extends React.Component {
@@ -131,10 +130,7 @@ export class MainView extends React.Component {
window.removeEventListener("keydown", KeyManager.Instance.handle);
window.addEventListener("keydown", KeyManager.Instance.handle);
- let imgurl = "https://upload.wikimedia.org/wikipedia/commons/thumb/3/3a/Cat03.jpg/1200px-Cat03.jpg";
- let image = Docs.Create.ImageDocument(imgurl, { width: 200, title: "an image of a cat" });
- let parameters = { title: StrCast(image.title), MEDIA_BINARY_DATA: GooglePhotosClientUtils.toDataURL(Cast(image.data, ImageField)) };
- // PostToServer(RouteStore.googlePhotosMediaUpload, parameters).then(console.log);
+ this.executeGooglePhotosRoutine();
reaction(() => {
let workspaces = CurrentUserUtils.UserDocument.workspaces;
@@ -153,6 +149,15 @@ export class MainView extends React.Component {
}, { fireImmediately: true });
}
+ executeGooglePhotosRoutine = async () => {
+ let imgurl = "https://upload.wikimedia.org/wikipedia/commons/thumb/3/3a/Cat03.jpg/1200px-Cat03.jpg";
+ let doc = Docs.Create.ImageDocument(imgurl, { width: 200, title: "an image of a cat" });
+ doc.caption = "Well isn't this a nice cat image!";
+ let photos = await GooglePhotosClientUtils.endpoint();
+ let albumId = (await photos.albums.list(50)).albums.filter((album: any) => album.title === "This is a generically created album!")[0].id;
+ console.log(await GooglePhotosClientUtils.UploadMedia([doc], { id: albumId }));
+ }
+
componentWillUnMount() {
window.removeEventListener("keydown", KeyManager.Instance.handle);
//close presentation
diff --git a/src/client/views/nodes/FormattedTextBox.tsx b/src/client/views/nodes/FormattedTextBox.tsx
index b671d06ea..fda9ea33f 100644
--- a/src/client/views/nodes/FormattedTextBox.tsx
+++ b/src/client/views/nodes/FormattedTextBox.tsx
@@ -133,6 +133,7 @@ export class FormattedTextBox extends DocComponent<(FieldViewProps & FormattedTe
if (this.props.isOverlay) {
DragManager.StartDragFunctions.push(() => FormattedTextBox.InputBoxOverlay = undefined);
}
+ FormattedTextBox.Instance = this;
}
public get CurrentDiv(): HTMLDivElement { return this._ref.current!; }