From aad0a01880a9144497005095d1384a2fea920f5b Mon Sep 17 00:00:00 2001 From: Kiyoka Nishiyama Date: Sat, 7 May 2022 07:40:43 +0000 Subject: [PATCH] Applying Review Findings - Changed logic to check if the image path is an assets path. - Renamed funciton. - Removed 'async' and 'await' keyword. --- src/renderer/components/editorWithTabs/editor.vue | 4 ++-- src/renderer/util/fileSystem.js | 11 +++++------ 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/renderer/components/editorWithTabs/editor.vue b/src/renderer/components/editorWithTabs/editor.vue index 3c0d1faf..be311ad4 100644 --- a/src/renderer/components/editorWithTabs/editor.vue +++ b/src/renderer/components/editorWithTabs/editor.vue @@ -101,7 +101,7 @@ import Printer from '@/services/printService' import { SpellcheckerLanguageCommand } from '@/commands' import { SpellChecker } from '@/spellchecker' import { isOsx, animatedScrollTo } from '@/util' -import { IsIdenticalImagePath, moveImageToFolder, moveToRelativeFolder, uploadImage } from '@/util/fileSystem' +import { getRelativeImagePathIfIdentical, moveImageToFolder, moveToRelativeFolder, uploadImage } from '@/util/fileSystem' import { guessClipboardFilePath } from '@/util/clipboard' import { getCssForOptions, getHtmlToc } from '@/util/pdf' import { addCommonStyle, setEditorWidth } from '@/util/theme' @@ -738,7 +738,7 @@ export default { break } case 'folder': { - const { identical, relPath } = await IsIdenticalImagePath(relativeBasePath, resolvedImageRelativeDirectoryName, image) + const { identical, relPath } = getRelativeImagePathIfIdentical(relativeBasePath, resolvedImageRelativeDirectoryName, image) if (identical && isTabSavedOnDisk && imagePreferRelativeDirectory) { destImagePath = relPath } else { diff --git a/src/renderer/util/fileSystem.js b/src/renderer/util/fileSystem.js index 784264a6..ae594afc 100644 --- a/src/renderer/util/fileSystem.js +++ b/src/renderer/util/fileSystem.js @@ -6,7 +6,7 @@ import cp from 'child_process' import { tmpdir } from 'os' import dayjs from 'dayjs' import { Octokit } from '@octokit/rest' -import { isImageFile } from 'common/filesystem/paths' +import { isChildOfDirectory, isImageFile } from 'common/filesystem/paths' import { isWindows } from './index' export const create = async (pathname, type) => { @@ -34,14 +34,14 @@ export const getContentHash = content => { } /** - * Check two image paths are identical + * If the two paths are identical, return relative image file path * * @param {String} cwd The relative base path (project root or full folder path of opened file). * @param {String} relativeName The relative directory name of image assets. * @param {String} srcImagePath The source image file path. * @returns {{identical: boolean, relPath: string}} Returns the idencial or not and relativePath of image file. */ -export const IsIdenticalImagePath = async (cwd, relativeName, srcImagePath) => { +export const getRelativeImagePathIfIdentical = (cwd, relativeName, srcImagePath) => { let identical = false let relPath = '' if (typeof srcImagePath !== 'string') { @@ -58,9 +58,8 @@ export const IsIdenticalImagePath = async (cwd, relativeName, srcImagePath) => { // Path combination: // - root directory + relative directory name const absAssetsDirPath = path.resolve(cwd, relativeName) - const absAssetsFilePath = path.resolve(absAssetsDirPath, path.basename(srcImagePath)) - if (absAssetsFilePath === srcImagePath) { - // Find relative path between given file and saved image. + const absAssetsFilePath = path.resolve(cwd, srcImagePath) + if (isChildOfDirectory(absAssetsDirPath, absAssetsFilePath)) { relPath = path.relative(cwd, srcImagePath) if (isWindows) { // Use forward slashes for better compatibility with websites.