forked from go-gitea/gitea
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace clipboard.js with async clipboard api (go-gitea#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/writeText
- Loading branch information
1 parent
d330144
commit fe9a76a
Showing
5 changed files
with
36 additions
and
88 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,38 @@ | ||
export default async function initClipboard() { | ||
const els = document.querySelectorAll('.clipboard'); | ||
if (!els || !els.length) return; | ||
const selector = '[data-clipboard-target], [data-clipboard-text]'; | ||
|
||
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); | ||
clipboard.on('success', (e) => { | ||
e.clearSelection(); | ||
$(e.trigger).popup('destroy'); | ||
e.trigger.dataset.content = e.trigger.dataset.success; | ||
$(e.trigger).popup('show'); | ||
e.trigger.dataset.content = e.trigger.dataset.original; | ||
}); | ||
export default async function initClipboard() { | ||
for (const btn of document.querySelectorAll(selector) || []) { | ||
btn.addEventListener('click', async () => { | ||
let text; | ||
if (btn.dataset.clipboardText) { | ||
text = btn.dataset.clipboardText; | ||
} else if (btn.dataset.clipboardTarget) { | ||
text = document.querySelector(btn.dataset.clipboardTarget)?.value; | ||
} | ||
if (!text) return; | ||
|
||
clipboard.on('error', (e) => { | ||
$(e.trigger).popup('destroy'); | ||
e.trigger.dataset.content = e.trigger.dataset.error; | ||
$(e.trigger).popup('show'); | ||
e.trigger.dataset.content = e.trigger.dataset.original; | ||
}); | ||
try { | ||
await navigator.clipboard.writeText(text); | ||
onSuccess(btn); | ||
} catch { | ||
onError(btn); | ||
} | ||
}); | ||
} | ||
} |