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: Add clipboard write capability to extension framework #603

Merged
merged 1 commit into from
Apr 20, 2021
Merged
Show file tree
Hide file tree
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
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') => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Was this a very old fix-up you needed to check in?

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