feat(electron): spellcheck setting (#8730)

fix AF-1647
This commit is contained in:
pengx17
2024-11-11 13:03:33 +00:00
parent b3749246f6
commit 35f3fc7b5d
12 changed files with 172 additions and 65 deletions

View File

@@ -1,45 +0,0 @@
import { z } from 'zod';
export const workbenchViewIconNameSchema = z.enum([
'trash',
'allDocs',
'collection',
'tag',
'doc', // refers to a doc whose mode is not yet being resolved
'page',
'edgeless',
'journal',
]);
export const workbenchViewMetaSchema = z.object({
id: z.string(),
path: z
.object({
pathname: z.string().optional(),
hash: z.string().optional(),
search: z.string().optional(),
})
.optional(),
// todo: move title/module to cached stated
title: z.string().optional(),
iconName: workbenchViewIconNameSchema.optional(),
});
export const workbenchMetaSchema = z.object({
id: z.string(),
activeViewIndex: z.number(),
pinned: z.boolean().optional(),
basename: z.string(),
views: z.array(workbenchViewMetaSchema),
});
export const tabViewsMetaSchema = z.object({
activeWorkbenchId: z.string().optional(),
workbenches: z.array(workbenchMetaSchema).default([]),
});
export const TabViewsMetaKey = 'tabViewsMetaSchema';
export type TabViewsMetaSchema = z.infer<typeof tabViewsMetaSchema>;
export type WorkbenchMeta = z.infer<typeof workbenchMetaSchema>;
export type WorkbenchViewMeta = z.infer<typeof workbenchViewMetaSchema>;
export type WorkbenchViewModule = z.infer<typeof workbenchViewIconNameSchema>;

View File

@@ -2,6 +2,8 @@ import { join } from 'node:path';
import {
app,
Menu,
MenuItem,
session,
type View,
type WebContents,
@@ -26,16 +28,18 @@ import { CLOUD_BASE_URL, isDev } from '../config';
import { mainWindowOrigin, shellViewUrl } from '../constants';
import { ensureHelperProcess } from '../helper-process';
import { logger } from '../logger';
import { globalStateStorage } from '../shared-storage/storage';
import { getCustomThemeWindow } from './custom-theme-window';
import { getMainWindow, MainWindowManager } from './main-window';
import {
SpellCheckStateKey,
SpellCheckStateSchema,
TabViewsMetaKey,
type TabViewsMetaSchema,
tabViewsMetaSchema,
type WorkbenchMeta,
type WorkbenchViewMeta,
} from './tab-views-meta-schema';
} from '../shared-state-schema';
import { globalStateStorage } from '../shared-storage/storage';
import { getCustomThemeWindow } from './custom-theme-window';
import { getMainWindow, MainWindowManager } from './main-window';
async function getAdditionalArguments() {
const { getExposedMeta } = await import('../exposed');
@@ -74,6 +78,10 @@ const TabViewsMetaState = {
},
};
const spellCheckSettings = SpellCheckStateSchema.parse(
globalStateStorage.get(SpellCheckStateKey) ?? {}
);
type AddTabAction = {
type: 'add-tab';
payload: WorkbenchMeta;
@@ -816,13 +824,44 @@ export class WebContentViewsManager {
transparent: true,
contextIsolation: true,
sandbox: false,
spellcheck: false, // TODO(@pengx17): enable?
spellcheck: spellCheckSettings.enabled,
preload: join(__dirname, './preload.js'), // this points to the bundled preload module
// serialize exposed meta that to be used in preload
additionalArguments: additionalArguments,
},
});
if (spellCheckSettings.enabled) {
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', // TODO: i18n
click: () =>
view.webContents.session.addWordToSpellCheckerDictionary(
params.misspelledWord
),
})
);
}
menu.popup();
});
}
this.webViewsMap$.next(this.tabViewsMap.set(viewId, view));
let unsub = () => {};