Support more file types in save dialog override

This commit is contained in:
Christopher Willis-Ford 2019-01-08 11:23:55 -08:00
parent c0b8752fdc
commit 1113ba0c18
2 changed files with 104 additions and 13 deletions

102
src/main/FileFilters.js Normal file
View file

@ -0,0 +1,102 @@
const saveFilters = {
JPEG: {
name: 'JPEG Image',
extensions: ['jpg', 'jpeg']
},
MP3: {
name: 'MP3 Sound',
extensions: ['mp3']
},
PNG: {
name: 'PNG Image',
extensions: ['png']
},
SB: {
name: 'Scratch 1 Project',
extensions: ['sb']
},
SB2: {
name: 'Scratch 2 Project',
extensions: ['sb2']
},
SB3: {
name: 'Scratch 3 Project',
extensions: ['sb3']
},
Sprite2: {
name: 'Scratch 2 Sprite',
extensions: ['sprite2']
},
Sprite3: {
name: 'Scratch 3 Sprite',
extensions: ['sprite3']
},
SVG: {
name: 'SVG Image',
extensions: ['svg']
},
WAV: {
name: 'WAV Sound',
extensions: ['wav']
}
};
const loadFilters = {
...saveFilters,
AllBitmaps: {
name: 'All Bitmaps',
extensions: [
...saveFilters.JPEG.extensions,
...saveFilters.PNG.extensions
]
},
AllImages: {
name: 'All Images',
extensions: [
...saveFilters.JPEG.extensions,
...saveFilters.PNG.extensions,
...saveFilters.SVG.extensions
]
},
AllProjects: {
name: 'All Scratch Projects',
extensions: [
...saveFilters.SB3.extensions,
...saveFilters.SB2.extensions,
...saveFilters.SB.extensions
]
},
AllSounds: {
name: 'All Sounds',
extensions: [
...saveFilters.MP3.extensions,
...saveFilters.WAV.extensions
]
},
AllSprites: {
name: 'All Sprites',
extensions: [
...saveFilters.Sprite3.extensions,
...saveFilters.Sprite2.extensions
]
}
};
const filtersByExtension = Object.values(saveFilters).reduce((result, filter) => {
for (const extension of filter.extensions) {
result[extension] = filter;
}
return result;
}, {});
const getFilterForExtension = extNameNoDot =>
filtersByExtension[extNameNoDot] || {
name: `${extNameNoDot.toUpperCase()} Files`,
extensions: [extNameNoDot]
};
export {
saveFilters,
loadFilters,
getFilterForExtension
};

View file

@ -1,6 +1,7 @@
import {BrowserWindow, Menu, app, dialog} from 'electron';
import * as path from 'path';
import {format as formatUrl} from 'url';
import {getFilterForExtension} from './FileFilters';
import telemetry from './ScratchDesktopTelemetry';
import MacOSMenu from './MacOSMenu';
@ -10,13 +11,6 @@ telemetry.appWasOpened();
// const defaultSize = {width: 1096, height: 715}; // minimum
const defaultSize = {width: 1280, height: 800}; // good for MAS screenshots
const fileFilters = {
'.sb3': {
name: 'Scratch 3 Project',
extensions: ['sb3']
}
};
const isDevelopment = process.env.NODE_ENV !== 'production';
const createMainWindow = () => {
@ -69,12 +63,7 @@ const createMainWindow = () => {
const extNameNoDot = extName.replace(/^\./, '');
const options = {
defaultPath: path.join(app.getPath('documents'), baseName),
filters: [
fileFilters[extName] || {
name: `${extNameNoDot.toUpperCase()} Files`,
extensions: [extNameNoDot]
}
]
filters: [getFilterForExtension(extNameNoDot)]
};
const userChosenPath = dialog.showSaveDialog(window, options);
if (userChosenPath) {