| 1 |
<!-- |
| 2 |
@component |
| 3 |
A basic fullsize readonly editor that loads and displays the text document |
| 4 |
at the given URL. |
| 5 |
--> |
| 6 |
<script lang="ts"> |
| 7 |
import { replaceContents } from "../codemirror/editor-transactions"; |
| 8 |
import { initDocumentPreviewEditor } from "../codemirror/setup"; |
| 9 |
import { fetch, handleText } from "../lib/fetch"; |
| 10 |
|
| 11 |
interface Props { |
| 12 |
/** The URL to load the editor contents from. */ |
| 13 |
url: string; |
| 14 |
} |
| 15 |
|
| 16 |
let { url }: Props = $props(); |
| 17 |
|
| 18 |
const { editor, renderEditor } = initDocumentPreviewEditor(""); |
| 19 |
|
| 20 |
const set_editor_content = (value: string) => { |
| 21 |
if (value !== editor.state.sliceDoc()) { |
| 22 |
editor.dispatch(replaceContents(editor.state, value)); |
| 23 |
} |
| 24 |
}; |
| 25 |
|
| 26 |
$effect(() => { |
| 27 |
fetch(url) |
| 28 |
.then(handleText) |
| 29 |
.then(set_editor_content, () => { |
| 30 |
set_editor_content(`Loading ${url} failed...`); |
| 31 |
}); |
| 32 |
}); |
| 33 |
</script> |
| 34 |
|
| 35 |
<div use:renderEditor></div> |
| 36 |
|
| 37 |
<style> |
| 38 |
div { |
| 39 |
width: 100%; |
| 40 |
height: 100%; |
| 41 |
} |
| 42 |
|
| 43 |
div :global(.cm-editor) { |
| 44 |
width: 100%; |
| 45 |
height: 100%; |
| 46 |
} |
| 47 |
</style> |