2023-11-01 20:29:07 -04:00
|
|
|
import {LitElement, html, unsafeHTML} from './lit-all.min.js';
|
2023-10-29 20:22:30 -04:00
|
|
|
import * as tfrpc from '/static/tfrpc.js';
|
|
|
|
|
|
|
|
class TfWikiDocElement extends LitElement {
|
|
|
|
static get properties() {
|
|
|
|
return {
|
|
|
|
whoami: {type: String},
|
|
|
|
value: {type: Object},
|
|
|
|
blob: {type: String},
|
|
|
|
blob_original: {type: String},
|
2023-11-01 18:21:42 -04:00
|
|
|
blob_for_value: {type: String},
|
2023-11-01 20:29:07 -04:00
|
|
|
is_editing: {type: Boolean},
|
2023-10-29 20:22:30 -04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
}
|
|
|
|
|
2023-11-01 20:29:07 -04:00
|
|
|
markdown(md) {
|
|
|
|
var reader = new commonmark.Parser({safe: true});
|
|
|
|
var writer = new commonmark.HtmlRenderer();
|
|
|
|
var parsed = reader.parse(md || '');
|
|
|
|
return writer.render(parsed);
|
|
|
|
}
|
|
|
|
|
2023-10-29 20:22:30 -04:00
|
|
|
async load_blob() {
|
|
|
|
this.blob = await tfrpc.rpc.get_blob(this.value?.blob);
|
|
|
|
this.blob_original = this.blob;
|
|
|
|
}
|
|
|
|
|
|
|
|
on_edit(event) {
|
|
|
|
this.blob = event.srcElement.value;
|
|
|
|
}
|
|
|
|
|
2023-11-01 20:29:07 -04:00
|
|
|
on_discard(event) {
|
|
|
|
this.blob = this.blob_original;
|
|
|
|
this.is_editing = false;
|
|
|
|
}
|
|
|
|
|
2023-11-01 20:45:20 -04:00
|
|
|
async on_save_draft() {
|
|
|
|
let id = await tfrpc.rpc.store_blob(this.blob);
|
|
|
|
this.dispatchEvent(new CustomEvent('publish', {
|
|
|
|
bubbles: true,
|
|
|
|
detail: {
|
|
|
|
id: id,
|
|
|
|
draft: true,
|
|
|
|
},
|
|
|
|
}));
|
|
|
|
this.is_editing = false;
|
|
|
|
}
|
|
|
|
|
2023-11-01 20:29:07 -04:00
|
|
|
async on_publish() {
|
2023-10-29 20:22:30 -04:00
|
|
|
let id = await tfrpc.rpc.store_blob(this.blob);
|
|
|
|
this.dispatchEvent(new CustomEvent('publish', {
|
|
|
|
bubbles: true,
|
|
|
|
detail: {
|
|
|
|
id: id,
|
|
|
|
},
|
|
|
|
}));
|
2023-11-01 20:29:07 -04:00
|
|
|
this.is_editing = false;
|
2023-10-29 20:22:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2023-11-01 18:21:42 -04:00
|
|
|
let value = JSON.stringify(this.value);
|
|
|
|
if (this.blob_for_value != value) {
|
|
|
|
this.blob_for_value = value;
|
|
|
|
this.blob = undefined;
|
|
|
|
this.blob_original = undefined;
|
2023-10-29 20:22:30 -04:00
|
|
|
this.load_blob();
|
|
|
|
}
|
2023-11-01 20:29:07 -04:00
|
|
|
let self = this;
|
2023-10-29 20:22:30 -04:00
|
|
|
return html`
|
2023-11-01 20:29:07 -04:00
|
|
|
<div style="display: inline-flex; flex-direction: row">
|
|
|
|
<button ?disabled=${!this.whoami || this.is_editing} @click=${() => self.is_editing = true}>Edit</button>
|
2023-11-01 20:45:20 -04:00
|
|
|
<button ?disabled=${this.blob == this.blob_original} @click=${this.on_save_draft}>Save Draft</button>
|
2023-11-01 21:19:14 -04:00
|
|
|
<button ?disabled=${this.blob == this.blob_original && !this.value?.draft} @click=${this.on_publish}>Publish</button>
|
2023-11-01 20:29:07 -04:00
|
|
|
<button ?disabled=${!this.is_editing} @click=${this.on_discard}>Discard</button>
|
|
|
|
</div>
|
2023-11-01 19:39:34 -04:00
|
|
|
<div style="display: flex; flex-direction: row">
|
|
|
|
<textarea
|
2023-11-01 20:29:07 -04:00
|
|
|
?hidden=${!this.is_editing}
|
2023-11-01 19:39:34 -04:00
|
|
|
style="flex: 1 1; min-height: 10em"
|
|
|
|
@input=${this.on_edit} .value=${this.blob ?? ''}></textarea>
|
2023-11-01 20:29:07 -04:00
|
|
|
<div style="flex: 1 1">${unsafeHTML(this.markdown(this.blob))}</div>
|
2023-11-01 19:39:34 -04:00
|
|
|
</div>
|
2023-10-29 20:22:30 -04:00
|
|
|
`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
customElements.define('tf-wiki-doc', TfWikiDocElement);
|