Run prettier.

This commit is contained in:
2024-02-24 11:09:34 -05:00
parent 8e7e0ed490
commit d5267be38c
90 changed files with 5789 additions and 2265 deletions

View File

@ -78,4 +78,4 @@ async function main() {
await app.setDocument(utf8Decode(await getFile('index.html')));
}
main();
main();

View File

@ -11,10 +11,13 @@ function markdown(md) {
let node = event.node;
if (event.entering) {
if (node.destination?.startsWith('&')) {
node.destination = '/' + node.destination + '/view?filename=' + node.firstChild?.literal;
node.destination =
'/' + node.destination + '/view?filename=' + node.firstChild?.literal;
} else if (node.type === 'link') {
if (node.destination.indexOf(':') == -1 &&
node.destination.indexOf('/') == -1) {
if (
node.destination.indexOf(':') == -1 &&
node.destination.indexOf('/') == -1
) {
node.destination = `${node.destination}`;
}
}
@ -29,7 +32,9 @@ async function main() {
let wiki_name = request.path.substring(0, slash);
let wiki_doc_name = request.path.substring(slash + 1);
let ids = Object.keys(await ssb.following(await ssb.getOwnerIdentities(), 1));
let ids = Object.keys(
await ssb.following(await ssb.getOwnerIdentities(), 1)
);
let [max_row_id, wikis] = await utils.collection(ids, 'wiki', null, -1, {});
let wiki;
for (let w of Object.values(wikis)) {
@ -40,7 +45,13 @@ async function main() {
}
let wiki_doc;
if (wiki) {
let [max_row_id, wiki_docs] = await utils.collection(ids, 'wiki-doc', wiki.id, -1, {});
let [max_row_id, wiki_docs] = await utils.collection(
ids,
'wiki-doc',
wiki.id,
-1,
{}
);
for (let w of Object.values(wiki_docs)) {
if (w.name === wiki_doc_name && !w.tombstone) {
wiki_doc = w;
@ -70,4 +81,4 @@ async function main() {
});
}
}
main();
main();

View File

@ -1,14 +1,16 @@
<!DOCTYPE html>
<!doctype html>
<html>
<head>
<base target="_top">
<base target="_top" />
</head>
<body style="color: #fff">
<tf-collections-app></tf-collections-app>
<script>window.litDisableBundleWarning = true;</script>
<script>
window.litDisableBundleWarning = true;
</script>
<script src="tf-collection.js" type="module"></script>
<script src="tf-id-picker.js" type="module"></script>
<script src="tf-wiki-doc.js" type="module"></script>
<script src="tf-wiki-app.js" type="module"></script>
</body>
</html>
</html>

View File

@ -14,52 +14,62 @@ class TfCollectionElement extends LitElement {
on_create(event) {
let name = this.shadowRoot.getElementById('create_name').value;
this.dispatchEvent(new CustomEvent('create', {
bubbles: true,
detail: {
name: name,
},
}));
this.dispatchEvent(
new CustomEvent('create', {
bubbles: true,
detail: {
name: name,
},
})
);
this.is_creating = false;
}
on_rename(event) {
let id = this.shadowRoot.getElementById('select').value;
let name = this.shadowRoot.getElementById('rename_name').value;
this.dispatchEvent(new CustomEvent('rename', {
bubbles: true,
detail: {
id: id,
value: this.collection[id],
name: name,
},
}));
this.dispatchEvent(
new CustomEvent('rename', {
bubbles: true,
detail: {
id: id,
value: this.collection[id],
name: name,
},
})
);
this.is_renaming = false;
}
on_tombstone(event) {
let id = this.shadowRoot.getElementById('select').value;
if (confirm(`Are you sure you want to delete '${this.collection[id].name}'?`)) {
this.dispatchEvent(new CustomEvent('tombstone', {
bubbles: true,
detail: {
id: id,
value: this.collection[id],
},
}));
if (
confirm(`Are you sure you want to delete '${this.collection[id].name}'?`)
) {
this.dispatchEvent(
new CustomEvent('tombstone', {
bubbles: true,
detail: {
id: id,
value: this.collection[id],
},
})
);
}
}
on_selected(event) {
let id = event.srcElement.value;
this.selected_id = id != '' ? id : undefined;
this.dispatchEvent(new CustomEvent('change', {
bubbles: true,
detail: {
id: id,
value: this.collection[id],
},
}));
this.dispatchEvent(
new CustomEvent('change', {
bubbles: true,
detail: {
id: id,
value: this.collection[id],
},
})
);
}
render() {
@ -68,28 +78,38 @@ class TfCollectionElement extends LitElement {
<span style="display: inline-flex; flex-direction: row">
<select @change=${this.on_selected} id="select" value=${this.selected_id}>
<option value="" ?selected=${this.selected_id === ''} disabled hidden>(select)</option>
${Object.values(this.collection ?? {}).sort((x, y) => x.name.localeCompare(y.name)).map(x => html`<option value=${x.id} ?selected=${this.selected_id === x.id}>${x.name}</option>`)}
${Object.values(this.collection ?? {})
.sort((x, y) => x.name.localeCompare(y.name))
.map(
(x) =>
html`<option
value=${x.id}
?selected=${this.selected_id === x.id}
>
${x.name}
</option>`
)}
</select>
<span ?hidden=${!this.is_renaming || !this.whoami}>
<span style="display: inline-flex; flex-direction: row; margin-left: 8px; margin-right: 8px">
<label for="rename_name">🏷Rename to:</label>
<input type="text" id="rename_name"></input>
<button @click=${this.on_rename}>Rename ${this.type}</button>
<button @click=${() => self.is_renaming = false}>x</button>
<button @click=${() => (self.is_renaming = false)}>x</button>
</span>
</span>
<button @click=${() => self.is_renaming = true} ?disabled=${this.is_renaming || !this.selected_id} ?hidden=${!this.whoami}>🏷</button>
<button @click=${() => (self.is_renaming = true)} ?disabled=${this.is_renaming || !this.selected_id} ?hidden=${!this.whoami}>🏷</button>
<button @click=${self.on_tombstone} ?disabled=${!this.selected_id} ?hidden=${!this.whoami}>🪦</button>
<span ?hidden=${!this.is_creating || !this.whoami}>
<label for="create_name">New ${this.type} name:</label>
<input type="text" id="create_name"></input>
<button @click=${this.on_create}>Create ${this.type}</button>
<button @click=${() => self.is_creating = false}>x</button>
<button @click=${() => (self.is_creating = false)}>x</button>
</span>
<button @click=${() => self.is_creating = true} ?hidden=${this.is_creating || !this.whoami}>+</button>
<button @click=${() => (self.is_creating = true)} ?hidden=${this.is_creating || !this.whoami}>+</button>
</span>
`;
}
}
customElements.define('tf-collection', TfCollectionElement);
customElements.define('tf-collection', TfCollectionElement);

View File

@ -2,8 +2,8 @@ import {LitElement, html} from './lit-all.min.js';
import * as tfrpc from '/static/tfrpc.js';
/*
** Provide a list of IDs, and this lets the user pick one.
*/
** Provide a list of IDs, and this lets the user pick one.
*/
class TfIdentityPickerElement extends LitElement {
static get properties() {
return {
@ -19,18 +19,25 @@ class TfIdentityPickerElement extends LitElement {
changed(event) {
this.selected = event.srcElement.value;
this.dispatchEvent(new Event('change', {
srcElement: this,
}));
this.dispatchEvent(
new Event('change', {
srcElement: this,
})
);
}
render() {
return html`
<select @change=${this.changed} style="max-width: 100%">
${(this.ids ?? []).map(id => html`<option ?selected=${id == this.selected} value=${id}>${id}</option>`)}
${(this.ids ?? []).map(
(id) =>
html`<option ?selected=${id == this.selected} value=${id}>
${id}
</option>`
)}
</select>
`;
}
}
customElements.define('tf-id-picker', TfIdentityPickerElement);
customElements.define('tf-id-picker', TfIdentityPickerElement);

View File

@ -31,7 +31,7 @@ class TfCollectionsAppElement extends LitElement {
tfrpc.register(function hash_changed(hash) {
self.notify_hash_changed(hash);
});
tfrpc.rpc.get_hash().then(hash => self.notify_hash_changed(hash));
tfrpc.rpc.get_hash().then((hash) => self.notify_hash_changed(hash));
}
async load() {
@ -49,10 +49,16 @@ class TfCollectionsAppElement extends LitElement {
let max_rowid;
let wikis;
let start_whoami = this.whoami;
while (true)
{
while (true) {
console.log('read_wikis', this.whoami);
[max_rowid, wikis] = await tfrpc.rpc.collection(this.following, 'wiki', undefined, max_rowid, wikis, false);
[max_rowid, wikis] = await tfrpc.rpc.collection(
this.following,
'wiki',
undefined,
max_rowid,
wikis,
false
);
console.log('read ->', wikis);
if (this.whoami !== start_whoami) {
break;
@ -70,9 +76,14 @@ class TfCollectionsAppElement extends LitElement {
let start_id = this.wiki.id;
let max_rowid;
let wiki_docs;
while (true)
{
[max_rowid, wiki_docs] = await tfrpc.rpc.collection(this.wiki?.editors, 'wiki-doc', this.wiki?.id, max_rowid, wiki_docs);
while (true) {
[max_rowid, wiki_docs] = await tfrpc.rpc.collection(
this.wiki?.editors,
'wiki-doc',
this.wiki?.id,
max_rowid,
wiki_docs
);
if (this.wiki?.id !== start_id) {
break;
}
@ -92,7 +103,7 @@ class TfCollectionsAppElement extends LitElement {
let hash = this.hash ?? '';
hash = hash.charAt(0) == '#' ? hash.substring(1) : hash;
let slash = hash.indexOf('/');
return slash != -1 ? hash.substring(slash + 1) : undefined;
return slash != -1 ? hash.substring(slash + 1) : undefined;
}
update_wiki() {
@ -128,7 +139,11 @@ class TfCollectionsAppElement extends LitElement {
}
update_hash() {
tfrpc.rpc.set_hash(this.wiki_doc ? `${this.wiki.name}/${this.wiki_doc.name}` : `${this.wiki.name}`);
tfrpc.rpc.set_hash(
this.wiki_doc
? `${this.wiki.name}/${this.wiki_doc.name}`
: `${this.wiki.name}`
);
}
async on_wiki_changed(event) {
@ -174,7 +189,7 @@ class TfCollectionsAppElement extends LitElement {
if (confirm(`Are you sure you want to remove ${id} as an editor?`)) {
let editors = [...this.wiki.editors];
if (editors.indexOf(id) != -1) {
editors = editors.filter(x => x !== id);
editors = editors.filter((x) => x !== id);
}
await tfrpc.rpc.appendMessage(this.whoami, {
type: 'wiki',
@ -252,34 +267,45 @@ class TfCollectionsAppElement extends LitElement {
<tf-id-picker .ids=${this.ids} selected=${this.whoami} @change=${this.on_whoami_changed} ?hidden=${!this.ids?.length}></tf-id-picker>
</div>
<div>
${keyed(this.whoami, html`<tf-collection
.collection=${this.wikis}
whoami=${this.whoami}
selected_id=${this.wiki?.id}
@create=${this.on_wiki_create}
@rename=${this.on_wiki_rename}
@tombstone=${this.on_wiki_tombstone}
@change=${this.on_wiki_changed}></tf-collection>`)}
${keyed(this.wiki_doc?.id, html`<tf-collection
.collection=${this.wiki_docs}
whoami=${this.whoami}
selected_id=${(this.wiki_doc && this.wiki_doc?.parent == this.wiki?.id) ? this.wiki_doc?.id : ''}
@create=${this.on_wiki_doc_create}
@rename=${this.on_wiki_doc_rename}
@tombstone=${this.on_wiki_doc_tombstone}
@change=${this.on_wiki_doc_changed}></tf-collection>`)}
<button @click=${() => self.expand_editors = !self.expand_editors}>${this.wiki?.editors?.length} editor${this.wiki?.editors?.length > 1 ? 's' : ''}</button>
${keyed(
this.whoami,
html`<tf-collection
.collection=${this.wikis}
whoami=${this.whoami}
selected_id=${this.wiki?.id}
@create=${this.on_wiki_create}
@rename=${this.on_wiki_rename}
@tombstone=${this.on_wiki_tombstone}
@change=${this.on_wiki_changed}
></tf-collection>`
)}
${keyed(
this.wiki_doc?.id,
html`<tf-collection
.collection=${this.wiki_docs}
whoami=${this.whoami}
selected_id=${this.wiki_doc &&
this.wiki_doc?.parent == this.wiki?.id
? this.wiki_doc?.id
: ''}
@create=${this.on_wiki_doc_create}
@rename=${this.on_wiki_doc_rename}
@tombstone=${this.on_wiki_doc_tombstone}
@change=${this.on_wiki_doc_changed}
></tf-collection>`
)}
<button @click=${() => (self.expand_editors = !self.expand_editors)}>${this.wiki?.editors?.length} editor${this.wiki?.editors?.length > 1 ? 's' : ''}</button>
<div ?hidden=${!this.wiki?.editors || !this.expand_editors}>
<div>
<ul>
${this.wiki?.editors.map(id => html`<li><button ?hidden=${id == this.whoami} @click=${() => self.on_remove_editor(id)}>x</button> ${id}</li>`)}
${this.wiki?.editors.map((id) => html`<li><button ?hidden=${id == this.whoami} @click=${() => self.on_remove_editor(id)}>x</button> ${id}</li>`)}
<li>
<button @click=${() => self.adding_editor = true} ?hidden=${this.wiki?.editors?.indexOf(this.whoami) == -1 || this.adding_editor}>+</button>
<button @click=${() => (self.adding_editor = true)} ?hidden=${this.wiki?.editors?.indexOf(this.whoami) == -1 || this.adding_editor}>+</button>
<div ?hidden=${!this.adding_editor}>
<label for="add_editor">Add Editor:</label>
<input type="text" id="add_editor"></input>
<button @click=${this.on_add_editor}>Add Editor</button>
<button @click=${() => self.adding_editor = false}>x</button>
<button @click=${() => (self.adding_editor = false)}>x</button>
</div>
</li>
</ul>
@ -288,25 +314,54 @@ class TfCollectionsAppElement extends LitElement {
</div>
<div style="display: flex; flex-direction: row">
<div style="flex: 0 0">
${Object.values(this.wikis || {}).sort((x, y) => x.name.localeCompare(y.name)).map(wiki => html`
<div class="toc ${self.wiki?.id === wiki.id ? 'selected' : ''}" style="white-space: nowrap; cursor: pointer" @click=${() => self.on_wiki_changed({detail: {value: wiki}})}>${wiki.name}</div>
<ul>
${Object.values(self.wiki_docs || {}).filter(doc => doc.parent === wiki?.id).sort((x, y) => x.name.localeCompare(y.name)).map(doc => html`
<li class="toc ${self.wiki_doc?.id === doc.id ? 'selected' : ''}" style="white-space: nowrap; cursor: pointer; list-style: none; text-indent: -1rem" @click=${() => self.on_wiki_doc_changed({detail: {value: doc}})}>${doc?.private ? '🔒' : '📄'} ${doc.name}</li>
`)}
</ul>
`)}
${Object.values(this.wikis || {})
.sort((x, y) => x.name.localeCompare(y.name))
.map(
(wiki) => html`
<div
class="toc ${self.wiki?.id === wiki.id ? 'selected' : ''}"
style="white-space: nowrap; cursor: pointer"
@click=${() => self.on_wiki_changed({detail: {value: wiki}})}
>
${wiki.name}
</div>
<ul>
${Object.values(self.wiki_docs || {})
.filter((doc) => doc.parent === wiki?.id)
.sort((x, y) => x.name.localeCompare(y.name))
.map(
(doc) => html`
<li
class="toc ${self.wiki_doc?.id === doc.id
? 'selected'
: ''}"
style="white-space: nowrap; cursor: pointer; list-style: none; text-indent: -1rem"
@click=${() =>
self.on_wiki_doc_changed({detail: {value: doc}})}
>
${doc?.private ? '🔒' : '📄'} ${doc.name}
</li>
`
)}
</ul>
`
)}
</div>
${this.wiki_doc && this.wiki_doc.parent === this.wiki?.id ? html`
<tf-wiki-doc
style="width: 100%"
whoami=${this.whoami}
.wiki=${this.wiki}
.value=${this.wiki_doc}></tf-wiki-doc>
` : undefined}
${
this.wiki_doc && this.wiki_doc.parent === this.wiki?.id
? html`
<tf-wiki-doc
style="width: 100%"
whoami=${this.whoami}
.wiki=${this.wiki}
.value=${this.wiki_doc}
></tf-wiki-doc>
`
: undefined
}
</div>
`;
}
}
customElements.define('tf-collections-app', TfCollectionsAppElement);
customElements.define('tf-collections-app', TfCollectionsAppElement);

View File

@ -29,10 +29,16 @@ class TfWikiDocElement extends LitElement {
let node = event.node;
if (event.entering) {
if (node.destination?.startsWith('&')) {
node.destination = '/' + node.destination + '/view?filename=' + node.firstChild?.literal;
node.destination =
'/' +
node.destination +
'/view?filename=' +
node.firstChild?.literal;
} else if (node.type === 'link') {
if (node.destination.indexOf(':') == -1 &&
node.destination.indexOf('/') == -1) {
if (
node.destination.indexOf(':') == -1 &&
node.destination.indexOf('/') == -1
) {
node.destination = `#${this.wiki?.name}/${node.destination}`;
}
}
@ -70,7 +76,9 @@ class TfWikiDocElement extends LitElement {
}
thumbnail(md) {
let m = md ? md.match(/\!\[image:[^\]]+\]\((\&.{44}\.sha256)\).*/) : undefined;
let m = md
? md.match(/\!\[image:[^\]]+\]\((\&.{44}\.sha256)\).*/)
: undefined;
return m ? m[1] : undefined;
}
@ -106,12 +114,16 @@ class TfWikiDocElement extends LitElement {
key: this.value.id,
parent: this.value.parent,
blob: id,
mentions: this.blob.match(/(&.{44}.sha256)/g)?.map(x => ({link: x})),
mentions: this.blob.match(/(&.{44}.sha256)/g)?.map((x) => ({link: x})),
private: this.value?.private,
};
if (draft) {
message.recps = this.value.editors;
message = await tfrpc.rpc.encrypt(this.whoami, this.value.editors, JSON.stringify(message));
message = await tfrpc.rpc.encrypt(
this.whoami,
this.value.editors,
JSON.stringify(message)
);
}
await tfrpc.rpc.appendMessage(this.whoami, message);
this.is_editing = false;
@ -136,16 +148,16 @@ class TfWikiDocElement extends LitElement {
summary: this.summary(blob),
thumbnail: this.thumbnail(blob),
blog: id,
mentions: this.blob.match(/(&.{44}.sha256)/g)?.map(x => ({link: x})),
mentions: this.blob.match(/(&.{44}.sha256)/g)?.map((x) => ({link: x})),
};
await tfrpc.rpc.appendMessage(this.whoami, message);
this.is_editing = false;
}
convert_to_format(buffer, type, mime_type) {
return new Promise(function(resolve, reject) {
return new Promise(function (resolve, reject) {
let img = new Image();
img.onload = function() {
img.onload = function () {
let canvas = document.createElement('canvas');
let width_scale = Math.min(img.width, 1024) / img.width;
let height_scale = Math.min(img.height, 1024) / img.height;
@ -155,13 +167,17 @@ class TfWikiDocElement extends LitElement {
let context = canvas.getContext('2d');
context.drawImage(img, 0, 0, canvas.width, canvas.height);
let data_url = canvas.toDataURL(mime_type);
let result = atob(data_url.split(',')[1]).split('').map(x => x.charCodeAt(0));
let result = atob(data_url.split(',')[1])
.split('')
.map((x) => x.charCodeAt(0));
resolve(result);
};
img.onerror = function(event) {
img.onerror = function (event) {
reject(new Error('Failed to load image.'));
};
let raw = Array.from(new Uint8Array(buffer)).map(b => String.fromCharCode(b)).join('');
let raw = Array.from(new Uint8Array(buffer))
.map((b) => String.fromCharCode(b))
.join('');
let original = `data:${type};base64,${btoa(raw)}`;
img.src = original;
});
@ -187,7 +203,11 @@ class TfWikiDocElement extends LitElement {
let best_buffer;
let best_type;
for (let format of ['image/png', 'image/jpeg', 'image/webp']) {
let test_buffer = await self.convert_to_format(buffer, file.type, format);
let test_buffer = await self.convert_to_format(
buffer,
file.type,
format
);
if (!best_buffer || test_buffer.length < best_buffer.length) {
best_buffer = test_buffer;
best_type = format;
@ -206,7 +226,7 @@ class TfWikiDocElement extends LitElement {
}
document.execCommand('insertText', false, insert);
self.on_edit({srcElement: editor});
} catch(e) {
} catch (e) {
alert(e?.message);
}
}
@ -234,31 +254,84 @@ class TfWikiDocElement extends LitElement {
let thumbnail_ref = this.thumbnail(this.blob);
return html`
<style>
a:link { color: #268bd2 }
a:visited { color: #6c71c4 }
a:hover { color: #859900 }
a:active { color: #2aa198 }
a:link {
color: #268bd2;
}
a:visited {
color: #6c71c4;
}
a:hover {
color: #859900;
}
a:active {
color: #2aa198;
}
</style>
<div style="display: inline-flex; flex-direction: row">
<button ?disabled=${!this.whoami || this.is_editing} @click=${() => self.is_editing = true}>Edit</button>
<button ?disabled=${this.blob == this.blob_original} @click=${this.on_save_draft}>Save Draft</button>
<button ?disabled=${this.blob == this.blob_original && !this.value?.draft} @click=${this.on_publish}>Publish</button>
<button ?disabled=${!this.is_editing} @click=${this.on_discard}>Discard</button>
<button ?disabled=${!this.is_editing} @click=${() => self.value = Object.assign({}, self.value, {private: !self.value.private})}>${this.value?.private ? 'Make Public' : 'Make Private'}</button>
<button ?disabled=${!this.is_editing} @click=${this.on_blog_publish}>Publish Blog</button>
<button
?disabled=${!this.whoami || this.is_editing}
@click=${() => (self.is_editing = true)}
>
Edit
</button>
<button
?disabled=${this.blob == this.blob_original}
@click=${this.on_save_draft}
>
Save Draft
</button>
<button
?disabled=${this.blob == this.blob_original && !this.value?.draft}
@click=${this.on_publish}
>
Publish
</button>
<button ?disabled=${!this.is_editing} @click=${this.on_discard}>
Discard
</button>
<button
?disabled=${!this.is_editing}
@click=${() =>
(self.value = Object.assign({}, self.value, {
private: !self.value.private,
}))}
>
${this.value?.private ? 'Make Public' : 'Make Private'}
</button>
<button ?disabled=${!this.is_editing} @click=${this.on_blog_publish}>
Publish Blog
</button>
</div>
<div ?hidden=${!this.value?.private} style="color: #800">🔒 document is private</div>
<div style="display: flex; flex-direction: row; ${this.value?.private ? 'border-top: 4px solid #800' : ''}">
<div ?hidden=${!this.value?.private} style="color: #800">
🔒 document is private
</div>
<div
style="display: flex; flex-direction: row; ${this.value?.private
? 'border-top: 4px solid #800'
: ''}"
>
<textarea
?hidden=${!this.is_editing}
style="flex: 1 1; min-height: 10em; ${this.value?.private ? 'border: 4px solid #800' : ''}"
style="flex: 1 1; min-height: 10em; ${this.value?.private
? 'border: 4px solid #800'
: ''}"
@input=${this.on_edit}
@paste=${this.paste}
.value=${this.blob ?? ''}></textarea>
.value=${this.blob ?? ''}
></textarea>
<div style="flex: 1 1">
<div ?hidden=${!this.is_editing} style="border: 1px solid #fff; border-radius: 1em; padding: 0.5em">
<img ?hidden=${!thumbnail_ref} style="max-width: 128px; max-height: 128px; float: right" src="/${thumbnail_ref}/view">
<h1 ?hidden=${!this.title(this.blob)}>${unsafeHTML(this.markdown(this.title(this.blob)))}</h1>
<div
?hidden=${!this.is_editing}
style="border: 1px solid #fff; border-radius: 1em; padding: 0.5em"
>
<img
?hidden=${!thumbnail_ref}
style="max-width: 128px; max-height: 128px; float: right"
src="/${thumbnail_ref}/view"
/>
<h1 ?hidden=${!this.title(this.blob)}>
${unsafeHTML(this.markdown(this.title(this.blob)))}
</h1>
${unsafeHTML(this.markdown(this.summary(this.blob)))}
</div>
${unsafeHTML(this.markdown(this.blob))}
@ -268,4 +341,4 @@ class TfWikiDocElement extends LitElement {
}
}
customElements.define('tf-wiki-doc', TfWikiDocElement);
customElements.define('tf-wiki-doc', TfWikiDocElement);

View File

@ -2,7 +2,7 @@ async function process_message(whoami, collection, message, kind, parent) {
let content = JSON.parse(message.content);
if (typeof content == 'string') {
let x;
for (let id of (whoami || [])) {
for (let id of whoami || []) {
x = await ssb.privateMessageDecrypt(id, content);
if (x) {
try {
@ -17,8 +17,7 @@ async function process_message(whoami, collection, message, kind, parent) {
if (!x) {
return;
}
if (content.type !== kind ||
(parent && content.parent !== parent)) {
if (content.type !== kind || (parent && content.parent !== parent)) {
return;
}
} else {
@ -28,7 +27,10 @@ async function process_message(whoami, collection, message, kind, parent) {
if (content?.tombstone) {
delete collection[content.key];
} else {
collection[content.key] = Object.assign(collection[content.key] || {}, content);
collection[content.key] = Object.assign(
collection[content.key] || {},
content
);
}
} else {
collection[message.id] = Object.assign(content, {id: message.id});
@ -40,7 +42,7 @@ async function process_message(whoami, collection, message, kind, parent) {
}
let g_new_message_resolve;
let g_new_message_promise = new Promise(function(resolve, reject) {
let g_new_message_promise = new Promise(function (resolve, reject) {
g_new_message_resolve = resolve;
});
@ -48,9 +50,9 @@ function new_message() {
return g_new_message_promise;
}
ssb.addEventListener('message', function(id) {
ssb.addEventListener('message', function (id) {
let resolve = g_new_message_resolve;
g_new_message_promise = new Promise(function(resolve, reject) {
g_new_message_promise = new Promise(function (resolve, reject) {
g_new_message_resolve = resolve;
});
if (resolve) {
@ -58,26 +60,42 @@ ssb.addEventListener('message', function(id) {
}
});
export async function collection(ids, kind, parent, max_rowid, data, include_private) {
export async function collection(
ids,
kind,
parent,
max_rowid,
data,
include_private
) {
let whoami = await ssb.getIdentities();
data = data ?? {};
let rowid = 0;
let first = true;
await ssb.sqlAsync('SELECT MAX(rowid) AS rowid FROM messages', [], function(row) {
rowid = row.rowid;
});
await ssb.sqlAsync(
'SELECT MAX(rowid) AS rowid FROM messages',
[],
function (row) {
rowid = row.rowid;
}
);
while (true) {
if (rowid == max_rowid) {
await new_message();
await ssb.sqlAsync('SELECT MAX(rowid) AS rowid FROM messages', [], function(row) {
rowid = row.rowid;
});
await ssb.sqlAsync(
'SELECT MAX(rowid) AS rowid FROM messages',
[],
function (row) {
rowid = row.rowid;
}
);
first = false;
}
let modified = false;
let rows = [];
await ssb.sqlAsync(`
await ssb.sqlAsync(
`
SELECT messages.id, author, content, timestamp
FROM messages
JOIN json_each(?1) AS id ON messages.author = id.value
@ -88,9 +106,19 @@ export async function collection(ids, kind, parent, max_rowid, data, include_pri
(?5 IS NULL OR json_extract(messages.content, '$.parent') = ?5)) OR
(?6 AND content LIKE '"%'))
ORDER BY timestamp
`, [JSON.stringify(ids), max_rowid ?? -1, rowid, kind, parent, include_private ? true : false], function(row) {
rows.push(row);
});
`,
[
JSON.stringify(ids),
max_rowid ?? -1,
rowid,
kind,
parent,
include_private ? true : false,
],
function (row) {
rows.push(row);
}
);
max_rowid = rowid;
for (let row of rows) {
if (await process_message(whoami, data, row, kind, parent)) {
@ -102,4 +130,4 @@ export async function collection(ids, kind, parent, max_rowid, data, include_pri
}
}
return [rowid, data];
}
}