Skip to content

Commit

Permalink
feat: Add clipboard write capability to extension framework (#603)
Browse files Browse the repository at this point in the history
  • Loading branch information
bryans99 authored Apr 20, 2021
1 parent 214b6a9 commit d6b52ea
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 2 deletions.
9 changes: 9 additions & 0 deletions packages/extension-sdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,15 @@ const myDataObj = JSON.parse(value)
await extensionSDK.localStorageRemoveItem('data')
```

### Clipboard

** Looker >=21.8 **. Extensions may write to the system clipboard. For security reasons, the extension
is not given read access to the clipboard.

```ts
extensionSDK.clipboardWrite('Hello Clipboard')
```

### User Attributes

Extensions can read Looker provided user attributes, and can define, read and modify their own user attributes. For extension scoped user attributes the key should be namespaced as follows:
Expand Down
14 changes: 12 additions & 2 deletions packages/extension-sdk/src/connect/extension_host_api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ describe('extension_host_api tests', () => {
console.error = originalConsoleError
})

const createHostApi = (initMessage = {}) => {
const createHostApi = (initMessage = {}, lookerVersion = '6.25.0') => {
const initializedCallback = jest.fn()
const hostApi = new ExtensionHostApiImpl({
chattyHost,
Expand All @@ -73,7 +73,7 @@ describe('extension_host_api tests', () => {
const resp = hostApi.handleNotification({
type: ExtensionNotificationType.INITIALIZE,
payload: {
lookerVersion: '6.25.0',
lookerVersion,
extensionId: 'ks::ks',
route: '/sandbox',
hostUrl: 'https://self-signed.looker.com:9999',
Expand Down Expand Up @@ -354,6 +354,16 @@ describe('extension_host_api tests', () => {
done()
})

it('writes to clipboard', async (done) => {
const hostApi = createHostApi({}, '21.7.0')
await hostApi.clipboardWrite('ABCD')
expect(sendAndReceiveSpy).toHaveBeenCalledWith('EXTENSION_API_REQUEST', {
payload: { type: 'write', value: 'ABCD' },
type: 'CLIPBOARD',
})
done()
})

it('tracks an action', () => {
const hostApi = createHostApi()
hostApi.track('MY_TRACK_NAME', 'MY_TRACK_ACTION', { a: 'a', b: 'bb' })
Expand Down
11 changes: 11 additions & 0 deletions packages/extension-sdk/src/connect/extension_host_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,17 @@ export class ExtensionHostApiImpl implements ExtensionHostApi {
})
}

async clipboardWrite(value: string): Promise<void> {
const errorMessage = this.verifyLookerVersion('>=21.7')
if (errorMessage) {
return Promise.reject(new Error(errorMessage))
}
return this.sendAndReceive(ExtensionRequestType.CLIPBOARD, {
type: 'write',
value,
})
}

async userAttributeSetItem(name: string, value = ''): Promise<boolean> {
// User attributes added in Looker version 7.13, updated in 7.15
const errorMessage = this.verifyLookerVersion('>=7.15')
Expand Down
15 changes: 15 additions & 0 deletions packages/extension-sdk/src/connect/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ export enum ExtensionRequestType {
* Close popovers in the looker host
*/
CLOSE_HOST_POPOVERS = 'CLOSE_HOST_POPOVERS',
/**
* Clipboard request
*/
CLIPBOARD = 'CLIPBOARD',
/**
* Local storage request
*/
Expand Down Expand Up @@ -185,6 +189,11 @@ export interface RouteChangeRequest {
route: string
}

export interface ClipboardRequest {
type: 'write'
value: string
}

export interface LocalStorageRequest {
type: 'get' | 'set' | 'remove'
name: string
Expand Down Expand Up @@ -481,6 +490,12 @@ export interface ExtensionSDK {
*/
localStorageGetItem(name: string): Promise<string | null>

/**
* Write string to clipboard.
* @param value to write to clipboard.
*/
clipboardWrite(value: string): Promise<void>

/**
* Set a user attribute value.
* @param name of item
Expand Down

0 comments on commit d6b52ea

Please sign in to comment.