aboutsummaryrefslogtreecommitdiff
path: root/src/client/documents/Documents.ts
blob: abb9544a5f0cff491e663d80a782bb11848ebc72 (plain)
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import { Document } from "../../fields/Document";
import { Server } from "../Server";
import { KeyStore } from "../../fields/KeyStore";
import { TextField } from "../../fields/TextField";
import { NumberField } from "../../fields/NumberField";
import { ListField } from "../../fields/ListField";
import { FormattedTextBox } from "../views/nodes/FormattedTextBox";
import { CollectionDockingView } from "../views/collections/CollectionDockingView";
import { CollectionSchemaView } from "../views/collections/CollectionSchemaView";
import { ImageField } from "../../fields/ImageField";
import { ImageBox } from "../views/nodes/ImageBox";
import { CollectionFreeFormView } from "../views/collections/CollectionFreeFormView";
import { FieldId } from "../../fields/Field";

interface DocumentOptions {
    x?: number;
    y?: number;
    width?: number;
    height?: number;
    nativeWidth?: number;
    nativeHeight?: number;
    title?: string;
}

export namespace Documents {
    export function initProtos(callback: () => void) {
        Server.GetFields([collectionProtoId, textProtoId, imageProtoId, schemaProtoId, dockProtoId], (fields) => {
            collectionProto = fields[collectionProtoId] as Document;
            imageProto = fields[imageProtoId] as Document;
            textProto = fields[textProtoId] as Document;
            dockProto = fields[dockProtoId] as Document;
            schemaProto = fields[schemaProtoId] as Document;
            callback()
        });
    }

    function setupOptions(doc: Document, options: DocumentOptions): void {
        if (options.x !== undefined) {
            doc.SetData(KeyStore.X, options.x, NumberField);
        }
        if (options.y !== undefined) {
            doc.SetData(KeyStore.Y, options.y, NumberField);
        }
        if (options.width !== undefined) {
            doc.SetData(KeyStore.Width, options.width, NumberField);
        }
        if (options.height !== undefined) {
            doc.SetData(KeyStore.Height, options.height, NumberField);
        }
        if (options.nativeWidth !== undefined) {
            doc.SetData(KeyStore.NativeWidth, options.nativeWidth, NumberField);
        }
        if (options.nativeHeight !== undefined) {
            doc.SetData(KeyStore.NativeHeight, options.nativeHeight, NumberField);
        }
        if (options.title !== undefined) {
            doc.SetData(KeyStore.Title, options.title, TextField);
        }
        doc.SetData(KeyStore.Scale, 1, NumberField);
        doc.SetData(KeyStore.PanX, 0, NumberField);
        doc.SetData(KeyStore.PanY, 0, NumberField);
    }

    let textProto: Document;
    const textProtoId = "textProto";
    function GetTextPrototype(): Document {
        if (!textProto) {
            textProto = new Document(textProtoId);
            textProto.Set(KeyStore.X, new NumberField(0));
            textProto.Set(KeyStore.Y, new NumberField(0));
            textProto.Set(KeyStore.Width, new NumberField(300));
            textProto.Set(KeyStore.Height, new NumberField(150));
            textProto.Set(KeyStore.Layout, new TextField(FormattedTextBox.LayoutString()));
            textProto.Set(KeyStore.LayoutKeys, new ListField([KeyStore.Data]));
        }
        return textProto;
    }

    export function TextDocument(options: DocumentOptions = {}): Document {
        let doc = GetTextPrototype().MakeDelegate();
        setupOptions(doc, options);
        // doc.SetField(KeyStore.Data, new RichTextField());
        return doc;
    }

    let schemaProto: Document;
    const schemaProtoId = "schemaProto";
    function GetSchemaPrototype(): Document {
        if (!schemaProto) {
            schemaProto = new Document(schemaProtoId);
            schemaProto.Set(KeyStore.X, new NumberField(0));
            schemaProto.Set(KeyStore.Y, new NumberField(0));
            schemaProto.Set(KeyStore.Width, new NumberField(300));
            schemaProto.Set(KeyStore.Height, new NumberField(150));
            schemaProto.Set(KeyStore.Layout, new TextField(CollectionSchemaView.LayoutString()));
            schemaProto.Set(KeyStore.LayoutKeys, new ListField([KeyStore.Data]));
        }
        return schemaProto;
    }

