Skip to content

Commit

Permalink
feat(playground): support components to be rendered flexible
Browse files Browse the repository at this point in the history
Closes #292
  • Loading branch information
Niklas Kiefer authored and fake-join[bot] committed Aug 29, 2022
1 parent a439235 commit 8a99ea3
Show file tree
Hide file tree
Showing 4 changed files with 326 additions and 16 deletions.
1 change: 1 addition & 0 deletions packages/form-js-playground/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
],
"devDependencies": {
"css-loader": "^6.3.0",
"min-dash": "^3.8.1",
"min-dom": "^3.2.1",
"rollup-plugin-css-only": "^3.1.0",
"style-loader": "^3.3.0"
Expand Down
41 changes: 39 additions & 2 deletions packages/form-js-playground/src/Playground.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ import { PlaygroundRoot } from './components/PlaygroundRoot';

/**
* @typedef { {
* container: Element,
* container?: Element,
* schema: any,
* data: any,
* editor?: { inlinePropertiesPanel: Boolean }
* actions?: { display: Boolean }
* } } FormPlaygroundOptions
*/

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

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

parent.appendChild(container);
if (parent) {
parent.appendChild(container);
}

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

const withRef = function(fn) {
return function(...args) {
if (!ref) {
throw new Error('Playground is not initialized.');
}

fn(...args);
};
};

container.addEventListener('dragover', handleDrop);

render(
Expand Down Expand Up @@ -93,4 +106,28 @@ export default function Playground(options) {
this.destroy = function() {
this.emit('destroy');
};

this.attachEditorContainer = withRef(function(node) {
return ref.attachEditorContainer(node);
});

this.attachPreviewContainer = withRef(function(node) {
return ref.attachFormContainer(node);
});

this.attachDataContainer = withRef(function(node) {
return ref.attachDataContainer(node);
});

this.attachResultContainer = withRef(function(node) {
return ref.attachResultContainer(node);
});

this.attachPaletteContainer = withRef(function(node) {
return ref.attachPaletteContainer(node);
});

this.attachPropertiesPanelContainer = withRef(function(node) {
return ref.attachPropertiesPanelContainer(node);
});
}
49 changes: 36 additions & 13 deletions packages/form-js-playground/src/components/PlaygroundRoot.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,15 @@ import './PlaygroundRoot.css';
export function PlaygroundRoot(props) {

const {
actions: actionsConfig = {},
editor: editorConfig = {},
emit
} = props;

const {
display: displayActions = true
} = actionsConfig;

const {
inlinePropertiesPanel = true
} = editorConfig;
Expand All @@ -39,10 +44,12 @@ export function PlaygroundRoot(props) {
const resultContainerRef = useRef();
const propertiesPanelContainerRef = useRef();

const paletteRef = useRef();
const formEditorRef = useRef();
const formRef = useRef();
const dataEditorRef = useRef();
const resultViewRef = useRef();
const propertiesPanelRef = useRef();

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

Expand All @@ -57,6 +64,12 @@ export function PlaygroundRoot(props) {
// pipe to playground API
useEffect(() => {
props.onInit({
attachDataContainer: (node) => dataEditorRef.current.attachTo(node),
attachEditorContainer: (node) => formEditorRef.current.attachTo(node),
attachFormContainer: (node) => formRef.current.attachTo(node),
attachPaletteContainer: (node) => paletteRef.current.attachTo(node),
attachPropertiesPanelContainer: (node) => propertiesPanelRef.current.attachTo(node),
attachResultContainer: (node) => resultViewRef.current.attachTo(node),
get: (name, strict) => formEditorRef.current.get(name, strict),
setSchema: setInitialSchema
});
Expand Down Expand Up @@ -89,6 +102,9 @@ export function PlaygroundRoot(props) {
}
});

paletteRef.current = formEditor.get('palette');
propertiesPanelRef.current = formEditor.get('propertiesPanel');

formEditor.on('changed', () => {
setSchema(formEditor.getSchema());
});
Expand Down Expand Up @@ -178,19 +194,26 @@ export function PlaygroundRoot(props) {
<div class="fjs-pgl-main">

<Section name="Form Definition">
<Section.HeaderItem>
<button
class="fjs-pgl-button"
title="Download form definition"
onClick={ handleDownload }
>Download</button>
</Section.HeaderItem>
<Section.HeaderItem>
<button
class="fjs-pgl-button"
onClick={ showEmbedModal }
>Embed</button>
</Section.HeaderItem>

{
displayActions && <Section.HeaderItem>
<button
class="fjs-pgl-button"
title="Download form definition"
onClick={ handleDownload }
>Download</button>
</Section.HeaderItem>
}

{
displayActions && <Section.HeaderItem>
<button
class="fjs-pgl-button"
onClick={ showEmbedModal }
>Embed</button>
</Section.HeaderItem>
}

<div ref={ editorContainerRef } class="fjs-pgl-form-container"></div>
</Section>
<Section name="Form Preview">
Expand Down
Loading

0 comments on commit 8a99ea3

Please sign in to comment.