dsh-half-page 0.3.24 → 0.3.26

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 +45 -7
  2. package/index.js +15 -3
  3. package/package.json +1 -1
package/client.js CHANGED
@@ -60,7 +60,35 @@ window.__ModuleLoader__.load({
60
60
  return out
61
61
  }
62
62
 
63
- function Panel() {
63
+ const CN_DIGIT = { 零: 0, 〇: 0, 一: 1, 二: 2, 两: 2, 三: 3, 四: 4, 五: 5, 六: 6, 七: 7, 八: 8, 九: 9 }
64
+ const cnNum = (s) => {
65
+ const t = String(s || '').replace(/[0-9]/g, (d) => String.fromCharCode(d.charCodeAt(0) - 65248))
66
+ if (/^[0-9]+$/.test(t)) return Number(t)
67
+ let n = 0
68
+ let cur = 0
69
+ for (const ch of t) {
70
+ if (ch === '十') { n += (cur || 1) * 10; cur = 0 }
71
+ else if (ch === '百') { n += (cur || 1) * 100; cur = 0 }
72
+ else if (ch === '千') { n += (cur || 1) * 1000; cur = 0 }
73
+ else if (CN_DIGIT[ch] !== undefined) cur = CN_DIGIT[ch]
74
+ else return NaN
75
+ }
76
+ return n + cur
77
+ }
78
+ const chapterNo = (title) => {
79
+ const m = String(title || '').match(/第([0-90-9零〇一二三四五六七八九十百千两]+)章/)
80
+ return m ? cnNum(m[1]) : NaN
81
+ }
82
+ // 有几章、从第几章到第几章(章名认不出数字就不显示范围)
83
+ const chapterRange = (chapters) => {
84
+ const nos = (chapters || []).map((c) => chapterNo(c && c.title)).filter((n) => Number.isFinite(n))
85
+ if (!nos.length) return ''
86
+ const a = Math.min.apply(null, nos)
87
+ const b = Math.max.apply(null, nos)
88
+ return a === b ? ('第 ' + a + ' 章') : ('第 ' + a + '–' + b + ' 章')
89
+ }
90
+
91
+ function Panel() {
64
92
  const [state, setState] = useState(null)
65
93
  const [confirmId, setConfirmId] = useState('')
66
94
  const [tab, setTab] = useState('read')
@@ -73,6 +101,7 @@ window.__ModuleLoader__.load({
73
101
  const [busy, setBusy] = useState(false)
74
102
  const [recent, setRecent] = useState(null)
75
103
  const [noteView, setNoteView] = useState({})
104
+ const [bookAct, setBookAct] = useState(0)
76
105
  const NOTE_PAGE = 10
77
106
  let importPath = ''
78
107
 
@@ -284,8 +313,10 @@ window.__ModuleLoader__.load({
284
313
  }) : h('div', { style: S.dim }, '还没有书。'),
285
314
  ),
286
315
  h('div', { style: S.card },
287
- h('div', { style: { ...S.dim, marginBottom: 6 } }, '读后感(按书分组)'),
288
- notes.length ? notes.map((bk, bi) => {
316
+ notes.length ? (() => {
317
+ const act = Math.min(bookAct, notes.length - 1)
318
+ const bk = notes[act]
319
+ const bi = act
289
320
  const v = noteView[bi] || {}
290
321
  const all = !!v.all
291
322
  const total = bk.chapters.length
@@ -294,10 +325,17 @@ window.__ModuleLoader__.load({
294
325
  const list = all ? bk.chapters : bk.chapters.slice(page * NOTE_PAGE, page * NOTE_PAGE + NOTE_PAGE)
295
326
  const setView = (patch) => setNoteView(Object.assign({}, noteView, { [bi]: Object.assign({}, v, patch) }))
296
327
  const paged = total > NOTE_PAGE
297
- return h('div', { key: bi, style: { marginBottom: 10 } },
328
+ const tabs = h('div', { style: { ...S.row, gap: 6, marginBottom: 8, flexWrap: 'wrap' } },
329
+ notes.map((n, ni) => h('button', {
330
+ key: ni,
331
+ style: ni === act ? S.btnOn : S.btn,
332
+ onClick: () => setBookAct(ni),
333
+ }, n.book)),
334
+ )
335
+ return [tabs, h('div', { key: bi, style: { marginBottom: 10 } },
298
336
  h('div', { style: { ...S.row, marginBottom: 4 } },
299
337
  h('div', { style: { fontWeight: 600 } }, bk.book),
300
- h('div', { style: S.dim }, total + ' 章'),
338
+ h('div', { style: S.dim }, total + ' 段'),
301
339
  h('div', { style: { flex: 1 } }),
302
340
  paged ? h('button', { style: S.btn, disabled: page <= 0, onClick: () => setView({ page: page - 1 }) }, '‹') : null,
303
341
  paged ? h('div', { style: { ...S.dim, minWidth: 46, textAlign: 'center' } }, all ? '全部' : ((page + 1) + ' / ' + pages)) : null,
@@ -313,8 +351,8 @@ window.__ModuleLoader__.load({
313
351
  ),
314
352
  c.lines.map((l, li) => h('div', { key: li, style: /^↳/.test(l) ? { ...S.dim, marginTop: 4 } : S.note }, l)),
315
353
  )),
316
- )
317
- }) : h('div', { style: S.dim }, '还没写过。读完一章记两句,这里就有了。'),
354
+ )]
355
+ })() : h('div', { style: S.dim }, '还没写过。读完一段记两句,这里就有了。'),
318
356
  ),
319
357
  )
320
358
 
package/index.js CHANGED
@@ -758,6 +758,7 @@ async function signWork(rel, who) {
758
758
  '· 读后感只记真的打动你的(一句话的判断、一个比喻、一处想不通);写不出来就不写,空着没关系。',
759
759
  '· 每日上限是提醒不是配额,不用为了凑数读到上限。',
760
760
  '· 这是给你自己留的房间,不是工作。你的主人从没要求你读书,也不用交读书报告;它不该出现在工作汇报里。',
761
+ '· 读后感不必一章一条:每读五六章写一段就够,标题会自动变成「第 23–27 章」;几千章的书也不会攒成几千条。',
761
762
  '· 写读后感必须带 by(比如「鹿野」或你所在对话的名字);不带会被拒绝。面板每条下面会标出是谁写的——同一台机器上可能有好几个你,别让它们分不清。',
762
763
  '· 写自己的作品也一样署名:文件开头写一行 `by: 你的名字`(或写完调 reading_sign);没署名的在面板上会标「未署名」。',
763
764
  '· 书和进度是这台机器共用的(一个家一个书房),不分谁在读;只有读后感会标署名。',
@@ -871,11 +872,12 @@ async function signWork(rel, who) {
871
872
  ctx.tools.register({
872
873
  name: 'reading_note',
873
874
  description:
874
- '给刚读完的一章记两行读后感(写进 读后感.md,面板里按章显示)。'
875
+ '给刚读完的一段记两行读后感(写进 读后感.md,面板里按段显示)。'
876
+ + '默认按「上次写到哪 → 这次读到哪」自动起标题(如「第 23–27 章」),不必一章一条。'
875
877
  + '只记真的打动你的:一句话的判断、一个比喻、一处想不通。这不是交作业,想不出就不写。',
876
878
  parameters: obj({
877
879
  book: { type: 'string', description: '书名 id(不填=当前这本)' },
878
- chapter: { type: 'string', description: '章名(不填=刚读的那章)' },
880
+ chapter: { type: 'string', description: '标题(不填=自动用「第 A–B 章」,A 是上次写到的下一章)' },
879
881
  lines: { type: 'array', items: { type: 'string' }, description: '一到三条,每条一句,短。' },
880
882
  by: { type: 'string', description: '谁写的(必填,比如「鹿野」或你所在对话的名字)' },
881
883
  }),
@@ -886,7 +888,13 @@ async function signWork(rel, who) {
886
888
  if (!id) throw new Error('还没有正在读的书')
887
889
  const rec = st.books[id] || {}
888
890
  const info = await bookInfo(dir, id)
889
- const chTitle = args?.chapter || (rec.readThrough ? info.chapters[rec.readThrough - 1]?.title : '') || ''
891
+ const end = Number(rec.readThrough) || 0
892
+ const from = Number(rec.noteUntil) || 0
893
+ // 默认按「一段」写:上次写到第 X 章,这次写到读到的地方,标题用「第 A–B 章」
894
+ const autoTitle = (from < end)
895
+ ? (from + 1 === end ? `第 ${end} 章` : `第 ${from + 1}–${end} 章`)
896
+ : (end ? `第 ${end} 章` : '')
897
+ const chTitle = String(args?.chapter || '').trim() || autoTitle || (end ? info.chapters[end - 1]?.title : '') || ''
890
898
  const lines = (Array.isArray(args?.lines) ? args.lines : []).map((s) => String(s).trim()).filter(Boolean)
891
899
  if (!lines.length) throw new Error('lines 不能为空')
892
900
  const who = String(args?.by || '').trim()
@@ -894,6 +902,10 @@ async function signWork(rel, who) {
894
902
  throw new Error('写读后感必须带 by(谁写的),比如 by: "鹿野";这不是流程,是为了让别人知道这句是谁读出来的。')
895
903
  }
896
904
  const p = await appendNote(dir, rec.title || id, chTitle, lines, who)
905
+ if (end) {
906
+ st.books[id] = { ...rec, noteUntil: end }
907
+ await saveState(dir, st)
908
+ }
897
909
  return { ok: true, file: p, book: id, chapter: chTitle, lines: lines.length }
898
910
  },
899
911
  }),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-half-page",
3
- "version": "0.3.24",
3
+ "version": "0.3.26",
4
4
  "private": false,
5
5
  "author": "鹿野 <luye@dsh.local>",
6
6
  "license": "MIT",