|
| 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 | +} |
0 commit comments