-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
[Embeddables rebuild] Add new registry #176018
Merged
ThomThomson
merged 13 commits into
elastic:main
from
ThomThomson:embeddableRebuild/addNewRegistry
Feb 2, 2024
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
d45c093
Initial build out of new Embeddable registry
ThomThomson ea49dc0
Merge remote-tracking branch 'upstream/main' into embeddableRebuild/a…
ThomThomson 937e059
Refactor state diffing
ThomThomson 66ab84e
[CI] Auto-commit changed files from 'node scripts/lint_ts_projects --…
kibanamachine c30d1ef
Merge remote-tracking branch 'upstream/main' into embeddableRebuild/a…
ThomThomson 991bf4c
i18n fix
ThomThomson 0647056
mock presentation container
ThomThomson 51794d1
add jest tests, fix types, run initial unsaved changes check in initi…
ThomThomson 7cff095
fix type
ThomThomson 77f2206
remove skip
ThomThomson c453eb6
move eui markdown react embeddable to examples
ThomThomson 98172ae
[CI] Auto-commit changed files from 'node scripts/lint_ts_projects --…
kibanamachine 1f94fd4
review feedback
ThomThomson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
143 changes: 143 additions & 0 deletions
143
examples/embeddable_examples/public/react_embeddables/eui_markdown_react_embeddable.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,143 @@ | ||
/* | ||
* 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 { EuiMarkdownEditor, EuiMarkdownFormat } from '@elastic/eui'; | ||
import { css } from '@emotion/react'; | ||
import { euiThemeVars } from '@kbn/ui-theme'; | ||
import { | ||
ReactEmbeddableFactory, | ||
RegisterReactEmbeddable, | ||
registerReactEmbeddableFactory, | ||
useReactEmbeddableApiHandle, | ||
initializeReactEmbeddableUuid, | ||
initializeReactEmbeddableTitles, | ||
SerializedReactEmbeddableTitles, | ||
DefaultEmbeddableApi, | ||
useReactEmbeddableUnsavedChanges, | ||
} from '@kbn/embeddable-plugin/public'; | ||
import { i18n } from '@kbn/i18n'; | ||
import { useInheritedViewMode, useStateFromPublishingSubject } from '@kbn/presentation-publishing'; | ||
import React from 'react'; | ||
import { BehaviorSubject } from 'rxjs'; | ||
|
||
// ----------------------------------------------------------------------------- | ||
// Types for this embeddable | ||
// ----------------------------------------------------------------------------- | ||
type MarkdownEditorSerializedState = SerializedReactEmbeddableTitles & { | ||
content: string; | ||
}; | ||
|
||
type MarkdownEditorApi = DefaultEmbeddableApi; | ||
|
||
const type = 'euiMarkdown'; | ||
|
||
// ----------------------------------------------------------------------------- | ||
// Define the Embeddable Factory | ||
// ----------------------------------------------------------------------------- | ||
const markdownEmbeddableFactory: ReactEmbeddableFactory< | ||
MarkdownEditorSerializedState, | ||
MarkdownEditorApi | ||
> = { | ||
// ----------------------------------------------------------------------------- | ||
// Deserialize function | ||
// ----------------------------------------------------------------------------- | ||
deserializeState: (state) => { | ||
// We could run migrations here. | ||
// We should inject references here. References are given as state.references | ||
|
||
return state.rawState as MarkdownEditorSerializedState; | ||
}, | ||
|
||
// ----------------------------------------------------------------------------- | ||
// Register the Embeddable component | ||
// ----------------------------------------------------------------------------- | ||
getComponent: async (state, maybeId) => { | ||
/** | ||
* initialize state (source of truth) | ||
*/ | ||
const uuid = initializeReactEmbeddableUuid(maybeId); | ||
const { titlesApi, titleComparators, serializeTitles } = initializeReactEmbeddableTitles(state); | ||
const contentSubject = new BehaviorSubject(state.content); | ||
|
||
/** | ||
* getComponent is async so you can async import the component or load a saved object here. | ||
* the loading will be handed gracefully by the Presentation Container. | ||
*/ | ||
|
||
return RegisterReactEmbeddable((apiRef) => { | ||
/** | ||
* Unsaved changes logic is handled automatically by this hook. You only need to provide | ||
* a subject, setter, and optional state comparator for each key in your state type. | ||
*/ | ||
const { unsavedChanges, resetUnsavedChanges } = useReactEmbeddableUnsavedChanges( | ||
uuid, | ||
markdownEmbeddableFactory, | ||
{ | ||
content: [contentSubject, (value) => contentSubject.next(value)], | ||
...titleComparators, | ||
} | ||
); | ||
|
||
/** | ||
* Publish the API. This is what gets forwarded to the Actions framework, and to whatever the | ||
* parent of this embeddable is. | ||
*/ | ||
const thisApi = useReactEmbeddableApiHandle( | ||
{ | ||
...titlesApi, | ||
unsavedChanges, | ||
resetUnsavedChanges, | ||
serializeState: async () => { | ||
return { | ||
rawState: { | ||
...serializeTitles(), | ||
content: contentSubject.getValue(), | ||
}, | ||
}; | ||
}, | ||
}, | ||
apiRef, | ||
uuid | ||
); | ||
|
||
// get state for rendering | ||
const content = useStateFromPublishingSubject(contentSubject); | ||
const viewMode = useInheritedViewMode(thisApi) ?? 'view'; | ||
|
||
return viewMode === 'edit' ? ( | ||
<EuiMarkdownEditor | ||
css={css` | ||
width: 100%; | ||
`} | ||
value={content ?? ''} | ||
onChange={(value) => contentSubject.next(value)} | ||
aria-label={i18n.translate('dashboard.test.markdownEditor.ariaLabel', { | ||
defaultMessage: 'Dashboard markdown editor', | ||
})} | ||
height="full" | ||
/> | ||
) : ( | ||
<EuiMarkdownFormat | ||
css={css` | ||
padding: ${euiThemeVars.euiSizeS}; | ||
`} | ||
> | ||
{content ?? ''} | ||
</EuiMarkdownFormat> | ||
); | ||
}); | ||
}, | ||
}; | ||
|
||
// ----------------------------------------------------------------------------- | ||
// Register the defined Embeddable Factory - notice that this isn't defined | ||
// on the plugin. Instead, it's a simple imported function. I.E to register an | ||
// Embeddable, you only need the embeddable plugin in your requiredBundles | ||
// ----------------------------------------------------------------------------- | ||
export const registerMarkdownEditorEmbeddable = () => | ||
registerReactEmbeddableFactory(type, markdownEmbeddableFactory); |
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
51 changes: 51 additions & 0 deletions
51
packages/presentation/presentation_containers/interfaces/last_saved_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,51 @@ | ||
/* | ||
* 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 { PublishingSubject } from '@kbn/presentation-publishing'; | ||
import { BehaviorSubject, Subject } from 'rxjs'; | ||
import { filter, map } from 'rxjs/operators'; | ||
import { SerializedPanelState } from './serialized_state'; | ||
|
||
export interface PublishesLastSavedState { | ||
lastSavedState: Subject<void>; // a notification that the last saved state has changed | ||
getLastSavedStateForChild: (childId: string) => SerializedPanelState | undefined; | ||
} | ||
|
||
export const apiPublishesLastSavedState = (api: unknown): api is PublishesLastSavedState => { | ||
return Boolean( | ||
api && | ||
(api as PublishesLastSavedState).lastSavedState && | ||
(api as PublishesLastSavedState).getLastSavedStateForChild | ||
); | ||
}; | ||
|
||
export const getLastSavedStateSubjectForChild = <StateType extends unknown = unknown>( | ||
parentApi: unknown, | ||
childId: string, | ||
deserializer?: (state: SerializedPanelState) => StateType | ||
): PublishingSubject<StateType | undefined> | undefined => { | ||
if (!parentApi) return; | ||
const fetchUnsavedChanges = (): StateType | undefined => { | ||
if (!apiPublishesLastSavedState(parentApi)) return; | ||
const rawLastSavedState = parentApi.getLastSavedStateForChild(childId); | ||
if (rawLastSavedState === undefined) return; | ||
return deserializer | ||
? deserializer(rawLastSavedState) | ||
: (rawLastSavedState.rawState as StateType); | ||
}; | ||
|
||
const lastSavedStateForChild = new BehaviorSubject<StateType | undefined>(fetchUnsavedChanges()); | ||
if (!apiPublishesLastSavedState(parentApi)) return; | ||
parentApi.lastSavedState | ||
.pipe( | ||
map(() => fetchUnsavedChanges()), | ||
filter((rawLastSavedState) => rawLastSavedState !== undefined) | ||
) | ||
.subscribe(lastSavedStateForChild); | ||
return lastSavedStateForChild; | ||
}; |
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
19 changes: 19 additions & 0 deletions
19
packages/presentation/presentation_containers/interfaces/serialized_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,19 @@ | ||
/* | ||
* 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 type { SavedObjectReference } from '@kbn/core-saved-objects-api-server'; | ||
|
||
/** | ||
* A package containing the serialized Embeddable state, with references extracted. When saving Embeddables using any | ||
* strategy, this is the format that should be used. | ||
*/ | ||
export interface SerializedPanelState<RawStateType extends object = object> { | ||
references?: SavedObjectReference[]; | ||
rawState: RawStateType; | ||
version?: string; | ||
} |
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,20 @@ | ||
/* | ||
* 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 { Subject } from 'rxjs'; | ||
import { PresentationContainer } from './interfaces/presentation_container'; | ||
|
||
export const getMockPresentationContainer = (): PresentationContainer => { | ||
return { | ||
registerPanelApi: jest.fn(), | ||
removePanel: jest.fn(), | ||
replacePanel: jest.fn(), | ||
lastSavedState: new Subject<void>(), | ||
getLastSavedStateForChild: jest.fn(), | ||
}; | ||
}; |
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
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
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about lazy loading component. Examples should show best practices and best practices recommend keeping bundle size as small as possible.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a good point. This markdown example is mostly there for testing purposes at the moment. I've created this follow-up issue to describe further examples that should be built, and an async-imported embeddable is high up on the list.