Browse Source

refac/enh: channel input paste behaviour

Timothy Jaeryang Baek 3 weeks ago
parent
commit
6549fc839f
1 changed files with 31 additions and 1 deletions
  1. 31 1
      src/lib/components/channel/MessageInput.svelte

+ 31 - 1
src/lib/components/channel/MessageInput.svelte

@@ -888,7 +888,37 @@
 											}}
 											}}
 											on:paste={async (e) => {
 											on:paste={async (e) => {
 												e = e.detail.event;
 												e = e.detail.event;
-												console.info(e);
+												console.log(e);
+
+												const clipboardData = e.clipboardData || window.clipboardData;
+
+												if (clipboardData && clipboardData.items) {
+													for (const item of clipboardData.items) {
+														if (item.type.indexOf('image') !== -1) {
+															const blob = item.getAsFile();
+															const reader = new FileReader();
+
+															reader.onload = function (e) {
+																files = [
+																	...files,
+																	{
+																		type: 'image',
+																		url: `${e.target.result}`
+																	}
+																];
+															};
+
+															reader.readAsDataURL(blob);
+														} else if (item?.kind === 'file') {
+															const file = item.getAsFile();
+															if (file) {
+																const _files = [file];
+																await inputFilesHandler(_files);
+																e.preventDefault();
+															}
+														}
+													}
+												}
 											}}
 											}}
 										/>
 										/>
 									{/key}
 									{/key}