admin管理员组文章数量:1333451
I use embed mode of diagrams and try to post a layout action. However, the layout event does not occur in the lifecycle.
=> How to correctly send the layout action to initialize the layout for my xml content?
I tried to send the message after ready event but got
INFO Error undefined undefined undefined Error: Not a diagram file (error on line 1 at column 1: Start tag expected, '<' not found)
=> After ready event, the message seems to be expected as xml, not as json.
I also tried to enable the layout action similar to the configure action with layout=1 as url parameter. But there does not seem such an url parameter.
window.addEventListener('message', function(event) { //...
if (event.data.includes('layout')) { var iframe = document.getElementById('drawio-iframe'); console.log('layout'); var layoutMessage = JSON.stringify({ action: 'layout', layouts: [ { type: 'horizontalFlow', animate: true } ] }); iframe.contentWindow.postMessage(layoutMessage, '*');})
Related:
- How to automatically apply flow layout when opening xml file in draw.io / diagrams?
- How to reproduce horizontal flow layout as custom layout?
I use embed mode of diagrams and try to post a layout action. However, the layout event does not occur in the lifecycle.
=> How to correctly send the layout action to initialize the layout for my xml content?
I tried to send the message after ready event but got
INFO Error undefined undefined undefined Error: Not a diagram file (error on line 1 at column 1: Start tag expected, '<' not found)
=> After ready event, the message seems to be expected as xml, not as json.
I also tried to enable the layout action similar to the configure action with layout=1 as url parameter. But there does not seem such an url parameter.
window.addEventListener('message', function(event) { //...
if (event.data.includes('layout')) { var iframe = document.getElementById('drawio-iframe'); console.log('layout'); var layoutMessage = JSON.stringify({ action: 'layout', layouts: [ { type: 'horizontalFlow', animate: true } ] }); iframe.contentWindow.postMessage(layoutMessage, '*');})
Related:
- https://www.drawio/doc/faq/embed-mode
- How to automatically apply flow layout when opening xml file in draw.io / diagrams?
- How to reproduce horizontal flow layout as custom layout?
1 Answer
Reset to default 0Just found out, that there are several modes for the protocol used by diagrams. That can be confusing for beginners.
In order to send layout actions, one needs to enable the json protocal first, using the url attribute proto=json
. Then the event lifecycle changes and instead of a "ready" event, an "init" event occurs.
After that "init" event it is possible to send load and layout actions.
function sendLayoutMessage() {{
// applies a horizontal flow layout
/* available layouts:
https://www.drawio/doc/faq/apply-layouts#custom-layouts
0 : "mxHierarchicalLayout"
1 : "mxCircleLayout"
2 : "mxCompactTreeLayout"
3 : "mxEdgeLabelLayout"
4 : "mxFastOrganicLayout"
5 : "mxParallelEdgeLayout"
6 : "mxPartitionLayout"
7 : "mxRadialTreeLayout"
8 : "mxStackLayout
*/
console.log('layout');
var iframe = document.getElementById('drawio-iframe');
var layoutMessage = JSON.stringify({{
action: 'layout',
layouts: [
{{
"layout": "mxHierarchicalLayout",
"config": {{
"orientation": "west",
"intraCellSpacing": 0.1,
"interRankCellSpacing": 0.1,
"interHierarchySpacing": 0.1,
"parallelEdgeSpacing": 0.1
}}
}}
]
}});
iframe.contentWindow.postMessage(layoutMessage, '*');
}}
Related follow-up question:
How to reproduce horizontal flow layout as custom layout?
本文标签: javascriptHow to send layout action to embeddiagramsnetStack Overflow
版权声明:本文标题:javascript - How to send layout action to embed.diagrams.net? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742351092a2458506.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论