From a629b2344983c664be79147690d1ffc9c30e27af Mon Sep 17 00:00:00 2001
From: Jonathan Wilkes <jon.w.wilkes@gmail.com>
Date: Sun, 6 Mar 2016 15:52:31 -0500
Subject: [PATCH] partial fix for jwilkes/purr-data#19: many lines of text
 inside an Object Box can mess up the GUI viewport This should fix cases that
 come from pasting an enormous amount of text inside the box

---
 pd/nw/pd_canvas.js | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)

diff --git a/pd/nw/pd_canvas.js b/pd/nw/pd_canvas.js
index 774f0825b..25d8bba6c 100644
--- a/pd/nw/pd_canvas.js
+++ b/pd/nw/pd_canvas.js
@@ -164,6 +164,23 @@ var canvas_events = (function() {
                 return 0;
             }
         },
+        grow_svg_for_element= function(elem) {
+            // See if an element overflows the svg bbox, and
+            // enlarge the svg to accommodate it
+            var svg = document.getElementById("patchsvg"),
+                elem_bbox = elem.getBoundingClientRect(),
+                svg_viewbox = svg.getAttribute("viewBox").split(" "),
+                w = Math.max(elem_bbox.left + elem_bbox.width, svg_viewbox[2]),
+                h = Math.max(elem_bbox.top + elem_bbox.height, svg_viewbox[3]);
+            svg.setAttribute("viewBox",
+                [Math.min(elem_bbox.left, svg_viewbox[0]),
+                 Math.min(elem_bbox.top, svg_viewbox[1]),
+                 w,
+                 h
+                ].join(" "));
+            svg.setAttribute("width", w);
+            svg.setAttribute("height", h);
+        },
         events = {
             mousemove: function(evt) {
                 //pdgui.post("x: " + evt.pageX + " y: " + evt.pageY +
@@ -429,6 +446,7 @@ var canvas_events = (function() {
                 evt.preventDefault();
                 document.execCommand("insertText", false,
                     evt.clipboardData.getData("text"));
+                grow_svg_for_element(textbox());
             },
             floating_text_click: function(evt) {
                 if (target_is_scrollbar(evt)) {
-- 
GitLab