-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Improvements for Content Copy #21842
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
914399b
Improvements for Content Copy
silverwind 5eeabcf
check for blob
silverwind bd43820
move comment
silverwind a6bf4ff
Update templates/repo/view_file.tmpl
silverwind a16485b
fix is-loading check
silverwind 6ff315e
Update web_src/js/features/copycontent.js
silverwind 76992e9
Update indirect eslint dependencies to allow ClipboardItem global
silverwind 307c955
Revert "Update indirect eslint dependencies to allow ClipboardItem gl…
silverwind 348cbb5
add disabled tooltip and fallback image convertion to png
silverwind df1d150
Fix typo
silverwind babd642
tweak comment
silverwind 617988c
add test
silverwind a4c2ab8
Update web_src/js/features/clipboard.js
silverwind cbc80cb
Update web_src/js/features/copycontent.js
silverwind e43a837
handle canvas.toBlob failure
silverwind ccb0820
rename translation key
silverwind cf710eb
make convertImage more flexible
silverwind ff8cd67
add another safeguard catch handler
silverwind File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import {copyToClipboard} from './clipboard.js'; | ||
import {showTemporaryTooltip} from '../modules/tippy.js'; | ||
import {convertImage} from '../utils.js'; | ||
const {i18n} = window.config; | ||
|
||
async function doCopy(content, btn) { | ||
const success = await copyToClipboard(content); | ||
showTemporaryTooltip(btn, success ? i18n.copy_success : i18n.copy_error); | ||
} | ||
|
||
export function initCopyContent() { | ||
const btn = document.getElementById('copy-content'); | ||
if (!btn || btn.classList.contains('disabled')) return; | ||
delvh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
btn.addEventListener('click', async () => { | ||
if (btn.classList.contains('is-loading')) return; | ||
let content, isImage; | ||
const link = btn.getAttribute('data-link'); | ||
|
||
// when data-link is present, we perform a fetch. this is either because | ||
// the text to copy is not in the DOM or it is an image which should be | ||
// fetched to copy in full resolution | ||
if (link) { | ||
btn.classList.add('is-loading'); | ||
try { | ||
const res = await fetch(link, {credentials: 'include', redirect: 'follow'}); | ||
const contentType = res.headers.get('content-type'); | ||
|
||
if (contentType.startsWith('image/') && !contentType.startsWith('image/svg')) { | ||
isImage = true; | ||
content = await res.blob(); | ||
} else { | ||
content = await res.text(); | ||
} | ||
} catch { | ||
return showTemporaryTooltip(btn, i18n.copy_error); | ||
} finally { | ||
btn.classList.remove('is-loading'); | ||
} | ||
} else { // text, read from DOM | ||
const lineEls = document.querySelectorAll('.file-view .lines-code'); | ||
content = Array.from(lineEls).map((el) => el.textContent).join(''); | ||
} | ||
|
||
try { | ||
await doCopy(content, btn); | ||
} catch { | ||
if (isImage) { // convert image to png as last-resort as some browser only support png copy | ||
try { | ||
await doCopy(await convertImage(content, 'image/png'), btn); | ||
} catch { | ||
showTemporaryTooltip(btn, i18n.copy_error); | ||
} | ||
} else { | ||
showTemporaryTooltip(btn, i18n.copy_error); | ||
} | ||
} | ||
}); | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.