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
|
import { Doc, DocListCast } from '../../fields/Doc';
import { InkField } from '../../fields/InkField';
import { List } from '../../fields/List';
import { StrCast } from '../../fields/Types';
import { AudioField, ImageField, PdfField, VideoField } from '../../fields/URLField';
import { Docs, DocumentOptions } from './Documents';
/**
* Changes the field key in the doc's layout string to be the specified field
*/
export function ResetLayoutFieldKey(doc: Doc, fieldKey: string) {
doc.layout = StrCast(doc.layout).replace(/={'.*'}/, `={'${fieldKey}'}`);
return doc;
}
export function DocumentFromField(target: Doc, fieldKey: string, proto?: Doc, options?: DocumentOptions): Doc | undefined {
const field = target[fieldKey];
const resolved = options ?? {};
const nonDocFieldToDoc = () => {
if (field instanceof ImageField) return Docs.Create.ImageDocument(field.url.href, resolved);
if (field instanceof VideoField) return Docs.Create.VideoDocument(field.url.href, resolved);
if (field instanceof PdfField) return Docs.Create.PdfDocument(field.url.href, resolved);
if (field instanceof AudioField) return Docs.Create.AudioDocument(field.url.href, resolved);
if (field instanceof InkField) return Docs.Create.InkDocument(field.inkData, resolved);
if (field instanceof List && field[0] instanceof Doc) return Docs.Create.StackingDocument(DocListCast(field), resolved);
return Docs.Create.TextDocument('', { ...{ _width: 200, _height: 25, _layout_autoHeight: true }, ...resolved });
};
const created = field instanceof Doc ? field : ResetLayoutFieldKey(nonDocFieldToDoc(), fieldKey);
created.title = fieldKey;
proto && created.proto && (created.proto = Doc.GetProto(proto));
return created;
}
|