@biunow/agent-biutalk 1.1.6 → 1.1.8

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 (112) hide show
  1. package/_shared/.tmp-panel-smoke/art/smoke.mp4 +0 -0
  2. package/_shared/.tmp-panel-smoke/art-pipe/out/biuart-01_cave-mwq5kfpm.mp4 +0 -0
  3. package/_shared/.tmp-panel-smoke/art-pipe/panel-delivery.json +7 -0
  4. package/_shared/.tmp-panel-smoke/reel-pipe/film/index.html +7 -0
  5. package/_shared/.tmp-panel-smoke/reel-pipe/film/main.js +68 -0
  6. package/_shared/.tmp-panel-smoke/reel-pipe/film/panel-beats.json +20 -0
  7. package/_shared/.tmp-panel-smoke/reel-pipe/style-demo/CREDITS +20 -0
  8. package/_shared/.tmp-panel-smoke/reel-pipe/style-demo/build.sh +17 -0
  9. package/_shared/.tmp-panel-smoke/reel-pipe/style-demo/ease.js +43 -0
  10. package/_shared/.tmp-panel-smoke/reel-pipe/style-demo/events.json +1 -0
  11. package/_shared/.tmp-panel-smoke/reel-pipe/style-demo/film.js +533 -0
  12. package/_shared/.tmp-panel-smoke/reel-pipe/style-demo/fonts/OFL-Archivo.txt +93 -0
  13. package/_shared/.tmp-panel-smoke/reel-pipe/style-demo/fonts/OFL-DMSerifDisplay.txt +93 -0
  14. package/_shared/.tmp-panel-smoke/reel-pipe/style-demo/fonts/OFL-UnifrakturMaguntia.txt +94 -0
  15. package/_shared/.tmp-panel-smoke/reel-pipe/style-demo/fonts/fonts.css +3 -0
  16. package/_shared/.tmp-panel-smoke/reel-pipe/style-demo/index.html +6 -0
  17. package/_shared/.tmp-panel-smoke/reel-pipe/style-demo/lines.json +10 -0
  18. package/_shared/.tmp-panel-smoke/reel-pipe/style-demo/main.js +15 -0
  19. package/_shared/.tmp-panel-smoke/reel-pipe/style-demo/mix.py +116 -0
  20. package/_shared/.tmp-panel-smoke/reel-pipe/style-demo/music/score.py +168 -0
  21. package/_shared/.tmp-panel-smoke/reel-pipe/style-demo/music/score_meta.json +59 -0
  22. package/_shared/.tmp-panel-smoke/reel-pipe/style-demo/score.json +235 -0
  23. package/_shared/.tmp-panel-smoke/reel-pipe/style-demo/stills/styleframe.jpg +0 -0
  24. package/_shared/.tmp-panel-smoke/reel-pipe/style-demo/tools/fonts.sh +17 -0
  25. package/_shared/.tmp-panel-smoke/reel-pipe/style-demo/tools/subs.py +11 -0
  26. package/_shared/.tmp-panel-smoke/reel-pipe/style-demo/tools/words.py +20 -0
  27. package/_shared/.tmp-panel-smoke/reel-pipe/style-demo/voices/dur.json +10 -0
  28. package/_shared/.tmp-panel-smoke/reel-pipe/style-demo/voices/words.json +213 -0
  29. package/_shared/.tmp-panel-smoke/reel-pipe/style-demo/voices/words_rel.json +56 -0
  30. package/_shared/.tmp-panel-smoke/reel-pipe/style-ref.json +5 -0
  31. package/_shared/.tmp-panel-smoke/reel-pipe2/film/index.html +7 -0
  32. package/_shared/.tmp-panel-smoke/reel-pipe2/film/main.js +68 -0
  33. package/_shared/.tmp-panel-smoke/reel-pipe2/film/panel-beats.json +20 -0
  34. package/_shared/.tmp-panel-smoke/reel-pipe2/out/video.mp4 +0 -0
  35. package/_shared/.tmp-panel-smoke/reel-pipe2/panel-delivery.json +8 -0
  36. package/_shared/.tmp-panel-smoke/reel-pipe2/style-demo/CREDITS +20 -0
  37. package/_shared/.tmp-panel-smoke/reel-pipe2/style-demo/build.sh +17 -0
  38. package/_shared/.tmp-panel-smoke/reel-pipe2/style-demo/ease.js +43 -0
  39. package/_shared/.tmp-panel-smoke/reel-pipe2/style-demo/events.json +1 -0
  40. package/_shared/.tmp-panel-smoke/reel-pipe2/style-demo/film.js +533 -0
  41. package/_shared/.tmp-panel-smoke/reel-pipe2/style-demo/fonts/OFL-Archivo.txt +93 -0
  42. package/_shared/.tmp-panel-smoke/reel-pipe2/style-demo/fonts/OFL-DMSerifDisplay.txt +93 -0
  43. package/_shared/.tmp-panel-smoke/reel-pipe2/style-demo/fonts/OFL-UnifrakturMaguntia.txt +94 -0
  44. package/_shared/.tmp-panel-smoke/reel-pipe2/style-demo/fonts/fonts.css +3 -0
  45. package/_shared/.tmp-panel-smoke/reel-pipe2/style-demo/index.html +6 -0
  46. package/_shared/.tmp-panel-smoke/reel-pipe2/style-demo/lines.json +10 -0
  47. package/_shared/.tmp-panel-smoke/reel-pipe2/style-demo/main.js +15 -0
  48. package/_shared/.tmp-panel-smoke/reel-pipe2/style-demo/mix.py +116 -0
  49. package/_shared/.tmp-panel-smoke/reel-pipe2/style-demo/music/score.py +168 -0
  50. package/_shared/.tmp-panel-smoke/reel-pipe2/style-demo/music/score_meta.json +59 -0
  51. package/_shared/.tmp-panel-smoke/reel-pipe2/style-demo/score.json +235 -0
  52. package/_shared/.tmp-panel-smoke/reel-pipe2/style-demo/stills/styleframe.jpg +0 -0
  53. package/_shared/.tmp-panel-smoke/reel-pipe2/style-demo/tools/fonts.sh +17 -0
  54. package/_shared/.tmp-panel-smoke/reel-pipe2/style-demo/tools/subs.py +11 -0
  55. package/_shared/.tmp-panel-smoke/reel-pipe2/style-demo/tools/words.py +20 -0
  56. package/_shared/.tmp-panel-smoke/reel-pipe2/style-demo/voices/dur.json +10 -0
  57. package/_shared/.tmp-panel-smoke/reel-pipe2/style-demo/voices/words.json +213 -0
  58. package/_shared/.tmp-panel-smoke/reel-pipe2/style-demo/voices/words_rel.json +56 -0
  59. package/_shared/.tmp-panel-smoke/reel-pipe2/style-ref.json +5 -0
  60. package/_shared/.tmp-panel-smoke/remo-pipe/brief.md +2 -0
  61. package/_shared/.tmp-panel-smoke/remo-pipe/remo-job.json +4 -0
  62. package/_shared/.tmp-panel-smoke/remo-pipe2/brief.md +1 -0
  63. package/_shared/.tmp-panel-smoke/remo-pipe2/out/final.mp4 +0 -0
  64. package/_shared/.tmp-panel-smoke/remo-pipe2/panel-delivery.json +8 -0
  65. package/_shared/.tmp-panel-smoke/remo-pipe2/remo-job.json +4 -0
  66. package/_shared/.tmp-panel-smoke/vibe-pipe-ok/ep00-demo.mp4 +0 -0
  67. package/_shared/.tmp-panel-smoke/vibe-pipe-ok/out/ep00-demo.mp4 +0 -0
  68. package/_shared/.tmp-panel-smoke/vibe-pipe-ok/panel-delivery.json +7 -0
  69. package/_shared/.tmp-panel-smoke/vibe-pipe-ok/publish.json +10 -0
  70. package/_shared/.tmp-panel-smoke/vibe-still.jpg +0 -0
  71. package/_shared/.tmp-panel-smoke/vibe-still.log +6 -0
  72. package/_shared/.tmp-panel-smoke/vibe-still.log.err +0 -0
  73. package/_shared/.tmp-panel-smoke/vibe-still2.log +26 -0
  74. package/_shared/.tmp-panel-smoke/vibe-still2.log.err +0 -0
  75. package/_shared/lib/film-locate.mjs +203 -0
  76. package/_shared/lib/voice-catalog.mjs +127 -0
  77. package/_shared/scripts/apply-upload-limits.mjs +62 -0
  78. package/_shared/scripts/patch-film-hosts.mjs +233 -0
  79. package/_shared/scripts/patch-voice-options.mjs +41 -0
  80. package/_shared/scripts/sync-local-agents.mjs +67 -0
  81. package/_shared/scripts/wire-film-bridge.mjs +24 -0
  82. package/_shared/scripts/wire-voice-plus.mjs +68 -0
  83. package/_shared/studio/assert-panel-permit.mjs +67 -0
  84. package/_shared/studio/composer.css +559 -0
  85. package/_shared/studio/composer.js +728 -0
  86. package/_shared/studio/optimize-prompt.mjs +84 -0
  87. package/_shared/studio/optimize-prompt.test.mjs +47 -0
  88. package/_shared/studio/recipes.mjs +87 -0
  89. package/_shared/studio/select-menu.js +450 -0
  90. package/_shared/studio/to-english-prompt.mjs +165 -0
  91. package/_shared/studio/to-english-prompt.test.mjs +72 -0
  92. package/bin/server.js +52 -36
  93. package/data/looks.json +8 -0
  94. package/lib/film-bridge.mjs +1 -0
  95. package/lib/host.mjs +149 -264
  96. package/lib/voice-bridge.mjs +2 -0
  97. package/manifest.json +4 -3
  98. package/package.json +1 -1
  99. package/skills/biutalk/EXEC.md +69 -0
  100. package/skills/biutalk/SKILL.md +12 -10
  101. package/skills/biutalk/scripts/doctor.mjs +124 -0
  102. package/skills/biutalk/scripts/qc.mjs +129 -0
  103. package/skills/biutalk/scripts/scaffold.mjs +205 -0
  104. package/studio/app.js +17 -1
  105. package/studio/composer.css +115 -1
  106. package/studio/composer.js +389 -10
  107. package/studio/index.html +5 -1
  108. package/studio/optimize-prompt.mjs +84 -0
  109. package/studio/optimize-prompt.test.mjs +47 -0
  110. package/studio/recipes.mjs +1 -0
  111. package/studio/to-english-prompt.mjs +165 -0
  112. package/studio/to-english-prompt.test.mjs +72 -0
@@ -0,0 +1,84 @@
1
+ /** 把用户原话收成当前模板能直接出片的一句。会员接口才调用。 */
2
+
3
+ function same(a, b) {
4
+ return String(a || '').replace(/\s+/g, '') === String(b || '').replace(/\s+/g, '');
5
+ }
6
+
7
+ function quotes(text) {
8
+ return [...String(text || '').matchAll(/「([^」]+)」/g)]
9
+ .map((match) => match[1].trim())
10
+ .filter(Boolean);
11
+ }
12
+
13
+ function clip(text, max = 80) {
14
+ const chars = [...String(text || '').replace(/\s+/g, ' ').trim()];
15
+ if (chars.length <= max) return chars.join('');
16
+ return chars.slice(0, max).join('').replace(/[,,、::;;\s]+$/u, '');
17
+ }
18
+
19
+ function topicFromFreeText(text) {
20
+ let value = String(text || '').replace(/\s+/g, ' ').trim();
21
+ value = value.replace(/^用[\u4e00-\u9fffA-Za-z0-9]{1,16}讲/, '');
22
+ value = value.replace(/^[「『"']+|[」』"']+$/g, '').trim();
23
+ const sentence = (value.split(/[。!?!?]/)[0] || value).trim();
24
+ return sentence.replace(/^[「『"']+|[」』"']+$/g, '').trim();
25
+ }
26
+
27
+ function fail(message) {
28
+ const error = new Error(message);
29
+ error.code = 'PROMPT';
30
+ throw error;
31
+ }
32
+
33
+ export function optimizePrompt(input) {
34
+ const text = String(input?.text || '').trim();
35
+ const skeleton = String(input?.templatePrompt || '').trim();
36
+ const narration = String(input?.narration || '');
37
+ const attachment = String(input?.attachment || '');
38
+ if (!text) fail('先写下要讲的事');
39
+ if (!(skeleton && /「[^」]*」/.test(skeleton))) return { prompt: cap(text), kept: true };
40
+
41
+ const samples = quotes(skeleton);
42
+ let topic = quotes(text).find((quote) => !samples.some((sample) => same(sample, quote))) || '';
43
+ if (!topic) {
44
+ const free = topicFromFreeText(text);
45
+ const stillSample = !free || samples.some((sample) => same(sample, free)) || (skeleton && same(text, skeleton));
46
+ if (stillSample) fail(skeleton ? '先把引号里换成你要讲的事' : '先写下要讲的事');
47
+ topic = free;
48
+ }
49
+ topic = clip(topic);
50
+ if (!topic) fail('先写下要讲的事');
51
+
52
+ let prompt = skeleton.replace(/「[^」]*」/, `「${topic}」`);
53
+ const detail = extraDetail(text, skeleton, topic);
54
+ if (detail) prompt = `${prompt.replace(/。?$/, '。')}${detail}。`;
55
+ if (narration === 'off' && !/没有口播|不要口播|没有旁白|不要旁白/.test(prompt)) prompt += '没有口播。';
56
+ if (narration === 'required' && !/口播|旁白/.test(prompt)) prompt += '口播用你上传的录音。';
57
+ if (attachment === 'required' && !/附件|上传/.test(prompt)) prompt += '必须的附件跟这一条一起交。';
58
+ return { prompt: cap(prompt.replace(/[ \t]+/g, ' ').trim()) };
59
+ }
60
+
61
+ function extraDetail(text, skeleton, topic) {
62
+ const known = skeleton.replace(/\s+/g, '');
63
+ const sentences = String(text || '')
64
+ .replace(/\s+/g, ' ')
65
+ .split(/[。!?!?]/)
66
+ .map((sentence) => sentence.trim())
67
+ .filter(Boolean);
68
+ for (const sentence of sentences) {
69
+ const bare = sentence
70
+ .replace(/^用[\u4e00-\u9fffA-Za-z0-9]{1,16}讲/, '')
71
+ .replace(/[「」『』]/g, '')
72
+ .trim();
73
+ if (!bare || same(bare, topic)) continue;
74
+ if (known.includes(bare.replace(/\s+/g, ''))) continue;
75
+ if (/另外|再讲|同时再|以及/.test(bare)) continue;
76
+ return clip(bare, 48);
77
+ }
78
+ return '';
79
+ }
80
+
81
+ function cap(prompt) {
82
+ if ([...prompt].length <= 4000) return prompt;
83
+ return [...prompt].slice(0, 4000).join('');
84
+ }
@@ -0,0 +1,47 @@
1
+ import assert from 'node:assert/strict';
2
+ import test from 'node:test';
3
+ import { optimizePrompt } from './optimize-prompt.mjs';
4
+
5
+ const skeleton = '用灯火史话讲「古代驿站为什么能把一封信送到这么远」。火光里先给结论,再给一个能记住的细节,没有口播。';
6
+
7
+ test('空内容要求先写要讲的事', () => {
8
+ assert.throws(() => optimizePrompt({ text: ' ', templatePrompt: skeleton }), /先写下要讲的事/);
9
+ });
10
+
11
+ test('没改模板例句时要求换成自己的事', () => {
12
+ assert.throws(() => optimizePrompt({ text: skeleton, templatePrompt: skeleton }), /先把引号里换成你要讲的事/);
13
+ });
14
+
15
+ test('自由输入收进当前模板,并保留模板后半句', () => {
16
+ const got = optimizePrompt({ text: '古代人都想当皇帝', templatePrompt: skeleton });
17
+ assert.equal(
18
+ got.prompt,
19
+ '用灯火史话讲「古代人都想当皇帝」。火光里先给结论,再给一个能记住的细节,没有口播。',
20
+ );
21
+ });
22
+
23
+ test('改过引号后仍锁回模板句式', () => {
24
+ const got = optimizePrompt({
25
+ text: '用灯火史话讲「古代人都想当皇帝」。另外再讲科举和驿站。',
26
+ templatePrompt: skeleton,
27
+ });
28
+ assert.equal(got.prompt.includes('古代人都想当皇帝'), true);
29
+ assert.equal(got.prompt.includes('没有口播'), true);
30
+ assert.equal(got.prompt.includes('科举'), false);
31
+ });
32
+
33
+ test('没选模板时保留原文,不压成套话', () => {
34
+ const text = '飞机上为什么要开飞行模式。先讲信号会干扰导航,再说规定,最后说实际影响。';
35
+ const got = optimizePrompt({ text, narration: 'required' });
36
+ assert.equal(got.prompt, text);
37
+ assert.equal(got.kept, true);
38
+ });
39
+
40
+ test('模板句后面留下用户写的那一个细节', () => {
41
+ const got = optimizePrompt({
42
+ text: '用灯火史话讲「古代人都想当皇帝」。嘴里说一句想反,唐律也要流二千里。',
43
+ templatePrompt: skeleton,
44
+ });
45
+ assert.equal(got.prompt.includes('没有口播'), true);
46
+ assert.equal(got.prompt.includes('嘴里说一句想反,唐律也要流二千里'), true);
47
+ });
@@ -0,0 +1,87 @@
1
+ import fs from 'node:fs';
2
+
3
+ /** 从《风格提示词.md》读出每条模板的起步提示词。标题用 ## 或 ###。 */
4
+ export function loadRecipes(file) {
5
+ if (!file || !fs.existsSync(file)) return [];
6
+ const lines = fs.readFileSync(file, 'utf8').split(/\r?\n/);
7
+ const recipes = [];
8
+ let section = '';
9
+ let title = '';
10
+ let blurb = [];
11
+ let buf = [];
12
+ let inCode = false;
13
+
14
+ const flush = () => {
15
+ const prompt = buf.join('\n').trim();
16
+ if (title && prompt) {
17
+ recipes.push({
18
+ section,
19
+ title: title.trim(),
20
+ blurb: blurb.join(' ').replace(/\s+/g, ' ').trim(),
21
+ prompt,
22
+ });
23
+ }
24
+ buf = [];
25
+ blurb = [];
26
+ };
27
+
28
+ for (const line of lines) {
29
+ if (line.startsWith('```')) {
30
+ if (inCode) {
31
+ inCode = false;
32
+ flush();
33
+ title = '';
34
+ } else {
35
+ inCode = true;
36
+ }
37
+ continue;
38
+ }
39
+ if (inCode) {
40
+ buf.push(line);
41
+ continue;
42
+ }
43
+ if (line.startsWith('### ')) {
44
+ title = line.slice(4).trim();
45
+ blurb = [];
46
+ continue;
47
+ }
48
+ if (line.startsWith('## ')) {
49
+ section = line.slice(3).trim();
50
+ title = section;
51
+ blurb = [];
52
+ continue;
53
+ }
54
+ if (line.startsWith('# ')) continue;
55
+ if (title && line.trim()) blurb.push(line.trim());
56
+ }
57
+ return recipes;
58
+ }
59
+
60
+ function norm(value) {
61
+ return String(value || '').trim().toLowerCase().replace(/\s+/g, '');
62
+ }
63
+
64
+ export function findRecipe(recipes, keys) {
65
+ const wants = [...new Set((keys || []).map(norm).filter(Boolean))];
66
+ for (const want of wants) {
67
+ const hit = (recipes || []).find((recipe) => {
68
+ const title = norm(recipe.title);
69
+ const head = norm(String(recipe.title).split('·')[0]);
70
+ return title === want || head === want;
71
+ });
72
+ if (hit) return hit;
73
+ }
74
+ return null;
75
+ }
76
+
77
+ export function recipeView(recipe, extras = {}) {
78
+ const prompt = String(recipe?.prompt || extras.prompt || '').trim();
79
+ return {
80
+ prompt,
81
+ blurb: recipe?.blurb || '',
82
+ narration: extras.narration || 'optional',
83
+ attachment: extras.attachment || 'optional',
84
+ audio: extras.audio || '',
85
+ hint: extras.hint || recipe?.blurb || '',
86
+ };
87
+ }
@@ -0,0 +1,450 @@
1
+ /**
2
+ * Vanilla SelectMenu — Biunow CreateDesk SelectMenu 移植(缩略图网格 + 悬浮预览)
3
+ * usage: const sm = SelectMenu.mount(el, { value, options, size, onChange })
4
+ */
5
+ (function (global) {
6
+ const PREVIEW_W = 340;
7
+ const PREVIEW_H = 320;
8
+
9
+ function esc(s) {
10
+ return String(s || '')
11
+ .replace(/&/g, '&amp;')
12
+ .replace(/</g, '&lt;')
13
+ .replace(/>/g, '&gt;')
14
+ .replace(/"/g, '&quot;');
15
+ }
16
+
17
+ function SelectMenu(opts) {
18
+ this.opts = opts || {};
19
+ this.value = this.opts.value || '';
20
+ this.options = this.opts.options || [];
21
+ this.size = this.opts.size || 'sm';
22
+ this.placeholder = this.opts.placeholder || '请选择';
23
+ this.open = false;
24
+ this.activeIndex = 0;
25
+ this.dropUp = false;
26
+ this.pos = { left: 0, top: 0, bottom: 0, width: 0 };
27
+ this.previewIndex = -1;
28
+ this.hoverTop = 0;
29
+ this.cat = '';
30
+ this.entryLabel = '';
31
+ this.entrySeconds = null;
32
+ this.entryOpen = false;
33
+ this.entryDraft = '';
34
+ this.entryError = false;
35
+ this.valueBeforeEntry = '';
36
+ this._previewTimer = null;
37
+ this.root = null;
38
+ this.trigger = null;
39
+ this.panel = null;
40
+ this.previewEl = null;
41
+ }
42
+
43
+ SelectMenu.mount = function (host, opts) {
44
+ const sm = new SelectMenu(opts);
45
+ sm.mount(host);
46
+ return sm;
47
+ };
48
+
49
+ SelectMenu.prototype.gridMode = function () {
50
+ return this.options.some((o) => !!o.thumb);
51
+ };
52
+
53
+ SelectMenu.prototype.hasGroups = function () {
54
+ return this.options.some((o) => !!o.group);
55
+ };
56
+
57
+ SelectMenu.prototype.groups = function () {
58
+ const seen = [];
59
+ this.options.forEach((o) => {
60
+ if (o.group && !seen.includes(o.group)) seen.push(o.group);
61
+ });
62
+ return seen;
63
+ };
64
+
65
+ SelectMenu.prototype.visibleOptions = function () {
66
+ if (!this.hasGroups() || !this.cat) return this.options;
67
+ return this.options.filter((o) => o.group === this.cat);
68
+ };
69
+
70
+ SelectMenu.prototype.selected = function () {
71
+ return this.options.find((o) => o.value === this.value) || null;
72
+ };
73
+
74
+ SelectMenu.prototype.mount = function (host) {
75
+ this.root = typeof host === 'string' ? document.querySelector(host) : host;
76
+ if (!this.root) throw new Error('SelectMenu host missing');
77
+ this.root.classList.add('sm');
78
+ this.root.innerHTML = '';
79
+ this.trigger = document.createElement('button');
80
+ this.trigger.type = 'button';
81
+ this.trigger.className = `sm-trigger sm-${this.size}`;
82
+ this.trigger.setAttribute('aria-haspopup', 'listbox');
83
+ this.trigger.addEventListener('click', (e) => {
84
+ e.stopPropagation();
85
+ this.toggle();
86
+ });
87
+ this.root.appendChild(this.trigger);
88
+ this.renderTrigger();
89
+ this._onDoc = (e) => {
90
+ if (!this.open) return;
91
+ const t = e.target;
92
+ if (this.root.contains(t) || this.panel?.contains(t)) return;
93
+ this.close();
94
+ };
95
+ this._onRepo = () => {
96
+ if (this.open) this.measure();
97
+ };
98
+ };
99
+
100
+ SelectMenu.prototype.setOptions = function (options, value) {
101
+ this.options = options || [];
102
+ if (value !== undefined) this.value = value;
103
+ if (!this.options.some((o) => o.value === this.value) && this.options[0]) {
104
+ this.value = this.options[0].value;
105
+ }
106
+ this.renderTrigger();
107
+ if (this.open) this.renderPanel();
108
+ };
109
+
110
+ SelectMenu.prototype.setValue = function (value, silent, extra) {
111
+ this.value = value;
112
+ this.renderTrigger();
113
+ if (!silent && typeof this.opts.onChange === 'function') this.opts.onChange(this.value, extra);
114
+ };
115
+
116
+ SelectMenu.prototype.renderTrigger = function () {
117
+ const sel = this.selected();
118
+ const thumb = sel?.thumb
119
+ ? `<span class="sm-thumb sm-thumb-trigger"><img src="${esc(sel.thumb)}" alt=""/></span>`
120
+ : '';
121
+ const customLabel = sel?.entry && this.entryLabel;
122
+ const hint = sel?.hint && !this.gridMode() && !customLabel
123
+ ? `<span class="sm-hint">${esc(sel.hint)}</span>`
124
+ : '';
125
+ const label = sel ? (customLabel || sel.label) : this.placeholder;
126
+ this.trigger.classList.toggle('on', this.open);
127
+ this.trigger.innerHTML = `${thumb}<span class="sm-value${sel ? '' : ' placeholder'}">${esc(
128
+ label,
129
+ )}</span>${hint}<svg class="sm-chevron${this.open ? ' on' : ''}" viewBox="0 0 16 16" fill="none"><path d="M4 6l4 4 4-4" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"/></svg>`;
130
+ };
131
+
132
+ SelectMenu.prototype.measure = function () {
133
+ const r = this.trigger.getBoundingClientRect();
134
+ const vw = window.innerWidth;
135
+ const vh = window.innerHeight;
136
+ const grid = this.gridMode();
137
+ const visible = this.visibleOptions();
138
+ const minWidth = grid ? 404 : 210;
139
+ const width = Math.min(Math.max(r.width, minWidth), vw - 24);
140
+ const left = Math.min(Math.max(12, r.left), Math.max(12, vw - width - 12));
141
+ const gap = 6;
142
+ const margin = 12;
143
+ const below = Math.max(0, vh - r.bottom - gap - margin);
144
+ const above = Math.max(0, r.top - gap - margin);
145
+ let chrome = 0;
146
+ if (this.panel) {
147
+ this.panel.style.left = `${left}px`;
148
+ this.panel.style.width = `${width}px`;
149
+ const cats = this.panel.querySelector('.sm-cats');
150
+ const entry = this.panel.querySelector('.sm-entry');
151
+ chrome = (cats ? cats.offsetHeight : 0) + (entry ? entry.offsetHeight : 0);
152
+ }
153
+ const scrollCap = grid ? 430 : 292;
154
+ const naturalScroll = grid
155
+ ? Math.min(scrollCap, Math.ceil(Math.max(visible.length, 1) / 3) * 96 + 14)
156
+ : Math.min(scrollCap, visible.length * 38 + 12);
157
+ const want = Math.min(chrome + naturalScroll, 240);
158
+ this.dropUp = below < want && above > below;
159
+ const available = Math.max(0, this.dropUp ? above : below);
160
+ const room = Math.min(available, chrome + naturalScroll);
161
+ const scrollMax = Math.max(0, Math.min(scrollCap, room - chrome));
162
+ this.pos = { left, width, top: r.bottom + gap, bottom: vh - r.top + gap };
163
+ if (this.panel) {
164
+ this.panel.style.maxHeight = `${room}px`;
165
+ this.panel.style.overflow = scrollMax > 0 ? 'hidden' : 'auto';
166
+ const scroll = this.panel.querySelector('.sm-scroll');
167
+ if (scroll) scroll.style.maxHeight = `${scrollMax}px`;
168
+ if (this.dropUp) {
169
+ this.panel.style.bottom = `${this.pos.bottom}px`;
170
+ this.panel.style.top = 'auto';
171
+ } else {
172
+ this.panel.style.top = `${this.pos.top}px`;
173
+ this.panel.style.bottom = 'auto';
174
+ }
175
+ }
176
+ };
177
+
178
+ SelectMenu.prototype.toggle = function () {
179
+ if (this.open) this.close();
180
+ else {
181
+ const visible = this.visibleOptions();
182
+ const idx = visible.findIndex((o) => o.value === this.value);
183
+ this.openAt(idx >= 0 ? idx : 0);
184
+ }
185
+ };
186
+
187
+ SelectMenu.prototype.openAt = function (index) {
188
+ this.activeIndex = index;
189
+ this.open = true;
190
+ this.renderTrigger();
191
+ this.renderPanel();
192
+ this.measure();
193
+ document.addEventListener('pointerdown', this._onDoc, true);
194
+ window.addEventListener('resize', this._onRepo);
195
+ window.addEventListener('scroll', this._onRepo, true);
196
+ };
197
+
198
+ SelectMenu.prototype.close = function () {
199
+ if (this.entryOpen) {
200
+ this.value = this.valueBeforeEntry;
201
+ this.entryOpen = false;
202
+ this.entryError = false;
203
+ }
204
+ this.open = false;
205
+ this.hidePreview();
206
+ this.renderTrigger();
207
+ if (this.panel) {
208
+ this.panel.remove();
209
+ this.panel = null;
210
+ }
211
+ if (this.previewEl) {
212
+ this.previewEl.remove();
213
+ this.previewEl = null;
214
+ }
215
+ document.removeEventListener('pointerdown', this._onDoc, true);
216
+ window.removeEventListener('resize', this._onRepo);
217
+ window.removeEventListener('scroll', this._onRepo, true);
218
+ };
219
+
220
+ SelectMenu.prototype.hidePreview = function () {
221
+ if (this._previewTimer) clearTimeout(this._previewTimer);
222
+ this._previewTimer = null;
223
+ this.previewIndex = -1;
224
+ if (this.previewEl) {
225
+ this.previewEl.remove();
226
+ this.previewEl = null;
227
+ }
228
+ };
229
+
230
+ SelectMenu.prototype.schedulePreview = function (index, top) {
231
+ this.hoverTop = top;
232
+ if (this._previewTimer) clearTimeout(this._previewTimer);
233
+ this._previewTimer = setTimeout(() => {
234
+ this.previewIndex = index;
235
+ this.renderPreview();
236
+ }, 250);
237
+ };
238
+
239
+ SelectMenu.prototype.renderPreview = function () {
240
+ if (!this.open || !this.gridMode() || this.previewIndex < 0) {
241
+ if (this.previewEl) {
242
+ this.previewEl.remove();
243
+ this.previewEl = null;
244
+ }
245
+ return;
246
+ }
247
+ const opt = this.visibleOptions()[this.previewIndex];
248
+ if (!opt) return;
249
+ if (!this.previewEl) {
250
+ this.previewEl = document.createElement('div');
251
+ this.previewEl.className = 'sm-preview';
252
+ document.body.appendChild(this.previewEl);
253
+ }
254
+ const vw = window.innerWidth;
255
+ const vh = window.innerHeight;
256
+ let left = this.pos.left - PREVIEW_W - 10;
257
+ if (left < 12) left = this.pos.left + this.pos.width + 10;
258
+ const top = Math.min(Math.max(12, this.hoverTop - 46), Math.max(12, vh - PREVIEW_H - 12));
259
+ this.previewEl.style.left = `${left}px`;
260
+ this.previewEl.style.top = `${top}px`;
261
+ this.previewEl.style.width = `${PREVIEW_W}px`;
262
+ const img = opt.thumb
263
+ ? `<img src="${esc(opt.thumb)}" alt=""/>`
264
+ : `<span class="sm-thumb-ph">${esc((opt.label || '?').slice(0, 1))}</span>`;
265
+ this.previewEl.innerHTML = `<div class="sm-preview-img">${img}</div><div class="sm-preview-body"><div class="sm-preview-title"><strong>${esc(
266
+ opt.label,
267
+ )}</strong>${opt.hint ? `<span class="sm-preview-hint">${esc(opt.hint)}</span>` : ''}</div>${
268
+ opt.desc ? `<p class="sm-preview-desc">${esc(opt.desc)}</p>` : ''
269
+ }</div>`;
270
+ };
271
+
272
+ SelectMenu.prototype.renderPanel = function () {
273
+ if (!this.panel) {
274
+ this.panel = document.createElement('div');
275
+ this.panel.className = 'sm-panel';
276
+ this.panel.setAttribute('role', 'listbox');
277
+ this.panel.tabIndex = -1;
278
+ document.body.appendChild(this.panel);
279
+ }
280
+ const grid = this.gridMode();
281
+ const grouped = this.hasGroups();
282
+ const visible = this.visibleOptions();
283
+ this.panel.classList.toggle('has-cats', grouped);
284
+ const scroll = document.createElement('div');
285
+ scroll.className = 'sm-scroll' + (grid ? ' grid' : '');
286
+ if (grid && grouped) {
287
+ const room = Math.max(168, window.innerHeight - 24 - 92);
288
+ scroll.style.maxHeight = `${Math.min(430, room)}px`;
289
+ }
290
+ visible.forEach((opt, i) => {
291
+ const btn = document.createElement('button');
292
+ btn.type = 'button';
293
+ btn.className =
294
+ 'sm-option' +
295
+ (i === this.activeIndex ? ' on' : '') +
296
+ (opt.value === this.value ? ' picked' : '') +
297
+ (grid ? ' tile' : '');
298
+ btn.setAttribute('role', 'option');
299
+ let html = '';
300
+ if (grid) {
301
+ html += opt.thumb
302
+ ? `<span class="sm-thumb"><img src="${esc(opt.thumb)}" alt="" loading="lazy"/></span>`
303
+ : `<span class="sm-thumb"><span class="sm-thumb-ph">${esc((opt.label || '?').slice(0, 1))}</span></span>`;
304
+ }
305
+ html += `<span class="sm-opt-label">${esc(opt.label)}</span>`;
306
+ if (opt.hint && !grid) html += `<span class="sm-opt-hint">${esc(opt.hint)}</span>`;
307
+ if (opt.value === this.value) html += `<span class="sm-check">✓</span>`;
308
+ btn.innerHTML = html;
309
+ btn.addEventListener('mouseenter', (e) => {
310
+ this.activeIndex = i;
311
+ scroll.querySelectorAll('.sm-option').forEach((el, idx) => {
312
+ el.classList.toggle('on', idx === i);
313
+ });
314
+ if (grid) this.schedulePreview(i, e.currentTarget.getBoundingClientRect().top);
315
+ });
316
+ btn.addEventListener('click', () => this.pick(opt.value));
317
+ scroll.appendChild(btn);
318
+ });
319
+ scroll.addEventListener('mouseleave', () => this.hidePreview());
320
+ this.panel.innerHTML = '';
321
+ if (grouped) this.panel.appendChild(this.renderCats());
322
+ this.panel.appendChild(scroll);
323
+ if (this.entryOpen) {
324
+ this.panel.appendChild(this.renderEntry());
325
+ const input = this.panel.querySelector('.sm-entry input');
326
+ setTimeout(() => input && input.focus(), 0);
327
+ }
328
+ };
329
+
330
+ SelectMenu.prototype.renderCats = function () {
331
+ const bar = document.createElement('div');
332
+ bar.className = 'sm-cats';
333
+ const items = [{ id: '', label: '全部' }].concat(
334
+ this.groups().map((name) => ({ id: name, label: name })),
335
+ );
336
+ items.forEach((item) => {
337
+ const btn = document.createElement('button');
338
+ btn.type = 'button';
339
+ btn.className = 'sm-cat' + (this.cat === item.id ? ' on' : '');
340
+ btn.textContent = item.label;
341
+ btn.setAttribute('aria-pressed', this.cat === item.id ? 'true' : 'false');
342
+ btn.addEventListener('click', (e) => {
343
+ e.preventDefault();
344
+ e.stopPropagation();
345
+ if (this.cat === item.id) return;
346
+ this.cat = item.id;
347
+ this.hidePreview();
348
+ const visible = this.visibleOptions();
349
+ const picked = visible.findIndex((o) => o.value === this.value);
350
+ this.activeIndex = picked >= 0 ? picked : 0;
351
+ this.renderPanel();
352
+ this.measure();
353
+ });
354
+ bar.appendChild(btn);
355
+ });
356
+ return bar;
357
+ };
358
+
359
+ SelectMenu.prototype.renderEntry = function () {
360
+ const box = document.createElement('div');
361
+ box.className = 'sm-entry';
362
+ const input = document.createElement('input');
363
+ input.setAttribute('inputmode', 'numeric');
364
+ input.setAttribute('maxlength', '3');
365
+ input.setAttribute('aria-label', '自定义秒数');
366
+ input.placeholder = '15–180';
367
+ input.value = this.entryDraft;
368
+ if (this.entryError) input.classList.add('bad');
369
+ const unit = document.createElement('span');
370
+ unit.className = 'unit';
371
+ unit.textContent = '秒';
372
+ const ok = document.createElement('button');
373
+ ok.type = 'button';
374
+ ok.textContent = '确定';
375
+ input.addEventListener('input', () => {
376
+ const next = input.value.replace(/[^\d]/g, '').slice(0, 3);
377
+ if (input.value !== next) input.value = next;
378
+ this.entryDraft = next;
379
+ this.entryError = false;
380
+ input.classList.remove('bad');
381
+ const err = box.querySelector('.sm-entry-err');
382
+ if (err) err.remove();
383
+ });
384
+ input.addEventListener('keydown', (e) => {
385
+ if (e.key === 'Enter') {
386
+ e.preventDefault();
387
+ this.commitEntry();
388
+ } else if (e.key === 'Escape') {
389
+ e.preventDefault();
390
+ this.close();
391
+ }
392
+ });
393
+ ok.addEventListener('click', (e) => {
394
+ e.preventDefault();
395
+ e.stopPropagation();
396
+ this.commitEntry();
397
+ });
398
+ box.appendChild(input);
399
+ box.appendChild(unit);
400
+ box.appendChild(ok);
401
+ if (this.entryError) {
402
+ const err = document.createElement('p');
403
+ err.className = 'sm-entry-err';
404
+ err.textContent = '请输入秒数';
405
+ box.appendChild(err);
406
+ }
407
+ return box;
408
+ };
409
+
410
+ SelectMenu.prototype.commitEntry = function () {
411
+ const input = this.panel?.querySelector('.sm-entry input');
412
+ if (input) this.entryDraft = input.value;
413
+ const parse = this.opts.parseEntry;
414
+ const n = typeof parse === 'function' ? parse(this.entryDraft) : null;
415
+ if (n == null) {
416
+ this.entryError = true;
417
+ this.renderPanel();
418
+ this.measure();
419
+ return;
420
+ }
421
+ this.entrySeconds = n;
422
+ this.entryLabel = this.opts.formatEntry ? this.opts.formatEntry(n) : String(n);
423
+ this.entryOpen = false;
424
+ this.entryError = false;
425
+ this.setValue(this.value, false, n);
426
+ this.close();
427
+ };
428
+
429
+ SelectMenu.prototype.pick = function (value) {
430
+ const opt = this.options.find((o) => o.value === value);
431
+ if (opt && opt.entry) {
432
+ if (!this.entryOpen) this.valueBeforeEntry = this.value;
433
+ this.entryOpen = true;
434
+ this.entryError = false;
435
+ this.entryDraft = this.entrySeconds != null ? String(this.entrySeconds) : '';
436
+ this.value = value;
437
+ this.renderTrigger();
438
+ this.renderPanel();
439
+ this.measure();
440
+ return;
441
+ }
442
+ this.entryOpen = false;
443
+ this.entryLabel = '';
444
+ this.entrySeconds = null;
445
+ this.setValue(value);
446
+ this.close();
447
+ };
448
+
449
+ global.SelectMenu = SelectMenu;
450
+ })(window);