|
| 1 | +import * as React from 'react' |
| 2 | +import * as ReactDOM from 'react-dom' |
| 3 | +import { PaneDefinition, NewPaneOptions } from '../types' |
| 4 | +import $rdf from 'rdflib' |
| 5 | +import solidUi from 'solid-ui' |
| 6 | +import { saveMarkdown, loadMarkdown } from './service' |
| 7 | +import { View } from './view' |
| 8 | + |
| 9 | +const { icons, store } = solidUi |
| 10 | + |
| 11 | +export const Pane: PaneDefinition = { |
| 12 | + icon: `${icons.iconBase}noun_79217.svg`, |
| 13 | + name: 'MarkdownPane', |
| 14 | + label: (subject) => subject.uri.endsWith('.md') ? 'Handle markdown file' : null, |
| 15 | + mintNew: function (options) { |
| 16 | + const newInstance = createFileName(options) |
| 17 | + return saveMarkdown(store, newInstance.uri, '# This is your markdown file\n\nHere be stuff!') |
| 18 | + .then(() => ({ |
| 19 | + ...options, |
| 20 | + newInstance |
| 21 | + })) |
| 22 | + .catch((err: any) => { |
| 23 | + console.error('Error creating new instance of markdown file', err) |
| 24 | + return options |
| 25 | + }) |
| 26 | + }, |
| 27 | + render: (subject) => { |
| 28 | + const container = document.createElement('div') |
| 29 | + |
| 30 | + loadMarkdown(store, subject.uri).then((markdown) => { |
| 31 | + const view = ( |
| 32 | + <View |
| 33 | + markdown={markdown} |
| 34 | + onSave={(newMarkdown) => saveMarkdown(store, subject.uri, newMarkdown)} |
| 35 | + /> |
| 36 | + ) |
| 37 | + ReactDOM.render(view, container) |
| 38 | + }) |
| 39 | + return container |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +function createFileName (options: NewPaneOptions): $rdf.NamedNode { |
| 44 | + let uri = options.newBase |
| 45 | + if (uri.endsWith('/')) { |
| 46 | + uri = uri.slice(0, -1) + '.md' |
| 47 | + } |
| 48 | + return $rdf.sym(uri) |
| 49 | +} |
0 commit comments