mirror of
https://github.com/qaiu/netdisk-fast-download.git
synced 2026-12-04 02:23:59 +00:00
web: filter loaded filenames and scope tree select-all
Both directory views and the standalone list page filter already-loaded names, ignoring case and surrounding spaces. Tree select-all and deselect only change the current matches, and a new cursor page stays filtered without being selected. Co-authored-by: qaiu <[email protected]>
This commit is contained in:
5 files changed
+884
-81
No files matched your search
@@ -0,0 +1,155 @@
|
||||
/**
|
||||
* 目录文件名筛选与批量勾选范围。
|
||||
*
|
||||
* 只处理已经加载的行,不发起下一页请求。分页仍由 folderPaging 的
|
||||
* nextCursor / pageCursor 推进;这里只决定新页里哪些行可见、哪些会被全选。
|
||||
*/
|
||||
const { fileIdentity, uniqueFiles } = require('./batchTreeCollect')
|
||||
const { appendUnique, nextState } = require('./folderPaging')
|
||||
|
||||
function normalizeQuery(keyword) {
|
||||
return String(keyword == null ? '' : keyword).trim().toLowerCase()
|
||||
}
|
||||
|
||||
function matchesFileName(file, keyword) {
|
||||
const query = normalizeQuery(keyword)
|
||||
if (!query) return true
|
||||
return String((file && file.fileName) || '').toLowerCase().includes(query)
|
||||
}
|
||||
|
||||
/** 空关键字返回副本,不清空调用方持有的已加载数组。 */
|
||||
function filterByFileName(list, keyword) {
|
||||
const rows = list || []
|
||||
const query = normalizeQuery(keyword)
|
||||
if (!query) return rows.slice()
|
||||
return rows.filter(file => matchesFileName(file, keyword))
|
||||
}
|
||||
|
||||
function isSelectableFile(file) {
|
||||
return !!(
|
||||
file
|
||||
&& file.parserUrl
|
||||
&& file.fileType !== 'folder'
|
||||
&& file.fileType !== 'url'
|
||||
&& file.fileType !== 'more'
|
||||
)
|
||||
}
|
||||
|
||||
function countableEntries(list) {
|
||||
return (list || []).filter(file => file && file.fileType !== 'more' && file.id !== 'root')
|
||||
}
|
||||
|
||||
function filterSummary(loaded, keyword) {
|
||||
const rows = countableEntries(loaded)
|
||||
const matched = filterByFileName(rows, keyword)
|
||||
return {
|
||||
loaded: rows.length,
|
||||
matched: matched.length,
|
||||
text: `仅筛选已加载的 ${rows.length} 项,匹配 ${matched.length} 项`
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 全选:只并入当前关键字命中的已加载可选文件。
|
||||
* 不在这份可见集合里的原选择保留,隐藏项不会被加进来。
|
||||
*/
|
||||
function selectMatching(selected, loaded, keyword, isSelectable = isSelectableFile) {
|
||||
const visible = filterByFileName(loaded, keyword).filter(file => isSelectable(file))
|
||||
const visibleKeys = new Set(visible.map(fileIdentity))
|
||||
const kept = (selected || []).filter(file => !visibleKeys.has(fileIdentity(file)))
|
||||
return uniqueFiles(kept.concat(visible))
|
||||
}
|
||||
|
||||
/** 取消全选:只移出当前可见可选文件,范围外的选择不动。 */
|
||||
function deselectMatching(selected, loaded, keyword, isSelectable = isSelectableFile) {
|
||||
const visibleKeys = new Set(
|
||||
filterByFileName(loaded, keyword)
|
||||
.filter(file => isSelectable(file))
|
||||
.map(fileIdentity)
|
||||
)
|
||||
return (selected || []).filter(file => !visibleKeys.has(fileIdentity(file)))
|
||||
}
|
||||
|
||||
function toggleSelected(selected, file, isSelectable = isSelectableFile) {
|
||||
const list = (selected || []).slice()
|
||||
if (!isSelectable(file)) return list
|
||||
const key = fileIdentity(file)
|
||||
const index = list.findIndex(item => fileIdentity(item) === key)
|
||||
if (index >= 0) {
|
||||
list.splice(index, 1)
|
||||
return list
|
||||
}
|
||||
list.push(file)
|
||||
return list
|
||||
}
|
||||
|
||||
function isSelected(selected, file) {
|
||||
const key = fileIdentity(file)
|
||||
return (selected || []).some(item => fileIdentity(item) === key)
|
||||
}
|
||||
|
||||
/**
|
||||
* 已加载树节点。未展开的文件夹不往下走,因此不会把未请求的子项算进全选范围。
|
||||
* 「加载更多」占位节点跳过。
|
||||
*/
|
||||
function collectLoadedNodes(node) {
|
||||
const out = []
|
||||
for (const child of (node && node.childNodes) || []) {
|
||||
const data = child && child.data
|
||||
if (!data || data.fileType === 'more') continue
|
||||
out.push(data)
|
||||
if (child.loaded) {
|
||||
out.push(...collectLoadedNodes(child))
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
/**
|
||||
* 用目录分页的 nextCursor 追加一页。
|
||||
* 已有选择原样保留:新页即使命中关键字也不会被静默勾上。
|
||||
*/
|
||||
function applyCursorPage(state, page) {
|
||||
const loaded = appendUnique(state.loaded, (page && page.list) || [])
|
||||
const listUrl = (state.more && state.more.listUrl) || state.listUrl
|
||||
return {
|
||||
keyword: state.keyword,
|
||||
view: state.view,
|
||||
loaded,
|
||||
selected: (state.selected || []).slice(),
|
||||
listUrl,
|
||||
more: nextState(listUrl, page)
|
||||
}
|
||||
}
|
||||
|
||||
function switchView(state, view) {
|
||||
return {
|
||||
...state,
|
||||
view,
|
||||
keyword: state.keyword,
|
||||
selected: (state.selected || []).slice()
|
||||
}
|
||||
}
|
||||
|
||||
function clearKeyword(state) {
|
||||
return {
|
||||
...state,
|
||||
keyword: ''
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
normalizeQuery,
|
||||
matchesFileName,
|
||||
filterByFileName,
|
||||
isSelectableFile,
|
||||
filterSummary,
|
||||
selectMatching,
|
||||
deselectMatching,
|
||||
toggleSelected,
|
||||
isSelected,
|
||||
collectLoadedNodes,
|
||||
applyCursorPage,
|
||||
switchView,
|
||||
clearKeyword
|
||||
}
|
||||
@@ -0,0 +1,237 @@
|
||||
const { describe, it } = require('node:test')
|
||||
const assert = require('node:assert/strict')
|
||||
|
||||
const { readPage } = require('./folderPaging')
|
||||
const {
|
||||
normalizeQuery,
|
||||
filterByFileName,
|
||||
filterSummary,
|
||||
selectMatching,
|
||||
deselectMatching,
|
||||
toggleSelected,
|
||||
isSelected,
|
||||
collectLoadedNodes,
|
||||
applyCursorPage,
|
||||
switchView,
|
||||
clearKeyword
|
||||
} = require('./nameFilter')
|
||||
|
||||
function file(name, id, extra = {}) {
|
||||
return {
|
||||
fileName: name,
|
||||
fileId: id,
|
||||
fileType: 'file',
|
||||
parserUrl: '/v2/redirectUrl/lz/same',
|
||||
...extra
|
||||
}
|
||||
}
|
||||
|
||||
function folder(name, id) {
|
||||
return {
|
||||
fileName: name,
|
||||
fileId: id,
|
||||
fileType: 'folder',
|
||||
parserUrl: `/v2/getFileList?dir=${id}`
|
||||
}
|
||||
}
|
||||
|
||||
function baseState(overrides = {}) {
|
||||
return {
|
||||
keyword: '',
|
||||
view: 'pane',
|
||||
loaded: [],
|
||||
selected: [],
|
||||
listUrl: '/v2/getFileList?url=share',
|
||||
more: null,
|
||||
...overrides
|
||||
}
|
||||
}
|
||||
|
||||
describe('filename filter', () => {
|
||||
const loaded = [
|
||||
file('Alpha.TXT', '1'),
|
||||
file('notes.md', '2'),
|
||||
file(' photo.JPG', '3'),
|
||||
folder('Albums', '4')
|
||||
]
|
||||
|
||||
it('trims and matches a case-insensitive substring', () => {
|
||||
assert.equal(normalizeQuery(' ApK '), 'apk')
|
||||
const matched = filterByFileName(loaded, ' aL ')
|
||||
assert.deepEqual(matched.map(item => item.fileId), ['1', '4'])
|
||||
})
|
||||
|
||||
it('blank keyword restores the loaded list without dropping rows', () => {
|
||||
const cleared = filterByFileName(loaded, ' ')
|
||||
assert.deepEqual(cleared.map(item => item.fileId), ['1', '2', '3', '4'])
|
||||
assert.notEqual(cleared, loaded)
|
||||
})
|
||||
|
||||
it('describes loaded N and matched M', () => {
|
||||
const summary = filterSummary(loaded, 'photo')
|
||||
assert.equal(summary.loaded, 4)
|
||||
assert.equal(summary.matched, 1)
|
||||
assert.equal(summary.text, '仅筛选已加载的 4 项,匹配 1 项')
|
||||
})
|
||||
})
|
||||
|
||||
describe('select and deselect scope', () => {
|
||||
const outside = file('kept.zip', 'out', { parserUrl: '/v2/redirectUrl/lz/out' })
|
||||
const match = file('Alpha.TXT', '1')
|
||||
const other = file('Beta.bin', '2')
|
||||
const hiddenFolder = folder('Alpha-dir', '3')
|
||||
const link = file('Alpha-link.url', '4', { fileType: 'url', parserUrl: '' })
|
||||
const loaded = [match, other, hiddenFolder, link]
|
||||
|
||||
it('selects only loaded selectable files matching the keyword', () => {
|
||||
const selected = selectMatching([outside], loaded, 'alpha')
|
||||
assert.deepEqual(selected.map(item => item.fileId), ['out', '1'])
|
||||
assert.equal(isSelected(selected, other), false)
|
||||
assert.equal(isSelected(selected, hiddenFolder), false)
|
||||
})
|
||||
|
||||
it('deselect removes only the current match and keeps selections outside it', () => {
|
||||
const selected = deselectMatching([outside, match, other], loaded, 'alpha')
|
||||
assert.deepEqual(selected.map(item => item.fileId), ['out', '2'])
|
||||
})
|
||||
|
||||
it('does not select a loaded file hidden by the keyword', () => {
|
||||
const selected = selectMatching([other], loaded, 'alpha')
|
||||
assert.equal(isSelected(selected, other), true)
|
||||
assert.equal(isSelected(selected, match), true)
|
||||
const narrowed = selectMatching(selected, loaded, 'beta')
|
||||
assert.deepEqual(narrowed.map(item => item.fileId).sort(), ['1', '2'])
|
||||
})
|
||||
})
|
||||
|
||||
describe('same name, different id', () => {
|
||||
const first = file('Report.TXT', 'a')
|
||||
const second = file('Report.TXT', 'b')
|
||||
|
||||
it('keeps both files when selecting and can toggle one without the other', () => {
|
||||
const selected = selectMatching([], [first, second], 'report')
|
||||
assert.equal(selected.length, 2)
|
||||
assert.notEqual(selected[0], selected[1])
|
||||
const onlySecond = toggleSelected(selected, first)
|
||||
assert.deepEqual(onlySecond.map(item => item.fileId), ['b'])
|
||||
assert.equal(isSelected(onlySecond, first), false)
|
||||
assert.equal(isSelected(onlySecond, second), true)
|
||||
})
|
||||
|
||||
it('distinguishes files that only differ by id when fileId is absent', () => {
|
||||
const left = { fileName: 'Report.TXT', id: 'x', fileType: 'file', parserUrl: '/v2/redirectUrl/lz/same' }
|
||||
const right = { fileName: 'Report.TXT', id: 'y', fileType: 'file', parserUrl: '/v2/redirectUrl/lz/same' }
|
||||
const selected = toggleSelected([], left)
|
||||
assert.equal(isSelected(selected, right), false)
|
||||
assert.equal(toggleSelected(selected, right).length, 2)
|
||||
})
|
||||
})
|
||||
|
||||
describe('cursor page', () => {
|
||||
it('shows new matches from the next cursor without selecting them', () => {
|
||||
let state = baseState({
|
||||
keyword: 'alpha',
|
||||
loaded: [file('Alpha.TXT', '1'), file('Beta.bin', '2')],
|
||||
selected: [file('Beta.bin', '2')],
|
||||
more: { listUrl: '/v2/getFileList?url=share', nextPage: 2, nextCursor: 'c1', loading: false, error: '' }
|
||||
})
|
||||
assert.deepEqual(filterByFileName(state.loaded, state.keyword).map(item => item.fileId), ['1'])
|
||||
|
||||
state = applyCursorPage(state, readPage({
|
||||
list: [file('ALPHA-extra.txt', '3'), file('Gamma.bin', '4')],
|
||||
paged: true,
|
||||
hasMore: true,
|
||||
nextPage: 3,
|
||||
nextCursor: 'c2'
|
||||
}))
|
||||
|
||||
assert.deepEqual(state.loaded.map(item => item.fileId), ['1', '2', '3', '4'])
|
||||
assert.deepEqual(filterByFileName(state.loaded, state.keyword).map(item => item.fileId), ['1', '3'])
|
||||
assert.deepEqual(state.selected.map(item => item.fileId), ['2'])
|
||||
assert.equal(state.more.nextCursor, 'c2')
|
||||
assert.equal(state.more.nextPage, 3)
|
||||
})
|
||||
|
||||
it('keeps load-more available when nothing matches yet', () => {
|
||||
let state = baseState({
|
||||
keyword: 'zzzz',
|
||||
loaded: [file('Alpha.TXT', '1')],
|
||||
more: { listUrl: '/v2/getFileList?url=share', nextPage: 2, nextCursor: 'c1', loading: false, error: '' }
|
||||
})
|
||||
assert.equal(filterByFileName(state.loaded, state.keyword).length, 0)
|
||||
assert.equal(state.more.nextCursor, 'c1')
|
||||
|
||||
state = applyCursorPage(state, readPage({
|
||||
list: [file('zzzz-found.txt', '9')],
|
||||
hasMore: true,
|
||||
nextPage: 3,
|
||||
nextCursor: 'c2'
|
||||
}))
|
||||
assert.deepEqual(filterByFileName(state.loaded, state.keyword).map(item => item.fileId), ['9'])
|
||||
assert.equal(state.selected.length, 0)
|
||||
assert.ok(state.more)
|
||||
})
|
||||
})
|
||||
|
||||
describe('view switch and clear', () => {
|
||||
it('keeps the keyword and selected identities when the view changes', () => {
|
||||
const chosen = file('Alpha.TXT', '1')
|
||||
const state = baseState({
|
||||
keyword: ' Alpha ',
|
||||
view: 'pane',
|
||||
loaded: [chosen, file('Beta.bin', '2')],
|
||||
selected: [chosen]
|
||||
})
|
||||
const tree = switchView(state, 'tree')
|
||||
assert.equal(tree.view, 'tree')
|
||||
assert.equal(tree.keyword, ' Alpha ')
|
||||
assert.deepEqual(tree.selected.map(item => item.fileId), ['1'])
|
||||
const back = switchView(tree, 'pane')
|
||||
assert.equal(back.keyword, state.keyword)
|
||||
assert.equal(isSelected(back.selected, chosen), true)
|
||||
})
|
||||
|
||||
it('clearing the keyword shows every loaded row again', () => {
|
||||
const state = baseState({
|
||||
keyword: 'alpha',
|
||||
loaded: [file('Alpha.TXT', '1'), file('Beta.bin', '2')]
|
||||
})
|
||||
assert.deepEqual(filterByFileName(state.loaded, state.keyword).map(item => item.fileId), ['1'])
|
||||
const cleared = clearKeyword(state)
|
||||
assert.deepEqual(
|
||||
filterByFileName(cleared.loaded, cleared.keyword).map(item => item.fileId),
|
||||
['1', '2']
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe('loaded tree nodes', () => {
|
||||
it('skips unloaded folders, hidden placeholders, and does not invent a fetch', () => {
|
||||
const secret = file('secret.txt', 'secret')
|
||||
const more = { data: { fileType: 'more', fileName: '加载更多…' }, loaded: true, childNodes: [] }
|
||||
const root = {
|
||||
childNodes: [
|
||||
{ data: file('root.txt', 'root'), loaded: true, childNodes: [] },
|
||||
{
|
||||
data: folder('closed', 'closed'),
|
||||
loaded: false,
|
||||
childNodes: [{ data: secret, loaded: true, childNodes: [] }]
|
||||
},
|
||||
{
|
||||
data: folder('open', 'open'),
|
||||
loaded: true,
|
||||
childNodes: [
|
||||
{ data: file('open-a.txt', 'oa'), loaded: true, childNodes: [] },
|
||||
more
|
||||
]
|
||||
},
|
||||
more
|
||||
]
|
||||
}
|
||||
const loaded = collectLoadedNodes(root)
|
||||
assert.deepEqual(loaded.map(item => item.fileId), ['root', 'closed', 'open', 'oa'])
|
||||
const selected = selectMatching([], loaded, '')
|
||||
assert.deepEqual(selected.map(item => item.fileId), ['root', 'oa'])
|
||||
assert.equal(isSelected(selected, secret), false)
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user