forked from VinceG/USPS-php-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix error checking, makes check for keys before trying to access
- Loading branch information
1 parent
e3f73cd
commit 2c89391
Showing
5 changed files
with
182 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// This gives you default context menu (cut, copy, paste) | ||
// in all input fields and textareas across your app. | ||
|
||
import { remote } from 'electron'; | ||
|
||
const Menu = remote.Menu; | ||
const MenuItem = remote.MenuItem; | ||
|
||
const isAnyTextSelected = () => { | ||
return window.getSelection().toString() !== ''; | ||
}; | ||
|
||
const cut = new MenuItem({ | ||
label: 'Cut', | ||
click: () => { | ||
document.execCommand('cut'); | ||
}, | ||
}); | ||
|
||
const copy = new MenuItem({ | ||
label: 'Copy', | ||
click: () => { | ||
document.execCommand('copy'); | ||
}, | ||
}); | ||
|
||
const paste = new MenuItem({ | ||
label: 'Paste', | ||
click: () => { | ||
document.execCommand('paste'); | ||
}, | ||
}); | ||
|
||
const normalMenu = new Menu(); | ||
normalMenu.append(copy); | ||
|
||
const textEditingMenu = new Menu(); | ||
textEditingMenu.append(cut); | ||
textEditingMenu.append(copy); | ||
textEditingMenu.append(paste); | ||
|
||
document.addEventListener('contextmenu', (event) => { | ||
switch (event.target.nodeName) { | ||
case 'TEXTAREA': | ||
case 'INPUT': | ||
event.preventDefault(); | ||
textEditingMenu.popup(remote.getCurrentWindow()); | ||
break; | ||
default: | ||
if (isAnyTextSelected()) { | ||
event.preventDefault(); | ||
normalMenu.popup(remote.getCurrentWindow()); | ||
} | ||
} | ||
}, false); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Convenient way for opening links in external browser, not in the app. | ||
// Useful especially if you have a lot of links to deal with. | ||
// | ||
// Usage: | ||
// | ||
// Every link with class ".js-external-link" will be opened in external browser. | ||
// <a class="js-external-link" href="http://google.com">google</a> | ||
// | ||
// The same behaviour for many links can be achieved by adding | ||
// this class to any parent tag of an anchor tag. | ||
// <p class="js-external-link"> | ||
// <a href="http://google.com">google</a> | ||
// <a href="http://bing.com">bing</a> | ||
// </p> | ||
|
||
import { shell } from 'electron'; | ||
|
||
const supportExternalLinks = (event) => { | ||
let href; | ||
let isExternal = false; | ||
|
||
const checkDomElement = (element) => { | ||
if (element.nodeName === 'A') { | ||
href = element.getAttribute('href'); | ||
} | ||
if (element.classList.contains('js-external-link')) { | ||
isExternal = true; | ||
} | ||
if (href && isExternal) { | ||
shell.openExternal(href); | ||
event.preventDefault(); | ||
} else if (element.parentElement) { | ||
checkDomElement(element.parentElement); | ||
} | ||
}; | ||
|
||
checkDomElement(event.target); | ||
}; | ||
|
||
document.addEventListener('click', supportExternalLinks, false); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// This helper remembers the size and position of your windows (and restores | ||
// them in that place after app relaunch). | ||
// Can be used for more than one window, just construct many | ||
// instances of it and give each different name. | ||
|
||
import { app, BrowserWindow, screen } from 'electron'; | ||
import jetpack from 'fs-jetpack'; | ||
|
||
export default (name, options) => { | ||
const userDataDir = jetpack.cwd(app.getPath('userData')); | ||
const stateStoreFile = `window-state-${name}.json`; | ||
const defaultSize = { | ||
width: options.width, | ||
height: options.height, | ||
}; | ||
let state = {}; | ||
let win; | ||
|
||
const restore = () => { | ||
let restoredState = {}; | ||
try { | ||
restoredState = userDataDir.read(stateStoreFile, 'json'); | ||
} catch (err) { | ||
// For some reason json can't be read (might be corrupted). | ||
// No worries, we have defaults. | ||
} | ||
return Object.assign({}, defaultSize, restoredState); | ||
}; | ||
|
||
const getCurrentPosition = () => { | ||
const position = win.getPosition(); | ||
const size = win.getSize(); | ||
return { | ||
x: position[0], | ||
y: position[1], | ||
width: size[0], | ||
height: size[1], | ||
}; | ||
}; | ||
|
||
const windowWithinBounds = (windowState, bounds) => { | ||
return windowState.x >= bounds.x | ||
&& windowState.y >= bounds.y | ||
&& windowState.x + windowState.width <= bounds.x + bounds.width | ||
&& windowState.y + windowState.height <= bounds.y + bounds.height; | ||
}; | ||
|
||
const resetToDefaults = () => { | ||
const bounds = screen.getPrimaryDisplay().bounds; | ||
return Object.assign({}, defaultSize, { | ||
x: (bounds.width - defaultSize.width) / 2, | ||
y: (bounds.height - defaultSize.height) / 2, | ||
}); | ||
}; | ||
|
||
const ensureVisibleOnSomeDisplay = (windowState) => { | ||
const visible = screen.getAllDisplays().some((display) => { | ||
return windowWithinBounds(windowState, display.bounds); | ||
}); | ||
if (!visible) { | ||
// Window is partially or fully not visible now. | ||
// Reset it to safe defaults. | ||
return resetToDefaults(); | ||
} | ||
return windowState; | ||
}; | ||
|
||
const saveState = () => { | ||
if (!win.isMinimized() && !win.isMaximized()) { | ||
Object.assign(state, getCurrentPosition()); | ||
} | ||
userDataDir.write(stateStoreFile, state, { atomic: true }); | ||
}; | ||
|
||
state = ensureVisibleOnSomeDisplay(restore()); | ||
|
||
win = new BrowserWindow(Object.assign({}, options, state)); | ||
|
||
win.on('close', saveState); | ||
|
||
return win; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters