forked from cory/tildefriends
		
	Docs and emoji picker and probably some other random app updates.
git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@4124 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
		| @@ -24,20 +24,41 @@ export function picker(callback, anchor) { | ||||
| 		div.style.border = '1px solid #000'; | ||||
| 		div.style.display = 'block'; | ||||
| 		div.style.position = 'absolute'; | ||||
| 		div.style.minWidth = '16em'; | ||||
| 		div.style.width = '16em'; | ||||
| 		div.style.maxWidth = '16em'; | ||||
| 		div.style.maxHeight = '16em'; | ||||
| 		div.style.overflow = 'scroll'; | ||||
| 		div.style.fontWeight = 'bold'; | ||||
| 		div.style.fontSize = 'xx-large'; | ||||
| 		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'); | ||||
| 		div.appendChild(list); | ||||
|  | ||||
| 		function cleanup() { | ||||
| 			div.parentElement.removeChild(div); | ||||
| 			window.removeEventListener('keydown', key_down); | ||||
| 		} | ||||
|  | ||||
| 		function key_down(event) { | ||||
| 			if (event.key == 'Escape') { | ||||
| 				cleanup(); | ||||
| 			} | ||||
| 		} | ||||
|  | ||||
| 		function refresh() { | ||||
| 			while (list.firstChild) { | ||||
| 				list.removeChild(list.firstChild); | ||||
| 			} | ||||
| 			let search = input.value; | ||||
| 			let any_at_all = false; | ||||
| 			Object.entries(json).forEach(function(row) { | ||||
| 				let header = document.createElement('div'); | ||||
| 				header.appendChild(document.createTextNode(row[0])); | ||||
| @@ -51,28 +72,25 @@ export function picker(callback, anchor) { | ||||
| 					} | ||||
| 					let emoji = document.createElement('span'); | ||||
| 					const k_size = '1.25em'; | ||||
| 					emoji.style.width = k_size; | ||||
| 					emoji.style.maxWidth = k_size; | ||||
| 					emoji.style.minWidth = k_size; | ||||
| 					emoji.style.height = k_size; | ||||
| 					emoji.style.maxHeight = k_size; | ||||
| 					emoji.style.minHeight = k_size; | ||||
| 					emoji.style.display = 'inline-block'; | ||||
| 					emoji.style.overflow = 'hidden'; | ||||
| 					emoji.style.cursor = 'pointer'; | ||||
| 					emoji.onclick = function() { | ||||
| 						callback(entry); | ||||
| 						div.parentElement.removeChild(div); | ||||
| 					} | ||||
| 					emoji.title = entry.name; | ||||
| 					emoji.appendChild(document.createTextNode(entry.emoji)); | ||||
| 					list.appendChild(emoji); | ||||
| 					any = true; | ||||
| 					any_at_all = true; | ||||
| 				} | ||||
| 				if (!any) { | ||||
| 					list.removeChild(header); | ||||
| 				} | ||||
| 			}); | ||||
| 			if (!any_at_all) { | ||||
| 				list.appendChild(document.createTextNode('No matches found.')); | ||||
| 			} | ||||
| 		} | ||||
| 		refresh(); | ||||
| 		input.oninput = refresh; | ||||
| @@ -81,5 +99,7 @@ export function picker(callback, anchor) { | ||||
| 		div.style.top = '50%'; | ||||
| 		div.style.left = '50%'; | ||||
| 		div.style.transform = 'translate(-50%, -50%)'; | ||||
| 		input.focus(); | ||||
| 		window.addEventListener('keydown', key_down); | ||||
| 	}); | ||||
| } | ||||
| @@ -61,7 +61,7 @@ class TfComposeElement extends LitElement { | ||||
| 		preview.innerHTML = tfutils.markdown(text); | ||||
| 	} | ||||
|  | ||||
| 	convert_to_webp(buffer, type) { | ||||
| 	convert_to_format(buffer, type, mime_type) { | ||||
| 		return new Promise(function(resolve, reject) { | ||||
| 			let img = new Image(); | ||||
| 			img.onload = function() { | ||||
| @@ -73,7 +73,7 @@ class TfComposeElement extends LitElement { | ||||
| 				canvas.height = img.height * scale; | ||||
| 				let context = canvas.getContext('2d'); | ||||
| 				context.drawImage(img, 0, 0, canvas.width, canvas.height); | ||||
| 				let data_url = canvas.toDataURL('image/webp'); | ||||
| 				let data_url = canvas.toDataURL(mime_type); | ||||
| 				let result = atob(data_url.split(',')[1]).split('').map(x => x.charCodeAt(0)); | ||||
| 				resolve(result); | ||||
| 			} | ||||
| @@ -92,8 +92,18 @@ class TfComposeElement extends LitElement { | ||||
| 			let buffer = await file.arrayBuffer(); | ||||
| 			let type = file.type; | ||||
| 			if (type.startsWith('image/')) { | ||||
| 				buffer = await self.convert_to_webp(buffer, file.type); | ||||
| 				type = 'image/webp'; | ||||
| 				let best_buffer; | ||||
| 				let best_type; | ||||
| 				for (let format of ['image/png', 'image/jpeg']) { | ||||
| 					let test_buffer = await self.convert_to_format(buffer, file.type, format); | ||||
| 					console.log(format, test_buffer.length); | ||||
| 					if (!best_buffer || test_buffer.length < best_buffer.length) { | ||||
| 						best_buffer = test_buffer; | ||||
| 						best_type = format; | ||||
| 					} | ||||
| 				} | ||||
| 				buffer = best_buffer; | ||||
| 				type = best_type; | ||||
| 			} else { | ||||
| 				buffer = Array.from(new Uint8Array(buffer)); | ||||
| 			} | ||||
|   | ||||
		Reference in New Issue
	
	Block a user