-
Notifications
You must be signed in to change notification settings - Fork 8.5k
[Example] Embeddable by Reference and Value #68719
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
Merged
ThomThomson
merged 25 commits into
elastic:master
from
ThomThomson:example/refToValEmbeddable
Jul 9, 2020
Merged
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
f8a21a8
Started example
ThomThomson 74b101a
Created book embeddable, started work on an unwrapper function for re…
ThomThomson f8670a1
Merge branch 'master' of github.com:elastic/kibana into example/refTo…
ThomThomson c057648
finished value and reference embeddable
ThomThomson 16d0fc5
Changed the default books :)
ThomThomson 45da976
type changes
ThomThomson ba62f65
Merge branch 'master' of github.com:elastic/kibana into example/refTo…
ThomThomson a4bf199
type fix
ThomThomson 2cf06b1
Update fields
ThomThomson 576e2f9
added attribute service to embeddable start contract. added a wrap me…
ThomThomson 48bddd0
Merge branch 'master' of github.com:elastic/kibana into example/refTo…
ThomThomson f49d8e1
small fixes
ThomThomson 58d89ef
Merge branch 'master' of github.com:elastic/kibana into example/refTo…
ThomThomson e620bcb
fixed typo in node order
ThomThomson 5dc4fb1
Merge branch 'master' of github.com:elastic/kibana into example/refTo…
ThomThomson c1ced25
Merge branch 'master' of github.com:elastic/kibana into example/refTo…
ThomThomson 5115a3b
Merge branch 'master' of github.com:elastic/kibana into example/refTo…
ThomThomson 7f9d8a0
Merge branch 'master' into example/refToValEmbeddable
elasticmachine 215d6be
Merge branch 'master' of github.com:elastic/kibana into example/refTo…
ThomThomson c44775b
merge
ThomThomson 6de27c0
Merge branch 'master' of github.com:elastic/kibana into example/refTo…
ThomThomson a653ff2
Merge branch 'master' of github.com:elastic/kibana into example/refTo…
ThomThomson d3af35e
Merge branch 'master' of github.com:elastic/kibana into example/refTo…
ThomThomson b0b8745
Removed create from saved object
ThomThomson 7c826af
Removed unused import
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
28 changes: 28 additions & 0 deletions
28
examples/embeddable_examples/common/book_saved_object_attributes.ts
This file contains hidden or 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,28 @@ | ||
| /* | ||
| * Licensed to Elasticsearch B.V. under one or more contributor | ||
| * license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright | ||
| * ownership. Elasticsearch B.V. licenses this file to you under | ||
| * the Apache License, Version 2.0 (the "License"); you may | ||
| * not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| import { SavedObjectAttributes } from '../../../src/core/types'; | ||
|
|
||
| export const BOOK_SAVED_OBJECT = 'book'; | ||
|
|
||
| export interface BookSavedObjectAttributes extends SavedObjectAttributes { | ||
| title: string; | ||
| author?: string; | ||
| readIt?: boolean; | ||
| } |
This file contains hidden or 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 hidden or 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
90 changes: 90 additions & 0 deletions
90
examples/embeddable_examples/public/book/book_component.tsx
This file contains hidden or 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,90 @@ | ||
| /* | ||
| * Licensed to Elasticsearch B.V. under one or more contributor | ||
| * license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright | ||
| * ownership. Elasticsearch B.V. licenses this file to you under | ||
| * the Apache License, Version 2.0 (the "License"); you may | ||
| * not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
| import React from 'react'; | ||
| import { EuiFlexItem, EuiFlexGroup, EuiIcon } from '@elastic/eui'; | ||
|
|
||
| import { EuiText } from '@elastic/eui'; | ||
| import { EuiFlexGrid } from '@elastic/eui'; | ||
| import { withEmbeddableSubscription } from '../../../../src/plugins/embeddable/public'; | ||
| import { BookEmbeddableInput, BookEmbeddableOutput, BookEmbeddable } from './book_embeddable'; | ||
|
|
||
| interface Props { | ||
| input: BookEmbeddableInput; | ||
| output: BookEmbeddableOutput; | ||
| embeddable: BookEmbeddable; | ||
| } | ||
|
|
||
| function wrapSearchTerms(task?: string, search?: string) { | ||
| if (!search || !task) return task; | ||
| const parts = task.split(new RegExp(`(${search})`, 'g')); | ||
| return parts.map((part, i) => | ||
| part === search ? ( | ||
| <span key={i} style={{ backgroundColor: 'yellow' }}> | ||
| {part} | ||
| </span> | ||
| ) : ( | ||
| part | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| export function BookEmbeddableComponentInner({ input: { search }, output: { attributes } }: Props) { | ||
| const title = attributes?.title; | ||
| const author = attributes?.author; | ||
| const readIt = attributes?.readIt; | ||
|
|
||
| return ( | ||
| <EuiFlexGroup gutterSize="s"> | ||
| <EuiFlexItem> | ||
| <EuiFlexGrid columns={1} gutterSize="none"> | ||
| {title ? ( | ||
| <EuiFlexItem> | ||
| <EuiText data-test-subj="bookEmbeddableTitle"> | ||
| <h3>{wrapSearchTerms(title, search)},</h3> | ||
| </EuiText> | ||
| </EuiFlexItem> | ||
| ) : null} | ||
| {author ? ( | ||
| <EuiFlexItem> | ||
| <EuiText data-test-subj="bookEmbeddableAuthor"> | ||
| <h5>-{wrapSearchTerms(author, search)}</h5> | ||
| </EuiText> | ||
| </EuiFlexItem> | ||
| ) : null} | ||
| {readIt ? ( | ||
| <EuiFlexItem> | ||
| <EuiIcon type="check" /> | ||
| </EuiFlexItem> | ||
| ) : ( | ||
| <EuiFlexItem> | ||
| <EuiIcon type="cross" /> | ||
| </EuiFlexItem> | ||
| )} | ||
| </EuiFlexGrid> | ||
| </EuiFlexItem> | ||
| </EuiFlexGroup> | ||
| ); | ||
| } | ||
|
|
||
| export const BookEmbeddableComponent = withEmbeddableSubscription< | ||
| BookEmbeddableInput, | ||
| BookEmbeddableOutput, | ||
| BookEmbeddable, | ||
| {} | ||
| >(BookEmbeddableComponentInner); |
123 changes: 123 additions & 0 deletions
123
examples/embeddable_examples/public/book/book_embeddable.tsx
This file contains hidden or 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,123 @@ | ||
| /* | ||
| * Licensed to Elasticsearch B.V. under one or more contributor | ||
| * license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright | ||
| * ownership. Elasticsearch B.V. licenses this file to you under | ||
| * the Apache License, Version 2.0 (the "License"); you may | ||
| * not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
| import React from 'react'; | ||
| import ReactDOM from 'react-dom'; | ||
| import { Subscription } from 'rxjs'; | ||
| import { | ||
| Embeddable, | ||
| EmbeddableInput, | ||
| IContainer, | ||
| EmbeddableOutput, | ||
| SavedObjectEmbeddableInput, | ||
| AttributeService, | ||
| } from '../../../../src/plugins/embeddable/public'; | ||
| import { BookSavedObjectAttributes } from '../../common'; | ||
| import { BookEmbeddableComponent } from './book_component'; | ||
|
|
||
| export const BOOK_EMBEDDABLE = 'book'; | ||
| export type BookEmbeddableInput = BookByValueInput | BookByReferenceInput; | ||
| export interface BookEmbeddableOutput extends EmbeddableOutput { | ||
| hasMatch: boolean; | ||
| attributes: BookSavedObjectAttributes; | ||
| } | ||
|
|
||
| interface BookInheritedInput extends EmbeddableInput { | ||
| search?: string; | ||
| } | ||
|
|
||
| export type BookByValueInput = { attributes: BookSavedObjectAttributes } & BookInheritedInput; | ||
| export type BookByReferenceInput = SavedObjectEmbeddableInput & BookInheritedInput; | ||
|
|
||
| /** | ||
| * Returns whether any attributes contain the search string. If search is empty, true is returned. If | ||
| * there are no savedAttributes, false is returned. | ||
| * @param search - the search string | ||
| * @param savedAttributes - the saved object attributes for the saved object with id `input.savedObjectId` | ||
| */ | ||
| function getHasMatch(search?: string, savedAttributes?: BookSavedObjectAttributes): boolean { | ||
| if (!search) return true; | ||
| if (!savedAttributes) return false; | ||
| return Boolean( | ||
| (savedAttributes.author && savedAttributes.author.match(search)) || | ||
| (savedAttributes.title && savedAttributes.title.match(search)) | ||
| ); | ||
| } | ||
|
|
||
| export class BookEmbeddable extends Embeddable<BookEmbeddableInput, BookEmbeddableOutput> { | ||
| public readonly type = BOOK_EMBEDDABLE; | ||
| private subscription: Subscription; | ||
| private node?: HTMLElement; | ||
| private savedObjectId?: string; | ||
| private attributes?: BookSavedObjectAttributes; | ||
|
|
||
| constructor( | ||
| initialInput: BookEmbeddableInput, | ||
| private attributeService: AttributeService< | ||
| BookSavedObjectAttributes, | ||
| BookByValueInput, | ||
| BookByReferenceInput | ||
| >, | ||
| { | ||
| parent, | ||
| }: { | ||
| parent?: IContainer; | ||
| } | ||
| ) { | ||
| super(initialInput, {} as BookEmbeddableOutput, parent); | ||
|
|
||
| this.subscription = this.getInput$().subscribe(async () => { | ||
| const savedObjectId = (this.getInput() as BookByReferenceInput).savedObjectId; | ||
| const attributes = (this.getInput() as BookByValueInput).attributes; | ||
| if (this.attributes !== attributes || this.savedObjectId !== savedObjectId) { | ||
| this.savedObjectId = savedObjectId; | ||
| this.reload(); | ||
| } else { | ||
| this.updateOutput({ | ||
| attributes: this.attributes, | ||
| hasMatch: getHasMatch(this.input.search, this.attributes), | ||
| }); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| public render(node: HTMLElement) { | ||
| if (this.node) { | ||
| ReactDOM.unmountComponentAtNode(this.node); | ||
ThomThomson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| this.node = node; | ||
| ReactDOM.render(<BookEmbeddableComponent embeddable={this} />, node); | ||
| } | ||
|
|
||
| public async reload() { | ||
| this.attributes = await this.attributeService.unwrapAttributes(this.input); | ||
|
|
||
| this.updateOutput({ | ||
| attributes: this.attributes, | ||
| hasMatch: getHasMatch(this.input.search, this.attributes), | ||
| }); | ||
| } | ||
|
|
||
| public destroy() { | ||
| super.destroy(); | ||
| this.subscription.unsubscribe(); | ||
| if (this.node) { | ||
| ReactDOM.unmountComponentAtNode(this.node); | ||
| } | ||
| } | ||
| } | ||
127 changes: 127 additions & 0 deletions
127
examples/embeddable_examples/public/book/book_embeddable_factory.tsx
This file contains hidden or 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,127 @@ | ||
| /* | ||
| * Licensed to Elasticsearch B.V. under one or more contributor | ||
| * license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright | ||
| * ownership. Elasticsearch B.V. licenses this file to you under | ||
| * the Apache License, Version 2.0 (the "License"); you may | ||
| * not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| import React from 'react'; | ||
| import { i18n } from '@kbn/i18n'; | ||
| import { BookSavedObjectAttributes, BOOK_SAVED_OBJECT } from '../../common'; | ||
| import { toMountPoint } from '../../../../src/plugins/kibana_react/public'; | ||
| import { | ||
| EmbeddableFactoryDefinition, | ||
| EmbeddableStart, | ||
| IContainer, | ||
| AttributeService, | ||
| EmbeddableFactory, | ||
| } from '../../../../src/plugins/embeddable/public'; | ||
| import { | ||
| BookEmbeddable, | ||
| BOOK_EMBEDDABLE, | ||
| BookEmbeddableInput, | ||
| BookEmbeddableOutput, | ||
| BookByValueInput, | ||
| BookByReferenceInput, | ||
| } from './book_embeddable'; | ||
| import { CreateEditBookComponent } from './create_edit_book_component'; | ||
| import { OverlayStart } from '../../../../src/core/public'; | ||
|
|
||
| interface StartServices { | ||
| getAttributeService: EmbeddableStart['getAttributeService']; | ||
| openModal: OverlayStart['openModal']; | ||
| } | ||
|
|
||
| export type BookEmbeddableFactory = EmbeddableFactory< | ||
| BookEmbeddableInput, | ||
| BookEmbeddableOutput, | ||
| BookEmbeddable, | ||
| BookSavedObjectAttributes | ||
| >; | ||
|
|
||
| export class BookEmbeddableFactoryDefinition | ||
| implements | ||
| EmbeddableFactoryDefinition< | ||
| BookEmbeddableInput, | ||
| BookEmbeddableOutput, | ||
| BookEmbeddable, | ||
| BookSavedObjectAttributes | ||
| > { | ||
| public readonly type = BOOK_EMBEDDABLE; | ||
| public savedObjectMetaData = { | ||
| name: 'Book', | ||
| includeFields: ['title', 'author', 'readIt'], | ||
| type: BOOK_SAVED_OBJECT, | ||
| getIconForSavedObject: () => 'pencil', | ||
| }; | ||
|
|
||
| private attributeService?: AttributeService< | ||
| BookSavedObjectAttributes, | ||
| BookByValueInput, | ||
| BookByReferenceInput | ||
| >; | ||
|
|
||
| constructor(private getStartServices: () => Promise<StartServices>) {} | ||
|
|
||
| public async isEditable() { | ||
| return true; | ||
| } | ||
|
|
||
| public async create(input: BookEmbeddableInput, parent?: IContainer) { | ||
| return new BookEmbeddable(input, await this.getAttributeService(), { | ||
| parent, | ||
| }); | ||
| } | ||
|
|
||
| public getDisplayName() { | ||
| return i18n.translate('embeddableExamples.book.displayName', { | ||
| defaultMessage: 'Book', | ||
| }); | ||
| } | ||
|
|
||
| public async getExplicitInput(): Promise<Omit<BookEmbeddableInput, 'id'>> { | ||
| const { openModal } = await this.getStartServices(); | ||
| return new Promise<Omit<BookEmbeddableInput, 'id'>>((resolve) => { | ||
| const onSave = async (attributes: BookSavedObjectAttributes, useRefType: boolean) => { | ||
| const wrappedAttributes = (await this.getAttributeService()).wrapAttributes( | ||
| attributes, | ||
| useRefType | ||
| ); | ||
| resolve(wrappedAttributes); | ||
| }; | ||
| const overlay = openModal( | ||
| toMountPoint( | ||
| <CreateEditBookComponent | ||
| onSave={(attributes: BookSavedObjectAttributes, useRefType: boolean) => { | ||
| onSave(attributes, useRefType); | ||
| overlay.close(); | ||
| }} | ||
| /> | ||
| ) | ||
| ); | ||
| }); | ||
| } | ||
|
|
||
| private async getAttributeService() { | ||
| if (!this.attributeService) { | ||
| this.attributeService = await (await this.getStartServices()).getAttributeService< | ||
| BookSavedObjectAttributes, | ||
| BookByValueInput, | ||
| BookByReferenceInput | ||
| >(this.type); | ||
| } | ||
| return this.attributeService; | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.