fix: #878 select existing tab (#986)

This commit is contained in:
Felix Häusler 2019-04-28 19:23:42 +02:00 committed by Ran Luo
parent 1ab1e853f4
commit 3b9c16779d
3 changed files with 42 additions and 1 deletions

View File

@ -16,6 +16,7 @@
- Fixed some commonmark failed examples and add test case (#943)
- Fixed some bugs after press `backspace` (#934, #938)
- Change `inline math` vertical align to `top` (#977)
- Prevent to open the same file twice, instead select the existing tab (#878)
### 0.14.0

View File

@ -2,6 +2,7 @@ import { clipboard, ipcRenderer, shell } from 'electron'
import path from 'path'
import bus from '../bus'
import { hasKeys } from '../util'
import { isSameFileSync } from '../util/fileSystem'
import listToTree from '../util/listToTree'
import { createDocumentState, getOptionsFromState, getSingleFileState, getBlankFileState } from './help'
import notice from '../services/notification'
@ -392,6 +393,7 @@ const actions = {
}
},
// This event is only used when loading a file during window creation.
LISTEN_FOR_OPEN_SINGLE_FILE ({ commit, state, dispatch }) {
ipcRenderer.on('AGANI::open-single-file', (e, { markdown, filename, pathname, options }) => {
const fileState = getSingleFileState({ markdown, filename, pathname, options })
@ -410,6 +412,7 @@ const actions = {
})
},
// Open a new tab, optionally with content.
LISTEN_FOR_NEW_TAB ({ dispatch }) {
ipcRenderer.on('AGANI::new-tab', (e, markdownDocument) => {
if (markdownDocument) {
@ -457,6 +460,15 @@ const actions = {
return
}
// Check if tab already exist and select existing tab if so.
const { tabs } = state
const { pathname } = markdownDocument
const existingTab = tabs.find(t => isSameFileSync(t.pathname, pathname))
if (existingTab) {
dispatch('UPDATE_CURRENT_FILE', existingTab)
return
}
dispatch('SHOW_TAB_VIEW', false)
const { markdown, isMixedLineEndings } = markdownDocument

View File

@ -1,3 +1,4 @@
import path from 'path'
import fse from 'fs-extra'
export const create = (pathname, type) => {
@ -15,3 +16,30 @@ export const paste = ({ src, dest, type }) => {
export const rename = (src, dest) => {
return fse.move(src, dest)
}
/**
* Check if the both paths point to the same file.
*
* @param {*} pathA The first path.
* @param {*} pathB The second path.
* @param {*} isNormalized Are both paths already normalized.
*/
export const isSameFileSync = (pathA, pathB, isNormalized=false) => {
if (!pathA || !pathB) return false
const a = isNormalized ? pathA : path.normalize(pathA)
const b = isNormalized ? pathB : path.normalize(pathB)
if (a.length !== b.length) {
return false
} else if (a === b) {
return true
} else if (a.toLowerCase() === b.toLowerCase()) {
try {
const fiA = fse.statSync(a)
const fiB = fse.statSync(b)
return fiA.ino === fiB.ino
} catch (_) {
// Ignore error
}
}
return false
}