Skip to content

Commit

Permalink
Merge pull request #926 from RocketChat/fix/preload-issues
Browse files Browse the repository at this point in the history
[FIX] Preload issues
  • Loading branch information
tassoevan authored Oct 16, 2018
2 parents 327f92c + dc8294b commit 5c19c1a
Show file tree
Hide file tree
Showing 8 changed files with 221 additions and 200 deletions.
1 change: 1 addition & 0 deletions src/i18n/lang/pt-BR.i18n.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"&Help": "Aj&uda",
"Hide": "Esconder",
"Host_not_validated": "O host \"%s\" não pôde ser validado, portanto não foi adicionado.",
"Houston_we_have_a_problem": "Houston, nós temos um problema",
"Invalid_Host": "Host inválido",
"Invalid_url": "Endereço inválido",
"Learn more": "Saiba mais",
Expand Down
113 changes: 60 additions & 53 deletions src/public/lib/SpellCheck.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,53 @@
const os = require('os');
const fs = require('fs');
const path = require('path');
const checker = require('spellchecker');
const { clipboard, remote, webFrame, shell } = require('electron');
const { MenuItem, dialog } = remote;
const { clipboard, remote, shell, webFrame } = require('electron');
const i18n = require('../../i18n/index');

const webContents = remote.getCurrentWebContents();
let menu = new remote.Menu();
const { app, dialog, getCurrentWebContents, getCurrentWindow, Menu, MenuItem } = remote;

const path = remote.require('path');
const isWindows = ['win32', 'win64'].indexOf(os.platform()) !== -1;
const webContents = getCurrentWebContents();
let menu = new Menu();

class SpellCheck {

get userLanguage() {
const lang = localStorage.getItem('userLanguage');
if (lang) {
return lang.replace('-', '_');
const localStorage = {
getItem(key) {
try {
return window.localStorage.getItem(key);
} catch (e) {
console.error(e);
return null;
}
},

return undefined;
}

get dictionaries() {
const dictionaries = localStorage.getItem('spellcheckerDictionaries');
if (dictionaries) {
const result = JSON.parse(dictionaries);
if (Array.isArray(result)) {
return result;
}
setItem(key, value) {
try {
window.localStorage.setItem(key, value);
} catch (e) {
console.error(e);
}
},
};

return undefined;
}
class SpellCheck {

constructor() {
this.enabledDictionaries = [];
this.contractions = this.getContractions();

this.contractions = [
"ain't", "aren't", "can't", "could've", "couldn't", "couldn't've", "didn't", "doesn't", "don't", "hadn't",
"hadn't've", "hasn't", "haven't", "he'd", "he'd've", "he'll", "he's", "how'd", "how'll", "how's", "I'd",
"I'd've", "I'll", "I'm", "I've", "isn't", "it'd", "it'd've", "it'll", "it's", "let's", "ma'am", "mightn't",
"mightn't've", "might've", "mustn't", "must've", "needn't", "not've", "o'clock", "shan't", "she'd", "she'd've",
"she'll", "she's", "should've", "shouldn't", "shouldn't've", "that'll", "that's", "there'd", "there'd've",
"there're", "there's", "they'd", "they'd've", "they'll", "they're", "they've", "wasn't", "we'd", "we'd've",
"we'll", "we're", "we've", "weren't", "what'll", "what're", "what's", "what've", "when's", "where'd",
"where's", "where've", "who'd", "who'll", "who're", "who's", "who've", "why'll", "why're", "why's", "won't",
"would've", "wouldn't", "wouldn't've", "y'all", "y'all'd've", "you'd", "you'd've", "you'll", "you're", "you've",
].reduce((contractionMap, word) => {
contractionMap[word.replace(/'.*/, '')] = true;
return contractionMap;
}, {});

this.loadAvailableDictionaries();
this.setEnabledDictionaries();

Expand Down Expand Up @@ -84,6 +95,17 @@ class SpellCheck {
});
}

get userLanguage() {
const lang = localStorage.getItem('userLanguage');
return lang ? lang.replace('-', '_') : null;
}

get dictionaries() {
const dictionaries = localStorage.getItem('spellcheckerDictionaries');
const result = JSON.parse(dictionaries || '[]');
return Array.isArray(result) ? result : [];
}

/**
* Set enabled dictionaries on load
* Either sets enabled dictionaries to saved preferences, or enables the first
Expand Down Expand Up @@ -134,10 +156,14 @@ class SpellCheck {
if (this.availableDictionaries.length === 0) {
this.multiLanguage = false;
// Dictionaries path is correct for build
this.dictionariesPath = path.join(remote.app.getAppPath(), '../dictionaries');
this.dictionariesPath = path.join(
app.getAppPath(),
process.mainModule.filename.indexOf('app.asar') !== -1 ? '..' : '.',
'dictionaries'
);
this.getDictionariesFromInstallDirectory();
} else {
this.multiLanguage = !isWindows;
this.multiLanguage = process.platform !== 'win32';
this.availableDictionaries = this.availableDictionaries.map((dict) => dict.replace('-', '_'));
}
}
Expand Down Expand Up @@ -208,27 +234,6 @@ class SpellCheck {
}
}

getContractions() {
const contractions = [
"ain't", "aren't", "can't", "could've", "couldn't", "couldn't've", "didn't", "doesn't", "don't", "hadn't",
"hadn't've", "hasn't", "haven't", "he'd", "he'd've", "he'll", "he's", "how'd", "how'll", "how's", "I'd",
"I'd've", "I'll", "I'm", "I've", "isn't", "it'd", "it'd've", "it'll", "it's", "let's", "ma'am", "mightn't",
"mightn't've", "might've", "mustn't", "must've", "needn't", "not've", "o'clock", "shan't", "she'd", "she'd've",
"she'll", "she's", "should've", "shouldn't", "shouldn't've", "that'll", "that's", "there'd", "there'd've",
"there're", "there's", "they'd", "they'd've", "they'll", "they're", "they've", "wasn't", "we'd", "we'd've",
"we'll", "we're", "we've", "weren't", "what'll", "what're", "what's", "what've", "when's", "where'd",
"where's", "where've", "who'd", "who'll", "who're", "who's", "who've", "why'll", "why're", "why's", "won't",
"would've", "wouldn't", "wouldn't've", "y'all", "y'all'd've", "you'd", "you'd've", "you'll", "you're", "you've",
];

const contractionMap = contractions.reduce((acc, word) => {
acc[word.replace(/'.*/, '')] = true;
return acc;
}, {});

return contractionMap;
}

