2023-11-04 12:52:49 -04:00
|
|
|
import {LitElement, html, keyed} from './lit-all.min.js';
|
2023-10-29 20:22:30 -04:00
|
|
|
import * as tfrpc from '/static/tfrpc.js';
|
|
|
|
|
|
|
|
class TfCollectionsAppElement extends LitElement {
|
|
|
|
static get properties() {
|
|
|
|
return {
|
|
|
|
ids: {type: Array},
|
2023-11-01 19:39:34 -04:00
|
|
|
owner_ids: {type: Array},
|
2023-10-29 20:22:30 -04:00
|
|
|
whoami: {type: String},
|
2023-11-03 22:00:35 -04:00
|
|
|
|
|
|
|
wikis: {type: Object},
|
|
|
|
wiki_docs: {type: Object},
|
|
|
|
|
2023-11-01 18:21:42 -04:00
|
|
|
wiki: {type: Object},
|
2023-10-29 20:22:30 -04:00
|
|
|
wiki_doc: {type: Object},
|
2023-11-01 18:21:42 -04:00
|
|
|
hash: {type: String},
|
2023-10-29 20:22:30 -04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
this.ids = [];
|
2023-11-01 19:39:34 -04:00
|
|
|
this.owner_ids = [];
|
2023-10-29 20:22:30 -04:00
|
|
|
this.load();
|
2023-11-01 18:21:42 -04:00
|
|
|
let self = this;
|
|
|
|
tfrpc.register(function hash_changed(hash) {
|
2023-11-03 22:00:35 -04:00
|
|
|
self.notify_hash_changed(hash);
|
2023-11-01 18:21:42 -04:00
|
|
|
});
|
2023-11-03 22:00:35 -04:00
|
|
|
tfrpc.rpc.get_hash().then(hash => self.notify_hash_changed(hash));
|
2023-10-29 20:22:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
async load() {
|
|
|
|
this.ids = await tfrpc.rpc.getIdentities();
|
2023-11-01 19:39:34 -04:00
|
|
|
this.owner_ids = await tfrpc.rpc.getOwnerIdentities();
|
2023-10-29 20:22:30 -04:00
|
|
|
this.whoami = await tfrpc.rpc.localStorageGet('collections_whoami');
|
2023-11-03 22:00:35 -04:00
|
|
|
|
|
|
|
await this.read_wikis();
|
|
|
|
await this.read_Wiki_docs();
|
|
|
|
}
|
|
|
|
|
|
|
|
async read_wikis() {
|
|
|
|
let max_rowid;
|
|
|
|
let wikis;
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
[max_rowid, wikis] = await tfrpc.rpc.collection(this.owner_ids, 'wiki', undefined, max_rowid, wikis);
|
|
|
|
this.wikis = wikis;
|
|
|
|
this.update_wiki();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async read_wiki_docs() {
|
|
|
|
if (!this.wiki?.id) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let max_rowid;
|
|
|
|
let wiki_docs;
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
[max_rowid, wiki_docs] = await tfrpc.rpc.collection(this.owner_ids, 'wiki-doc', this.wiki?.id, max_rowid, wiki_docs);
|
|
|
|
this.wiki_docs = wiki_docs;
|
|
|
|
this.update_wiki_doc();
|
|
|
|
}
|
2023-10-29 20:22:30 -04:00
|
|
|
}
|
|
|
|
|
2023-11-03 22:00:35 -04:00
|
|
|
hash_wiki() {
|
|
|
|
let hash = this.hash ?? '';
|
|
|
|
hash = hash.charAt(0) == '#' ? hash.substring(1) : hash;
|
|
|
|
let parts = hash.split('/');
|
|
|
|
return parts[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
hash_wiki_doc() {
|
|
|
|
let hash = this.hash ?? '';
|
|
|
|
hash = hash.charAt(0) == '#' ? hash.substring(1) : hash;
|
|
|
|
let slash = hash.indexOf('/');
|
|
|
|
return slash != -1 ? hash.substring(slash + 1) : undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
update_wiki() {
|
|
|
|
let want_wiki = this.hash_wiki();
|
|
|
|
for (let wiki of Object.values(this.wikis ?? {})) {
|
|
|
|
if (wiki.name === want_wiki) {
|
|
|
|
this.wiki = wiki;
|
|
|
|
this.read_wiki_docs();
|
|
|
|
break;
|
|
|
|
}
|
2023-11-01 18:21:42 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-03 22:00:35 -04:00
|
|
|
update_wiki_doc() {
|
|
|
|
let want_wiki_doc = this.hash_wiki_doc();
|
|
|
|
for (let wiki_doc of Object.values(this.wiki_docs ?? {})) {
|
|
|
|
if (wiki_doc.name === want_wiki_doc) {
|
|
|
|
this.wiki_doc = wiki_doc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
notify_hash_changed(hash) {
|
2023-11-01 18:21:42 -04:00
|
|
|
this.hash = hash;
|
2023-11-03 22:00:35 -04:00
|
|
|
this.update_wiki();
|
|
|
|
this.update_wiki_doc();
|
2023-11-01 18:21:42 -04:00
|
|
|
}
|
|
|
|
|
2023-10-29 20:22:30 -04:00
|
|
|
async on_whoami_changed(event) {
|
|
|
|
let new_id = event.srcElement.selected;
|
|
|
|
await tfrpc.rpc.localStorageSet('collections_whoami', new_id);
|
|
|
|
this.whoami = new_id;
|
|
|
|
}
|
|
|
|
|
2023-11-01 18:21:42 -04:00
|
|
|
update_hash() {
|
|
|
|
tfrpc.rpc.set_hash(this.wiki_doc ? `${this.wiki.name}/${this.wiki_doc.name}` : `${this.wiki.name}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
async on_wiki_changed(event) {
|
|
|
|
this.wiki = event.detail.value;
|
2023-11-03 22:00:35 -04:00
|
|
|
this.wiki_doc = undefined;
|
2023-11-04 12:52:49 -04:00
|
|
|
this.wiki_docs = undefined;
|
2023-11-03 22:00:35 -04:00
|
|
|
this.update_hash();
|
|
|
|
this.read_wiki_docs();
|
2023-11-01 18:21:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
async on_wiki_doc_changed(event) {
|
2023-11-03 22:00:35 -04:00
|
|
|
this.wiki_doc = event.detail.value;
|
|
|
|
this.update_hash();
|
2023-11-01 18:21:42 -04:00
|
|
|
}
|
|
|
|
|
2023-10-29 20:22:30 -04:00
|
|
|
async on_wiki_publish(event) {
|
|
|
|
let blob_id = event.detail.id;
|
|
|
|
let message = {
|
|
|
|
type: 'wiki-doc',
|
|
|
|
key: this.wiki_doc.id,
|
|
|
|
parent: this.wiki_doc.parent,
|
|
|
|
blob: blob_id,
|
|
|
|
};
|
2023-11-01 20:45:20 -04:00
|
|
|
if (event.detail.draft) {
|
|
|
|
message.recps = this.wiki_doc.editors;
|
|
|
|
print(message);
|
|
|
|
message = await tfrpc.rpc.encrypt(this.whoami, this.wiki_doc.editors, JSON.stringify(message));
|
|
|
|
}
|
2023-10-29 20:22:30 -04:00
|
|
|
print(message);
|
|
|
|
await tfrpc.rpc.appendMessage(this.whoami, message);
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
let self = this;
|
|
|
|
return html`
|
2023-11-01 19:39:34 -04:00
|
|
|
<div>
|
|
|
|
<tf-id-picker .ids=${this.ids} selected=${this.whoami} @change=${this.on_whoami_changed}></tf-id-picker>
|
|
|
|
</div>
|
|
|
|
<div>
|
|
|
|
<tf-collection
|
2023-11-03 22:00:35 -04:00
|
|
|
.collection=${this.wikis}
|
|
|
|
selected_id=${this.wiki?.id}
|
|
|
|
@change=${this.on_wiki_changed}></tf-collection>
|
2023-11-04 12:52:49 -04:00
|
|
|
${keyed(this.wiki_doc?.id, html`<tf-collection
|
2023-11-03 22:00:35 -04:00
|
|
|
.collection=${this.wiki_docs}
|
2023-11-04 12:32:21 -04:00
|
|
|
selected_id=${(this.wiki_doc && this.wiki_doc?.parent == this.wiki?.id) ? this.wiki_doc?.id : ''}
|
2023-11-04 12:52:49 -04:00
|
|
|
@change=${this.on_wiki_doc_changed}></tf-collection>`)}
|
2023-11-01 19:39:34 -04:00
|
|
|
</div>
|
|
|
|
${this.wiki_doc && this.wiki_doc.parent === this.wiki?.id ? html`
|
|
|
|
<tf-wiki-doc
|
|
|
|
whoami=${this.whoami}
|
|
|
|
.value=${this.wiki_doc}
|
|
|
|
@publish=${this.on_wiki_publish}></tf-wiki-doc>
|
|
|
|
` : undefined}
|
2023-10-29 20:22:30 -04:00
|
|
|
`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
customElements.define('tf-collections-app', TfCollectionsAppElement);
|