Skip to content
This repository has been archived by the owner on Jun 20, 2022. It is now read-only.

Commit

Permalink
Spellcheck on editor frames
Browse files Browse the repository at this point in the history
  • Loading branch information
moughxyz committed Oct 8, 2019
1 parent 16d15ba commit b9af3b6
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 81 deletions.
13 changes: 6 additions & 7 deletions app/index.html
Expand Up @@ -11,11 +11,6 @@
try {
const {ipcRenderer} = require('electron');
window.ipcRenderer = ipcRenderer;
try {
require("./javascripts/render/spellcheck.js")
} catch (e) {
console.error(e);
}

// https://github.com/electron/electron/blob/master/docs/faq.md#i-can-not-use-jqueryrequirejsmeteorangularjs-in-electron
window.nodeRequire = require;
Expand Down Expand Up @@ -74,6 +69,12 @@
let useSystemMenuBar = Store.instance().get("useSystemMenuBar");

angular.element(document).ready(function () {
try {
window.nodeRequire("./javascripts/render/spellcheck.js")
} catch (e) {
console.error(e);
}

var desktopManager = angular.element(document).injector().get('desktopManager');

desktopManager.desktop_setApplicationDataPath(userDataPath);
Expand Down Expand Up @@ -120,8 +121,6 @@
ipcRenderer.send("major-data-change");
})

window.nodeRequire("./javascripts/render/editorContextMenu.js");

/*
Title bar events
*/
Expand Down
37 changes: 0 additions & 37 deletions app/javascripts/render/editorContextMenu.js

This file was deleted.

106 changes: 69 additions & 37 deletions app/javascripts/render/spellcheck.js
Expand Up @@ -2,39 +2,25 @@
// https://github.com/WhisperSystems/Signal-Desktop/blob/development/js/spell_check.js

(function () {
var electron = require('electron');
var {remote, app} = require('electron');
const electron = require('electron');
const {remote, app} = require('electron');
// var remote = electron.remote;
// var app = remote.app;
var webFrame = electron.webFrame;
var path = require('path');
const webFrame = electron.webFrame;
const path = require('path');

var osLocale = require('os-locale');
var os = require('os');
var semver = require('semver');
var spellchecker = require('spellchecker');
const osLocale = require('os-locale');
const os = require('os');
const semver = require('semver');
const spellchecker = require('spellchecker');
// `remote.require` since `Menu` is a main-process module.
var buildEditorContextMenu = remote.require('electron-editor-context-menu');
const buildEditorContextMenu = remote.require('electron-editor-context-menu');

var EN_VARIANT = /^en/;
const EN_VARIANT = /^en/;

// Prevent the spellchecker from showing contractions as errors.
var ENGLISH_SKIP_WORDS = [
'ain',
'couldn',
'didn',
'doesn',
'hadn',
'hasn',
'mightn',
'mustn',
'needn',
'oughtn',
'shan',
'shouldn',
'wasn',
'weren',
'wouldn'
const ENGLISH_SKIP_WORDS = ['ain', 'couldn', 'didn', 'doesn', 'hadn', 'hasn', 'mightn', 'mustn',
'needn', 'oughtn', 'shan', 'shouldn', 'wasn', 'weren', 'wouldn'
];

function setupLinux(locale) {
Expand Down Expand Up @@ -62,7 +48,7 @@

// We load locale this way and not via app.getLocale() because this call returns
// 'es_ES' and not just 'es.' And hunspell requires the fully-qualified locale.
var locale = osLocale.sync().replace('-', '_');
const locale = osLocale.sync().replace('-', '_');

// The LANG environment variable is how node spellchecker finds its default language:
// https://github.com/atom/node-spellchecker/blob/59d2d5eee5785c4b34e9669cd5d987181d17c098/lib/spellchecker.js#L29
Expand Down Expand Up @@ -123,30 +109,76 @@
}
};

webFrame.setSpellCheckProvider(
'en-US',
simpleChecker
);
function loadSpellcheckIntoFrames() {
webFrame.setSpellCheckProvider('en-US', simpleChecker);
}

window.addEventListener('contextmenu', function(e) {
// Only show the context menu in text editors.
if (!e.target.closest('textarea, input, [contenteditable="true"]')) {
return;
}
loadSpellcheckIntoFrames();

var selectedText = window.getSelection().toString();
function getContextMenuForText(selectedText) {
var isMisspelled = selectedText && simpleChecker.isMisspelled(selectedText);
var spellingSuggestions = isMisspelled && simpleChecker.getSuggestions(selectedText).slice(0, 5);
var menu = buildEditorContextMenu({
isMisspelled: isMisspelled,
spellingSuggestions: spellingSuggestions,
});
return menu;
}

window.addEventListener('contextmenu', function(e) {
// Only show the context menu in text editors.
if (!e.target.closest('textarea, input, [contenteditable="true"]')) {
return;
}

let selectedText = window.getSelection().toString();
let menu = getContextMenuForText(selectedText);

// The 'contextmenu' event is emitted after 'selectionchange' has fired but possibly before the
// visible selection has changed. Try to wait to show the menu until after that, otherwise the
// visible selection will update after the menu dismisses and look weird.
setTimeout(function() {
menu.popup({window: remote.getCurrentWindow()});
}, 30);
});



/*
Context menus and spellcheck for editor extensions
*/

var desktopManager = angular.element(document).injector().get('desktopManager');

function editorExtensionContextEvent(e) {
let selectedText = e.view.getSelection().toString();
let menu = getContextMenuForText(selectedText);

// The 'contextmenu' event is emitted after 'selectionchange' has fired but possibly before the
// visible selection has changed. Try to wait to show the menu until after that, otherwise the
// visible selection will update after the menu dismisses and look weird.
setTimeout(function() {
menu.popup({window: remote.getCurrentWindow()});
}, 30);
}

function addContextMenuTo(uuid) {
let componentFrame = document.querySelector('[data-component-id="' + uuid + '"]');
if(componentFrame) {
// add content menu event
componentFrame.contentWindow.addEventListener("contextmenu", editorExtensionContextEvent);
}
}

// register activation observer to be notified when a component is registered
desktopManager.desktop_registerComponentActivationObserver((component) => {
try {
// Reload spellcheck integration after iframe is loaded (https://github.com/electron/electron/issues/13514#issuecomment-445396551)
loadSpellcheckIntoFrames();

addContextMenuTo(component.uuid);
} catch (e) {
console.error(e);
}
});
})();

0 comments on commit b9af3b6

Please sign in to comment.