From cbfc068ff41c5c8f0cd80f0c133a014272d7a89b Mon Sep 17 00:00:00 2001 From: linonetwo Date: Fri, 29 Dec 2023 23:00:26 +0800 Subject: [PATCH] feat: use getTiddlerFilePath -> openPath --- src/services/native/index.ts | 20 ++++++++++---------- src/services/native/interface.ts | 14 ++++++++++++-- src/services/wiki/index.ts | 7 +++---- src/services/wiki/interface.ts | 15 ++++++++------- 4 files changed, 33 insertions(+), 23 deletions(-) diff --git a/src/services/native/index.ts b/src/services/native/index.ts index bd43d1dd..69e5e144 100644 --- a/src/services/native/index.ts +++ b/src/services/native/index.ts @@ -1,3 +1,4 @@ +/* eslint-disable @typescript-eslint/strict-boolean-expressions */ /* eslint-disable @typescript-eslint/require-await */ import { app, dialog, ipcMain, MessageBoxOptions, shell } from 'electron'; import fs from 'fs-extra'; @@ -7,6 +8,7 @@ import { Observable } from 'rxjs'; import { NativeChannel } from '@/constants/channels'; import { ZX_FOLDER } from '@/constants/paths'; +import { githubDesktopUrl } from '@/constants/urls'; import { lazyInject } from '@services/container'; import { ILogLevels, logger } from '@services/libs/log'; import { getLocalHostUrlWithActualIP, getUrlWithCorrectProtocol, replaceUrlPortWithSettingPort } from '@services/libs/url'; @@ -21,7 +23,6 @@ import { ZxNotInitializedError } from './error'; import { findEditorOrDefault, findGitGUIAppOrDefault, launchExternalEditor } from './externalApp'; import { INativeService, IPickDirectoryOptions } from './interface'; import { reportErrorToGithubWithTemplates } from './reportError'; -import { githubDesktopUrl } from '@/constants/urls'; @injectable() export class NativeService implements INativeService { @@ -64,26 +65,29 @@ export class NativeService implements INativeService { return false; } - public async openPath(filePath: string): Promise { - // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions + public async open(uri: string, showItemInFolder = false): Promise { + logger.debug(`NativeService.open() Opening ${uri}`, { showItemInFolder }); + showItemInFolder ? shell.showItemInFolder(uri) : await shell.openExternal(uri); + } + + public async openPath(filePath: string, showItemInFolder?: boolean): Promise { if (!filePath.trim()) { return; } logger.debug(`NativeService.openPath() Opening ${filePath}`); // TODO: add a switch that tell user these are dangerous features, use at own risk. if (path.isAbsolute(filePath)) { - await shell.openPath(filePath); + showItemInFolder ? shell.showItemInFolder(filePath) : await shell.openPath(filePath); } else { const activeWorkspace = this.workspaceService.getActiveWorkspaceSync(); if (activeWorkspace?.wikiFolderLocation !== undefined) { const absolutePath = path.resolve(path.join(activeWorkspace.wikiFolderLocation, filePath)); - await shell.openPath(absolutePath); + showItemInFolder ? shell.showItemInFolder(absolutePath) : await shell.openPath(absolutePath); } } } public async copyPath(fromFilePath: string, toFilePath: string, options?: { fileToDir?: boolean }): Promise { - // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions if (!fromFilePath.trim() || !toFilePath.trim()) { logger.error('NativeService.copyPath() fromFilePath or toFilePath is empty', { fromFilePath, toFilePath }); return false; @@ -205,10 +209,6 @@ ${message.message} return []; } - public async open(uri: string, isDirectory = false): Promise { - isDirectory ? shell.showItemInFolder(uri) : await shell.openExternal(uri); - } - public async mkdir(absoulutePath: string): Promise { await fs.mkdirp(absoulutePath); } diff --git a/src/services/native/interface.ts b/src/services/native/interface.ts index 0f59ed6a..685825bb 100644 --- a/src/services/native/interface.ts +++ b/src/services/native/interface.ts @@ -44,11 +44,21 @@ export interface INativeService { getLocalHostUrlWithActualInfo(urlToReplace: string, workspaceID: string): Promise; log(level: string, message: string, meta?: Record): Promise; mkdir(absoulutePath: string): Promise; - open(uri: string, isDirectory?: boolean): Promise; + /** + * Open a file or URI in the desktop's default manner, or show in file manager. + * @param uri File path or URI starts with any scheme. + * @param showItemInFolder Show the given file in a file manager. If possible, select the file. + */ + open(uri: string, showItemInFolder?: boolean): Promise; openInEditor(filePath: string, editorName?: string | undefined): Promise; openInGitGuiApp(filePath: string, editorName?: string | undefined): Promise; openNewGitHubIssue(error: Error): Promise; - openPath(filePath: string): Promise; + /** + * Open a file path, if is a relative path from wiki folder in the wiki folder, it will open it too. + * @param filePath relative path from wiki folder, or an absolute path. + * @param showItemInFolder Show the given file in a file manager. If possible, select the file. + */ + openPath(filePath: string, showItemInFolder?: boolean): Promise; path(method: 'basename' | 'dirname' | 'join', pathString: string | undefined, ...paths: string[]): Promise; pickDirectory(defaultPath?: string, options?: IPickDirectoryOptions): Promise; pickFile(filters?: Electron.OpenDialogOptions['filters']): Promise; diff --git a/src/services/wiki/index.ts b/src/services/wiki/index.ts index 5385b873..cde0e3c5 100644 --- a/src/services/wiki/index.ts +++ b/src/services/wiki/index.ts @@ -2,7 +2,7 @@ /* eslint-disable @typescript-eslint/no-misused-promises */ /* eslint-disable @typescript-eslint/require-await */ /* eslint-disable @typescript-eslint/no-dynamic-delete */ -import { dialog, ipcMain, shell } from 'electron'; +import { dialog, ipcMain } from 'electron'; import { backOff } from 'exponential-backoff'; import fs from 'fs-extra'; import { injectable } from 'inversify'; @@ -781,13 +781,12 @@ export class Wiki implements IWikiService { }); } - public async openTiddlerInExternal(title: string, workspaceID?: string): Promise { + public async getTiddlerFilePath(title: string, workspaceID?: string): Promise { const wikiWorker = this.getWorker(workspaceID ?? (await this.workspaceService.getActiveWorkspace())?.id ?? ''); if (wikiWorker !== undefined) { const tiddlerFileMetadata = await wikiWorker.getTiddlerFileMetadata(title); if (tiddlerFileMetadata?.filepath !== undefined) { - logger.debug(`openTiddlerInExternal() Opening ${tiddlerFileMetadata.filepath}`); - await shell.openPath(tiddlerFileMetadata.filepath); + return tiddlerFileMetadata.filepath; } } } diff --git a/src/services/wiki/interface.ts b/src/services/wiki/interface.ts index 58395654..d230f4f2 100644 --- a/src/services/wiki/interface.ts +++ b/src/services/wiki/interface.ts @@ -50,6 +50,13 @@ export interface IWikiService { ensureWikiExist(wikiPath: string, shouldBeMainWiki: boolean): Promise; extractWikiHTML(htmlWikiPath: string, saveWikiFolderPath: string): Promise; getSubWikiPluginContent(mainWikiPath: string): Promise; + /** + * Get tiddler's absolute path. So you can open image or PDF in OS native viewer or some else usage like this, using `window?.service?.native?.openPath?.(filePath)` + * @returns absolute path like `'/Users/linonetwo/Desktop/repo/TiddlyGit-Desktop/wiki-dev/wiki/tiddlers/Index.tid'` + * @param homePath Workspace home path, used to locate wiki worker + * @param title tiddler title to open + */ + getTiddlerFilePath(title: string, workspaceID?: string): Promise; getTiddlerText(workspace: IWorkspace, title: string): Promise; getWikiChangeObserver$(workspaceID: string): Observable; getWikiErrorLogs(workspaceID: string, wikiName: string): Promise<{ content: string; filePath: string }>; @@ -59,12 +66,6 @@ export interface IWikiService { */ getWorker(workspaceID: string): ModuleThread | undefined; linkWiki(mainWikiPath: string, folderName: string, subWikiPath: string): Promise; - /** - * Open image or PDF in OS native viewer or some else usage like this. - * @param homePath Workspace home path, used to locate wiki worker - * @param title tiddler title to open - */ - openTiddlerInExternal(title: string, workspaceID: string): Promise; packetHTMLFromWikiFolder(wikiFolderLocation: string, pathOfNewHTML: string): Promise; removeWiki(wikiPath: string, mainWikiToUnLink?: string, onlyRemoveLink?: boolean): Promise; /** send tiddlywiki action message to current active wiki */ @@ -116,7 +117,7 @@ export const WikiServiceIPCDescriptor = { getTiddlerText: ProxyPropertyType.Function, getWikiErrorLogs: ProxyPropertyType.Function, linkWiki: ProxyPropertyType.Function, - openTiddlerInExternal: ProxyPropertyType.Function, + getTiddlerFilePath: ProxyPropertyType.Function, packetHTMLFromWikiFolder: ProxyPropertyType.Function, removeWiki: ProxyPropertyType.Function, requestWikiSendActionMessage: ProxyPropertyType.Function,