Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion assets/js/hooks/virtualized_lines.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import HyperList from "hyperlist";
import { parseHookProps } from "../lib/attribute";
import {
findChildOrThrow,
findClosestOrThrow,
getLineHeight,
isScrolledToEnd,
scrollToEnd,
Expand All @@ -16,6 +17,10 @@ import {
* * `max-height` - the maximum height of the element, exceeding
* this height enables scrolling
*
* * `max-height-amplified` - the maximum height of the element
* when "Amplify Output" is selected in the UI, exceeding this
* height enables scrolling when selected
*
* * `follow` - whether to automatically scroll to the bottom as
* new lines appear
*
Expand All @@ -40,6 +45,9 @@ const VirtualizedLines = {
this.lineHeight = getLineHeight(this.el);
this.templateEl = findChildOrThrow(this.el, "[data-template]");
this.contentEl = findChildOrThrow(this.el, "[data-content]");
this.cellEl = findClosestOrThrow(this.el, "[data-el-cell]");
this.amplifyOutput = this.isAmplifyOutput();
this.amplifyObserver = this.newAmplifyObserver();

this.capLines();

Expand Down Expand Up @@ -67,9 +75,14 @@ const VirtualizedLines = {
}
},

destroyed() {
this.amplifyObserver.disconnect();
},

getProps() {
return parseHookProps(this.el, [
"max-height",
"max-height-amplified",
"follow",
"max-lines",
"ignore-trailing-empty-line",
Expand All @@ -81,7 +94,7 @@ const VirtualizedLines = {
const numberOfLines = lineEls.length;

const height = Math.min(
this.props.maxHeight,
this.amplifyOutput ? this.props.maxHeightAmplified : this.props.maxHeight,
this.lineHeight * numberOfLines,
);

Expand Down Expand Up @@ -139,6 +152,25 @@ const VirtualizedLines = {
}
}
},

newAmplifyObserver() {
const observer = new MutationObserver((mutationRecords) => {
if (
mutationRecords.length != 1 ||
mutationRecords[0].target != this.cellEl ||
mutationRecords[0].attributeName != "data-js-amplified"
) { throw new Error("unexpected mutation changing Amplify Output"); }

this.amplifyOutput = this.isAmplifyOutput();
this.updated();
});
observer.observe(this.cellEl, {attributeFilter: ["data-js-amplified"]});
return observer;
},

isAmplifyOutput() {
return (this.cellEl.getAttribute("data-js-amplified") != null);
},
};

export default VirtualizedLines;
12 changes: 12 additions & 0 deletions assets/js/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,18 @@ export function findChildOrThrow(element, selector) {
return child;
}

export function findClosestOrThrow(element, selector) {
const closest = element.closest(selector);

if (!closest) {
throw new Error(
`expected closest matching ${selector}, but none was found`,
);
}

return closest;
}

export function cancelEvent(event) {
// Cancel any default browser behavior.
event.preventDefault();
Expand Down
1 change: 1 addition & 0 deletions lib/livebook_web/live/output/terminal_text_component.ex
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ defmodule LivebookWeb.Output.TerminalTextComponent do
class="relative group/root"
phx-hook="VirtualizedLines"
data-p-max-height={hook_prop(300)}
data-p-max-height-amplified={hook_prop(600)}
data-p-follow={hook_prop(true)}
data-p-max-lines={hook_prop(Livebook.Notebook.max_terminal_lines())}
data-p-ignore-trailing-empty-line={hook_prop(true)}
Expand Down