mirror of
https://github.com/qaiu/netdisk-fast-download.git
synced 2026-12-04 02:23:59 +00:00
web: load more for paged folders in pane and tree views
Failed loads keep loaded items and offer retry; stale responses after navigation are dropped; select-all and folder batch selection warn when pages remain unloaded. Co-authored-by: qaiu <[email protected]>
This commit is contained in:
6 files changed
+347
-18
No files matched your search
@@ -0,0 +1,83 @@
|
||||
/**
|
||||
* 目录分页(/v2/getFileListPage)前端辅助。
|
||||
*
|
||||
* - 后端给出的目录链接是 /v2/getFileList?...,换成 /v2/getFileListPage 并带 page / pageCursor 即可分页。
|
||||
* - 不是 getFileList 链接的目录(例如第三方解析器返回的其它地址)不分页,按原样整体加载。
|
||||
* - 加载失败(如蓝奏「请刷新,重试」限流)只提示重试,已加载的页保留不动。
|
||||
*/
|
||||
const { fileIdentity } = require('./batchTreeCollect.js')
|
||||
|
||||
const MORE_TYPE = 'more'
|
||||
const LIST_PATH_RE = /^(.*?\/v2\/getFileList)(\?.*)?$/
|
||||
|
||||
function toPageUrl(listUrl, page = 1, cursor = '') {
|
||||
if (!listUrl) return null
|
||||
const m = String(listUrl).match(LIST_PATH_RE)
|
||||
if (!m) return null
|
||||
let url = `${m[1]}Page${m[2] || ''}`
|
||||
const extra = [`page=${encodeURIComponent(page)}`]
|
||||
if (cursor) extra.push(`pageCursor=${encodeURIComponent(cursor)}`)
|
||||
url += (url.includes('?') ? '&' : '?') + extra.join('&')
|
||||
return url
|
||||
}
|
||||
|
||||
/** 兼容两种响应:分页 envelope 或旧接口的纯数组。 */
|
||||
function readPage(data) {
|
||||
if (Array.isArray(data)) {
|
||||
return { list: data, paged: false, hasMore: false, nextPage: null, nextCursor: null }
|
||||
}
|
||||
const d = data || {}
|
||||
const hasMore = !!d.hasMore && Number.isInteger(d.nextPage)
|
||||
return {
|
||||
list: Array.isArray(d.list) ? d.list : [],
|
||||
paged: !!d.paged,
|
||||
hasMore,
|
||||
nextPage: hasMore ? d.nextPage : null,
|
||||
nextCursor: hasMore ? (d.nextCursor || null) : null,
|
||||
total: d.total ?? null
|
||||
}
|
||||
}
|
||||
|
||||
/** 追加下一页,按文件身份去重,保持已加载项的顺序。 */
|
||||
function appendUnique(existing, incoming) {
|
||||
const out = [...(existing || [])]
|
||||
const seen = new Set(out.map(fileIdentity))
|
||||
for (const item of incoming || []) {
|
||||
const key = fileIdentity(item)
|
||||
if (seen.has(key)) continue
|
||||
seen.add(key)
|
||||
out.push(item)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
/** 下一页的请求状态;没有更多时返回 null。 */
|
||||
function nextState(listUrl, page) {
|
||||
if (!listUrl || !page || !page.hasMore) return null
|
||||
return { listUrl, nextPage: page.nextPage, nextCursor: page.nextCursor, loading: false, error: '' }
|
||||
}
|
||||
|
||||
function makeMoreNode(parentId, state) {
|
||||
return {
|
||||
id: `${parentId}::more`,
|
||||
fileName: '加载更多…',
|
||||
fileType: MORE_TYPE,
|
||||
isLeaf: true,
|
||||
disabled: true,
|
||||
paging: state
|
||||
}
|
||||
}
|
||||
|
||||
function isMoreNode(data) {
|
||||
return !!(data && data.fileType === MORE_TYPE)
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
MORE_TYPE,
|
||||
toPageUrl,
|
||||
readPage,
|
||||
appendUnique,
|
||||
nextState,
|
||||
makeMoreNode,
|
||||
isMoreNode
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
const { describe, it } = require('node:test')
|
||||
const assert = require('node:assert/strict')
|
||||
|
||||
const {
|
||||
toPageUrl,
|
||||
readPage,
|
||||
appendUnique,
|
||||
nextState,
|
||||
makeMoreNode,
|
||||
isMoreNode
|
||||
} = require('./folderPaging.js')
|
||||
|
||||
function file(id) {
|
||||
return { fileId: id, fileName: `${id}.apk`, fileType: 'file', parserUrl: `/v2/redirectUrl/lz/${id}` }
|
||||
}
|
||||
|
||||
describe('toPageUrl', () => {
|
||||
it('switches getFileList to getFileListPage and keeps existing params', () => {
|
||||
assert.equal(
|
||||
toPageUrl('http://h/v2/getFileList?url=https%3A%2F%2Fa.com%2Fb1&dirId=7&auth=x%2By', 2, 'ab-_c='),
|
||||
'http://h/v2/getFileListPage?url=https%3A%2F%2Fa.com%2Fb1&dirId=7&auth=x%2By&page=2&pageCursor=ab-_c%3D'
|
||||
)
|
||||
})
|
||||
|
||||
it('works without query and without cursor', () => {
|
||||
assert.equal(toPageUrl('/v2/getFileList', 1), '/v2/getFileListPage?page=1')
|
||||
})
|
||||
|
||||
it('does not page unknown folder links', () => {
|
||||
assert.equal(toPageUrl('https://other/api/list?x=1', 2), null)
|
||||
assert.equal(toPageUrl('/v2/getFileListPage?url=a', 2), null)
|
||||
assert.equal(toPageUrl('', 2), null)
|
||||
})
|
||||
})
|
||||
|
||||
describe('readPage', () => {
|
||||
it('reads paged envelope', () => {
|
||||
const page = readPage({ list: [file('a')], paged: true, page: 1, hasMore: true, nextPage: 2, nextCursor: 'c' })
|
||||
assert.equal(page.list.length, 1)
|
||||
assert.equal(page.hasMore, true)
|
||||
assert.equal(page.nextPage, 2)
|
||||
assert.equal(page.nextCursor, 'c')
|
||||
})
|
||||
|
||||
it('treats legacy array as complete list', () => {
|
||||
const page = readPage([file('a'), file('b')])
|
||||
assert.equal(page.list.length, 2)
|
||||
assert.equal(page.hasMore, false)
|
||||
assert.equal(nextState('/v2/getFileList', page), null)
|
||||
})
|
||||
|
||||
it('hasMore without a valid nextPage is not followed', () => {
|
||||
assert.equal(readPage({ list: [], hasMore: true, nextPage: null }).hasMore, false)
|
||||
assert.equal(readPage(null).list.length, 0)
|
||||
})
|
||||
})
|
||||
|
||||
describe('appendUnique', () => {
|
||||
it('keeps loaded items and drops duplicates from the next page', () => {
|
||||
const merged = appendUnique([file('a'), file('b')], [file('b'), file('c')])
|
||||
assert.deepEqual(merged.map(f => f.fileId), ['a', 'b', 'c'])
|
||||
})
|
||||
|
||||
it('a failed (empty) load leaves loaded items untouched', () => {
|
||||
const loaded = [file('a')]
|
||||
assert.deepEqual(appendUnique(loaded, undefined), loaded)
|
||||
})
|
||||
})
|
||||
|
||||
describe('more node', () => {
|
||||
it('carries the next page state and is not a file or folder', () => {
|
||||
const state = nextState('/v2/getFileList?url=x', readPage({ list: [], hasMore: true, nextPage: 3, nextCursor: 'k' }))
|
||||
const node = makeMoreNode('fid:1', state)
|
||||
assert.equal(isMoreNode(node), true)
|
||||
assert.equal(node.isLeaf, true)
|
||||
assert.equal(node.paging.nextPage, 3)
|
||||
assert.equal(node.paging.nextCursor, 'k')
|
||||
assert.equal(isMoreNode(file('a')), false)
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user