-
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
[Canvas] Adds expression registration example plugin #101611
Closed
Closed
Changes from all commits
Commits
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
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,31 @@ | ||
# Expression Registration Example | ||
|
||
This plugin is intended to serve as an example of how to create a plugin to register functions and renderers to the expressions plugin. It registers a `demofunction` function and a `demo` type renderer to the `expressions` plugin and displays a simple expression that makes use of them. This should be a good starting point for anyone building a new plugin to add functionality to the `expressions` plugin. | ||
|
||
Plugins that are made exclusively to add to expressions should be prefixed with `expression`. Examples `expression_shape`, `expression_reveal_image` | ||
|
||
|
||
## Porting from Canvas | ||
|
||
When porting existing functions and renderers from Canvas, here are some key things to watch out for | ||
|
||
#### Prefer React Components | ||
Some of the existing Canvas renderers do a lot of manual DOM manipulation. We prefer all of that logic be encapsulated in a React component, which is much easier to test via Jest and Storybook | ||
|
||
#### ESLint | ||
Some of the Canvas functions and renderers were built outside of our existing eslint config, so expect there to be ESLint errors (filenames not snake cased, etc) as they are moved over. These should be relatively simple to fix. | ||
|
||
#### Handlers | ||
Canvas has extended the IInterpreterRenderHandlers interface to add additional handlers that we pass to our renderes. This is going to prevent reuse across applications. Any use of these extended methods should be changed to use the `event` method that is a part of IInterpreterRenderHandlers. | ||
|
||
``` | ||
// Non-standard handler | ||
handlers.resize({height: 100, width: 150}); | ||
|
||
// Should become | ||
handlers.event('resize', {height: 100, width: 150}); | ||
``` | ||
|
||
|
||
|
||
|
16 changes: 16 additions & 0 deletions
16
examples/expression_registration_example/common/i18n/index.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,16 @@ | ||
/* | ||
* 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 { i18n } from '@kbn/i18n'; | ||
|
||
export const RendererStrings = { | ||
getLabelText: () => | ||
i18n.translate('expressionRegistrationExample.labelMessage', { | ||
defaultMessage: 'Here is the input text', | ||
}), | ||
}; |
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,9 @@ | ||
/* | ||
* 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 * from './i18n'; |
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,11 @@ | ||
{ | ||
"id": "vistypeDemo", | ||
"version": "0.0.1", | ||
"kibanaVersion": "kibana", | ||
"server": false, | ||
"ui": true, | ||
"requiredPlugins": ["expressions", "developerExamples"], | ||
"optionalPlugins": [], | ||
"extraPublicDirs": [], | ||
"requiredBundles": [] | ||
} |
37 changes: 37 additions & 0 deletions
37
examples/expression_registration_example/public/demo_renderer.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,37 @@ | ||
/* | ||
* 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 React, { FC } from 'react'; | ||
import { render, unmountComponentAtNode } from 'react-dom'; | ||
import type { ExpressionRenderDefinition } from '../../../src/plugins/expressions/public'; | ||
import { RendererStrings } from '../common'; | ||
import { DemoRenderValue } from './expression_functions/demo_function'; | ||
|
||
interface Props { | ||
text: string; | ||
color: string; | ||
} | ||
|
||
export const DemoComponent: FC<Props> = ({ text, color }) => ( | ||
<div style={{ color }}> | ||
{RendererStrings.getLabelText()}: {text} | ||
</div> | ||
); | ||
|
||
export const DemoRenderer: ExpressionRenderDefinition<DemoRenderValue> = { | ||
name: 'demo', | ||
displayName: 'demo renderer', | ||
reuseDomNode: true, | ||
render: async (domNode, { text, color }, handlers) => { | ||
handlers.onDestroy(() => unmountComponentAtNode(domNode)); | ||
|
||
render(<DemoComponent text={text} color={color} />, domNode); | ||
|
||
handlers.done(); | ||
}, | ||
}; |
55 changes: 55 additions & 0 deletions
55
examples/expression_registration_example/public/expression_functions/demo_function.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,55 @@ | ||
/* | ||
* 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 { | ||
ExpressionFunctionDefinition, | ||
Render, | ||
} from '../../../../src/plugins/expressions/public'; | ||
|
||
export interface Arguments { | ||
inputText: string; | ||
inputColor?: string; | ||
} | ||
|
||
export interface DemoRenderValue { | ||
text: string; | ||
color: string; | ||
} | ||
|
||
export const demoFunction = (): ExpressionFunctionDefinition< | ||
'demofunction', | ||
null, | ||
Arguments, | ||
Render<DemoRenderValue> | ||
> => ({ | ||
name: 'demofunction', | ||
help: 'This is a demo function', | ||
type: 'render', | ||
args: { | ||
inputText: { | ||
types: ['string'], | ||
help: 'This is the text to be displayed', | ||
required: true, | ||
}, | ||
inputColor: { | ||
types: ['string'], | ||
help: 'This is the color the text should be', | ||
required: false, | ||
}, | ||
}, | ||
fn: (input, args) => { | ||
return { | ||
type: 'render', | ||
as: 'demo', | ||
value: { | ||
text: args.inputText, | ||
color: args.inputColor || 'inherit', | ||
}, | ||
}; | ||
}, | ||
}); |
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,11 @@ | ||
/* | ||
* 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 { VisTypeDemoPlugin } from './plugin'; | ||
|
||
export const plugin = () => new VisTypeDemoPlugin(); |
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.
maybe it makes sense to provide more details here or link to this issue: #74644
for example the resize most likely shouldn't be an event, but should be handled internally ... unless we want to let the outside world know that a resize happened. but in most cases we only want to do some rerendering so that can be done completely internally.
some others are much simpler, for example
onDestroy
needs to be replaced withdestroy
, no need forevent