Skip to content

feat(cdk-experimental/clipboard): add better support for iOS devices, preliminary documentations #16830

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 3 commits into from
Aug 27, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions src/cdk-experimental/clipboard/clipboard.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
**Warning: this component is still experimental. It may have bugs and the API may change at any
time**

The scrolling package provides helpers for working with the system clipboard.

## The `Clipboard` service

The `Clipboard` service copies text to the
user's clipboard. It has two methods, `copy` and `beginCopy`. For cases where
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"For cases where you are copying a relatively small amount of text,"
->
"For relatively short text,"

Is there any quantitative guidance here? A certain number of characters?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know of any - it very much depends on the user's computer specs. We could do some research about this and update the section before release.

you are copying a relatively small amount of text, you can just call `copy` to
place it on the clipboard.

```typescript
class MyCopier {
constructor(private clipboard: Clipboard) {}

copy() {
this.clipboard.copy('Hello clipboard!');
}
}
```

However, for a large amount of text the browser needs time to load it into a
hidden textarea where it can be copied. Just calling `copy` directly may fail in
this case, so you can pre-load the text by calling `beginCopy`. This method
returns a `PendingCopy` object that has a `copy` method to finish copying the
text that was buffered. Please note, if you call `beginCopy` it is up to you to
clean up the `PendingCopy` object by calling `destroy` on it after you are
finished.

```typescript
class MyCopier {
hugeText: string;

constructor(private clipboard: Clipboard) {}

copy() {
const pending = this.clipboard.beginCopy(this.hugeText);
let remainingAttempts = 3;
const attempt = () => {
const result = pending.copy();
if (!result && --remainingAttempts) {
setTimeout(attempt);
} else {
// Remember to destroy when you're done!
pending.destroy();
}
}
setTimeout(attempt);
}
}
```

## The `cdkCopyToClipboard` directive

The `cdkCopyToClipboard` directive can be used to easily add copy-on-click
functionality to an existing element. The directive selector doubles as an
`@Input()` for the text to be copied.

```html
<img src="doge.jpg" alt="Doge" [xapCopyToClipboard]="getDogeSpeakText()">
```
1 change: 1 addition & 0 deletions src/cdk-experimental/clipboard/clipboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ export class PendingCopy {
const currentFocus = document.activeElement;

textarea.select();
textarea.setSelectionRange(0, textarea.value.length);
successful = this._document.execCommand('copy');

if (currentFocus instanceof HTMLElement) {
Expand Down