aboutsummaryrefslogtreecommitdiff
path: root/src/fields/Doc.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/fields/Doc.ts')
-rw-r--r--src/fields/Doc.ts80
1 files changed, 55 insertions, 25 deletions
diff --git a/src/fields/Doc.ts b/src/fields/Doc.ts
index ff416bbe7..56d50846a 100644
--- a/src/fields/Doc.ts
+++ b/src/fields/Doc.ts
@@ -16,9 +16,9 @@ import { DateField } from './DateField';
import {
AclAdmin, AclAugment, AclEdit, AclPrivate, AclReadonly, Animation, AudioPlay, Brushed, CachedUpdates, DirectLinks,
DocAcl, DocCss, DocData, DocFields, DocLayout, DocViews, FieldKeys, FieldTuples, ForceServerWrite, Height, Highlight,
- Initializing, Self, SelfProxy, UpdatingFromServer, Width
+ Initializing, Self, SelfProxy, TransitionTimer, UpdatingFromServer, Width
} from './DocSymbols'; // prettier-ignore
-import { Copy, FieldChanged, HandleUpdate, Id, Parent, ToScriptString, ToString } from './FieldSymbols';
+import { Copy, FieldChanged, HandleUpdate, Id, Parent, ToJavascriptString, ToScriptString, ToString } from './FieldSymbols';
import { InkField, InkTool } from './InkField';
import { List, ListFieldName } from './List';
import { ObjectField } from './ObjectField';
@@ -52,6 +52,28 @@ export namespace Field {
default: return field?.[ToScriptString]?.() ?? 'null';
} // prettier-ignore
}
+ export function toJavascriptString(field: Field) {
+ var rawjava = '';
+
+ switch (typeof field) {
+ case 'string':
+ case 'number':
+ case 'boolean':rawjava = String(field);
+ break;
+ default: rawjava = field?.[ToJavascriptString]?.() ?? '';
+ } // prettier-ignore
+ var script = rawjava;
+ // this is a bit hacky, but we treat '^@' references to a published document
+ // as a kind of macro to include the content of those documents
+ Doc.MyPublishedDocs.forEach(doc => {
+ const regex = new RegExp(`^\\^${doc.title}\\s`, 'm');
+ const sections = (Cast(doc.text, RichTextField, null)?.Text ?? '').split('--DOCDATA--');
+ if (script.match(regex)) {
+ script = script.replace(regex, sections[0]) + (sections.length > 1 ? sections[1] : '');
+ }
+ });
+ return script;
+ }
export function toString(field: Field) {
if (typeof field === 'string' || typeof field === 'number' || typeof field === 'boolean') return String(field);
return field?.[ToString]?.() || '';
@@ -287,6 +309,8 @@ export class Doc extends RefField {
public [DocFields] = () => this[Self][FieldTuples]; // Object.keys(this).reduce((fields, key) => { fields[key] = this[key]; return fields; }, {} as any);
public [Width] = () => NumCast(this[SelfProxy]._width);
public [Height] = () => NumCast(this[SelfProxy]._height);
+ public [TransitionTimer]: any = undefined;
+ public [ToJavascriptString] = () => `idToDoc("${this[Self][Id]}")`; // what should go here?
public [ToScriptString] = () => `idToDoc("${this[Self][Id]}")`;
public [ToString] = () => `Doc(${GetEffectiveAcl(this[SelfProxy]) === AclPrivate ? '-inaccessible-' : this[SelfProxy].title})`;
public get [DocLayout]() { return this[SelfProxy].__LAYOUT__; } // prettier-ignore
@@ -367,7 +391,7 @@ export class Doc extends RefField {
export namespace Doc {
export function SetContainer(doc: Doc, container: Doc) {
- doc.embedContainer = container;
+ container !== Doc.MyRecentlyClosed && (doc.embedContainer = container);
}
export function RunCachedUpdate(doc: Doc, field: string) {
const update = doc[CachedUpdates][field];
@@ -508,12 +532,9 @@ export namespace Doc {
* Removes doc from the list of Docs at listDoc[fieldKey]
* @returns true if successful, false otherwise.
*/
- export function RemoveDocFromList(listDoc: Doc, fieldKey: string | undefined, doc: Doc) {
+ export function RemoveDocFromList(listDoc: Doc, fieldKey: string | undefined, doc: Doc, ignoreProto = false) {
const key = fieldKey ? fieldKey : Doc.LayoutFieldKey(listDoc);
- if (listDoc[key] === undefined) {
- listDoc[DocData][key] = new List<Doc>();
- }
- const list = Cast(listDoc[key], listSpec(Doc));
+ const list = Doc.Get(listDoc, key, ignoreProto) === undefined ? (listDoc[DocData][key] = new List<Doc>()) : Cast(listDoc[key], listSpec(Doc));
if (list) {
const ind = list.indexOf(doc);
if (ind !== -1) {
@@ -528,12 +549,9 @@ export namespace Doc {
* Adds doc to the list of Docs stored at listDoc[fieldKey].
* @returns true if successful, false otherwise.
*/
- export function AddDocToList(listDoc: Doc, fieldKey: string | undefined, doc: Doc, relativeTo?: Doc, before?: boolean, first?: boolean, allowDuplicates?: boolean, reversed?: boolean) {
+ export function AddDocToList(listDoc: Doc, fieldKey: string | undefined, doc: Doc, relativeTo?: Doc, before?: boolean, first?: boolean, allowDuplicates?: boolean, reversed?: boolean, ignoreProto?: boolean) {
const key = fieldKey ? fieldKey : Doc.LayoutFieldKey(listDoc);
- if (listDoc[key] === undefined) {
- listDoc[DocData][key] = new List<Doc>();
- }
- const list = Cast(listDoc[key], listSpec(Doc));
+ const list = Doc.Get(listDoc, key, ignoreProto) === undefined ? (listDoc[DocData][key] = new List<Doc>()) : Cast(listDoc[key], listSpec(Doc));
if (list) {
if (!allowDuplicates) {
const pind = list.findIndex(d => d instanceof Doc && d[Id] === doc[Id]);
@@ -558,6 +576,16 @@ export namespace Doc {
return false;
}
+ export function RemoveEmbedding(doc: Doc, embedding: Doc) {
+ Doc.RemoveDocFromList(doc[DocData], 'proto_embeddings', embedding);
+ }
+ export function AddEmbedding(doc: Doc, embedding: Doc) {
+ Doc.AddDocToList(doc[DocData], 'proto_embeddings', embedding, undefined, undefined, undefined, undefined, undefined, true);
+ }
+ export function GetEmbeddings(doc: Doc) {
+ return DocListCast(Doc.Get(doc[DocData], 'proto_embeddings', true));
+ }
+
export function MakeEmbedding(doc: Doc, id?: string) {
const embedding = (!GetT(doc, 'isDataDoc', 'boolean', true) && doc.proto) || doc.type === DocumentType.CONFIG ? Doc.MakeCopy(doc, undefined, id) : Doc.MakeDelegate(doc, id);
const layout = Doc.LayoutField(embedding);
@@ -565,20 +593,18 @@ export namespace Doc {
Doc.SetLayout(embedding, Doc.MakeEmbedding(layout));
}
embedding.createdFrom = doc;
- embedding.proto_embeddingId = doc[DocData].proto_embeddingId = DocListCast(doc[DocData].proto_embeddings).length - 1;
+ embedding.proto_embeddingId = doc[DocData].proto_embeddingId = Doc.GetEmbeddings(doc).length - 1;
!Doc.GetT(embedding, 'title', 'string', true) && (embedding.title = ComputedField.MakeFunction(`renameEmbedding(this)`));
embedding.author = Doc.CurrentUserEmail;
- Doc.AddDocToList(doc[DocData], 'proto_embeddings', embedding);
-
return embedding;
}
export function BestEmbedding(doc: Doc) {
const dataDoc = doc[DocData];
- const availableEmbeddings = DocListCast(dataDoc.proto_embeddings);
+ const availableEmbeddings = Doc.GetEmbeddings(dataDoc);
const bestEmbedding = [...(dataDoc !== doc ? [doc] : []), ...availableEmbeddings].find(doc => !doc.embedContainer && doc.author === Doc.CurrentUserEmail);
- bestEmbedding && Doc.AddDocToList(dataDoc, 'protoEmbeddings', doc);
+ bestEmbedding && Doc.AddDocToList(dataDoc, 'protoEmbeddings', doc, undefined, undefined, undefined, undefined, undefined, true);
return bestEmbedding ?? Doc.MakeEmbedding(doc);
}
@@ -852,7 +878,7 @@ export namespace Doc {
export function FindReferences(infield: Doc | List<any>, references: Set<Doc>, system: boolean | undefined) {
if (infield instanceof Promise) return;
if (!(infield instanceof Doc)) {
- infield.forEach(val => (val instanceof Doc || val instanceof List) && FindReferences(val, references, system));
+ infield?.forEach(val => (val instanceof Doc || val instanceof List) && FindReferences(val, references, system));
return;
}
const doc = infield as Doc;
@@ -937,7 +963,7 @@ export namespace Doc {
Doc.GetProto(copy).embedContainer = undefined;
Doc.GetProto(copy).proto_embeddings = new List<Doc>([copy]);
} else {
- Doc.AddDocToList(copy[DocData], 'proto_embeddings', copy);
+ Doc.AddEmbedding(copy, copy);
}
copy.embedContainer = undefined;
if (retitle) {
@@ -958,9 +984,10 @@ export namespace Doc {
Object.keys(doc)
.filter(key => key.startsWith('acl'))
.forEach(key => (delegate[key] = doc[key]));
- if (!Doc.IsSystem(doc)) Doc.AddDocToList(doc[DocData], 'proto_embeddings', delegate);
+ if (!Doc.IsSystem(doc)) Doc.AddEmbedding(doc, delegate);
title && (delegate.title = title);
delegate[Initializing] = false;
+ Doc.AddEmbedding(doc, delegate);
return delegate;
}
return undefined;
@@ -981,7 +1008,7 @@ export namespace Doc {
delegate[Initializing] = true;
delegate.proto = delegateProto;
delegate.author = Doc.CurrentUserEmail;
- Doc.AddDocToList(delegateProto[DocData], 'proto_embeddings', delegate);
+ Doc.AddEmbedding(delegateProto, delegate);
delegate[Initializing] = false;
delegateProto[Initializing] = false;
return delegate;
@@ -1018,13 +1045,13 @@ export namespace Doc {
// This function converts a generic field layout display into a field layout that displays a specific
// metadata field indicated by the title of the template field (not the default field that it was rendering)
//
- export function MakeMetadataFieldTemplate(templateField: Doc, templateDoc: Opt<Doc>): boolean {
+ export function MakeMetadataFieldTemplate(templateField: Doc, templateDoc: Opt<Doc>, keepFieldKey = false): boolean {
// find the metadata field key that this template field doc will display (indicated by its title)
- const metadataFieldKey = StrCast(templateField.isTemplateForField) || StrCast(templateField.title).replace(/^-/, '');
+ const metadataFieldKey = keepFieldKey ? Doc.LayoutFieldKey(templateField) : StrCast(templateField.isTemplateForField) || StrCast(templateField.title).replace(/^-/, '') || Doc.LayoutFieldKey(templateField);
// update the original template to mark it as a template
templateField.isTemplateForField = metadataFieldKey;
- templateField.title = metadataFieldKey;
+ !keepFieldKey && (templateField.title = metadataFieldKey);
const templateFieldValue = templateField[metadataFieldKey] || templateField[Doc.LayoutFieldKey(templateField)];
const templateCaptionValue = templateField.caption;
@@ -1640,3 +1667,6 @@ ScriptingGlobals.add(function setDocFilter(container: Doc, key: string, value: a
ScriptingGlobals.add(function setDocRangeFilter(container: Doc, key: string, range: number[]) {
Doc.setDocRangeFilter(container, key, range);
});
+ScriptingGlobals.add(function toJavascriptString(str: string) {
+ return Field.toJavascriptString(str as Field);
+});