Skip to content

Commit

Permalink
WEB-2063 readTextFromClipboard/writeTextToClipboard (#151)
Browse files Browse the repository at this point in the history
  • Loading branch information
atabel authored Oct 7, 2024
1 parent 28acd63 commit 9afff71
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 0 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1237,6 +1237,26 @@ getBatteryInfo: () => Promise<{
information is unavailable.
- `isPowerSafeMode`: true if the device is in power saving mode.
### readTextFromClipboard
<kbd>App version >=24.10</kbd>
Reads the current text in the clipboard
```ts
readTextFromClipboard: () => Promise<string>;
```
### writeTextToClipboard
<kbd>App version >=24.10</kbd>
Writes the given text to the clipboard
```ts
writeTextToClipboard: (text: string) => Promise<void>;
```
## Error handling
If an uncontrolled error occurs, promise will be rejected with an error object:
Expand Down
1 change: 1 addition & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,4 @@ export type {
export {openOnboarding} from './src/open-onboarding';

export {getProfileImage, startProfileImageFlow} from './src/profile';
export {readTextFromClipboard, writeTextToClipboard} from './src/clipboard';
34 changes: 34 additions & 0 deletions src/__tests__/clipboard-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import {readTextFromClipboard, writeTextToClipboard} from '../../index';
import {createFakeWebKitPostMessage} from './fake-post-message';

test('read from clipboard', async () => {
createFakeWebKitPostMessage({
checkMessage: (msg) => {
expect(msg.type).toBe('CLIPBOARD_READ_TEXT');
},
getResponse: (msg) => ({
type: 'CLIPBOARD_READ_TEXT',
payload: 'some text',
id: msg.id,
}),
});

const res = await readTextFromClipboard();

expect(res).toEqual('some text');
});

test('write to clipboard', async () => {
createFakeWebKitPostMessage({
checkMessage: (msg) => {
expect(msg.type).toBe('CLIPBOARD_WRITE_TEXT');
expect(msg.payload).toBe('some text');
},
getResponse: (msg) => ({
type: 'CLIPBOARD_WRITE_TEXT',
id: msg.id,
}),
});

await writeTextToClipboard('some text');
});
7 changes: 7 additions & 0 deletions src/clipboard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import {postMessageToNativeApp} from './post-message';

export const readTextFromClipboard = (): Promise<string> =>
postMessageToNativeApp({type: 'CLIPBOARD_READ_TEXT'});

export const writeTextToClipboard = (text: string): Promise<void> =>
postMessageToNativeApp({type: 'CLIPBOARD_WRITE_TEXT', payload: text});
10 changes: 10 additions & 0 deletions src/post-message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,16 @@ export type ResponsesFromNativeApp = {
isPowerSafeMode: boolean;
};
};
CLIPBOARD_READ_TEXT: {
type: 'CLIPBOARD_READ_TEXT';
id: string;
payload: string;
};
CLIPBOARD_WRITE_TEXT: {
type: 'CLIPBOARD_WRITE_TEXT';
id: string;
payload: void;
};
};

export type NativeAppResponsePayload<
Expand Down

0 comments on commit 9afff71

Please sign in to comment.