Skip to content

Commit 84ac01e

Browse files
author
Niklas Kiefer
committed
feat(playground): support components to be rendered flexible
Closes #292
1 parent d9e416c commit 84ac01e

File tree

4 files changed

+289
-15
lines changed

4 files changed

+289
-15
lines changed

packages/form-js-playground/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
],
5454
"devDependencies": {
5555
"css-loader": "^6.3.0",
56+
"min-dash": "^3.8.1",
5657
"min-dom": "^3.2.1",
5758
"rollup-plugin-css-only": "^3.1.0",
5859
"style-loader": "^3.3.0"

packages/form-js-playground/src/Playground.js

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { PlaygroundRoot } from './components/PlaygroundRoot';
1212
* schema: any,
1313
* data: any,
1414
* editor?: { inlinePropertiesPanel: Boolean }
15+
* actions?: { display: Boolean }
1516
* } } FormPlaygroundOptions
1617
*/
1718

@@ -36,7 +37,9 @@ export default function Playground(options) {
3637

3738
container.classList.add('fjs-pgl-parent');
3839

39-
parent.appendChild(container);
40+
if (parent) {
41+
parent.appendChild(container);
42+
}
4043

4144
const handleDrop = fileDrop('Drop a form file', function(files) {
4245
const file = files[0];
@@ -51,6 +54,16 @@ export default function Playground(options) {
5154
}
5255
});
5356

57+
const withRef = function(fn) {
58+
return function(...args) {
59+
if (!ref) {
60+
throw new Error('Playground is not initialized.');
61+
}
62+
63+
fn(...args);
64+
};
65+
};
66+
5467
container.addEventListener('dragover', handleDrop);
5568

5669
render(
@@ -93,4 +106,28 @@ export default function Playground(options) {
93106
this.destroy = function() {
94107
this.emit('destroy');
95108
};
109+
110+
this.attachEditorContainer = withRef(function(node) {
111+
return ref.attachEditorContainer(node);
112+
});
113+
114+
this.attachPreviewContainer = withRef(function(node) {
115+
return ref.attachFormContainer(node);
116+
});
117+
118+
this.attachDataContainer = withRef(function(node) {
119+
return ref.attachDataContainer(node);
120+
});
121+
122+
this.attachResultContainer = withRef(function(node) {
123+
return ref.attachResultContainer(node);
124+
});
125+
126+
this.attachPaletteContainer = withRef(function(node) {
127+
return ref.attachPaletteContainer(node);
128+
});
129+
130+
this.attachPropertiesPanelContainer = withRef(function(node) {
131+
return ref.attachPropertiesPanelContainer(node);
132+
});
96133
}

packages/form-js-playground/src/components/PlaygroundRoot.js

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,15 @@ import './PlaygroundRoot.css';
2121
export function PlaygroundRoot(props) {
2222

2323
const {
24+
actions: actionsConfig = {},
2425
editor: editorConfig = {},
2526
emit
2627
} = props;
2728

29+
const {
30+
display: displayActions = true
31+
} = actionsConfig;
32+
2833
const {
2934
inlinePropertiesPanel = true
3035
} = editorConfig;
@@ -36,10 +41,12 @@ export function PlaygroundRoot(props) {
3641
const resultContainerRef = useRef();
3742
const propertiesPanelContainerRef = useRef();
3843

44+
const paletteRef = useRef();
3945
const formEditorRef = useRef();
4046
const formRef = useRef();
4147
const dataEditorRef = useRef();
4248
const resultViewRef = useRef();
49+
const propertiesPanelRef = useRef();
4350

4451
const [ showEmbed, setShowEmbed ] = useState(false);
4552

@@ -54,6 +61,12 @@ export function PlaygroundRoot(props) {
5461
// pipe to playground API
5562
useEffect(() => {
5663
props.onInit({
64+
attachDataContainer: (node) => dataEditorRef.current.attachTo(node),
65+
attachEditorContainer: (node) => formEditorRef.current.attachTo(node),
66+
attachFormContainer: (node) => formRef.current.attachTo(node),
67+
attachPaletteContainer: (node) => paletteRef.current.attachTo(node),
68+
attachPropertiesPanelContainer: (node) => propertiesPanelRef.current.attachTo(node),
69+
attachResultContainer: (node) => resultViewRef.current.attachTo(node),
5770
get: (name, strict) => formEditorRef.current.get(name, strict),
5871
setSchema: setInitialSchema
5972
});
@@ -86,6 +99,9 @@ export function PlaygroundRoot(props) {
8699
}
87100
});
88101

102+
paletteRef.current = formEditor.get('palette');
103+
propertiesPanelRef.current = formEditor.get('propertiesPanel');
104+
89105
formEditor.on('changed', () => {
90106
setSchema(formEditor.getSchema());
91107
});
@@ -175,19 +191,26 @@ export function PlaygroundRoot(props) {
175191
<div class="fjs-pgl-main">
176192

177193
<Section name="Form Definition">
178-
<Section.HeaderItem>
179-
<button
180-
class="fjs-pgl-button"
181-
title="Download form definition"
182-
onClick={ handleDownload }
183-
>Download</button>
184-
</Section.HeaderItem>
185-
<Section.HeaderItem>
186-
<button
187-
class="fjs-pgl-button"
188-
onClick={ showEmbedModal }
189-
>Embed</button>
190-
</Section.HeaderItem>
194+
195+
{
196+
displayActions && <Section.HeaderItem>
197+
<button
198+
class="fjs-pgl-button"
199+
title="Download form definition"
200+
onClick={ handleDownload }
201+
>Download</button>
202+
</Section.HeaderItem>
203+
}
204+
205+
{
206+
displayActions && <Section.HeaderItem>
207+
<button
208+
class="fjs-pgl-button"
209+
onClick={ showEmbedModal }
210+
>Embed</button>
211+
</Section.HeaderItem>
212+
}
213+
191214
<div ref={ editorContainerRef } class="fjs-pgl-form-container"></div>
192215
</Section>
193216
<Section name="Form Preview">

0 commit comments

Comments
 (0)