Skip to content
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

feat(clipboard): allow to write images on web plugin #2523

Merged
merged 1 commit into from
Mar 4, 2020
Merged
Changes from all commits
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
49 changes: 41 additions & 8 deletions core/src/web/clipboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from '../core-plugin-definitions';

declare var navigator: any;
declare var ClipboardItem: any;

export class ClipboardPluginWeb extends WebPlugin implements ClipboardPlugin {
constructor() {
Expand All @@ -22,10 +23,24 @@ export class ClipboardPluginWeb extends WebPlugin implements ClipboardPlugin {
return Promise.reject('Clipboard API not available in this browser');
}

if (options.string !== undefined || options.url) {
Copy link
Contributor

Choose a reason for hiding this comment

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

@jcesarmobile Have you changed this line on purpose? This might have reintroduced issue #2366

Copy link
Member Author

Choose a reason for hiding this comment

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

Oops, sorry, will fix it tomorrow as I’m working in other clipboard changes

Copy link
Member Author

Choose a reason for hiding this comment

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

should be fixed again by #2527

if (options.string || options.url) {
if (!navigator.clipboard.writeText) {
return Promise.reject('Writting to clipboard not supported in this browser');
}
await navigator.clipboard.writeText(options.string !== undefined ? options.string : options.url);
} else if (options.image) {
return Promise.reject('Setting images not supported on the web');
if (!navigator.clipboard.write) {
return Promise.reject('Setting images not supported in this browser');
}
try {
const blob = await (await fetch(options.image)).blob();
const clipboardItemInput = new ClipboardItem({[blob.type] : blob});
await navigator.clipboard.write([clipboardItemInput]);
}catch(err) {
return Promise.reject('Failed to write image');
}
} else {
return Promise.reject('Nothing to write');
}
return Promise.resolve();
}
Expand All @@ -35,17 +50,35 @@ export class ClipboardPluginWeb extends WebPlugin implements ClipboardPlugin {
return Promise.reject('Clipboard API not available in this browser');
}
if (_options.type === 'string' || _options.type === 'url') {
if (!navigator.clipboard.readText) {
return Promise.reject('Reading from clipboard not supported in this browser');
}
const text = await navigator.clipboard.readText();
return Promise.resolve({ value: text});
} else {
const data = await navigator.clipboard.read();
for (const item of data.items) {
if (item.type === 'text/plain') {
return Promise.resolve({ value: item.getAs('text/plain')});
}
if (navigator.clipboard.read) {
const clipboardItems = await navigator.clipboard.read();
const imgBlob = await clipboardItems[0].getType('image/png');
const data = await this._getBlobData(imgBlob);
return Promise.resolve({ value: data});
} else {
return Promise.reject('Reading images not supported in this browser');
}
}
return Promise.reject('Unable to get data from clipboard');
}

private _getBlobData(imgBlob: Blob) {
return new Promise<string>((resolve, reject) => {
var reader = new FileReader();
reader.readAsDataURL(imgBlob);
reader.onloadend = () => {
const r = reader.result as string;
resolve(r);
};
reader.onerror = (e) => {
reject(e);
};
});
}
}

Expand Down