Skip to content
This repository has been archived by the owner on Jun 10, 2024. It is now read-only.

Issue#60 new save button #71

Closed
wants to merge 3 commits into from
Closed
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
100 changes: 98 additions & 2 deletions src/components/scenegraph/Toolbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,85 @@ function slugify(text) {
.replace(/-+$/, ''); // Trim - from end of text
}

function getElementData(entity) {
const elementTree = getAttributes(entity);
const children = entity.childNodes;
if (children.length) {
elementTree['children'] = [];
for (const child of children) {
if (child.nodeType === Node.ELEMENT_NODE) {
elementTree['children'].push(getElementData(child));
}
}
}
return elementTree;
}

function getAttributes(entity) {
const elemObj = {};
elemObj['element'] = entity.tagName.toLowerCase();

if (entity.id) {
elemObj['id'] = entity.id;
}
if (entity.className) {
// convert from DOMTokenList to Array
elemObj['class'] = Array.from(entity.classList);
}
if (entity.getAttribute('mixin')) {
elemObj['mixin'] = entity.getAttribute('mixin');
}

const entityComponents = entity.components;
if (entityComponents) {
elemObj['components'] = {};
for (const componentName in entityComponents) {
const modifiedProperty = getModifiedProperties(entity, componentName);
if (!isEmpty(modifiedProperty)) {
elemObj['components'][componentName] = modifiedProperty;
}
}
}
return elemObj;
}

function isEmpty(object) {
return object ? Object.keys(object).length === 0 : true;
}

function getModifiedProperties(entity, componentName) {
const data = entity.components[componentName].data;
const defaultData = entity.components[componentName].schema;

// If its single-property like position, rotation, etc
if (isSingleProperty(defaultData)) {
const defaultValue = defaultData.default;
const currentValue = data;
if ((currentValue || defaultValue) && currentValue !== defaultValue) {
return data;
}
}

const diff = {};
for (const key in data) {
const defaultValue = defaultData[key].default;
const currentValue = data[key];

// Some parameters could be null and '' like mergeTo
if (
(currentValue || defaultValue) &&
!AFRAME.utils.deepEqual(currentValue, defaultValue)
) {
diff[key] = data[key];
}
}
return diff;
}

function isSingleProperty(schema) {
return AFRAME.schema.isSingleProperty(schema);
}

/**
* Tools and actions.
*/
Expand Down Expand Up @@ -104,6 +183,23 @@ export default class Toolbar extends Component {
isSaveActionActive: !this.state.isSaveActionActive
}));

convertToObject = () => {
const entity = document.getElementById('streets');

const data = getElementData(entity);

const jsonString = `data:text/json;chatset=utf-8,${encodeURIComponent(
JSON.stringify({ data: data })
)}`;

const link = document.createElement('a');
link.href = jsonString;
link.download = 'data.json';

link.click();
link.remove();
};

render() {
// const watcherClassNames = classnames({
// button: true,
Expand Down Expand Up @@ -144,8 +240,8 @@ export default class Toolbar extends Component {
<button type={'button'} onClick={this.exportSceneToGLTF}>
glTF 3D Model
</button>
<button type={'button'} onClick={this.makeScreenshot}>
PNG Screenshot
<button type={'button'} onClick={this.convertToObject}>
3DStreet JSON
</button>
<button
type={'button'}
Expand Down