Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
46 changes: 44 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 All @@ -59,6 +72,7 @@ export default function Playground(options) {
data={ data }
onStateChanged={ (_state) => state = _state }
onInit={ _ref => ref = _ref }
emit={ emitter.emit }
{ ...rest }
/>,
container
Expand All @@ -85,7 +99,35 @@ export default function Playground(options) {
return ref.setSchema(schema);
};

this.get = function(name, strict) {
return ref.get(name, strict);
};

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);
});
}
60 changes: 46 additions & 14 deletions packages/form-js-playground/src/components/PlaygroundRoot.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,15 @@ import './PlaygroundRoot.css';
export function PlaygroundRoot(props) {

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

const {
display: displayActions = true
} = actionsConfig;

const {
inlinePropertiesPanel = true
} = editorConfig;
Expand All @@ -38,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 @@ -53,8 +61,16 @@ export function PlaygroundRoot(props) {

const [ resultData, setResultData ] = useState(props.data || {});

// 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 @@ -86,10 +102,19 @@ export function PlaygroundRoot(props) {
}
});

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

formEditor.on('changed', () => {
setSchema(formEditor.getSchema());
});

formEditor.on('formEditor.rendered', () => {

// notifiy interested parties after render
emit('formPlayground.rendered');
});

form.on('changed', event => {
setResultData(event.data);
});
Expand Down Expand Up @@ -169,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