Skip to content

Commit

Permalink
Merge pull request #939 from 3DStreet/more-save-as
Browse files Browse the repository at this point in the history
More save as
  • Loading branch information
kfarr authored Nov 19, 2024
2 parents 977aa44 + 38b41ad commit c4bfdfc
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
createElementsForScenesFromJSON,
fileJSON,
inputStreetmix
} from '../../../lib/toolbar';
} from '@/editor/lib/SceneUtils.js';
import { getCommunityScenes, getUserScenes } from '../../../api/scene';
import { Load24Icon, Loader, Upload24Icon } from '../../../icons';
import { signIn } from '../../../api';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import { signIn } from '../../../api';
import { useAuthContext } from '../../../contexts';
import { Copy32Icon, Save24Icon } from '../../../icons';
import { Button, Dropdown, Input } from '../../components';
import Toolbar from '../../scenegraph/Toolbar';
import Modal from '../Modal.jsx';
import posthog from 'posthog-js';
import { saveBlob } from '../../../lib/utils';
import { uploadThumbnailImage, saveScreenshot } from '../../../api/scene';
import useStore from '@/store';
import { convertToObject } from '@/editor/lib/SceneUtils';

const filterHelpers = (scene, visible) => {
scene.traverse((o) => {
Expand Down Expand Up @@ -80,7 +80,12 @@ function ScreenshotModal() {
{
value: '.3dstreet.json',
label: '.3dstreet.json',
onClick: Toolbar.convertToObject
onClick: () => {
posthog.capture('convert_to_json_clicked', {
scene_id: STREET.utils.getCurrentSceneId()
});
convertToObject();
}
}
];

Expand Down
53 changes: 3 additions & 50 deletions src/editor/components/scenegraph/Toolbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,34 +87,6 @@ export default class Toolbar extends Component {
}
};

static convertToObject = () => {
try {
posthog.capture('convert_to_json_clicked', {
scene_id: STREET.utils.getCurrentSceneId()
});
const entity = document.getElementById('street-container');

const data = STREET.utils.convertDOMElToObject(entity);

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

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

link.click();
link.remove();
STREET.notify.successMessage('3DStreet JSON file saved successfully.');
} catch (error) {
STREET.notify.errorMessage(
`Error trying to save 3DStreet JSON file. Error: ${error}`
);
console.error(error);
}
};

cloudSaveHandlerWithImageUpload = async (doSaveAs) => {
this.makeScreenshot();
const currentSceneId = await this.cloudSaveHandler({ doSaveAs });
Expand All @@ -127,6 +99,7 @@ export default class Toolbar extends Component {
newHandler = () => {
posthog.capture('new_scene_clicked');
AFRAME.INSPECTOR.selectEntity(null);
useStore.getState().newScene();
STREET.utils.newScene();
AFRAME.scenes[0].emit('newScene');
};
Expand Down Expand Up @@ -180,7 +153,7 @@ export default class Toolbar extends Component {
// we want to save, so if we *still* have no sceneID at this point, then create a new one
if (!currentSceneId || !!doSaveAs) {
// ask user for scene title here currentSceneTitle
let newSceneTitle = prompt('Scene Title:', currentSceneTitle);
let newSceneTitle = prompt('Scene Title:', currentSceneTitle || '');

if (newSceneTitle) {
currentSceneTitle = newSceneTitle;
Expand Down Expand Up @@ -221,6 +194,7 @@ export default class Toolbar extends Component {

// Change the hash URL without reloading
window.location.hash = `#/scenes/${currentSceneId}.json`;
this.toggleSaveActionState();
this.setState({ savedScene: true });
this.setSavedSceneFalse();

Expand Down Expand Up @@ -283,33 +257,12 @@ export default class Toolbar extends Component {
screenshotEl.setAttribute('screentock', 'takeScreenshot', true);
};

toggleScenePlaying = () => {
if (this.state.isPlaying) {
AFRAME.scenes[0].pause();
this.setState((prevState) => ({ ...prevState, isPlaying: false }));
Events.emit('sceneplayingtoggle', false);
AFRAME.scenes[0].isPlaying = true;
document.getElementById('aframeInspectorMouseCursor').play();
return;
}
AFRAME.scenes[0].isPlaying = false;
AFRAME.scenes[0].play();
this.setState((prevState) => ({ ...prevState, isPlaying: true }));
Events.emit('sceneplayingtoggle', true);
};

toggleSaveActionState = () => {
this.setState((prevState) => ({
isSaveActionActive: !prevState.isSaveActionActive
}));
};

toggleLoadActionState = () => {
this.setState((prevState) => ({
isLoadActionActive: !prevState.isLoadActionActive
}));
};

render() {
const isEditor = !!this.state.inspectorEnabled;
return (
Expand Down
25 changes: 25 additions & 0 deletions src/editor/lib/toolbar.js → src/editor/lib/SceneUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,28 @@ export function fileJSON(event) {

reader.readAsText(event.target.files[0]);
}

export function convertToObject() {
try {
const entity = document.getElementById('street-container');

const data = STREET.utils.convertDOMElToObject(entity);

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

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

link.click();
link.remove();
STREET.notify.successMessage('3DStreet JSON file saved successfully.');
} catch (error) {
STREET.notify.errorMessage(
`Error trying to save 3DStreet JSON file. Error: ${error}`
);
console.error(error);
}
}
2 changes: 2 additions & 0 deletions src/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ const useStore = create(
setSceneId: (newSceneId) => set({ sceneId: newSceneId }),
sceneTitle: null,
setSceneTitle: (newSceneTitle) => set({ sceneTitle: newSceneTitle }),
newScene: () =>
set({ sceneId: null, sceneTitle: null, authorId: null }),
authorId: null,
setAuthorId: (newAuthorId) => set({ authorId: newAuthorId }),
modal: firstModal(),
Expand Down

0 comments on commit c4bfdfc

Please sign in to comment.