@shiqianjiang/ceru-plugin-cli 0.1.0

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 (314) hide show
  1. package/ASSET_LICENSE +238 -0
  2. package/LICENSE +21 -0
  3. package/NOTICE +1 -0
  4. package/README.md +29 -0
  5. package/assets/catalog.json +1 -0
  6. package/assets/electron-app/main.cjs +1 -0
  7. package/assets/electron-app/package.json +8 -0
  8. package/assets/electron.cjs +42 -0
  9. package/assets/index.html +93 -0
  10. package/assets/originals/account.svg +1 -0
  11. package/assets/originals/add.svg +1 -0
  12. package/assets/originals/app.logo.svg +10 -0
  13. package/assets/originals/chart.svg +1 -0
  14. package/assets/originals/close.svg +1 -0
  15. package/assets/originals/cloud.svg +1 -0
  16. package/assets/originals/download.svg +1 -0
  17. package/assets/originals/heart.svg +1 -0
  18. package/assets/originals/import.svg +1 -0
  19. package/assets/originals/info.svg +1 -0
  20. package/assets/originals/library.svg +1 -0
  21. package/assets/originals/music-note.svg +1 -0
  22. package/assets/originals/pause.svg +1 -0
  23. package/assets/originals/placeholder.avatar.webp +0 -0
  24. package/assets/originals/placeholder.cover.png +0 -0
  25. package/assets/originals/platform.all.svg +1 -0
  26. package/assets/originals/platform.kg.svg +1 -0
  27. package/assets/originals/platform.kw.svg +1 -0
  28. package/assets/originals/platform.local.svg +1 -0
  29. package/assets/originals/platform.mg.svg +1 -0
  30. package/assets/originals/platform.tx.svg +1 -0
  31. package/assets/originals/platform.wy.svg +1 -0
  32. package/assets/originals/play.svg +1 -0
  33. package/assets/originals/refresh.svg +1 -0
  34. package/assets/originals/search.svg +1 -0
  35. package/assets/originals/server.svg +1 -0
  36. package/assets/originals/settings.svg +1 -0
  37. package/assets/originals/share.svg +1 -0
  38. package/assets/playground.js +446 -0
  39. package/assets/sandbox.js +36 -0
  40. package/assets/shared-react.js +77 -0
  41. package/assets/shared-vue.js +44 -0
  42. package/assets/style.css +264 -0
  43. package/assets/vendor-licenses/lodash-es.txt +47 -0
  44. package/assets/vendor-licenses/react-dom.txt +21 -0
  45. package/assets/vendor-licenses/react.txt +21 -0
  46. package/assets/vendor-licenses/scheduler.txt +21 -0
  47. package/assets/vendor-licenses/vue.txt +21 -0
  48. package/assets/vendor-licenses/vue_reactivity.txt +21 -0
  49. package/assets/vendor-licenses/vue_runtime-core.txt +21 -0
  50. package/assets/vendor-licenses/vue_runtime-dom.txt +21 -0
  51. package/assets/vendor-licenses/vue_shared.txt +21 -0
  52. package/dist/bin.d.ts +2 -0
  53. package/dist/bin.js +6 -0
  54. package/dist/dev.d.ts +7 -0
  55. package/dist/dev.js +507 -0
  56. package/dist/frameworks.d.ts +9 -0
  57. package/dist/frameworks.js +310 -0
  58. package/dist/index.d.ts +2 -0
  59. package/dist/index.js +206 -0
  60. package/dist/project.d.ts +34 -0
  61. package/dist/project.js +342 -0
  62. package/package.json +67 -0
  63. package/schemas/config.schema.json +610 -0
  64. package/templates/connected-library/js/.prettierrc.json +5 -0
  65. package/templates/connected-library/js/.vscode/ceru.code-snippets +27 -0
  66. package/templates/connected-library/js/.vscode/extensions.json +3 -0
  67. package/templates/connected-library/js/.vscode/launch.json +30 -0
  68. package/templates/connected-library/js/.vscode/settings.json +20 -0
  69. package/templates/connected-library/js/.vscode/tasks.json +36 -0
  70. package/templates/connected-library/js/README.md +38 -0
  71. package/templates/connected-library/js/_gitignore +6 -0
  72. package/templates/connected-library/js/ceru.plugin.json +76 -0
  73. package/templates/connected-library/js/package.json +21 -0
  74. package/templates/connected-library/js/src/index.js +38 -0
  75. package/templates/connected-library/js/tsconfig.json +16 -0
  76. package/templates/connected-library/js/ui/settings.json +24 -0
  77. package/templates/connected-library/ts/.prettierrc.json +5 -0
  78. package/templates/connected-library/ts/.vscode/ceru.code-snippets +27 -0
  79. package/templates/connected-library/ts/.vscode/extensions.json +3 -0
  80. package/templates/connected-library/ts/.vscode/launch.json +30 -0
  81. package/templates/connected-library/ts/.vscode/settings.json +20 -0
  82. package/templates/connected-library/ts/.vscode/tasks.json +36 -0
  83. package/templates/connected-library/ts/README.md +38 -0
  84. package/templates/connected-library/ts/_gitignore +6 -0
  85. package/templates/connected-library/ts/ceru.plugin.json +76 -0
  86. package/templates/connected-library/ts/package.json +21 -0
  87. package/templates/connected-library/ts/src/index.ts +38 -0
  88. package/templates/connected-library/ts/tsconfig.json +16 -0
  89. package/templates/connected-library/ts/ui/settings.json +24 -0
  90. package/templates/guest-adapter/js/.prettierrc.json +5 -0
  91. package/templates/guest-adapter/js/.vscode/ceru.code-snippets +27 -0
  92. package/templates/guest-adapter/js/.vscode/extensions.json +3 -0
  93. package/templates/guest-adapter/js/.vscode/launch.json +30 -0
  94. package/templates/guest-adapter/js/.vscode/settings.json +20 -0
  95. package/templates/guest-adapter/js/.vscode/tasks.json +36 -0
  96. package/templates/guest-adapter/js/README.md +38 -0
  97. package/templates/guest-adapter/js/_gitignore +6 -0
  98. package/templates/guest-adapter/js/ceru.plugin.json +95 -0
  99. package/templates/guest-adapter/js/package.json +21 -0
  100. package/templates/guest-adapter/js/src/guest.js +6 -0
  101. package/templates/guest-adapter/js/src/index.js +8 -0
  102. package/templates/guest-adapter/js/tsconfig.json +16 -0
  103. package/templates/guest-adapter/js/ui/settings.json +14 -0
  104. package/templates/guest-adapter/ts/.prettierrc.json +5 -0
  105. package/templates/guest-adapter/ts/.vscode/ceru.code-snippets +27 -0
  106. package/templates/guest-adapter/ts/.vscode/extensions.json +3 -0
  107. package/templates/guest-adapter/ts/.vscode/launch.json +30 -0
  108. package/templates/guest-adapter/ts/.vscode/settings.json +20 -0
  109. package/templates/guest-adapter/ts/.vscode/tasks.json +36 -0
  110. package/templates/guest-adapter/ts/README.md +38 -0
  111. package/templates/guest-adapter/ts/_gitignore +6 -0
  112. package/templates/guest-adapter/ts/ceru.plugin.json +95 -0
  113. package/templates/guest-adapter/ts/package.json +21 -0
  114. package/templates/guest-adapter/ts/src/guest.ts +6 -0
  115. package/templates/guest-adapter/ts/src/index.ts +8 -0
  116. package/templates/guest-adapter/ts/tsconfig.json +16 -0
  117. package/templates/guest-adapter/ts/ui/settings.json +14 -0
  118. package/templates/importer/js/.prettierrc.json +5 -0
  119. package/templates/importer/js/.vscode/ceru.code-snippets +27 -0
  120. package/templates/importer/js/.vscode/extensions.json +3 -0
  121. package/templates/importer/js/.vscode/launch.json +30 -0
  122. package/templates/importer/js/.vscode/settings.json +20 -0
  123. package/templates/importer/js/.vscode/tasks.json +36 -0
  124. package/templates/importer/js/README.md +38 -0
  125. package/templates/importer/js/_gitignore +6 -0
  126. package/templates/importer/js/ceru.plugin.json +63 -0
  127. package/templates/importer/js/package.json +21 -0
  128. package/templates/importer/js/src/index.js +37 -0
  129. package/templates/importer/js/tsconfig.json +16 -0
  130. package/templates/importer/js/ui/settings.json +24 -0
  131. package/templates/importer/ts/.prettierrc.json +5 -0
  132. package/templates/importer/ts/.vscode/ceru.code-snippets +27 -0
  133. package/templates/importer/ts/.vscode/extensions.json +3 -0
  134. package/templates/importer/ts/.vscode/launch.json +30 -0
  135. package/templates/importer/ts/.vscode/settings.json +20 -0
  136. package/templates/importer/ts/.vscode/tasks.json +36 -0
  137. package/templates/importer/ts/README.md +38 -0
  138. package/templates/importer/ts/_gitignore +6 -0
  139. package/templates/importer/ts/ceru.plugin.json +63 -0
  140. package/templates/importer/ts/package.json +21 -0
  141. package/templates/importer/ts/src/index.ts +37 -0
  142. package/templates/importer/ts/tsconfig.json +16 -0
  143. package/templates/importer/ts/ui/settings.json +24 -0
  144. package/templates/react/js/.prettierrc.json +5 -0
  145. package/templates/react/js/.vscode/ceru.code-snippets +27 -0
  146. package/templates/react/js/.vscode/extensions.json +3 -0
  147. package/templates/react/js/.vscode/launch.json +30 -0
  148. package/templates/react/js/.vscode/settings.json +20 -0
  149. package/templates/react/js/.vscode/tasks.json +36 -0
  150. package/templates/react/js/README.md +36 -0
  151. package/templates/react/js/_gitignore +6 -0
  152. package/templates/react/js/ceru.plugin.json +60 -0
  153. package/templates/react/js/package.json +25 -0
  154. package/templates/react/js/src/App.jsx +22 -0
  155. package/templates/react/js/src/index.js +7 -0
  156. package/templates/react/js/src/view.jsx +8 -0
  157. package/templates/react/js/tsconfig.json +16 -0
  158. package/templates/react/ts/.prettierrc.json +5 -0
  159. package/templates/react/ts/.vscode/ceru.code-snippets +27 -0
  160. package/templates/react/ts/.vscode/extensions.json +3 -0
  161. package/templates/react/ts/.vscode/launch.json +30 -0
  162. package/templates/react/ts/.vscode/settings.json +20 -0
  163. package/templates/react/ts/.vscode/tasks.json +36 -0
  164. package/templates/react/ts/README.md +36 -0
  165. package/templates/react/ts/_gitignore +6 -0
  166. package/templates/react/ts/ceru.plugin.json +60 -0
  167. package/templates/react/ts/package.json +25 -0
  168. package/templates/react/ts/src/App.tsx +23 -0
  169. package/templates/react/ts/src/index.ts +7 -0
  170. package/templates/react/ts/src/view.tsx +8 -0
  171. package/templates/react/ts/tsconfig.json +16 -0
  172. package/templates/snapshot.json +257 -0
  173. package/templates/source/js/.prettierrc.json +5 -0
  174. package/templates/source/js/.vscode/ceru.code-snippets +27 -0
  175. package/templates/source/js/.vscode/extensions.json +3 -0
  176. package/templates/source/js/.vscode/launch.json +30 -0
  177. package/templates/source/js/.vscode/settings.json +20 -0
  178. package/templates/source/js/.vscode/tasks.json +36 -0
  179. package/templates/source/js/README.md +38 -0
  180. package/templates/source/js/_gitignore +6 -0
  181. package/templates/source/js/ceru.plugin.json +51 -0
  182. package/templates/source/js/package.json +21 -0
  183. package/templates/source/js/src/index.js +50 -0
  184. package/templates/source/js/tsconfig.json +16 -0
  185. package/templates/source/ts/.prettierrc.json +5 -0
  186. package/templates/source/ts/.vscode/ceru.code-snippets +27 -0
  187. package/templates/source/ts/.vscode/extensions.json +3 -0
  188. package/templates/source/ts/.vscode/launch.json +30 -0
  189. package/templates/source/ts/.vscode/settings.json +20 -0
  190. package/templates/source/ts/.vscode/tasks.json +36 -0
  191. package/templates/source/ts/README.md +38 -0
  192. package/templates/source/ts/_gitignore +6 -0
  193. package/templates/source/ts/ceru.plugin.json +51 -0
  194. package/templates/source/ts/package.json +21 -0
  195. package/templates/source/ts/src/index.ts +50 -0
  196. package/templates/source/ts/tsconfig.json +16 -0
  197. package/templates/vue/js/.prettierrc.json +5 -0
  198. package/templates/vue/js/.vscode/ceru.code-snippets +27 -0
  199. package/templates/vue/js/.vscode/extensions.json +3 -0
  200. package/templates/vue/js/.vscode/launch.json +30 -0
  201. package/templates/vue/js/.vscode/settings.json +20 -0
  202. package/templates/vue/js/.vscode/tasks.json +36 -0
  203. package/templates/vue/js/README.md +36 -0
  204. package/templates/vue/js/_gitignore +6 -0
  205. package/templates/vue/js/ceru.plugin.json +59 -0
  206. package/templates/vue/js/package.json +22 -0
  207. package/templates/vue/js/src/App.vue +31 -0
  208. package/templates/vue/js/src/env.d.ts +5 -0
  209. package/templates/vue/js/src/index.js +7 -0
  210. package/templates/vue/js/src/view.js +8 -0
  211. package/templates/vue/js/tsconfig.json +17 -0
  212. package/templates/vue/ts/.prettierrc.json +5 -0
  213. package/templates/vue/ts/.vscode/ceru.code-snippets +27 -0
  214. package/templates/vue/ts/.vscode/extensions.json +3 -0
  215. package/templates/vue/ts/.vscode/launch.json +30 -0
  216. package/templates/vue/ts/.vscode/settings.json +20 -0
  217. package/templates/vue/ts/.vscode/tasks.json +36 -0
  218. package/templates/vue/ts/README.md +36 -0
  219. package/templates/vue/ts/_gitignore +6 -0
  220. package/templates/vue/ts/ceru.plugin.json +59 -0
  221. package/templates/vue/ts/package.json +22 -0
  222. package/templates/vue/ts/src/App.vue +32 -0
  223. package/templates/vue/ts/src/env.d.ts +5 -0
  224. package/templates/vue/ts/src/index.ts +7 -0
  225. package/templates/vue/ts/src/view.ts +8 -0
  226. package/templates/vue/ts/tsconfig.json +17 -0
  227. package/templates/vue-tsx/js/.prettierrc.json +5 -0
  228. package/templates/vue-tsx/js/.vscode/ceru.code-snippets +27 -0
  229. package/templates/vue-tsx/js/.vscode/extensions.json +3 -0
  230. package/templates/vue-tsx/js/.vscode/launch.json +30 -0
  231. package/templates/vue-tsx/js/.vscode/settings.json +20 -0
  232. package/templates/vue-tsx/js/.vscode/tasks.json +36 -0
  233. package/templates/vue-tsx/js/README.md +36 -0
  234. package/templates/vue-tsx/js/_gitignore +6 -0
  235. package/templates/vue-tsx/js/ceru.plugin.json +59 -0
  236. package/templates/vue-tsx/js/package.json +22 -0
  237. package/templates/vue-tsx/js/src/App.jsx +14 -0
  238. package/templates/vue-tsx/js/src/env.d.ts +5 -0
  239. package/templates/vue-tsx/js/src/index.js +7 -0
  240. package/templates/vue-tsx/js/src/view.js +8 -0
  241. package/templates/vue-tsx/js/tsconfig.json +17 -0
  242. package/templates/vue-tsx/ts/.prettierrc.json +5 -0
  243. package/templates/vue-tsx/ts/.vscode/ceru.code-snippets +27 -0
  244. package/templates/vue-tsx/ts/.vscode/extensions.json +3 -0
  245. package/templates/vue-tsx/ts/.vscode/launch.json +30 -0
  246. package/templates/vue-tsx/ts/.vscode/settings.json +20 -0
  247. package/templates/vue-tsx/ts/.vscode/tasks.json +36 -0
  248. package/templates/vue-tsx/ts/README.md +36 -0
  249. package/templates/vue-tsx/ts/_gitignore +6 -0
  250. package/templates/vue-tsx/ts/ceru.plugin.json +59 -0
  251. package/templates/vue-tsx/ts/package.json +22 -0
  252. package/templates/vue-tsx/ts/src/App.tsx +15 -0
  253. package/templates/vue-tsx/ts/src/env.d.ts +5 -0
  254. package/templates/vue-tsx/ts/src/index.ts +7 -0
  255. package/templates/vue-tsx/ts/src/view.ts +8 -0
  256. package/templates/vue-tsx/ts/tsconfig.json +17 -0
  257. package/templates/web-dist/js/.prettierrc.json +5 -0
  258. package/templates/web-dist/js/.vscode/ceru.code-snippets +27 -0
  259. package/templates/web-dist/js/.vscode/extensions.json +3 -0
  260. package/templates/web-dist/js/.vscode/launch.json +30 -0
  261. package/templates/web-dist/js/.vscode/settings.json +20 -0
  262. package/templates/web-dist/js/.vscode/tasks.json +36 -0
  263. package/templates/web-dist/js/README.md +38 -0
  264. package/templates/web-dist/js/_gitignore +6 -0
  265. package/templates/web-dist/js/ceru.plugin.json +57 -0
  266. package/templates/web-dist/js/package.json +21 -0
  267. package/templates/web-dist/js/src/index.js +7 -0
  268. package/templates/web-dist/js/tsconfig.json +16 -0
  269. package/templates/web-dist/js/web-dist/index.html +14 -0
  270. package/templates/web-dist/js/web-dist/main.js +4 -0
  271. package/templates/web-dist/js/web-dist/sample.png +0 -0
  272. package/templates/web-dist/js/web-dist/style.css +11 -0
  273. package/templates/web-dist/ts/.prettierrc.json +5 -0
  274. package/templates/web-dist/ts/.vscode/ceru.code-snippets +27 -0
  275. package/templates/web-dist/ts/.vscode/extensions.json +3 -0
  276. package/templates/web-dist/ts/.vscode/launch.json +30 -0
  277. package/templates/web-dist/ts/.vscode/settings.json +20 -0
  278. package/templates/web-dist/ts/.vscode/tasks.json +36 -0
  279. package/templates/web-dist/ts/README.md +38 -0
  280. package/templates/web-dist/ts/_gitignore +6 -0
  281. package/templates/web-dist/ts/ceru.plugin.json +57 -0
  282. package/templates/web-dist/ts/package.json +21 -0
  283. package/templates/web-dist/ts/src/index.ts +7 -0
  284. package/templates/web-dist/ts/tsconfig.json +16 -0
  285. package/templates/web-dist/ts/web-dist/index.html +14 -0
  286. package/templates/web-dist/ts/web-dist/main.js +4 -0
  287. package/templates/web-dist/ts/web-dist/sample.png +0 -0
  288. package/templates/web-dist/ts/web-dist/style.css +11 -0
  289. package/templates/web-surface/js/.prettierrc.json +5 -0
  290. package/templates/web-surface/js/.vscode/ceru.code-snippets +27 -0
  291. package/templates/web-surface/js/.vscode/extensions.json +3 -0
  292. package/templates/web-surface/js/.vscode/launch.json +30 -0
  293. package/templates/web-surface/js/.vscode/settings.json +20 -0
  294. package/templates/web-surface/js/.vscode/tasks.json +36 -0
  295. package/templates/web-surface/js/README.md +36 -0
  296. package/templates/web-surface/js/_gitignore +6 -0
  297. package/templates/web-surface/js/ceru.plugin.json +55 -0
  298. package/templates/web-surface/js/package.json +21 -0
  299. package/templates/web-surface/js/src/index.js +7 -0
  300. package/templates/web-surface/js/src/view.js +8 -0
  301. package/templates/web-surface/js/tsconfig.json +16 -0
  302. package/templates/web-surface/ts/.prettierrc.json +5 -0
  303. package/templates/web-surface/ts/.vscode/ceru.code-snippets +27 -0
  304. package/templates/web-surface/ts/.vscode/extensions.json +3 -0
  305. package/templates/web-surface/ts/.vscode/launch.json +30 -0
  306. package/templates/web-surface/ts/.vscode/settings.json +20 -0
  307. package/templates/web-surface/ts/.vscode/tasks.json +36 -0
  308. package/templates/web-surface/ts/README.md +36 -0
  309. package/templates/web-surface/ts/_gitignore +6 -0
  310. package/templates/web-surface/ts/ceru.plugin.json +55 -0
  311. package/templates/web-surface/ts/package.json +21 -0
  312. package/templates/web-surface/ts/src/index.ts +7 -0
  313. package/templates/web-surface/ts/src/view.ts +8 -0
  314. package/templates/web-surface/ts/tsconfig.json +16 -0
