Run prettier.
This commit is contained in:
@ -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);
|
||||
|
Reference in New Issue
Block a user