dsh-half-page 0.3.32 → 0.3.34

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/client.js +7 -4
  2. package/index.js +20 -1
  3. package/package.json +1 -1
package/client.js CHANGED
@@ -86,7 +86,9 @@ window.__ModuleLoader__.load({
86
86
  return n + cur
87
87
  }
88
88
  const chapterNo = (title) => {
89
- const m = String(title || '').match(/第([0-90-9零〇一二三四五六七八九十百千两]+)章/)
89
+ // 标题可能是「第 3–7 章」(带空格与范围),所以先去空格再取第一个数字
90
+ const t = String(title || '').replace(/\s+/g, '')
91
+ const m = t.match(/第([0-90-9零〇一二三四五六七八九十百千两]+)/)
90
92
  return m ? cnNum(m[1]) : NaN
91
93
  }
92
94
  // 有几章、从第几章到第几章(章名认不出数字就不显示范围)
@@ -334,13 +336,14 @@ window.__ModuleLoader__.load({
334
336
  const page = Math.min(v.page || 0, pages - 1)
335
337
  // 排序:章节升 / 章节降 / 时间新在前(没写时间的排在后面)
336
338
  const sortLabels = ['章节 \u2191', '章节 \u2193', '时间 \u2193']
337
- const sortMode = v.sort || 0
339
+ const sortMode = (v.sort === undefined ? ((state && state.noteSort) || 0) : v.sort) % 3
338
340
  const sorted = bk.chapters.slice()
339
341
  if (sortMode === 1) sorted.sort((a, b) => (chapterNo(b.title) || -1) - (chapterNo(a.title) || -1))
340
342
  else if (sortMode === 2) sorted.sort((a, b) => String(b.at || '').localeCompare(String(a.at || '')))
341
343
  else sorted.sort((a, b) => (chapterNo(a.title) || 1e9) - (chapterNo(b.title) || 1e9))
342
344
  const list = all ? sorted : sorted.slice(page * NOTE_PAGE, page * NOTE_PAGE + NOTE_PAGE)
343
345
  const setView = (patch) => setNoteView(Object.assign({}, noteView, { [bi]: Object.assign({}, v, patch) }))
346
+ const saveSort = (n) => { try { getJson('/dsh-reading.notesort', { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify({ sort: n }) }) } catch (e) {} }
344
347
  const paged = total > NOTE_PAGE
345
348
  const tabs = h('div', { style: { ...S.row, gap: 6, marginBottom: 8, flexWrap: 'wrap' } },
346
349
  notes.map((n, ni) => h('button', {
@@ -358,10 +361,10 @@ window.__ModuleLoader__.load({
358
361
  paged ? h('div', { style: { ...S.dim, minWidth: 46, textAlign: 'center' } }, all ? '全部' : ((page + 1) + ' / ' + pages)) : null,
359
362
  paged ? h('button', { style: S.btn, disabled: all || page >= pages - 1, onClick: () => setView({ page: page + 1 }) }, '›') : null,
360
363
  paged ? h('button', { style: all ? S.btnOn : S.btn, onClick: () => setView({ all: !all, page: 0 }) }, all ? '分页' : '全部') : null,
361
- h('button', { style: sortMode === 2 ? S.btn : S.btnOn, onClick: () => setView({ sort: sortMode === 1 ? 0 : (sortMode === 0 ? 1 : 0), page: 0 }) }, sortMode === 1 ? '章节 ↓' : '章节 ↑'),
362
- h('button', { style: sortMode === 2 ? S.btnOn : S.btn, onClick: () => setView({ sort: 2, page: 0 }) }, '时间 ↓'),
364
+ h('button', { style: S.btnOn, onClick: () => { const next = (sortMode + 1) % 3; setView({ sort: next, page: 0 }); saveSort(next) } }, sortLabels[sortMode]),
363
365
  h('button', { style: S.btn, onClick: () => copy(bk.chapters.map((c) => '## ' + c.title + '\n' + c.lines.join('\n')).join('\n\n')) }, '复制这本'),
364
366
  ),
367
+ (sortMode === 2 && bk.chapters.filter((c) => !c.at).length) ? h('div', { style: { ...S.dim, marginBottom: 6 } }, '有 ' + bk.chapters.filter((c) => !c.at).length + ' 段早期笔记当时没记时间,排在最后。') : null,
365
368
  list.map((c, ci) => h('div', { key: ci, style: { marginBottom: 6 } },
366
369
  h('div', { style: S.row },
367
370
  h('div', { style: { ...S.dim, color: C.warn } }, c.title),
package/index.js CHANGED
@@ -268,7 +268,7 @@ export function apply(ctx) {
268
268
  let notes = ''
269
269
  try { notes = await readFile(await notesPath(dir), 'utf8') } catch {}
270
270
  res.setHeader('content-type', 'application/json; charset=utf-8')
271
- res.end(JSON.stringify({ ok: true, dir, perDay, today: today(), readToday: rt, current: st.current, books, notes }))
271
+ res.end(JSON.stringify({ ok: true, dir, perDay, today: today(), readToday: rt, current: st.current, books, notes, noteSort: Number(cfgNow.noteSort) || 0 }))
272
272
  },
273
273
  }),
274
274
  )
@@ -742,6 +742,25 @@ async function signWork(rel, who) {
742
742
  }),
743
743
  )
744
744
 
745
+ // 读后感的排序方式(0=章节升 1=章节降 2=时间降),存在 reading.config.json 里
746
+ ctx.effect(() =>
747
+ ctx.webServer.register({
748
+ kind: 'exact',
749
+ path: '/dsh-reading.notesort',
750
+ async handler(req, res) {
751
+ let body = ''
752
+ for await (const chunk of req) body += chunk
753
+ let patch = {}
754
+ try { patch = JSON.parse(body || '{}') } catch {}
755
+ const sort = Math.max(0, Math.min(2, Number(patch.sort) || 0))
756
+ const cfg = { ...(await config()) }
757
+ await writeJson(CONFIG, { ...cfg, noteSort: sort })
758
+ res.setHeader('content-type', 'application/json; charset=utf-8')
759
+ res.end(JSON.stringify({ ok: true, noteSort: sort }))
760
+ },
761
+ }),
762
+ )
763
+
745
764
  // 书籍状态:在读 / 已读完 / 弃坑(空 status 表示清掉)
746
765
  ctx.effect(() =>
747
766
  ctx.webServer.register({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-half-page",
3
- "version": "0.3.32",
3
+ "version": "0.3.34",
4
4
  "private": false,
5
5
  "author": "鹿野 <luye@dsh.local>",
6
6
  "license": "MIT",