From f9fba837d850d33a38919962e575bc0ea4634d56 Mon Sep 17 00:00:00 2001
From: bob <bcz@cs.brown.edu>
Date: Mon, 10 Feb 2020 12:22:12 -0500
Subject: fixed removing script when it has no text

---
 src/client/views/ScriptBox.tsx | 46 +++++++++++++++++++++++-------------------
 1 file changed, 25 insertions(+), 21 deletions(-)

(limited to 'src/client/views/ScriptBox.tsx')

diff --git a/src/client/views/ScriptBox.tsx b/src/client/views/ScriptBox.tsx
index d24256886..cc5d7640e 100644
--- a/src/client/views/ScriptBox.tsx
+++ b/src/client/views/ScriptBox.tsx
@@ -93,29 +93,33 @@ export class ScriptBox extends React.Component<ScriptBoxProps> {
         const params: string[] = [];
         const setParams = (p: string[]) => params.splice(0, params.length, ...p);
         const scriptingBox = <ScriptBox initialText={originalText} setParams={setParams} onCancel={overlayDisposer} onSave={(text, onError) => {
-            const script = CompileScript(text, {
-                params: { this: Doc.name, ...contextParams },
-                typecheck: false,
-                editable: true,
-                transformer: DocumentIconContainer.getTransformer()
-            });
-            if (!script.compiled) {
-                onError(script.errors.map(error => error.messageText).join("\n"));
-                return;
-            }
+            if (!text) {
+                doc[fieldKey] = undefined;
+            } else {
+                const script = CompileScript(text, {
+                    params: { this: Doc.name, ...contextParams },
+                    typecheck: false,
+                    editable: true,
+                    transformer: DocumentIconContainer.getTransformer()
+                });
+                if (!script.compiled) {
+                    onError(script.errors.map(error => error.messageText).join("\n"));
+                    return;
+                }
 
-            const div = document.createElement("div");
-            div.style.width = "90";
-            div.style.height = "20";
-            div.style.background = "gray";
-            div.style.position = "absolute";
-            div.style.display = "inline-block";
-            div.style.transform = `translate(${clientX}px, ${clientY}px)`;
-            div.innerHTML = "button";
-            params.length && DragManager.StartButtonDrag([div], text, doc.title + "-instance", {}, params, (button: Doc) => { }, clientX, clientY);
+                const div = document.createElement("div");
+                div.style.width = "90";
+                div.style.height = "20";
+                div.style.background = "gray";
+                div.style.position = "absolute";
+                div.style.display = "inline-block";
+                div.style.transform = `translate(${clientX}px, ${clientY}px)`;
+                div.innerHTML = "button";
+                params.length && DragManager.StartButtonDrag([div], text, doc.title + "-instance", {}, params, (button: Doc) => { }, clientX, clientY);
 
-            doc[fieldKey] = new ScriptField(script);
-            overlayDisposer();
+                doc[fieldKey] = new ScriptField(script);
+                overlayDisposer();
+            }
         }} showDocumentIcons />;
         overlayDisposer = OverlayView.Instance.addWindow(scriptingBox, { x: 400, y: 200, width: 500, height: 400, title: title });
     }
-- 
cgit v1.2.3-70-g09d2