    export function SchemaDocument(documents: Array<Document>, options: DocumentOptions = {}): Document {
        let doc = GetSchemaPrototype().MakeDelegate();
        setupOptions(doc, options);
        doc.Set(KeyStore.Data, new ListField(documents));
        return doc;
    }


    let dockProto: Document;
    const dockProtoId = "dockProto";
    function GetDockPrototype(): Document {
        if (!dockProto) {
            dockProto = new Document();
            dockProto.Set(KeyStore.X, new NumberField(0));
            dockProto.Set(KeyStore.Y, new NumberField(0));
            dockProto.Set(KeyStore.Width, new NumberField(300));
            dockProto.Set(KeyStore.Height, new NumberField(150));
            dockProto.Set(KeyStore.Layout, new TextField(CollectionDockingView.LayoutString()));
            dockProto.Set(KeyStore.LayoutKeys, new ListField([KeyStore.Data]));
        }
        return dockProto;
    }

    export function DockDocument(documents: Array<Document>, options: DocumentOptions = {}): Document {
        let doc = GetDockPrototype().MakeDelegate();
        setupOptions(doc, options);
        doc.Set(KeyStore.Data, new ListField(documents));
        return doc;
    }


    let imageProto: Document;
    const imageProtoId = "imageProto";
    function GetImagePrototype(): Document {
        if (!imageProto) {
            imageProto = new Document(imageProtoId);
            imageProto.Set(KeyStore.Title, new TextField("IMAGE PROTO"));
            imageProto.Set(KeyStore.X, new NumberField(0));
            imageProto.Set(KeyStore.Y, new NumberField(0));
            imageProto.Set(KeyStore.NativeWidth, new NumberField(300));
            imageProto.Set(KeyStore.NativeHeight, new NumberField(300));
            imageProto.Set(KeyStore.Width, new NumberField(300));
            imageProto.Set(KeyStore.Height, new NumberField(300));
            imageProto.Set(KeyStore.Layout, new TextField(CollectionFreeFormView.LayoutString("AnnotationsKey")));
            imageProto.Set(KeyStore.BackgroundLayout, new TextField(ImageBox.LayoutString()));
            // imageProto.SetField(KeyStore.Layout, new TextField('<div style={"background-image: " + {Data}} />'));
            imageProto.Set(KeyStore.LayoutKeys, new ListField([KeyStore.Data, KeyStore.Annotations]));
            return imageProto;
        }
        return imageProto;
    }

    export function ImageDocument(url: string, options: DocumentOptions = {}): Document {
        let doc = GetImagePrototype().MakeDelegate();
        setupOptions(doc, options);
        doc.Set(KeyStore.Data, new ImageField(new URL(url)));

        let annotation = Documents.TextDocument({ title: "hello" });
        doc.Set(KeyStore.Annotations, new ListField([annotation]));
        return doc;
    }

    let collectionProto: Document;
    const collectionProtoId = "collectionProto";
    function GetCollectionPrototype(): Document {
        if (!collectionProto) {
            collectionProto = new Document(collectionProtoId);
            collectionProto.Set(KeyStore.X, new NumberField(0));
            collectionProto.Set(KeyStore.Y, new NumberField(0));
            collectionProto.Set(KeyStore.Scale, new NumberField(1));
            collectionProto.Set(KeyStore.PanX, new NumberField(0));
            collectionProto.Set(KeyStore.PanY, new NumberField(0));
            collectionProto.Set(KeyStore.Width, new NumberField(300));
            collectionProto.Set(KeyStore.Height, new NumberField(300));
            collectionProto.Set(KeyStore.Layout, new TextField(CollectionFreeFormView.LayoutString("DataKey")));
            collectionProto.Set(KeyStore.LayoutKeys, new ListField([KeyStore.Data]));
        }
        return collectionProto;
    }

    export function CollectionDocument(documents: Array<Document>, options: DocumentOptions = {}, id?: string): Document {
        let doc = GetCollectionPrototype().MakeDelegate(id);
        setupOptions(doc, options);
        doc.Set(KeyStore.Data, new ListField(documents));
        return doc;
    }
}