|
1 | 1 | import {emojiKeys, emojiHTML, emojiString} from './emoji.js'; |
| 2 | +import {uniq} from '../utils.js'; |
2 | 3 |
|
3 | | -export const issuesTribute = window.config.Tribute ? new Tribute({ |
4 | | - values: window.config.tributeValues, |
5 | | - noMatchTemplate() { return null }, |
6 | | - menuItemTemplate(item) { |
7 | | - const div = $('<div/>'); |
8 | | - div.append($('<img/>', {src: item.original.avatar})); |
9 | | - div.append($('<span/>', {class: 'name'}).text(item.original.name)); |
10 | | - if (item.original.fullname && item.original.fullname !== '') { |
11 | | - div.append($('<span/>', {class: 'fullname'}).text(item.original.fullname)); |
12 | | - } |
13 | | - return div.html(); |
14 | | - } |
15 | | -}) : null; |
16 | | - |
17 | | -export const emojiTribute = window.config.Tribute ? new Tribute({ |
18 | | - collection: [{ |
19 | | - trigger: ':', |
20 | | - requireLeadingSpace: true, |
21 | | - values(query, cb) { |
22 | | - const matches = []; |
23 | | - for (const name of emojiKeys) { |
24 | | - if (name.includes(query)) { |
25 | | - matches.push(name); |
26 | | - if (matches.length > 5) break; |
| 4 | +function makeCollections({mentions, emoji}) { |
| 5 | + const collections = []; |
| 6 | + |
| 7 | + if (mentions) { |
| 8 | + collections.push({ |
| 9 | + trigger: ':', |
| 10 | + requireLeadingSpace: true, |
| 11 | + values: (query, cb) => { |
| 12 | + const matches = []; |
| 13 | + for (const name of emojiKeys) { |
| 14 | + if (name.includes(query)) { |
| 15 | + matches.push(name); |
| 16 | + if (matches.length > 5) break; |
| 17 | + } |
27 | 18 | } |
| 19 | + cb(matches); |
| 20 | + }, |
| 21 | + lookup: (item) => item, |
| 22 | + selectTemplate: (item) => { |
| 23 | + if (typeof item === 'undefined') return null; |
| 24 | + return emojiString(item.original); |
| 25 | + }, |
| 26 | + menuItemTemplate: (item) => { |
| 27 | + return `<div class="tribute-item">${emojiHTML(item.original)}<span>${item.original}</span></div>`; |
28 | 28 | } |
29 | | - cb(matches); |
30 | | - }, |
31 | | - lookup(item) { |
32 | | - return item; |
33 | | - }, |
34 | | - selectTemplate(item) { |
35 | | - if (typeof item === 'undefined') return null; |
36 | | - return emojiString(item.original); |
37 | | - }, |
38 | | - menuItemTemplate(item) { |
39 | | - return `<div class="tribute-item">${emojiHTML(item.original)}<span>${item.original}</span></div>`; |
40 | | - } |
41 | | - }] |
42 | | -}) : null; |
43 | | - |
44 | | -export function initTribute() { |
45 | | - if (!window.config.Tribute) return; |
46 | | - |
47 | | - let content = document.getElementById('content'); |
48 | | - if (content !== null) { |
49 | | - issuesTribute.attach(content); |
| 29 | + }); |
50 | 30 | } |
51 | 31 |
|
52 | | - const emojiInputs = document.querySelectorAll('.emoji-input'); |
53 | | - if (emojiInputs.length > 0) { |
54 | | - emojiTribute.attach(emojiInputs); |
| 32 | + if (emoji) { |
| 33 | + collections.push({ |
| 34 | + values: window.config.tributeValues, |
| 35 | + noMatchTemplate: () => null, |
| 36 | + menuItemTemplate: (item) => { |
| 37 | + return ` |
| 38 | + <div class="tribute-item"> |
| 39 | + <img src="${item.original.avatar}"/> |
| 40 | + <span class="name">${item.original.name}</span> |
| 41 | + ${item.original.fullname && item.original.fullname !== '' ? `<span class="fullname">${item.original.fullname}</span>` : ''} |
| 42 | + </div> |
| 43 | + `; |
| 44 | + } |
| 45 | + }); |
55 | 46 | } |
56 | 47 |
|
57 | | - content = document.getElementById('content'); |
58 | | - if (content !== null) { |
59 | | - emojiTribute.attach(document.getElementById('content')); |
| 48 | + return collections; |
| 49 | +} |
| 50 | + |
| 51 | +export default async function attachTribute(elementOrNodeList, {mentions, emoji} = {}) { |
| 52 | + if (!window.config.Tribute || !elementOrNodeList) return; |
| 53 | + const nodes = Array.from('length' in elementOrNodeList ? elementOrNodeList : [elementOrNodeList]); |
| 54 | + if (!nodes.length) return; |
| 55 | + |
| 56 | + const mentionNodes = nodes.filter((node) => { |
| 57 | + return mentions || node.id === 'content'; |
| 58 | + }); |
| 59 | + const emojiNodes = nodes.filter((node) => { |
| 60 | + return emoji || node.id === 'content' || node.classList.contains('emoji-input'); |
| 61 | + }); |
| 62 | + const uniqueNodes = uniq([...mentionNodes, ...emojiNodes]); |
| 63 | + if (!uniqueNodes.length) return; |
| 64 | + |
| 65 | + const {default: Tribute} = await import(/* webpackChunkName: "tribute" */'tributejs'); |
| 66 | + |
| 67 | + const collections = makeCollections({ |
| 68 | + mentions: mentions || mentionNodes.length > 0, |
| 69 | + emoji: emoji || emojiNodes.length > 0, |
| 70 | + }); |
| 71 | + |
| 72 | + const tribute = new Tribute({collection: collections}); |
| 73 | + for (const node of uniqueNodes) { |
| 74 | + tribute.attach(node); |
60 | 75 | } |
| 76 | + return tribute; |
61 | 77 | } |
0 commit comments