feat: use getTiddlerFilePath -> openPath

This commit is contained in:
linonetwo 2023-12-29 23:00:26 +08:00
parent cb66c9361e
commit cbfc068ff4
4 changed files with 33 additions and 23 deletions

View file

@ -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<void> {
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
public async open(uri: string, showItemInFolder = false): Promise<void> {
logger.debug(`NativeService.open() Opening ${uri}`, { showItemInFolder });
showItemInFolder ? shell.showItemInFolder(uri) : await shell.openExternal(uri);
}
public async openPath(filePath: string, showItemInFolder?: boolean): Promise<void> {
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<false | string> {
// 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<void> {
isDirectory ? shell.showItemInFolder(uri) : await shell.openExternal(uri);
}
public async mkdir(absoulutePath: string): Promise<void> {
await fs.mkdirp(absoulutePath);
}

View file

@ -44,11 +44,21 @@ export interface INativeService {
getLocalHostUrlWithActualInfo(urlToReplace: string, workspaceID: string): Promise<string>;
log(level: string, message: string, meta?: Record<string, unknown>): Promise<void>;
mkdir(absoulutePath: string): Promise<void>;
open(uri: string, isDirectory?: boolean): Promise<void>;
/**
* 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<void>;
openInEditor(filePath: string, editorName?: string | undefined): Promise<boolean>;
openInGitGuiApp(filePath: string, editorName?: string | undefined): Promise<boolean>;
openNewGitHubIssue(error: Error): Promise<void>;
openPath(filePath: string): Promise<void>;
/**
* 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<void>;
path(method: 'basename' | 'dirname' | 'join', pathString: string | undefined, ...paths: string[]): Promise<string | undefined>;
pickDirectory(defaultPath?: string, options?: IPickDirectoryOptions): Promise<string[]>;
pickFile(filters?: Electron.OpenDialogOptions['filters']): Promise<string[]>;

View file

@ -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<void> {
public async getTiddlerFilePath(title: string, workspaceID?: string): Promise<string | undefined> {
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;
}
}
}

View file

@ -50,6 +50,13 @@ export interface IWikiService {
ensureWikiExist(wikiPath: string, shouldBeMainWiki: boolean): Promise<void>;
extractWikiHTML(htmlWikiPath: string, saveWikiFolderPath: string): Promise<string | undefined>;
getSubWikiPluginContent(mainWikiPath: string): Promise<ISubWikiPluginContent[]>;
/**
* 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<string | undefined>;
getTiddlerText(workspace: IWorkspace, title: string): Promise<string | undefined>;
getWikiChangeObserver$(workspaceID: string): Observable<IChangedTiddlers>;
getWikiErrorLogs(workspaceID: string, wikiName: string): Promise<{ content: string; filePath: string }>;
@ -59,12 +66,6 @@ export interface IWikiService {
*/
getWorker(workspaceID: string): ModuleThread<WikiWorker> | undefined;
linkWiki(mainWikiPath: string, folderName: string, subWikiPath: string): Promise<void>;
/**
* 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<void>;
packetHTMLFromWikiFolder(wikiFolderLocation: string, pathOfNewHTML: string): Promise<void>;
removeWiki(wikiPath: string, mainWikiToUnLink?: string, onlyRemoveLink?: boolean): Promise<void>;
/** 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,