aboutsummaryrefslogtreecommitdiff
path: root/src/client/views/nodes/KeyValueBox.tsx
diff options
context:
space:
mode:
authormonikahedman <monika_hedman@brown.edu>2019-07-14 17:08:03 -0400
committermonikahedman <monika_hedman@brown.edu>2019-07-14 17:08:03 -0400
commit7f011d633021fece4d071b741f8571440236ea71 (patch)
treece2e7dc7c29a2a2ebab57efbc8001b2d753819d8 /src/client/views/nodes/KeyValueBox.tsx
parent2575564d70828820521074455383e940d521cca8 (diff)
parent7d9e29690956327d1ed9981cd2882d08b72b5c86 (diff)
pulled from master
Diffstat (limited to 'src/client/views/nodes/KeyValueBox.tsx')
-rw-r--r--src/client/views/nodes/KeyValueBox.tsx97
1 files changed, 56 insertions, 41 deletions
diff --git a/src/client/views/nodes/KeyValueBox.tsx b/src/client/views/nodes/KeyValueBox.tsx
index 9407d742c..c9dd9a64e 100644
--- a/src/client/views/nodes/KeyValueBox.tsx
+++ b/src/client/views/nodes/KeyValueBox.tsx
@@ -2,13 +2,13 @@
import { action, computed, observable } from "mobx";
import { observer } from "mobx-react";
import 'react-image-lightbox/style.css'; // This only needs to be imported once in your app
-import { CompileScript, ScriptOptions } from "../../util/Scripting";
+import { CompileScript, ScriptOptions, CompiledScript } from "../../util/Scripting";
import { FieldView, FieldViewProps } from './FieldView';
import "./KeyValueBox.scss";
import { KeyValuePair } from "./KeyValuePair";
import React = require("react");
import { NumCast, Cast, FieldValue, StrCast } from "../../../new_fields/Types";
-import { Doc, Field, FieldResult } from "../../../new_fields/Doc";
+import { Doc, Field, FieldResult, DocListCastAsync } from "../../../new_fields/Doc";
import { ComputedField, ScriptField } from "../../../new_fields/ScriptField";
import { SetupDrag } from "../../util/DragManager";
import { Docs } from "../../documents/Documents";
@@ -18,6 +18,14 @@ import { List } from "../../../new_fields/List";
import { TextField } from "../../util/ProsemirrorCopy/prompt";
import { RichTextField } from "../../../new_fields/RichTextField";
import { ImageField } from "../../../new_fields/URLField";
+import { SelectionManager } from "../../util/SelectionManager";
+import { listSpec } from "../../../new_fields/Schema";
+
+export type KVPScript = {
+ script: CompiledScript;
+ type: "computed" | "script" | false;
+ onDelegate: boolean;
+};
@observer
export class KeyValueBox extends React.Component<FieldViewProps> {
@@ -46,22 +54,27 @@ export class KeyValueBox extends React.Component<FieldViewProps> {
}
}
}
- public static SetField(doc: Doc, key: string, value: string) {
+ public static CompileKVPScript(value: string): KVPScript | undefined {
let eq = value.startsWith("=");
- let target = eq ? doc : Doc.GetProto(doc);
value = eq ? value.substr(1) : value;
- let dubEq = value.startsWith(":=") ? 1 : value.startsWith(";=") ? 2 : 0;
+ const dubEq = value.startsWith(":=") ? "computed" : value.startsWith(";=") ? "script" : false;
value = dubEq ? value.substr(2) : value;
let options: ScriptOptions = { addReturn: true, params: { this: "Doc" } };
if (dubEq) options.typecheck = false;
let script = CompileScript(value, options);
if (!script.compiled) {
- return false;
+ return undefined;
}
+ return { script, type: dubEq, onDelegate: eq };
+ }
+
+ public static ApplyKVPScript(doc: Doc, key: string, kvpScript: KVPScript): boolean {
+ const { script, type, onDelegate } = kvpScript;
+ const target = onDelegate ? doc : Doc.GetProto(doc);
let field: Field;
- if (dubEq === 1) {
+ if (type === "computed") {
field = new ComputedField(script);
- } else if (dubEq === 2) {
+ } else if (type === "script") {
field = new ScriptField(script);
} else {
let res = script.run({ this: target });
@@ -75,6 +88,12 @@ export class KeyValueBox extends React.Component<FieldViewProps> {
return false;
}
+ public static SetField(doc: Doc, key: string, value: string) {
+ const script = this.CompileKVPScript(value);
+ if (!script) return false;
+ return this.ApplyKVPScript(doc, key, script);
+ }
+
onPointerDown = (e: React.PointerEvent): void => {
if (e.buttons === 1 && this.props.isSelected()) {
e.stopPropagation();
@@ -157,9 +176,9 @@ export class KeyValueBox extends React.Component<FieldViewProps> {
}
getTemplate = async () => {
- let parent = Docs.StackingDocument([], { width: 800, height: 800, title: "Template" });
+ let parent = Docs.Create.StackingDocument([], { width: 800, height: 800, title: "Template" });
parent.singleColumn = false;
- parent.columnWidth = 50;
+ parent.columnWidth = 100;
for (let row of this.rows.filter(row => row.isChecked)) {
await this.createTemplateField(parent, row);
row.uncheck();
@@ -167,45 +186,41 @@ export class KeyValueBox extends React.Component<FieldViewProps> {
return parent;
}
- createTemplateField = async (parent: Doc, row: KeyValuePair) => {
- let collectionKeyProp = `fieldKey={"data"}`;
+ createTemplateField = async (parentStackingDoc: Doc, row: KeyValuePair) => {
let metaKey = row.props.keyName;
- let metaKeyProp = `fieldKey={"${metaKey}"}`;
-
let sourceDoc = await Cast(this.props.Document.data, Doc);
if (!sourceDoc) {
return;
}
- let target = this.inferType(sourceDoc[metaKey], metaKey);
-
- let template = Doc.MakeAlias(target);
- template.proto = parent;
- template.title = metaKey;
- template.nativeWidth = 0;
- template.nativeHeight = 0;
- template.embed = true;
- template.isTemplate = true;
- template.templates = new List<string>([Templates.TitleBar(metaKey)]);
- if (target.backgroundLayout) {
- let metaAnoKeyProp = `fieldKey={"${metaKey}"} fieldExt={"annotations"}`;
- let collectionAnoKeyProp = `fieldKey={"annotations"}`;
- template.layout = StrCast(target.layout).replace(collectionAnoKeyProp, metaAnoKeyProp);
- template.backgroundLayout = StrCast(target.backgroundLayout).replace(collectionKeyProp, metaKeyProp);
- } else {
- template.layout = StrCast(target.layout).replace(collectionKeyProp, metaKeyProp);
- }
- Doc.AddDocToList(parent, "data", template);
- row.uncheck();
+
+ let fieldTemplate = await this.inferType(sourceDoc[metaKey], metaKey);
+ let previousViewType = fieldTemplate.viewType;
+ Doc.MakeTemplate(fieldTemplate, metaKey, Doc.GetProto(parentStackingDoc));
+ previousViewType && (fieldTemplate.viewType = previousViewType);
+
+ Cast(parentStackingDoc.data, listSpec(Doc))!.push(fieldTemplate);
}
- inferType = (field: FieldResult, metaKey: string) => {
+ inferType = async (data: FieldResult, metaKey: string) => {
let options = { width: 300, height: 300, title: metaKey };
- if (field instanceof RichTextField || typeof field === "string" || typeof field === "number") {
- return Docs.TextDocument(options);
- } else if (field instanceof List) {
- return Docs.StackingDocument([], options);
- } else if (field instanceof ImageField) {
- return Docs.ImageDocument("https://www.freepik.com/free-icon/picture-frame-with-mountain-image_748687.htm", options);
+ if (data instanceof RichTextField || typeof data === "string" || typeof data === "number") {
+ return Docs.Create.TextDocument(options);
+ } else if (data instanceof List) {
+ if (data.length === 0) {
+ return Docs.Create.StackingDocument([], options);
+ }
+ let first = await Cast(data[0], Doc);
+ if (!first) {
+ return Docs.Create.StackingDocument([], options);
+ }
+ switch (first.type) {
+ case "image":
+ return Docs.Create.StackingDocument([], options);
+ case "text":
+ return Docs.Create.TreeDocument([], options);
+ }
+ } else if (data instanceof ImageField) {
+ return Docs.Create.ImageDocument("https://image.flaticon.com/icons/png/512/23/23765.png", options);
}
return new Doc;
}