From 843e172e562eb2be38eb5dadad48036749e8c970 Mon Sep 17 00:00:00 2001 From: Cory McWilliams Date: Wed, 24 Jan 2024 02:58:53 +0000 Subject: [PATCH] Allow pasting non-image files into the wiki editor. git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@4789 ed5197a5-7fde-0310-b194-c3ffbd925b24 --- apps/wiki.json | 2 +- apps/wiki/tf-wiki-app.js | 2 +- apps/wiki/tf-wiki-doc.js | 31 +++++++++++++++++++++---------- 3 files changed, 23 insertions(+), 12 deletions(-) diff --git a/apps/wiki.json b/apps/wiki.json index ba1c03620..14dc08d61 100644 --- a/apps/wiki.json +++ b/apps/wiki.json @@ -1,5 +1,5 @@ { "type": "tildefriends-app", "emoji": "📝", - "previous": "&w+d5glrC/yHNrj1b6nihHNn4rpNZG5R4GuThkZ/XT6w=.sha256" + "previous": "&qrcrBeaWg89ikgql9hXdr68krkg+5NZkmwTbpodEW4U=.sha256" } \ No newline at end of file diff --git a/apps/wiki/tf-wiki-app.js b/apps/wiki/tf-wiki-app.js index d287a2302..17ca2e303 100644 --- a/apps/wiki/tf-wiki-app.js +++ b/apps/wiki/tf-wiki-app.js @@ -292,7 +292,7 @@ class TfCollectionsAppElement extends LitElement {
self.on_wiki_changed({detail: {value: wiki}})}>${wiki.name}
`)} diff --git a/apps/wiki/tf-wiki-doc.js b/apps/wiki/tf-wiki-doc.js index 40ed53bed..e6acc0755 100644 --- a/apps/wiki/tf-wiki-doc.js +++ b/apps/wiki/tf-wiki-doc.js @@ -73,7 +73,6 @@ class TfWikiDocElement extends LitElement { thumbnail(md) { let m = md ? md.match(/\!\[image:[^\]]+\]\((\&.{44}\.sha256)\).*/) : undefined; - console.log('thumb', m); return m ? m[1] : undefined; } @@ -170,11 +169,22 @@ class TfWikiDocElement extends LitElement { }); } + humanSize(value) { + let units = ['B', 'kB', 'MB', 'GB']; + let i = 0; + while (i < units.length - 1 && value >= 1024) { + value /= 1024; + i++; + } + return `${Math.round(value * 10) / 10} ${units[i]}`; + } + async add_file(editor, file) { try { let self = this; let buffer = await file.arrayBuffer(); let type = file.type; + let insert; if (type.startsWith('image/')) { let best_buffer; let best_type; @@ -187,12 +197,16 @@ class TfWikiDocElement extends LitElement { } buffer = best_buffer; type = best_type; + let id = await tfrpc.rpc.store_blob(buffer); + let name = type.split('/')[0] + ':' + file.name; + insert = `\n![${name}](${id})`; } else { buffer = Array.from(new Uint8Array(buffer)); + let id = await tfrpc.rpc.store_blob(buffer); + let name = file.name; + insert = `\n[${name}](${id}) (${this.humanSize(buffer.length)})`; } - let id = await tfrpc.rpc.store_blob(buffer); - let name = type.split('/')[0] + ':' + file.name; - editor.value += `\n![${name}](${id})`; + document.execCommand('insertText', false, insert); self.on_edit({srcElement: editor}); } catch(e) { alert(e?.message); @@ -202,13 +216,10 @@ class TfWikiDocElement extends LitElement { paste(event) { let self = this; for (let item of event.clipboardData.items) { - if (item.type?.startsWith('image/')) { - let file = item.getAsFile(); - if (!file) { - continue; - } + let file = item.getAsFile(); + if (file) { self.add_file(event.srcElement, file); - break; + event.preventDefault(); } } }