2021-03-05 05:09:57 +08:00
|
|
|
import HyperList from "hyperlist";
|
2021-03-22 23:23:42 +08:00
|
|
|
import {
|
|
|
|
getAttributeOrThrow,
|
|
|
|
parseBoolean,
|
|
|
|
parseInteger,
|
|
|
|
} from "../lib/attribute";
|
2021-03-11 22:28:18 +08:00
|
|
|
import { getLineHeight } from "../lib/utils";
|
2021-03-05 05:09:57 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A hook used to render text lines as a virtual list,
|
|
|
|
* so that only the visible lines are actually in the DOM.
|
|
|
|
*
|
|
|
|
* Configuration:
|
|
|
|
*
|
|
|
|
* * `data-max-height` - the maximum height of the element, exceeding this height enables scrolling
|
|
|
|
*
|
2021-03-22 23:23:42 +08:00
|
|
|
* * `data-follow` - whether to automatically scroll to the bottom as new lines appear
|
|
|
|
*
|
2021-03-05 05:09:57 +08:00
|
|
|
* The element should have two children:
|
|
|
|
*
|
|
|
|
* * one annotated with `data-template` attribute, it should be hidden
|
|
|
|
* and contain all the line elements as its children
|
|
|
|
*
|
|
|
|
* * one annotated with `data-content` where the visible elements are rendered,
|
|
|
|
* it should contain any styling relevant for the container
|
|
|
|
*/
|
|
|
|
const VirtualizedLines = {
|
|
|
|
mounted() {
|
|
|
|
this.props = getProps(this);
|
2021-03-11 22:28:18 +08:00
|
|
|
this.state = {
|
|
|
|
lineHeight: null,
|
|
|
|
templateElement: null,
|
|
|
|
contentElement: null,
|
|
|
|
virtualizedList: null,
|
|
|
|
};
|
2021-03-05 05:09:57 +08:00
|
|
|
|
2021-03-11 22:28:18 +08:00
|
|
|
this.state.lineHeight = getLineHeight(this.el);
|
2021-03-05 05:09:57 +08:00
|
|
|
|
2021-03-11 22:28:18 +08:00
|
|
|
this.state.templateElement = this.el.querySelector("[data-template]");
|
2021-03-05 05:09:57 +08:00
|
|
|
|
2021-03-11 22:28:18 +08:00
|
|
|
if (!this.state.templateElement) {
|
|
|
|
throw new Error(
|
|
|
|
"VirtualizedLines must have a child with data-template attribute"
|
|
|
|
);
|
2021-03-05 05:09:57 +08:00
|
|
|
}
|
|
|
|
|
2021-03-11 22:28:18 +08:00
|
|
|
this.state.contentElement = this.el.querySelector("[data-content]");
|
2021-03-05 05:09:57 +08:00
|
|
|
|
2021-03-11 22:28:18 +08:00
|
|
|
if (!this.state.templateElement) {
|
|
|
|
throw new Error("VirtualizedLines must have a child with data-content");
|
2021-03-05 05:09:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const config = hyperListConfig(
|
2021-03-11 22:28:18 +08:00
|
|
|
this.state.templateElement,
|
2021-03-05 05:09:57 +08:00
|
|
|
this.props.maxHeight,
|
2021-03-11 22:28:18 +08:00
|
|
|
this.state.lineHeight
|
2021-03-05 05:09:57 +08:00
|
|
|
);
|
2021-03-11 22:28:18 +08:00
|
|
|
this.virtualizedList = new HyperList(this.state.contentElement, config);
|
2021-03-05 05:09:57 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
updated() {
|
|
|
|
this.props = getProps(this);
|
|
|
|
|
|
|
|
const config = hyperListConfig(
|
2021-03-11 22:28:18 +08:00
|
|
|
this.state.templateElement,
|
2021-03-05 05:09:57 +08:00
|
|
|
this.props.maxHeight,
|
2021-03-11 22:28:18 +08:00
|
|
|
this.state.lineHeight
|
2021-03-05 05:09:57 +08:00
|
|
|
);
|
2021-03-22 23:23:42 +08:00
|
|
|
|
|
|
|
const scrollTop = Math.round(this.state.contentElement.scrollTop);
|
|
|
|
const maxScrollTop = Math.round(
|
|
|
|
this.state.contentElement.scrollHeight -
|
|
|
|
this.state.contentElement.clientHeight
|
|
|
|
);
|
|
|
|
const isAtTheEnd = scrollTop === maxScrollTop;
|
|
|
|
|
2021-03-11 22:28:18 +08:00
|
|
|
this.virtualizedList.refresh(this.state.contentElement, config);
|
2021-03-22 23:23:42 +08:00
|
|
|
|
|
|
|
if (this.props.follow && isAtTheEnd) {
|
|
|
|
this.state.contentElement.scrollTop = this.state.contentElement.scrollHeight;
|
|
|
|
}
|
2021-03-05 05:09:57 +08:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
function hyperListConfig(templateElement, maxHeight, lineHeight) {
|
|
|
|
const numberOfLines = templateElement.childElementCount;
|
|
|
|
|
|
|
|
return {
|
|
|
|
height: Math.min(maxHeight, lineHeight * numberOfLines),
|
|
|
|
total: numberOfLines,
|
|
|
|
itemHeight: lineHeight,
|
|
|
|
generate: (index) => {
|
|
|
|
// Clone n-th child of the template container.
|
|
|
|
return templateElement.children.item(index).cloneNode(true);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function getProps(hook) {
|
|
|
|
return {
|
|
|
|
maxHeight: getAttributeOrThrow(hook.el, "data-max-height", parseInteger),
|
2021-03-22 23:23:42 +08:00
|
|
|
follow: getAttributeOrThrow(hook.el, "data-follow", parseBoolean),
|
2021-03-05 05:09:57 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export default VirtualizedLines;
|