diff options
author | bob <bcz@cs.brown.edu> | 2020-02-25 12:45:33 -0500 |
---|---|---|
committer | bob <bcz@cs.brown.edu> | 2020-02-25 12:45:33 -0500 |
commit | c5c8c99151de576548dab215b82d44132b06f9a4 (patch) | |
tree | 9c05f024525aefb8f47b7837bfd88339eed0eb2b | |
parent | 9399d2a44261ffce3f33cf1ea2dac032a8d3fdc8 (diff) |
added inheritable computations to template fields
-rw-r--r-- | src/client/util/RichTextSchema.tsx | 27 | ||||
-rw-r--r-- | src/new_fields/ScriptField.ts | 2 |
2 files changed, 17 insertions, 12 deletions
diff --git a/src/client/util/RichTextSchema.tsx b/src/client/util/RichTextSchema.tsx index 626d9d75d..cc6b035d7 100644 --- a/src/client/util/RichTextSchema.tsx +++ b/src/client/util/RichTextSchema.tsx @@ -21,6 +21,7 @@ import { DocumentManager } from "./DocumentManager"; import ParagraphNodeSpec from "./ParagraphNodeSpec"; import { Transform } from "./Transform"; import React = require("react"); +import { CollectionSchemaBooleanCell } from "../views/collections/CollectionSchemaCells"; const blockquoteDOM: DOMOutputSpecArray = ["blockquote", 0], hrDOM: DOMOutputSpecArray = ["hr"], preDOM: DOMOutputSpecArray = ["pre", ["code", 0]], brDOM: DOMOutputSpecArray = ["br"], ulDOM: DOMOutputSpecArray = ["ul", 0]; @@ -869,6 +870,16 @@ export class DashFieldView { this._fieldWrapper.style.position = "relative"; this._fieldWrapper.style.display = "inline-block"; + const onchanged = (e: any) => { + this._reactionDisposer?.(); + let newText = this._fieldSpan.innerText.startsWith(":=") ? ":=-computed-" : this._fieldSpan.innerText; + this._options?.forEach(opt => StrCast(opt.title).startsWith(newText) && (newText = StrCast(opt.title))); + this._dashDoc![this._fieldKey] = newText; + if (newText.startsWith(":=") && this._dashDoc && e.data === null && !e.inputType.includes("delete")) { + Doc.Layout(tbox.props.Document)[this._fieldKey] = ComputedField.MakeFunction(this._fieldSpan.innerText.substring(2)); + } + } + this._fieldSpan = document.createElement("div"); this._fieldSpan.id = Utils.GenerateGuid(); this._fieldSpan.contentEditable = "true"; @@ -876,7 +887,7 @@ export class DashFieldView { this._fieldSpan.style.display = "inline-block"; this._fieldSpan.style.minWidth = "50px"; this._fieldSpan.style.backgroundColor = "rgba(155, 155, 155, 0.24)"; - this._fieldSpan.addEventListener("input", this.onchanged); + this._fieldSpan.addEventListener("input", onchanged); this._fieldSpan.onkeypress = function (e: any) { e.stopPropagation(); }; this._fieldSpan.onkeyup = function (e: any) { e.stopPropagation(); }; this._fieldSpan.onmousedown = function (e: any) { e.stopPropagation(); }; @@ -913,21 +924,15 @@ export class DashFieldView { setDashDoc(tbox.props.DataDoc || tbox.dataDoc); } this._reactionDisposer?.(); - this._reactionDisposer = reaction(() => this._dashDoc?.[node.attrs.fieldKey], fval => this._fieldSpan.innerHTML = Field.toString(fval as Field) || "(null)", { fireImmediately: true }); + this._reactionDisposer = reaction(() => { + const dashVal = this._dashDoc?.[node.attrs.fieldKey]; + return StrCast(dashVal).startsWith(":=") || !dashVal ? Doc.Layout(tbox.props.Document)[this._fieldKey] : dashVal; + }, fval => this._fieldSpan.innerHTML = Field.toString(fval as Field) || "(null)", { fireImmediately: true }); this._fieldWrapper.appendChild(this._labelSpan); this._fieldWrapper.appendChild(this._fieldSpan); (this as any).dom = this._fieldWrapper; } - onchanged = () => { - this._reactionDisposer?.(); - - let newText = this._fieldSpan.innerText; - this._options?.forEach(opt => StrCast(opt.title).startsWith(newText) && (newText = StrCast(opt.title))); - this._dashDoc![this._fieldKey] = newText; - this._reactionDisposer = reaction(() => this._dashDoc?.[this._fieldKey], fval => this._fieldSpan.innerHTML = Field.toString(fval as Field) || "(null)"); - - } destroy() { this._reactionDisposer?.(); } diff --git a/src/new_fields/ScriptField.ts b/src/new_fields/ScriptField.ts index 4c78ea3aa..131e1f339 100644 --- a/src/new_fields/ScriptField.ts +++ b/src/new_fields/ScriptField.ts @@ -130,7 +130,7 @@ export class ScriptField extends ObjectField { export class ComputedField extends ScriptField { _lastComputedResult: any; //TODO maybe add an observable cache based on what is passed in for doc, considering there shouldn't really be that many possible values for doc - value = computedFn((doc: Doc) => this._lastComputedResult = this.script.run({ this: doc, _last_: this._lastComputedResult }, console.log).result); + value = computedFn((doc: Doc) => this._lastComputedResult = this.script.run({ source: doc.expandedTemplate || doc, this: doc, _last_: this._lastComputedResult }, console.log).result); public static MakeScript(script: string, params: object = {}) { const compiled = ScriptField.CompileScript(script, params, false); return compiled.compiled ? new ComputedField(compiled) : undefined; |