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

feat: open images from dialog menu #7

Merged
merged 2 commits into from
Dec 4, 2021
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
25 changes: 24 additions & 1 deletion src/main/menu.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const { app, dialog, BrowserWindow, shell } = require('electron')
const { version, author } = require('../../package.json')
const os = require('os')
const { readFileOrDir } = require('./utils')
const { ImageOptimizer } = require('./image-compressor')

const isMac = process.platform === 'darwin'
const year = new Date().getFullYear()
Expand Down Expand Up @@ -66,6 +68,27 @@ module.exports = context => {
submenu: createImageOptimizerMenu(context)
}

const file = {
label: 'File',
submenu: [
{
label: 'Open Images',
async click () {
const { filePaths } = await dialog.showOpenDialog({
properties: ['openFile', 'openDirectory', 'multiSelections']
})
if (filePaths.length) {
const files = readFileOrDir(filePaths)
const optimizer = new ImageOptimizer(files, context)
context.webContents.send('drop-from-dialog')
optimizer.start()
}
},
accelerator: 'CommandOrControl+O'
}
]
}

const help = {
label: 'Help',
role: 'help',
Expand Down Expand Up @@ -107,5 +130,5 @@ module.exports = context => {
]
}

return [imageOptimizer, help]
return [imageOptimizer, file, help]
}
16 changes: 15 additions & 1 deletion src/main/utils/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const fs = require('fs')
const path = require('path')
const mime = require('mime-types')

function isFile (path) {
const stat = fs.lstatSync(path)
Expand All @@ -18,6 +20,17 @@ function getFileSize (path) {
}
}

function getFilesOrDirs (paths) {
return paths.map(p => {
const { name, ext } = path.parse(p)
return {
name: name + ext,
path: p,
type: isFolder(p) ? '' : mime.lookup(p)
}
})
}

function formatBytes (bytes, decimals = 2) {
if (bytes === 0) return '0 Bytes'

Expand All @@ -33,5 +46,6 @@ function formatBytes (bytes, decimals = 2) {
module.exports = {
isFile,
isFolder,
getFileSize
getFileSize,
readFileOrDir: getFilesOrDirs
}
4 changes: 4 additions & 0 deletions src/renderer/components/AppDragArea.vue
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ export default {
return {}
},

created () {
ipc.on('drop-from-dialog', () => this.$emit('hide', true))
},

methods: {
onDrop (e) {
e.preventDefault()
Expand Down