Last active
December 4, 2024 18:29
-
-
Save trulysinclair/871762b37b84d247d33f28080c2b20c9 to your computer and use it in GitHub Desktop.
Simple Spellcheck for Views
This file contains hidden or 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
// A BrowserView can be used to embed additional web content into a BrowserWindow. | |
// It is like a child window, except that it is positioned relative to its owning | |
// window. It is meant to be an alternative to the webview tag. | |
// | |
// For more info, see: | |
// https://electronjs.org/docs/api/browser-view | |
// In the main process. | |
const { BrowserView, BrowserWindow, app, Menu, MenuItem } = require('electron/main') | |
app.whenReady().then(() => { | |
let win = new BrowserWindow({ | |
width: 800, height: 600, webPreferences: { | |
nodeIntegration: true | |
} | |
}) | |
win.on('closed', () => { | |
win = null | |
}) | |
const view = new BrowserView({ | |
webPreferences: { | |
spellcheck: true | |
} | |
}) | |
view.webContents.session.setSpellCheckerLanguages(['en-US']); | |
win.setBrowserView(view) | |
view.setBounds({ x: 0, y: 0, width: 800, height: 600 }) | |
view.webContents.loadURL('https://quilljs.com/playground/snow') | |
view.webContents.on('context-menu', (event, params) => { | |
const menu = new Menu() | |
// Add each spelling suggestion | |
for (const suggestion of params.dictionarySuggestions) { | |
menu.append(new MenuItem({ | |
label: suggestion, | |
click: () => view.webContents.replaceMisspelling(suggestion) | |
})) | |
} | |
// Allow users to add the misspelled word to the dictionary | |
if (params.misspelledWord) { | |
menu.append( | |
new MenuItem({ | |
label: 'Add to dictionary', | |
click: () => view.webContents.session.addWordToSpellCheckerDictionary(params.misspelledWord) | |
}) | |
) | |
} | |
menu.popup() | |
}) | |
console.log(view.webContents.session.availableSpellCheckerLanguages) | |
}) |
This file contains hidden or 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
{ | |
"name": "berserk-show-telephone-b5qdg", | |
"productName": "berserk-show-telephone-b5qdg", | |
"description": "My Electron application description", | |
"keywords": [], | |
"main": "./main.js", | |
"version": "1.0.0", | |
"author": "trulysinclair", | |
"scripts": { | |
"start": "electron ." | |
}, | |
"dependencies": {}, | |
"devDependencies": { | |
"electron": "32.0.1" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment