aboutsummaryrefslogtreecommitdiff
path: root/src/client/documents/DocFromField.ts
diff options
context:
space:
mode:
authorZachary Zhang <zacharyzhang7@gmail.com>2024-07-10 13:28:21 -0400
committerZachary Zhang <zacharyzhang7@gmail.com>2024-07-10 13:28:21 -0400
commit376eb907e68be8408f12fe79dd23a6e5f46ffe60 (patch)
tree1e56480951e0b940dd47b98412eb2bc3c6351946 /src/client/documents/DocFromField.ts
parent8922b3de6fcd53d5a09e719b2885e94ddb069c76 (diff)
parentf23ea0cbde3c4193b8039f317fe71d965c8cbf40 (diff)
Merge branch 'master' into zach-starter
Diffstat (limited to 'src/client/documents/DocFromField.ts')
-rw-r--r--src/client/documents/DocFromField.ts16
1 files changed, 12 insertions, 4 deletions
diff --git a/src/client/documents/DocFromField.ts b/src/client/documents/DocFromField.ts
index b65bbbdf5..9acb9c225 100644
--- a/src/client/documents/DocFromField.ts
+++ b/src/client/documents/DocFromField.ts
@@ -1,5 +1,4 @@
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';
@@ -12,7 +11,17 @@ 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 {
+/**
+ * Creates a new document based on the type of (and containing the) data in the specified field of an existing document.
+ * If the field contains a list, then it may be useful to specify a classProto to indicate the type of
+ * collection Doc that gets created.
+ * @param target document to retrive field from
+ * @param fieldKey field key to retrieve
+ * @param classProto optionally a class proto to set on the new document
+ * @param options metadata configuration for new document
+ * @returns
+ */
+export function DocumentFromField(target: Doc, fieldKey: string, classProto?: Doc, options?: DocumentOptions): Doc | undefined {
const field = target[fieldKey];
const resolved = options ?? {};
const nonDocFieldToDoc = () => {
@@ -20,12 +29,11 @@ export function DocumentFromField(target: Doc, fieldKey: string, proto?: Doc, op
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));
+ classProto && created.proto && (created.proto = classProto);
return created;
}