Skip to content

Commit

Permalink
add save/open feature
Browse files Browse the repository at this point in the history
  • Loading branch information
ycw committed May 12, 2024
1 parent 12db963 commit 83cad5e
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 1 deletion.
80 changes: 80 additions & 0 deletions editor/js/Menubar.File.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,86 @@ function MenubarFile( editor ) {

}


// Save

const fileSaveProject = new UIRow()
.addClass( 'option' )
.setTextContent( strings.getKey( 'menubar/file/save' ) )
.onClick( function () {

const json = editor.toJSON();
const blob = new Blob( [ JSON.stringify( json ) ], { type: 'application/json' } );
editor.utils.save( blob, 'project.json' );

} );

options.add( fileSaveProject );

// Open

const openProjectForm = document.createElement( 'form' );
openProjectForm.style.display = 'none';
document.body.appendChild( openProjectForm );

const openProjectInput = document.createElement( 'input' );
openProjectInput.multiple = false;
openProjectInput.type = 'file';
openProjectInput.accept = '.json';
openProjectInput.addEventListener( 'change', async function () {

const file = openProjectInput.files[ 0 ];

if ( file === undefined ) return;

try {

const json = JSON.parse( await file.text() );

async function onEditorCleared() {

await editor.fromJSON( json );

editor.signals.editorCleared.remove( onEditorCleared );

}

editor.signals.editorCleared.add( onEditorCleared );

editor.clear();

} catch ( e ) {

alert( strings.getKey( 'prompt/file/failedToOpenProject' ) );
console.error( e );

} finally {

form.reset();

}

} );

openProjectForm.appendChild( openProjectInput );

option = new UIRow()
.addClass( 'option' )
.setTextContent( strings.getKey( 'menubar/file/open' ) )
.onClick( function () {

if ( confirm( strings.getKey( 'prompt/file/open' ) ) ) {

openProjectInput.click();

}

} );

options.add( option );

//

options.add( new UIHorizontalRule() );

// Import
Expand Down
13 changes: 12 additions & 1 deletion editor/js/Strings.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ function Strings( config ) {
en: {

'prompt/file/open': 'Any unsaved data will be lost. Are you sure?',
'prompt/file/failedToOpenProject': 'Failed to open project!',
'prompt/file/export/noMeshSelected': 'No Mesh selected!',
'prompt/file/export/noObjectSelected': 'No Object selected!',
'prompt/script/remove': 'Are you sure?',
Expand Down Expand Up @@ -45,6 +46,8 @@ function Strings( config ) {
'menubar/file/newProject/Particles': 'Particles',
'menubar/file/newProject/Pong': 'Pong',
'menubar/file/newProject/Shaders': 'Shaders',
'menubar/file/save': 'Save',
'menubar/file/open': 'Open',
'menubar/file/import': 'Import',
'menubar/file/export': 'Export',

Expand Down Expand Up @@ -391,6 +394,7 @@ function Strings( config ) {
fr: {

'prompt/file/open': 'Toutes les données non enregistrées seront perdues Êtes-vous sûr ?',
'prompt/file/failedToOpenProject': 'Échec de l\'ouverture du projet !',
'prompt/file/export/noMeshSelected': 'Aucun maillage sélectionné !',
'prompt/file/export/noObjectSelected': 'Aucun objet sélectionné !',
'prompt/script/remove': 'Es-tu sûr?',
Expand Down Expand Up @@ -429,7 +433,8 @@ function Strings( config ) {
'menubar/file/newProject/Particles': 'Particles',
'menubar/file/newProject/Pong': 'Pong',
'menubar/file/newProject/Shaders': 'Shaders',

'menubar/file/save': 'Save',
'menubar/file/open': 'Open',
'menubar/file/import': 'Importer',
'menubar/file/export': 'Exporter',

Expand Down Expand Up @@ -776,6 +781,7 @@ function Strings( config ) {
zh: {

'prompt/file/open': '您确定吗?未保存的数据将会丢失。',
'prompt/file/failedToOpenProject': '无法打开项目!',
'prompt/file/export/noMeshSelected': '未选择网格!',
'prompt/file/export/noObjectSelected': '未选择对象!',
'prompt/script/remove': '你确定吗?',
Expand Down Expand Up @@ -814,6 +820,8 @@ function Strings( config ) {
'menubar/file/newProject/Particles': '粒子',
'menubar/file/newProject/Pong': '乒乓球',
'menubar/file/newProject/Shaders': '着色器',
'menubar/file/save': '保存',
'menubar/file/open': '打开',
'menubar/file/import': '导入',
'menubar/file/export': '导出',

Expand Down Expand Up @@ -1160,6 +1168,7 @@ function Strings( config ) {
ja: {

'prompt/file/open': '保存されていないデータは失われます。 本気ですか?',
'prompt/file/failedToOpenProject': 'プロジェクトを開くことができませんでした!',
'prompt/file/export/noMeshSelected': 'メッシュが選択されていません!',
'prompt/file/export/noObjectSelected': 'オブジェクトが選択されていません!',
'prompt/script/remove': '本気ですか?',
Expand Down Expand Up @@ -1198,6 +1207,8 @@ function Strings( config ) {
'menubar/file/newProject/Particles': 'パーティクル',
'menubar/file/newProject/Pong': 'ピンポン',
'menubar/file/newProject/Shaders': 'シェーダー',
'menubar/file/save': '保存',
'menubar/file/open': '開く',
'menubar/file/import': 'インポート',
'menubar/file/export': 'エクスポート',

Expand Down

0 comments on commit 83cad5e

Please sign in to comment.