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

Editor: Added Save and Open #28351

Merged
merged 2 commits into from
May 15, 2024
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
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

option = 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( option );

// 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