diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/client/util/ScriptManager.ts | 28 | ||||
-rw-r--r-- | src/client/util/Scripting.ts | 15 | ||||
-rw-r--r-- | src/client/views/nodes/ScriptingBox.tsx | 36 |
3 files changed, 30 insertions, 49 deletions
diff --git a/src/client/util/ScriptManager.ts b/src/client/util/ScriptManager.ts index 6c90f43eb..2b82786b3 100644 --- a/src/client/util/ScriptManager.ts +++ b/src/client/util/ScriptManager.ts @@ -33,6 +33,7 @@ export class ScriptManager { public addScript(scriptDoc: Doc): boolean { console.log("in add script method"); + const scriptList = ScriptManager.Instance.getAllScripts(); scriptList.push(scriptDoc); if (ScriptManager.Instance.ScriptManagerDoc) { @@ -46,8 +47,10 @@ export class ScriptManager { public deleteScript(scriptDoc: Doc): boolean { - if (scriptDoc.funcName) { - Scripting.removeGlobal(StrCast(scriptDoc.funcName)); + console.log("in delete script method"); + + if (scriptDoc.functionName) { + Scripting.removeGlobal(StrCast(scriptDoc.functionName)); } const scriptList = ScriptManager.Instance.getAllScripts(); const index = ScriptManager.Instance.getAllScripts().indexOf(scriptDoc); @@ -62,9 +65,22 @@ export class ScriptManager { } public static addScriptToGlobals(scriptDoc: Doc): void { + + Scripting.removeGlobal(StrCast(scriptDoc.functionName)); + 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)); + const paramNames = params.reduce((o: string, p: string) => { + if (params.indexOf(p) === params.length - 1) { + o = o + p.split(":")[0].trim(); + } else { + o = o + p.split(":")[0].trim() + ","; + } + return o; + }, "" as string); + + const f = new Function(paramNames, StrCast(scriptDoc.rawScript)); + + Object.defineProperty(f, 'name', { value: StrCast(scriptDoc.functionName), writable: false }); let parameters = "("; params.forEach((element: string, i: number) => { @@ -76,9 +92,9 @@ export class ScriptManager { }); if (parameters === "(") { - Scripting.addGlobal(f, StrCast(scriptDoc.description), "", StrCast(scriptDoc.funcName)); + Scripting.addGlobal(f, StrCast(scriptDoc.functionDescription)); } else { - Scripting.addGlobal(f, StrCast(scriptDoc.description), parameters, StrCast(scriptDoc.funcName)); + Scripting.addGlobal(f, StrCast(scriptDoc.functionDescription), parameters); } } } diff --git a/src/client/util/Scripting.ts b/src/client/util/Scripting.ts index b34bf0406..bb43c9af8 100644 --- a/src/client/util/Scripting.ts +++ b/src/client/util/Scripting.ts @@ -53,7 +53,7 @@ export namespace Scripting { export function addGlobal(global: { name: string }, decription?: string, params?: string): void; export function addGlobal(global: { name: string }, decription?: string, params?: string, name?: string): void; - export function addGlobal(first: any, second?: any, third?: string, fourth?: string) { + export function addGlobal(first: any, second?: any, third?: string) { let n: any; let obj: any; @@ -70,13 +70,6 @@ export namespace Scripting { if (third !== undefined) { obj.push(third); } - if (fourth !== undefined) { - console.log("WE SHOULD BE HERE"); - n = fourth; - } else { - console.log("HOW DID WE GET HERE"); - n = first.name; - } } } else if (first && typeof first.name === "string") { n = first.name; @@ -84,7 +77,9 @@ export namespace Scripting { } else { throw new Error("Must either register an object with a name, or give a name and an object"); } - if (_scriptingGlobals.hasOwnProperty(n)) { + if (n === undefined || n === "undefined") { + return false; + } else if (_scriptingGlobals.hasOwnProperty(n)) { throw new Error(`Global with name ${n} is already registered, choose another name`); } _scriptingGlobals[n] = obj; @@ -99,7 +94,7 @@ export namespace Scripting { } export function removeGlobal(name: string) { - if (_scriptingGlobals.hasKey(name)) { + if (getGlobals().includes(name)) { delete _scriptingGlobals.container[name]; return true; } diff --git a/src/client/views/nodes/ScriptingBox.tsx b/src/client/views/nodes/ScriptingBox.tsx index c2581240b..61f58cb0f 100644 --- a/src/client/views/nodes/ScriptingBox.tsx +++ b/src/client/views/nodes/ScriptingBox.tsx @@ -187,7 +187,7 @@ export class ScriptingBox extends ViewBoxAnnotatableComponent<FieldViewProps, Sc editable: true, transformer: DocumentIconContainer.getTransformer(), params, - typecheck: true + typecheck: false }); this.dataDoc.documentText = this.rawScript; this.dataDoc.data = result.compiled ? new ScriptField(result) : undefined; @@ -253,38 +253,8 @@ export class ScriptingBox extends ViewBoxAnnotatableComponent<FieldViewProps, Sc this.dataDoc.funcName = this.functionName; this.dataDoc.descripition = this.functionDescription; - // ScriptingBox.DeleteScript?.(this.dataDoc); - // ScriptingBox.AddScript?.(this.dataDoc); - - //const p = this.compileParams.reduce((o: ScriptParam, p: string) => { o[p] = "any"; return o; }, {} as ScriptParam); - //const f = new Function(...Array.from(Object.keys(p)), this.rawScript); - - const paramNames = this.compileParams.reduce((o: string, p: string) => { - if (this.compileParams.indexOf(p) === this.compileParams.length - 1) { - o = o + p.split(":")[0].trim(); - } else { - o = o + p.split(":")[0].trim() + ","; - } - return o; - }, "" as string); - const f = new Function(paramNames, this.rawScript); - - let parameters = "("; - this.compileParams.forEach((element: string, i: number) => { - if (i === this.compileParams.length - 1) { - parameters = parameters + element + ")"; - } else { - parameters = parameters + element + ", "; - } - }); - - console.log(this.functionName); - - if (parameters === "(") { - Scripting.addGlobal(f, this.dataDoc.description, "", this.functionName); - } else { - Scripting.addGlobal(f, this.dataDoc.description, parameters, this.functionName); - } + ScriptingBox.DeleteScript?.(this.dataDoc); + ScriptingBox.AddScript?.(this.dataDoc); console.log("created"); } |