@@ -0,0 +1,446 @@
1
+ const $ = (id) => document.getElementById(id)
2
+ let state,
3
+ revision = 0,
4
+ sequence = 0,
5
+ stopped = false,
6
+ viewId = null,
7
+ currentInvocation
8
+ const frames = new Map(),
9
+ calls = new Map(),
10
+ registrations = new Map(),
11
+ surfaceStates = new Map()
12
+ const api = async (path, data) => {
13
+ const response = await fetch('/api/' + path, {
14
+ method: data === undefined ? 'GET' : 'POST',
15
+ headers: {
16
+ 'x-ceru-dev-token': window.CERU_DEV_TOKEN,
17
+ ...(data === undefined ? {} : { 'Content-Type': 'application/json' }),
18
+ },
19
+ body: data === undefined ? undefined : JSON.stringify(data),
20
+ })
21
+ const result = await response.json()
22
+ if (!response.ok) throw new Error(result.error || 'Host request failed')
23
+ return result
24
+ }
25
+ function log(level, message) {
26
+ const el = document.createElement('div')
27
+ el.className = 'log ' + level
28
+ const time = document.createElement('span')
29
+ time.className = 'time'
30
+ time.textContent = new Date().toLocaleTimeString() + ' '
31
+ el.append(
32
+ time,
33
+ document.createTextNode(
34
+ typeof message === 'string' ? message : JSON.stringify(message, null, 2),
35
+ ),
36
+ )
37
+ $('logs').prepend(el)
38
+ while ($('logs').children.length > 300) $('logs').lastChild.remove()
39
+ }
40
+ const iconUrl = (name) =>
41
+ 'data:image/svg+xml;base64,' +
42
+ btoa(unescape(encodeURIComponent(state.catalog.icons[name] || state.catalog.icons['music-note'])))
43
+ function post(frame, type, data) {
44
+ frame.element.contentWindow?.postMessage({ type, data, generation: frame.generation }, '*')
45
+ }
46
+ function removeFrames() {
47
+ for (const frame of frames.values()) frame.element.remove()
48
+ frames.clear()
49
+ registrations.clear()
50
+ for (const call of calls.values()) {
51
+ clearTimeout(call.timer)
52
+ call.reject(new Error('Plugin reloaded or stopped'))
53
+ }
54
+ calls.clear()
55
+ drawRegistrations()
56
+ }
57
+ function mount(moduleId, kind, container) {
58
+ const element = document.createElement('iframe')
59
+ element.setAttribute('sandbox', 'allow-scripts')
60
+ element.src = '/sandbox.html?module=' + encodeURIComponent(moduleId)
61
+ const frame = { element, moduleId, kind, generation: crypto.randomUUID(), active: false }
62
+ frames.set(moduleId, frame)
63
+ container.append(element)
64
+ return frame
65
+ }
66
+ function start() {
67
+ stopped = false
68
+ removeFrames()
69
+ $('status').textContent = '加载中'
70
+ if (state.manifest.modules.logic) mount(state.manifest.modules.logic.entry, 'logic', $('runners'))
71
+ else $('status').textContent = '无后台入口'
72
+ if (viewId) showView(viewId)
73
+ }
74
+ function drawRegistrations() {
75
+ $('registrations').replaceChildren()
76
+ $('provider').replaceChildren()
77
+ for (const [key, item] of registrations) {
78
+ const row = document.createElement('div')
79
+ row.className = 'item'
80
+ if (item.kind === 'provider') {
81
+ const meta = state.manifest.contributes?.providers?.find((p) => p.id === item.id)
82
+ const img = document.createElement('img')
83
+ img.width = 22
84
+ img.height = 22
85
+ img.src = iconUrl(meta?.icon?.name)
86
+ row.append(img)
87
+ row.append(document.createTextNode(meta?.name || item.id))
88
+ const option = document.createElement('option')
89
+ option.value = item.id
90
+ option.textContent = meta?.name || item.id
91
+ $('provider').append(option)
92
+ } else {
93
+ const button = document.createElement('button')
94
+ button.textContent = item.id
95
+ button.onclick = () =>
96
+ invoke(item.frame, 'action', item.id, '', [{}])
97
+ .then(showResult)
98
+ .catch((e) => log('error', e.message))
99
+ row.append(button)
100
+ }
101
+ $('registrations').append(row)
102
+ }
103
+ if (!registrations.size) $('registrations').textContent = '尚未注册'
104
+ }
105
+ function invoke(frame, kind, target, method, args) {
106
+ const id = 'call-' + ++sequence
107
+ currentInvocation = { frame, id }
108
+ log('info', { call: kind === 'action' ? target : target + '.' + method })
109
+ return new Promise((resolve, reject) => {
110
+ calls.set(id, {
111
+ frame,
112
+ resolve,
113
+ reject,
114
+ timer: setTimeout(() => {
115
+ calls.delete(id)
116
+ post(frame, 'cancel', { id })
117
+ reject(new Error('Invocation timed out'))
118
+ }, 20000),
119
+ })
120
+ post(frame, 'invoke', { id, kind, target, method, args })
121
+ })
122
+ }
123
+ function showResult(value) {
124
+ $('result-detail').textContent = JSON.stringify(value ?? null, null, 2)
125
+ document.getElementById('media-preview')?.remove()
126
+ if (value?.ok && value.media?.kind === 'media') {
127
+ const audio = document.createElement('audio')
128
+ audio.id = 'media-preview'
129
+ audio.controls = true
130
+ audio.src = '/media/' + encodeURIComponent(value.media.id)
131
+ $('result-detail').after(audio)
132
+ }
133
+ return value
134
+ }
135
+ function runAction(action, input) {
136
+ const item = registrations.get('action:' + action)
137
+ if (!item) return Promise.reject(new Error('Action is not registered: ' + action))
138
+ return invoke(item.frame, 'action', action, '', [input])
139
+ }
140
+ function renderSchema(node, values, container) {
141
+ if (!node || typeof node !== 'object') return
142
+ if (['section', 'stack', 'form', 'grid'].includes(node.type)) {
143
+ const group = document.createElement('div')
144
+ if (node.title) {
145
+ const title = document.createElement('h3')
146
+ title.textContent = node.title
147
+ group.append(title)
148
+ }
149
+ for (const child of (node.children || []).slice(0, 100)) renderSchema(child, values, group)
150
+ container.append(group)
151
+ return
152
+ }
153
+ if (node.type === 'button') {
154
+ const button = document.createElement('button')
155
+ button.textContent = node.label || '执行'
156
+ button.onclick = () =>
157
+ runAction(node.action, values)
158
+ .then(showResult)
159
+ .catch((e) => log('error', e.message))
160
+ container.append(button)
161
+ return
162
+ }
163
+ if (['text-input', 'input', 'number', 'toggle', 'host-credential'].includes(node.type)) {
164
+ const label = document.createElement('label')
165
+ label.textContent = node.label || node.bind || ''
166
+ const input = document.createElement('input')
167
+ input.type = node.type === 'number' ? 'number' : node.type === 'toggle' ? 'checkbox' : 'text'
168
+ if (node.type === 'host-credential') {
169
+ input.disabled = true
170
+ input.placeholder = '凭据保险箱由生产 Host 提供'
171
+ } else input.value = values[node.bind] ?? ''
172
+ input.oninput = () => {
173
+ values[node.bind] = input.type === 'checkbox' ? input.checked : input.value
174
+ }
175
+ label.append(input)
176
+ container.append(label)
177
+ return
178
+ }
179
+ const text = document.createElement('p')
180
+ text.textContent =
181
+ node.text || node.label || (node.bind ? String(values[node.bind] ?? '') : '[' + node.type + ']')
182
+ container.append(text)
183
+ }
184
+ function showView(id) {
185
+ viewId = id
186
+ for (const [key, frame] of frames)
187
+ if (frame.kind !== 'logic') {
188
+ frame.element.remove()
189
+ frames.delete(key)
190
+ }
191
+ $('preview').replaceChildren()
192
+ const view = state.manifest.modules.surfaces?.find((v) => v.id === id)
193
+ if (view?.kind === 'web') mount(view.entry, 'web', $('preview'))
194
+ else if (view?.kind === 'schema')
195
+ renderSchema(
196
+ state.resources[view.entry]?.value?.root,
197
+ { ...(surfaceStates.get(id) || {}) },
198
+ $('preview'),
199
+ )
200
+ else {
201
+ const adapter = state.manifest.contributes?.guestAdapters?.find((v) => v.id === id)
202
+ if (adapter) mount(adapter.bootstrap, 'guest', $('preview'))
203
+ }
204
+ }
205
+ function drawStatic() {
206
+ $('plugin-name').textContent = state.manifest.name
207
+ $('plugin-meta').textContent =
208
+ state.manifest.id + ' · v' + state.manifest.version + ' · revision ' + revision
209
+ $('views').replaceChildren()
210
+ for (const view of [
211
+ ...(state.manifest.modules.surfaces || []),
212
+ ...(state.manifest.contributes?.guestAdapters || []),
213
+ ]) {
214
+ const button = document.createElement('button')
215
+ button.textContent = view.id + (view.kind ? ' · ' + view.kind : ' · guest bootstrap')
216
+ button.onclick = () => showView(view.id)
217
+ $('views').append(button)
218
+ }
219
+ $('permissions').replaceChildren()
220
+ for (const permission of state.manifest.permissions || []) {
221
+ const row = document.createElement('div')
222
+ row.className = 'permission'
223
+ const title = document.createElement('strong')
224
+ title.textContent = permission.key
225
+ const reason = document.createElement('small')
226
+ reason.textContent = permission.reason
227
+ const input = document.createElement('input')
228
+ input.placeholder = '授权的确切 origin'
229
+ input.value =
230
+ state.grants[permission.key]?.origin ||
231
+ (typeof permission.scope?.origin === 'string' ? permission.scope.origin : '')
232
+ const button = document.createElement('button')
233
+ button.textContent = state.grants[permission.key] ? '撤销' : '授予'
234
+ button.onclick = async () => {
235
+ try {
236
+ await api('grant', {
237
+ key: permission.key,
238
+ allow: !state.grants[permission.key],
239
+ origin: input.value,
240
+ })
241
+ state = await api('state')
242
+ drawStatic()
243
+ } catch (e) {
244
+ log('error', e.message)
245
+ }
246
+ }
247
+ row.append(title, reason)
248
+ if (permission.name.startsWith('network.')) row.append(input)
249
+ row.append(button)
250
+ $('permissions').append(row)
251
+ }
252
+ if (!(state.manifest.permissions || []).length)
253
+ $('permissions').textContent = '此插件未声明额外权限'
254
+ $('icons').replaceChildren()
255
+ for (const name of Object.keys(state.catalog.icons)) {
256
+ const card = document.createElement('div')
257
+ card.className = 'icon-card'
258
+ const img = document.createElement('img')
259
+ img.src = iconUrl(name)
260
+ card.append(img, document.createTextNode(name))
261
+ $('icons').append(card)
262
+ }
263
+ $('static-assets').replaceChildren()
264
+ for (const [name, url] of Object.entries(state.catalog.assets)) {
265
+ const card = document.createElement('div'),
266
+ image = document.createElement('img'),
267
+ label = document.createElement('div')
268
+ image.src = url
269
+ label.textContent = name
270
+ label.className = 'hint'
271
+ card.append(image, label)
272
+ $('static-assets').append(card)
273
+ }
274
+ }
275
+ window.addEventListener('message', async (event) => {
276
+ const frame = [...frames.values()].find((f) => f.element.contentWindow === event.source)
277
+ if (!frame || !event.data?.type) return
278
+ const message = event.data
279
+ if (message.type === 'ready') {
280
+ post(frame, 'init', {
281
+ generation: frame.generation,
282
+ kind: frame.kind,
283
+ manifest: state.manifest,
284
+ catalog: state.catalog,
285
+ resources: state.resources,
286
+ })
287
+ return
288
+ }
289
+ if (message.generation !== frame.generation) return
290
+ const data = message.data
291
+ if (message.type === 'active') {
292
+ frame.active = true
293
+ $('status').textContent = '运行中'
294
+ log('info', frame.moduleId + ' activated')
295
+ }
296
+ if (message.type === 'failed') {
297
+ $('status').textContent = '运行失败'
298
+ log('error', data.message)
299
+ }
300
+ if (message.type === 'log') log(data.level, data.values)
301
+ if (message.type === 'notify') log(data.level, data.message)
302
+ if (message.type === 'open-view') showView(data.surfaceId)
303
+ if (message.type === 'register') {
304
+ if (frame.kind !== 'logic') return
305
+ const valid =
306
+ data.kind === 'provider'
307
+ ? state.manifest.contributes?.providers?.some((p) => p.id === data.id)
308
+ : state.manifest.contributes?.commands?.some((p) => p.action === data.id)
309
+ if (valid) {
310
+ registrations.set(data.kind + ':' + data.id, { ...data, frame })
311
+ drawRegistrations()
312
+ }
313
+ }
314
+ if (message.type === 'unregister') {
315
+ registrations.delete(data.kind + ':' + data.id)
316
+ drawRegistrations()
317
+ }
318
+ if (message.type === 'state' && frame.kind === 'logic') {
319
+ surfaceStates.set(data.surfaceId, data.state)
320
+ for (const view of frames.values()) if (view.kind === 'web') post(view, 'state', data.state)
321
+ if (
322
+ viewId === data.surfaceId &&
323
+ state.manifest.modules.surfaces?.find((v) => v.id === viewId)?.kind === 'schema'
324
+ )
325
+ showView(viewId)
326
+ }
327
+ if (message.type === 'invoke-result') {
328
+ const call = calls.get(data.id)
329
+ if (call?.frame === frame) {
330
+ clearTimeout(call.timer)
331
+ calls.delete(data.id)
332
+ data.error ? call.reject(new Error(data.error)) : call.resolve(data.value)
333
+ }
334
+ }
335
+ if (message.type === 'rpc') {
336
+ try {
337
+ let value
338
+ if (data.method === 'surface.invoke' && frame.kind === 'web')
339
+ value = await runAction(data.data.action, data.data.input)
340
+ else if (frame.kind !== 'logic')
341
+ throw new Error('This Surface/Guest cannot call a logic Host API directly')
342
+ else if (data.method === 'permissions.request') {
343
+ const declaration = state.manifest.permissions?.find((p) => p.key === data.data.key)
344
+ if (!declaration) value = { status: 'undeclared' }
345
+ else {
346
+ const allowed = confirm(
347
+ '开发权限申请:' +
348
+ declaration.key +
349
+ '\n' +
350
+ declaration.reason +
351
+ '\n若需要动态 origin,请先在权限面板中指定。',
352
+ )
353
+ if (allowed) {
354
+ const scope = data.data.scope?.origin
355
+ await api('grant', {
356
+ key: declaration.key,
357
+ allow: true,
358
+ origin: typeof scope === 'string' ? scope : undefined,
359
+ })
360
+ state = await api('state')
361
+ drawStatic()
362
+ }
363
+ value = { status: allowed ? 'granted' : 'denied' }
364
+ }
365
+ } else value = (await api('rpc', { method: data.method, data: data.data })).value
366
+ post(frame, 'rpc-result', { id: data.id, value })
367
+ } catch (e) {
368
+ post(frame, 'rpc-result', { id: data.id, error: e.message })
369
+ }
370
+ }
371
+ })
372
+ $('search').onclick = async () => {
373
+ try {
374
+ const id = $('provider').value,
375
+ item = registrations.get('provider:' + id)
376
+ if (!item) throw new Error('尚未注册 Provider')
377
+ const result = await invoke(item.frame, 'provider', id, 'search', [
378
+ { query: $('query').value, kinds: ['track'], filters: {}, limit: 20 },
379
+ ])
380
+ showResult(result)
381
+ $('results').replaceChildren()
382
+ for (const track of result.items || []) {
383
+ const row = document.createElement('div')
384
+ row.className = 'track'
385
+ const title = document.createElement('div')
386
+ title.textContent = track.title
387
+ const button = document.createElement('button')
388
+ button.textContent = '调用解析'
389
+ button.onclick = () =>
390
+ invoke(item.frame, 'provider', id, 'resolve', [track.ref, undefined])
391
+ .then(showResult)
392
+ .catch((e) => log('error', e.message))
393
+ row.append(title, button)
394
+ $('results').append(row)
395
+ }
396
+ if (!(result.items || []).length) $('results').textContent = '没有结果'
397
+ } catch (e) {
398
+ log('error', e.message)
399
+ }
400
+ }
401
+ $('cancel').onclick = () => {
402
+ if (currentInvocation) post(currentInvocation.frame, 'cancel', { id: currentInvocation.id })
403
+ }
404
+ $('restart').onclick = start
405
+ $('stop').onclick = () => {
406
+ stopped = true
407
+ removeFrames()
408
+ $('status').textContent = '已停止'
409
+ }
410
+ $('clear-log').onclick = () => $('logs').replaceChildren()
411
+ $('apply-config').onclick = async () => {
412
+ try {
413
+ await api('config', JSON.parse($('config').value))
414
+ start()
415
+ } catch (e) {
416
+ log('error', e.message)
417
+ }
418
+ }
419
+ async function poll() {
420
+ try {
421
+ const next = await api('state')
422
+ $('build-error').classList.toggle('hidden', !next.error)
423
+ $('build-error').textContent = next.error || ''
424
+ if (next.revision !== revision) {
425
+ state = next
426
+ revision = next.revision
427
+ drawStatic()
428
+ if (!stopped) start()
429
+ }
430
+ } catch (e) {
431
+ $('status').textContent = 'Host 已断开'
432
+ log('error', e.message)
433
+ }
434
+ }
435
+ window.__ceruDev = {
436
+ get state() {
437
+ return state
438
+ },
439
+ get registrations() {
440
+ return [...registrations.keys()]
441
+ },
442
+ runAction,
443
+ restart: start,
444
+ }
445
+ poll()
446
+ setInterval(poll, 750)