From 9fecbd97e87dae85ea6182306375d73f42be36b5 Mon Sep 17 00:00:00 2001 From: Cory McWilliams Date: Thu, 21 Sep 2023 00:26:29 +0000 Subject: [PATCH] Autocomplete blob refs. That's too cool. git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@4467 ed5197a5-7fde-0310-b194-c3ffbd925b24 --- apps/ssb/tf-compose.js | 43 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 39 insertions(+), 4 deletions(-) diff --git a/apps/ssb/tf-compose.js b/apps/ssb/tf-compose.js index ea2476245..1f4f264ee 100644 --- a/apps/ssb/tf-compose.js +++ b/apps/ssb/tf-compose.js @@ -237,12 +237,47 @@ class TfComposeElement extends LitElement { input.click(); } + async autocomplete(text, callback) { + this.last_autocomplete = text; + let results = []; + try { + let rows = await tfrpc.rpc.query(` + SELECT messages.content FROM messages_fts(?) + JOIN messages ON messages.rowid = messages_fts.rowid + WHERE messages.content LIKE ? + ORDER BY timestamp DESC LIMIT 10 + `, ['"' + text.replace('"', '""') + '"', `%![%${text}%](%)%`]); + for (let row of rows) { + for (let match of row.content.matchAll(/!\[([^\]]*)\]\((&.*?)\)/g)) { + if (match[1].toLowerCase().indexOf(text) != -1) { + results.push({key: match[1], value: match[2]}); + } + } + } + } finally { + if (this.last_autocomplete === text) { + callback(results); + } + } + } + firstUpdated() { let tribute = new Tribute({ - values: Object.entries(this.users).map(x => ({key: x[1].name, value: x[0]})), - selectTemplate: function(item) { - return `[@${item.original.key}](${item.original.value})`; - }, + collection: [ + { + values: Object.entries(this.users).map(x => ({key: x[1].name, value: x[0]})), + selectTemplate: function(item) { + return `[@${item.original.key}](${item.original.value})`; + }, + }, + { + trigger: '&', + values: this.autocomplete, + selectTemplate: function(item) { + return `![${item.original.key}](${item.original.value})`; + }, + }, + ], }); tribute.attach(this.renderRoot.getElementById('edit')); }