| 
									
										
										
										
											2024-04-04 02:12:43 +01:00
										 |  |  | import * as tfrpc from '/static/tfrpc.js'; | 
					
						
							| 
									
										
										
										
											2024-05-30 12:40:21 -04:00
										 |  |  | import {html, render} from './lit-all.min.js'; | 
					
						
							| 
									
										
										
										
											2025-10-22 19:39:20 -04:00
										 |  |  | import {styles, generate_theme} from './tf-styles.js'; | 
					
						
							| 
									
										
										
										
											2024-04-04 02:12:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-06 23:26:43 +00:00
										 |  |  | let g_emojis; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function get_emojis() { | 
					
						
							|  |  |  | 	if (g_emojis) { | 
					
						
							|  |  |  | 		return Promise.resolve(g_emojis); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2024-02-24 11:09:34 -05:00
										 |  |  | 	return fetch('emojis.json').then(function (result) { | 
					
						
							| 
									
										
										
										
											2022-09-06 23:26:43 +00:00
										 |  |  | 		g_emojis = result.json(); | 
					
						
							|  |  |  | 		return g_emojis; | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-29 20:48:47 -04:00
										 |  |  | export async function picker(callback, anchor, author, recent) { | 
					
						
							| 
									
										
										
										
											2024-04-04 02:12:43 +01:00
										 |  |  | 	let json = await get_emojis(); | 
					
						
							| 
									
										
										
										
											2023-01-14 22:27:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-04 02:12:43 +01:00
										 |  |  | 	let div = document.createElement('div'); | 
					
						
							|  |  |  | 	div.id = 'emoji_picker'; | 
					
						
							|  |  |  | 	div.style.color = '#000'; | 
					
						
							|  |  |  | 	div.style.background = '#fff'; | 
					
						
							|  |  |  | 	div.style.border = '1px solid #000'; | 
					
						
							| 
									
										
										
										
											2024-12-23 10:28:12 -05:00
										 |  |  | 	div.style.display = 'flex'; | 
					
						
							| 
									
										
										
										
											2024-04-04 02:12:43 +01:00
										 |  |  | 	div.style.overflow = 'scroll'; | 
					
						
							|  |  |  | 	div.style.fontWeight = 'bold'; | 
					
						
							|  |  |  | 	div.style.fontSize = 'xx-large'; | 
					
						
							| 
									
										
										
										
											2024-12-23 10:28:12 -05:00
										 |  |  | 	div.style.flex = '1 1'; | 
					
						
							|  |  |  | 	div.style.flexDirection = 'column'; | 
					
						
							| 
									
										
										
										
											2024-04-04 02:12:43 +01:00
										 |  |  | 	let input = document.createElement('input'); | 
					
						
							|  |  |  | 	input.type = 'text'; | 
					
						
							|  |  |  | 	input.style.display = 'block'; | 
					
						
							|  |  |  | 	input.style.boxSizing = 'border-box'; | 
					
						
							|  |  |  | 	input.style.width = '100%'; | 
					
						
							|  |  |  | 	input.style.margin = '0'; | 
					
						
							|  |  |  | 	input.style.position = 'relative'; | 
					
						
							|  |  |  | 	div.appendChild(input); | 
					
						
							|  |  |  | 	let list = document.createElement('div'); | 
					
						
							| 
									
										
										
										
											2024-12-23 10:28:12 -05:00
										 |  |  | 	list.style.overflow = 'scroll'; | 
					
						
							| 
									
										
										
										
											2024-04-04 02:12:43 +01:00
										 |  |  | 	div.appendChild(list); | 
					
						
							|  |  |  | 	div.addEventListener('mousedown', function (event) { | 
					
						
							|  |  |  | 		event.stopPropagation(); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	function key_down(event) { | 
					
						
							|  |  |  | 		if (event.key == 'Escape') { | 
					
						
							| 
									
										
										
										
											2023-05-04 00:32:50 +00:00
										 |  |  | 			cleanup(); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2024-04-04 02:12:43 +01:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-05-04 00:32:50 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-04 02:12:43 +01:00
										 |  |  | 	function chosen(event) { | 
					
						
							|  |  |  | 		console.log(event.srcElement.innerText); | 
					
						
							|  |  |  | 		callback(event.srcElement.innerText); | 
					
						
							|  |  |  | 		cleanup(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	function refresh() { | 
					
						
							|  |  |  | 		while (list.firstChild) { | 
					
						
							|  |  |  | 			list.removeChild(list.firstChild); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		let search = input.value.toLowerCase(); | 
					
						
							|  |  |  | 		let any_at_all = false; | 
					
						
							|  |  |  | 		if (recent) { | 
					
						
							|  |  |  | 			let emoji_to_name = {}; | 
					
						
							|  |  |  | 			for (let row of Object.values(json)) { | 
					
						
							|  |  |  | 				for (let entry of Object.entries(row)) { | 
					
						
							|  |  |  | 					emoji_to_name[entry[1]] = entry[0]; | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2022-09-06 23:26:43 +00:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2024-04-04 02:12:43 +01:00
										 |  |  | 			let header = document.createElement('div'); | 
					
						
							|  |  |  | 			header.appendChild(document.createTextNode('Recent')); | 
					
						
							|  |  |  | 			list.appendChild(header); | 
					
						
							|  |  |  | 			let any = false; | 
					
						
							|  |  |  | 			for (let entry of recent) { | 
					
						
							|  |  |  | 				if ( | 
					
						
							|  |  |  | 					search && | 
					
						
							|  |  |  | 					search.length && | 
					
						
							|  |  |  | 					(emoji_to_name[entry] || '').toLowerCase().indexOf(search) == -1 | 
					
						
							|  |  |  | 				) { | 
					
						
							|  |  |  | 					continue; | 
					
						
							| 
									
										
										
										
											2022-09-06 23:26:43 +00:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2024-04-04 02:12:43 +01:00
										 |  |  | 				let emoji = document.createElement('span'); | 
					
						
							|  |  |  | 				const k_size = '1.25em'; | 
					
						
							|  |  |  | 				emoji.style.display = 'inline-block'; | 
					
						
							|  |  |  | 				emoji.style.overflow = 'hidden'; | 
					
						
							|  |  |  | 				emoji.style.cursor = 'pointer'; | 
					
						
							|  |  |  | 				emoji.onclick = chosen; | 
					
						
							|  |  |  | 				emoji.title = emoji_to_name[entry] || entry; | 
					
						
							|  |  |  | 				emoji.appendChild(document.createTextNode(entry)); | 
					
						
							|  |  |  | 				list.appendChild(emoji); | 
					
						
							|  |  |  | 				any = true; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			if (!any) { | 
					
						
							|  |  |  | 				list.removeChild(header); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		for (let row of Object.entries(json)) { | 
					
						
							|  |  |  | 			let header = document.createElement('div'); | 
					
						
							|  |  |  | 			header.appendChild(document.createTextNode(row[0])); | 
					
						
							|  |  |  | 			list.appendChild(header); | 
					
						
							|  |  |  | 			let any = false; | 
					
						
							|  |  |  | 			for (let entry of Object.entries(row[1])) { | 
					
						
							|  |  |  | 				if ( | 
					
						
							|  |  |  | 					search && | 
					
						
							|  |  |  | 					search.length && | 
					
						
							|  |  |  | 					entry[0].toLowerCase().indexOf(search) == -1 | 
					
						
							|  |  |  | 				) { | 
					
						
							|  |  |  | 					continue; | 
					
						
							| 
									
										
										
										
											2022-09-06 23:26:43 +00:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2024-04-04 02:12:43 +01:00
										 |  |  | 				let emoji = document.createElement('span'); | 
					
						
							|  |  |  | 				const k_size = '1.25em'; | 
					
						
							|  |  |  | 				emoji.style.display = 'inline-block'; | 
					
						
							|  |  |  | 				emoji.style.overflow = 'hidden'; | 
					
						
							|  |  |  | 				emoji.style.cursor = 'pointer'; | 
					
						
							|  |  |  | 				emoji.onclick = chosen; | 
					
						
							|  |  |  | 				emoji.title = entry[0]; | 
					
						
							|  |  |  | 				emoji.appendChild(document.createTextNode(entry[1])); | 
					
						
							|  |  |  | 				list.appendChild(emoji); | 
					
						
							|  |  |  | 				any = true; | 
					
						
							|  |  |  | 				any_at_all = true; | 
					
						
							| 
									
										
										
										
											2023-05-04 00:32:50 +00:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2024-04-04 02:12:43 +01:00
										 |  |  | 			if (!any) { | 
					
						
							|  |  |  | 				list.removeChild(header); | 
					
						
							| 
									
										
										
										
											2023-01-14 22:27:35 +00:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2022-09-06 23:26:43 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2024-04-04 02:12:43 +01:00
										 |  |  | 		if (!any_at_all) { | 
					
						
							|  |  |  | 			list.appendChild(document.createTextNode('No matches found.')); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	refresh(); | 
					
						
							|  |  |  | 	input.oninput = refresh; | 
					
						
							| 
									
										
										
										
											2024-12-22 14:45:16 -05:00
										 |  |  | 	let parent = document.createElement('div'); | 
					
						
							|  |  |  | 	function cleanup() { | 
					
						
							|  |  |  | 		parent.parentElement.removeChild(parent); | 
					
						
							|  |  |  | 		window.removeEventListener('keydown', key_down); | 
					
						
							|  |  |  | 		document.body.removeEventListener('mousedown', cleanup); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2024-05-30 12:40:21 -04:00
										 |  |  | 	let modal = html`
 | 
					
						
							|  |  |  | 		<style> | 
					
						
							|  |  |  | 			${styles} | 
					
						
							|  |  |  | 		</style> | 
					
						
							| 
									
										
										
										
											2025-10-22 19:39:20 -04:00
										 |  |  | 		<style> | 
					
						
							|  |  |  | 			${generate_theme()} | 
					
						
							|  |  |  | 		</style> | 
					
						
							| 
									
										
										
										
											2024-12-23 11:08:27 -05:00
										 |  |  | 		<div | 
					
						
							|  |  |  | 			class="w3-modal" | 
					
						
							|  |  |  | 			style="display: block; box-sizing: border-box; z-index: 10" | 
					
						
							|  |  |  | 		> | 
					
						
							| 
									
										
										
										
											2024-12-23 10:28:12 -05:00
										 |  |  | 			<div class="w3-modal-content w3-card-4"> | 
					
						
							| 
									
										
										
										
											2024-12-23 11:08:27 -05:00
										 |  |  | 				<div | 
					
						
							|  |  |  | 					class="w3-content w3-theme-d1" | 
					
						
							| 
									
										
										
										
											2025-01-27 12:45:54 -05:00
										 |  |  | 					style="display: flex; flex-direction: column; max-height: 80vh" | 
					
						
							| 
									
										
										
										
											2024-12-23 11:08:27 -05:00
										 |  |  | 				> | 
					
						
							| 
									
										
										
										
											2024-12-23 10:28:12 -05:00
										 |  |  | 					<header class="w3-container" style="flex: 0 0"> | 
					
						
							| 
									
										
										
										
											2024-12-22 14:45:16 -05:00
										 |  |  | 						<h1>Choose a Reaction</h1> | 
					
						
							|  |  |  | 						<span class="w3-button w3-display-topright" @click=${cleanup} | 
					
						
							|  |  |  | 							>×</span | 
					
						
							|  |  |  | 						> | 
					
						
							|  |  |  | 					</header> | 
					
						
							|  |  |  | 					${div} | 
					
						
							| 
									
										
										
										
											2024-12-23 10:28:12 -05:00
										 |  |  | 					<footer class="w3-container w3-padding" style="flex: 0 0"> | 
					
						
							| 
									
										
										
										
											2024-12-22 14:45:16 -05:00
										 |  |  | 						<button class="w3-button" @click=${cleanup}>Close</button> | 
					
						
							|  |  |  | 					</footer> | 
					
						
							|  |  |  | 				</div> | 
					
						
							|  |  |  | 			</div> | 
					
						
							| 
									
										
										
										
											2024-05-30 12:40:21 -04:00
										 |  |  | 		</div> | 
					
						
							|  |  |  | 	`;
 | 
					
						
							|  |  |  | 	document.body.appendChild(parent); | 
					
						
							|  |  |  | 	render(modal, parent); | 
					
						
							| 
									
										
										
										
											2024-04-04 02:12:43 +01:00
										 |  |  | 	input.focus(); | 
					
						
							|  |  |  | 	document.body.addEventListener('mousedown', cleanup); | 
					
						
							|  |  |  | 	window.addEventListener('keydown', key_down); | 
					
						
							| 
									
										
										
										
											2024-02-24 11:09:34 -05:00
										 |  |  | } |