一些检查失败了
automerge / automerge (push) Has been cancelled
react-native-android-build-apk / AssembleRelease (push) Has been cancelled
Build macOS M1 / Main (push) Has been cancelled
Joplin Continuous Integration / Main (macos-15-intel) (push) Has been cancelled
Joplin Continuous Integration / Main (ubuntu-22.04) (push) Has been cancelled
Joplin Continuous Integration / Main (ubuntu-22.04-arm) (push) Has been cancelled
Joplin Continuous Integration / Main (windows-11-vs2026-arm) (push) Has been cancelled
Joplin Continuous Integration / Main (windows-2025) (push) Has been cancelled
Joplin Continuous Integration / ServerDockerImage (ubuntu-22.04) (push) Has been cancelled
Joplin Continuous Integration / ServerDockerImage (ubuntu-22.04-arm) (push) Has been cancelled
Joplin UI tests / Main (ubuntu-22.04) (push) Has been cancelled
Joplin UI tests / Main (windows-2025) (push) Has been cancelled
Joplin UI tests / Mobile (push) Has been cancelled
Close stale issues / ProcessStaleIssues (push) Has been cancelled
112 行
3.1 KiB
TypeScript
112 行
3.1 KiB
TypeScript
/* eslint-disable multiline-comment-style */
|
|
|
|
import { ClipboardContent } from './types';
|
|
|
|
interface ElectronClipboardLike {
|
|
readText(): string;
|
|
writeText(text: string): void;
|
|
readHTML(): string;
|
|
writeHTML(html: string): void;
|
|
readImage(): { toDataURL(): string } | null;
|
|
writeImage(image: unknown): void;
|
|
availableFormats(): string[];
|
|
write(data: Record<string, unknown>): void;
|
|
}
|
|
|
|
interface ElectronNativeImageLike {
|
|
createFromDataURL(dataUrl: string): unknown;
|
|
}
|
|
|
|
export default class JoplinClipboard {
|
|
|
|
private electronClipboard_: ElectronClipboardLike = null;
|
|
private electronNativeImage_: ElectronNativeImageLike = null;
|
|
|
|
public constructor(electronClipboard: ElectronClipboardLike, electronNativeImage: ElectronNativeImageLike) {
|
|
this.electronClipboard_ = electronClipboard;
|
|
this.electronNativeImage_ = electronNativeImage;
|
|
}
|
|
|
|
public async readText(): Promise<string> {
|
|
return this.electronClipboard_.readText();
|
|
}
|
|
|
|
public async writeText(text: string): Promise<void> {
|
|
this.electronClipboard_.writeText(text);
|
|
}
|
|
|
|
/** <span class="platform-desktop">desktop</span> */
|
|
public async readHtml(): Promise<string> {
|
|
return this.electronClipboard_.readHTML();
|
|
}
|
|
|
|
/** <span class="platform-desktop">desktop</span> */
|
|
public async writeHtml(html: string): Promise<void> {
|
|
this.electronClipboard_.writeHTML(html);
|
|
}
|
|
|
|
/**
|
|
* Returns the image in [data URL](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs) format.
|
|
*
|
|
* <span class="platform-desktop">desktop</span>
|
|
*/
|
|
public async readImage(): Promise<string> {
|
|
const image = this.electronClipboard_.readImage();
|
|
return image ? image.toDataURL() : '';
|
|
}
|
|
|
|
/**
|
|
* Takes an image in [data URL](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs) format.
|
|
*
|
|
* <span class="platform-desktop">desktop</span>
|
|
*/
|
|
public async writeImage(dataUrl: string): Promise<void> {
|
|
this.electronClipboard_.writeImage(this.electronNativeImage_.createFromDataURL(dataUrl));
|
|
}
|
|
|
|
/**
|
|
* Returns the list available formats (mime types).
|
|
*
|
|
* For example [ 'text/plain', 'text/html' ]
|
|
*/
|
|
public async availableFormats(): Promise<string[]> {
|
|
return this.electronClipboard_.availableFormats();
|
|
}
|
|
|
|
/**
|
|
* Writes multiple formats to the clipboard simultaneously.
|
|
* This allows setting both text/plain and text/html at the same time.
|
|
*
|
|
* <span class="platform-desktop">desktop</span>
|
|
*
|
|
* @example
|
|
* ```typescript
|
|
* await joplin.clipboard.write({
|
|
* text: 'Plain text version',
|
|
* html: '<strong>HTML version</strong>'
|
|
* });
|
|
* ```
|
|
*/
|
|
public async write(content: ClipboardContent): Promise<void> {
|
|
const clipboardData: Record<string, unknown> = {};
|
|
|
|
if (content.text !== undefined) {
|
|
clipboardData.text = content.text;
|
|
}
|
|
|
|
if (content.html !== undefined) {
|
|
clipboardData.html = content.html;
|
|
}
|
|
|
|
if (content.image !== undefined) {
|
|
clipboardData.image = this.electronNativeImage_.createFromDataURL(content.image);
|
|
}
|
|
|
|
// Only write to clipboard if there's actually data to write
|
|
if (Object.keys(clipboardData).length > 0) {
|
|
this.electronClipboard_.write(clipboardData);
|
|
}
|
|
}
|
|
|
|
}
|