From 0f178f1d74e17b15cec0fc98a12ccb2acaec937a Mon Sep 17 00:00:00 2001 From: anika-ahluwalia Date: Sat, 30 May 2020 12:52:40 -0500 Subject: generating suggestions for methods --- src/client/util/Scripting.ts | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) (limited to 'src/client/util/Scripting.ts') diff --git a/src/client/util/Scripting.ts b/src/client/util/Scripting.ts index ab577315c..817e6b29d 100644 --- a/src/client/util/Scripting.ts +++ b/src/client/util/Scripting.ts @@ -49,12 +49,25 @@ export function isCompileError(toBeDetermined: CompileResult): toBeDetermined is export namespace Scripting { export function addGlobal(global: { name: string }): void; export function addGlobal(name: string, global: any): void; - export function addGlobal(nameOrGlobal: any, global?: any) { - let n: string; + + export function addGlobal(global: { name: string }, decription?: string, params?: any): void; + + export function addGlobal(nameOrGlobal: any, global?: any, params?: any) { + let n: any; let obj: any; - if (global !== undefined && typeof nameOrGlobal === "string") { - n = nameOrGlobal; - obj = global; + + if (global !== undefined) { + if (typeof nameOrGlobal === "string") { + n = nameOrGlobal; + obj = global; + } else { + n = nameOrGlobal.name; + obj = [nameOrGlobal]; + obj.push(global); + if (params) { + obj.push(params); + } + } } else if (nameOrGlobal && typeof nameOrGlobal.name === "string") { n = nameOrGlobal.name; obj = nameOrGlobal; @@ -87,6 +100,10 @@ export namespace Scripting { export function getGlobals() { return Object.keys(scriptingGlobals); } + + export function getGlobalObj() { + return _scriptingGlobals; + } } export function scriptingGlobal(constructor: { new(...args: any[]): any }) { -- cgit v1.2.3-70-g09d2 From 823b8728b5506697b7d305a8c07979984b8351c8 Mon Sep 17 00:00:00 2001 From: anika-ahluwalia Date: Mon, 1 Jun 2020 15:27:57 -0500 Subject: adding descriptions and params to methods --- src/client/util/CurrentUserUtils.ts | 3 +- src/client/util/Scripting.ts | 1 - src/client/views/nodes/ScriptingBox.scss | 76 +++++++------- src/client/views/nodes/ScriptingBox.tsx | 168 ++++++++++++++++++++++++++----- 4 files changed, 180 insertions(+), 68 deletions(-) (limited to 'src/client/util/Scripting.ts') diff --git a/src/client/util/CurrentUserUtils.ts b/src/client/util/CurrentUserUtils.ts index 1fdf50dd4..632187b49 100644 --- a/src/client/util/CurrentUserUtils.ts +++ b/src/client/util/CurrentUserUtils.ts @@ -737,5 +737,6 @@ export class CurrentUserUtils { } Scripting.addGlobal("setupMobileInkingDoc", function setupMobileInkingDoc(userDoc: Doc) { return CurrentUserUtils.setupMobileInkingDoc(userDoc); }); -Scripting.addGlobal(function setupMobileUploadDoc(userDoc: Doc) { return CurrentUserUtils.setupMobileUploadDoc(userDoc); }); +Scripting.addGlobal(function setupMobileUploadDoc(userDoc: Doc) { return CurrentUserUtils.setupMobileUploadDoc(userDoc); }, + "initializes the Mobile upload document", "{ userDoc: the document to initialize }"); Scripting.addGlobal(function createNewWorkspace() { return MainView.Instance.createNewWorkspace(); }); \ No newline at end of file diff --git a/src/client/util/Scripting.ts b/src/client/util/Scripting.ts index 817e6b29d..00b01a75a 100644 --- a/src/client/util/Scripting.ts +++ b/src/client/util/Scripting.ts @@ -49,7 +49,6 @@ export function isCompileError(toBeDetermined: CompileResult): toBeDetermined is export namespace Scripting { export function addGlobal(global: { name: string }): void; export function addGlobal(name: string, global: any): void; - export function addGlobal(global: { name: string }, decription?: string, params?: any): void; export function addGlobal(nameOrGlobal: any, global?: any, params?: any) { diff --git a/src/client/views/nodes/ScriptingBox.scss b/src/client/views/nodes/ScriptingBox.scss index 256119809..6113efbb5 100644 --- a/src/client/views/nodes/ScriptingBox.scss +++ b/src/client/views/nodes/ScriptingBox.scss @@ -22,7 +22,7 @@ display: flex; flex-direction: row; justify-content: center; - overflow-y: scroll; + overflow-y: hidden; .scriptingBox-textArea { flex: 70; @@ -32,53 +32,43 @@ resize: none; padding: 7px; overflow-y: scroll; + overflow-x: hidden; body { font-family: Arial, Helvetica, sans-serif; border: 1px solid red; - } - + } + .rta { position: relative; - font-size: 18px; + font-size: 12px; width: 100%; height: 100%; margin-bottom: 60px !important; - } - .rta__loader.rta__loader--empty-suggestion-data { - border-radius: 3px; - box-shadow: 0 0 5px rgba(27, 31, 35, 0.1); - padding: 5px; - } - .rta--loading .rta__loader.rta__loader--suggestion-data { - position: absolute; - top: 0; - left: 0; - width: 100%; - height: 100%; - background: rgba(255, 255, 255, 0.8); - } - .rta--loading .rta__loader.rta__loader--suggestion-data > * { - position: relative; - top: 50%; - } - .rta__textarea { + overflow-y: scroll; + font-size: 9px; + } + + .rta__textarea { width: 100%; height: 100%; - font-size: 1em; - } - .rta__autocomplete { + font-size: 10px; + } + + .rta__autocomplete { position: absolute; display: block; margin-top: 1em; - } - .rta__autocomplete--top { + } + + .rta__autocomplete--top { margin-top: 0; margin-bottom: 1em; max-height: 100px; overflow-y: auto; - } - .rta__list { + } + + .rta__list { margin: 0; padding: 0; background: #fff; @@ -86,28 +76,30 @@ border-radius: 3px; box-shadow: 0 0 5px rgba(27, 31, 35, 0.1); list-style: none; - } - .rta__entity { + overflow-y: scroll; + } + + .rta__entity { background: white; width: 100%; text-align: left; outline: none; - } - .rta__entity:hover { + } + + .rta__entity:hover { cursor: pointer; - } - .rta__item:not(:last-child) { - border-bottom: 1px solid #dfe2e5; - } - .rta__entity > * { + } + + .rta__entity>* { padding-left: 4px; padding-right: 4px; - } - .rta__entity--selected { + } + + .rta__entity--selected { color: #fff; text-decoration: none; background: #0366d6; - } + } } .scriptingBox-plist { diff --git a/src/client/views/nodes/ScriptingBox.tsx b/src/client/views/nodes/ScriptingBox.tsx index b0f7b3993..72c92f00b 100644 --- a/src/client/views/nodes/ScriptingBox.tsx +++ b/src/client/views/nodes/ScriptingBox.tsx @@ -38,18 +38,62 @@ export class ScriptingBox extends ViewBoxAnnotatableComponent p.split(":")[0].trim()); } - @computed get paramsTypes() { return this.compileParams.map(p => p.split(":")[1].trim()); } - @computed get rawScript() { return StrCast(this.dataDoc[this.props.fieldKey + "-rawScript"], ""); } - @computed get compileParams() { return Cast(this.dataDoc[this.props.fieldKey + "-params"], listSpec("string"), []); } + @computed({ keepAlive: true }) get paramsNames() { return this.compileParams.map(p => p.split(":")[0].trim()); } + @computed({ keepAlive: true }) get paramsTypes() { return this.compileParams.map(p => p.split(":")[1].trim()); } + @computed({ keepAlive: true }) get rawScript() { return StrCast(this.dataDoc[this.props.fieldKey + "-rawScript"], ""); } + @computed({ keepAlive: true }) get compileParams() { return Cast(this.dataDoc[this.props.fieldKey + "-params"], listSpec("string"), []); } set rawScript(value) { this.dataDoc[this.props.fieldKey + "-rawScript"] = value; } set compileParams(value) { this.dataDoc[this.props.fieldKey + "-params"] = new List(value); } + // WORK ON THIS + // in: global, description, params + @computed get _descriptions() { + const descrip: string[] = []; + let value = ""; + this._scriptKeys.forEach((element: any) => { + const result = this._scriptGlobals[element]; + if (typeof result === "object") { + const d = result[1]; + if (d !== undefined) { + value = d; + } else { + value = ""; + } + } else { + value = ""; + } + descrip.push(value); + }); + return descrip; + } + + @computed get _scriptParams() { + const params: string[] = []; + let value = ""; + this._scriptKeys.forEach((element: any) => { + const result = this._scriptGlobals[element]; + if (typeof result === "object") { + const p = result[2]; + if (p !== undefined) { + value = StrCast(p); + } else { + value = ""; + } + } else { + value = ""; + } + params.push(value); + }); + return params; + } + @action componentDidMount() { this.rawScript = ScriptCast(this.dataDoc[this.props.fieldKey])?.script?.originalScript ?? this.rawScript; @@ -307,16 +351,32 @@ export class ScriptingBox extends ViewBoxAnnotatableComponent { + // if (element.indexOf(this._currWord) >= 0) { + // this._suggestions.push(StrCast(element)); + // } + // }); + + // console.log(this._suggestions); + // return (this._suggestions); + // } + + @action + handleToken(str: string) { + + this._currWord = str; this._suggestions = []; this._scriptKeys.forEach((element: string) => { - if (element.indexOf(this._currWord) >= 0) { + if (element.toLowerCase().indexOf(this._currWord.toLowerCase()) >= 0) { this._suggestions.push(StrCast(element)); } }); @@ -326,23 +386,67 @@ export class ScriptingBox extends ViewBoxAnnotatableComponent { - if (element.indexOf(this._currWord) >= 0) { - this._suggestions.push(StrCast(element)); - } - }); + const index = this._scriptKeys.indexOf(this._currWord); + const params = StrCast(this._scriptParams[index]); + + this._suggestions.push(params); console.log(this._suggestions); + return (this._suggestions); } + + getDescription(value: string) { + const index = this._scriptKeys.indexOf(value); + const descrip = this._descriptions[index]; + let display = ""; + if (descrip !== undefined) { + if (descrip.length > 0) { + display = descrip; + } + } + return display; + } + + getParams(value: string) { + const index = this._scriptKeys.indexOf(value); + const descrip = this._scriptParams[index]; + let display = ""; + if (descrip !== undefined) { + if (descrip.length > 0) { + display = descrip; + } + } + return display; + } + + setHovered(bool: boolean) { + this._hovered = bool; + } + + returnParam(item: string) { + const params = item.split(","); + let value = ""; + let first = true; + params.forEach((element) => { + if (first) { + value = element.split(":")[0].trim(); + first = false; + } else { + value = value + ", " + element.split(":")[0].trim(); + } + }); + return value; + } + textarea: any; - @computed get renderScriptingBox() { + @computed({ keepAlive: true }) get renderScriptingBox() { return 0 ? "70%" : "100%", resize: "none", height: "100%" }} movePopupAsYouType={true} loadingComponent={() => Loading} - ref={(rta: any) => { this.rta = rta; }} + + ref={(rta) => { this.rta = rta; }} //innerRef={textarea => { this.rawScript = textarea.value; }} minChar={0} - // ISSUE IS HEERE, doesn't display entity in a menu, prints hello for reach item in the list trigger={{ " ": { dataProvider: (token: any) => this.handleToken(token), + component: ({ entity: value }) => +
this.setHovered(true)} + onMouseLeave={() => this.setHovered(false)}> + {value} +
+ {this._hovered ?
{this.getDescription(value)}
: (null)} + {this._hovered ?
{this.getParams(value)}
: (null)} +
+ , + output: (item: any, trigger) => trigger + item.trim(), + }, + + "(": { + dataProvider: (token: any) => this.handleFunc(this.rta.getCaretPosition()), component: ({ entity: value }) =>
{value}
, - //afterWhitespace: true, - output: (item: any) => item, + output: (item: any) => "(" + this.returnParam(item) + ")", } + }} onCaretPositionChange={(number: any) => null} //this.handleKeyPress(number)} @@ -374,7 +494,7 @@ export class ScriptingBox extends ViewBoxAnnotatableComponent @@ -467,7 +587,7 @@ export class ScriptingBox extends ViewBoxAnnotatableComponent
this.props.isSelected(true) && e.stopPropagation()}> - {!this._applied ? this.renderScriptingInputs() : this.renderParamsInputs()} + {!this._applied ? this.renderScriptingInputs : this.renderParamsInputs()} {!this._applied ? this.renderScriptingTools() : this.renderParamsTools()}
-- cgit v1.2.3-70-g09d2 From 4ac63cedfdfd33b47c97e7cc8be60ec9b5490a3e Mon Sep 17 00:00:00 2001 From: anika-ahluwalia Date: Sat, 6 Jun 2020 12:48:25 -0500 Subject: changing { to ( and adding , --- src/client/util/CurrentUserUtils.ts | 4 ++-- src/client/util/DropConverter.ts | 2 +- src/client/util/LinkManager.ts | 2 +- src/client/util/Scripting.ts | 1 + src/client/views/GestureOverlay.tsx | 2 +- src/client/views/collections/CollectionDockingView.tsx | 2 +- src/client/views/nodes/ScriptingBox.tsx | 5 +++++ src/fields/ScriptField.ts | 2 +- 8 files changed, 13 insertions(+), 7 deletions(-) (limited to 'src/client/util/Scripting.ts') diff --git a/src/client/util/CurrentUserUtils.ts b/src/client/util/CurrentUserUtils.ts index 3dcb484be..8509a1e64 100644 --- a/src/client/util/CurrentUserUtils.ts +++ b/src/client/util/CurrentUserUtils.ts @@ -737,8 +737,8 @@ export class CurrentUserUtils { } Scripting.addGlobal(function setupMobileInkingDoc(userDoc: Doc) { return CurrentUserUtils.setupMobileInkingDoc(userDoc); }, - "initializes the Mobile inking document", "{ userDoc: Doc }"); + "initializes the Mobile inking document", "(userDoc: Doc)"); Scripting.addGlobal(function setupMobileUploadDoc(userDoc: Doc) { return CurrentUserUtils.setupMobileUploadDoc(userDoc); }, - "initializes the Mobile upload document", "{ userDoc: Doc }"); + "initializes the Mobile upload document", "(userDoc: Doc)"); Scripting.addGlobal(function createNewWorkspace() { return MainView.Instance.createNewWorkspace(); }, "creates a new workspace when called"); \ No newline at end of file diff --git a/src/client/util/DropConverter.ts b/src/client/util/DropConverter.ts index 785e087f9..ea1769d85 100644 --- a/src/client/util/DropConverter.ts +++ b/src/client/util/DropConverter.ts @@ -77,4 +77,4 @@ export function convertDropDataToButtons(data: DragManager.DocumentDragData) { }); } Scripting.addGlobal(function convertToButtons(dragData: any) { convertDropDataToButtons(dragData as DragManager.DocumentDragData); }, - "converts the dropped data to buttons", "{ dragData: any }"); \ No newline at end of file + "converts the dropped data to buttons", "(dragData: any)"); \ No newline at end of file diff --git a/src/client/util/LinkManager.ts b/src/client/util/LinkManager.ts index 9b20af4cb..133f40d5a 100644 --- a/src/client/util/LinkManager.ts +++ b/src/client/util/LinkManager.ts @@ -212,4 +212,4 @@ export class LinkManager { } Scripting.addGlobal(function links(doc: any) { return new List(LinkManager.Instance.getAllRelatedLinks(doc)); }, - "creates a link to inputted document", "{ doc: any }"); \ No newline at end of file + "creates a link to inputted document", "(doc: any)"); \ No newline at end of file diff --git a/src/client/util/Scripting.ts b/src/client/util/Scripting.ts index 00b01a75a..817e6b29d 100644 --- a/src/client/util/Scripting.ts +++ b/src/client/util/Scripting.ts @@ -49,6 +49,7 @@ export function isCompileError(toBeDetermined: CompileResult): toBeDetermined is export namespace Scripting { export function addGlobal(global: { name: string }): void; export function addGlobal(name: string, global: any): void; + export function addGlobal(global: { name: string }, decription?: string, params?: any): void; export function addGlobal(nameOrGlobal: any, global?: any, params?: any) { diff --git a/src/client/views/GestureOverlay.tsx b/src/client/views/GestureOverlay.tsx index facfd1ab5..e9a848a5a 100644 --- a/src/client/views/GestureOverlay.tsx +++ b/src/client/views/GestureOverlay.tsx @@ -818,4 +818,4 @@ Scripting.addGlobal(function resetPen() { }, "resets the pen tool"); Scripting.addGlobal(function createText(text: any, x: any, y: any) { GestureOverlay.Instance.dispatchGesture("text", [{ X: x, Y: y }], text); -}, "creates a text document with inputted text and coordinates", "{ text: any, x: any, y: any }"); \ No newline at end of file +}, "creates a text document with inputted text and coordinates", "(text: any, x: any, y: any)"); \ No newline at end of file diff --git a/src/client/views/collections/CollectionDockingView.tsx b/src/client/views/collections/CollectionDockingView.tsx index 6e27169e2..8c0b0a1c8 100644 --- a/src/client/views/collections/CollectionDockingView.tsx +++ b/src/client/views/collections/CollectionDockingView.tsx @@ -860,5 +860,5 @@ export class DockedFrameRenderer extends React.Component { } } Scripting.addGlobal(function openOnRight(doc: any) { CollectionDockingView.AddRightSplit(doc); }, - "opens up the inputted document on the right side of the screen", "{ doc: any }"); + "opens up the inputted document on the right side of the screen", "(doc: any)"); Scripting.addGlobal(function useRightSplit(doc: any, shiftKey?: boolean) { CollectionDockingView.UseRightSplit(doc, undefined, shiftKey); }); diff --git a/src/client/views/nodes/ScriptingBox.tsx b/src/client/views/nodes/ScriptingBox.tsx index 071a990a3..f44f4fdad 100644 --- a/src/client/views/nodes/ScriptingBox.tsx +++ b/src/client/views/nodes/ScriptingBox.tsx @@ -524,6 +524,11 @@ export class ScriptingBox extends ViewBoxAnnotatableComponent { + if (i !== parameters.length - 1) { + parameters[i] = element + ","; + } + }); console.log("numEntered: " + numEntered); diff --git a/src/fields/ScriptField.ts b/src/fields/ScriptField.ts index d1a1c46cf..11b3b0524 100644 --- a/src/fields/ScriptField.ts +++ b/src/fields/ScriptField.ts @@ -161,7 +161,7 @@ export class ComputedField extends ScriptField { Scripting.addGlobal(function getIndexVal(list: any[], index: number) { return list.reduce((p, x, i) => (i <= index && x !== undefined) || p === undefined ? x : p, undefined as any); -}, "returns the value at a given index of a list", "{ list: any[], index: number }"); +}, "returns the value at a given index of a list", "(list: any[], index: number)"); export namespace ComputedField { let useComputed = true; -- cgit v1.2.3-70-g09d2 From b21aeae569c3ebcf2538918a13cf64b9d4c8bade Mon Sep 17 00:00:00 2001 From: anika-ahluwalia Date: Mon, 8 Jun 2020 10:57:59 -0500 Subject: adding ScriptManager and saving --- src/client/documents/DocumentTypes.ts | 1 + src/client/documents/Documents.ts | 12 +++++++ src/client/util/ScriptManager.ts | 63 +++++++++++++++++++++++++++++++++ src/client/util/Scripting.ts | 9 +++-- src/client/views/nodes/ScriptingBox.tsx | 39 +++++++++++++++----- 5 files changed, 113 insertions(+), 11 deletions(-) create mode 100644 src/client/util/ScriptManager.ts (limited to 'src/client/util/Scripting.ts') diff --git a/src/client/documents/DocumentTypes.ts b/src/client/documents/DocumentTypes.ts index 06d35038a..7ba21b2f6 100644 --- a/src/client/documents/DocumentTypes.ts +++ b/src/client/documents/DocumentTypes.ts @@ -34,5 +34,6 @@ export enum DocumentType { COMPARISON = "comparison", // before/after view with slider (view of 2 images) LINKDB = "linkdb", // database of links ??? why do we have this + SCRIPTDB = "scriptdb", // database of scripts RECOMMENDATION = "recommendation", // view of a recommendation } \ No newline at end of file diff --git a/src/client/documents/Documents.ts b/src/client/documents/Documents.ts index 38fe04534..27fbbc433 100644 --- a/src/client/documents/Documents.ts +++ b/src/client/documents/Documents.ts @@ -262,6 +262,11 @@ export namespace Docs { layout: { view: EmptyBox, dataField: defaultDataKey }, options: { childDropAction: "alias", title: "Global Link Database" } }], + [DocumentType.SCRIPTDB, { + data: new List(), + layout: { view: EmptyBox, dataField: defaultDataKey }, + options: { childDropAction: "alias", title: "Global Script Database" } + }], [DocumentType.SCRIPTING, { layout: { view: ScriptingBox, dataField: defaultDataKey } }], @@ -360,6 +365,13 @@ export namespace Docs { return Prototypes.get(DocumentType.LINKDB); } + /** + * A collection of all scripts in the database + */ + export function MainScriptDocument() { + return Prototypes.get(DocumentType.SCRIPTDB); + } + /** * This is a convenience method that is used to initialize * prototype documents for the first time. diff --git a/src/client/util/ScriptManager.ts b/src/client/util/ScriptManager.ts new file mode 100644 index 000000000..5bddb44ca --- /dev/null +++ b/src/client/util/ScriptManager.ts @@ -0,0 +1,63 @@ +import { Doc, DocListCast } from "../../fields/Doc"; +import { List } from "../../fields/List"; +import { Docs } from "../documents/Documents"; +import { Scripting, ScriptParam } from "./Scripting"; +import { StrCast } from "../../fields/Types"; + + +export class ScriptManager { + + private static _instance: ScriptManager; + public static get Instance(): ScriptManager { + return this._instance || (this._instance = new this()); + } + private constructor() { + } + + public get ScriptManagerDoc(): Doc | undefined { + return Docs.Prototypes.MainScriptDocument(); + } + + public getAllScripts(): Doc[] { + const sdoc = ScriptManager.Instance.ScriptManagerDoc; + if (sdoc) { + const docs = DocListCast(sdoc.data); + return docs; + } + return []; + } + + public addScript(scriptDoc: Doc): boolean { + const scriptList = ScriptManager.Instance.getAllScripts(); + scriptList.push(scriptDoc); + if (ScriptManager.Instance.ScriptManagerDoc) { + ScriptManager.Instance.ScriptManagerDoc.data = new List(scriptList); + return true; + } + return false; + } + + public deleteScript(scriptDoc: Doc): boolean { + const scriptList = ScriptManager.Instance.getAllScripts(); + const index = ScriptManager.Instance.getAllScripts().indexOf(scriptDoc); + if (index > -1) { + scriptList.splice(index, 1); + if (ScriptManager.Instance.ScriptManagerDoc) { + ScriptManager.Instance.ScriptManagerDoc.data = new List(scriptList); + return true; + } + } + return false; + } +} + +const scriptList = ScriptManager.Instance.getAllScripts(); + +scriptList.forEach((scriptDoc: Doc) => { + + const p = scriptDoc.compileParams?.reduce((o: ScriptParam, p: string) => { o[p] = "any"; return o; }, {} as ScriptParam); + const f = new Function(...Array.from(Object.keys(p)), StrCast(scriptDoc.rawScript)); + + Scripting.addGlobal(f, StrCast(scriptDoc.description), StrCast(p), StrCast(scriptDoc.name)); + +}); \ No newline at end of file diff --git a/src/client/util/Scripting.ts b/src/client/util/Scripting.ts index 817e6b29d..5619b22b0 100644 --- a/src/client/util/Scripting.ts +++ b/src/client/util/Scripting.ts @@ -51,8 +51,9 @@ export namespace Scripting { export function addGlobal(name: string, global: any): void; export function addGlobal(global: { name: string }, decription?: string, params?: any): void; + export function addGlobal(global: { name: string }, decription?: string, params?: any, name?: any): void; - export function addGlobal(nameOrGlobal: any, global?: any, params?: any) { + export function addGlobal(nameOrGlobal: any, global?: any, params?: any, name?: any) { let n: any; let obj: any; @@ -61,12 +62,16 @@ export namespace Scripting { n = nameOrGlobal; obj = global; } else { - n = nameOrGlobal.name; obj = [nameOrGlobal]; obj.push(global); if (params) { obj.push(params); } + if (name) { + n = name; + } else { + n = nameOrGlobal.name; + } } } else if (nameOrGlobal && typeof nameOrGlobal.name === "string") { n = nameOrGlobal.name; diff --git a/src/client/views/nodes/ScriptingBox.tsx b/src/client/views/nodes/ScriptingBox.tsx index 149640c07..6f94ae8f9 100644 --- a/src/client/views/nodes/ScriptingBox.tsx +++ b/src/client/views/nodes/ScriptingBox.tsx @@ -22,6 +22,7 @@ const _global = (window /* browser */ || global /* node */) as any; import ReactTextareaAutocomplete from "@webscopeio/react-textarea-autocomplete"; import "@webscopeio/react-textarea-autocomplete/style.css"; +import { ScriptManager } from "../../util/ScriptManager"; const ScriptingSchema = createSchema({}); @@ -184,23 +185,30 @@ export class ScriptingBox extends ViewBoxAnnotatableComponent { - this.onCompile(); - const bindings: { [name: string]: any } = {}; - this.paramsNames.forEach(key => bindings[key] = this.dataDoc[key]); - // binds vars so user doesnt have to refer to everything as self. - ScriptCast(this.dataDoc.data, null)?.script.run({ self: this.rootDoc, this: this.layoutDoc, ...bindings }, this.onError); + if (this.onCompile()) { + const bindings: { [name: string]: any } = {}; + this.paramsNames.forEach(key => bindings[key] = this.dataDoc[key]); + // binds vars so user doesnt have to refer to everything as self. + ScriptCast(this.dataDoc.data, null)?.script.run({ self: this.rootDoc, this: this.layoutDoc, ...bindings }, this.onError); + } } // checks if the script compiles and switches to applied UI @action onApply = () => { - this.onCompile(); - this._applied = true; + if (this.onCompile()) { + this._applied = true; + } } @action @@ -208,6 +216,17 @@ export class ScriptingBox extends ViewBoxAnnotatableComponent { + if (this.onCompile()) { + this.dataDoc.funcName = "testingTitle"; + this.dataDoc.descripition = "description test"; + ScriptManager.Instance.addScript(this.dataDoc); + } else { + this._errorMessage = "Can not save script, does not compile"; + } + } + // overlays document numbers (ex. d32) over all documents when clicked on onFocus = () => { this._overlayDisposer?.(); @@ -653,8 +672,10 @@ export class ScriptingBox extends ViewBoxAnnotatableComponent - - + + + + {this.rootDoc.layoutKey !== "layout_onClick" ? (null) : } ; -- cgit v1.2.3-70-g09d2 From c232bbfd28937c139ae18727310c91d9c6c2dbec Mon Sep 17 00:00:00 2001 From: anika-ahluwalia Date: Mon, 8 Jun 2020 13:07:11 -0500 Subject: added function UI --- src/client/util/ScriptManager.ts | 25 ++++++++- src/client/util/Scripting.ts | 14 ++++- src/client/views/nodes/ScriptingBox.scss | 2 + src/client/views/nodes/ScriptingBox.tsx | 96 +++++++++++++++++++++++++++++--- 4 files changed, 126 insertions(+), 11 deletions(-) (limited to 'src/client/util/Scripting.ts') diff --git a/src/client/util/ScriptManager.ts b/src/client/util/ScriptManager.ts index 5bddb44ca..c87fdf5fd 100644 --- a/src/client/util/ScriptManager.ts +++ b/src/client/util/ScriptManager.ts @@ -2,7 +2,8 @@ import { Doc, DocListCast } from "../../fields/Doc"; import { List } from "../../fields/List"; import { Docs } from "../documents/Documents"; import { Scripting, ScriptParam } from "./Scripting"; -import { StrCast } from "../../fields/Types"; +import { StrCast, Cast } from "../../fields/Types"; +import { listSpec } from "../../fields/Schema"; export class ScriptManager { @@ -38,6 +39,10 @@ export class ScriptManager { } public deleteScript(scriptDoc: Doc): boolean { + + if (scriptDoc.funcName) { + Scripting.removeGlobal(StrCast(scriptDoc.funcName)); + } const scriptList = ScriptManager.Instance.getAllScripts(); const index = ScriptManager.Instance.getAllScripts().indexOf(scriptDoc); if (index > -1) { @@ -55,9 +60,23 @@ const scriptList = ScriptManager.Instance.getAllScripts(); scriptList.forEach((scriptDoc: Doc) => { - const p = scriptDoc.compileParams?.reduce((o: ScriptParam, p: string) => { o[p] = "any"; return o; }, {} as ScriptParam); + const params = Cast(scriptDoc.compileParams, listSpec("string"), []); + const p = params.reduce((o: ScriptParam, p: string) => { o[p] = "any"; return o; }, {} as ScriptParam); const f = new Function(...Array.from(Object.keys(p)), StrCast(scriptDoc.rawScript)); - Scripting.addGlobal(f, StrCast(scriptDoc.description), StrCast(p), StrCast(scriptDoc.name)); + let parameters = "("; + params.forEach((element: string, i: number) => { + if (i === params.length - 1) { + parameters = parameters + element + ")"; + } else { + parameters = parameters + element + ", "; + } + }); + + if (parameters === "(") { + Scripting.addGlobal(f, StrCast(scriptDoc.description), StrCast(scriptDoc.funcName)); + } else { + Scripting.addGlobal(f, StrCast(scriptDoc.description), parameters, StrCast(scriptDoc.funcName)); + } }); \ No newline at end of file diff --git a/src/client/util/Scripting.ts b/src/client/util/Scripting.ts index 5619b22b0..16012eb5a 100644 --- a/src/client/util/Scripting.ts +++ b/src/client/util/Scripting.ts @@ -65,7 +65,11 @@ export namespace Scripting { obj = [nameOrGlobal]; obj.push(global); if (params) { - obj.push(params); + if (params.indexOf("(") > 0) { + obj.push(params); + } else { + n = params; + } } if (name) { n = name; @@ -93,6 +97,14 @@ export namespace Scripting { scriptingGlobals = globals; } + export function removeGlobal(name: string) { + if (_scriptingGlobals.hasKey(name)) { + delete _scriptingGlobals.container[name]; + return true; + } + return false; + } + export function resetScriptingGlobals() { scriptingGlobals = _scriptingGlobals; } diff --git a/src/client/views/nodes/ScriptingBox.scss b/src/client/views/nodes/ScriptingBox.scss index f28a2fcef..1e4dc4868 100644 --- a/src/client/views/nodes/ScriptingBox.scss +++ b/src/client/views/nodes/ScriptingBox.scss @@ -35,6 +35,8 @@ flex-direction: row; justify-content: center; + + .scriptingBox-textArea { flex: 70; height: 100%; diff --git a/src/client/views/nodes/ScriptingBox.tsx b/src/client/views/nodes/ScriptingBox.tsx index 6f94ae8f9..2e7b544d2 100644 --- a/src/client/views/nodes/ScriptingBox.tsx +++ b/src/client/views/nodes/ScriptingBox.tsx @@ -39,6 +39,7 @@ export class ScriptingBox extends ViewBoxAnnotatableComponent p.split(":")[0].trim()); } @computed({ keepAlive: true }) get paramsTypes() { return this.compileParams.map(p => p.split(":")[1].trim()); } @@ -213,20 +217,46 @@ export class ScriptingBox extends ViewBoxAnnotatableComponent { + this._errorMessage = ""; this._applied = false; + this._function = false; } @action onSave = () => { if (this.onCompile()) { - this.dataDoc.funcName = "testingTitle"; - this.dataDoc.descripition = "description test"; - ScriptManager.Instance.addScript(this.dataDoc); + this._function = true; } else { this._errorMessage = "Can not save script, does not compile"; } } + @action + onCreate = () => { + + if (this._functionName.length === 0) { + this._errorMessage = "Must enter a function name"; + return false; + } + + if (this._functionName.indexOf(" ") > 0) { + this._errorMessage = "Name can not include spaces"; + return false; + } + + this.dataDoc.funcName = this._functionName; + this.dataDoc.descripition = this._functionDescription; + + ScriptManager.Instance.deleteScript(this.dataDoc); + + this.dataDoc.funcName = "testingTitle"; + this.dataDoc.descripition = "description test"; + + ScriptManager.Instance.addScript(this.dataDoc); + + console.log("created"); + } + // overlays document numbers (ex. d32) over all documents when clicked on onFocus = () => { this._overlayDisposer?.(); @@ -270,6 +300,40 @@ export class ScriptingBox extends ViewBoxAnnotatableComponent this._functionDescription = e.target.value} + placeholder="enter description here" + value={this._functionDescription} + style={{ height: "40%", width: "100%" }} + />; + + const nameInput = +