Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add JSON export functionality for grid configuration #44

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
34 changes: 33 additions & 1 deletion client/src/components/Element.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { loadElementSchemaValue } from 'state/editor/loadElementSchemaValue';
import { elementTypeType } from 'types/elementTypeType';
import { elementType } from 'types/elementType';
import { elementDragSource, isOverTop } from 'lib/dragHelpers';
import Modal from 'react-modal';

/**
* The Element component used in the context of an ElementEditor shows the summary
Expand All @@ -35,6 +36,8 @@ class Element extends Component {
this.updateFormTab = this.updateFormTab.bind(this);
this.handleChangeSize = this.handleChangeSize.bind(this);
this.handleChangeOffset = this.handleChangeOffset.bind(this);
this.handleExportJSON = this.handleExportJSON.bind(this);
this.closeModal = this.closeModal.bind(this);

this.state = {
previewExpanded: false,
Expand All @@ -43,6 +46,8 @@ class Element extends Component {
childRenderingError: false,
size: props.element.blockSchema.grid.column.size,
offset: props.element.blockSchema.grid.column.offset,
modalIsOpen: false,
jsonRepresentation: '',
};
}

Expand Down Expand Up @@ -243,6 +248,21 @@ class Element extends Component {
});
}

handleExportJSON() {
const { element } = this.props;
const jsonRepresentation = JSON.stringify(element.blockSchema.grid, null, 2);
this.setState({
modalIsOpen: true,
jsonRepresentation,
});
}

closeModal() {
this.setState({
modalIsOpen: false,
});
}

render() {
const {
element,
Expand All @@ -260,7 +280,7 @@ class Element extends Component {
onDragEnd,
} = this.props;

const { childRenderingError, previewExpanded } = this.state;
const { childRenderingError, previewExpanded, modalIsOpen, jsonRepresentation } = this.state;

if (!element.id) {
return null;
Expand Down Expand Up @@ -332,7 +352,19 @@ class Element extends Component {
handleChangeOffset={this.handleChangeOffset}
/>
}

<button onClick={this.handleExportJSON}>Export JSON</button>
</div>

<Modal
isOpen={modalIsOpen}
onRequestClose={this.closeModal}
contentLabel="JSON Representation"
>
<h2>JSON Representation</h2>
<pre>{jsonRepresentation}</pre>
<button onClick={this.closeModal}>Close</button>
</Modal>
</div>);

if (!previewExpanded) {
Expand Down
6 changes: 5 additions & 1 deletion client/src/components/ElementEditor/Toolbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ class Toolbar extends PureComponent {
AddBlockToTopButton,
elementTypes,
areaId,
connectDropTarget } = this.props;
connectDropTarget,
onExportJSON
} = this.props;
return connectDropTarget(
<div className="element-editor__toolbar">
<AddBlockToTopButton
Expand All @@ -23,6 +25,7 @@ class Toolbar extends PureComponent {
elementTypes={elementTypes}
areaId={areaId}
/>
<button onClick={onExportJSON}>Export JSON</button>
</div>
);
}
Expand All @@ -37,6 +40,7 @@ Toolbar.propTypes = {
connectDropTarget: PropTypes.func.isRequired,
onDragOver: PropTypes.func, // eslint-disable-line react/no-unused-prop-types
onDragDrop: PropTypes.func, // eslint-disable-line react/no-unused-prop-types
onExportJSON: PropTypes.func.isRequired,
};

const toolbarTarget = {
Expand Down
25 changes: 25 additions & 0 deletions src/Extensions/BaseElementExtension.php
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -288,4 +288,29 @@ public function getAnchorTitle(): string
{
return $this->owner->singular_name() . '_' . $this->owner->getTitle() . '_' . $this->owner->ID;
}

public function generateGridJSON(): string
{
$gridData = [
'sizeXS' => $this->owner->SizeXS,
'sizeSM' => $this->owner->SizeSM,
'sizeMD' => $this->owner->SizeMD,
'sizeLG' => $this->owner->SizeLG,
'sizeXL' => $this->owner->SizeXL,
'offsetXS' => $this->owner->OffsetXS,
'offsetSM' => $this->owner->OffsetSM,
'offsetMD' => $this->owner->OffsetMD,
'offsetLG' => $this->owner->OffsetLG,
'offsetXL' => $this->owner->OffsetXL,
'visibilityXS' => $this->owner->VisibilityXS,
'visibilitySM' => $this->owner->VisibilitySM,
'visibilityMD' => $this->owner->VisibilityMD,
'visibilityLG' => $this->owner->VisibilityLG,
'visibilityXL' => $this->owner->VisibilityXL,
'titleClass' => $this->owner->TitleClass,
'titleTag' => $this->owner->TitleTag,
];

return json_encode($gridData, JSON_PRETTY_PRINT);
}
}
11 changes: 11 additions & 0 deletions src/Extensions/ElementalAreaExtension.php
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,15 @@ public function ElementControllersWithRows(): ?ArrayList

return $this->controllers;
}

public function generateGridJSON(): string
{
$gridData = [];

foreach ($this->controllers as $controller) {
$gridData[] = $controller->getElement()->toMap();
}

return json_encode($gridData, JSON_PRETTY_PRINT);
}
}
11 changes: 11 additions & 0 deletions src/Models/ElementRow.php
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,15 @@ public function getContainerClasses(): string

return implode(' ', $classes);
}

public function generateGridJSON(): string
{
$gridData = [
'isFluid' => $this->IsFluid,
'customSectionClass' => $this->CustomSectionClass,
'extraClass' => $this->ExtraClass,
];

return json_encode($gridData, JSON_PRETTY_PRINT);
}
}