enable() {
webFrame.setSpellCheckProvider('', false, {
spellCheck: (text) => this.isCorrect(text),
Expand All @@ -237,15 +242,17 @@ class SpellCheck {
this.setupContextMenuListener();
}

getMenu() {
createMenuTemplate() {
return [
{
label: i18n.__('&Undo'),
role: 'undo',
accelerator: 'CommandOrControl+Z',
},
{
label: i18n.__('&Redo'),
role: 'redo',
accelerator: process.platform === 'win32' ? 'Control+Y' : 'CommandOrControl+Shift+Z',
},
{
type: 'separator',
Expand Down Expand Up @@ -321,7 +328,7 @@ class SpellCheck {
window.addEventListener('contextmenu', (event) => {
event.preventDefault();

const template = this.getMenu();
const template = this.createMenuTemplate();

if (this.languagesMenu && this.browseForLanguageMenu) {
template.unshift({ type: 'separator' });
Expand Down Expand Up @@ -383,8 +390,8 @@ class SpellCheck {
}
}

menu = remote.Menu.buildFromTemplate(template);
menu.popup(remote.getCurrentWindow(), undefined, undefined, 5);
menu = Menu.buildFromTemplate(template);
menu.popup(getCurrentWindow(), undefined, undefined, 5);
}, 0);
}, false);
}
Expand Down
39 changes: 24 additions & 15 deletions src/public/loading-error.html
Original file line number Diff line number Diff line change
@@ -1,23 +1,32 @@
<!doctype html>
<html>
<head>
<title>Rocket.Chat</title>
<meta charset="utf-8" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" />
<title>Rocket.Chat</title>
<link rel="stylesheet" type="text/css" href="../stylesheets/main.css">
<script>
document.addEventListener('DOMContentLoaded', () => {
document.querySelector('.title').innerHTML = i18n.__('Houston_we_have_a_problem');
document.querySelector('.subtitle').innerHTML = i18n.__('Server_Failed_to_Load');
document.querySelector('.reload-button').innerHTML = i18n.__('Reload');
document.querySelector('.reload-button').addEventListener('click', ({ target }) => {
document.querySelector('.reload-button').classList.add('hidden');
document.querySelector('.reloading-server').classList.remove('hidden');
// location.reload(true);
window.reloadServer();
}, false);
}, false);
</script>
</head>
<body class="loading-error">
<div class="load-fail" id="load-fail">
<h1><script>document.write(window.i18n.__('Houston_we_have_a_problem'))</script></h1>
<h2><script>document.write(window.i18n.__('Server_Failed_to_Load'))</script></h2>
<div class="loading-animation" id="loading" style="display:none;">
<div class="bounce1"></div>
<div class="bounce2"></div>
<div class="bounce3"></div>
</div>
<button class="button primary" id="reload"><script>document.write(window.i18n.__('Reload'))</script></button>
</div>
<body class="loading-error-page">
<h1 class="title">Houston, we have a problem</h1>
<h2 class="subtitle">Server Failed to Load</h2>
<div class="reloading-server hidden">
<span class="dot"></span>
<span class="dot"></span>
<span class="dot"></span>
<span class="message"></span>
</div>
<button class="button primary reload-button">Reload</button>
</body>
</html>
Loading

0 comments on commit 5c19c1a

Please sign in to comment.