File size: 2,155 Bytes
1be41e2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
<script lang="ts">
import { createEventDispatcher, onMount, tick } from "svelte";
export let elem_classes: string[] = [];
export let value: string;
export let visible: boolean | "hidden" = true;
export let autoscroll = false;
const dispatch = createEventDispatcher<{
change: undefined;
click: undefined;
}>();
let div: HTMLDivElement;
let scrollable_parent: HTMLElement | null = null;
function get_scrollable_parent(element: HTMLElement): HTMLElement | null {
let parent = element.parentElement;
while (parent) {
const style = window.getComputedStyle(parent);
if (
style.overflow === "auto" ||
style.overflow === "scroll" ||
style.overflowY === "auto" ||
style.overflowY === "scroll"
) {
return parent;
}
parent = parent.parentElement;
}
return null;
}
function is_at_bottom(): boolean {
if (!div) return true;
if (!scrollable_parent) {
return (
window.innerHeight + window.scrollY >=
document.documentElement.scrollHeight - 100
);
}
return (
scrollable_parent.offsetHeight + scrollable_parent.scrollTop >=
scrollable_parent.scrollHeight - 100
);
}
function scroll_to_bottom(): void {
if (!div) return;
if (scrollable_parent) {
scrollable_parent.scrollTo(0, scrollable_parent.scrollHeight);
} else {
window.scrollTo(0, document.documentElement.scrollHeight);
}
}
async function scroll_on_value_update(): Promise<void> {
if (!autoscroll || !div) return;
if (!scrollable_parent) {
scrollable_parent = get_scrollable_parent(div);
}
if (is_at_bottom()) {
await new Promise((resolve) => setTimeout(resolve, 300));
scroll_to_bottom();
}
}
onMount(() => {
if (autoscroll) {
scroll_to_bottom();
}
scroll_on_value_update();
});
$: value, dispatch("change");
$: if (value && autoscroll) {
scroll_on_value_update();
}
</script>
<!-- svelte-ignore a11y-click-events-have-key-events a11y-no-static-element-interactions -->
<div
bind:this={div}
class="prose {elem_classes.join(' ')}"
class:hide={!visible}
on:click={() => dispatch("click")}
>
{@html value}
</div>
<style>
.hide {
display: none;
}
</style>
|