Skip to content

Commit 8a99ea3

Browse files
Niklas Kieferfake-join[bot]
authored andcommitted
feat(playground): support components to be rendered flexible
Closes #292
1 parent a439235 commit 8a99ea3

File tree

4 files changed

+326
-16
lines changed

4 files changed

+326
-16
lines changed

packages/form-js-playground/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
],
6262
"devDependencies": {
6363
"css-loader": "^6.3.0",
64+
"min-dash": "^3.8.1",
6465
"min-dom": "^3.2.1",
6566
"rollup-plugin-css-only": "^3.1.0",
6667
"style-loader": "^3.3.0"

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

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ import { PlaygroundRoot } from './components/PlaygroundRoot';
88

99
/**
1010
* @typedef { {
11-
* container: Element,
11+
* container?: Element,
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
@@ -24,10 +24,15 @@ import './PlaygroundRoot.css';
2424
export function PlaygroundRoot(props) {
2525

2626
const {
27+
actions: actionsConfig = {},
2728
editor: editorConfig = {},
2829
emit
2930
} = props;
3031

32+
const {
33+
display: displayActions = true
34+
} = actionsConfig;
35+
3136
const {
3237
inlinePropertiesPanel = true
3338
} = editorConfig;
@@ -39,10 +44,12 @@ export function PlaygroundRoot(props) {
3944
const resultContainerRef = useRef();
4045
const propertiesPanelContainerRef = useRef();
4146

47+
const paletteRef = useRef();
4248
const formEditorRef = useRef();
4349
const formRef = useRef();
4450
const dataEditorRef = useRef();
4551
const resultViewRef = useRef();
52+
const propertiesPanelRef = useRef();
4653

4754
const [ showEmbed, setShowEmbed ] = useState(false);
4855

@@ -57,6 +64,12 @@ export function PlaygroundRoot(props) {
5764
// pipe to playground API
5865
useEffect(() => {
5966
props.onInit({
67+
attachDataContainer: (node) => dataEditorRef.current.attachTo(node),
68+
attachEditorContainer: (node) => formEditorRef.current.attachTo(node),
69+
attachFormContainer: (node) => formRef.current.attachTo(node),
70+
attachPaletteContainer: (node) => paletteRef.current.attachTo(node),
71+
attachPropertiesPanelContainer: (node) => propertiesPanelRef.current.attachTo(node),
72+
attachResultContainer: (node) => resultViewRef.current.attachTo(node),
6073
get: (name, strict) => formEditorRef.current.get(name, strict),
6174
setSchema: setInitialSchema
6275
});
@@ -89,6 +102,9 @@ export function PlaygroundRoot(props) {
89102
}
90103
});
91104

105+
paletteRef.current = formEditor.get('palette');
106+
propertiesPanelRef.current = formEditor.get('propertiesPanel');
107+
92108
formEditor.on('changed', () => {
93109
setSchema(formEditor.getSchema());
94110
});
@@ -178,19 +194,26 @@ export function PlaygroundRoot(props) {
178194
<div class="fjs-pgl-main">
179195

180196
<Section name="Form Definition">
181-
<Section.HeaderItem>
182-
<button
183-
class="fjs-pgl-button"
184-
title="Download form definition"
185-
onClick={ handleDownload }
186-
>Download</button>
187-
</Section.HeaderItem>
188-
<Section.HeaderItem>
189-
<button
190-
class="fjs-pgl-button"
191-
onClick={ showEmbedModal }
192-
>Embed</button>
193-
</Section.HeaderItem>
197+
198+
{
199+
displayActions && <Section.HeaderItem>
200+
<button
201+
class="fjs-pgl-button"
202+
title="Download form definition"
203+
onClick={ handleDownload }
204+
>Download</button>
205+
</Section.HeaderItem>
206+
}
207+
208+
{
209+
displayActions && <Section.HeaderItem>
210+
<button
211+
class="fjs-pgl-button"
212+
onClick={ showEmbedModal }
213+
>Embed</button>
214+
</Section.HeaderItem>
215+
}
216+
194217
<div ref={ editorContainerRef } class="fjs-pgl-form-container"></div>
195218
</Section>
196219
<Section name="Form Preview">

0 commit comments

Comments
 (0)