|
| 1 | +import React, {PureComponent} from 'react'; |
| 2 | +import PropTypes from 'prop-types'; |
| 3 | +import ReactDOM from 'react-dom'; |
| 4 | +import manifest from '@neos-project/neos-ui-extensibility'; |
| 5 | +import {selectors} from '@neos-project/neos-ui-redux-store'; |
| 6 | +import {DropDown} from '@neos-project/react-ui-components'; |
| 7 | +import {connect} from 'react-redux'; |
| 8 | +import {$transform, $get} from 'plow-js'; |
| 9 | +import EditorEnvelope from '@neos-project/neos-ui-editors'; |
| 10 | +import style from './style.css'; |
| 11 | + |
| 12 | +@connect($transform({ |
| 13 | + currentlyEditedPropertyName: selectors.UI.ContentCanvas.currentlyEditedPropertyName, |
| 14 | + getNodeByContextPath: selectors.CR.Nodes.nodeByContextPath, |
| 15 | + focusedNodePath: selectors.CR.Nodes.focusedNodePathSelector |
| 16 | +})) |
| 17 | +class InlineEditorEnvelope extends PureComponent { |
| 18 | + state = { |
| 19 | + isOpen: false |
| 20 | + } |
| 21 | + static childContextTypes = { |
| 22 | + store: PropTypes.object.isRequired, |
| 23 | + globalRegistry: PropTypes.object.isRequired, |
| 24 | + configuration: PropTypes.object.isRequired, |
| 25 | + routes: PropTypes.object.isRequired |
| 26 | + }; |
| 27 | + getChildContext() { |
| 28 | + const {configuration, globalRegistry, routes, store} = this.props; |
| 29 | + return {configuration, globalRegistry, routes, store}; |
| 30 | + } |
| 31 | + handleToggle = () => { |
| 32 | + this.setState({isOpen: !this.state.isOpen}); |
| 33 | + } |
| 34 | + render() { |
| 35 | + const {contextPath, fusionPath, propertyName, persistChange, editorOptions, getNodeByContextPath, focusedNodePath} = this.props; |
| 36 | + const value = $get(['properties', propertyName], getNodeByContextPath(contextPath)); |
| 37 | + return ( |
| 38 | + <div style={{display: 'inline-block'}}> |
| 39 | + <DropDown.Stateless isOpen={this.state.isOpen} padded={true} onToggle={this.handleToggle} onClose={() => null}> |
| 40 | + <DropDown.Header className="enveloper_dropdown_header"> |
| 41 | + <style>{'\ |
| 42 | + .enveloper_dropdown_header{\ |
| 43 | + width: 30px;\ |
| 44 | + height: 30px;\ |
| 45 | + padding: 8px;\ |
| 46 | + }\ |
| 47 | + .enveloper_dropdown_contents{\ |
| 48 | + width: 320px;\ |
| 49 | + background-color: #272727;\ |
| 50 | + }\ |
| 51 | + .enveloper_dropdown_header i {\ |
| 52 | + display: none;\ |
| 53 | + }\ |
| 54 | + '}</style> |
| 55 | + <svg style={{backgroundColor: '#323232', position: 'absolute', fill: 'white', width: '14px', height: '14px'}} width="1792" height="1792" viewBox="0 0 1792 1792" xmlns="http://www.w3.org/2000/svg"><path d="M491 1536l91-91-235-235-91 91v107h128v128h107zm523-928q0-22-22-22-10 0-17 7l-542 542q-7 7-7 17 0 22 22 22 10 0 17-7l542-542q7-7 7-17zm-54-192l416 416-832 832h-416v-416zm683 96q0 53-37 90l-166 166-416-416 166-165q36-38 90-38 53 0 91 38l235 234q37 39 37 91z" /></svg> |
| 56 | + </DropDown.Header> |
| 57 | + <DropDown.Contents className="enveloper_dropdown_contents" scrollable={false}> |
| 58 | + <div> |
| 59 | + <EditorEnvelope |
| 60 | + identifier={propertyName} |
| 61 | + label={$get('label', editorOptions)} |
| 62 | + editor={$get('editor', editorOptions)} |
| 63 | + value={value && value.toJS ? value.toJS() : value} |
| 64 | + hooks={null} |
| 65 | + options={editorOptions} |
| 66 | + commit={value => { |
| 67 | + persistChange({ |
| 68 | + type: 'Neos.Neos.Ui:Property', |
| 69 | + subject: contextPath, |
| 70 | + payload: { |
| 71 | + propertyName, |
| 72 | + value, |
| 73 | + nodeDomAddress: { |
| 74 | + contextPath, |
| 75 | + fusionPath |
| 76 | + } |
| 77 | + } |
| 78 | + }); |
| 79 | + }} |
| 80 | + renderSecondaryInspector={() => null} |
| 81 | + /> |
| 82 | + </div> |
| 83 | + </DropDown.Contents> |
| 84 | + </DropDown.Stateless> |
| 85 | + </div> |
| 86 | + ); |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +const findParentFusionPath = (node, contextPath) => { |
| 91 | + if (node) { |
| 92 | + const fusionPath = node.getAttribute('data-__neos-fusion-path'); |
| 93 | + if (fusionPath && node.getAttribute('data-__neos-node-contextpath') === contextPath) { |
| 94 | + return fusionPath; |
| 95 | + } |
| 96 | + return findParentFusionPath(node.parentNode, contextPath); |
| 97 | + } |
| 98 | + return null; |
| 99 | +}; |
| 100 | + |
| 101 | +manifest('Flowpack.StructuredEditing:EditorEnvelope', {}, (globalRegistry, {routes, configuration, store}) => { |
| 102 | + const inlineEditorRegistry = globalRegistry.get('inlineEditors'); |
| 103 | + inlineEditorRegistry.set('Flowpack.StructuredEditing/EditorEnvelope', { |
| 104 | + bootstrap: () => null, |
| 105 | + createInlineEditor: config => { |
| 106 | + const domNode = config.propertyDomNode; |
| 107 | + const fusionPath = findParentFusionPath(domNode, config.contextPath); |
| 108 | + ReactDOM.render( |
| 109 | + (<InlineEditorEnvelope |
| 110 | + globalRegistry={globalRegistry} |
| 111 | + routes={routes} |
| 112 | + configuration={configuration} |
| 113 | + store={store} |
| 114 | + fusionPath={fusionPath} |
| 115 | + {...config} |
| 116 | + />), domNode); |
| 117 | + }, |
| 118 | + ToolbarComponent: () => null |
| 119 | + }); |
| 120 | +}); |
0 commit comments