forked from marktext/marktext
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquickOpen.js
More file actions
202 lines (173 loc) · 5.29 KB
/
quickOpen.js
File metadata and controls
202 lines (173 loc) · 5.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import path from 'path'
import { ipcRenderer } from 'electron'
import { isChildOfDirectory, hasMarkdownExtension, MARKDOWN_INCLUSIONS } from '../../common/filesystem/paths'
import bus from '../bus'
import { delay } from '@/util'
import FileSearcher from '@/node/fileSearcher'
const SPECIAL_CHARS = /[\[\]\\^$.\|\?\*\+\(\)\/]{1}/g // eslint-disable-line no-useless-escape
// The quick open command
class QuickOpenCommand {
constructor (rootState) {
this.id = 'file.quick-open'
this.description = 'File: Quick Open'
this.placeholder = 'Search file to open'
this.shortcut = null
this.subcommands = []
this.subcommandSelectedIndex = -1
// Reference to folder and editor and project state.
this._editorState = rootState.editor
this._folderState = rootState.project
this._directorySearcher = new FileSearcher()
this._cancelFn = null
}
search = async query => {
// Show opened files when no query given.
if (!query) {
return this.subcommands
}
const { _cancelFn } = this
if (_cancelFn) {
_cancelFn()
this._cancelFn = null
}
const timeout = delay(300)
this._cancelFn = () => {
timeout.cancel()
this._cancelFn = null
}
await timeout
return this._doSearch(query)
}
run = async () => {
const { _editorState, _folderState } = this
if (!_folderState.projectTree && _editorState.tabs.length === 0) {
throw new Error(null)
}
this.subcommands = _editorState.tabs
.map(t => t.pathname)
// Filter untitled tabs
.filter(t => !!t)
.map(pathname => {
const item = { id: pathname }
Object.assign(item, this._getPath(pathname))
return item
})
}
execute = async () => {
// Timeout to hide the command palette and then show again to prevent issues.
await delay(100)
bus.$emit('show-command-palette', this)
}
executeSubcommand = async id => {
const { windowId } = global.marktext.env
ipcRenderer.send('mt::open-file-by-window-id', windowId, id)
}
unload = () => {
this.subcommands = []
}
// --- private ------------------------------------------
_doSearch = query => {
this._cancelFn = null
const { _editorState, _folderState } = this
const isRootDirOpened = !!_folderState.projectTree
const tabsAvailable = _editorState.tabs.length > 0
// Only show opened files if no directory is opened.
if (!isRootDirOpened && !tabsAvailable) {
return []
}
const searchResult = []
const rootPath = isRootDirOpened ? _folderState.projectTree.pathname : null
// Add files that are not in the current root directory but opened.
if (tabsAvailable) {
const re = new RegExp(query.replace(SPECIAL_CHARS, p => {
if (p === '*') return '.*'
return p === '\\' ? '\\\\' : `\\${p}`
}), 'i')
for (const tab of _editorState.tabs) {
const { pathname } = tab
if (pathname && re.test(pathname) &&
(!rootPath || !isChildOfDirectory(rootPath, pathname))
) {
searchResult.push(pathname)
}
}
}
if (!isRootDirOpened) {
return searchResult
.map(pathname => {
return {
id: pathname,
description: pathname,
title: pathname
}
})
}
// Search root directory on disk.
return new Promise((resolve, reject) => {
let canceled = false
const promises = this._directorySearcher.search([rootPath], '', {
didMatch: result => {
if (canceled) return
searchResult.push(result)
},
didSearchPaths: numPathsFound => {
// Cancel when more than 30 files were found. User should specify the search query.
if (!canceled && numPathsFound > 30) {
canceled = true
if (promises.cancel) {
promises.cancel()
}
}
},
// Only search markdown files that contain the query string.
inclusions: this._getInclusions(query)
})
.then(() => {
this._cancelFn = null
resolve(
searchResult
.map(pathname => {
const item = { id: pathname }
Object.assign(item, this._getPath(pathname))
return item
})
)
})
.catch(error => {
this._cancelFn = null
reject(error)
})
this._cancelFn = () => {
this._cancelFn = null
canceled = true
if (promises.cancel) {
promises.cancel()
}
}
})
}
_getInclusions = query => {
// NOTE: This will fail on `foo.m` because we search for `foo.m.md`.
if (hasMarkdownExtension(query)) {
return [`*${query}`]
}
const inclusions = []
for (let i = 0; i < MARKDOWN_INCLUSIONS.length; ++i) {
inclusions[i] = `*${query}` + MARKDOWN_INCLUSIONS[i]
}
return inclusions
}
_getPath = pathname => {
const rootPath = this._folderState.projectTree.pathname
if (!isChildOfDirectory(rootPath, pathname)) {
return { title: pathname, description: pathname }
}
const p = path.relative(rootPath, pathname)
const item = { description: p }
if (p.length > 50) {
item.title = p
}
return item
}
}
export default QuickOpenCommand