From e3ad2c2c8f920a5538541b8e495af52815e36dc6 Mon Sep 17 00:00:00 2001
From: Tyler Schicke <tyler_schicke@brown.edu>
Date: Fri, 19 Apr 2019 23:52:34 -0400
Subject: Started fixing stuff

---
 src/client/views/DocComponent.tsx | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
 create mode 100644 src/client/views/DocComponent.tsx

(limited to 'src/client/views/DocComponent.tsx')

diff --git a/src/client/views/DocComponent.tsx b/src/client/views/DocComponent.tsx
new file mode 100644
index 000000000..31282744b
--- /dev/null
+++ b/src/client/views/DocComponent.tsx
@@ -0,0 +1,14 @@
+import * as React from 'react';
+import { Doc } from '../../new_fields/Doc';
+import { computed } from 'mobx';
+
+export function DocComponent<P extends { Document: Doc }, T>(schemaCtor: (doc: Doc) => T) {
+    class Component extends React.Component<P> {
+        //TODO This might be pretty inefficient if doc isn't observed, because computed doesn't cache then
+        @computed
+        get Document() {
+            return schemaCtor(this.props.Document);
+        }
+    }
+    return Component;
+}
\ No newline at end of file
-- 
cgit v1.2.3-70-g09d2


From f3aef5750b48f375b06540a85b46e404f607c37f Mon Sep 17 00:00:00 2001
From: Tyler Schicke <tyler_schicke@brown.edu>
Date: Tue, 23 Apr 2019 18:16:05 -0400
Subject: Most of collection sub view

---
 src/client/views/DocComponent.tsx                  |  2 +-
 src/client/views/collections/CollectionSubView.tsx | 99 ++++++++++------------
 src/debug/Test.tsx                                 |  4 +-
 src/new_fields/Doc.ts                              | 20 ++---
 4 files changed, 60 insertions(+), 65 deletions(-)

(limited to 'src/client/views/DocComponent.tsx')

