Skip to content

Commit

Permalink
Update Canvas usage of Storage from kibana-utils (elastic#55595) (ela…
Browse files Browse the repository at this point in the history
…stic#58278)

Co-authored-by: Elastic Machine <[email protected]>

Co-authored-by: Elastic Machine <[email protected]>
  • Loading branch information
Corey Robertson and elasticmachine authored Mar 9, 2020
1 parent ab6bfd9 commit 1ba3f89
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 40 deletions.
2 changes: 0 additions & 2 deletions x-pack/legacy/plugins/canvas/public/legacy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { CanvasStartDeps } from './plugin'; // eslint-disable-line import/order
// @ts-ignore Untyped Kibana Lib
import chrome, { loadingCount } from 'ui/chrome'; // eslint-disable-line import/order
import { absoluteToParsedUrl } from 'ui/url/absolute_to_parsed_url'; // eslint-disable-line import/order
import { Storage } from '../../../../../src/plugins/kibana_utils/public'; // eslint-disable-line import/order
// @ts-ignore Untyped Kibana Lib
import { formatMsg } from '../../../../../src/plugins/kibana_legacy/public'; // eslint-disable-line import/order

Expand All @@ -31,7 +30,6 @@ const shimStartPlugins: CanvasStartDeps = {
absoluteToParsedUrl,
// ToDo: Copy directly into canvas
formatMsg,
storage: Storage,
// ToDo: Won't be a part of New Platform. Will need to handle internally
trackSubUrlForApp: chrome.trackSubUrlForApp,
},
Expand Down
16 changes: 0 additions & 16 deletions x-pack/legacy/plugins/canvas/public/lib/__tests__/clipboard.js

This file was deleted.

17 changes: 0 additions & 17 deletions x-pack/legacy/plugins/canvas/public/lib/clipboard.js

This file was deleted.

36 changes: 36 additions & 0 deletions x-pack/legacy/plugins/canvas/public/lib/clipboard.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
jest.mock('../../../../../../src/plugins/kibana_utils/public');

import { Storage } from '../../../../../../src/plugins/kibana_utils/public';
import { setClipboardData, getClipboardData } from './clipboard';
import { LOCALSTORAGE_CLIPBOARD } from '../../common/lib/constants';
import { elements } from '../../__tests__/fixtures/workpads';

const set = jest.fn();
const get = jest.fn();

describe('clipboard', () => {
beforeAll(() => {
// @ts-ignore
Storage.mockImplementation(() => ({
set,
get,
}));
});

test('stores data to local storage', () => {
setClipboardData(elements);

expect(set).toBeCalledWith(LOCALSTORAGE_CLIPBOARD, JSON.stringify(elements));
});

test('gets data from local storage', () => {
getClipboardData();

expect(get).toBeCalledWith(LOCALSTORAGE_CLIPBOARD);
});
});
25 changes: 25 additions & 0 deletions x-pack/legacy/plugins/canvas/public/lib/clipboard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { Storage } from '../../../../../../src/plugins/kibana_utils/public';
import { LOCALSTORAGE_CLIPBOARD } from '../../common/lib/constants';
import { getWindow } from './get_window';

let storage: Storage;

const getStorage = (): Storage => {
if (!storage) {
storage = new Storage(getWindow().localStorage);
}

return storage;
};

export const setClipboardData = (data: any) => {
getStorage().set(LOCALSTORAGE_CLIPBOARD, JSON.stringify(data));
};

export const getClipboardData = () => getStorage().get(LOCALSTORAGE_CLIPBOARD);
6 changes: 4 additions & 2 deletions x-pack/legacy/plugins/canvas/public/lib/get_window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
*/

// return window if it exists, otherwise just return an object literal
const windowObj = { location: null };
const windowObj = { location: null, localStorage: {} as Window['localStorage'] };

export const getWindow = (): Window | { location: Location | null } => {
export const getWindow = ():
| Window
| { location: Location | null; localStorage: Window['localStorage'] } => {
return typeof window === 'undefined' ? windowObj : window;
};
3 changes: 0 additions & 3 deletions x-pack/legacy/plugins/canvas/public/plugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import React from 'react';
import ReactDOM from 'react-dom';
import { Chrome } from 'ui/chrome';
import { i18n } from '@kbn/i18n';
import { Storage } from '../../../../../src/plugins/kibana_utils/public';
import { CoreSetup, CoreStart, Plugin } from '../../../../../src/core/public';
import { HomePublicPluginSetup } from '../../../../../src/plugins/home/public';
// @ts-ignore: Untyped Local
Expand Down Expand Up @@ -43,7 +42,6 @@ export interface CanvasStartDeps {
__LEGACY: {
absoluteToParsedUrl: (url: string, basePath: string) => any;
formatMsg: any;
storage: typeof Storage;
trackSubUrlForApp: Chrome['trackSubUrlForApp'];
};
}
Expand Down Expand Up @@ -92,7 +90,6 @@ export class CanvasPlugin
loadExpressionTypes();
loadTransitions();

initClipboard(plugins.__LEGACY.storage);
initLoadingIndicator(core.http.addLoadingCountSource);

core.chrome.setBadge(
Expand Down

0 comments on commit 1ba3f89

Please sign in to comment.