-
-
Notifications
You must be signed in to change notification settings - Fork 429
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
spellchecker: Use Electron 8 built-in spellchecker.
* Using electron built-in spellchecker * Added the custom context menu Co-authored-by: Anders Kaseorg <[email protected]> Fixes: #504
- Loading branch information
1 parent
4261874
commit 0fff633
Showing
12 changed files
with
248 additions
and
303 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,102 @@ | ||
import {remote, ContextMenuParams} from 'electron'; | ||
import * as t from '../utils/translation-util'; | ||
const {clipboard, Menu} = remote; | ||
|
||
export const contextMenu = (webContents: Electron.WebContents, event: Event, props: ContextMenuParams) => { | ||
const isText = Boolean(props.selectionText.length); | ||
const isLink = Boolean(props.linkURL); | ||
|
||
const makeSuggestion = (suggestion: string) => ({ | ||
label: suggestion, | ||
visible: true, | ||
async click() { | ||
await webContents.insertText(suggestion); | ||
} | ||
}); | ||
|
||
let menuTemplate: Electron.MenuItemConstructorOptions[] = [{ | ||
label: t.__('Add to Dictionary'), | ||
visible: props.isEditable && isText && props.misspelledWord.length !== 0, | ||
click(_item) { | ||
webContents.session.addWordToSpellCheckerDictionary(props.misspelledWord); | ||
} | ||
}, { | ||
type: 'separator' | ||
}, { | ||
label: `${t.__('Look Up')} "${props.selectionText}"`, | ||
visible: process.platform === 'darwin' && isText, | ||
click(_item) { | ||
webContents.showDefinitionForSelection(); | ||
} | ||
}, { | ||
type: 'separator' | ||
}, { | ||
label: t.__('Cut'), | ||
visible: isText, | ||
enabled: props.isEditable, | ||
accelerator: 'CommandOrControl+X', | ||
click(_item) { | ||
webContents.cut(); | ||
} | ||
}, { | ||
label: t.__('Copy'), | ||
accelerator: 'CommandOrControl+C', | ||
click(_item) { | ||
webContents.copy(); | ||
} | ||
}, { | ||
label: t.__('Paste'), // Bug: Paste replaces text | ||
accelerator: 'CommandOrControl+V', | ||
enabled: props.isEditable, | ||
click() { | ||
webContents.paste(); | ||
} | ||
}, { | ||
type: 'separator' | ||
}, { | ||
label: t.__('Copy Link'), | ||
visible: isText && isLink, | ||
click(_item) { | ||
clipboard.write({ | ||
bookmark: props.linkText, | ||
text: props.linkURL | ||
}); | ||
} | ||
}, { | ||
label: t.__('Copy Image'), | ||
visible: props.mediaType === 'image', | ||
click(_item) { | ||
webContents.copyImageAt(props.x, props.y); | ||
} | ||
}, { | ||
label: t.__('Copy Image URL'), | ||
visible: props.mediaType === 'image', | ||
click(_item) { | ||
clipboard.write({ | ||
bookmark: props.srcURL, | ||
text: props.srcURL | ||
}); | ||
} | ||
}, { | ||
type: 'separator' | ||
}, { | ||
label: t.__('Services'), | ||
visible: process.platform === 'darwin', | ||
role: 'services' | ||
}]; | ||
|
||
if (props.misspelledWord) { | ||
if (props.dictionarySuggestions.length > 0) { | ||
const suggestions: Electron.MenuItemConstructorOptions[] = props.dictionarySuggestions.map((suggestion: string) => makeSuggestion(suggestion)); | ||
menuTemplate = suggestions.concat(menuTemplate); | ||
} else { | ||
menuTemplate.unshift({ | ||
label: t.__('No Suggestion Found'), | ||
enabled: false | ||
}); | ||
} | ||
} | ||
|
||
const menu = Menu.buildFromTemplate(menuTemplate); | ||
menu.popup(); | ||
}; |
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
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
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
Oops, something went wrong.