diff options
author | Bob Zeleznik <zzzman@gmail.com> | 2020-02-24 18:23:41 -0500 |
---|---|---|
committer | Bob Zeleznik <zzzman@gmail.com> | 2020-02-24 18:23:41 -0500 |
commit | 13e6971c50d4efbb71c551a4570bf59fd5e091c2 (patch) | |
tree | 86874b55d50cc8a48eca5af288fa9a40f22ea992 /src | |
parent | 07216856dbea356fed37858c90249add1b42d661 (diff) |
fixed scripting in schema's to allow documents to be assigned
Diffstat (limited to 'src')
-rw-r--r-- | src/client/util/type_decls.d | 1 | ||||
-rw-r--r-- | src/client/views/collections/CollectionSchemaCells.tsx | 8 | ||||
-rw-r--r-- | src/new_fields/Doc.ts | 6 |
3 files changed, 12 insertions, 3 deletions
diff --git a/src/client/util/type_decls.d b/src/client/util/type_decls.d index 127f7b798..97f6b79fb 100644 --- a/src/client/util/type_decls.d +++ b/src/client/util/type_decls.d @@ -207,4 +207,5 @@ declare const Docs: { StackingDocument(documents: Doc[], options?: DocumentOptions): Doc; }; +declare function assignDoc(doc:Doc, field:any, id:any):string; declare function d(...args:any[]):any; diff --git a/src/client/views/collections/CollectionSchemaCells.tsx b/src/client/views/collections/CollectionSchemaCells.tsx index 851fded71..facde3648 100644 --- a/src/client/views/collections/CollectionSchemaCells.tsx +++ b/src/client/views/collections/CollectionSchemaCells.tsx @@ -37,8 +37,8 @@ export interface CellProps { renderDepth: number; addDocTab: (document: Doc, where: string) => boolean; pinToPres: (document: Doc) => void; - moveDocument: (document: Doc, targetCollection: Doc | undefined, - addDocument: (document: Doc) => boolean) => boolean; + moveDocument: (document: Doc, targetCollection: Doc | undefined, + addDocument: (document: Doc) => boolean) => boolean; isFocused: boolean; changeFocusedCellByIndex: (row: number, col: number) => void; setIsEditing: (isEditing: boolean) => void; @@ -236,7 +236,9 @@ export class CollectionSchemaCell extends React.Component<CellProps> { const script = CompileScript(value, { requiredType: type, typecheck: false, editable: true, addReturn: true, params: { this: Doc.name, $r: "number", $c: "number", $: "any" } }); if (script.compiled) { DocListCast(this.props.Document[this.props.fieldKey]). - forEach((doc, i) => this.applyToDoc(doc, i, this.props.col, script.run)); + forEach((doc, i) => value.startsWith(":=") ? + this.props.setComputed(value.substring(2), doc, this.props.rowProps.column.id!, i, this.props.col) : + this.applyToDoc(doc, i, this.props.col, script.run)); } }} /> diff --git a/src/new_fields/Doc.ts b/src/new_fields/Doc.ts index a24746caf..6142ebb36 100644 --- a/src/new_fields/Doc.ts +++ b/src/new_fields/Doc.ts @@ -17,6 +17,7 @@ import { listSpec } from "./Schema"; import { ComputedField } from "./ScriptField"; import { Cast, FieldValue, NumCast, StrCast, ToConstructor } from "./Types"; import { deleteProperty, getField, getter, makeEditable, makeReadOnly, setter, updateFunction } from "./util"; +import { DocumentManager } from "../client/util/DocumentManager"; export namespace Field { export function toKeyValueString(doc: Doc, key: string): string { @@ -833,6 +834,10 @@ export namespace Doc { layoutDoc._nativeHeight = NumCast(layoutDoc._height, height); } } + export function assignDocToField(doc: Doc, field: string, id: string) { + DocServer.GetRefField(id).then(layout => layout instanceof Doc && (doc[field] = layout)); + return id; + } } Scripting.addGlobal(function renameAlias(doc: any, n: any) { return StrCast(Doc.GetProto(doc).title).replace(/\([0-9]*\)/, "") + `(${n})`; }); @@ -849,6 +854,7 @@ Scripting.addGlobal(function setNativeView(doc: any) { Doc.setNativeView(doc); } Scripting.addGlobal(function undo() { return UndoManager.Undo(); }); Scripting.addGlobal(function redo() { return UndoManager.Redo(); }); Scripting.addGlobal(function DOC(id: string) { console.log("Can't parse a document id in a script"); return "invalid"; }); +Scripting.addGlobal(function assignDoc(doc: Doc, field: string, id: string) { return Doc.assignDocToField(doc, field, id); }); Scripting.addGlobal(function curPresentationItem() { const curPres = Doc.UserDoc().curPresentation as Doc; return curPres && DocListCast(curPres[Doc.LayoutFieldKey(curPres)])[NumCast(curPres._itemIndex)]; |