@panomc/theme-core 1.0.0-dev.59 → 1.0.0-dev.61

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 (308) hide show
  1. package/assets/css/pano-fallback-legacy.css +12381 -0
  2. package/assets/css/pano-fallback.css +3222 -0
  3. package/bin/__tests__/check/fixtures/plugin-contracts/market/controllers.json +4 -0
  4. package/bin/__tests__/check/fixtures/plugin-contracts/market/views.json +67 -0
  5. package/bin/__tests__/check/fixtures/routes.json +5 -0
  6. package/bin/__tests__/check/helpers.js +55 -0
  7. package/bin/__tests__/check/overrides.test.js +287 -0
  8. package/bin/__tests__/check/theme.test.js +332 -0
  9. package/bin/__tests__/cli/accept-and-list.test.js +159 -0
  10. package/bin/__tests__/cli/config-edit.test.js +213 -0
  11. package/bin/__tests__/cli/contracts-pull.test.js +187 -0
  12. package/bin/__tests__/cli/core-meta.test.js +305 -0
  13. package/bin/__tests__/cli/eject-view.test.js +356 -0
  14. package/bin/__tests__/cli/helpers.js +258 -0
  15. package/bin/__tests__/scaffold/scaffold.test.js +84 -0
  16. package/bin/bundle-internal-libs.js +14 -2
  17. package/bin/check.js +1150 -213
  18. package/bin/contracts.js +1643 -0
  19. package/bin/generate-contract.js +378 -0
  20. package/bin/generate-licenses.js +14 -2
  21. package/bin/generate-runtime-shims.js +27 -9
  22. package/bin/new.js +101 -2
  23. package/bin/package-zip.js +16 -2
  24. package/bin/sync.js +360 -7
  25. package/bin/theme-core.js +65 -107
  26. package/lang/en-US.json +1 -0
  27. package/lang/ru.json +1 -0
  28. package/lang/tr.json +1 -0
  29. package/package.json +7 -3
  30. package/skin-contract.json +864 -19
  31. package/src/kit/__tests__/hooks-server-fetch.test.js +56 -0
  32. package/src/kit/__tests__/route-map.test.js +154 -0
  33. package/src/kit/__tests__/theme-meta.test.js +249 -0
  34. package/src/kit/hooks-client.js +3 -0
  35. package/src/kit/hooks-server.js +91 -7
  36. package/src/kit/route-map.js +300 -0
  37. package/src/kit/specifiers.js +3 -0
  38. package/src/kit/svelte-config.js +3 -0
  39. package/src/kit/theme-meta.js +405 -0
  40. package/src/kit/vite-config.js +156 -12
  41. package/src/lib/NotificationManager.js +18 -0
  42. package/src/lib/PluginAPI.js +313 -9
  43. package/src/lib/Store.js +18 -5
  44. package/src/lib/__tests__/avatarVersion.test.js +25 -0
  45. package/src/lib/__tests__/check.test.js +201 -0
  46. package/src/lib/__tests__/controllerHost.test.js +363 -0
  47. package/src/lib/__tests__/cx03.test.js +245 -0
  48. package/src/lib/__tests__/engineBoot.test.js +151 -0
  49. package/src/lib/__tests__/engineParts.helper.js +284 -0
  50. package/src/lib/__tests__/engineParts.test.js +179 -0
  51. package/src/lib/__tests__/engineSidebars.helper.js +353 -0
  52. package/src/lib/__tests__/engineSidebars.test.js +273 -0
  53. package/src/lib/__tests__/engineStyles.test.js +227 -0
  54. package/src/lib/__tests__/engineWiring.test.js +1000 -0
  55. package/src/lib/__tests__/fallbackStyles.test.js +1254 -0
  56. package/src/lib/__tests__/fixtures/Probe.svelte +5 -0
  57. package/src/lib/__tests__/fixtures/engine-parts.ssr.json +29 -0
  58. package/src/lib/__tests__/fixtures/engine-sidebars.ssr.json +16 -0
  59. package/src/lib/__tests__/home.test.js +563 -0
  60. package/src/lib/__tests__/injectedScope.test.js +490 -0
  61. package/src/lib/__tests__/languageConcurrency.test.js +55 -0
  62. package/src/lib/__tests__/languageLayers.test.js +286 -0
  63. package/src/lib/__tests__/pageHead.test.js +167 -0
  64. package/src/lib/__tests__/pageMeta.util.test.js +216 -0
  65. package/src/lib/__tests__/pageTitle.util.test.js +96 -0
  66. package/src/lib/__tests__/pluginApiViews.test.js +529 -0
  67. package/src/lib/__tests__/plugins.test.js +53 -0
  68. package/src/lib/__tests__/profileNav.util.test.js +145 -0
  69. package/src/lib/__tests__/registryReady.test.js +26 -0
  70. package/src/lib/__tests__/returnTo.util.test.js +294 -0
  71. package/src/lib/__tests__/routeConfig.test.js +361 -0
  72. package/src/lib/__tests__/sidebarSpec.util.test.js +152 -0
  73. package/src/lib/__tests__/skinContract.test.js +558 -0
  74. package/src/lib/__tests__/svelteSsr.helper.js +68 -0
  75. package/src/lib/__tests__/tc03.test.js +427 -0
  76. package/src/lib/__tests__/tc11.test.js +184 -0
  77. package/src/lib/__tests__/translationLayers.test.js +161 -0
  78. package/src/lib/avatarVersion.js +39 -0
  79. package/src/lib/components/Breadcrumb.svelte +9 -40
  80. package/src/lib/components/Date.svelte +1 -1
  81. package/src/lib/components/DefaultToast.svelte +13 -3
  82. package/src/lib/components/FallbackLinks.svelte +28 -0
  83. package/src/lib/components/FallbackScope.svelte +24 -0
  84. package/src/lib/components/Footer.svelte +8 -8
  85. package/src/lib/components/Header.svelte +3 -3
  86. package/src/lib/components/Hook.svelte +12 -1
  87. package/src/lib/components/LoginFormBody.svelte +5 -5
  88. package/src/lib/components/Main.svelte +22 -47
  89. package/src/lib/components/Navbar.svelte +30 -29
  90. package/src/lib/components/NoContent.svelte +2 -2
  91. package/src/lib/components/NotificationContainer.svelte +31 -82
  92. package/src/lib/components/OnlineAdmins.svelte +3 -2
  93. package/src/lib/components/PageActions.svelte +1 -1
  94. package/src/lib/components/PageHead.svelte +50 -0
  95. package/src/lib/components/PageTitle.svelte +2 -2
  96. package/src/lib/components/Pagination.svelte +6 -6
  97. package/src/lib/components/PlayerHead.svelte +4 -4
  98. package/src/lib/components/PluginBlock.svelte +132 -0
  99. package/src/lib/components/PluginSlot.svelte +40 -0
  100. package/src/lib/components/Post.svelte +13 -230
  101. package/src/lib/components/Posts.svelte +7 -14
  102. package/src/lib/components/RegisterForm.svelte +12 -11
  103. package/src/lib/components/Sidebar.svelte +9 -7
  104. package/src/lib/components/TicketRow.svelte +8 -48
  105. package/src/lib/components/TicketStatus.svelte +10 -16
  106. package/src/lib/components/Tickets.svelte +8 -30
  107. package/src/lib/components/Toast.svelte +16 -14
  108. package/src/lib/components/ToastContainer.svelte +16 -9
  109. package/src/lib/components/ViewComponent.svelte +13 -1
  110. package/src/lib/components/modals/CloseTicketConfirmModal.svelte +15 -46
  111. package/src/lib/components/modals/ConfirmRemoveAllNotificationsModal.svelte +17 -39
  112. package/src/lib/components/modals/LogoutSessionConfirmModal.svelte +12 -30
  113. package/src/lib/components/sidebars/HomeSidebar.svelte +33 -284
  114. package/src/lib/components/sidebars/PlayerDetailSidebar.svelte +14 -63
  115. package/src/lib/components/sidebars/PluginSidebar.svelte +21 -0
  116. package/src/lib/components/sidebars/ProfileNavCard.svelte +24 -0
  117. package/src/lib/components/sidebars/ProfileSidebar.svelte +44 -84
  118. package/src/lib/components/sidebars/SupportSidebar.svelte +19 -22
  119. package/src/lib/components/sidebars/TicketCreateAndDetailSidebar.svelte +23 -35
  120. package/src/lib/components/sidebars/profileNav.util.js +105 -0
  121. package/src/lib/components/sidebars/sidebarSpec.util.js +92 -0
  122. package/src/lib/controllerHost.js +261 -0
  123. package/src/lib/fallbackStyles.js +317 -0
  124. package/src/lib/home.js +202 -0
  125. package/src/lib/language.util.js +70 -37
  126. package/src/lib/layouts/AppLayout.svelte +16 -8
  127. package/src/lib/layouts/AuthLayout.svelte +6 -4
  128. package/src/lib/layouts/MainLayout.svelte +17 -12
  129. package/src/lib/layouts/ProfileLayout.svelte +6 -4
  130. package/src/lib/layouts/ThemeSettingsLayout.svelte +6 -4
  131. package/src/lib/layouts/TicketsLayout.svelte +6 -5
  132. package/src/lib/pageMeta.util.js +212 -0
  133. package/src/lib/pageShape.js +33 -0
  134. package/src/lib/pageTitle.util.js +32 -0
  135. package/src/lib/pages/ActivateEmailPage.svelte +2 -7
  136. package/src/lib/pages/ConfirmNewEmailPage.svelte +2 -7
  137. package/src/lib/pages/ErrorPage.svelte +5 -2
  138. package/src/lib/pages/HomePage.svelte +31 -8
  139. package/src/lib/pages/LoginPage.svelte +38 -41
  140. package/src/lib/pages/NotificationsPage.svelte +2 -7
  141. package/src/lib/pages/PlayerDetailPage.svelte +2 -7
  142. package/src/lib/pages/PostDetailPage.svelte +2 -7
  143. package/src/lib/pages/PreviewPostPage.svelte +2 -7
  144. package/src/lib/pages/RegisterPage.svelte +10 -13
  145. package/src/lib/pages/RenewPasswordPage.svelte +2 -7
  146. package/src/lib/pages/ResetPasswordPage.svelte +2 -7
  147. package/src/lib/pages/RulesPage.svelte +3 -8
  148. package/src/lib/pages/SupportPage.svelte +2 -7
  149. package/src/lib/pages/ThemeSettingsPage.svelte +2 -7
  150. package/src/lib/pages/profile/ProfilePage.svelte +2 -7
  151. package/src/lib/pages/profile/SettingsPage.svelte +2 -7
  152. package/src/lib/pages/profile/TicketsPage.svelte +2 -7
  153. package/src/lib/pages/ticket/CreateTicketPage.svelte +2 -7
  154. package/src/lib/pages/ticket/TicketDetailPage.svelte +2 -7
  155. package/src/lib/plugins.js +61 -0
  156. package/src/lib/returnTo.util.js +116 -0
  157. package/src/lib/services/auth.js +11 -11
  158. package/src/lib/services/posts.js +21 -10
  159. package/src/lib/services/profile.js +5 -5
  160. package/src/lib/services/theme-setting.js +1 -1
  161. package/src/lib/services/tickets.js +36 -15
  162. package/src/lib/siteRealtime.js +55 -8
  163. package/src/lib/themePageLoad.js +38 -0
  164. package/src/lib/translationLayers.js +144 -0
  165. package/src/lib/ui-logics/layout-logics/AppLayoutLogics.js +160 -6
  166. package/src/lib/ui-logics/layout-logics/AuthLayoutLogics.js +3 -2
  167. package/src/lib/ui-logics/layout-logics/ProfileLayoutLogics.js +3 -2
  168. package/src/lib/ui-logics/layout-logics/TicketsLayoutLogics.js +3 -2
  169. package/src/lib/ui-logics/page-logics/ActiveEmailPageLogics.js +4 -4
  170. package/src/lib/ui-logics/page-logics/ConfirmNewEmailPageLogics.js +4 -4
  171. package/src/lib/ui-logics/page-logics/CreateTicketPageLogics.js +5 -5
  172. package/src/lib/ui-logics/page-logics/HomePageLogics.js +5 -3
  173. package/src/lib/ui-logics/page-logics/NotificationsPageLogics.js +40 -16
  174. package/src/lib/ui-logics/page-logics/PlayerDetailPageLogics.js +3 -3
  175. package/src/lib/ui-logics/page-logics/PostDetailPageLogics.js +3 -3
  176. package/src/lib/ui-logics/page-logics/PreviewPostPageLogics.js +1 -1
  177. package/src/lib/ui-logics/page-logics/RenewPasswordPageLogics.js +4 -4
  178. package/src/lib/ui-logics/page-logics/ResetPasswordPageLogics.js +6 -6
  179. package/src/lib/ui-logics/page-logics/SettingsPageLogics.js +8 -8
  180. package/src/lib/ui-logics/page-logics/TicketDetailPageLogics.js +3 -3
  181. package/src/lib/ui-logics/page-logics/TicketsPageLogics.js +5 -3
  182. package/src/lib/views/ActivateEmailView.samples.js +22 -0
  183. package/src/lib/views/ActivateEmailView.svelte +4 -4
  184. package/src/lib/views/AppLayoutView.samples.js +17 -0
  185. package/src/lib/views/AppLayoutView.svelte +1 -1
  186. package/src/lib/views/AuthLayoutView.samples.js +4 -0
  187. package/src/lib/views/AuthLayoutView.svelte +1 -1
  188. package/src/lib/views/Breadcrumb.samples.js +15 -0
  189. package/src/lib/views/CloseTicketConfirmModal.samples.js +12 -0
  190. package/src/lib/views/ConfirmNewEmailView.samples.js +22 -0
  191. package/src/lib/views/ConfirmNewEmailView.svelte +4 -4
  192. package/src/lib/views/ConfirmRemoveAllNotificationsModal.samples.js +9 -0
  193. package/src/lib/views/CreateTicketView.samples.js +21 -0
  194. package/src/lib/views/CreateTicketView.svelte +5 -5
  195. package/src/lib/views/Date.samples.js +9 -0
  196. package/src/lib/views/DefaultToast.samples.js +7 -0
  197. package/src/lib/views/ErrorView.samples.js +8 -0
  198. package/src/lib/views/ErrorView.svelte +4 -2
  199. package/src/lib/views/Footer.samples.js +8 -0
  200. package/src/lib/views/Header.samples.js +8 -0
  201. package/src/lib/views/HomeSidebar.samples.js +59 -0
  202. package/src/lib/views/HomeView.samples.js +34 -0
  203. package/src/lib/views/HomeView.svelte +14 -6
  204. package/src/lib/views/LoginFormBody.samples.js +8 -0
  205. package/src/lib/views/LoginView.samples.js +50 -0
  206. package/src/lib/views/LoginView.svelte +24 -22
  207. package/src/lib/views/LogoutSessionConfirmModal.samples.js +8 -0
  208. package/src/lib/views/Main.samples.js +23 -0
  209. package/src/lib/views/MainLayoutView.svelte +3 -4
  210. package/src/lib/views/Navbar.samples.js +8 -0
  211. package/src/lib/views/NoContent.samples.js +7 -0
  212. package/src/lib/views/NotificationContainer.samples.js +25 -0
  213. package/src/lib/views/NotificationsView.samples.js +36 -0
  214. package/src/lib/views/NotificationsView.svelte +21 -12
  215. package/src/lib/views/PageActions.samples.js +7 -0
  216. package/src/lib/views/PageTitle.samples.js +8 -0
  217. package/src/lib/views/Pagination.samples.js +7 -0
  218. package/src/lib/views/PlayerDetailSidebar.samples.js +35 -0
  219. package/src/lib/views/PlayerDetailView.samples.js +8 -0
  220. package/src/lib/views/PlayerDetailView.svelte +3 -3
  221. package/src/lib/views/PlayerHead.samples.js +8 -0
  222. package/src/lib/views/Post.samples.js +10 -0
  223. package/src/lib/views/PostDetailView.samples.js +28 -0
  224. package/src/lib/views/PostDetailView.svelte +24 -16
  225. package/src/lib/views/Posts.samples.js +9 -0
  226. package/src/lib/views/PreviewPostView.samples.js +20 -0
  227. package/src/lib/views/PreviewPostView.svelte +10 -10
  228. package/src/lib/views/ProfileLayoutView.samples.js +4 -0
  229. package/src/lib/views/ProfileSidebar.samples.js +33 -0
  230. package/src/lib/views/ProfileView.samples.js +29 -0
  231. package/src/lib/views/ProfileView.svelte +2 -2
  232. package/src/lib/views/RegisterForm.samples.js +7 -0
  233. package/src/lib/views/RegisterView.samples.js +28 -0
  234. package/src/lib/views/RegisterView.svelte +5 -3
  235. package/src/lib/views/RenewPasswordView.samples.js +24 -0
  236. package/src/lib/views/RenewPasswordView.svelte +6 -6
  237. package/src/lib/views/ResetPasswordView.samples.js +24 -0
  238. package/src/lib/views/ResetPasswordView.svelte +3 -3
  239. package/src/lib/views/RulesView.samples.js +14 -0
  240. package/src/lib/views/RulesView.svelte +3 -3
  241. package/src/lib/views/SettingsView.svelte +48 -17
  242. package/src/lib/views/Sidebar.samples.js +7 -0
  243. package/src/lib/views/SupportSidebar.samples.js +24 -0
  244. package/src/lib/views/SupportView.samples.js +16 -0
  245. package/src/lib/views/SupportView.svelte +8 -6
  246. package/src/lib/views/ThemeSettingsLayoutView.samples.js +9 -0
  247. package/src/lib/views/ThemeSettingsLayoutView.svelte +1 -1
  248. package/src/lib/views/ThemeSettingsView.svelte +89 -89
  249. package/src/lib/views/TicketCreateAndDetailSidebar.samples.js +33 -0
  250. package/src/lib/views/TicketDetailView.samples.js +34 -0
  251. package/src/lib/views/TicketDetailView.svelte +17 -15
  252. package/src/lib/views/TicketRow.samples.js +10 -0
  253. package/src/lib/views/TicketStatus.samples.js +8 -0
  254. package/src/lib/views/Tickets.samples.js +9 -0
  255. package/src/lib/views/TicketsLayoutView.samples.js +4 -0
  256. package/src/lib/views/TicketsView.samples.js +23 -0
  257. package/src/lib/views/TicketsView.svelte +11 -9
  258. package/src/lib/views/Toast.samples.js +9 -0
  259. package/src/lib/views/ToastContainer.samples.js +8 -0
  260. package/src/lib/views/parts/Breadcrumb.svelte +53 -0
  261. package/src/lib/views/parts/CloseTicketConfirmModal.svelte +63 -0
  262. package/src/lib/views/parts/ConfirmRemoveAllNotificationsModal.svelte +55 -0
  263. package/src/lib/views/parts/DefaultToast.svelte +22 -0
  264. package/src/lib/views/parts/HomeSidebar.svelte +334 -0
  265. package/src/lib/views/parts/LogoutSessionConfirmModal.svelte +46 -0
  266. package/src/lib/views/parts/Main.svelte +71 -0
  267. package/src/lib/views/parts/NotificationContainer.svelte +109 -0
  268. package/src/lib/views/parts/PlayerDetailSidebar.svelte +81 -0
  269. package/src/lib/views/parts/Post.svelte +255 -0
  270. package/src/lib/views/parts/Posts.svelte +25 -0
  271. package/src/lib/views/parts/ProfileSidebar.svelte +115 -0
  272. package/src/lib/views/parts/Sidebar.svelte +19 -0
  273. package/src/lib/views/parts/SupportSidebar.svelte +34 -0
  274. package/src/lib/views/parts/TicketCreateAndDetailSidebar.svelte +51 -0
  275. package/src/lib/views/parts/TicketRow.svelte +63 -0
  276. package/src/lib/views/parts/TicketStatus.svelte +24 -0
  277. package/src/lib/views/parts/Tickets.svelte +45 -0
  278. package/src/lib/views/parts/Toast.svelte +27 -0
  279. package/src/lib/views/parts/ToastContainer.svelte +17 -0
  280. package/src/lib/views/parts/engine-views.generated.js +63 -0
  281. package/src/lib/views/parts/publicHref.js +18 -0
  282. package/src/lib/views/parts/ticketStatuses.js +7 -0
  283. package/src/plugin-engine/__tests__/engine.test.js +161 -0
  284. package/src/plugin-engine/engine.js +80 -12
  285. package/src/registry/__tests__/registry.test.js +510 -0
  286. package/src/registry/__tests__/view.test.js +52 -0
  287. package/src/registry/index.js +700 -51
  288. package/src/registry/routes.js +62 -0
  289. package/src/registry/view.js +69 -0
  290. package/src/routes/RootLayout.svelte +24 -1
  291. package/src/routes/catalogue/List.svelte +163 -0
  292. package/src/routes/catalogue/Stage.svelte +355 -0
  293. package/src/routes/catalogue/__tests__/browserSafe.test.js +13 -0
  294. package/src/routes/catalogue/__tests__/catalogue.test.js +539 -0
  295. package/src/routes/catalogue/catalogue.js +129 -0
  296. package/src/routes/catalogue/deps.js +147 -0
  297. package/src/routes/catalogue/engineSamples.js +63 -0
  298. package/src/routes/catalogue/fixtures.js +77 -0
  299. package/src/routes/catalogue/list.js +37 -0
  300. package/src/routes/catalogue/model.js +286 -0
  301. package/src/routes/catalogue/stage.js +69 -0
  302. package/src/routes/catalogue/views-json.js +24 -0
  303. package/src/routes/plugin-ui/Layout.svelte +29 -4
  304. package/src/routes/plugin-ui/Page.svelte +86 -31
  305. package/src/routes/plugin-ui/load.js +173 -0
  306. package/src/routes/plugins-client-file.js +8 -3
  307. package/src/routes/plugins-ui-file.js +60 -0
  308. package/src/routes/root-layout-load.js +59 -2
