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
188
189
190
191
192
193
194
195
196
197
|
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 { ImageField } from "../../fields/ImageField";
import { ImageBox } from "../views/nodes/ImageBox";
import { CollectionView, CollectionViewType } from "../views/collections/CollectionView";
import { FieldView } from "../views/nodes/FieldView";
import { HtmlField } from "../../fields/HtmlField";
import { WebView } from "../views/nodes/WebView";
export 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], (fields) => {
collectionProto = fields[collectionProtoId] as Document;
imageProto = fields[imageProtoId] as Document;
textProto = fields[textProtoId] 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 htmlProto: Document;
const htmlProtoId = "htmlProto";
function GetHtmlPrototype(): Document {
if (!htmlProto) {
htmlProto = new Document(htmlProtoId);
htmlProto.Set(KeyStore.X, new NumberField(0));
htmlProto.Set(KeyStore.Y, new NumberField(0));
htmlProto.Set(KeyStore.Width, new NumberField(300));
htmlProto.Set(KeyStore.Height, new NumberField(150));
htmlProto.Set(KeyStore.Layout, new TextField(WebView.LayoutString()));
htmlProto.Set(KeyStore.LayoutKeys, new ListField([KeyStore.Data]));
}
return htmlProto;
}
export function HtmlDocument(html: string, options: DocumentOptions = {}): Document {
let doc = GetHtmlPrototype().MakeDelegate();
setupOptions(doc, options);
doc.Set(KeyStore.Data, new HtmlField(html));
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(CollectionView.LayoutString("AnnotationsKey")));
imageProto.SetNumber(KeyStore.ViewType, CollectionViewType.Freeform)
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;
}
// example of custom display string for an image that shows a caption.
function EmbeddedCaption() {
return `<div style="height:100%">
<div style="position:relative; margin:auto; height:85%;" >`
+ ImageBox.LayoutString() +
`</div>
<div style="position:relative; overflow:auto; height:15%; text-align:center; ">`
+ FormattedTextBox.LayoutString("CaptionKey") +
`</div>
</div>` };
function FixedCaption() {
return `<div style="position:absolute; height:30px; bottom:0; width:100%">
<div style="position:absolute; width:100%; height:100%; overflow:auto;text-align:center;bottom:0;">`
+ FormattedTextBox.LayoutString("CaptionKey") +
`</div>
</div>` };
export function ImageDocument(url: string, options: DocumentOptions = {}): Document {
let doc = GetImagePrototype().MakeDelegate();
setupOptions(doc, options);
doc.Set(KeyStore.Data, new ImageField(new URL(url)));
doc.Set(KeyStore.Caption, new TextField("my caption..."));
doc.Set(KeyStore.BackgroundLayout, new TextField(EmbeddedCaption()));
doc.Set(KeyStore.OverlayLayout, new TextField(FixedCaption()));
doc.Set(KeyStore.LayoutKeys, new ListField([KeyStore.Data, KeyStore.Annotations, KeyStore.Caption]));
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.Scale, new NumberField(1));
collectionProto.Set(KeyStore.PanX, new NumberField(0));
collectionProto.Set(KeyStore.PanY, new NumberField(0));
collectionProto.Set(KeyStore.Layout, new TextField(CollectionView.LayoutString("DataKey")));
collectionProto.Set(KeyStore.LayoutKeys, new ListField([KeyStore.Data]));
}
return collectionProto;
}
export function CollectionDocument(data: Array<Document> | string, viewType: CollectionViewType, options: DocumentOptions = {}, id?: string): Document {
let doc = GetCollectionPrototype().MakeDelegate(id);
setupOptions(doc, options);
if (typeof data === "string") {
doc.SetText(KeyStore.Data, data);
} else {
doc.SetData(KeyStore.Data, data, ListField);
}
doc.SetNumber(KeyStore.ViewType, viewType);
return doc;
}
export function FreeformDocument(documents: Array<Document>, options: DocumentOptions, id?: string) {
return CollectionDocument(documents, CollectionViewType.Freeform, options, id)
}
export function SchemaDocument(documents: Array<Document>, options: DocumentOptions, id?: string) {
return CollectionDocument(documents, CollectionViewType.Schema, options, id)
}
export function DockDocument(config: string, options: DocumentOptions, id?: string) {
return CollectionDocument(config, CollectionViewType.Docking, options, id)
}
}
|