tildefriends/apps/collections/tf-wiki-doc.js

54 lines
1.2 KiB
JavaScript

import {LitElement, html} from './lit-all.min.js';
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},
blob_for_id: {type: String},
};
}
constructor() {
super();
}
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;
}
async publish() {
let id = await tfrpc.rpc.store_blob(this.blob);
this.dispatchEvent(new CustomEvent('publish', {
bubbles: true,
detail: {
id: id,
},
}));
}
render() {
if (this.blob_for_id != this.value?.blob) {
this.blob_for_id = this.value?.blob;
this.load_blob();
}
return html`
<div>WIKI DOC ${this.value.name}</div>
<pre>${JSON.stringify(this.value, null, 2)}</pre>
<div>
<textarea @input=${this.on_edit}>${this.blob}</textarea>
<div>${this.blob}</div>
<button ?disabled=${this.blob == this.blob_original} @click=${this.publish}>Publish</button>
`;
}
}
customElements.define('tf-wiki-doc', TfWikiDocElement);