Skip to content

Commit 6de1fc8

Browse files
megothArne Hassel
authored andcommitted
Shaping up the markdown parts a bit, introducing service file
1 parent 87ca301 commit 6de1fc8

4 files changed

Lines changed: 31 additions & 34 deletions

File tree

markdown/index.tsx

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import solidUi from 'solid-ui'
33
import { NamedNode, sym } from 'rdflib'
44
import { MarkdownController } from './markdown.controller'
55
import { MarkdownView } from './markdown.view'
6+
import { saveMarkdown } from './markdown.service'
67

78
const { icons, store } = solidUi
89

@@ -12,16 +13,14 @@ export const Pane: PaneDefinition = {
1213
label: (subject: NamedNode) => subject.uri.endsWith('.md') ? 'Handle markdown file' : null,
1314
mintNew: function (options: NewPaneOptions) {
1415
const newInstance = createFileName(options)
15-
return store.fetcher.webOperation('PUT', newInstance, {
16-
data: '# This is your markdown file\n\nHere be stuff!',
17-
contentType: 'text/markdown; charset=UTF-8'
18-
})
19-
.then(() => ({
16+
return saveMarkdown(newInstance.uri, '# This is your markdown file\n\nHere be stuff!')
17+
.then((): NewPaneOptions => ({
2018
newInstance,
2119
...options
2220
}))
2321
.catch((err: any) => {
2422
console.error('Error creating new instance of markdown file', err)
23+
return options
2524
})
2625
},
2726
render: (subject: NamedNode) => {

markdown/markdown.controller.ts

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
import solidUi from 'solid-ui'
21
import { S } from 'surplus'
32
import { DataSignal } from 's-js/src/S'
4-
5-
const { store } = solidUi
3+
import { loadMarkdown, saveMarkdown } from './markdown.service'
64

75
export enum STATE {
86
'LOADING',
@@ -14,17 +12,13 @@ export class MarkdownController {
1412
public state: DataSignal<STATE>
1513
public rawText: DataSignal<string>
1614

17-
// public renderedText: DataSignal<string>
18-
1915
constructor (private subjectUri: string) {
2016
this.state = S.value(STATE.LOADING)
2117
this.rawText = S.value('')
22-
// this.renderedText = S.value('')
2318

24-
store.fetcher.webOperation('GET', subjectUri)
25-
.then((response: any) => {
26-
this.rawText(response.responseText)
27-
// this.renderedText(marked(response.responseText))
19+
loadMarkdown(subjectUri)
20+
.then((responseText) => {
21+
this.rawText(responseText)
2822
this.state(STATE.RENDERING)
2923
})
3024
}
@@ -33,20 +27,9 @@ export class MarkdownController {
3327
const wasEditing = this.state() === STATE.EDITING
3428
if (wasEditing) {
3529
this.state(STATE.LOADING)
36-
return MarkdownController.save(this.subjectUri, this.rawText())
30+
return saveMarkdown(this.subjectUri, this.rawText())
3731
.then(() => this.state(STATE.RENDERING))
3832
}
3933
this.state(STATE.EDITING)
4034
}
41-
42-
update (fieldName: string): void {
43-
console.log(fieldName, this)
44-
}
45-
46-
static save (uri: string, content: string): Promise<any> {
47-
return store.fetcher.webOperation('PUT', uri, {
48-
data: content,
49-
contentType: 'text/markdown; charset=UTF-8'
50-
})
51-
}
5235
}

markdown/markdown.service.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import solidUi from 'solid-ui'
2+
3+
const { store } = solidUi
4+
5+
export function loadMarkdown (uri: string): Promise<string> {
6+
return store.fetcher.webOperation('GET', uri)
7+
.then((response: any) => response.responseText)
8+
}
9+
10+
export function saveMarkdown (uri: string, data: string): Promise<any> {
11+
return store.fetcher.webOperation('PUT', uri, {
12+
data,
13+
contentType: 'text/markdown; charset=UTF-8'
14+
})
15+
}

markdown/markdown.view.tsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@ export const MarkdownView = (controller: MarkdownController) =>
99
<section>
1010
{controller.state() === STATE.LOADING ? 'LOADING' : null}
1111
{controller.state() === STATE.RENDERING
12-
? <div>
13-
<button onClick={() => controller.toggle()}>EDIT</button>
12+
? [
13+
<button onClick={() => controller.toggle()}>EDIT</button>,
1414
<div fn={(el: HTMLElement) => {
1515
el.innerHTML = marked(controller.rawText())
1616
}}/>
17-
</div>
17+
]
1818
: null}
1919
{controller.state() === STATE.EDITING
20-
? <div>
21-
<button onClick={() => controller.toggle()}>RENDER</button>
22-
<textarea width='100%' rows={20} fn={data(controller.rawText)}>{controller.rawText}</textarea>
23-
</div>
20+
? [
21+
<button onClick={() => controller.toggle()}>RENDER</button>,
22+
<textarea fn={data(controller.rawText)}>{controller.rawText}</textarea>
23+
]
2424
: null}
2525
</section>

0 commit comments

Comments
 (0)