|
| 1 | +import { MarkdownBuilder } from './api/markdown/MarkdownBuilder'; |
| 2 | +import { MarkdownString } from './api/markdown/MarkdownString'; |
| 3 | +import { MessageWrapper } from './messages/MessageManager'; |
| 4 | +import MessageComponent from './messages/MessageComponent.svelte'; |
| 5 | +import { Component } from 'obsidian'; |
| 6 | +import JsEnginePlugin from './main'; |
| 7 | +import { ReactiveComponent } from './api/reactive/ReactiveComponent'; |
| 8 | + |
| 9 | +export class ResultRenderer { |
| 10 | + readonly plugin: JsEnginePlugin; |
| 11 | + readonly container: HTMLElement; |
| 12 | + readonly sourcePath: string; |
| 13 | + readonly component: Component; |
| 14 | + |
| 15 | + constructor(plugin: JsEnginePlugin, container: HTMLElement, sourcePath: string, component: Component) { |
| 16 | + this.plugin = plugin; |
| 17 | + this.container = container; |
| 18 | + this.sourcePath = sourcePath; |
| 19 | + this.component = component; |
| 20 | + } |
| 21 | + |
| 22 | + public async render(content: unknown): Promise<void> { |
| 23 | + if (content == null) { |
| 24 | + return; |
| 25 | + } |
| 26 | + |
| 27 | + this.container.empty(); |
| 28 | + |
| 29 | + if (typeof content === 'string') { |
| 30 | + this.container.innerText = content; |
| 31 | + return; |
| 32 | + } |
| 33 | + |
| 34 | + if (content instanceof MarkdownBuilder) { |
| 35 | + content = content.toMarkdown(); |
| 36 | + } |
| 37 | + |
| 38 | + if (content instanceof MarkdownString) { |
| 39 | + console.log(content.content); |
| 40 | + await content.render(this.container, this.sourcePath, this.component); |
| 41 | + return; |
| 42 | + } |
| 43 | + |
| 44 | + if (content instanceof HTMLElement) { |
| 45 | + this.container.append(content); |
| 46 | + } |
| 47 | + |
| 48 | + if (content instanceof MessageWrapper) { |
| 49 | + new MessageComponent({ |
| 50 | + target: this.container, |
| 51 | + props: { |
| 52 | + messageWrapper: content, |
| 53 | + messageManager: this.plugin.messageManager, |
| 54 | + showDeleteButton: false, |
| 55 | + showMessageSource: false, |
| 56 | + }, |
| 57 | + }); |
| 58 | + } |
| 59 | + |
| 60 | + if (content instanceof ReactiveComponent) { |
| 61 | + content.setRenderer(this); |
| 62 | + content.initialRender(); |
| 63 | + } |
| 64 | + } |
| 65 | +} |
0 commit comments