Skip to content

Commit

Permalink
clean up stray elements on console clear better, match ensureStarting…
Browse files Browse the repository at this point in the history
…OnNewLine in virtualscroller.js (rstudio#8510)
  • Loading branch information
adamconroy authored Dec 2, 2020
1 parent 863767a commit 3ce9c7e
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 16 deletions.
9 changes: 2 additions & 7 deletions src/gwt/src/org/rstudio/core/client/ConsoleOutputWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,8 @@ public void ensureStartingOnNewLine()
{
if (virtualConsole_ != null)
{
Node child = virtualConsole_.getParent().getLastChild();
if (child != null &&
child.getNodeType() == Node.ELEMENT_NODE &&
!Element.as(child).getInnerText().endsWith("\n"))
{
virtualConsole_.submit("\n");
}
virtualConsole_.ensureStartingOnNewLine();

// clear the virtual console so we start with a fresh slate
virtualConsole_ = null;
}
Expand Down
23 changes: 15 additions & 8 deletions src/gwt/src/org/rstudio/core/client/VirtualConsole.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.TreeMap;
import java.util.TreeSet;

import com.google.gwt.dom.client.Node;
import com.google.inject.Provider;
import com.google.inject.assistedinject.Assisted;
import org.rstudio.core.client.regex.Match;
Expand Down Expand Up @@ -363,7 +364,7 @@ else if (start <= l && end <= r && end > l)
moves.put(l, overlap.start);

if (parent_ != null && !range.text().isEmpty())
parent_.insertBefore(range.element, overlap.element);
overlap.element.getParentElement().insertBefore(range.element, overlap.element);

}
}
Expand Down Expand Up @@ -647,13 +648,6 @@ public void submit(String data, String clazz, boolean forceNewRange, boolean ari
match = match.nextMatch();
}

Entry<Integer, ClassRange> last = class_.lastEntry();
if (last != null)
{
ClassRange range = last.getValue();
if (isVirtualized()) VirtualScrollerManager.prune(parent_.getParentElement(), range.element);
}

// If there was any plain text after the last control character, add it
if (tail < data.length())
text(data.substring(tail), currentClazz, forceNewRange);
Expand All @@ -673,6 +667,19 @@ public String getNewText()
return newText_ == null ? "" : newText_.toString();
}

public void ensureStartingOnNewLine()
{
if (isVirtualized())
VirtualScrollerManager.ensureStartingOnNewLine(parent_.getParentElement());
else
{
Node child = getParent().getLastChild();
if (child != null &&
child.getNodeType() == Node.ELEMENT_NODE &&
!Element.as(child).getInnerText().endsWith("\n"))
submit("\n");
}
}
private class ClassRange
{
public ClassRange(int pos, String className, String text)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.util.Date;
import java.util.HashMap;

import com.google.gwt.dom.client.Element;
import org.rstudio.core.client.Debug;
import org.rstudio.core.client.ExternalJavaScriptLoader;
Expand Down Expand Up @@ -101,6 +102,18 @@ public static void clear(Element parent)
scrollers_.get(parent.getAttribute(scrollerAttribute_)).clear();
}

public static void ensureStartingOnNewLine(Element parent)
{
if (!initialized_ || parent == null ) return;

parent = getVirtualScrollerAncestor(parent);

if (scrollers_.get(parent.getAttribute(scrollerAttribute_)) == null)
return;

scrollerForElement(parent).ensureStartingOnNewLine();
}

public static Element getCurBucket(Element parent)
{
if (scrollerForElement(parent) == null) return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@ public VirtualScrollerNative() {}
public native Element getCurBucket();
public native void clear();
public native void prune(Element ele);
public native void ensureStartingOnNewLine();
}

Original file line number Diff line number Diff line change
Expand Up @@ -269,11 +269,20 @@ var VirtualScroller;
},

clear: function() {
var i = 0;

// remove all buckets from the DOM
for (var i = 0; i < this.buckets.length; i++) {
for (i = 0; i < this.buckets.length; i++) {
this.buckets[i].remove();
}

// remove possible vestigial contents of the parent element that
// may have snuck in before the VirtualScroller was initialized
var eleChildren = this.consoleEle.children;
while (this.consoleEle.children.length > 0) {
this.consoleEle.removeChild(this.consoleEle.children[0]);
}

this._setJumpToLatestVisible(false);
this.visibleBuckets = [];
this.buckets = [];
Expand Down Expand Up @@ -302,6 +311,16 @@ var VirtualScroller;
element.innerText = element.innerText.substring(indexToSlice);
},

ensureStartingOnNewLine: function() {
if (this.getCurBucket().children < 1)
return;

// get the last element from the last bucket
var lastText = this.getCurBucket().lastElementChild.innerHTML;
if (!lastText.endsWith("\n"))
this.getCurBucket().lastElementChild.innerHTML = lastText + "\n";
},

_createAndAddNewBucket: function() {
var newBucket = document.createElement("span");

Expand Down

0 comments on commit 3ce9c7e

Please sign in to comment.