From 7cc3ab0d4db512542e201e0120a01f5ba710da2c Mon Sep 17 00:00:00 2001 From: lin onetwo Date: Mon, 22 May 2023 21:31:54 +0800 Subject: [PATCH] fix: ExportWholeWikiHTML as index.html --- localization/locales/en/translation.json | 2 +- localization/locales/zh_CN/translation.json | 2 +- src/constants/fileNames.ts | 5 +++++ src/pages/EditWorkspace/blog.tsx | 4 +--- src/pages/EditWorkspace/index.tsx | 2 +- src/services/native/index.ts | 7 ++++--- src/services/native/interface.ts | 10 +++++++++- src/services/wiki/wikiWorker.ts | 16 +++++++++++----- src/services/workspacesView/index.ts | 6 +++++- 9 files changed, 38 insertions(+), 16 deletions(-) diff --git a/localization/locales/en/translation.json b/localization/locales/en/translation.json index de3e9687..44dae338 100644 --- a/localization/locales/en/translation.json +++ b/localization/locales/en/translation.json @@ -409,7 +409,7 @@ "PrintPage": "Print Page", "PrintActiveTiddler": "Print Active Tiddler", "Wiki": "Wiki", - "ExportWholeWikiHTML": "Export Whole Wiki as HTML" + "ExportWholeWikiHTML": "Export Whole Wiki as HTML to folder" }, "ErrorMessage": "Error message", "ClickForDetails": "Click For Details", diff --git a/localization/locales/zh_CN/translation.json b/localization/locales/zh_CN/translation.json index 08761b43..67e3ded8 100644 --- a/localization/locales/zh_CN/translation.json +++ b/localization/locales/zh_CN/translation.json @@ -103,7 +103,7 @@ "PrintPage": "打印页面", "PrintActiveTiddler": "打印当前笔记", "Wiki": "Wiki", - "ExportWholeWikiHTML": "导出整个Wiki为HTML" + "ExportWholeWikiHTML": "导出整个Wiki为HTML存入文件夹" }, "AddWorkspace": { "AddWorkspace": "添加工作区", diff --git a/src/constants/fileNames.ts b/src/constants/fileNames.ts index 5d5f5eb7..9dbedce8 100644 --- a/src/constants/fileNames.ts +++ b/src/constants/fileNames.ts @@ -4,3 +4,8 @@ export const developmentHttpsCertKeyFolderName = 'https-keys-dev'; /** Used to place mock wiki during dev and testing */ export const developmentWikiFolderName = 'tidgi-dev'; export const localizationFolderName = 'localization'; + +export const wikiPictureExtensions = ['jpg', 'jpeg', 'png', 'gif', 'tiff', 'tif', 'bmp', 'dib']; +export const wikiHtmlExtensions = ['html', 'htm', 'hta', 'Html', 'HTML', 'HTM', 'HTA']; +export const tlsCertExtensions = ['crt']; +export const tlsKeyExtensions = ['key']; diff --git a/src/pages/EditWorkspace/blog.tsx b/src/pages/EditWorkspace/blog.tsx index b8379241..aac91047 100644 --- a/src/pages/EditWorkspace/blog.tsx +++ b/src/pages/EditWorkspace/blog.tsx @@ -7,6 +7,7 @@ import styled from 'styled-components'; import { ListItem, ListItemText } from '@/components/ListItem'; import { rootTiddlers } from '@/constants/defaultTiddlerNames'; +import { tlsCertExtensions, tlsKeyExtensions } from '@/constants/fileNames'; import { defaultServerIP } from '@/constants/urls'; import { IWorkspace } from '@services/workspaces/interface'; @@ -26,9 +27,6 @@ const AutocompleteWithMarginTop: typeof Autocomplete = styled(Autocomplete)` margin-top: 8px; `; -const tlsCertExtensions = ['crt']; -const tlsKeyExtensions = ['key']; - export interface IBlogOptionsProps { actualIP: string | undefined; workspace: IWorkspace; diff --git a/src/pages/EditWorkspace/index.tsx b/src/pages/EditWorkspace/index.tsx index 881b3633..5261c7eb 100644 --- a/src/pages/EditWorkspace/index.tsx +++ b/src/pages/EditWorkspace/index.tsx @@ -21,6 +21,7 @@ import { List, ListItem, ListItemText } from '@/components/ListItem'; import { useRestartSnackbar } from '@/components/RestartSnackbar'; import { TokenForm } from '@/components/TokenForm'; import { DEFAULT_USER_NAME, getTidGiAuthHeaderWithToken } from '@/constants/auth'; +import { wikiPictureExtensions } from '@/constants/fileNames'; import { useActualIp } from '@services/native/hooks'; import { SupportedStorageServices } from '@services/types'; import { isEqual } from 'lodash'; @@ -139,7 +140,6 @@ const getValidIconPath = (iconPath?: string | null): string => { }; const workspaceID = (window.meta as WindowMeta[WindowNames.editWorkspace]).workspaceID as string; -const wikiPictureExtensions = ['jpg', 'jpeg', 'png', 'gif', 'tiff', 'tif', 'bmp', 'dib']; export default function EditWorkspace(): JSX.Element { const { t } = useTranslation(); diff --git a/src/services/native/index.ts b/src/services/native/index.ts index 86af5983..db69ab8f 100644 --- a/src/services/native/index.ts +++ b/src/services/native/index.ts @@ -19,7 +19,7 @@ import { IWorkspaceService } from '@services/workspaces/interface'; import i18next from 'i18next'; import { ZxNotInitializedError } from './error'; import { findEditorOrDefault, findGitGUIAppOrDefault, launchExternalEditor } from './externalApp'; -import { INativeService } from './interface'; +import { INativeService, IPickDirectoryOptions } from './interface'; import { reportErrorToGithubWithTemplates } from './reportError'; @injectable() @@ -176,10 +176,11 @@ ${message.message} } } - public async pickDirectory(defaultPath?: string): Promise { + public async pickDirectory(defaultPath?: string, options?: IPickDirectoryOptions): Promise { const dialogResult = await dialog.showOpenDialog({ - properties: ['openDirectory'], + properties: options?.allowOpenFile === true ? ['openDirectory', 'openFile'] : ['openDirectory'], defaultPath, + filters: options?.filters, }); if (!dialogResult.canceled && dialogResult.filePaths.length > 0) { return dialogResult.filePaths; diff --git a/src/services/native/interface.ts b/src/services/native/interface.ts index 707d3864..75c0cb45 100644 --- a/src/services/native/interface.ts +++ b/src/services/native/interface.ts @@ -6,6 +6,14 @@ import { IZxFileInput } from '@services/wiki/wikiWorker'; import { WindowNames } from '@services/windows/WindowProperties'; import { ProxyPropertyType } from 'electron-ipc-cat/common'; +export interface IPickDirectoryOptions { + /** + * Only works in MacOS, will use openDirectory as default on other platforms + * @url https://github.com/electron/electron/issues/26885 + */ + allowOpenFile?: boolean; + filters?: Electron.OpenDialogOptions['filters']; +} /** * Wrap call to electron api, so we won't need remote module in renderer process */ @@ -33,7 +41,7 @@ export interface INativeService { openNewGitHubIssue(error: Error): Promise; openPath(filePath: string): Promise; path(method: 'basename' | 'dirname' | 'join', pathString: string | undefined, ...paths: string[]): Promise; - pickDirectory(defaultPath?: string): Promise; + pickDirectory(defaultPath?: string, options?: IPickDirectoryOptions): Promise; pickFile(filters?: Electron.OpenDialogOptions['filters']): Promise; quit(): void; /** diff --git a/src/services/wiki/wikiWorker.ts b/src/services/wiki/wikiWorker.ts index b4dd2a5a..3508f918 100644 --- a/src/services/wiki/wikiWorker.ts +++ b/src/services/wiki/wikiWorker.ts @@ -196,14 +196,19 @@ function executeZxScript(file: IZxFileInput, zxPath: string): Observable isHtmlWikiRegex.test(htmlWikiPath); + async function extractWikiHTML(htmlWikiPath: string, saveWikiFolderPath: string, constants: { TIDDLYWIKI_PACKAGE_FOLDER: string }): Promise { // tiddlywiki --load ./mywiki.html --savewikifolder ./mywikifolder // --savewikifolder [] // . /mywikifolder is the path where the tiddlder and plugins folders are stored const { TIDDLYWIKI_PACKAGE_FOLDER } = constants; - const reg = /(?:html|htm|Html|HTML|HTM|HTA|hta)$/; - const isHtmlWiki = reg.test(htmlWikiPath); - if (!isHtmlWiki) { + + if (!isHtmlWiki(htmlWikiPath)) { throw new Error(`Please enter the path to the tiddlywiki.html file. Current path can't be used. ${htmlWikiPath}`); } if (await exists(saveWikiFolderPath)) { @@ -226,12 +231,13 @@ async function extractWikiHTML(htmlWikiPath: string, saveWikiFolderPath: string, }); } -async function packetHTMLFromWikiFolder(folderWikiPath: string, folderToSaveWikiHtml: string, constants: { TIDDLYWIKI_PACKAGE_FOLDER: string }): Promise { +async function packetHTMLFromWikiFolder(folderWikiPath: string, pathOfNewHTML: string, constants: { TIDDLYWIKI_PACKAGE_FOLDER: string }): Promise { // tiddlywiki ./mywikifolder --rendertiddler '$:/core/save/all' mywiki.html text/plain // . /mywikifolder is the path to the wiki folder, which generally contains the tiddlder and plugins directories const { TIDDLYWIKI_PACKAGE_FOLDER } = constants; const wikiInstance = TiddlyWiki(); - wikiInstance.boot.argv = [folderWikiPath, '--rendertiddler', '$:/core/save/all', folderToSaveWikiHtml, 'text/plain']; + // a .html file path should be provided, but if provided a folder path, we can add /index.html to fix it. + wikiInstance.boot.argv = [folderWikiPath, '--rendertiddler', '$:/core/save/all', isHtmlWiki(pathOfNewHTML) ? pathOfNewHTML : `${pathOfNewHTML}/index.html`, 'text/plain']; await new Promise((resolve, reject) => { try { wikiInstance.boot.startup({ diff --git a/src/services/workspacesView/index.ts b/src/services/workspacesView/index.ts index 36c69174..151f8c57 100644 --- a/src/services/workspacesView/index.ts +++ b/src/services/workspacesView/index.ts @@ -7,6 +7,7 @@ import { injectable } from 'inversify'; import { DEFAULT_DOWNLOADS_PATH } from '@/constants/appPaths'; import { MetaDataChannel, WikiChannel } from '@/constants/channels'; +import { wikiHtmlExtensions } from '@/constants/fileNames'; import { tiddlywikiLanguagesMap } from '@/constants/languages'; import { WikiCreationMethod } from '@/constants/wikiCreation'; import type { IAuthenticationService } from '@services/auth/interface'; @@ -269,7 +270,10 @@ export class WorkspaceView implements IWorkspaceViewService { logger.error('Can not export whole wiki, activeWorkspace is undefined'); return; } - const folderToSaveWikiHtml = await this.nativeService.pickDirectory(DEFAULT_DOWNLOADS_PATH); + const folderToSaveWikiHtml = await this.nativeService.pickDirectory(DEFAULT_DOWNLOADS_PATH, { + allowOpenFile: true, + filters: [{ name: 'HTML', extensions: wikiHtmlExtensions }], + }); await this.wikiService.packetHTMLFromWikiFolder(activeWorkspace.wikiFolderLocation, folderToSaveWikiHtml[0]); }, enabled: hasWorkspaces,