From 8ddec1c70c01b3d7d919908299e1168b75698dc7 Mon Sep 17 00:00:00 2001 From: Tyler Schicke Date: Sat, 20 Apr 2019 19:06:28 -0400 Subject: More refactoring --- .../views/collections/CollectionDockingView.tsx | 65 +++++++++++----------- src/new_fields/Doc.ts | 13 +++-- src/new_fields/List.ts | 2 +- src/new_fields/Proxy.ts | 20 +++---- src/new_fields/Types.ts | 2 - src/new_fields/URLField.ts | 12 ++-- src/new_fields/util.ts | 4 +- 7 files changed, 59 insertions(+), 59 deletions(-) (limited to 'src') diff --git a/src/client/views/collections/CollectionDockingView.tsx b/src/client/views/collections/CollectionDockingView.tsx index e4c647635..b6c87231f 100644 --- a/src/client/views/collections/CollectionDockingView.tsx +++ b/src/client/views/collections/CollectionDockingView.tsx @@ -4,11 +4,8 @@ import 'golden-layout/src/css/goldenlayout-dark-theme.css'; import { action, observable, reaction, trace } from "mobx"; import { observer } from "mobx-react"; import * as ReactDOM from 'react-dom'; -import { Document } from "../../../fields/Document"; -import { KeyStore } from "../../../fields/KeyStore"; import Measure from "react-measure"; -import { FieldId, Opt, Field, FieldWaiting } from "../../../fields/Field"; -import { Utils, returnTrue, emptyFunction, emptyDocFunction, returnOne } from "../../../Utils"; +import { Utils, returnTrue, emptyFunction, returnOne } from "../../../Utils"; import { Server } from "../../Server"; import { undoBatch } from "../../util/UndoManager"; import { DocumentView } from "../nodes/DocumentView"; @@ -17,20 +14,22 @@ import React = require("react"); import { SubCollectionViewProps } from "./CollectionSubView"; import { ServerUtils } from "../../../server/ServerUtil"; import { DragManager, DragLinksAsDocuments } from "../../util/DragManager"; -import { TextField } from "../../../fields/TextField"; -import { ListField } from "../../../fields/ListField"; -import { Transform } from '../../util/Transform' +import { Transform } from '../../util/Transform'; +import { Doc, Id, Opt, Field, FieldId } from "../../../new_fields/Doc"; +import { Cast, FieldValue } from "../../../new_fields/Types"; +import { List } from "../../../new_fields/List"; +import { DocServer } from "../../DocServer"; @observer export class CollectionDockingView extends React.Component { public static Instance: CollectionDockingView; - public static makeDocumentConfig(document: Document) { + public static makeDocumentConfig(document: Doc) { return { type: 'react-component', component: 'DocumentFrameRenderer', - title: document.Title, + title: document.title, props: { - documentId: document.Id, + documentId: document[Id], //collectionDockingView: CollectionDockingView.Instance } }; @@ -48,14 +47,14 @@ export class CollectionDockingView extends React.Component this.AddRightSplit(dragDoc, true).contentItems[0].tab._dragListener. onMouseDown({ pageX: e.pageX, pageY: e.pageY, preventDefault: emptyFunction, button: 0 })); } @action - public OpenFullScreen(document: Document) { + public OpenFullScreen(document: Doc) { let newItemStackConfig = { type: 'stack', content: [CollectionDockingView.makeDocumentConfig(document)] @@ -83,7 +82,7 @@ export class CollectionDockingView extends React.Component void = () => { if (this._containerRef.current) { reaction( - () => this.props.Document.GetText(KeyStore.Data, ""), + () => Cast(this.props.Document.data, "string", ""), () => { if (!this._goldenLayout || this._ignoreStateChange !== JSON.stringify(this._goldenLayout.toConfig())) { setTimeout(() => this.setupGoldenLayout(), 1); @@ -203,7 +202,7 @@ export class CollectionDockingView extends React.Component) => - (sourceDoc instanceof Document) && DragLinksAsDocuments(tab, x, y, sourceDoc))); + (sourceDoc instanceof Doc) && DragLinksAsDocuments(tab, x, y, sourceDoc))); } else if ((className === "lm_title" || className === "lm_tab lm_active") && !e.shiftKey) { e.stopPropagation(); @@ -213,11 +212,11 @@ export class CollectionDockingView extends React.Component) => { - if (f instanceof Document) { + if (f instanceof Doc) { DragManager.StartDocumentDrag([tab], new DragManager.DocumentDragData([f]), x, y, { handlers: { - dragComplete: action(emptyFunction), + dragComplete: emptyFunction, }, hideSource: false }); @@ -235,7 +234,7 @@ export class CollectionDockingView extends React.Component { var json = JSON.stringify(this._goldenLayout.toConfig()); - this.props.Document.SetText(KeyStore.Data, json); + this.props.Document.data = json; } itemDropped = () => { @@ -264,10 +263,9 @@ export class CollectionDockingView extends React.Component${count}`); tab.element.append(counter); counter.DashDocId = tab.contentItem.config.props.documentId; - tab.reactionDisposer = reaction(() => [f.GetT(KeyStore.LinkedFromDocs, ListField), f.GetT(KeyStore.LinkedToDocs, ListField)], - (lists) => { - let count = (lists.length > 0 && lists[0] && lists[0]!.Data ? lists[0]!.Data.length : 0) + - (lists.length > 1 && lists[1] && lists[1]!.Data ? lists[1]!.Data.length : 0); + tab.reactionDisposer = reaction((): [List | null | undefined, List | null | undefined] => [Cast(f.linkedFromDocs, List), Cast(f.linkedToDocs, List)], + ([linkedFrom, linkedTo]) => { + let count = (linkedFrom ? linkedFrom.length : 0) + (linkedTo ? linkedTo.length : 0); counter.innerHTML = count; }); })); @@ -319,19 +317,22 @@ export class DockedFrameRenderer extends React.Component { _mainCont = React.createRef(); @observable private _panelWidth = 0; @observable private _panelHeight = 0; - @observable private _document: Opt; + @observable private _document: Opt; constructor(props: any) { super(props); - Server.GetField(this.props.documentId, action((f: Opt) => this._document = f as Document)); + DocServer.GetRefField(this.props.documentId).then(action((f: Opt) => this._document = f as Doc)); } - nativeWidth = () => this._document!.GetNumber(KeyStore.NativeWidth, this._panelWidth); - nativeHeight = () => this._document!.GetNumber(KeyStore.NativeHeight, this._panelHeight); + nativeWidth = () => Cast(this._document!.nativeWidth, "number", this._panelWidth); + nativeHeight = () => Cast(this._document!.nativeHeight, "number", this._panelHeight); contentScaling = () => { - let wscale = this._panelWidth / (this.nativeWidth() ? this.nativeWidth() : this._panelWidth); - if (wscale * this.nativeHeight() > this._panelHeight) - return this._panelHeight / (this.nativeHeight() ? this.nativeHeight() : this._panelHeight); + const nativeH = this.nativeHeight(); + const nativeW = this.nativeWidth(); + let wscale = this._panelWidth / nativeW; + if (wscale * nativeH > this._panelHeight) { + return this._panelHeight / nativeH; + } return wscale; } @@ -349,7 +350,7 @@ export class DockedFrameRenderer extends React.Component { return (
- { selectOnLoad={false} parentActive={returnTrue} whenActiveChanged={emptyFunction} - focus={emptyDocFunction} + focus={emptyFunction} ContainingCollectionView={undefined} />
); } diff --git a/src/new_fields/Doc.ts b/src/new_fields/Doc.ts index e0eb44ee9..8cbd8cf38 100644 --- a/src/new_fields/Doc.ts +++ b/src/new_fields/Doc.ts @@ -6,14 +6,15 @@ import { DocServer } from "../client/DocServer"; import { setter, getter, getField } from "./util"; import { Cast, FieldCtor } from "./Types"; +export type FieldId = string; export const HandleUpdate = Symbol("HandleUpdate"); export const Id = Symbol("Id"); export abstract class RefField { @serializable(alias("id", primitive())) - private __id: string; - readonly [Id]: string; + private __id: FieldId; + readonly [Id]: FieldId; - constructor(id?: string) { + constructor(id?: FieldId) { this.__id = id || Utils.GenerateGuid(); this[Id] = this.__id; } @@ -39,7 +40,7 @@ export const Self = Symbol("Self"); @Deserializable("doc").withFields(["id"]) export class Doc extends RefField { - constructor(id?: string, forceSave?: boolean) { + constructor(id?: FieldId, forceSave?: boolean) { super(id); const doc = new Proxy(this, { set: setter, @@ -53,7 +54,7 @@ export class Doc extends RefField { return doc; } - [key: string]: Field | null | undefined; + [key: string]: Field | FieldWaiting | undefined; @serializable(alias("fields", map(autoObject()))) @observable @@ -72,7 +73,6 @@ export namespace Doc { return new Promise(res => getField(self, key, ignoreProto, res)); } export function GetTAsync(doc: Doc, key: string, ctor: FieldCtor, ignoreProto: boolean = false): Promise { - const self = doc[Self]; return new Promise(async res => { const field = await GetAsync(doc, key, ignoreProto); return Cast(field, ctor); @@ -90,6 +90,7 @@ export namespace Doc { return undefined; } const delegate = new Doc(); + //TODO Does this need to be doc[Self]? delegate.prototype = doc; return delegate; } diff --git a/src/new_fields/List.ts b/src/new_fields/List.ts index a1a623f83..58b252f7b 100644 --- a/src/new_fields/List.ts +++ b/src/new_fields/List.ts @@ -9,7 +9,7 @@ class ListImpl extends ObjectField { constructor() { super(); const list = new Proxy(this, { - set: function (a, b, c, d) { return setter(a, b, c, d); }, + set: setter, get: getter, deleteProperty: () => { throw new Error("Currently properties can't be deleted from documents, assign to undefined instead"); }, defineProperty: () => { throw new Error("Currently properties can't be defined on documents using Object.defineProperty"); }, diff --git a/src/new_fields/Proxy.ts b/src/new_fields/Proxy.ts index 3b4b2e452..6a78388c1 100644 --- a/src/new_fields/Proxy.ts +++ b/src/new_fields/Proxy.ts @@ -1,7 +1,8 @@ import { Deserializable } from "../client/util/SerializationHelper"; -import { RefField, Id, ObjectField } from "./Doc"; +import { RefField, Id, ObjectField, FieldWaiting } from "./Doc"; import { primitive, serializable } from "serializr"; -import { observable } from "mobx"; +import { observable, action } from "mobx"; +import { DocServer } from "../client/DocServer"; @Deserializable("proxy") export class ProxyField extends ObjectField { @@ -32,7 +33,7 @@ export class ProxyField extends ObjectField { private failed = false; private promise?: Promise; - value(callback?: ((field: T | undefined) => void)): T | undefined | null { + value(callback?: ((field: T | undefined) => void)): T | undefined | FieldWaiting { if (this.cache) { callback && callback(this.cache); return this.cache; @@ -41,13 +42,12 @@ export class ProxyField extends ObjectField { return undefined; } if (!this.promise) { - // this.promise = Server.GetField(this.fieldId).then(action((field: any) => { - // this.promise = undefined; - // this.cache = field; - // if (field === undefined) this.failed = true; - // return field; - // })); - this.promise = new Promise(r => r()); + this.promise = DocServer.GetRefField(this.fieldId).then(action((field: any) => { + this.promise = undefined; + this.cache = field; + if (field === undefined) this.failed = true; + return field; + })); } callback && this.promise.then(callback); return null; diff --git a/src/new_fields/Types.ts b/src/new_fields/Types.ts index 6ffb3e02f..0fbd8984c 100644 --- a/src/new_fields/Types.ts +++ b/src/new_fields/Types.ts @@ -50,8 +50,6 @@ export function Cast>(field: Field | null | undefined } else if (field instanceof (ctor as any)) { return field as ToType; } - } else { - return defaultVal; } return defaultVal; } diff --git a/src/new_fields/URLField.ts b/src/new_fields/URLField.ts index d27a2b692..e456a7d16 100644 --- a/src/new_fields/URLField.ts +++ b/src/new_fields/URLField.ts @@ -1,16 +1,16 @@ import { Deserializable } from "../client/util/SerializationHelper"; -import { serializable } from "serializr"; +import { serializable, custom } from "serializr"; import { ObjectField } from "./Doc"; function url() { - return { - serializer: function (value: URL) { + return custom( + function (value: URL) { return value.href; }, - deserializer: function (jsonValue: string, done: (err: any, val: any) => void) { - done(undefined, new URL(jsonValue)); + function (jsonValue: string) { + return new URL(jsonValue); } - }; + ); } @Deserializable("url") diff --git a/src/new_fields/util.ts b/src/new_fields/util.ts index 0f08ecf03..3806044bd 100644 --- a/src/new_fields/util.ts +++ b/src/new_fields/util.ts @@ -22,6 +22,7 @@ export function setter(target: any, prop: string | symbol | number, value: any, value = new ProxyField(value); } if (value instanceof ObjectField) { + //TODO Instead of target, maybe use target[Self] if (value[Parent] && value[Parent] !== target) { throw new Error("Can't put the same object in multiple documents at the same time"); } @@ -51,7 +52,7 @@ export function getter(target: any, prop: string | symbol | number, receiver: an if (SerializationHelper.IsSerializing()) { return target[prop]; } - return getField(target, prop, receiver); + return getField(target, prop); } export function getField(target: any, prop: string | number, ignoreProto: boolean = false, callback?: (field: Field | undefined) => void): any { @@ -69,5 +70,4 @@ export function getField(target: any, prop: string | number, ignoreProto: boolea } callback && callback(field); return field; - } -- cgit v1.2.3-70-g09d2