diff --git a/src/client/views/DocComponent.tsx b/src/client/views/DocComponent.tsx
index 31282744b..d6562492f 100644
--- a/src/client/views/DocComponent.tsx
+++ b/src/client/views/DocComponent.tsx
@@ -6,7 +6,7 @@ export function DocComponent<P extends { Document: Doc }, T>(schemaCtor: (doc: D
     class Component extends React.Component<P> {
         //TODO This might be pretty inefficient if doc isn't observed, because computed doesn't cache then
         @computed
-        get Document() {
+        get Document(): T {
             return schemaCtor(this.props.Document);
         }
     }
diff --git a/src/client/views/collections/CollectionSubView.tsx b/src/client/views/collections/CollectionSubView.tsx
index 1a839fdcb..642e5aed6 100644
--- a/src/client/views/collections/CollectionSubView.tsx
+++ b/src/client/views/collections/CollectionSubView.tsx
@@ -1,12 +1,8 @@
 import { action, runInAction } from "mobx";
-import { Document } from "../../../fields/Document";
-import { ListField } from "../../../fields/ListField";
 import React = require("react");
-import { KeyStore } from "../../../fields/KeyStore";
-import { FieldWaiting, Opt } from "../../../fields/Field";
 import { undoBatch, UndoManager } from "../../util/UndoManager";
 import { DragManager } from "../../util/DragManager";
-import { Documents, DocumentOptions } from "../../documents/Documents";
+import { Docs, DocumentOptions } from "../../documents/Documents";
 import { RouteStore } from "../../../server/RouteStore";
 import { TupleField } from "../../../fields/TupleField";
 import { CurrentUserUtils } from "../../../server/authentication/models/current_user_utils";
@@ -18,10 +14,12 @@ import * as rp from 'request-promise';
 import { CollectionView } from "./CollectionView";
 import { CollectionPDFView } from "./CollectionPDFView";
 import { CollectionVideoView } from "./CollectionVideoView";
-import { Doc } from "../../../new_fields/Doc";
+import { Doc, ObjectField, Opt } from "../../../new_fields/Doc";
 import { DocComponent } from "../DocComponent";
 import { listSpec } from "../../../new_fields/Schema";
-import { Cast } from "../../../new_fields/Types";
+import { Cast, PromiseValue } from "../../../new_fields/Types";
+import { List } from "../../../new_fields/List";
+import { DocServer } from "../../DocServer";
 
 export interface CollectionViewProps extends FieldViewProps {
     addDocument: (document: Doc, allowDuplicates?: boolean) => boolean;
@@ -57,27 +55,27 @@ export function CollectionSubView<T>(schemaCtor: (doc: Doc) => T) {
         }
 
         @action
-        protected setCursorPosition(position: [number, number]) {
+        protected async setCursorPosition(position: [number, number]) {
             let ind;
             let doc = this.props.Document;
             let id = CurrentUserUtils.id;
             let email = CurrentUserUtils.email;
             if (id && email) {
                 let textInfo: [string, string] = [id, email];
-                doc.GetTAsync(KeyStore.Prototype, Document).then(proto => {
-                    if (!proto) {
-                        return;
-                    }
-                    proto.GetOrCreateAsync<ListField<CursorEntry>>(KeyStore.Cursors, ListField, action((field: ListField<CursorEntry>) => {
-                        let cursors = field.Data;
-                        if (cursors.length > 0 && (ind = cursors.findIndex(entry => entry.Data[0][0] === id)) > -1) {
-                            cursors[ind].Data[1] = position;
-                        } else {
-                            let entry = new TupleField<[string, string], [number, number]>([textInfo, position]);
-                            cursors.push(entry);
-                        }
-                    }));
-                });
+                const proto = await doc.proto;
+                if (!proto) {
+                    return;
+                }
+                let cursors = await Cast(proto.cursors, listSpec(ObjectField));
+                if (!cursors) {
+                    proto.cursors = cursors = new List<ObjectField>();
+                }
+                if (cursors.length > 0 && (ind = cursors.findIndex(entry => entry.Data[0][0] === id)) > -1) {
+                    cursors[ind].Data[1] = position;
+                } else {
+                    let entry = new TupleField<[string, string], [number, number]>([textInfo, position]);
+                    cursors.push(entry);
+                }
             }
         }
 
@@ -86,9 +84,9 @@ export function CollectionSubView<T>(schemaCtor: (doc: Doc) => T) {
         protected drop(e: Event, de: DragManager.DropEvent): boolean {
             if (de.data instanceof DragManager.DocumentDragData) {
                 if (de.data.aliasOnDrop || de.data.copyOnDrop) {
-                    [KeyStore.Width, KeyStore.Height, KeyStore.CurPage].map(key =>
+                    ["width", "height", "curPage"].map(key =>
                         de.data.draggedDocuments.map((draggedDocument: Document, i: number) =>
-                            draggedDocument.GetTAsync(key, NumberField, (f: Opt<NumberField>) => f ? de.data.droppedDocuments[i].SetNumber(key, f.Data) : null)));
+                            PromiseValue(Cast(draggedDocument[key], "number")).then(f => f && (de.data.droppedDocuments[i][key] = f))));
                 }
                 let added = false;
                 if (de.data.aliasOnDrop || de.data.copyOnDrop) {
@@ -114,42 +112,42 @@ export function CollectionSubView<T>(schemaCtor: (doc: Doc) => T) {
             return false;
         }
 
-        protected async getDocumentFromType(type: string, path: string, options: DocumentOptions): Promise<Opt<Document>> {
-            let ctor: ((path: string, options: DocumentOptions) => (Document | Promise<Document | undefined>)) | undefined = undefined;
+        protected async getDocumentFromType(type: string, path: string, options: DocumentOptions): Promise<Opt<Doc>> {
+            let ctor: ((path: string, options: DocumentOptions) => (Doc | Promise<Doc | undefined>)) | undefined = undefined;
             if (type.indexOf("image") !== -1) {
-                ctor = Documents.ImageDocument;
+                ctor = Docs.ImageDocument;
             }
             if (type.indexOf("video") !== -1) {
-                ctor = Documents.VideoDocument;
+                ctor = Docs.VideoDocument;
             }
             if (type.indexOf("audio") !== -1) {
-                ctor = Documents.AudioDocument;
+                ctor = Docs.AudioDocument;
             }
             if (type.indexOf("pdf") !== -1) {
-                ctor = Documents.PdfDocument;
+                ctor = Docs.PdfDocument;
                 options.nativeWidth = 1200;
             }
             if (type.indexOf("excel") !== -1) {
-                ctor = Documents.DBDocument;
+                ctor = Docs.DBDocument;
                 options.copyDraggedItems = true;
             }
             if (type.indexOf("html") !== -1) {
                 if (path.includes('localhost')) {
                     let s = path.split('/');
                     let id = s[s.length - 1];
-                    Server.GetField(id).then(field => {
-                        if (field instanceof Document) {
-                            let alias = field.CreateAlias();
-                            alias.SetNumber(KeyStore.X, options.x || 0);
-                            alias.SetNumber(KeyStore.Y, options.y || 0);
-                            alias.SetNumber(KeyStore.Width, options.width || 300);
-                            alias.SetNumber(KeyStore.Height, options.height || options.width || 300);
+                    DocServer.GetRefField(id).then(field => {
+                        if (field instanceof Doc) {
+                            let alias = Doc.MakeAlias(field);
+                            alias.x = options.x || 0;
+                            alias.y = options.y || 0;
+                            alias.width = options.width || 300;
+                            alias.height = options.height || options.width || 300;
                             this.props.addDocument(alias, false);
                         }
                     });
                     return undefined;
                 }
-                ctor = Documents.WebDocument;
+                ctor = Docs.WebDocument;
                 options = { height: options.width, ...options, title: path };
             }
             return ctor ? ctor(path, options) : undefined;
@@ -169,8 +167,8 @@ export function CollectionSubView<T>(schemaCtor: (doc: Doc) => T) {
 
             if (html && html.indexOf("<img") !== 0 && !html.startsWith("<a")) {
                 console.log("not good");
-                let htmlDoc = Documents.HtmlDocument(html, { ...options, width: 300, height: 300 });
-                htmlDoc.SetText(KeyStore.DocumentText, text);
+                let htmlDoc = Docs.HtmlDocument(html, { ...options, width: 300, height: 300 });
+                htmlDoc.documentText = text;
                 this.props.addDocument(htmlDoc, false);
                 return;
             }
@@ -212,18 +210,15 @@ export function CollectionSubView<T>(schemaCtor: (doc: Doc) => T) {
                             let path = window.location.origin + file;
                             let docPromise = this.getDocumentFromType(type, path, { ...options, nativeWidth: 600, width: 300, title: dropFileName });
 
-                            docPromise.then(action((doc?: Document) => {
-                                let docs = this.props.Document.GetT(KeyStore.Data, ListField);
-                                if (docs !== FieldWaiting) {
-                                    if (!docs) {
-                                        docs = new ListField<Document>();
-                                        this.props.Document.Set(KeyStore.Data, docs);
-                                    }
-                                    if (doc) {
-                                        docs.Data.push(doc);
-                                    }
+                            docPromise.then(async doc => {
+                                let docs = await Cast(this.props.Document[this.props.fieldKey], listSpec(Doc));
+                                if (!docs) {
+                                    this.props.Document[this.props.fieldKey] = docs = new List();
+                                }
+                                if (doc) {
+                                    docs.push(doc);
                                 }
-                            }));
+                            });
                         }));
                     });
                     promises.push(prom);
diff --git a/src/debug/Test.tsx b/src/debug/Test.tsx
index 179f987e0..7415d4b28 100644
--- a/src/debug/Test.tsx
+++ b/src/debug/Test.tsx
@@ -14,8 +14,8 @@ const schema1 = createSchema({
     testDoc: Doc
 });
 
-const TestDoc = makeInterface(schema1);
 type TestDoc = makeInterface<[typeof schema1]>;
+const TestDoc: (doc?: Doc) => TestDoc = makeInterface(schema1);
 
 const schema2 = createSchema({
     hello: ImageField,
@@ -45,7 +45,6 @@ class Test extends React.Component {
 
 
         const test1: TestDoc = TestDoc(doc);
-        const test2: Test2Doc = Test2Doc(doc);
         assert(test1.hello === 5);
         assert(test1.fields === undefined);
         assert(test1.test === "hello doc");
@@ -54,6 +53,7 @@ class Test extends React.Component {
         test1.myField = 20;
         assert(test1.myField === 20);
 
+        const test2: Test2Doc = Test2Doc(doc);
         assert(test2.hello === undefined);
         // assert(test2.fields === "test");
         assert(test2.test === undefined);
diff --git a/src/new_fields/Doc.ts b/src/new_fields/Doc.ts
index 987cb2cc4..cce578b60 100644
--- a/src/new_fields/Doc.ts
+++ b/src/new_fields/Doc.ts
@@ -70,16 +70,16 @@ export class Doc extends RefField {
 }
 
 export namespace Doc {
-    export function GetAsync(doc: Doc, key: string, ignoreProto: boolean = false): Promise<Field | undefined> {
-        const self = doc[Self];
-        return new Promise(res => getField(self, key, ignoreProto, res));
-    }
-    export function GetTAsync<T extends Field>(doc: Doc, key: string, ctor: ToConstructor<T>, ignoreProto: boolean = false): Promise<T | undefined> {
-        return new Promise(async res => {
-            const field = await GetAsync(doc, key, ignoreProto);
-            return Cast(field, ctor);
-        });
-    }
+    // export function GetAsync(doc: Doc, key: string, ignoreProto: boolean = false): Promise<Field | undefined> {
+    //     const self = doc[Self];
+    //     return new Promise(res => getField(self, key, ignoreProto, res));
+    // }
+    // export function GetTAsync<T extends Field>(doc: Doc, key: string, ctor: ToConstructor<T>, ignoreProto: boolean = false): Promise<T | undefined> {
+    //     return new Promise(async res => {
+    //         const field = await GetAsync(doc, key, ignoreProto);
+    //         return Cast(field, ctor);
+    //     });
+    // }
     export function Get(doc: Doc, key: string, ignoreProto: boolean = false): FieldResult {
         const self = doc[Self];
         return getField(self, key, ignoreProto);
-- 
cgit v1.2.3-70-g09d2