@@ -0,0 +1,261 @@
1
+ // Theme host for public controllers (doc 02 section 1, "Theme" host).
2
+ //
3
+ // A controller reaches the world only through a ControllerHost. This file builds the one the theme
4
+ // engine hands to `pano.controllers` (`controllers.setHostFactory(createThemeHost)`):
5
+ //
6
+ // createThemeHost({ event? }) browser: one host for the whole page; server: one host per call
7
+ // bindControllerSession(session) browser only, once per layout: feeds `host.session()` / `host.onSession()`
8
+ //
9
+ // Nothing request-bound is ever stored on the server. A server host reads the session of the request it
10
+ // was created for (`event.locals`, the same data the root layout turns into its `session`), through
11
+ // `event`, and nothing else. `bindControllerSession` does nothing outside the browser, so two SSR
12
+ // requests that interleave can never see each other's user.
13
+
14
+ import { get } from "svelte/store";
15
+ import { _, locale } from "svelte-i18n";
16
+
17
+ import { browser } from "$app/environment";
18
+
19
+ import ApiUtil from "$pano/lib/api.util.js";
20
+ import { sanitizeReturnTo } from "./returnTo.util.js";
21
+ import { panoApi, routedGoto } from "$pano/lib/PluginAPI.js";
22
+ import { route } from "../registry/index.js";
23
+
24
+ /**
25
+ * @typedef {{ user: object | null, csrfToken: string | null }} HostSession
26
+ */
27
+
28
+ const GUEST = Object.freeze({ user: null, csrfToken: null });
29
+
30
+ // The toast container is a component module the host has no other use for, and the layout has already loaded
31
+ // it, so it is imported when the first toast is shown.
32
+ let showToast = async (...args) => (await import("$pano/lib/components/ToastContainer.svelte")).show(...args);
33
+
34
+ /** Tests only: replace what shows a toast. */
35
+ export function setToastForTests(fn) {
36
+ showToast = fn;
37
+ }
38
+
39
+ // ---------------------------------------------------------------------------
40
+ // Browser session binding
41
+ // ---------------------------------------------------------------------------
42
+
43
+ /** @type {HostSession} */
44
+ let current = GUEST;
45
+ let bound = false;
46
+ /** @type {null | (() => void)} */
47
+ let stopBinding = null;
48
+ /** @type {Set<(session: HostSession) => void>} */
49
+ const listeners = new Set();
50
+
51
+ /** Who the session belongs to: a login or a logout changes it, a refreshed token alone does not. */
52
+ function identityOf(user) {
53
+ if (!user) return null;
54
+ return user.id ?? user.username ?? "user";
55
+ }
56
+
57
+ function toHostSession(value) {
58
+ return {
59
+ user: value?.user ?? null,
60
+ csrfToken: value?.csrfToken ?? null,
61
+ };
62
+ }
63
+
64
+ function receive(value) {
65
+ const next = toHostSession(value);
66
+ const changed = !bound || identityOf(next.user) !== identityOf(current.user);
67
+
68
+ current = next;
69
+ bound = true;
70
+
71
+ if (!changed) return;
72
+
73
+ for (const listener of [...listeners]) {
74
+ try {
75
+ listener(next);
76
+ } catch (e) {
77
+ console.error("[theme-core] a controller session listener failed", e);
78
+ }
79
+ }
80
+ }
81
+
82
+ /**
83
+ * Feeds the browser host with the layout's session. Call it once where the layout sets its
84
+ * `session` context (browser only). Listeners registered through `host.onSession` run on the first
85
+ * bind and whenever the user logs in or out.
86
+ *
87
+ * @param {{ subscribe: (run: (value: any) => void) => (() => void) } | object | null} session
88
+ * the layout's `session` store (`{ user, csrfToken, siteInfo }`), or a plain value
89
+ * @returns {() => void} unbind (the layout calls it when it is destroyed)
90
+ */
91
+ export function bindControllerSession(session) {
92
+ // Server: nothing is bound, nothing is stored (see the header).
93
+ if (!browser) return () => {};
94
+
95
+ stopBinding?.();
96
+ stopBinding = null;
97
+
98
+ if (session && typeof session.subscribe === "function") {
99
+ const stop = session.subscribe(receive);
100
+ const unbind = () => {
101
+ stop();
102
+ if (stopBinding === unbind) stopBinding = null;
103
+ };
104
+ stopBinding = unbind;
105
+ return unbind;
106
+ }
107
+
108
+ receive(session);
109
+ return () => {};
110
+ }
111
+
112
+ /** Tests only: forget the binding and every listener. */
113
+ export function resetControllerSessionForTests() {
114
+ stopBinding?.();
115
+ stopBinding = null;
116
+ current = GUEST;
117
+ bound = false;
118
+ listeners.clear();
119
+ }
120
+
121
+ // ---------------------------------------------------------------------------
122
+ // Host
123
+ // ---------------------------------------------------------------------------
124
+
125
+ function queryString(query) {
126
+ if (!query || typeof query !== "object") return "";
127
+
128
+ const parts = [];
129
+ for (const key of Object.keys(query)) {
130
+ const value = query[key];
131
+ if (value === undefined || value === null) continue;
132
+ for (const item of Array.isArray(value) ? value : [value]) {
133
+ if (item === undefined || item === null) continue;
134
+ parts.push(`${encodeURIComponent(key)}=${encodeURIComponent(String(item))}`);
135
+ }
136
+ }
137
+
138
+ return parts.length ? "?" + parts.join("&") : "";
139
+ }
140
+
141
+ const networkError = () => ({ error: { code: "NETWORK_ERROR" } });
142
+
143
+ /**
144
+ * Turns what `ApiUtil` hands back into the controller contract: an object, never a rejection;
145
+ * `{ error: { code } }` for a failure. A legacy `{ result: "error", error: "CODE" }` body keeps its
146
+ * other keys and gets the object form of `error`.
147
+ * @param {unknown} body
148
+ */
149
+ function toControllerBody(body) {
150
+ if (body === undefined || body === null) return networkError();
151
+ if (typeof Blob !== "undefined" && body instanceof Blob) return body;
152
+ if (typeof body === "string") return body === "" ? {} : { error: { code: "INVALID_RESPONSE" } };
153
+ if (typeof body !== "object") return { error: { code: "INVALID_RESPONSE" } };
154
+
155
+ if (typeof body.error === "string") {
156
+ return { ...body, error: { code: body.error } };
157
+ }
158
+
159
+ return body;
160
+ }
161
+
162
+ function registerPath(returnTo) {
163
+ const safe = sanitizeReturnTo(returnTo);
164
+
165
+ return safe === "/" ? "/register" : `/register?redirect=${encodeURIComponent(safe)}`;
166
+ }
167
+
168
+ function safeStorage(kind) {
169
+ if (!browser) return null;
170
+
171
+ try {
172
+ return (kind === "session" ? globalThis.sessionStorage : globalThis.localStorage) ?? null;
173
+ } catch {
174
+ return null;
175
+ }
176
+ }
177
+
178
+ /**
179
+ * @param {{ event?: any }} [options] the SvelteKit load / request event of the server request this
180
+ * host serves. The browser needs none.
181
+ * @returns {import("@panomc/plugin-kit/controller").ControllerHost}
182
+ */
183
+ export function createThemeHost({ event } = {}) {
184
+ /** @returns {HostSession} */
185
+ const session = () => {
186
+ if (browser) return current;
187
+ if (!event) return GUEST;
188
+
189
+ return toHostSession(event.locals);
190
+ };
191
+
192
+ async function request({ method = "GET", path = "", query, body, headers, blob } = {}) {
193
+ if (!browser && !event) {
194
+ return { error: { code: "NO_REQUEST_EVENT" } };
195
+ }
196
+
197
+ const verb = String(method).toUpperCase();
198
+ const hasBody = body !== undefined && body !== null && verb !== "GET" && verb !== "HEAD";
199
+ const data = { method: verb, ...(headers ? { headers } : {}), ...(hasBody ? { body } : {}) };
200
+
201
+ try {
202
+ const result = await ApiUtil.customRequest({
203
+ path: String(path) + queryString(query),
204
+ data,
205
+ request: event,
206
+ csrfToken: session().csrfToken ?? undefined,
207
+ blob,
208
+ });
209
+
210
+ return toControllerBody(result);
211
+ } catch {
212
+ return networkError();
213
+ }
214
+ }
215
+
216
+ return {
217
+ baseUrl: browser ? (globalThis.location?.origin ?? "") : (event?.url?.origin ?? ""),
218
+ browser,
219
+ request,
220
+ session,
221
+ locale: () => get(locale) ?? undefined,
222
+ onSession(fn) {
223
+ if (!browser || typeof fn !== "function") return () => {};
224
+
225
+ listeners.add(fn);
226
+
227
+ return () => {
228
+ listeners.delete(fn);
229
+ };
230
+ },
231
+ t: (key, values) => get(_)(key, values ? { values } : undefined),
232
+ toast(key, options) {
233
+ if (!browser) return;
234
+
235
+ Promise.resolve(showToast(key, { ...(options?.values ?? {}) }, undefined, { variant: options?.variant })).catch(
236
+ (e) => console.error("[theme-core] a controller toast failed", e),
237
+ );
238
+ },
239
+ storage: safeStorage,
240
+ now: () => Date.now(),
241
+ navigate(url, options) {
242
+ if (!browser) return;
243
+
244
+ routedGoto(url, options?.replace ? { replaceState: true } : undefined);
245
+ },
246
+ feature: (name) => panoApi.features.has(name),
247
+ loginUrl: (returnTo) => panoApi.auth.loginUrl(returnTo),
248
+ registerUrl: (returnTo) => route(registerPath(returnTo)),
249
+ };
250
+ }
251
+
252
+ /**
253
+ * What `controllers.setHostFactory` takes. The controller registry calls its factory with the SvelteKit
254
+ * event of the request being served (`controllers.load(name, { event })`), or with nothing in the browser;
255
+ * `createThemeHost` takes `{ event }`. Registering `createThemeHost` itself would hand it the event as the
256
+ * options object, so every server-side controller call got `NO_REQUEST_EVENT`.
257
+ * @param {any} [event]
258
+ */
259
+ export function themeHostFactory(event) {
260
+ return createThemeHost({ event });
261
+ }
@@ -0,0 +1,317 @@
1
+ // Engine side of the scoped fallback stylesheet (doc 03 section 4.4).
2
+ //
3
+ // A theme that sets `provides: { bootstrap: false }` gets, for every DEFAULT plugin view it did not override, a
4
+ // wrapper `<div class="pano-fb" data-pano-fb="<ns>" style="display: contents">` and a link to the plugin's own
5
+ // `client/fallback.css` (Bootstrap subset, scoped to that wrapper). This file holds everything that has no Svelte
6
+ // in it: the set of stylesheets a render needs, the href of each, and `wrap`, which decides per view.
7
+ //
8
+ // `FallbackScope.svelte` is handed in by the caller (this file stays loadable without the Svelte compiler).
9
+ import { getContext } from "svelte";
10
+ import { SvelteSet } from "svelte/reactivity";
11
+
12
+ /** Context key of the scope: the namespace of the fallback scope a view renders in, or null inside a stop scope. */
13
+ export const SCOPE_CONTEXT = "pano:fb";
14
+ /** Context key of the stylesheet store `RootLayout.svelte` creates. */
15
+ export const STYLES_CONTEXT = "pano:fb-styles";
16
+ /** Attribute the links carry, so the browser store starts with what the server already rendered. */
17
+ export const STYLE_ATTRIBUTE = "data-pano-fb-style";
18
+
19
+ /** The scope (and the stylesheet key) of an injected view of a plugin that is not on the new model. */
20
+ export const LEGACY_KEY = "legacy";
21
+
22
+ /** Roots that cannot sit inside a wrapper `div` (a table-row view gets no wrapper, doc 03 section 4.4 step 3). */
23
+ const TABLE_ROOTS = new Set(["tr", "td", "th", "tbody", "thead", "tfoot", "col", "colgroup", "caption"]);
24
+
25
+ /** The engine's own views (names without a colon) have one engine sheet and nothing else. */
26
+ const ENGINE_ENTRY = Object.freeze({
27
+ id: "pano",
28
+ ns: "pano",
29
+ styles: Object.freeze({ fallback: "assets/css/pano-fallback.css" }),
30
+ roots: Object.freeze({}),
31
+ });
32
+
33
+ /**
34
+ * @typedef {{ fallback?: string, own?: string, hash?: string, icons?: boolean }} StyleMeta
35
+ * @typedef {{ id: string, ns: string, styles?: StyleMeta, roots?: Record<string, string[]> }} StyleEntry
36
+ * @typedef {Record<string, StyleEntry>} StyleTable namespace -> what the engine knows of the plugin's styles
37
+ * @typedef {{ keys: Set<string>, add(key: string): void }} FallbackStyles
38
+ */
39
+
40
+ // ---------------------------------------------------------------------------
41
+ // The set of stylesheets a render needs
42
+ // ---------------------------------------------------------------------------
43
+
44
+ /**
45
+ * One per render (`RootLayout.svelte` creates it and puts it in context).
46
+ *
47
+ * Server: a plain `Set`, `add` is synchronous (the head is rendered after the page, so it sees every key).
48
+ * Client: a `SvelteSet` that starts with the links the server rendered, `add` is deferred with `queueMicrotask`
49
+ * (a view adds its key while it renders, which must not write reactive state), and keys are never removed during
50
+ * the session, so destroying the first view that needed a sheet does not take it away from the others.
51
+ *
52
+ * @param {{ browser?: boolean }} [options] `browser` defaults to "a document exists"
53
+ * @returns {FallbackStyles}
54
+ */
55
+ export function createFallbackStyles({ browser = typeof document !== "undefined" } = {}) {
56
+ if (!browser) {
57
+ const keys = new Set();
58
+
59
+ return { keys, add: (key) => void keys.add(key) };
60
+ }
61
+
62
+ const keys = new SvelteSet();
63
+
64
+ try {
65
+ for (const link of document.head.querySelectorAll(`link[${STYLE_ATTRIBUTE}]`)) {
66
+ keys.add(link.getAttribute(STYLE_ATTRIBUTE));
67
+ }
68
+ } catch {
69
+ // no document (a test without one): start empty
70
+ }
71
+
72
+ return {
73
+ keys,
74
+ add(key) {
75
+ if (keys.has(key)) return;
76
+
77
+ queueMicrotask(() => keys.add(key));
78
+ },
79
+ };
80
+ }
81
+
82
+ /** The store a render without a `RootLayout` (a test, a widget) still needs somewhere to put keys. */
83
+ let activeStyles = null;
84
+
85
+ /**
86
+ * `RootLayout.svelte` calls this once per render. A server render is synchronous, so the store of the render in
87
+ * progress is the one every `wrap` call of that render means; the browser has one store for the whole session.
88
+ * @param {FallbackStyles | null} styles
89
+ */
90
+ export function setActiveFallbackStyles(styles) {
91
+ activeStyles = styles;
92
+ }
93
+
94
+ /**
95
+ * key -> href. ns: `${base}/plugins/<id>/resources/plugin-ui/client/fallback.css?v=<hash>`;
96
+ * `own:<ns>`: `.../client/plugin.css?v=<hash>`; `pano`: `${base}/assets/css/pano-fallback.css`;
97
+ * `icons`: `${base}/assets/css/pano-fallback-icons.css`; `legacy`: `${base}/assets/css/pano-fallback-legacy.css`. null = a key nothing can serve.
98
+ * @param {string} key
99
+ * @param {StyleTable} plugins
100
+ * @param {string} [base] SvelteKit's `base` path
101
+ * @returns {string | null}
102
+ */
103
+ export function fallbackHref(key, plugins, base = "") {
104
+ if (key === "icons") return `${base}/assets/css/pano-fallback-icons.css`;
105
+ if (key === "pano") return `${base}/assets/css/pano-fallback.css`;
106
+ if (key === LEGACY_KEY) return `${base}/assets/css/pano-fallback-legacy.css`;
107
+
108
+ const own = key.startsWith("own:");
109
+ const entry = lookupEntry(plugins, own ? key.slice("own:".length) : key);
110
+ const file = own ? entry?.styles?.own : entry?.styles?.fallback;
111
+
112
+ if (!entry || typeof file !== "string" || !file) return null;
113
+
114
+ const hash = entry.styles?.hash;
115
+ const query = typeof hash === "string" && hash ? `?v=${encodeURIComponent(hash)}` : "";
116
+
117
+ return `${base}/plugins/${encodeURIComponent(entry.id)}/resources/plugin-ui/${file.replace(/^\/+/, "")}${query}`;
118
+ }
119
+
120
+ /** An entry by namespace, or by plugin id (a view named `<pluginId>:<Name>` is the alias of `<ns>:<Name>`). */
121
+ function lookupEntry(table, ns) {
122
+ if (!table || typeof table !== "object") return null;
123
+ if (table[ns]) return table[ns];
124
+
125
+ for (const entry of Object.values(table)) {
126
+ if (entry && entry.id === ns) return entry;
127
+ }
128
+
129
+ return null;
130
+ }
131
+
132
+ /**
133
+ * What the theme process knows of every plugin's styles, as plain data the browser can be sent
134
+ * (`pano-plugin.json` is read on the server only). Only plugins with a namespace take part.
135
+ * @param {Iterable<{ id: string, namespace?: string, styles?: any, views?: any }>} plugins
136
+ * @returns {StyleTable}
137
+ */
138
+ export function buildStyleTable(plugins) {
139
+ /** @type {StyleTable} */
140
+ const table = {};
141
+
142
+ for (const plugin of plugins) {
143
+ if (!plugin || typeof plugin.namespace !== "string" || !plugin.namespace) continue;
144
+
145
+ /** @type {StyleEntry} */
146
+ const entry = { id: plugin.id, ns: plugin.namespace };
147
+ const styles = plugin.styles;
148
+
149
+ if (styles && typeof styles === "object") {
150
+ entry.styles = {};
151
+
152
+ for (const key of ["fallback", "own", "hash"]) {
153
+ if (typeof styles[key] === "string" && styles[key]) entry.styles[key] = styles[key];
154
+ }
155
+
156
+ if (styles.icons === true) entry.styles.icons = true;
157
+ }
158
+
159
+ const views = plugin.views;
160
+
161
+ if (views && typeof views === "object") {
162
+ entry.roots = {};
163
+
164
+ for (const [name, view] of Object.entries(views)) {
165
+ if (Array.isArray(view?.roots)) entry.roots[name] = view.roots.filter((root) => typeof root === "string");
166
+ }
167
+ }
168
+
169
+ table[plugin.namespace] = entry;
170
+ }
171
+
172
+ return table;
173
+ }
174
+
175
+ // ---------------------------------------------------------------------------
176
+ // wrap
177
+ // ---------------------------------------------------------------------------
178
+
179
+ /** `getContext` that answers undefined outside a component (a preload, a test) instead of throwing. */
180
+ function readContext(key) {
181
+ try {
182
+ return getContext(key);
183
+ } catch {
184
+ return undefined;
185
+ }
186
+ }
187
+
188
+ /**
189
+ * @typedef {{ mode: "fallback" | "stop" | null, ns: string }} ScopePlan
190
+ *
191
+ * @typedef {object} WrapperDeps
192
+ * @property {Function} FallbackScope `routes`-independent wrapper component (`FallbackScope.svelte`)
193
+ * @property {() => StyleTable} getTable the styles of every plugin, by namespace
194
+ * @property {() => { bootstrap: boolean, fontawesome: boolean }} getProvides
195
+ * @property {(key: string) => any} [readScope] reads a context key (default: Svelte's `getContext`, guarded)
196
+ * @property {boolean} [dev] log a plugin without fallback styles once
197
+ * @property {(message: string) => void} [warn]
198
+ */
199
+
200
+ /**
201
+ * `views.wrap` of the pano context.
202
+ *
203
+ * `plan(name, source, inside?)` applies steps 1-2 and 5 of the table in doc 03 section 4.4 (the stylesheets a view
204
+ * needs) and returns what the view gets: a `fallback` scope, a `stop` scope or nothing. `wrap` turns the plan into a
205
+ * component: the component itself, or `(anchor, props) => FallbackScope(anchor, { Component, ns, mode, props })`
206
+ * with the plan on it as `.scope`.
207
+ *
208
+ * | Step | Condition | Result |
209
+ * |---|---|---|
210
+ * | 1 | plugin has `styles.own` | `styles.add('own:' + ns)` (every theme) |
211
+ * | 2 | `default`, plugin `styles.icons`, `provides.fontawesome === false` | `styles.add('icons')` |
212
+ * | 3 | `provides.bootstrap`, or no `styles.fallback`, or the view's `roots` holds a table root | the component |
213
+ * | 4 | `default` and already inside its own plugin's scope | the component |
214
+ * | 5 | `default` otherwise | `styles.add(ns)`; `fallback` scope |
215
+ * | 6 | `override` and inside a scope | `stop` scope |
216
+ * | 7 | `override` otherwise | the component |
217
+ *
218
+ * @param {WrapperDeps} deps
219
+ * `wrapInjected(module)` is `wrap` for what a registered item's `component` resolved to (a nav item, a sidebar widget,
220
+ * a hook; doc 03 section 4.4, call sites). A module that carries `__panoView` (`loadForInjection` puts it there) is a
221
+ * named view and goes through `wrap`. Anything else is the legacy form (`component` of a plugin that is not on the
222
+ * new model): in a theme without Bootstrap it gets the one `legacy` scope (the whole Bootstrap base, scoped) and, with
223
+ * `provides.fontawesome === false`, the icon sheet.
224
+ *
225
+ * @returns {{ wrap: (name: string, Component: Function, source: "default" | "override") => Function,
226
+ * wrapInjected: (module: any) => Function,
227
+ * plan: (name: string, source: "default" | "override", inside?: unknown) => ScopePlan }}
228
+ */
229
+ export function createViewWrapper({ FallbackScope, getTable, getProvides, readScope = readContext, dev = false, warn = console.warn }) {
230
+ /** @type {Set<string>} */
231
+ const warned = new Set();
232
+
233
+ const store = () => readScope(STYLES_CONTEXT) ?? activeStyles;
234
+
235
+ function plan(name, source, inside = readScope(SCOPE_CONTEXT)) {
236
+ const colon = typeof name === "string" ? name.indexOf(":") : -1;
237
+ const requested = colon > 0 ? name.slice(0, colon) : "pano";
238
+ const viewName = colon > 0 ? name.slice(colon + 1) : name;
239
+ const entry = requested === "pano" ? ENGINE_ENTRY : lookupEntry(getTable(), requested);
240
+ const ns = entry?.ns ?? requested;
241
+ const styles = entry?.styles;
242
+ const provides = getProvides();
243
+ const sheets = store();
244
+
245
+ // 1, 2
246
+ if (styles?.own) sheets?.add(`own:${ns}`);
247
+ if (source === "default" && styles?.icons && provides.fontawesome === false) sheets?.add("icons");
248
+
249
+ // 3
250
+ if (provides.bootstrap) return { mode: null, ns };
251
+
252
+ if (!styles?.fallback) {
253
+ if (dev && entry && !styles && !warned.has(ns)) {
254
+ warned.add(ns);
255
+ warn(`[theme-core] ${entry.id} has no fallback styles — rebuild it with @panomc/plugin-kit`);
256
+ }
257
+
258
+ return { mode: null, ns };
259
+ }
260
+
261
+ if (entry.roots?.[viewName]?.some((root) => TABLE_ROOTS.has(root))) return { mode: null, ns };
262
+
263
+ if (source === "default") {
264
+ // 4
265
+ if (inside === ns) return { mode: null, ns };
266
+
267
+ // 5
268
+ sheets?.add(ns);
269
+
270
+ return { mode: "fallback", ns };
271
+ }
272
+
273
+ // 6, 7
274
+ return typeof inside === "string" ? { mode: "stop", ns } : { mode: null, ns };
275
+ }
276
+
277
+ function wrap(name, Component, source) {
278
+ const { mode, ns } = plan(name, source);
279
+
280
+ if (!mode) return Component;
281
+
282
+ const scoped = (anchorOrPayload, props) => FallbackScope(anchorOrPayload, { Component, ns, mode, props });
283
+
284
+ // a caller that mounts the view itself (the plugin page in the browser) puts the scope on its own element
285
+ scoped.scope = { mode, ns };
286
+
287
+ return scoped;
288
+ }
289
+
290
+ function wrapInjected(module) {
291
+ const Component = module?.default ?? module;
292
+
293
+ if (module && typeof module === "object" && typeof module.__panoView === "string" && module.__panoView) {
294
+ return wrap(module.__panoView, Component, module.__panoSource === "override" ? "override" : "default") ?? Component;
295
+ }
296
+
297
+ const provides = getProvides();
298
+
299
+ if (provides.bootstrap) return Component;
300
+
301
+ const sheets = store();
302
+
303
+ sheets?.add(LEGACY_KEY);
304
+ if (provides.fontawesome === false) sheets?.add("icons");
305
+
306
+ if (readScope(SCOPE_CONTEXT) === LEGACY_KEY) return Component;
307
+
308
+ const scoped = (anchorOrPayload, props) =>
309
+ FallbackScope(anchorOrPayload, { Component, ns: LEGACY_KEY, mode: "fallback", props });
310
+
311
+ scoped.scope = { mode: "fallback", ns: LEGACY_KEY };
312
+
313
+ return scoped;
314
+ }
315
+
316
+ return { wrap, wrapInjected, plan };
317
+ }