|
| 1 | +{ |
| 2 | + class ZenEmojiPicker extends ZenDOMOperatedFeature { |
| 3 | + #panel; |
| 4 | + |
| 5 | + #anchor; |
| 6 | + |
| 7 | + #currentPromise = null; |
| 8 | + #currentPromiseResolve = null; |
| 9 | + #currentPromiseReject = null; |
| 10 | + |
| 11 | + init() { |
| 12 | + this.#panel = document.getElementById('PanelUI-zen-emojis-picker'); |
| 13 | + this.#panel.addEventListener('popupshowing', this); |
| 14 | + this.#panel.addEventListener('popuphidden', this); |
| 15 | + document.getElementById('PanelUI-zen-emojis-picker-none').addEventListener('command', this); |
| 16 | + this.searchInput.addEventListener('input', this); |
| 17 | + } |
| 18 | + |
| 19 | + handleEvent(event) { |
| 20 | + switch (event.type) { |
| 21 | + case 'popupshowing': |
| 22 | + this.#onPopupShowing(event); |
| 23 | + break; |
| 24 | + case 'popuphidden': |
| 25 | + this.#onPopupHidden(event); |
| 26 | + break; |
| 27 | + case 'command': |
| 28 | + if (event.target.id === 'PanelUI-zen-emojis-picker-none') { |
| 29 | + this.#selectEmoji(null); |
| 30 | + } |
| 31 | + break; |
| 32 | + case 'input': |
| 33 | + this.#onSearchInput(event); |
| 34 | + break; |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + get #emojis() { |
| 39 | + if (this._emojis) { |
| 40 | + return this._emojis; |
| 41 | + } |
| 42 | + const lazy = {}; |
| 43 | + Services.scriptloader.loadSubScript( |
| 44 | + 'chrome://browser/content/zen-components/ZenEmojisData.min.mjs', |
| 45 | + lazy |
| 46 | + ); |
| 47 | + this._emojis = lazy.ZenEmojisData; |
| 48 | + return this._emojis; |
| 49 | + } |
| 50 | + |
| 51 | + get emojiList() { |
| 52 | + return document.getElementById('PanelUI-zen-emojis-picker-list'); |
| 53 | + } |
| 54 | + |
| 55 | + get searchInput() { |
| 56 | + return document.getElementById('PanelUI-zen-emojis-picker-search'); |
| 57 | + } |
| 58 | + |
| 59 | + #clearEmojis() { |
| 60 | + delete this._emojis; |
| 61 | + } |
| 62 | + |
| 63 | + #onSearchInput(event) { |
| 64 | + const input = event.target; |
| 65 | + const value = input.value.trim().toLowerCase(); |
| 66 | + // search for emojis.tags and order by emojis.order |
| 67 | + const filteredEmojis = this.#emojis |
| 68 | + .filter((emoji) => { |
| 69 | + return emoji.tags.some((tag) => tag.toLowerCase().includes(value)); |
| 70 | + }) |
| 71 | + .sort((a, b) => a.order - b.order); |
| 72 | + for (const button of this.emojiList.children) { |
| 73 | + const buttonEmoji = button.getAttribute('label'); |
| 74 | + const emojiObject = filteredEmojis.find((emoji) => emoji.emoji === buttonEmoji); |
| 75 | + if (emojiObject) { |
| 76 | + button.hidden = !emojiObject.tags.some((tag) => tag.toLowerCase().includes(value)); |
| 77 | + button.style.order = emojiObject.order; |
| 78 | + } else { |
| 79 | + button.hidden = true; |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + #onPopupShowing(event) { |
| 85 | + if (event.target !== this.#panel) return; |
| 86 | + const emojiList = this.emojiList; |
| 87 | + for (const emoji of this.#emojis) { |
| 88 | + const item = document.createXULElement('toolbarbutton'); |
| 89 | + item.className = 'toolbarbutton-1 zen-emojis-picker-emoji'; |
| 90 | + item.setAttribute('label', emoji.emoji); |
| 91 | + item.setAttribute('tooltiptext', emoji.annotation); |
| 92 | + item.addEventListener('command', () => { |
| 93 | + this.#selectEmoji(emoji.emoji); |
| 94 | + }); |
| 95 | + emojiList.appendChild(item); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + #onPopupHidden(event) { |
| 100 | + if (event.target !== this.#panel) return; |
| 101 | + this.#clearEmojis(); |
| 102 | + |
| 103 | + const emojiList = this.emojiList; |
| 104 | + emojiList.innerHTML = ''; |
| 105 | + |
| 106 | + if (this.#currentPromiseReject) { |
| 107 | + this.#currentPromiseReject(new Error('Emoji picker closed without selection')); |
| 108 | + } |
| 109 | + |
| 110 | + this.#currentPromise = null; |
| 111 | + this.#currentPromiseResolve = null; |
| 112 | + this.#currentPromiseReject = null; |
| 113 | + |
| 114 | + this.#anchor.removeAttribute('zen-emoji-open'); |
| 115 | + this.#anchor = null; |
| 116 | + } |
| 117 | + |
| 118 | + #selectEmoji(emoji) { |
| 119 | + this.#currentPromiseResolve?.(emoji); |
| 120 | + this.#panel.hidePopup(); |
| 121 | + } |
| 122 | + |
| 123 | + open(anchor) { |
| 124 | + if (this.#currentPromise) { |
| 125 | + return null; |
| 126 | + } |
| 127 | + this.#currentPromise = new Promise((resolve, reject) => { |
| 128 | + this.#currentPromiseResolve = resolve; |
| 129 | + this.#currentPromiseReject = reject; |
| 130 | + }); |
| 131 | + this.#anchor = anchor; |
| 132 | + this.#anchor.setAttribute('zen-emoji-open', 'true'); |
| 133 | + this.#panel.openPopup(anchor, 'after_start', 0, 0, false, false); |
| 134 | + return this.#currentPromise; |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + window.gZenEmojiPicker = new ZenEmojiPicker(); |
| 139 | +} |
0 commit comments