-
Notifications
You must be signed in to change notification settings - Fork 25
fix: copying created links in Safari #1594
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
2 commits
Select commit
Hold shift + click to select a range
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
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| export * from './useCopyLink' | ||
| export * from './useLinkTypes' |
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,104 @@ | ||
| import { useMessages } from '../piniaStores' | ||
| import { useGettext } from 'vue3-gettext' | ||
| import { LinkShare } from '@opencloud-eu/web-client' | ||
| import { useClipboard } from '@vueuse/core' | ||
|
|
||
| /** | ||
| * Dedicated composable for copying created links to clipboard because it requires | ||
| * special handling for Safari. For this to work you need to pass the link create method | ||
| * to copyLink so that the link is created within the same user interaction as the clipboard write. | ||
| * | ||
| * This composable also takes care of showing success and error messages. | ||
| */ | ||
| export const useCopyLink = () => { | ||
| const { $gettext, $ngettext } = useGettext() | ||
| const { showMessage, showErrorMessage } = useMessages() | ||
| const { copy } = useClipboard() | ||
|
|
||
| const getTextToCopy = ({ | ||
| result, | ||
| password | ||
| }: { | ||
| result: PromiseSettledResult<LinkShare>[] | ||
| password?: string | ||
| }) => { | ||
| const succeeded = result.filter( | ||
| (val): val is PromiseFulfilledResult<LinkShare> => val.status === 'fulfilled' | ||
| ) | ||
|
|
||
| let copyToClipboardText = '' | ||
| if (succeeded.length) { | ||
| let successMessage = $gettext('Link has been created successfully') | ||
|
|
||
| if (result.length === 1) { | ||
| // Only copy to clipboard if the user tries to create one single link | ||
| try { | ||
| copyToClipboardText = password | ||
| ? $gettext( | ||
| '%{link} Password:%{password}', | ||
| { link: succeeded[0].value.webUrl, password }, | ||
| true | ||
| ) | ||
| : succeeded[0].value.webUrl | ||
|
|
||
| successMessage = $gettext('The link has been copied to your clipboard.') | ||
| } catch (e) { | ||
| console.warn('Unable to copy link to clipboard', e) | ||
| } | ||
| } | ||
|
|
||
| showMessage({ | ||
| title: $ngettext(successMessage, 'Links have been created successfully.', succeeded.length) | ||
| }) | ||
| } | ||
|
|
||
| const failed = result.filter(({ status }) => status === 'rejected') | ||
| if (failed.length) { | ||
| showErrorMessage({ | ||
| errors: (failed as PromiseRejectedResult[]).map(({ reason }) => reason), | ||
| title: $ngettext('Failed to create link', 'Failed to create links', failed.length) | ||
| }) | ||
| } | ||
|
|
||
| return copyToClipboardText | ||
| } | ||
|
|
||
| const copyLink = async ({ | ||
| createLinkHandler, | ||
| password | ||
| }: { | ||
| createLinkHandler: () => Promise<PromiseSettledResult<LinkShare>[]> | ||
| password?: string | ||
| }) => { | ||
| // special handling for Safari because it doesn't allow async clipboard writes. works in most other browsers as well. | ||
| // see https://wolfgangrittner.dev/how-to-use-clipboard-api-in-safari/ and https://developer.apple.com/forums/thread/691873 | ||
| if (typeof ClipboardItem && navigator.clipboard.write) { | ||
| await new Promise<void>((resolve, reject) => { | ||
| const text = new ClipboardItem({ | ||
| 'text/plain': createLinkHandler() | ||
| .then((result) => { | ||
| const textToCopy = getTextToCopy({ result, password }) | ||
| const blob = new Blob([textToCopy], { type: 'text/plain' }) | ||
| resolve() | ||
| return blob | ||
| }) | ||
| .catch((error) => { | ||
| reject() | ||
| throw error | ||
| }) | ||
| }) | ||
|
|
||
| navigator.clipboard.write([text]) | ||
| }) | ||
| } else { | ||
| // edge case for browsers that don't support ClipboardItem (e.g. Firefox) | ||
| const result = await createLinkHandler() | ||
| const textToCopy = getTextToCopy({ result, password }) | ||
| if (textToCopy) { | ||
| copy(textToCopy) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return { copyLink } | ||
| } |
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
Oops, something went wrong.
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.