From 6928fec6695f468f17b5031e48192ad2f7d505f1 Mon Sep 17 00:00:00 2001
From: Marijn Haverbeke <marijn@haverbeke.nl>
Date: Wed, 4 Jan 2017 10:59:11 +0100
Subject: [PATCH] [panel addon] Implement a 'stable' option

Issue #4485
---
 addon/display/panel.js |  9 +++++++++
 demo/panel.html        |  4 ++--
 doc/manual.html        | 11 +++++++----
 3 files changed, 18 insertions(+), 6 deletions(-)

diff --git a/addon/display/panel.js b/addon/display/panel.js
index ba29484d..a6ac74f0 100644
--- a/addon/display/panel.js
+++ b/addon/display/panel.js
@@ -38,6 +38,9 @@
     var height = (options && options.height) || node.offsetHeight;
     this._setSize(null, info.heightLeft -= height);
     info.panels++;
+    if (options.stable && isAtTop(this, node))
+      this.scrollTo(null, this.getScrollInfo().top + height)
+
     return new Panel(this, node, options, height);
   });
 
@@ -109,4 +112,10 @@
     cm.setSize = cm._setSize;
     cm.setSize();
   }
+
+  function isAtTop(cm, dom) {
+    for (let sibling = dom.nextSibling; sibling; sibling = sibling.nextSibling)
+      if (sibling == cm.getWrapperElement()) return true
+    return false
+  }
 });
diff --git a/demo/panel.html b/demo/panel.html
index b3b0b7ca..1ce3d87c 100644
--- a/demo/panel.html
+++ b/demo/panel.html
@@ -115,7 +115,7 @@ function makePanel(where) {
 }
 function addPanel(where) {
   var node = makePanel(where);
-  panels[node.id] = editor.addPanel(node, {position: where});
+  panels[node.id] = editor.addPanel(node, {position: where, stable: true});
 }
 
 addPanel("top");
@@ -126,7 +126,7 @@ function replacePanel(form) {
   var panel = panels["panel-" + id];
   var node = makePanel("");
 
-  panels[node.id] = editor.addPanel(node, {replace: panel, position: "after-top"});
+  panels[node.id] = editor.addPanel(node, {replace: panel, position: "after-top", stable: true});
   return false;
 }
 </script>
diff --git a/doc/manual.html b/doc/manual.html
index 3c252381..7acc872b 100644
--- a/doc/manual.html
+++ b/doc/manual.html
@@ -2910,7 +2910,7 @@ editor.setOption("extraKeys", {
       changed.<br/>
       The method accepts the following options:
       <dl>
-        <dt><code><strong>position</strong> : string</code></dt>
+        <dt><code><strong>position</strong>: string</code></dt>
         <dd>Controls the position of the newly added panel. The
         following values are recognized:
           <dl>
@@ -2924,12 +2924,15 @@ editor.setOption("extraKeys", {
             <dd>Adds the panel at the top of the bottom panels.</dd>
           </dl>
         </dd>
-        <dt><code><strong>before</strong> : Panel</code></dt>
+        <dt><code><strong>before</strong>: Panel</code></dt>
         <dd>The new panel will be added before the given panel.</dd>
-        <dt><code><strong>after</strong> : Panel</code></dt>
+        <dt><code><strong>after</strong>: Panel</code></dt>
         <dd>The new panel will be added after the given panel.</dd>
-        <dt><code><strong>replace</strong> : Panel</code></dt>
+        <dt><code><strong>replace</strong>: Panel</code></dt>
         <dd>The new panel will replace the given panel.</dd>
+        <dt><code><strong>stable</strong>: bool</code></dt>
+        <dd>Whether to scroll the editor to keep the text's vertical
+        position stable, when adding a panel above it. Defaults to false.</dd>
       </dl>
       When using the <code>after</code>, <code>before</code> or <code>replace</code> options,
       if the panel doesn't exists or has been removed,
-- 
GitLab