refactor(react/widgets): typings for dynamic require + solve type errors

This commit is contained in:
Elian Doran 2025-08-29 19:29:15 +03:00
parent 3fd7afbb57
commit ab48a28635
No known key found for this signature in database
7 changed files with 19 additions and 21 deletions

View File

@ -1,6 +1,6 @@
import froca from "../services/froca.js"; import froca from "../services/froca.js";
import RootCommandExecutor from "./root_command_executor.js"; import RootCommandExecutor from "./root_command_executor.js";
import Entrypoints, { type SqlExecuteResults } from "./entrypoints.js"; import Entrypoints from "./entrypoints.js";
import options from "../services/options.js"; import options from "../services/options.js";
import utils, { hasTouchBar } from "../services/utils.js"; import utils, { hasTouchBar } from "../services/utils.js";
import zoomComponent from "./zoom.js"; import zoomComponent from "./zoom.js";
@ -32,6 +32,7 @@ import type { CreateNoteOpts } from "../services/note_create.js";
import { ColumnComponent } from "tabulator-tables"; import { ColumnComponent } from "tabulator-tables";
import { ChooseNoteTypeCallback } from "../widgets/dialogs/note_type_chooser.jsx"; import { ChooseNoteTypeCallback } from "../widgets/dialogs/note_type_chooser.jsx";
import type RootContainer from "../widgets/containers/root_container.js"; import type RootContainer from "../widgets/containers/root_container.js";
import { SqlExecuteResults } from "@triliumnext/commons";
interface Layout { interface Layout {
getRootWidget: (appContext: AppContext) => RootContainer; getRootWidget: (appContext: AppContext) => RootContainer;

View File

@ -10,7 +10,6 @@ import bundleService from "../services/bundle.js";
import froca from "../services/froca.js"; import froca from "../services/froca.js";
import linkService from "../services/link.js"; import linkService from "../services/link.js";
import { t } from "../services/i18n.js"; import { t } from "../services/i18n.js";
import type FNote from "../entities/fnote.js";
import { CreateChildrenResponse, SqlExecuteResponse } from "@triliumnext/commons"; import { CreateChildrenResponse, SqlExecuteResponse } from "@triliumnext/commons";
export default class Entrypoints extends Component { export default class Entrypoints extends Component {
@ -20,7 +19,7 @@ export default class Entrypoints extends Component {
openDevToolsCommand() { openDevToolsCommand() {
if (utils.isElectron()) { if (utils.isElectron()) {
utils.dynamicRequire("@electron/remote").getCurrentWindow().toggleDevTools(); utils.dynamicRequire("@electron/remote").getCurrentWindow().webContents.toggleDevTools();
} }
} }
@ -110,7 +109,7 @@ export default class Entrypoints extends Component {
if (utils.isElectron()) { if (utils.isElectron()) {
// standard JS version does not work completely correctly in electron // standard JS version does not work completely correctly in electron
const webContents = utils.dynamicRequire("@electron/remote").getCurrentWebContents(); const webContents = utils.dynamicRequire("@electron/remote").getCurrentWebContents();
const activeIndex = parseInt(webContents.navigationHistory.getActiveIndex()); const activeIndex = webContents.navigationHistory.getActiveIndex();
webContents.goToIndex(activeIndex - 1); webContents.goToIndex(activeIndex - 1);
} else { } else {
@ -122,7 +121,7 @@ export default class Entrypoints extends Component {
if (utils.isElectron()) { if (utils.isElectron()) {
// standard JS version does not work completely correctly in electron // standard JS version does not work completely correctly in electron
const webContents = utils.dynamicRequire("@electron/remote").getCurrentWebContents(); const webContents = utils.dynamicRequire("@electron/remote").getCurrentWebContents();
const activeIndex = parseInt(webContents.navigationHistory.getActiveIndex()); const activeIndex = webContents.navigationHistory.getActiveIndex();
webContents.goToIndex(activeIndex + 1); webContents.goToIndex(activeIndex + 1);
} else { } else {

View File

@ -218,7 +218,7 @@ function ajax(url: string, method: string, data: unknown, headers: Headers, sile
if (utils.isElectron()) { if (utils.isElectron()) {
const ipc = utils.dynamicRequire("electron").ipcRenderer; const ipc = utils.dynamicRequire("electron").ipcRenderer;
ipc.on("server-response", async (event: string, arg: Arg) => { ipc.on("server-response", async (_, arg: Arg) => {
if (arg.statusCode >= 200 && arg.statusCode < 300) { if (arg.statusCode >= 200 && arg.statusCode < 300) {
handleSuccessfulResponse(arg); handleSuccessfulResponse(arg);
} else { } else {

View File

@ -311,7 +311,13 @@ function copySelectionToClipboard() {
} }
} }
export function dynamicRequire(moduleName: string) { const dynamicRequireMappings = {
"@electron/remote": import("@electron/remote"),
"electron": import("electron"),
"child_process": import("child_process")
};
export function dynamicRequire<T extends keyof typeof dynamicRequireMappings>(moduleName: T): Awaited<typeof dynamicRequireMappings[T]>{
if (typeof __non_webpack_require__ !== "undefined") { if (typeof __non_webpack_require__ !== "undefined") {
return __non_webpack_require__(moduleName); return __non_webpack_require__(moduleName);
} else { } else {

View File

@ -55,7 +55,7 @@ export class TypedBasicWidget<T extends TypedComponent<any>> extends TypedCompon
* @param components the components to be added as children to this component provided the condition is truthy. * @param components the components to be added as children to this component provided the condition is truthy.
* @returns self for chaining. * @returns self for chaining.
*/ */
optChild(condition: boolean, ...components: T[]) { optChild(condition: boolean, ...components: (T | VNode)[]) {
if (condition) { if (condition) {
return this.child(...components); return this.child(...components);
} else { } else {

View File

@ -4,15 +4,7 @@ import treeService from "../../services/tree.js";
import ButtonFromNoteWidget from "./button_from_note.js"; import ButtonFromNoteWidget from "./button_from_note.js";
import type FNote from "../../entities/fnote.js"; import type FNote from "../../entities/fnote.js";
import type { CommandNames } from "../../components/app_context.js"; import type { CommandNames } from "../../components/app_context.js";
import type { WebContents } from "electron";
interface WebContents {
history: string[];
getActiveIndex(): number;
clearHistory(): void;
canGoBack(): boolean;
canGoForward(): boolean;
goToIndex(index: string): void;
}
interface ContextMenuItem { interface ContextMenuItem {
title: string; title: string;
@ -51,14 +43,14 @@ export default class HistoryNavigationButton extends ButtonFromNoteWidget {
async showContextMenu(e: JQuery.ContextMenuEvent) { async showContextMenu(e: JQuery.ContextMenuEvent) {
e.preventDefault(); e.preventDefault();
if (!this.webContents || this.webContents.history.length < 2) { if (!this.webContents || this.webContents.navigationHistory.length() < 2) {
return; return;
} }
let items: ContextMenuItem[] = []; let items: ContextMenuItem[] = [];
const activeIndex = this.webContents.getActiveIndex(); const history = this.webContents.navigationHistory;
const history = this.webContents.history; const activeIndex = history.getActiveIndex();
for (const idx in history) { for (const idx in history) {
const url = history[idx]; const url = history[idx];

View File

@ -516,7 +516,7 @@ export function useStaticTooltip(elRef: RefObject<Element>, config?: Partial<Too
}, [ elRef, config ]); }, [ elRef, config ]);
} }
export function useStaticTooltipWithKeyboardShortcut(elRef: RefObject<Element>, title: string, actionName: KeyboardActionNames) { export function useStaticTooltipWithKeyboardShortcut(elRef: RefObject<Element>, title: string, actionName: KeyboardActionNames | undefined) {
const [ keyboardShortcut, setKeyboardShortcut ] = useState<string[]>(); const [ keyboardShortcut, setKeyboardShortcut ] = useState<string[]>();
useStaticTooltip(elRef, { useStaticTooltip(elRef, {
title: keyboardShortcut?.length ? `${title} (${keyboardShortcut?.join(",")})` : title title: keyboardShortcut?.length ? `${title} (${keyboardShortcut?.join(",")})` : title