diff --git a/x-pack/legacy/plugins/canvas/public/legacy.ts b/x-pack/legacy/plugins/canvas/public/legacy.ts index ea873e6f2296d..0d2e77637f19d 100644 --- a/x-pack/legacy/plugins/canvas/public/legacy.ts +++ b/x-pack/legacy/plugins/canvas/public/legacy.ts @@ -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 @@ -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, }, diff --git a/x-pack/legacy/plugins/canvas/public/lib/__tests__/clipboard.js b/x-pack/legacy/plugins/canvas/public/lib/__tests__/clipboard.js deleted file mode 100644 index c616a76d0dcc3..0000000000000 --- a/x-pack/legacy/plugins/canvas/public/lib/__tests__/clipboard.js +++ /dev/null @@ -1,16 +0,0 @@ -/* - * 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 expect from '@kbn/expect'; -import { setClipboardData, getClipboardData } from '../clipboard'; -import { elements } from '../../../__tests__/fixtures/workpads'; - -describe('clipboard', () => { - it('stores and retrieves clipboard data', () => { - setClipboardData(elements); - expect(getClipboardData()).to.eql(JSON.stringify(elements)); - }); -}); diff --git a/x-pack/legacy/plugins/canvas/public/lib/clipboard.js b/x-pack/legacy/plugins/canvas/public/lib/clipboard.js deleted file mode 100644 index 1fd14f086c949..0000000000000 --- a/x-pack/legacy/plugins/canvas/public/lib/clipboard.js +++ /dev/null @@ -1,17 +0,0 @@ -/* - * 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 { LOCALSTORAGE_CLIPBOARD } from '../../common/lib/constants'; -import { getWindow } from './get_window'; - -let storage = null; - -export const initClipboard = function(Storage) { - storage = new Storage(getWindow().localStorage); -}; - -export const setClipboardData = data => storage.set(LOCALSTORAGE_CLIPBOARD, JSON.stringify(data)); -export const getClipboardData = () => storage.get(LOCALSTORAGE_CLIPBOARD); diff --git a/x-pack/legacy/plugins/canvas/public/lib/clipboard.test.ts b/x-pack/legacy/plugins/canvas/public/lib/clipboard.test.ts new file mode 100644 index 0000000000000..54c3000dae36c --- /dev/null +++ b/x-pack/legacy/plugins/canvas/public/lib/clipboard.test.ts @@ -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); + }); +}); diff --git a/x-pack/legacy/plugins/canvas/public/lib/clipboard.ts b/x-pack/legacy/plugins/canvas/public/lib/clipboard.ts new file mode 100644 index 0000000000000..50c5cdd0042fd --- /dev/null +++ b/x-pack/legacy/plugins/canvas/public/lib/clipboard.ts @@ -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); diff --git a/x-pack/legacy/plugins/canvas/public/lib/get_window.ts b/x-pack/legacy/plugins/canvas/public/lib/get_window.ts index 1ee7e68be8485..42c632f4a514f 100644 --- a/x-pack/legacy/plugins/canvas/public/lib/get_window.ts +++ b/x-pack/legacy/plugins/canvas/public/lib/get_window.ts @@ -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; }; diff --git a/x-pack/legacy/plugins/canvas/public/plugin.tsx b/x-pack/legacy/plugins/canvas/public/plugin.tsx index 44731628cf653..a5fbbccb4299f 100644 --- a/x-pack/legacy/plugins/canvas/public/plugin.tsx +++ b/x-pack/legacy/plugins/canvas/public/plugin.tsx @@ -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 @@ -43,7 +42,6 @@ export interface CanvasStartDeps { __LEGACY: { absoluteToParsedUrl: (url: string, basePath: string) => any; formatMsg: any; - storage: typeof Storage; trackSubUrlForApp: Chrome['trackSubUrlForApp']; }; } @@ -92,7 +90,6 @@ export class CanvasPlugin loadExpressionTypes(); loadTransitions(); - initClipboard(plugins.__LEGACY.storage); initLoadingIndicator(core.http.addLoadingCountSource); core.chrome.setBadge(