Class ClipboardBinding
- All Implemented Interfaces:
Serializable
Clipboard.onClick(T), used to declare what
a click should write to or read from the clipboard. The write*
methods copy a value (literal text, a field's value, HTML, or an image) to
the clipboard; the read* methods read the clipboard contents back.
The action runs in the browser at click time, while the user gesture required
for clipboard access is still valid.
Write actions come in two flavours: fire-and-forget (one argument) and
observed (with onCopied/onError callbacks). onCopied
receives the string that was copied; onError receives the browser's
error. Both consumers are required in the observed form ? pass
s -> {} or err -> {} to opt out of one.
Read actions always take both an onPayload consumer (receiving the
clipboard contents or null if empty) and an onError consumer
(receiving the browser's error, typically "NotAllowedError" when the
user denied the clipboard-read permission).
Button copy = new Button("Copy");
Clipboard.onClick(copy).writeText(textField);
Clipboard.onClick(copy).writeText(textField,
copied -> Notification.show("Copied " + copied),
err -> Notification.show("Failed: " + err.message()));
Button paste = new Button("Paste");
Clipboard.onClick(paste).readText(
text -> Notification.show("Pasted " + text),
err -> Notification.show("Failed: " + err.message()));
- Since:
- 25.2
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionvoidread(SerializableConsumer<@Nullable ClipboardPayload> onPayload, SerializableConsumer<PromiseAction.Error> onError) Reads the user's clipboard vianavigator.clipboard.read()when the underlying trigger fires and delivers the contents toonPayload, or routes any failure toonError.voidreadHtml(SerializableConsumer<@Nullable String> onHtml, SerializableConsumer<PromiseAction.Error> onError) Likeread(com.vaadin.flow.function.SerializableConsumer<com.vaadin.flow.component.clipboard.ClipboardPayload>, com.vaadin.flow.function.SerializableConsumer<com.vaadin.flow.component.trigger.internal.PromiseAction.Error>)but delivers only thetext/htmlfield of the clipboard payload toonHtml.voidreadText(SerializableConsumer<@Nullable String> onText, SerializableConsumer<PromiseAction.Error> onError) Likeread(com.vaadin.flow.function.SerializableConsumer<com.vaadin.flow.component.clipboard.ClipboardPayload>, com.vaadin.flow.function.SerializableConsumer<com.vaadin.flow.component.trigger.internal.PromiseAction.Error>)but delivers only thetext/plainfield of the clipboard payload toonText.voidwrite(ClipboardContent content) Copies a multi-format payload to the clipboard, packed into a singleClipboardItem.voidwrite(ClipboardContent content, SerializableConsumer<@Nullable String> onCopied, SerializableConsumer<PromiseAction.Error> onError) Likewrite(ClipboardContent)but reports the outcome back to the server.voidCopies a literal HTML string to the clipboard astext/htmlwhen the underlying trigger fires.voidwriteHtml(String literal, SerializableConsumer<@Nullable String> onCopied, SerializableConsumer<PromiseAction.Error> onError) LikewriteHtml(String)but reports the outcome back to the server.voidwriteImage(Component source) Copies the given component's root<img>element to the clipboard asimage/pngwhen the underlying trigger fires.voidwriteImage(Component source, SerializableRunnable onCopied, SerializableConsumer<PromiseAction.Error> onError) LikewriteImage(Component)but reports the outcome back to the server.voidwriteImage(DownloadHandler handler) Copies the image served by the givenDownloadHandlerto the clipboard asimage/pngwhen the underlying trigger fires.voidwriteImage(DownloadHandler handler, SerializableRunnable onCopied, SerializableConsumer<PromiseAction.Error> onError) LikewriteImage(DownloadHandler)but reports the outcome back to the server.writeText(C source) Copies the currentvalueproperty of the given component (typically an input field) to the clipboard astext/plainwhen the underlying trigger fires.writeText(C source, SerializableConsumer<@Nullable String> onCopied, SerializableConsumer<PromiseAction.Error> onError) LikewriteText(Component)but reports the outcome back to the server.voidCopies a literal string to the clipboard astext/plainwhen the underlying trigger fires.voidwriteText(String literal, SerializableConsumer<@Nullable String> onCopied, SerializableConsumer<PromiseAction.Error> onError) LikewriteText(String)but reports the outcome of the write promise back to the server.
-
Method Details
-
writeText
Copies a literal string to the clipboard astext/plainwhen the underlying trigger fires.- Parameters:
literal- the value to copy, notnull
-
writeText
public void writeText(String literal, SerializableConsumer<@Nullable String> onCopied, SerializableConsumer<PromiseAction.Error> onError) LikewriteText(String)but reports the outcome of the write promise back to the server.- Parameters:
literal- the value, notnullonCopied- UI-thread callback receiving the copied string, notnullonError- UI-thread callback receiving the browser's error, notnull
-
writeText
Copies the currentvalueproperty of the given component (typically an input field) to the clipboard astext/plainwhen the underlying trigger fires. The value is read on the client at the moment the trigger fires, so subsequent edits are reflected without any server round-trip.- Type Parameters:
C- component type implementingHasValue<?, String>- Parameters:
source- the component whosevalueshould be copied, notnull
-
writeText
public <C extends Component & HasValue<?,String>> void writeText(C source, SerializableConsumer<@Nullable String> onCopied, SerializableConsumer<PromiseAction.Error> onError) LikewriteText(Component)but reports the outcome back to the server.- Type Parameters:
C- component type implementingHasValue<?, String>- Parameters:
source- the component whosevalueshould be copied, notnullonCopied- UI-thread callback receiving the copied string, notnullonError- UI-thread callback receiving the browser's error, notnull
-
writeHtml
Copies a literal HTML string to the clipboard astext/htmlwhen the underlying trigger fires.- Parameters:
literal- the HTML, notnull
-
writeHtml
public void writeHtml(String literal, SerializableConsumer<@Nullable String> onCopied, SerializableConsumer<PromiseAction.Error> onError) LikewriteHtml(String)but reports the outcome back to the server.- Parameters:
literal- the HTML, notnullonCopied- UI-thread callback receiving the copied HTML, notnullonError- UI-thread callback receiving the browser's error, notnull
-
writeImage
Copies the given component's root<img>element to the clipboard asimage/pngwhen the underlying trigger fires. The image is drawn on a canvas and exported as PNG on the client, so the source can be any rasterisable format (image/png,image/jpeg,image/svg+xml, ...) as long as it has intrinsic dimensions.Cross-origin images need
crossorigin="anonymous"on the<img>plus matching CORS headers; otherwise the canvas is tainted and the write fails. Same-origin anddata:URLs always work.- Parameters:
source- the component whose root<img>should be copied, notnull- Throws:
IllegalArgumentException- if the source's root element is not an<img>
-
writeImage
public void writeImage(Component source, SerializableRunnable onCopied, SerializableConsumer<PromiseAction.Error> onError) LikewriteImage(Component)but reports the outcome back to the server. An image-only write has no string value, so success is reported as a plainSerializableRunnablerather than a value callback.- Parameters:
source- the component whose root<img>should be copied, notnullonCopied- UI-thread callback invoked once the write resolves, notnullonError- UI-thread callback receiving the browser's error, notnull- Throws:
IllegalArgumentException- if the source's root element is not an<img>
-
writeImage
Copies the image served by the givenDownloadHandlerto the clipboard asimage/pngwhen the underlying trigger fires.A hidden
<img>bound to the handler is appended to the trigger host, so the browser begins downloading the image as soon as this method is called ? well before the user can click. At fire time the image is already decoded (or finishes decoding inside the canvas converter'sloadlistener), drawn onto a canvas, and exported as PNG.Cross-origin concerns do not apply because the handler is served by the same origin as the application.
- Parameters:
handler- the download handler producing the image bytes, notnull
-
writeImage
public void writeImage(DownloadHandler handler, SerializableRunnable onCopied, SerializableConsumer<PromiseAction.Error> onError) LikewriteImage(DownloadHandler)but reports the outcome back to the server. An image-only write has no string value, so success is reported as a plainSerializableRunnablerather than a value callback.- Parameters:
handler- the download handler producing the image bytes, notnullonCopied- UI-thread callback invoked once the write resolves, notnullonError- UI-thread callback receiving the browser's error, notnull
-
write
Copies a multi-format payload to the clipboard, packed into a singleClipboardItem.- Parameters:
content- the content, notnull; must have at least one slot set- Throws:
IllegalArgumentException- ifcontenthas no slots set
-
write
public void write(ClipboardContent content, SerializableConsumer<@Nullable String> onCopied, SerializableConsumer<PromiseAction.Error> onError) Likewrite(ClipboardContent)but reports the outcome back to the server.- Parameters:
content- the content, notnull; must have at least one slot setonCopied- UI-thread callback receiving thetext/plainvalue if present, otherwise thetext/htmlvalue, notnullonError- UI-thread callback receiving the browser's error, notnull
-
read
public void read(SerializableConsumer<@Nullable ClipboardPayload> onPayload, SerializableConsumer<PromiseAction.Error> onError) Reads the user's clipboard vianavigator.clipboard.read()when the underlying trigger fires and delivers the contents toonPayload, or routes any failure toonError.The Clipboard API requires the call to happen inside a short-lived user gesture AND the user to grant the
clipboard-readpermission; binding to a click trigger satisfies the gesture, but the browser may still reject the read with"NotAllowedError"when the permission is denied.- Parameters:
onPayload- UI-thread callback receiving the clipboard contents, ornullif the clipboard was empty; notnullonError- UI-thread callback receiving the browser's error, notnull
-
readText
public void readText(SerializableConsumer<@Nullable String> onText, SerializableConsumer<PromiseAction.Error> onError) Likeread(com.vaadin.flow.function.SerializableConsumer<com.vaadin.flow.component.clipboard.ClipboardPayload>, com.vaadin.flow.function.SerializableConsumer<com.vaadin.flow.component.trigger.internal.PromiseAction.Error>)but delivers only thetext/plainfield of the clipboard payload toonText.onTextreceivesnullif the clipboard was empty or had notext/plainrepresentation.- Parameters:
onText- UI-thread callback receiving thetext/plainvalue, ornull; notnullonError- UI-thread callback receiving the browser's error, notnull
-
readHtml
public void readHtml(SerializableConsumer<@Nullable String> onHtml, SerializableConsumer<PromiseAction.Error> onError) Likeread(com.vaadin.flow.function.SerializableConsumer<com.vaadin.flow.component.clipboard.ClipboardPayload>, com.vaadin.flow.function.SerializableConsumer<com.vaadin.flow.component.trigger.internal.PromiseAction.Error>)but delivers only thetext/htmlfield of the clipboard payload toonHtml.onHtmlreceivesnullif the clipboard was empty or had notext/htmlrepresentation.- Parameters:
onHtml- UI-thread callback receiving thetext/htmlvalue, ornull; notnullonError- UI-thread callback receiving the browser's error, notnull
-