-
Notifications
You must be signed in to change notification settings - Fork 206
Manage state outside of React #1159
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
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,3 @@ | ||
| import { | ||
| useState, | ||
| useMemo, | ||
| useEffect, | ||
| useCallback | ||
| } from '@bpmn-io/properties-panel/preact/hooks'; | ||
|
|
||
| import { | ||
| find, | ||
| isArray, | ||
| reduce | ||
| } from 'min-dash'; | ||
|
|
||
| import { FeelLanguageContext, PropertiesPanel } from '@bpmn-io/properties-panel'; | ||
|
|
||
| import { | ||
|
|
@@ -28,7 +15,6 @@ const DEFAULT_FEEL_LANGUAGE_CONTEXT = { | |
| * @param {Object} props | ||
| * @param {djs.model.Base|Array<djs.model.Base>} [props.element] | ||
| * @param {Injector} props.injector | ||
| * @param { (djs.model.Base) => Array<PropertiesProvider> } props.getProviders | ||
| * @param {Object} props.layoutConfig | ||
| * @param {Object} props.descriptionConfig | ||
| * @param {Object} props.tooltipConfig | ||
|
|
@@ -38,194 +24,38 @@ const DEFAULT_FEEL_LANGUAGE_CONTEXT = { | |
| export default function BpmnPropertiesPanel(props) { | ||
| const { | ||
| element, | ||
| groups, | ||
| injector, | ||
| getProviders, | ||
| layoutConfig: initialLayoutConfig, | ||
| layoutConfig, | ||
| descriptionConfig, | ||
| tooltipConfig, | ||
| feelPopupContainer, | ||
| getFeelPopupLinks | ||
| } = props; | ||
|
|
||
| const canvas = injector.get('canvas'); | ||
| const elementRegistry = injector.get('elementRegistry'); | ||
| const eventBus = injector.get('eventBus'); | ||
| const translate = injector.get('translate'); | ||
|
|
||
| const [ state, setState ] = useState({ | ||
| selectedElement: element | ||
| }); | ||
|
|
||
| const selectedElement = state.selectedElement; | ||
|
|
||
| /** | ||
| * @param {djs.model.Base | Array<djs.model.Base>} element | ||
| */ | ||
| const _update = (element) => { | ||
|
|
||
| if (!element) { | ||
| return; | ||
| } | ||
|
|
||
| let newSelectedElement = element; | ||
|
|
||
| // handle labels | ||
| if (newSelectedElement && newSelectedElement.type === 'label') { | ||
| newSelectedElement = newSelectedElement.labelTarget; | ||
| } | ||
|
Comment on lines
-72
to
-75
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Likely cause of the problem. |
||
|
|
||
| setState({ | ||
| ...state, | ||
| selectedElement: newSelectedElement | ||
| }); | ||
|
|
||
| // notify interested parties on property panel updates | ||
| eventBus.fire('propertiesPanel.updated', { | ||
| element: newSelectedElement | ||
| }); | ||
| }; | ||
|
|
||
| // (2) react on element changes | ||
|
|
||
| // (2a) selection changed | ||
| useEffect(() => { | ||
| const onSelectionChanged = (e) => { | ||
| const { newSelection = [] } = e; | ||
|
|
||
| if (newSelection.length > 1) { | ||
| return _update(newSelection); | ||
| } | ||
|
|
||
| const newElement = newSelection[0]; | ||
|
|
||
| const rootElement = canvas.getRootElement(); | ||
|
|
||
| if (isImplicitRoot(rootElement)) { | ||
| return; | ||
| } | ||
|
|
||
| _update(newElement || rootElement); | ||
| }; | ||
|
|
||
| eventBus.on('selection.changed', onSelectionChanged); | ||
|
|
||
| return () => { | ||
| eventBus.off('selection.changed', onSelectionChanged); | ||
| }; | ||
| }, []); | ||
|
|
||
| // (2b) selected element changed | ||
| useEffect(() => { | ||
| const onElementsChanged = (e) => { | ||
| const elements = e.elements; | ||
|
|
||
| const updatedElement = findElement(elements, selectedElement); | ||
|
|
||
| if (updatedElement && elementExists(updatedElement, elementRegistry)) { | ||
| _update(updatedElement); | ||
| } | ||
| }; | ||
|
|
||
| eventBus.on('elements.changed', onElementsChanged); | ||
|
|
||
| return () => { | ||
| eventBus.off('elements.changed', onElementsChanged); | ||
| }; | ||
| }, [ selectedElement ]); | ||
| const selectedElement = element; | ||
|
|
||
| // (2c) root element changed | ||
| useEffect(() => { | ||
| const onRootAdded = (e) => { | ||
| const element = e.element; | ||
|
|
||
| _update(element); | ||
| }; | ||
|
|
||
| eventBus.on('root.added', onRootAdded); | ||
|
|
||
| return () => { | ||
| eventBus.off('root.added', onRootAdded); | ||
| }; | ||
| }, [ selectedElement ]); | ||
|
|
||
| // (2d) provided entries changed | ||
| useEffect(() => { | ||
| const onProvidersChanged = () => { | ||
| _update(selectedElement); | ||
| }; | ||
|
|
||
| eventBus.on('propertiesPanel.providersChanged', onProvidersChanged); | ||
|
|
||
| return () => { | ||
| eventBus.off('propertiesPanel.providersChanged', onProvidersChanged); | ||
| }; | ||
| }, [ selectedElement ]); | ||
|
|
||
| // (2e) element templates changed | ||
| useEffect(() => { | ||
| const onTemplatesChanged = () => { | ||
| _update(selectedElement); | ||
| }; | ||
|
|
||
| eventBus.on('elementTemplates.changed', onTemplatesChanged); | ||
|
|
||
| return () => { | ||
| eventBus.off('elementTemplates.changed', onTemplatesChanged); | ||
| }; | ||
| }, [ selectedElement ]); | ||
|
|
||
| // (3) create properties panel context | ||
| const bpmnPropertiesPanelContext = { | ||
| selectedElement, | ||
| injector, | ||
| getService(type, strict) { return injector.get(type, strict); } | ||
| }; | ||
|
|
||
| // (4) retrieve groups for selected element | ||
| const providers = getProviders(selectedElement); | ||
|
|
||
| const groups = useMemo(() => { | ||
| return reduce(providers, function(groups, provider) { | ||
|
|
||
| // do not collect groups for multi element state | ||
| if (isArray(selectedElement)) { | ||
| return []; | ||
| } | ||
|
|
||
| const updater = provider.getGroups(selectedElement); | ||
|
|
||
| return updater(groups); | ||
| }, []); | ||
| }, [ providers, selectedElement ]); | ||
|
|
||
| // (5) notify layout changes | ||
| const [ layoutConfig, setLayoutConfig ] = useState(initialLayoutConfig || {}); | ||
|
|
||
| const onLayoutChanged = useCallback((newLayout) => { | ||
| const onLayoutChanged = (layoutConfig) => { | ||
| eventBus.fire('propertiesPanel.layoutChanged', { | ||
| layout: newLayout | ||
| layout: layoutConfig | ||
| }); | ||
| }, [ eventBus ]); | ||
|
|
||
| // React to external layout changes | ||
| useEffect(() => { | ||
| const cb = (e) => { | ||
| const { layout } = e; | ||
| setLayoutConfig(layout); | ||
| }; | ||
|
|
||
| eventBus.on('propertiesPanel.setLayout', cb); | ||
| return () => eventBus.off('propertiesPanel.setLayout', cb); | ||
| }, [ eventBus, setLayoutConfig ]); | ||
| }; | ||
|
|
||
| // (6) notify description changes | ||
| const onDescriptionLoaded = (description) => { | ||
| eventBus.fire('propertiesPanel.descriptionLoaded', { | ||
| description | ||
| }); | ||
| }; | ||
|
|
||
| // (7) notify tooltip changes | ||
| const onTooltipLoaded = (tooltip) => { | ||
| eventBus.fire('propertiesPanel.tooltipLoaded', { | ||
| tooltip | ||
|
|
@@ -253,18 +83,3 @@ export default function BpmnPropertiesPanel(props) { | |
| </BpmnPropertiesPanelContext.Provider> | ||
| ); | ||
| } | ||
|
|
||
|
|
||
| // helpers ////////////////////////// | ||
|
|
||
| function isImplicitRoot(element) { | ||
| return element && element.isImplicit; | ||
| } | ||
|
|
||
| function findElement(elements, element) { | ||
| return find(elements, (e) => e === element); | ||
| } | ||
|
|
||
| function elementExists(element, elementRegistry) { | ||
| return element && elementRegistry.get(element.id); | ||
| } | ||
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.
I am super happy to see this removed.