mhsdesign/liveinspectorjsevents
Composer 安装命令:
composer require mhsdesign/liveinspectorjsevents
包简介
Send JsEvents to the iframe on Inspector changes
README 文档
README
Neos Ui Plugin for Inspector change events in the iframe.
Demo
demo-live-change-neos.mp4
Install
composer require "mhsdesign/liveinspectorjsevents:~2.0.0"
Tutorial for a self made implementation:
Change the class of a spacer to its property value:
your yaml:
A basic (standalone) nodeType yaml config with a select box
'MhsDesign.LiveInspectorDemo:Content.Spacer': superTypes: 'Neos.Neos:Content': true ui: icon: 'icon-internet-explorer' label: 'Example' # this will remove the Neos not inline editable overlay. inlineEditable: true inspector: groups: settings: label: 'Settings' properties: height: type: string ui: # not need to explicitly state it since its the default: # reloadIfChanged: false label: 'Height' inspector: group: settings editor: 'Neos.Neos/Inspector/Editors/SelectBoxEditor' editorOptions: allowEmpty: true values: # your css classes as key. height-sm: label: 'Small' height-md: label: 'Medium' height-lg: label: 'Large'
your fusion:
prototype(MhsDesign.LiveInspectorDemo:Content.Spacer) < prototype(Neos.Neos:ContentComponent) {
height = ${q(node).property('height')}
renderer = afx`
<div class={['spacer', props.height]}></div>
`
}
your js:
// all changers will be registered here: const changer = {} changer['MhsDesign.LiveInspectorDemo:Content.Spacer'] = (node, property) => { const {name, updated, previous} = property; /** type HTMLElement */ const el = neos.guestFrame.findElementByNode(node); switch (name) { case 'height': // sometimes the ui wraps an div around the html - sometimes not. const spacerDiv = querySelectClosest(el, '.spacer') if (previous !== '') { spacerDiv.classList.remove(previous) } if (updated !== '') { spacerDiv.classList.add(updated) } } } // call the changer defined for a node by nodeType const updateNode = (node, property) => { if (typeof changer[node.nodeType] !== "undefined") { changer[node.nodeType](node, property); } // alternative: // switch (node.nodeType) { // case 'MhsDesign.LiveInspectorDemo:Content.Spacer': // changeSpacer(node, property) // } } // register to the events document.addEventListener('Neos.NodeCommit', (event) => { const {node, property} = event.detail; updateNode(node, property) }) document.addEventListener('Neos.NodeDiscard', (event) => { const {node, properties} = event.detail; properties.forEach(property => { updateNode(node, property) }) }) // helper const querySelectClosest = (element, selector) => { if (element.matches(selector)) { return element; } return element.querySelector(selector) }
Usage
listen to the events (in the iframe):
document.addEventListener('Neos.NodeCommit', (event) => { const {node, property} = event.detail; // the updated property value and also the previous. // {name: 'title', updated: 'abcd', previous: 'abc'} console.log(property); // experimental: see below: console.log(neos.guestFrame.findElementByNode(node)); }) document.addEventListener('Neos.NodeDiscard', (event) => { const {node, properties} = event.detail; // all reset properties in a list. // [{name: 'title', updated: 'abc', previous: 'abcd'}] console.log(properties); })
A Javascript node object in the Neos UI looks like:
node = { "identifier": "99257f0c-70f0-405b-a82b-fa8e375c23fb", "contextPath": "/sites/root/main/node-l461lxh2i1a77", "nodeType": "Vendor.Site:Content.Heading", ... "properties": { // also some private "_" properties ... like "_hidden" ... "title": "my String Old" } }
Get the dom element of the corresponding node:
The following functionality or way of handling this is experimental, and could eventually change.
the global window.neos object is extended by this package and exposes under guestFrame this function, which makes it possible to get the dom element by node object:
neos.guestFrame.findElementByNode(node)
Under the hood it does something like: (But try to avoid using the following snippet directly as it uses eventually purely internal knowledge.)
const findElementByNode = (node) => { const {contextPath} = node; // https://github.com/neos/neos-ui/blob/7ede460ec1bb8dd4455fc636b875c137d112e89d/packages/neos-ui-guest-frame/src/dom.js#L76 return document.querySelector(`[data-__neos-node-contextpath="${contextPath}"]`); }
Internals
More dev notes (about internals, not so much about the events above):
it sometimes helps to have the Redux DevTools installed: https://chrome.google.com/webstore/detail/redux-devtools/lmhkpmbekcpmknklioeibfkpmmfibljd?hl=de
The API from the Neos UI.
// the commit action (when you change a property of a node) looks like: commitAction = { "type": "@neos/neos-ui/UI/Inspector/COMMIT", "payload": { "propertyId": "title", "value": "my String", ... "focusedNode": node, // we get something like in the node above } } } // when we dicard the changes, we wont know directly wich changes where made before. discardAction = { "type": "@neos/neos-ui/UI/Inspector/DISCARD", "payload": { "focusedNodeContextPath": "/sites/root/main/node-l461lxh2i1a77@user-mhs" // we can get all the node details from the CR via: // selectors.CR.Nodes.nodeByContextPath(state)(focusedNodeContextPath) } } // good to know, how to alway get the current node: const state = yield select(); const focusedNode = selectors.CR.Nodes.focusedSelector(state) // or const focusedNode = yield select(selectors.CR.Nodes.focusedSelector);
mhsdesign/liveinspectorjsevents 适用场景与选型建议
mhsdesign/liveinspectorjsevents 是一款 基于 JavaScript 开发的 Composer 扩展包,目前已累计 4.36k 次下载、GitHub Stars 达 5, 最近一次更新时间为 2021 年 09 月 05 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 mhsdesign/liveinspectorjsevents 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mhsdesign/liveinspectorjsevents 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 4.36k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 5
- 点击次数: 7
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: GPL-3.0
- 更新时间: 2021-09-05