-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Embeddables rebuild] Support for by reference embeddables (#182523)
Adds first-class by reference support to the new Embeddable framework and adds an example of how a new-styled by reference embeddable could work.
- Loading branch information
1 parent
95ad2f7
commit 53435ea
Showing
53 changed files
with
1,451 additions
and
508 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
examples/embeddable_examples/public/react_embeddables/saved_book/book_state.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { BehaviorSubject } from 'rxjs'; | ||
import { BookAttributes, BookAttributesManager } from './types'; | ||
|
||
export const defaultBookAttributes: BookAttributes = { | ||
bookTitle: 'Pillars of the earth', | ||
authorName: 'Ken follett', | ||
numberOfPages: 973, | ||
bookSynopsis: | ||
'A spellbinding epic set in 12th-century England, The Pillars of the Earth tells the story of the struggle to build the greatest Gothic cathedral the world has known.', | ||
}; | ||
|
||
export const stateManagerFromAttributes = (attributes: BookAttributes): BookAttributesManager => { | ||
const bookTitle = new BehaviorSubject<string>(attributes.bookTitle); | ||
const authorName = new BehaviorSubject<string>(attributes.authorName); | ||
const numberOfPages = new BehaviorSubject<number>(attributes.numberOfPages); | ||
const bookSynopsis = new BehaviorSubject<string | undefined>(attributes.bookSynopsis); | ||
|
||
return { | ||
bookTitle, | ||
authorName, | ||
numberOfPages, | ||
bookSynopsis, | ||
comparators: { | ||
bookTitle: [bookTitle, (val) => bookTitle.next(val)], | ||
authorName: [authorName, (val) => authorName.next(val)], | ||
numberOfPages: [numberOfPages, (val) => numberOfPages.next(val)], | ||
bookSynopsis: [bookSynopsis, (val) => bookSynopsis.next(val)], | ||
}, | ||
}; | ||
}; | ||
|
||
export const serializeBookAttributes = (stateManager: BookAttributesManager): BookAttributes => ({ | ||
bookTitle: stateManager.bookTitle.value, | ||
authorName: stateManager.authorName.value, | ||
numberOfPages: stateManager.numberOfPages.value, | ||
bookSynopsis: stateManager.bookSynopsis.value, | ||
}); |
10 changes: 10 additions & 0 deletions
10
examples/embeddable_examples/public/react_embeddables/saved_book/constants.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
export const SAVED_BOOK_ID = 'book'; | ||
export const ADD_SAVED_BOOK_ACTION_ID = 'create_saved_book'; |
71 changes: 71 additions & 0 deletions
71
...ples/embeddable_examples/public/react_embeddables/saved_book/create_saved_book_action.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { CoreStart } from '@kbn/core/public'; | ||
import { i18n } from '@kbn/i18n'; | ||
import { apiIsPresentationContainer } from '@kbn/presentation-containers'; | ||
import { EmbeddableApiContext } from '@kbn/presentation-publishing'; | ||
import { IncompatibleActionError } from '@kbn/ui-actions-plugin/public'; | ||
import { UiActionsPublicStart } from '@kbn/ui-actions-plugin/public/plugin'; | ||
import { embeddableExamplesGrouping } from '../embeddable_examples_grouping'; | ||
import { | ||
defaultBookAttributes, | ||
serializeBookAttributes, | ||
stateManagerFromAttributes, | ||
} from './book_state'; | ||
import { ADD_SAVED_BOOK_ACTION_ID, SAVED_BOOK_ID } from './constants'; | ||
import { openSavedBookEditor } from './saved_book_editor'; | ||
import { saveBookAttributes } from './saved_book_library'; | ||
import { | ||
BookByReferenceSerializedState, | ||
BookByValueSerializedState, | ||
BookSerializedState, | ||
} from './types'; | ||
|
||
export const registerCreateSavedBookAction = (uiActions: UiActionsPublicStart, core: CoreStart) => { | ||
uiActions.registerAction<EmbeddableApiContext>({ | ||
id: ADD_SAVED_BOOK_ACTION_ID, | ||
getIconType: () => 'folderClosed', | ||
grouping: [embeddableExamplesGrouping], | ||
isCompatible: async ({ embeddable }) => { | ||
return apiIsPresentationContainer(embeddable); | ||
}, | ||
execute: async ({ embeddable }) => { | ||
if (!apiIsPresentationContainer(embeddable)) throw new IncompatibleActionError(); | ||
const newPanelStateManager = stateManagerFromAttributes(defaultBookAttributes); | ||
|
||
const { addToLibrary } = await openSavedBookEditor(newPanelStateManager, true, core, { | ||
parentApi: embeddable, | ||
}); | ||
|
||
const initialState: BookSerializedState = await (async () => { | ||
// if we're adding this to the library, we only need to return the by reference state. | ||
if (addToLibrary) { | ||
const savedBookId = await saveBookAttributes( | ||
undefined, | ||
serializeBookAttributes(newPanelStateManager) | ||
); | ||
return { savedBookId } as BookByReferenceSerializedState; | ||
} | ||
return { | ||
attributes: serializeBookAttributes(newPanelStateManager), | ||
} as BookByValueSerializedState; | ||
})(); | ||
|
||
embeddable.addNewPanel<BookSerializedState>({ | ||
panelType: SAVED_BOOK_ID, | ||
initialState, | ||
}); | ||
}, | ||
getDisplayName: () => | ||
i18n.translate('embeddableExamples.savedbook.addBookAction.displayName', { | ||
defaultMessage: 'Book', | ||
}), | ||
}); | ||
uiActions.attachAction('ADD_PANEL_TRIGGER', ADD_SAVED_BOOK_ACTION_ID); | ||
}; |
Oops, something went wrong.