Skip to content

Commit

Permalink
fix copy for latest safari
Browse files Browse the repository at this point in the history
  • Loading branch information
akellbl4 committed Apr 12, 2022
1 parent 5de9544 commit c2e15a3
Show file tree
Hide file tree
Showing 7 changed files with 267 additions and 19,107 deletions.
25 changes: 25 additions & 0 deletions frontend/app/common/copy.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { doc } from 'prettier';
import { copy } from './copy';

describe('copy to clipboard', () => {
it('should call `execCommand` for old browser', async () => {
document.execCommand = jest.fn(() => true);

await copy('text');
expect(document.execCommand).toHaveBeenCalledTimes(1);
});

it('should call `clipboard.write` for new browser', async () => {
const clipboardWrite = jest.fn(() => Promise.resolve());
window.ClipboardItem = jest.fn();

Object.defineProperty(navigator, 'clipboard', {
value: {
write: clipboardWrite,
},
});

await copy('text');
expect(clipboardWrite).toHaveBeenCalledTimes(1);
});
});
14 changes: 9 additions & 5 deletions frontend/app/common/copy.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
export function copy(content: string): boolean {
export async function copy(content: string): Promise<void> {
// Use new API for modern browsers
if ('clipboard' in window.navigator) {
const blob = new Blob([content], { type: 'text/plain' });
return navigator.clipboard.write([new ClipboardItem({ [blob.type]: blob })]);
}

// We use `div` instead of `input` or `textarea` because we want to copy styles
const container = document.createElement('div');
const previouslyFocusedElement = document.activeElement as HTMLElement;
Expand All @@ -14,7 +20,7 @@ export function copy(content: string): boolean {

document.body.appendChild(container);

let selection = window.getSelection();
const selection = window.getSelection();
// save original selection
const originalRange = selection && selection.rangeCount > 0 ? selection.getRangeAt(0) : null;
const range = document.createRange();
Expand All @@ -26,8 +32,6 @@ export function copy(content: string): boolean {
selection.addRange(range);
}

document.execCommand('copy');

let success = false;
try {
success = document.execCommand('copy');
Expand All @@ -49,5 +53,5 @@ export function copy(content: string): boolean {
previouslyFocusedElement.focus();
}

return success;
return success ? Promise.resolve() : Promise.reject();
}
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ export const SubscribeByEmailForm: FunctionComponent = () => {
break;
}
} catch (e) {
// @ts-ignore
setError(extractErrorMessageFromResponse(e, intl));
} finally {
setLoading(false);
Expand Down Expand Up @@ -213,6 +214,7 @@ export const SubscribeByEmailForm: FunctionComponent = () => {
previousStep.current = Step.Subscribed;
setStep(Step.Unsubscribed);
} catch (e) {
// @ts-ignore
setError(extractErrorMessageFromResponse(e, intl));
} finally {
setLoading(false);
Expand Down
1 change: 1 addition & 0 deletions frontend/app/components/comment-form/comment-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ export class CommentForm extends Component<CommentFormProps, CommentFormState> {
this.setState({
isDisabled: false,
isErrorShown: true,
// @ts-ignore
errorMessage: extractErrorMessageFromResponse(e, this.props.intl),
});
return;
Expand Down
12 changes: 8 additions & 4 deletions frontend/app/components/comment/comment.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -308,12 +308,16 @@ export class Comment extends Component<CommentProps, State> {
parentCommentNode.scrollIntoView();
};

copyComment = () => {
const username = this.props.data.user.name;
copyComment = async () => {
const { name } = this.props.data.user;
const time = getLocalDatetime(this.props.intl, new Date(this.props.data.time));
const text = this.textNode.current?.textContent || '';
const text = this.textNode.current?.textContent ?? '';

copy(`<b>${username}</b>&nbsp;${time}<br>${text.replace(/\n+/g, '<br>')}`);
try {
await copy(`${name} ${time}\n${text}`);
} catch (e) {
console.log(e);
}

this.setState({ isCopied: true }, () => {
setTimeout(() => this.setState({ isCopied: false }), 3000);
Expand Down
Loading

0 comments on commit c2e15a3

Please sign in to comment.