Replace clipboard.js with async clipboard api (#15899)
	
		
	
				
					
				
			Use async clipboard api [1] over this dependency, saving around 10kB bundle size before minify while delivering the same functionality. The issue comment button works but does not have a popup indication. We could add some toast-style notifications in the future to fix that but I think it's out of scope of this PR. [1] https://developer.mozilla.org/en-US/docs/Web/API/Clipboard/writeTexttokarchuk/v1.17
							parent
							
								
									36dce0e457
								
							
						
					
					
						commit
						37205039fc
					
				@ -1,22 +1,38 @@ | 
				
			|||||||
export default async function initClipboard() { | 
					const selector = '[data-clipboard-target], [data-clipboard-text]'; | 
				
			||||||
  const els = document.querySelectorAll('.clipboard'); | 
					 | 
				
			||||||
  if (!els || !els.length) return; | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
  const {default: ClipboardJS} = await import(/* webpackChunkName: "clipboard" */'clipboard'); | 
					// TODO: replace these with toast-style notifications
 | 
				
			||||||
 | 
					function onSuccess(btn) { | 
				
			||||||
 | 
					  if (!btn.dataset.content) return; | 
				
			||||||
 | 
					  $(btn).popup('destroy'); | 
				
			||||||
 | 
					  btn.dataset.content = btn.dataset.success; | 
				
			||||||
 | 
					  $(btn).popup('show'); | 
				
			||||||
 | 
					  btn.dataset.content = btn.dataset.original; | 
				
			||||||
 | 
					} | 
				
			||||||
 | 
					function onError(btn) { | 
				
			||||||
 | 
					  if (!btn.dataset.content) return; | 
				
			||||||
 | 
					  $(btn).popup('destroy'); | 
				
			||||||
 | 
					  btn.dataset.content = btn.dataset.error; | 
				
			||||||
 | 
					  $(btn).popup('show'); | 
				
			||||||
 | 
					  btn.dataset.content = btn.dataset.original; | 
				
			||||||
 | 
					} | 
				
			||||||
 | 
					
 | 
				
			||||||
  const clipboard = new ClipboardJS(els); | 
					export default async function initClipboard() { | 
				
			||||||
  clipboard.on('success', (e) => { | 
					  for (const btn of document.querySelectorAll(selector) || []) { | 
				
			||||||
    e.clearSelection(); | 
					    btn.addEventListener('click', async () => { | 
				
			||||||
    $(e.trigger).popup('destroy'); | 
					      let text; | 
				
			||||||
    e.trigger.dataset.content = e.trigger.dataset.success; | 
					      if (btn.dataset.clipboardText) { | 
				
			||||||
    $(e.trigger).popup('show'); | 
					        text = btn.dataset.clipboardText; | 
				
			||||||
    e.trigger.dataset.content = e.trigger.dataset.original; | 
					      } else if (btn.dataset.clipboardTarget) { | 
				
			||||||
  }); | 
					        text = document.querySelector(btn.dataset.clipboardTarget)?.value; | 
				
			||||||
 | 
					      } | 
				
			||||||
 | 
					      if (!text) return; | 
				
			||||||
 | 
					
 | 
				
			||||||
  clipboard.on('error', (e) => { | 
					      try { | 
				
			||||||
    $(e.trigger).popup('destroy'); | 
					        await navigator.clipboard.writeText(text); | 
				
			||||||
    e.trigger.dataset.content = e.trigger.dataset.error; | 
					        onSuccess(btn); | 
				
			||||||
    $(e.trigger).popup('show'); | 
					      } catch { | 
				
			||||||
    e.trigger.dataset.content = e.trigger.dataset.original; | 
					        onError(btn); | 
				
			||||||
 | 
					      } | 
				
			||||||
    }); | 
					    }); | 
				
			||||||
  } | 
					  } | 
				
			||||||
 | 
					} | 
				
			||||||
 | 
				
			|||||||
					Loading…
					
					
				
		Reference in new issue