-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
ScrollViewMarkdownComponent.svelte
75 lines (66 loc) · 1.48 KB
/
ScrollViewMarkdownComponent.svelte
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
<script lang="ts">
import type TagFolderPlugin from "main";
import { MarkdownRenderer } from "obsidian";
import { onDestroy, onMount } from "svelte";
import { type ScrollViewFile } from "types";
interface Props {
file?: ScrollViewFile;
observer?: IntersectionObserver;
plugin: TagFolderPlugin;
}
let { file = { path: "" }, observer, plugin }: Props = $props();
let el = $state<HTMLElement>();
let renderedContent = $state("");
function onAppearing(this: HTMLElement, _: Event) {
if (file.content && el && renderedContent != file.content) {
MarkdownRenderer.render(
plugin.app,
file.content,
el,
file.path,
plugin,
);
renderedContent = file.content;
}
}
onMount(() => {
if (el && observer) {
observer.observe(el);
el.addEventListener("appearing", onAppearing);
}
});
onDestroy(() => {
if (el && observer) {
observer.unobserve(el);
el.removeEventListener("appearing", onAppearing);
}
});
$effect(() => {
if (
renderedContent &&
file &&
file.content &&
el &&
renderedContent != file.content
) {
el.style.minHeight = `${el.clientHeight}px`;
el.innerHTML = "";
MarkdownRenderer.render(
plugin.app,
file.content,
el,
file.path,
plugin,
);
renderedContent = file.content;
el.style.minHeight = "20px";
}
});
</script>
<div class="markdownBody" bind:this={el} style="min-height: 1em;"></div>
<style>
.markdownBody {
user-select: text;
-webkit-user-select: text;
}
</style>