Skip to content

Commit 87ca301

Browse files
megothArne Hassel
authored andcommitted
Built more upon markdown component
1 parent 6ec02ef commit 87ca301

6 files changed

Lines changed: 105 additions & 16 deletions

File tree

markdown/index.tsx

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import * as Surplus from 'surplus'
21
import { NewPaneOptions, PaneDefinition } from '../types'
32
import solidUi from 'solid-ui'
43
import { NamedNode, sym } from 'rdflib'
4+
import { MarkdownController } from './markdown.controller'
5+
import { MarkdownView } from './markdown.view'
56

6-
const { S } = Surplus
77
const { icons, store } = solidUi
88

99
export const Pane: PaneDefinition = {
10-
icon: `${icons.iconBase}noun_15177.svg`,
10+
icon: `${icons.iconBase}noun_79217.svg`,
1111
name: 'MarkdownPane',
1212
label: (subject: NamedNode) => subject.uri.endsWith('.md') ? 'Handle markdown file' : null,
1313
mintNew: function (options: NewPaneOptions) {
@@ -24,18 +24,9 @@ export const Pane: PaneDefinition = {
2424
console.error('Error creating new instance of markdown file', err)
2525
})
2626
},
27-
render: () => {
28-
let counter = 0
29-
let counterData = S.data(counter)
30-
const incr = () => {
31-
counter++
32-
counterData(counter)
33-
console.log(counter, counterData())
34-
}
35-
return <div>
36-
test --- <span>{counterData}</span>
37-
<button onClick={incr}>Increment counter</button>
38-
</div>
27+
render: (subject: NamedNode) => {
28+
const controller = new MarkdownController(subject.uri)
29+
return MarkdownView(controller)
3930
}
4031
}
4132

markdown/markdown.controller.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import solidUi from 'solid-ui'
2+
import { S } from 'surplus'
3+
import { DataSignal } from 's-js/src/S'
4+
5+
const { store } = solidUi
6+
7+
export enum STATE {
8+
'LOADING',
9+
'RENDERING',
10+
'EDITING'
11+
}
12+
13+
export class MarkdownController {
14+
public state: DataSignal<STATE>
15+
public rawText: DataSignal<string>
16+
17+
// public renderedText: DataSignal<string>
18+
19+
constructor (private subjectUri: string) {
20+
this.state = S.value(STATE.LOADING)
21+
this.rawText = S.value('')
22+
// this.renderedText = S.value('')
23+
24+
store.fetcher.webOperation('GET', subjectUri)
25+
.then((response: any) => {
26+
this.rawText(response.responseText)
27+
// this.renderedText(marked(response.responseText))
28+
this.state(STATE.RENDERING)
29+
})
30+
}
31+
32+
toggle () {
33+
const wasEditing = this.state() === STATE.EDITING
34+
if (wasEditing) {
35+
this.state(STATE.LOADING)
36+
return MarkdownController.save(this.subjectUri, this.rawText())
37+
.then(() => this.state(STATE.RENDERING))
38+
}
39+
this.state(STATE.EDITING)
40+
}
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+
}
52+
}

markdown/markdown.view.tsx

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { MarkdownController, STATE } from './markdown.controller'
2+
import * as Surplus from 'surplus'
3+
import data from 'surplus-mixin-data'
4+
import marked from 'marked'
5+
6+
const { S } = Surplus
7+
8+
export const MarkdownView = (controller: MarkdownController) =>
9+
<section>
10+
{controller.state() === STATE.LOADING ? 'LOADING' : null}
11+
{controller.state() === STATE.RENDERING
12+
? <div>
13+
<button onClick={() => controller.toggle()}>EDIT</button>
14+
<div fn={(el: HTMLElement) => {
15+
el.innerHTML = marked(controller.rawText())
16+
}}/>
17+
</div>
18+
: null}
19+
{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>
24+
: null}
25+
</section>

package-lock.json

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
"contacts-pane": "^1.0.3",
5454
"folder-pane": "^1.0.4",
5555
"issue-pane": "^1.0.4",
56+
"marked": "^0.6.2",
5657
"meeting-pane": "^1.0.4",
5758
"mime-types": "^2.1.24",
5859
"pane-registry": "^1.0.4",
@@ -61,7 +62,9 @@
6162
"solid-namespace": "^0.2.0",
6263
"solid-ui": ">=0.11.5",
6364
"source-pane": "^1.0.3",
64-
"surplus": "^0.5.3"
65+
"surplus": "^0.5.3",
66+
"surplus-mixin-data": "^0.5.0",
67+
"surplus-mixin-on": "^0.5.0"
6568
},
6669
"dependenciesBackup": {
6770
"@solid/better-simple-slideshow": "^0.1.0",

typings/marked/index.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
declare module 'marked' {
2+
export default function parse (src: string, opts?: any, cb?: Function): string
3+
}

0 commit comments

Comments
 (0)