| 
									
										
										
										
											2023-01-29 02:39:51 +01:00
										 |  |  | import { useContext } from "@nuxtjs/composition-api"; | 
					
						
							| 
									
										
										
										
											2022-01-16 15:24:24 -09:00
										 |  |  | import { useClipboard } from "@vueuse/core"; | 
					
						
							|  |  |  | import { alert } from "./use-toast"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-28 20:08:33 -08:00
										 |  |  | export function useCopy() { | 
					
						
							|  |  |  |   const { copy, copied, isSupported } = useClipboard(); | 
					
						
							| 
									
										
										
										
											2023-01-29 02:39:51 +01:00
										 |  |  |   const { i18n } = useContext(); | 
					
						
							| 
									
										
										
										
											2022-08-28 20:08:33 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   function copyText(text: string) { | 
					
						
							| 
									
										
										
										
											2023-12-29 16:48:28 +01:00
										 |  |  |     if (!isSupported.value) { | 
					
						
							| 
									
										
										
										
											2023-01-29 02:39:51 +01:00
										 |  |  |       alert.error(i18n.tc("general.clipboard-not-supported")); | 
					
						
							| 
									
										
										
										
											2022-08-28 20:08:33 -08:00
										 |  |  |       return; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-12-29 16:48:28 +01:00
										 |  |  |     copy(text).then(() => { | 
					
						
							|  |  |  |       // Verify copy success as no error is thrown on failure.
 | 
					
						
							|  |  |  |       if (copied.value) { | 
					
						
							|  |  |  |         alert.success(i18n.tc("general.copied-to-clipboard")); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       else { | 
					
						
							|  |  |  |         alert.error(i18n.tc("general.clipboard-copy-failure")); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2022-08-28 20:08:33 -08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return { copyText, copied }; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-16 15:24:24 -09:00
										 |  |  | export function useCopyList() { | 
					
						
							| 
									
										
										
										
											2023-12-29 16:48:28 +01:00
										 |  |  |   const { copy, isSupported, copied } = useClipboard(); | 
					
						
							| 
									
										
										
										
											2023-01-29 02:39:51 +01:00
										 |  |  |   const { i18n } = useContext(); | 
					
						
							| 
									
										
										
										
											2022-01-16 15:24:24 -09:00
										 |  |  | 
 | 
					
						
							|  |  |  |   function checkClipboard() { | 
					
						
							| 
									
										
										
										
											2023-12-29 16:48:28 +01:00
										 |  |  |     if (!isSupported.value) { | 
					
						
							| 
									
										
										
										
											2023-01-29 02:39:51 +01:00
										 |  |  |       alert.error(i18n.tc("general.your-browser-does-not-support-clipboard")); | 
					
						
							| 
									
										
										
										
											2022-01-16 15:24:24 -09:00
										 |  |  |       return false; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return true; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   function copyPlain(list: string[]) { | 
					
						
							|  |  |  |     if (!checkClipboard()) return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const text = list.join("\n"); | 
					
						
							|  |  |  |     copyText(text, list.length); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   function copyMarkdown(list: string[]) { | 
					
						
							|  |  |  |     if (!checkClipboard()) return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const text = list.map((item) => `- ${item}`).join("\n"); | 
					
						
							|  |  |  |     copyText(text, list.length); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   function copyMarkdownCheckList(list: string[]) { | 
					
						
							|  |  |  |     if (!checkClipboard()) return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const text = list.map((item) => `- [ ] ${item}`).join("\n"); | 
					
						
							|  |  |  |     copyText(text, list.length); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   function copyText(text: string, len: number) { | 
					
						
							|  |  |  |     copy(text).then(() => { | 
					
						
							| 
									
										
										
										
											2023-12-29 16:48:28 +01:00
										 |  |  |       // Verify copy success as no error is thrown on failure.
 | 
					
						
							|  |  |  |       if (copied.value) { | 
					
						
							|  |  |  |         alert.success(i18n.tc("general.copied-items-to-clipboard", len)); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       else { | 
					
						
							|  |  |  |         alert.error(i18n.tc("general.clipboard-copy-failure")); | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2022-01-16 15:24:24 -09:00
										 |  |  |     }); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return { | 
					
						
							|  |  |  |     copyPlain, | 
					
						
							|  |  |  |     copyMarkdown, | 
					
						
							|  |  |  |     copyMarkdownCheckList, | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | } |