cortico-world-fun 0.3.0 → 0.3.1

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cortico-world-fun",
3
- "version": "0.3.0",
3
+ "version": "0.3.1",
4
4
  "type": "module",
5
5
  "description": "娱乐时间 —— 给 Coopanion 桌宠二十处真能去逛的地方:游乐场、海洋馆、庙会、漫展、电影院、动物园、公园、电玩城、KTV、博物馆、猫咖、爬山、咖啡店、奶茶店、餐厅、图书馆、夜市、澡堂、旧货市场、江边。进门、逛摊位、买纪念品、拍张照、吃喝点什么,收摊记一笔。能和「鱼的每一天」「生图工作室」联动,但都不依赖。",
6
6
  "license": "MIT",
@@ -39,13 +39,16 @@
39
39
  "PUBLISHING.md"
40
40
  ],
41
41
  "scripts": {
42
+ "build:webui": "esbuild webui/webui.js --bundle --format=esm --platform=browser --target=es2022 --charset=utf8 --outfile=dist/webui.js",
43
+ "pretest": "npm run build:webui",
44
+ "prepack": "npm run build:webui",
42
45
  "test": "node test/story-packs.test.mjs && node test/story-world.test.mjs && node test/journal-world.test.mjs && node test/branches.test.mjs && node test/journeys.test.mjs && node test/exploration.test.mjs && node test/venues.test.mjs && node test/state.test.mjs && node test/events.test.mjs && node test/memories.test.mjs && node test/phases.test.mjs && node test/keepsakes.test.mjs && node test/user-venues.test.mjs && node test/aquarium.test.mjs && node test/wishes.test.mjs && node test/choices.test.mjs && node test/world.test.mjs && node test/panels.test.mjs",
43
- "verify": "node tools/fix-ps1-bom.mjs install.ps1 && node tools/check-prompt.mjs && node tools/check-isolation.mjs && node tools/smoke.mjs && node tools/make-describe.mjs --check",
46
+ "verify": "node tools/check-webui.mjs && node tools/fix-ps1-bom.mjs install.ps1 && node tools/check-prompt.mjs && node tools/check-isolation.mjs && node tools/smoke.mjs && node tools/make-describe.mjs --check",
44
47
  "describe": "node tools/make-describe.mjs",
45
48
  "check:publish": "node tools/publish-check.mjs --online",
46
49
  "check:isolation": "node tools/check-isolation.mjs",
47
50
  "verify:published": "node tools/verify-published.mjs",
48
- "prepublishOnly": "node test/story-packs.test.mjs && node test/story-world.test.mjs && node test/journal-world.test.mjs && node test/branches.test.mjs && node test/journeys.test.mjs && node test/exploration.test.mjs && node test/venues.test.mjs && node test/state.test.mjs && node test/events.test.mjs && node test/memories.test.mjs && node test/phases.test.mjs && node test/keepsakes.test.mjs && node test/user-venues.test.mjs && node test/aquarium.test.mjs && node test/wishes.test.mjs && node test/choices.test.mjs && node test/world.test.mjs && node test/panels.test.mjs && node tools/check-prompt.mjs && node tools/check-isolation.mjs && node tools/smoke.mjs && node tools/make-describe.mjs --check && node tools/publish-check.mjs --online"
51
+ "prepublishOnly": "npm test && npm run verify && node tools/publish-check.mjs --online"
49
52
  },
50
53
  "cortico": {
51
54
  "kind": "world",
@@ -67,5 +70,8 @@
67
70
  "description": "Twenty places she can actually go: an amusement park, an aquarium, a temple fair, a fan convention, a cinema, a zoo, a park, an arcade, a karaoke box, a museum, a cat cafe, a hike, a coffee shop, a bubble-tea stand, a restaurant, a library, a night market, a bathhouse, a flea market and a riverside walk. She walks in, wanders one spot at a time, buys a souvenir, takes a photo, has something to eat or drink, and the closing receipt sums the trip up in one line she can log into her own day.\n\nWorks alongside other plugins but depends on none. With Fish Every Day installed, the own-time menu points at this world and the closing receipt reminds her to log the trip with feast_life_play; food and drink at the coffee shop, tea stand and restaurant are meant to be ordered from Fish Every Day, where the nine-axis flavour profile and aroma actually live — without it she only knows she had a bubble tea, and she is told not to invent how it tasted. With the image studio installed, the receipt names the scene and outfit to draw; venues the library does not have are marked as such instead of inventing a name. None of them are required, and it still works if the image library is gone.\n\nVersion 0.3.0 adds user-authored story packs, two-stop outings, unfinished-trip recovery, permanent exploration and searchable trip journals. Four panels include original venue art, action timelines and souvenir icons. User stories and place settings are stored separately from plugin code; the small preset story collection is optional, and custom places work without stories."
68
71
  }
69
72
  }
73
+ },
74
+ "devDependencies": {
75
+ "esbuild": "0.28.2"
70
76
  }
71
77
  }
package/dist/artwork.js DELETED
@@ -1,64 +0,0 @@
1
- /** Original miniature line scenes drawn for fun-world. No external assets or requests.
2
- * Shared geometry/palette keeps the twenty places one little illustrated world.
3
- * These are decorative illustrations, never photographs or records of a visit.
4
- */
5
- const line = (d, extra = '') => `<path d="${d}" ${extra}/>`;
6
- const rect = (x, y, w, h, r = 3, extra = '') => `<rect x="${x}" y="${y}" width="${w}" height="${h}" rx="${r}" ${extra}/>`;
7
- const circle = (x, y, r, extra = '') => `<circle cx="${x}" cy="${y}" r="${r}" ${extra}/>`;
8
- const warm = 'fill="var(--f-art-warm,#e9b97e)"';
9
- const mint = 'fill="var(--f-art-mint,#99c7b9)"';
10
- const blue = 'fill="var(--f-art-blue,#9abfd0)"';
11
- const pink = 'fill="var(--f-art-pink,#d9abc1)"';
12
- const paper = 'fill="var(--f-card,#fbfbfb)"';
13
- const scenes = {
14
- amusement: circle(139, 48, 34, blue) + line('M139 14V82M105 48H173M115 24L163 72M115 72L163 24M139 48L113 108H165Z') + [ [139,14],[173,48],[139,82],[105,48] ].map(([x,y]) => rect(x-6,y-4,12,10,3,warm)).join('') + line('M202 104V64H267V104M196 64L235 38L274 64Z', warm) + rect(220,77,25,27,5,pink) + line('M39 91V45M39 45L69 53L39 63Z',mint),
15
- aquarium: line('M68 108V57C68 8 249 8 249 57V108',blue) + line('M88 108V58C88 31 229 31 229 58V108',paper) + line('M132 56C154 39 180 41 192 52L210 45V67L192 60C170 73 144 68 132 56Z',mint) + circle(181,52,1.5,'fill="currentColor"') + line('M105 108L108 87L117 100L121 78L128 108M208 108L215 82L219 99L228 89L231 108') + circle(111,57,3) + circle(118,43,2),
16
- templefair: line('M67 108V38M91 108V38M225 108V38M249 108V38M52 31Q157 47 264 31L260 44H56ZM58 56H258',warm) + line('M102 80H215M109 80V107M207 80V107M99 79L158 54L218 79Z',mint) + [111,157,203].map(x=>circle(x,60,9,pink)+line(`M${x} 47V51M${x} 69V74`)).join(''),
17
- con: rect(77,35,164,72,5,paper) + rect(91,48,63,47,3,pink) + line('M107 83L116 65L124 76L137 57L146 83Z') + rect(169,49,56,13,2,warm) + line('M169 73H225M169 83H209M91 107V114M227 107V114') + line('M47 57L53 43L59 57L73 63L59 69L53 83L47 69L33 63Z',blue),
18
- cinema: rect(75,23,174,79,5,blue) + rect(89,35,146,53,2,paper) + line('M149 46L149 76L175 61Z',warm) + [98,131,164,197,230].map(x=>rect(x-9,96,21,16,4,pink)).join('') + line('M42 96L38 68H62L58 96Z',warm) + line('M40 68Q39 54 48 59Q58 48 62 62'),
19
- zoo: rect(73,36,177,71,10,mint) + line('M82 42L100 22H224L243 42',warm) + line('M117 107V72Q117 46 142 46Q167 46 167 72V107',paper) + circle(137,62,3) + circle(153,62,3) + line('M140 78Q146 84 152 78M201 107V67M195 67Q177 42 193 43Q200 24 207 43Q226 38 219 57Z',blue) + circle(53,61,18,mint) + line('M53 79V107'),
20
- park: line('M119 114Q125 89 154 78Q176 67 178 49',warm) + circle(76,54,27,mint) + circle(236,49,33,mint) + line('M76 65V109M236 62V109') + rect(104,75,63,10,3,paper) + line('M110 86V102M160 86V102M105 67H166M113 67V76M157 67V76') + circle(157,25,13,warm),
21
- arcade: rect(105,25,85,82,7,pink) + rect(116,35,63,39,3,blue) + line('M129 58L139 45L150 60L159 49L169 65') + line('M105 81L94 94H199L190 81Z',warm) + circle(126,88,3) + circle(168,88,3) + line('M122 85V78') + rect(128,98,37,4,1,paper) + line('M224 45L235 34M233 62H251M215 32V19'),
22
- ktv: rect(62,29,126,69,6,blue) + line('M78 47H161M78 62H134M78 77H151') + line('M220 53L190 95Q187 103 195 106Q201 108 205 103L234 64',warm) + circle(231,48,17,pink) + line('M220 35L242 60M214 43L235 65M206 106Q236 121 255 99M272 31V61Q258 55 257 65Q260 77 273 69V40L290 35V55'),
23
- museum: line('M64 47L159 15L253 47Z',warm) + rect(65,47,188,10,1,paper) + [86,125,164,203].map(x=>rect(x,57,16,43,2,blue)).join('') + rect(61,101,195,10,1,paper) + circle(159,36,8,mint) + line('M41 110H278'),
24
- catcafe: rect(66,30,123,71,5,warm) + line('M76 43H178M128 43V101M76 77H178') + line('M201 97C177 89 185 72 199 73L198 58L211 67L225 59L228 78C242 102 217 110 201 97Z',pink) + circle(207,81,1.5,'fill="currentColor"') + circle(220,81,1.5,'fill="currentColor"') + line('M206 91Q215 96 221 88M237 98Q263 100 253 83') + rect(89,87,18,15,3,paper) + line('M107 90Q119 90 112 98'),
25
- hiking: line('M45 108L128 21L197 108Z',mint) + line('M127 21L104 47L128 43L145 47Z',paper) + line('M143 108L219 42L278 108Z',blue) + line('M150 108L145 94L164 79L164 70M219 42L205 56H232',paper) + circle(241,24,12,warm) + line('M83 109V85M75 93L83 79L91 93ZM248 109V86M239 96L248 78L257 96Z'),
26
- cafe: rect(77,30,159,77,4,warm) + line('M69 48H243L232 25H81Z',pink) + rect(89,61,61,46,3,blue) + rect(167,61,55,34,3,paper) + line('M177 73H204V85H177ZM204 75Q216 74 211 82M183 64Q178 59 184 55M195 64Q190 59 196 55M119 62V106M89 82H150'),
27
- milktea: rect(77,29,164,78,5,pink) + rect(88,38,141,15,2,paper) + line('M107 64L113 105H145L151 64Z',warm) + line('M102 64H156M127 64L135 40',paper) + [119,129,139].map(x=>circle(x,94,2.5,'fill="currentColor"')).join('') + rect(174,67,40,40,3,blue) + line('M179 80H209M179 89H201'),
28
- diner: rect(67,29,183,78,6,warm) + rect(80,40,69,48,3,blue) + line('M114 41V87M80 64H149') + line('M171 80H225M177 81V105M220 81V105') + line('M184 69H216Q217 56 201 56Q184 56 184 69ZM201 56V51',paper) + line('M183 36V47M196 32V43M209 36V47') + rect(88,95,51,11,3,pink),
29
- library: rect(71,29,177,78,4,warm) + line('M87 35V99M155 35V99M231 35V99M87 65H231M87 99H231') + [95,110,126,164,179,211].map((x,i)=>rect(x, i%2?45:41,10,i%2?19:23,1,i%2?mint:blue)).join('') + line('M106 83Q133 75 159 85Q183 75 208 83V111Q183 102 159 111Q134 102 106 111ZM159 85V111',paper),
30
- market: line('M51 31Q156 58 269 30') + [75,116,158,200,242].map(x=>circle(x,x===158?52:45,7,warm)).join('') + line('M60 104V77H130V104M49 77L67 59H123L141 77Z',pink) + line('M180 104V77H250V104M169 77L187 59H243L261 77Z',blue) + line('M73 90H118M192 89H237M81 104V92M225 104V90') + rect(142,93,22,14,2,mint),
31
- bathhouse: rect(69,33,181,74,5,warm) + line('M60 34L89 18H229L258 34Z',blue) + rect(89,53,142,54,2,paper) + line('M89 54H231V76Q211 66 195 76Q177 67 160 77Q141 67 124 76Q105 66 89 76ZM160 54V107',mint) + line('M112 44Q102 34 112 25M156 45Q145 34 156 25M200 44Q189 34 200 25'),
32
- thrift: rect(67,29,179,78,4,mint) + line('M60 48H253L241 26H73Z',warm) + rect(84,64,56,43,3,paper) + circle(112,81,12,blue) + line('M112 72V81L119 84') + line('M163 80L180 65L197 80M180 65V59Q180 53 187 55M163 80H197',pink) + rect(205,86,26,21,2,pink) + line('M210 86V79H226V86M90 103L102 96H129'),
33
- riverside: line('M36 90Q93 80 151 90T284 90M36 103Q93 93 151 103T284 103M52 115Q99 107 145 115M187 115Q224 107 269 115',blue) + line('M62 82V57M88 82V57M114 82V57M140 82V57M57 63H147M57 75H147') + line('M165 60Q202 8 262 60M173 60V78M253 60V78M170 60H259',warm) + circle(103,26,13,pink) + line('M218 105L230 96H253L247 105Z',mint),
34
- };
35
-
36
- export function venueArtwork(id) {
37
- const drawing = scenes[id] ?? (line('M83 107V51L160 20L237 51V107Z',mint) + rect(108,64,32,43,3,paper) + rect(169,62,42,27,3,blue));
38
- return `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 320 128" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false"><path d="M28 112H292" opacity=".2"/><g>${drawing}</g></svg>`;
39
- }
40
-
41
- const symbols = {
42
- ticket: line('M4 6H20V10Q16 12 20 14V18H4V14Q8 12 4 10ZM14 7V9M14 11V13M14 15V17'),
43
- keychain: circle(9,7,4) + line('M9 11V15M7 15H15L18 21H5Z'),
44
- bookmark: line('M7 3H17V21L12 17L7 21ZM10 7H14M10 10H14'),
45
- postcard: rect(3,5,18,14,2) + line('M14 8V16M17 9H19M6 14L9 10L12 14ZM6 16H12'),
46
- sticker: line('M4 4H20V15L15 20H4ZM15 20V15H20') + circle(9,9,1) + circle(15,9,1) + line('M8 12Q12 16 16 12'),
47
- coin: circle(12,12,9) + circle(12,12,6) + line('M12 7L14 10L17 12L14 14L12 17L10 14L7 12L10 10Z'),
48
- cup: line('M5 8H16V17Q10 22 5 17ZM16 10H20V15H16M8 3V5M12 3V5'),
49
- light: line('M8 15Q1 7 8 4Q16 0 19 8Q20 12 16 15V19H8ZM9 22H15M9 16H15'),
50
- plush: circle(6,6,3) + circle(18,6,3) + circle(12,10,7) + line('M7 16Q4 23 12 22Q20 23 17 16') + circle(9,9,1) + circle(15,9,1) + line('M11 12H13'),
51
- charm: line('M12 3V6M7 6H17L20 12L17 21H7L4 12ZM9 10H15M9 14H15M10 18H14'),
52
- generic: rect(4,8,16,13,2) + rect(3,5,18,4,1) + line('M12 5V21M12 5Q4 4 7 1Q10 0 12 5Q14 0 17 1Q20 4 12 5'),
53
- };
54
- export function souvenirCategory(name) {
55
- const value = String(name ?? '');
56
- return (/票|券|入场/.test(value) ? 'ticket' : /钥匙|挂件|挂饰|吊坠/.test(value) ? 'keychain'
57
- : /书签/.test(value) ? 'bookmark' : /明信片|卡片|画片|海报/.test(value) ? 'postcard'
58
- : /贴纸|贴画|徽章/.test(value) ? 'sticker' : /币|章/.test(value) ? 'coin'
59
- : /杯|茶|咖啡/.test(value) ? 'cup' : /灯/.test(value) ? 'light'
60
- : /玩偶|公仔|小熊|毛绒/.test(value) ? 'plush' : /御守|护符|签/.test(value) ? 'charm' : 'generic');
61
- }
62
- export function souvenirArtwork(name) {
63
- return `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">${symbols[souvenirCategory(name)]}</svg>`;
64
- }
package/dist/stories.js DELETED
@@ -1,395 +0,0 @@
1
- /** Venue story pack authoring for Coo WebUI. */
2
- const CSS = `
3
- .fun-stories{--fs-line:var(--border,#e3e4e3);--fs-ink:var(--foreground,#1b1a1e);--fs-dim:var(--muted-foreground,#5c5c60);--fs-card:var(--surface,#fbfbfb);--fs-soft:color-mix(in srgb,var(--fs-card) 93%,var(--fs-ink));--fs-accent:var(--accent,#00a870);--fs-error:var(--destructive,#b23b35);font:14px/1.65 var(--sans,-apple-system,"Segoe UI","PingFang SC","Microsoft YaHei",sans-serif);color:var(--fs-ink);padding:8px 4px 24px;overflow-wrap:anywhere}
4
- .fun-stories *{box-sizing:border-box}
5
- .fun-stories h2,.fun-stories h3,.fun-stories h4,.fun-stories p{margin:0}
6
- .fun-stories h2{font-size:20px}.fun-stories h3{font-size:15px}.fun-stories h4{font-size:14px}
7
- .fun-story-heading{margin-bottom:14px;padding:4px 0 12px;border-bottom:1px solid var(--fs-line)}
8
- .fun-story-note,.fun-story-field,.fun-story-feedback{color:var(--fs-dim);font-size:12px}
9
- .fun-story-editor,.fun-story-pack{border:1px solid var(--fs-line);border-radius:12px;background:var(--fs-card);padding:12px;margin-bottom:9px}
10
- .fun-story-editor{background:var(--fs-soft);margin-bottom:16px}
11
- .fun-story-editor>summary{cursor:pointer;font-weight:600}
12
- .fun-story-grid{display:grid;grid-template-columns:repeat(auto-fit,minmax(min(160px,100%),1fr));gap:6px 10px;margin:8px 0}
13
- .fun-story-grid :is(input,select){display:block;width:100%;min-width:0;margin-top:3px}
14
- .fun-story-branches{grid-template-columns:repeat(auto-fit,minmax(min(235px,100%),1fr))}
15
- .fun-story-branch{margin:0;padding:6px 9px;min-width:0;border:1px solid var(--fs-line);border-radius:var(--radius-sm,6px)}
16
- .fun-story-branch>.fun-story-field{margin:5px 0}
17
- .fun-story-row{display:flex;align-items:center;gap:6px 8px;flex-wrap:wrap;margin:6px 0}
18
- .fun-story-row>h3,.fun-story-row>h4{margin-right:auto}
19
- .fun-story-field{display:block;font-weight:600}
20
- .fun-story-row label{display:inline-flex;align-items:center;gap:5px}
21
- .fun-stories button,.fun-stories select,.fun-stories textarea,.fun-story-grid input{font:inherit;color:inherit;background:var(--fs-card);border:1px solid var(--fs-line);border-radius:var(--radius-sm,6px);padding:4px 9px}
22
- .fun-stories button,.fun-stories select{min-height:31px;max-width:100%}
23
- .fun-stories button{cursor:pointer}
24
- .fun-stories button:hover{border-color:var(--fs-accent)}
25
- .fun-stories button[aria-disabled=true]{opacity:.55;cursor:default}
26
- .fun-stories :is(button,select,textarea,input):focus-visible{outline:2px solid var(--fs-accent);outline-offset:2px}
27
- .fun-stories .fun-story-primary{border-color:var(--fs-accent);background:var(--fs-accent);color:var(--accent-foreground,#fff);font-weight:600}
28
- .fun-stories .fun-story-danger,.fun-story-errors{color:var(--fs-error)}
29
- .fun-stories textarea{display:block;width:100%;margin:4px 0;resize:vertical;font:12px/1.55 ui-monospace,Consolas,monospace}
30
- .fun-story-badge{font-size:11.5px;color:var(--fs-dim);border:1px solid var(--fs-line);border-radius:999px;padding:0 7px;background:var(--fs-soft)}
31
- .fun-story-unavailable{border-style:dashed}
32
- .fun-stories ul,.fun-stories ol{margin:3px 0;padding-inline-start:19px}
33
- .fun-story-feedback>:empty,.fun-story-result:empty,.fun-story-confirm:empty{display:none}
34
- .fun-story-confirm,.fun-story-result{margin-top:8px}
35
- .fun-story-node{padding:5px 0;border-bottom:1px solid var(--fs-line)}
36
- .fun-story-node:last-child{border-bottom:0}
37
- .fun-story-node p{white-space:pre-wrap}
38
- @media(max-width:420px){.fun-stories{padding-inline:0}.fun-story-editor,.fun-story-pack{padding:10px}}
39
- `;
40
-
41
- function el(tag, attrs = {}, ...children) {
42
- const node = document.createElement(tag);
43
- for (const [key, value] of Object.entries(attrs)) node.setAttribute(key, value);
44
- for (const child of children.flat()) {
45
- if (child == null) continue;
46
- node.appendChild(typeof child === 'string' ? document.createTextNode(child) : child);
47
- }
48
- return node;
49
- }
50
-
51
- function messageList(messages) {
52
- return messages?.length ? el('ul', {}, messages.map((message) => el('li', {}, String(message)))) : null;
53
- }
54
-
55
- function feedback() {
56
- const status = el('div', { role: 'status' });
57
- const alert = el('div', { class: 'fun-story-errors', role: 'alert' });
58
- return {
59
- node: el('div', { class: 'fun-story-feedback' }, status, alert),
60
- set(text = '', result) {
61
- const warnings = messageList(result?.warnings);
62
- status.replaceChildren(...[text ? document.createTextNode(text) : null, warnings].filter(Boolean));
63
- const errors = [...new Set([result?.error, ...(result?.errors ?? [])].filter(Boolean))];
64
- if (result?.ok === false && !errors.length) errors.push('操作失败,请重试。');
65
- alert.replaceChildren(...(errors.length ? [messageList(errors)] : []));
66
- },
67
- };
68
- }
69
-
70
- function previewGraph(result) {
71
- const nodes = (result.nodes ?? []).map((node) => {
72
- const when = node.when && Object.keys(node.when).length
73
- ? `条件:${typeof node.when === 'string' ? node.when : JSON.stringify(node.when)}` : '';
74
- const choices = (node.choices ?? []).map((choice) => el('li', {}, `${choice.label || choice.id} → ${choice.next || '结束'}`));
75
- return el('li', { class: 'fun-story-node' },
76
- el('strong', {}, `${node.id}${node.area ? ` · ${node.area}` : ''}`),
77
- node.event ? el('p', {}, node.event) : null,
78
- when ? el('p', { class: 'fun-story-note' }, when) : null,
79
- node.next ? el('p', { class: 'fun-story-note' }, `下一节点 → ${node.next}`) : null,
80
- choices.length ? el('ul', {}, choices) : null,
81
- !node.next && !choices.length ? el('p', { class: 'fun-story-note' }, '结束') : null);
82
- });
83
- return el('section', { class: 'fun-story-graph', 'aria-label': '只读节点图' },
84
- el('h4', {}, result.title || '节点图'),
85
- el('p', { class: 'fun-story-note' }, result.eligible ? '当前预览条件匹配' : '当前预览条件未匹配'),
86
- el('ol', {}, nodes));
87
- }
88
-
89
- function label(text, control) {
90
- return el('label', { class: 'fun-story-field' }, text, control);
91
- }
92
-
93
- function fillSelect(select, items, placeholder) {
94
- const selected = select.value;
95
- select.replaceChildren(el('option', { value: '' }, placeholder),
96
- ...items.map(({ value, label }) => el('option', { value }, label)));
97
- select.value = items.some((item) => item.value === selected) ? selected : '';
98
- }
99
-
100
- function simpleEditor(button, save) {
101
- const select = (key) => el('select', { class: `fun-story-author-${key}`, 'data-story-field': key });
102
- const input = (key, max, type = 'text') => el('input', { class: `fun-story-author-${key}`, type, 'data-story-field': key, ...(max ? { maxlength: String(max) } : {}) });
103
- const place = select('place');
104
- const area = select('area');
105
- const id = input('id', 24);
106
- id.value = `user-${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 7)}`;
107
- const title = input('title', 80);
108
- const event = input('event', 120);
109
- const prompt = input('prompt', 160);
110
- const from = input('from', null, 'time');
111
- const to = input('to', null, 'time');
112
- const branches = ['a', 'b'].map((key, index) => {
113
- const choice = input(`${key}-label`, 160);
114
- const result = input(`${key}-result`, 160);
115
- const event = input(`${key}-event`, 120);
116
- const area = select(`${key}-area`);
117
- const node = el('fieldset', { class: 'fun-story-branch' }, el('legend', { class: 'fun-story-field' }, `选项 ${index + 1}`),
118
- label('选项文字', choice), label('选择结果', result), label('后续事件', event), label('后续区域', area));
119
- return { key, choice, result, event, area, node };
120
- });
121
- let places = [];
122
- const report = feedback();
123
- const updateAreas = () => {
124
- const areas = (places.find((item) => item.id === place.value)?.areas ?? []).map((name) => ({ value: name, label: name }));
125
- fillSelect(area, areas, '选择起始区域');
126
- for (const branch of branches) fillSelect(branch.area, areas, '同起始区域');
127
- };
128
- place.addEventListener('change', () => {
129
- area.value = '';
130
- for (const branch of branches) branch.area.value = '';
131
- updateAreas();
132
- });
133
- const saveButton = button('保存新故事包', () => {
134
- const fields = [place, area, id, title, event, prompt, ...branches.flatMap((branch) => [branch.choice, branch.result, branch.event])];
135
- const missing = fields.find((field) => !field.value.trim());
136
- if (missing) {
137
- report.set('', { ok: false, error: '请填写地点、起始区域和故事内容。' });
138
- missing.focus();
139
- return;
140
- }
141
- if (!/^user-[a-z0-9_-]{1,19}$/.test(id.value.trim())) {
142
- report.set('', { ok: false, error: '标识须以 user- 开头,最多 24 字符,只能含小写字母、数字、短横线或下划线。' });
143
- id.focus();
144
- return;
145
- }
146
- if (Boolean(from.value) !== Boolean(to.value) || (from.value && from.value === to.value)) {
147
- report.set('', { ok: false, error: '请填写不同的开始和结束时间,或将两项都留空。' });
148
- (from.value && !to.value ? to : from).focus();
149
- return;
150
- }
151
- const start = { id: 'start', area: area.value, event: event.value.trim(), prompt: prompt.value.trim(),
152
- choices: branches.map((branch) => ({ id: branch.key, label: branch.choice.value.trim(), result: branch.result.value.trim(), next: `end-${branch.key}` })) };
153
- if (from.value) start.when = { from: from.value, to: to.value };
154
- const nodes = [start, ...branches.map((branch) => ({ id: `end-${branch.key}`, area: branch.area.value || area.value, event: branch.event.value.trim() }))];
155
- save(report, JSON.stringify({ version: 1, id: id.value.trim(), title: title.value.trim(), placeId: place.value, start: start.id, nodes }));
156
- }, { class: 'fun-story-action fun-story-primary' });
157
- const node = el('section', { class: 'fun-story-editor', 'aria-label': '编写故事' },
158
- el('h3', {}, '编写故事'),
159
- el('div', { class: 'fun-story-grid' }, label('地点', place), label('起始区域', area), label('故事标题', title), label('故事标识', id)),
160
- el('div', { class: 'fun-story-grid' }, label('起始事件', event), label('选择提问', prompt)),
161
- el('div', { class: 'fun-story-grid fun-story-branches' }, branches.map((branch) => branch.node)),
162
- el('details', {}, el('summary', {}, '触发时段(可选)'),
163
- el('div', { class: 'fun-story-grid' }, label('开始时间', from), label('结束时间', to))),
164
- el('div', { class: 'fun-story-row' }, saveButton), report.node);
165
- function update(items) {
166
- places = items;
167
- fillSelect(place, items.map((item) => ({ value: item.id, label: item.name || item.id })), '选择地点');
168
- updateAreas();
169
- }
170
- update([]);
171
- return { node, update };
172
- }
173
-
174
- export function mountStories(ctx) {
175
- const { root } = ctx;
176
- const box = el('div', { class: 'fun-stories' });
177
- root.replaceChildren(el('style', {}, CSS), box);
178
- let busy = false;
179
- let listRequest = 0;
180
- let lastPacks;
181
- let draftEdits = 0;
182
-
183
- function button(text, handler, attrs = {}) {
184
- const node = el('button', { type: 'button', ...attrs }, text);
185
- node.addEventListener('click', () => {
186
- if (node.getAttribute('aria-disabled') !== 'true' && !ctx.signal?.aborted) handler();
187
- });
188
- return node;
189
- }
190
-
191
- const errorResult = (error) => ({ ok: false, error: error?.message ?? String(error) });
192
- function setBusy(value) {
193
- busy = value;
194
- for (const node of box.querySelectorAll('.fun-story-action')) node.setAttribute('aria-disabled', String(value));
195
- }
196
- async function run(report, pending, method, args, render) {
197
- if (busy) return;
198
- setBusy(true);
199
- report.set(pending);
200
- try {
201
- const result = await ctx.invoke(method, [args]);
202
- if (!ctx.signal?.aborted) await render(result ?? { ok: false, error: '返回结果为空。' });
203
- } catch (error) {
204
- if (!ctx.signal?.aborted) report.set('', errorResult(error));
205
- } finally {
206
- if (!ctx.signal?.aborted) setBusy(false);
207
- }
208
- }
209
-
210
- const place = el('select', { class: 'fun-story-place' });
211
- const defaultPlace = () => el('option', { value: '' }, '默认地点');
212
- place.appendChild(defaultPlace());
213
- const draft = el('textarea', { class: 'fun-story-json', rows: '8', spellcheck: 'false', placeholder: '粘贴故事包 JSON…' });
214
- const overwrite = el('input', { type: 'checkbox', class: 'fun-story-overwrite' });
215
- const editorReport = feedback();
216
- const editorResult = el('div', { class: 'fun-story-result' });
217
- // List renders and pending requests never replace the editable control.
218
- draft.addEventListener('input', () => {
219
- draftEdits += 1;
220
- if (!busy) editorReport.set();
221
- editorResult.replaceChildren();
222
- });
223
-
224
- function editAction(text, method, pending) {
225
- return button(text, () => {
226
- const json = draft.value;
227
- const edits = draftEdits;
228
- const placeId = place.value;
229
- const args = method === 'template' ? (placeId ? { placeId } : {}) : { json };
230
- if (method === 'import' && overwrite.checked) args.replace = true;
231
- editorResult.replaceChildren();
232
- void run(editorReport, pending, method, args, async (result) => {
233
- const changed = draftEdits !== edits || draft.value !== json;
234
- const previous = changed ? '上次提交:' : '';
235
- if (method === 'template') {
236
- if (result.ok && !changed) {
237
- if (typeof result.json !== 'string') throw new Error('模板没有返回 JSON。');
238
- draft.value = result.json;
239
- overwrite.checked = false;
240
- }
241
- editorReport.set(result.ok ? (changed ? '草稿已修改,保留当前内容。' : '模板已填入。') : '', result);
242
- } else if (method === 'validate') {
243
- editorReport.set(previous + (result.ok ? '校验通过。' : '校验未通过。'), result);
244
- } else if (method === 'preview') {
245
- editorReport.set(previous + (result.ok ? '只读预览已生成。' : '预览失败。'), result);
246
- if (result.ok) editorResult.appendChild(previewGraph(result));
247
- } else {
248
- editorReport.set(previous + (result.ok ? `已导入${result.id ? ` · ${result.id}` : ''}。` : '导入失败。'), result);
249
- if (result.ok) {
250
- overwrite.checked = false;
251
- await load();
252
- }
253
- }
254
- });
255
- }, { class: `fun-story-action${method === 'import' ? ' fun-story-primary' : ''}` });
256
- }
257
-
258
- const loadReport = feedback();
259
- const libraryReport = feedback();
260
- const packList = el('div', { class: 'fun-story-list' });
261
- const refreshButton = button('刷新列表', () => { void load(); });
262
- const simple = simpleEditor(button, (report, json) => {
263
- void run(report, '正在保存新故事包…', 'import', { json }, async (result) => {
264
- report.set(result.ok ? `已保存新故事包${result.id ? ` · ${result.id}` : ''}。` : '保存失败。', result);
265
- if (result.ok) await load();
266
- });
267
- });
268
- box.replaceChildren(
269
- el('header', { class: 'fun-story-heading' }, el('h2', {}, '故事包'),
270
- el('p', { class: 'fun-story-note' }, '为地点编写事件节点与分支。草稿仅在导入时保存。')),
271
- simple.node,
272
- el('details', { class: 'fun-story-advanced' },
273
- el('summary', {}, '高级编辑 · 查看或粘贴完整 JSON'),
274
- el('section', { class: 'fun-story-editor', 'aria-label': '高级 JSON 编辑器' },
275
- el('h3', {}, '完整 JSON'),
276
- el('div', { class: 'fun-story-row' }, el('label', { class: 'fun-story-field' }, '模板地点', place),
277
- editAction('填入模板', 'template', '正在取模板…')),
278
- el('label', { class: 'fun-story-field' }, '故事包 JSON', draft),
279
- el('div', { class: 'fun-story-row' }, editAction('校验 JSON', 'validate', '正在校验…'),
280
- editAction('预览草稿', 'preview', '正在预览…'), editAction('导入故事包', 'import', '正在导入…'),
281
- el('label', { class: 'fun-story-field' }, overwrite, '覆盖同 ID 故事包')),
282
- editorReport.node, editorResult)),
283
- el('section', { 'aria-label': '故事包列表' },
284
- el('div', { class: 'fun-story-row' }, el('h3', {}, '已收录故事包'), refreshButton),
285
- loadReport.node, libraryReport.node, packList));
286
-
287
- function packCard(pack) {
288
- const title = pack.title || pack.id;
289
- const report = feedback();
290
- const resultBox = el('div', { class: 'fun-story-result' });
291
- const confirm = el('div', { class: 'fun-story-confirm' });
292
- const packAction = (text, key, handler, css = '') => button(text, handler, {
293
- class: `fun-story-action ${css}`, 'aria-disabled': String(busy),
294
- 'aria-label': `${text} ${title}`, 'data-story-focus': `${pack.id}:${key}`,
295
- });
296
- const read = (method, label) => {
297
- const opener = packAction(label, method, () => {
298
- confirm.replaceChildren();
299
- resultBox.replaceChildren();
300
- void run(report, method === 'preview' ? '正在预览…' : '正在导出…', method, { id: pack.id }, (result) => {
301
- report.set(result.ok ? (method === 'preview' ? '只读预览已生成。' : '已导出,可复制 JSON。') : '', result);
302
- if (!result.ok) return;
303
- if (method === 'preview') {
304
- resultBox.appendChild(previewGraph(result));
305
- } else {
306
- if (typeof result.json !== 'string') throw new Error('导出没有返回 JSON。');
307
- const output = el('textarea', { class: 'fun-story-export', rows: '6', readonly: '', spellcheck: 'false' });
308
- output.value = result.json;
309
- resultBox.appendChild(el('label', { class: 'fun-story-field' }, `导出的 JSON · ${title}`, output));
310
- }
311
- resultBox.appendChild(button('收起', () => {
312
- resultBox.replaceChildren();
313
- report.set();
314
- opener.focus();
315
- }));
316
- });
317
- });
318
- return opener;
319
- };
320
- const mutate = (method, label) => {
321
- void run(report, `正在${label}…`, method, { id: pack.id }, async (result) => {
322
- libraryReport.set();
323
- report.set('', result);
324
- if (!result.ok) return;
325
- libraryReport.set(`已${label}「${title}」。`, result);
326
- await load();
327
- confirm.replaceChildren();
328
- });
329
- };
330
- const toggle = pack.enabled ? ['disable', '停用'] : ['enable', '启用'];
331
- const remove = pack.builtin ? null : packAction('移除', 'remove', () => {
332
- const cancel = button('取消', () => { confirm.replaceChildren(); remove.focus(); });
333
- confirm.replaceChildren(el('span', {}, `移除「${title}」?`),
334
- el('div', { class: 'fun-story-row' }, cancel, packAction('确认移除', 'confirm', () => mutate('remove', '移除'), 'fun-story-danger')));
335
- cancel.focus();
336
- }, 'fun-story-danger');
337
- const warnings = pack.warnings?.length ? pack.warnings : pack.available === false ? ['绑定地点或区域当前不匹配。'] : [];
338
- return el('article', { class: 'fun-story-pack', 'data-pack-id': pack.id },
339
- el('div', { class: 'fun-story-row' }, el('h4', {}, title),
340
- el('span', { class: 'fun-story-badge' }, pack.builtin ? '内置' : '自定义'),
341
- el('span', { class: 'fun-story-badge' }, pack.enabled ? '已启用' : '已停用'),
342
- pack.available === false ? el('span', { class: 'fun-story-badge fun-story-unavailable' }, '暂不可用') : null),
343
- el('p', { class: 'fun-story-note' }, `${pack.placeName || pack.placeId || '未绑定地点'} · ${pack.nodeCount} 个节点 · 版本 ${pack.revision}`),
344
- pack.description ? el('p', {}, pack.description) : null,
345
- warnings.length ? el('div', { class: 'fun-story-note' }, messageList(warnings)) : null,
346
- el('div', { class: 'fun-story-row' }, read('preview', '预览'), read('export', '导出 JSON'),
347
- packAction(toggle[1], 'toggle', () => mutate(...toggle)), remove), confirm, report.node, resultBox);
348
- }
349
-
350
- function renderList(data) {
351
- const selected = place.value;
352
- place.replaceChildren(defaultPlace(), ...data.places.map((item) => el('option', { value: item.id }, item.name || item.id)));
353
- place.value = data.places.some((item) => item.id === selected) ? selected : '';
354
- simple.update(data.places);
355
- const signature = JSON.stringify(data.packs);
356
- if (signature === lastPacks) return;
357
- const focused = root.activeElement ?? document.activeElement;
358
- const buttonWasFocused = [...packList.querySelectorAll('button')].includes(focused);
359
- const focusKey = focused?.getAttribute?.('data-story-focus');
360
- const cards = data.packs.map(packCard);
361
- packList.replaceChildren(...(cards.length ? cards : [el('p', { class: 'fun-story-note' }, '还没有故事包。')]));
362
- lastPacks = signature;
363
- if (buttonWasFocused) {
364
- const replacement = [...packList.querySelectorAll('button')].find((node) => focusKey && node.getAttribute('data-story-focus') === focusKey);
365
- (replacement || refreshButton).focus();
366
- }
367
- }
368
-
369
- async function load() {
370
- if (ctx.signal?.aborted) return;
371
- const sequence = ++listRequest;
372
- refreshButton.setAttribute('aria-disabled', 'true');
373
- loadReport.set('正在读取故事包…');
374
- try {
375
- const data = await ctx.invoke('list', []);
376
- if (sequence !== listRequest || ctx.signal?.aborted) return;
377
- if (!data?.ok) {
378
- loadReport.set('列表加载失败。', data ?? { ok: false });
379
- return;
380
- }
381
- renderList(data);
382
- loadReport.set('', data);
383
- } catch (error) {
384
- if (sequence === listRequest && !ctx.signal?.aborted) loadReport.set('列表加载失败。', errorResult(error));
385
- } finally {
386
- if (sequence === listRequest && !ctx.signal?.aborted) {
387
- refreshButton.setAttribute('aria-disabled', 'false');
388
- refreshButton.textContent = loadReport.node.querySelector('.fun-story-errors').textContent ? '重试' : '刷新列表';
389
- }
390
- }
391
- }
392
-
393
- void load();
394
- return { refresh: load };
395
- }