emdash 0.0.0-b → 0.0.2

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 (661) hide show
  1. package/README.md +87 -43
  2. package/dist/adapters-BLMa4JGD.d.mts +106 -0
  3. package/dist/adapters-BLMa4JGD.d.mts.map +1 -0
  4. package/dist/apply-Bjfq_b4-.mjs +1293 -0
  5. package/dist/apply-Bjfq_b4-.mjs.map +1 -0
  6. package/dist/astro/index.d.mts +51 -0
  7. package/dist/astro/index.d.mts.map +1 -0
  8. package/dist/astro/index.mjs +1336 -0
  9. package/dist/astro/index.mjs.map +1 -0
  10. package/dist/astro/middleware/auth.d.mts +31 -0
  11. package/dist/astro/middleware/auth.d.mts.map +1 -0
  12. package/dist/astro/middleware/auth.mjs +654 -0
  13. package/dist/astro/middleware/auth.mjs.map +1 -0
  14. package/dist/astro/middleware/redirect.d.mts +22 -0
  15. package/dist/astro/middleware/redirect.d.mts.map +1 -0
  16. package/dist/astro/middleware/redirect.mjs +63 -0
  17. package/dist/astro/middleware/redirect.mjs.map +1 -0
  18. package/dist/astro/middleware/request-context.d.mts +18 -0
  19. package/dist/astro/middleware/request-context.d.mts.map +1 -0
  20. package/dist/astro/middleware/request-context.mjs +1310 -0
  21. package/dist/astro/middleware/request-context.mjs.map +1 -0
  22. package/dist/astro/middleware/setup.d.mts +20 -0
  23. package/dist/astro/middleware/setup.d.mts.map +1 -0
  24. package/dist/astro/middleware/setup.mjs +47 -0
  25. package/dist/astro/middleware/setup.mjs.map +1 -0
  26. package/dist/astro/middleware.d.mts +13 -0
  27. package/dist/astro/middleware.d.mts.map +1 -0
  28. package/dist/astro/middleware.mjs +1613 -0
  29. package/dist/astro/middleware.mjs.map +1 -0
  30. package/dist/astro/types.d.mts +250 -0
  31. package/dist/astro/types.d.mts.map +1 -0
  32. package/dist/astro/types.mjs +1 -0
  33. package/dist/base64-MBPo9ozB.mjs +59 -0
  34. package/dist/base64-MBPo9ozB.mjs.map +1 -0
  35. package/dist/byline-CL847F26.mjs +213 -0
  36. package/dist/byline-CL847F26.mjs.map +1 -0
  37. package/dist/bylines-C2a-2TGt.mjs +136 -0
  38. package/dist/bylines-C2a-2TGt.mjs.map +1 -0
  39. package/dist/chunk-ClPoSABd.mjs +21 -0
  40. package/dist/cli/index.d.mts +1 -0
  41. package/dist/cli/index.mjs +3909 -0
  42. package/dist/cli/index.mjs.map +1 -0
  43. package/dist/client/cf-access.d.mts +60 -0
  44. package/dist/client/cf-access.d.mts.map +1 -0
  45. package/dist/client/cf-access.mjs +179 -0
  46. package/dist/client/cf-access.mjs.map +1 -0
  47. package/dist/client/index.d.mts +398 -0
  48. package/dist/client/index.d.mts.map +1 -0
  49. package/dist/client/index.mjs +346 -0
  50. package/dist/client/index.mjs.map +1 -0
  51. package/dist/config-CKE8p9xM.mjs +55 -0
  52. package/dist/config-CKE8p9xM.mjs.map +1 -0
  53. package/dist/connection-B4zVnQIa.mjs +40 -0
  54. package/dist/connection-B4zVnQIa.mjs.map +1 -0
  55. package/dist/content-D6C2WsZC.mjs +824 -0
  56. package/dist/content-D6C2WsZC.mjs.map +1 -0
  57. package/dist/db/index.d.mts +4 -0
  58. package/dist/db/index.mjs +62 -0
  59. package/dist/db/index.mjs.map +1 -0
  60. package/dist/db/libsql.d.mts +11 -0
  61. package/dist/db/libsql.d.mts.map +1 -0
  62. package/dist/db/libsql.mjs +17 -0
  63. package/dist/db/libsql.mjs.map +1 -0
  64. package/dist/db/postgres.d.mts +11 -0
  65. package/dist/db/postgres.d.mts.map +1 -0
  66. package/dist/db/postgres.mjs +30 -0
  67. package/dist/db/postgres.mjs.map +1 -0
  68. package/dist/db/sqlite.d.mts +11 -0
  69. package/dist/db/sqlite.d.mts.map +1 -0
  70. package/dist/db/sqlite.mjs +16 -0
  71. package/dist/db/sqlite.mjs.map +1 -0
  72. package/dist/default-Cyi4aAxu.mjs +81 -0
  73. package/dist/default-Cyi4aAxu.mjs.map +1 -0
  74. package/dist/dialect-helpers-B9uSp2GJ.mjs +90 -0
  75. package/dist/dialect-helpers-B9uSp2GJ.mjs.map +1 -0
  76. package/dist/error-Cxz0tQeO.mjs +27 -0
  77. package/dist/error-Cxz0tQeO.mjs.map +1 -0
  78. package/dist/index-C1xF3OGh.d.mts +4527 -0
  79. package/dist/index-C1xF3OGh.d.mts.map +1 -0
  80. package/dist/index.d.mts +16 -0
  81. package/dist/index.mjs +30 -0
  82. package/dist/load-yOOlckBj.mjs +28 -0
  83. package/dist/load-yOOlckBj.mjs.map +1 -0
  84. package/dist/loader-fz8Q_3EO.mjs +447 -0
  85. package/dist/loader-fz8Q_3EO.mjs.map +1 -0
  86. package/dist/manifest-schema-Dcl0R6nM.mjs +184 -0
  87. package/dist/manifest-schema-Dcl0R6nM.mjs.map +1 -0
  88. package/dist/media/index.d.mts +26 -0
  89. package/dist/media/index.d.mts.map +1 -0
  90. package/dist/media/index.mjs +55 -0
  91. package/dist/media/index.mjs.map +1 -0
  92. package/dist/media/local-runtime.d.mts +39 -0
  93. package/dist/media/local-runtime.d.mts.map +1 -0
  94. package/dist/media/local-runtime.mjs +133 -0
  95. package/dist/media/local-runtime.mjs.map +1 -0
  96. package/dist/media-DqHVh136.mjs +200 -0
  97. package/dist/media-DqHVh136.mjs.map +1 -0
  98. package/dist/mode-C2EzN1uE.mjs +23 -0
  99. package/dist/mode-C2EzN1uE.mjs.map +1 -0
  100. package/dist/page/index.d.mts +140 -0
  101. package/dist/page/index.d.mts.map +1 -0
  102. package/dist/page/index.mjs +416 -0
  103. package/dist/page/index.mjs.map +1 -0
  104. package/dist/placeholder-CmGAmqeO.d.mts +276 -0
  105. package/dist/placeholder-CmGAmqeO.d.mts.map +1 -0
  106. package/dist/placeholder-SmpOx-_v.mjs +243 -0
  107. package/dist/placeholder-SmpOx-_v.mjs.map +1 -0
  108. package/dist/plugin-utils.d.mts +58 -0
  109. package/dist/plugin-utils.d.mts.map +1 -0
  110. package/dist/plugin-utils.mjs +78 -0
  111. package/dist/plugin-utils.mjs.map +1 -0
  112. package/dist/plugins/adapt-sandbox-entry.d.mts +22 -0
  113. package/dist/plugins/adapt-sandbox-entry.d.mts.map +1 -0
  114. package/dist/plugins/adapt-sandbox-entry.mjs +113 -0
  115. package/dist/plugins/adapt-sandbox-entry.mjs.map +1 -0
  116. package/dist/query-CS_iSj34.mjs +460 -0
  117. package/dist/query-CS_iSj34.mjs.map +1 -0
  118. package/dist/redirect-DIfIni3r.mjs +329 -0
  119. package/dist/redirect-DIfIni3r.mjs.map +1 -0
  120. package/dist/registry-D_w5HW4G.mjs +863 -0
  121. package/dist/registry-D_w5HW4G.mjs.map +1 -0
  122. package/dist/request-context.d.mts +49 -0
  123. package/dist/request-context.d.mts.map +1 -0
  124. package/dist/request-context.mjs +43 -0
  125. package/dist/request-context.mjs.map +1 -0
  126. package/dist/runner-C0hCbYnD.mjs +1412 -0
  127. package/dist/runner-C0hCbYnD.mjs.map +1 -0
  128. package/dist/runner-EAtf0ZIe.d.mts +27 -0
  129. package/dist/runner-EAtf0ZIe.d.mts.map +1 -0
  130. package/dist/runtime.d.mts +26 -0
  131. package/dist/runtime.d.mts.map +1 -0
  132. package/dist/runtime.mjs +42 -0
  133. package/dist/runtime.mjs.map +1 -0
  134. package/dist/search-DG603UrT.mjs +9211 -0
  135. package/dist/search-DG603UrT.mjs.map +1 -0
  136. package/dist/seed/index.d.mts +3 -0
  137. package/dist/seed/index.mjs +15 -0
  138. package/dist/seo/index.d.mts +70 -0
  139. package/dist/seo/index.d.mts.map +1 -0
  140. package/dist/seo/index.mjs +70 -0
  141. package/dist/seo/index.mjs.map +1 -0
  142. package/dist/storage/local.d.mts +39 -0
  143. package/dist/storage/local.d.mts.map +1 -0
  144. package/dist/storage/local.mjs +166 -0
  145. package/dist/storage/local.mjs.map +1 -0
  146. package/dist/storage/s3.d.mts +32 -0
  147. package/dist/storage/s3.d.mts.map +1 -0
  148. package/dist/storage/s3.mjs +175 -0
  149. package/dist/storage/s3.mjs.map +1 -0
  150. package/dist/tokens-DpgrkrXK.mjs +171 -0
  151. package/dist/tokens-DpgrkrXK.mjs.map +1 -0
  152. package/dist/transport-BFGblqwG.d.mts +42 -0
  153. package/dist/transport-BFGblqwG.d.mts.map +1 -0
  154. package/dist/transport-yxiQsi8I.mjs +418 -0
  155. package/dist/transport-yxiQsi8I.mjs.map +1 -0
  156. package/dist/types-BRuPJGdV.d.mts +102 -0
  157. package/dist/types-BRuPJGdV.d.mts.map +1 -0
  158. package/dist/types-C4-fAxN3.d.mts +182 -0
  159. package/dist/types-C4-fAxN3.d.mts.map +1 -0
  160. package/dist/types-CMMN0pNg.mjs +31 -0
  161. package/dist/types-CMMN0pNg.mjs.map +1 -0
  162. package/dist/types-CUBbjgmP.mjs +16 -0
  163. package/dist/types-CUBbjgmP.mjs.map +1 -0
  164. package/dist/types-DRjfYOEv.d.mts +426 -0
  165. package/dist/types-DRjfYOEv.d.mts.map +1 -0
  166. package/dist/types-DY5zk5HN.mjs +73 -0
  167. package/dist/types-DY5zk5HN.mjs.map +1 -0
  168. package/dist/types-DaNLHo_T.d.mts +184 -0
  169. package/dist/types-DaNLHo_T.d.mts.map +1 -0
  170. package/dist/types-DvhsUmSJ.d.mts +1111 -0
  171. package/dist/types-DvhsUmSJ.d.mts.map +1 -0
  172. package/dist/validate-CpBtVMsD.d.mts +378 -0
  173. package/dist/validate-CpBtVMsD.d.mts.map +1 -0
  174. package/dist/validate-CqRJb_xU.mjs +97 -0
  175. package/dist/validate-CqRJb_xU.mjs.map +1 -0
  176. package/dist/validate-O7PWmlnq.mjs +328 -0
  177. package/dist/validate-O7PWmlnq.mjs.map +1 -0
  178. package/locals.d.ts +46 -0
  179. package/package.json +233 -19
  180. package/src/api/authorize.ts +63 -0
  181. package/src/api/csrf.ts +48 -0
  182. package/src/api/error.ts +99 -0
  183. package/src/api/errors.ts +445 -0
  184. package/src/api/escape.ts +9 -0
  185. package/src/api/handlers/api-tokens.ts +240 -0
  186. package/src/api/handlers/comments.ts +314 -0
  187. package/src/api/handlers/content.ts +1315 -0
  188. package/src/api/handlers/dashboard.ts +205 -0
  189. package/src/api/handlers/device-flow.ts +684 -0
  190. package/src/api/handlers/index.ts +163 -0
  191. package/src/api/handlers/manifest.ts +158 -0
  192. package/src/api/handlers/marketplace.ts +930 -0
  193. package/src/api/handlers/media.ts +207 -0
  194. package/src/api/handlers/menus.ts +493 -0
  195. package/src/api/handlers/oauth-authorization.ts +429 -0
  196. package/src/api/handlers/oauth-clients.ts +349 -0
  197. package/src/api/handlers/oauth-user-lookup.ts +39 -0
  198. package/src/api/handlers/plugins.ts +254 -0
  199. package/src/api/handlers/redirects.ts +360 -0
  200. package/src/api/handlers/revision.ts +145 -0
  201. package/src/api/handlers/schema.ts +534 -0
  202. package/src/api/handlers/sections.ts +289 -0
  203. package/src/api/handlers/seo.ts +115 -0
  204. package/src/api/handlers/settings.ts +49 -0
  205. package/src/api/handlers/snapshot.ts +350 -0
  206. package/src/api/handlers/taxonomies.ts +523 -0
  207. package/src/api/index.ts +6 -0
  208. package/src/api/openapi/document.ts +2368 -0
  209. package/src/api/openapi/index.ts +1 -0
  210. package/src/api/parse.ts +139 -0
  211. package/src/api/redirect.ts +14 -0
  212. package/src/api/rev.ts +67 -0
  213. package/src/api/schemas/auth.ts +112 -0
  214. package/src/api/schemas/bylines.ts +85 -0
  215. package/src/api/schemas/comments.ts +117 -0
  216. package/src/api/schemas/common.ts +89 -0
  217. package/src/api/schemas/content.ts +191 -0
  218. package/src/api/schemas/import.ts +52 -0
  219. package/src/api/schemas/index.ts +17 -0
  220. package/src/api/schemas/media.ts +116 -0
  221. package/src/api/schemas/menus.ts +111 -0
  222. package/src/api/schemas/redirects.ts +155 -0
  223. package/src/api/schemas/schema.ts +203 -0
  224. package/src/api/schemas/search.ts +63 -0
  225. package/src/api/schemas/sections.ts +67 -0
  226. package/src/api/schemas/settings.ts +63 -0
  227. package/src/api/schemas/setup.ts +37 -0
  228. package/src/api/schemas/taxonomies.ts +113 -0
  229. package/src/api/schemas/users.ts +96 -0
  230. package/src/api/schemas/widgets.ts +80 -0
  231. package/src/api/site-url.ts +25 -0
  232. package/src/api/types.ts +82 -0
  233. package/src/astro/index.ts +27 -0
  234. package/src/astro/integration/index.ts +303 -0
  235. package/src/astro/integration/routes.ts +834 -0
  236. package/src/astro/integration/runtime.ts +338 -0
  237. package/src/astro/integration/virtual-modules.ts +469 -0
  238. package/src/astro/integration/vite-config.ts +335 -0
  239. package/src/astro/middleware/auth.ts +743 -0
  240. package/src/astro/middleware/redirect.ts +89 -0
  241. package/src/astro/middleware/request-context.ts +129 -0
  242. package/src/astro/middleware/setup.ts +89 -0
  243. package/src/astro/middleware.ts +398 -0
  244. package/src/astro/routes/PluginRegistry.tsx +15 -0
  245. package/src/astro/routes/admin.astro +81 -0
  246. package/src/astro/routes/api/admin/allowed-domains/[domain].ts +112 -0
  247. package/src/astro/routes/api/admin/allowed-domains/index.ts +108 -0
  248. package/src/astro/routes/api/admin/api-tokens/[id].ts +40 -0
  249. package/src/astro/routes/api/admin/api-tokens/index.ts +68 -0
  250. package/src/astro/routes/api/admin/bylines/[id]/index.ts +87 -0
  251. package/src/astro/routes/api/admin/bylines/index.ts +72 -0
  252. package/src/astro/routes/api/admin/comments/[id]/status.ts +116 -0
  253. package/src/astro/routes/api/admin/comments/[id].ts +64 -0
  254. package/src/astro/routes/api/admin/comments/bulk.ts +42 -0
  255. package/src/astro/routes/api/admin/comments/counts.ts +30 -0
  256. package/src/astro/routes/api/admin/comments/index.ts +46 -0
  257. package/src/astro/routes/api/admin/hooks/exclusive/[hookName].ts +91 -0
  258. package/src/astro/routes/api/admin/hooks/exclusive/index.ts +51 -0
  259. package/src/astro/routes/api/admin/oauth-clients/[id].ts +110 -0
  260. package/src/astro/routes/api/admin/oauth-clients/index.ts +71 -0
  261. package/src/astro/routes/api/admin/plugins/[id]/disable.ts +39 -0
  262. package/src/astro/routes/api/admin/plugins/[id]/enable.ts +39 -0
  263. package/src/astro/routes/api/admin/plugins/[id]/index.ts +38 -0
  264. package/src/astro/routes/api/admin/plugins/[id]/uninstall.ts +48 -0
  265. package/src/astro/routes/api/admin/plugins/[id]/update.ts +59 -0
  266. package/src/astro/routes/api/admin/plugins/index.ts +32 -0
  267. package/src/astro/routes/api/admin/plugins/marketplace/[id]/icon.ts +61 -0
  268. package/src/astro/routes/api/admin/plugins/marketplace/[id]/index.ts +33 -0
  269. package/src/astro/routes/api/admin/plugins/marketplace/[id]/install.ts +62 -0
  270. package/src/astro/routes/api/admin/plugins/marketplace/index.ts +38 -0
  271. package/src/astro/routes/api/admin/plugins/updates.ts +28 -0
  272. package/src/astro/routes/api/admin/themes/marketplace/[id]/index.ts +33 -0
  273. package/src/astro/routes/api/admin/themes/marketplace/[id]/thumbnail.ts +61 -0
  274. package/src/astro/routes/api/admin/themes/marketplace/index.ts +45 -0
  275. package/src/astro/routes/api/admin/users/[id]/disable.ts +69 -0
  276. package/src/astro/routes/api/admin/users/[id]/enable.ts +48 -0
  277. package/src/astro/routes/api/admin/users/[id]/index.ts +146 -0
  278. package/src/astro/routes/api/admin/users/[id]/send-recovery.ts +72 -0
  279. package/src/astro/routes/api/admin/users/index.ts +66 -0
  280. package/src/astro/routes/api/auth/dev-bypass.ts +139 -0
  281. package/src/astro/routes/api/auth/invite/accept.ts +52 -0
  282. package/src/astro/routes/api/auth/invite/complete.ts +84 -0
  283. package/src/astro/routes/api/auth/invite/index.ts +99 -0
  284. package/src/astro/routes/api/auth/logout.ts +40 -0
  285. package/src/astro/routes/api/auth/magic-link/send.ts +89 -0
  286. package/src/astro/routes/api/auth/magic-link/verify.ts +71 -0
  287. package/src/astro/routes/api/auth/me.ts +60 -0
  288. package/src/astro/routes/api/auth/oauth/[provider]/callback.ts +219 -0
  289. package/src/astro/routes/api/auth/oauth/[provider].ts +119 -0
  290. package/src/astro/routes/api/auth/passkey/[id].ts +124 -0
  291. package/src/astro/routes/api/auth/passkey/index.ts +54 -0
  292. package/src/astro/routes/api/auth/passkey/options.ts +82 -0
  293. package/src/astro/routes/api/auth/passkey/register/options.ts +86 -0
  294. package/src/astro/routes/api/auth/passkey/register/verify.ts +115 -0
  295. package/src/astro/routes/api/auth/passkey/verify.ts +66 -0
  296. package/src/astro/routes/api/auth/signup/complete.ts +85 -0
  297. package/src/astro/routes/api/auth/signup/request.ts +77 -0
  298. package/src/astro/routes/api/auth/signup/verify.ts +53 -0
  299. package/src/astro/routes/api/comments/[collection]/[contentId]/index.ts +312 -0
  300. package/src/astro/routes/api/content/[collection]/[id]/compare.ts +28 -0
  301. package/src/astro/routes/api/content/[collection]/[id]/discard-draft.ts +54 -0
  302. package/src/astro/routes/api/content/[collection]/[id]/duplicate.ts +61 -0
  303. package/src/astro/routes/api/content/[collection]/[id]/permanent.ts +33 -0
  304. package/src/astro/routes/api/content/[collection]/[id]/preview-url.ts +107 -0
  305. package/src/astro/routes/api/content/[collection]/[id]/publish.ts +56 -0
  306. package/src/astro/routes/api/content/[collection]/[id]/restore.ts +54 -0
  307. package/src/astro/routes/api/content/[collection]/[id]/revisions.ts +31 -0
  308. package/src/astro/routes/api/content/[collection]/[id]/schedule.ts +101 -0
  309. package/src/astro/routes/api/content/[collection]/[id]/terms/[taxonomy].ts +140 -0
  310. package/src/astro/routes/api/content/[collection]/[id]/translations.ts +30 -0
  311. package/src/astro/routes/api/content/[collection]/[id]/unpublish.ts +56 -0
  312. package/src/astro/routes/api/content/[collection]/[id].ts +137 -0
  313. package/src/astro/routes/api/content/[collection]/index.ts +59 -0
  314. package/src/astro/routes/api/content/[collection]/trash.ts +33 -0
  315. package/src/astro/routes/api/dashboard.ts +32 -0
  316. package/src/astro/routes/api/dev/emails.ts +36 -0
  317. package/src/astro/routes/api/import/probe.ts +47 -0
  318. package/src/astro/routes/api/import/wordpress/analyze.ts +510 -0
  319. package/src/astro/routes/api/import/wordpress/execute.ts +283 -0
  320. package/src/astro/routes/api/import/wordpress/media.ts +338 -0
  321. package/src/astro/routes/api/import/wordpress/prepare.ts +181 -0
  322. package/src/astro/routes/api/import/wordpress/rewrite-urls.ts +393 -0
  323. package/src/astro/routes/api/import/wordpress-plugin/analyze.ts +111 -0
  324. package/src/astro/routes/api/import/wordpress-plugin/callback.ts +58 -0
  325. package/src/astro/routes/api/import/wordpress-plugin/execute.ts +347 -0
  326. package/src/astro/routes/api/manifest.ts +62 -0
  327. package/src/astro/routes/api/mcp.ts +124 -0
  328. package/src/astro/routes/api/media/[id]/confirm.ts +93 -0
  329. package/src/astro/routes/api/media/[id].ts +145 -0
  330. package/src/astro/routes/api/media/file/[key].ts +79 -0
  331. package/src/astro/routes/api/media/providers/[providerId]/[itemId].ts +86 -0
  332. package/src/astro/routes/api/media/providers/[providerId]/index.ts +111 -0
  333. package/src/astro/routes/api/media/providers/index.ts +30 -0
  334. package/src/astro/routes/api/media/upload-url.ts +137 -0
  335. package/src/astro/routes/api/media.ts +190 -0
  336. package/src/astro/routes/api/menus/[name]/items.ts +87 -0
  337. package/src/astro/routes/api/menus/[name]/reorder.ts +33 -0
  338. package/src/astro/routes/api/menus/[name].ts +65 -0
  339. package/src/astro/routes/api/menus/index.ts +47 -0
  340. package/src/astro/routes/api/oauth/authorize.ts +412 -0
  341. package/src/astro/routes/api/oauth/device/authorize.ts +45 -0
  342. package/src/astro/routes/api/oauth/device/code.ts +51 -0
  343. package/src/astro/routes/api/oauth/device/token.ts +69 -0
  344. package/src/astro/routes/api/oauth/token/refresh.ts +38 -0
  345. package/src/astro/routes/api/oauth/token/revoke.ts +38 -0
  346. package/src/astro/routes/api/oauth/token.ts +184 -0
  347. package/src/astro/routes/api/openapi.json.ts +32 -0
  348. package/src/astro/routes/api/plugins/[pluginId]/[...path].ts +92 -0
  349. package/src/astro/routes/api/redirects/404s/index.ts +72 -0
  350. package/src/astro/routes/api/redirects/404s/summary.ts +33 -0
  351. package/src/astro/routes/api/redirects/[id].ts +84 -0
  352. package/src/astro/routes/api/redirects/index.ts +52 -0
  353. package/src/astro/routes/api/revisions/[revisionId]/index.ts +29 -0
  354. package/src/astro/routes/api/revisions/[revisionId]/restore.ts +58 -0
  355. package/src/astro/routes/api/schema/collections/[slug]/fields/[fieldSlug].ts +76 -0
  356. package/src/astro/routes/api/schema/collections/[slug]/fields/index.ts +52 -0
  357. package/src/astro/routes/api/schema/collections/[slug]/fields/reorder.ts +32 -0
  358. package/src/astro/routes/api/schema/collections/[slug]/index.ts +80 -0
  359. package/src/astro/routes/api/schema/collections/index.ts +47 -0
  360. package/src/astro/routes/api/schema/index.ts +109 -0
  361. package/src/astro/routes/api/schema/orphans/[slug].ts +36 -0
  362. package/src/astro/routes/api/schema/orphans/index.ts +26 -0
  363. package/src/astro/routes/api/search/enable.ts +64 -0
  364. package/src/astro/routes/api/search/index.ts +55 -0
  365. package/src/astro/routes/api/search/rebuild.ts +72 -0
  366. package/src/astro/routes/api/search/stats.ts +35 -0
  367. package/src/astro/routes/api/search/suggest.ts +53 -0
  368. package/src/astro/routes/api/sections/[slug].ts +84 -0
  369. package/src/astro/routes/api/sections/index.ts +52 -0
  370. package/src/astro/routes/api/settings/email.ts +150 -0
  371. package/src/astro/routes/api/settings.ts +67 -0
  372. package/src/astro/routes/api/setup/admin-verify.ts +100 -0
  373. package/src/astro/routes/api/setup/admin.ts +94 -0
  374. package/src/astro/routes/api/setup/dev-bypass.ts +199 -0
  375. package/src/astro/routes/api/setup/dev-reset.ts +40 -0
  376. package/src/astro/routes/api/setup/index.ts +126 -0
  377. package/src/astro/routes/api/setup/status.ts +122 -0
  378. package/src/astro/routes/api/snapshot.ts +75 -0
  379. package/src/astro/routes/api/taxonomies/[name]/terms/[slug].ts +95 -0
  380. package/src/astro/routes/api/taxonomies/[name]/terms/index.ts +69 -0
  381. package/src/astro/routes/api/taxonomies/index.ts +59 -0
  382. package/src/astro/routes/api/themes/preview.ts +77 -0
  383. package/src/astro/routes/api/typegen.ts +114 -0
  384. package/src/astro/routes/api/well-known/auth.ts +68 -0
  385. package/src/astro/routes/api/well-known/oauth-authorization-server.ts +44 -0
  386. package/src/astro/routes/api/well-known/oauth-protected-resource.ts +37 -0
  387. package/src/astro/routes/api/widget-areas/[name]/reorder.ts +68 -0
  388. package/src/astro/routes/api/widget-areas/[name]/widgets/[id].ts +127 -0
  389. package/src/astro/routes/api/widget-areas/[name]/widgets.ts +80 -0
  390. package/src/astro/routes/api/widget-areas/[name].ts +87 -0
  391. package/src/astro/routes/api/widget-areas/index.ts +99 -0
  392. package/src/astro/routes/api/widget-components.ts +22 -0
  393. package/src/astro/routes/robots.txt.ts +77 -0
  394. package/src/astro/routes/sitemap.xml.ts +97 -0
  395. package/src/astro/storage/adapters.ts +74 -0
  396. package/src/astro/storage/index.ts +19 -0
  397. package/src/astro/storage/types.ts +60 -0
  398. package/src/astro/types.ts +346 -0
  399. package/src/auth/api-tokens.ts +25 -0
  400. package/src/auth/challenge-store.ts +80 -0
  401. package/src/auth/mode.ts +96 -0
  402. package/src/auth/oauth-state-store.ts +96 -0
  403. package/src/auth/passkey-config.ts +27 -0
  404. package/src/auth/rate-limit.ts +158 -0
  405. package/src/auth/scopes.ts +33 -0
  406. package/src/auth/types.ts +104 -0
  407. package/src/aws-sdk.d.ts +100 -0
  408. package/src/bylines/index.ts +237 -0
  409. package/src/cleanup.ts +153 -0
  410. package/src/cli/client-factory.ts +100 -0
  411. package/src/cli/commands/auth.ts +46 -0
  412. package/src/cli/commands/bundle-utils.ts +247 -0
  413. package/src/cli/commands/bundle.ts +609 -0
  414. package/src/cli/commands/content.ts +442 -0
  415. package/src/cli/commands/dev.ts +191 -0
  416. package/src/cli/commands/doctor.ts +211 -0
  417. package/src/cli/commands/export-seed.ts +630 -0
  418. package/src/cli/commands/import/wordpress.ts +1056 -0
  419. package/src/cli/commands/init.ts +192 -0
  420. package/src/cli/commands/login.ts +547 -0
  421. package/src/cli/commands/media.ts +165 -0
  422. package/src/cli/commands/menu.ts +67 -0
  423. package/src/cli/commands/plugin-init.ts +291 -0
  424. package/src/cli/commands/plugin-validate.ts +31 -0
  425. package/src/cli/commands/plugin.ts +33 -0
  426. package/src/cli/commands/publish.ts +697 -0
  427. package/src/cli/commands/schema.ts +233 -0
  428. package/src/cli/commands/search-cmd.ts +54 -0
  429. package/src/cli/commands/seed.ts +286 -0
  430. package/src/cli/commands/taxonomy.ts +128 -0
  431. package/src/cli/commands/types.ts +68 -0
  432. package/src/cli/credentials.ts +236 -0
  433. package/src/cli/index.ts +70 -0
  434. package/src/cli/output.ts +75 -0
  435. package/src/cli/wxr/parser.ts +969 -0
  436. package/src/client/cf-access.ts +193 -0
  437. package/src/client/index.ts +854 -0
  438. package/src/client/portable-text.ts +413 -0
  439. package/src/client/transport.ts +200 -0
  440. package/src/comments/moderator.ts +46 -0
  441. package/src/comments/notifications.ts +144 -0
  442. package/src/comments/query.ts +105 -0
  443. package/src/comments/service.ts +213 -0
  444. package/src/components/Break.astro +45 -0
  445. package/src/components/Button.astro +71 -0
  446. package/src/components/Buttons.astro +49 -0
  447. package/src/components/Code.astro +59 -0
  448. package/src/components/Columns.astro +59 -0
  449. package/src/components/CommentForm.astro +315 -0
  450. package/src/components/Comments.astro +232 -0
  451. package/src/components/Cover.astro +128 -0
  452. package/src/components/EmDashBodyEnd.astro +32 -0
  453. package/src/components/EmDashBodyStart.astro +32 -0
  454. package/src/components/EmDashHead.astro +53 -0
  455. package/src/components/EmDashImage.astro +178 -0
  456. package/src/components/EmDashMedia.astro +167 -0
  457. package/src/components/Embed.astro +128 -0
  458. package/src/components/File.astro +122 -0
  459. package/src/components/Gallery.astro +93 -0
  460. package/src/components/HtmlBlock.astro +33 -0
  461. package/src/components/Image.astro +178 -0
  462. package/src/components/InlineEditor.astro +27 -0
  463. package/src/components/InlinePortableTextEditor.tsx +1905 -0
  464. package/src/components/LiveSearch.astro +614 -0
  465. package/src/components/PortableText.astro +51 -0
  466. package/src/components/Pullquote.astro +51 -0
  467. package/src/components/Table.astro +108 -0
  468. package/src/components/WidgetArea.astro +22 -0
  469. package/src/components/WidgetRenderer.astro +72 -0
  470. package/src/components/index.ts +116 -0
  471. package/src/components/marks/Link.astro +31 -0
  472. package/src/components/marks/StrikeThrough.astro +7 -0
  473. package/src/components/marks/Subscript.astro +7 -0
  474. package/src/components/marks/Superscript.astro +7 -0
  475. package/src/components/marks/Underline.astro +7 -0
  476. package/src/components/widgets/Archives.astro +65 -0
  477. package/src/components/widgets/Categories.astro +35 -0
  478. package/src/components/widgets/RecentPosts.astro +51 -0
  479. package/src/components/widgets/Search.astro +18 -0
  480. package/src/components/widgets/Tags.astro +38 -0
  481. package/src/content/converters/index.ts +9 -0
  482. package/src/content/converters/portable-text-to-prosemirror.ts +385 -0
  483. package/src/content/converters/prosemirror-to-portable-text.ts +413 -0
  484. package/src/content/converters/types.ts +120 -0
  485. package/src/content/index.ts +5 -0
  486. package/src/database/connection.ts +67 -0
  487. package/src/database/dialect-helpers.ts +138 -0
  488. package/src/database/index.ts +5 -0
  489. package/src/database/migrations/001_initial.ts +170 -0
  490. package/src/database/migrations/002_media_status.ts +26 -0
  491. package/src/database/migrations/003_schema_registry.ts +79 -0
  492. package/src/database/migrations/004_plugins.ts +62 -0
  493. package/src/database/migrations/005_menus.ts +67 -0
  494. package/src/database/migrations/006_taxonomy_defs.ts +51 -0
  495. package/src/database/migrations/007_widgets.ts +42 -0
  496. package/src/database/migrations/008_auth.ts +194 -0
  497. package/src/database/migrations/009_user_disabled.ts +27 -0
  498. package/src/database/migrations/011_sections.ts +65 -0
  499. package/src/database/migrations/012_search.ts +25 -0
  500. package/src/database/migrations/013_scheduled_publishing.ts +51 -0
  501. package/src/database/migrations/014_draft_revisions.ts +72 -0
  502. package/src/database/migrations/015_indexes.ts +82 -0
  503. package/src/database/migrations/016_api_tokens.ts +89 -0
  504. package/src/database/migrations/017_authorization_codes.ts +45 -0
  505. package/src/database/migrations/018_seo.ts +56 -0
  506. package/src/database/migrations/019_i18n.ts +618 -0
  507. package/src/database/migrations/020_collection_url_pattern.ts +23 -0
  508. package/src/database/migrations/021_remove_section_categories.ts +43 -0
  509. package/src/database/migrations/022_marketplace_plugin_state.ts +46 -0
  510. package/src/database/migrations/023_plugin_metadata.ts +33 -0
  511. package/src/database/migrations/024_media_placeholders.ts +32 -0
  512. package/src/database/migrations/025_oauth_clients.ts +28 -0
  513. package/src/database/migrations/026_cron_tasks.ts +49 -0
  514. package/src/database/migrations/027_comments.ts +87 -0
  515. package/src/database/migrations/028_drop_author_url.ts +9 -0
  516. package/src/database/migrations/029_redirects.ts +67 -0
  517. package/src/database/migrations/030_widen_scheduled_index.ts +48 -0
  518. package/src/database/migrations/031_bylines.ts +90 -0
  519. package/src/database/migrations/032_rate_limits.ts +39 -0
  520. package/src/database/migrations/runner.ts +170 -0
  521. package/src/database/repositories/audit.ts +294 -0
  522. package/src/database/repositories/byline.ts +387 -0
  523. package/src/database/repositories/comment.ts +458 -0
  524. package/src/database/repositories/content.ts +1144 -0
  525. package/src/database/repositories/index.ts +30 -0
  526. package/src/database/repositories/media.ts +347 -0
  527. package/src/database/repositories/options.ts +150 -0
  528. package/src/database/repositories/plugin-storage.ts +373 -0
  529. package/src/database/repositories/redirect.ts +480 -0
  530. package/src/database/repositories/revision.ts +200 -0
  531. package/src/database/repositories/seo.ts +176 -0
  532. package/src/database/repositories/taxonomy.ts +294 -0
  533. package/src/database/repositories/types.ts +132 -0
  534. package/src/database/repositories/user.ts +258 -0
  535. package/src/database/transaction.ts +54 -0
  536. package/src/database/types.ts +501 -0
  537. package/src/database/validate.ts +138 -0
  538. package/src/db/adapters.ts +125 -0
  539. package/src/db/index.ts +37 -0
  540. package/src/db/libsql.ts +23 -0
  541. package/src/db/postgres.ts +30 -0
  542. package/src/db/sqlite.ts +27 -0
  543. package/src/emdash-runtime.ts +2096 -0
  544. package/src/fields/boolean.ts +34 -0
  545. package/src/fields/datetime.ts +44 -0
  546. package/src/fields/file.ts +41 -0
  547. package/src/fields/image.ts +34 -0
  548. package/src/fields/index.ts +42 -0
  549. package/src/fields/integer.ts +50 -0
  550. package/src/fields/json.ts +37 -0
  551. package/src/fields/multiselect.ts +48 -0
  552. package/src/fields/number.ts +52 -0
  553. package/src/fields/portable-text.ts +33 -0
  554. package/src/fields/reference.ts +29 -0
  555. package/src/fields/richtext.ts +31 -0
  556. package/src/fields/select.ts +46 -0
  557. package/src/fields/slug.ts +38 -0
  558. package/src/fields/text.ts +55 -0
  559. package/src/fields/textarea.ts +52 -0
  560. package/src/fields/types.ts +64 -0
  561. package/src/i18n/config.ts +68 -0
  562. package/src/import/index.ts +90 -0
  563. package/src/import/menus.ts +436 -0
  564. package/src/import/registry.ts +111 -0
  565. package/src/import/sections.ts +103 -0
  566. package/src/import/settings.ts +281 -0
  567. package/src/import/sources/wordpress-plugin.ts +641 -0
  568. package/src/import/sources/wordpress-rest.ts +191 -0
  569. package/src/import/sources/wxr.ts +330 -0
  570. package/src/import/ssrf.ts +260 -0
  571. package/src/import/types.ts +418 -0
  572. package/src/import/utils.ts +412 -0
  573. package/src/index.ts +481 -0
  574. package/src/loader.ts +770 -0
  575. package/src/mcp/server.ts +1463 -0
  576. package/src/media/index.ts +32 -0
  577. package/src/media/local-runtime.ts +213 -0
  578. package/src/media/local.ts +46 -0
  579. package/src/media/normalize.ts +190 -0
  580. package/src/media/placeholder.ts +150 -0
  581. package/src/media/provider-loader.ts +78 -0
  582. package/src/media/types.ts +279 -0
  583. package/src/menus/index.ts +324 -0
  584. package/src/menus/types.ts +112 -0
  585. package/src/page/context.ts +93 -0
  586. package/src/page/fragments.ts +89 -0
  587. package/src/page/index.ts +58 -0
  588. package/src/page/jsonld.ts +94 -0
  589. package/src/page/metadata.ts +185 -0
  590. package/src/page/seo-contributions.ts +136 -0
  591. package/src/plugin-utils.ts +80 -0
  592. package/src/plugins/adapt-sandbox-entry.ts +207 -0
  593. package/src/plugins/context.ts +833 -0
  594. package/src/plugins/cron.ts +361 -0
  595. package/src/plugins/define-plugin.ts +259 -0
  596. package/src/plugins/email-console.ts +73 -0
  597. package/src/plugins/email.ts +209 -0
  598. package/src/plugins/hooks.ts +1273 -0
  599. package/src/plugins/index.ts +193 -0
  600. package/src/plugins/manager.ts +595 -0
  601. package/src/plugins/manifest-schema.ts +230 -0
  602. package/src/plugins/marketplace.ts +460 -0
  603. package/src/plugins/request-meta.ts +139 -0
  604. package/src/plugins/routes.ts +302 -0
  605. package/src/plugins/sandbox/index.ts +18 -0
  606. package/src/plugins/sandbox/noop.ts +76 -0
  607. package/src/plugins/sandbox/types.ts +173 -0
  608. package/src/plugins/scheduler/node.ts +122 -0
  609. package/src/plugins/scheduler/piggyback.ts +71 -0
  610. package/src/plugins/scheduler/types.ts +27 -0
  611. package/src/plugins/state.ts +208 -0
  612. package/src/plugins/storage-indexes.ts +326 -0
  613. package/src/plugins/storage-query.ts +240 -0
  614. package/src/plugins/types.ts +1284 -0
  615. package/src/preview/helpers.ts +27 -0
  616. package/src/preview/index.ts +40 -0
  617. package/src/preview/tokens.ts +279 -0
  618. package/src/preview/urls.ts +118 -0
  619. package/src/query.ts +674 -0
  620. package/src/redirects/patterns.ts +224 -0
  621. package/src/request-context.ts +67 -0
  622. package/src/runtime.ts +21 -0
  623. package/src/schema/index.ts +29 -0
  624. package/src/schema/query.ts +44 -0
  625. package/src/schema/registry.ts +965 -0
  626. package/src/schema/types.ts +276 -0
  627. package/src/schema/zod-generator.ts +413 -0
  628. package/src/search/fts-manager.ts +452 -0
  629. package/src/search/index.ts +26 -0
  630. package/src/search/query.ts +396 -0
  631. package/src/search/text-extraction.ts +162 -0
  632. package/src/search/types.ts +114 -0
  633. package/src/sections/index.ts +226 -0
  634. package/src/sections/types.ts +86 -0
  635. package/src/seed/apply.ts +1141 -0
  636. package/src/seed/default.ts +86 -0
  637. package/src/seed/index.ts +28 -0
  638. package/src/seed/load.ts +35 -0
  639. package/src/seed/types.ts +341 -0
  640. package/src/seed/validate.ts +642 -0
  641. package/src/seo/index.ts +179 -0
  642. package/src/settings/index.ts +203 -0
  643. package/src/settings/types.ts +58 -0
  644. package/src/storage/index.ts +28 -0
  645. package/src/storage/local.ts +249 -0
  646. package/src/storage/s3.ts +263 -0
  647. package/src/storage/types.ts +204 -0
  648. package/src/taxonomies/index.ts +309 -0
  649. package/src/taxonomies/types.ts +61 -0
  650. package/src/ui.ts +75 -0
  651. package/src/utils/base64.ts +73 -0
  652. package/src/utils/hash.ts +36 -0
  653. package/src/utils/sanitize.ts +20 -0
  654. package/src/utils/slugify.ts +29 -0
  655. package/src/utils/url.ts +48 -0
  656. package/src/virtual-modules.d.ts +111 -0
  657. package/src/visual-editing/editable.ts +108 -0
  658. package/src/visual-editing/toolbar.ts +1229 -0
  659. package/src/widgets/components.ts +105 -0
  660. package/src/widgets/index.ts +131 -0
  661. package/src/widgets/types.ts +81 -0
@@ -0,0 +1,1905 @@
1
+ /**
2
+ * Self-contained inline Portable Text editor for visual editing.
3
+ *
4
+ * Uses TipTap directly with content extensions — no admin UI deps.
5
+ * Includes BubbleMenu for inline formatting (bold, italic, etc.)
6
+ * but no toolbar, no media picker, no section picker.
7
+ *
8
+ * Converts between Portable Text and ProseMirror on mount/save.
9
+ * Auto-saves on blur, dispatches custom events for toolbar integration.
10
+ */
11
+
12
+ import { autoUpdate, flip, offset, shift, useFloating } from "@floating-ui/react";
13
+ import { Extension, type JSONContent, type Range } from "@tiptap/core";
14
+ import Focus from "@tiptap/extension-focus";
15
+ import Image from "@tiptap/extension-image";
16
+ import Link from "@tiptap/extension-link";
17
+ import Placeholder from "@tiptap/extension-placeholder";
18
+ import TextAlign from "@tiptap/extension-text-align";
19
+ import Typography from "@tiptap/extension-typography";
20
+ import Underline from "@tiptap/extension-underline";
21
+ import { useEditor, EditorContent, type Editor } from "@tiptap/react";
22
+ import { BubbleMenu } from "@tiptap/react/menus";
23
+ import StarterKit from "@tiptap/starter-kit";
24
+ import Suggestion from "@tiptap/suggestion";
25
+ import * as React from "react";
26
+ import { createPortal } from "react-dom";
27
+
28
+ // ── Portable Text types ────────────────────────────────────────────
29
+
30
+ interface PTSpan {
31
+ _type: "span";
32
+ _key: string;
33
+ text: string;
34
+ marks?: string[];
35
+ }
36
+
37
+ interface PTMarkDef {
38
+ _type: string;
39
+ _key: string;
40
+ [key: string]: unknown;
41
+ }
42
+
43
+ interface PTTextBlock {
44
+ _type: "block";
45
+ _key: string;
46
+ style?: "normal" | "h1" | "h2" | "h3" | "h4" | "h5" | "h6" | "blockquote";
47
+ listItem?: "bullet" | "number";
48
+ level?: number;
49
+ children: PTSpan[];
50
+ markDefs?: PTMarkDef[];
51
+ }
52
+
53
+ type PTBlock = PTTextBlock | { _type: string; _key: string; [key: string]: unknown };
54
+
55
+ /** Type guard for PTTextBlock */
56
+ function isPTTextBlock(block: PTBlock): block is PTTextBlock {
57
+ return block._type === "block";
58
+ }
59
+
60
+ /** Type guard for ProseMirror JSON document node */
61
+ function isPMNode(value: unknown): value is PMNode {
62
+ return (
63
+ typeof value === "object" && value !== null && "type" in value && typeof value.type === "string"
64
+ );
65
+ }
66
+
67
+ // ── Helpers ────────────────────────────────────────────────────────
68
+
69
+ function k(): string {
70
+ return Math.random().toString(36).substring(2, 11);
71
+ }
72
+
73
+ // ── ProseMirror → Portable Text ────────────────────────────────────
74
+
75
+ type PMNode = {
76
+ type: string;
77
+ attrs?: Record<string, unknown>;
78
+ content?: PMNode[];
79
+ marks?: Array<{ type: string; attrs?: Record<string, unknown> }>;
80
+ text?: string;
81
+ };
82
+
83
+ /** Safely extract a string attribute from ProseMirror attrs */
84
+ function attrStr(attrs: Record<string, unknown> | undefined, key: string): string {
85
+ const v = attrs?.[key];
86
+ return typeof v === "string" ? v : "";
87
+ }
88
+
89
+ /** Safely extract an optional string attribute from ProseMirror attrs */
90
+ function attrStrOpt(attrs: Record<string, unknown> | undefined, key: string): string | undefined {
91
+ const v = attrs?.[key];
92
+ return typeof v === "string" ? v : undefined;
93
+ }
94
+
95
+ /** Safely extract a number attribute from ProseMirror attrs */
96
+ function attrNum(attrs: Record<string, unknown> | undefined, key: string): number | undefined {
97
+ const v = attrs?.[key];
98
+ return typeof v === "number" ? v : undefined;
99
+ }
100
+
101
+ function pmToPortableText(doc: PMNode): PTBlock[] {
102
+ if (!doc || doc.type !== "doc" || !doc.content) return [];
103
+ const blocks: PTBlock[] = [];
104
+ for (const node of doc.content) {
105
+ const r = convertPMNode(node);
106
+ if (r) {
107
+ if (Array.isArray(r)) blocks.push(...r);
108
+ else blocks.push(r);
109
+ }
110
+ }
111
+ return blocks;
112
+ }
113
+
114
+ function convertPMNode(node: PMNode): PTBlock | PTBlock[] | null {
115
+ switch (node.type) {
116
+ case "paragraph": {
117
+ const { children, markDefs } = convertInline(node.content || []);
118
+ if (children.length === 0) return null;
119
+ return {
120
+ _type: "block",
121
+ _key: k(),
122
+ style: "normal",
123
+ children,
124
+ markDefs: markDefs.length > 0 ? markDefs : undefined,
125
+ };
126
+ }
127
+ case "heading": {
128
+ const { children, markDefs } = convertInline(node.content || []);
129
+ const level = attrNum(node.attrs, "level") ?? 1;
130
+ if (children.length === 0) return null;
131
+ const headingStyles: Record<number, PTTextBlock["style"]> = {
132
+ 1: "h1",
133
+ 2: "h2",
134
+ 3: "h3",
135
+ 4: "h4",
136
+ 5: "h5",
137
+ 6: "h6",
138
+ };
139
+ const headingStyle = headingStyles[level] ?? "h1";
140
+ return {
141
+ _type: "block",
142
+ _key: k(),
143
+ style: headingStyle,
144
+ children,
145
+ markDefs: markDefs.length > 0 ? markDefs : undefined,
146
+ };
147
+ }
148
+ case "bulletList":
149
+ return convertPMList(node.content || [], "bullet");
150
+ case "orderedList":
151
+ return convertPMList(node.content || [], "number");
152
+ case "blockquote": {
153
+ const blocks: PTTextBlock[] = [];
154
+ for (const child of node.content || []) {
155
+ if (child.type === "paragraph") {
156
+ const { children, markDefs } = convertInline(child.content || []);
157
+ if (children.length > 0) {
158
+ blocks.push({
159
+ _type: "block",
160
+ _key: k(),
161
+ style: "blockquote",
162
+ children,
163
+ markDefs: markDefs.length > 0 ? markDefs : undefined,
164
+ });
165
+ }
166
+ }
167
+ }
168
+ if (blocks.length === 1) {
169
+ const first = blocks[0];
170
+ return first ?? null;
171
+ }
172
+ return blocks.length > 0 ? blocks : null;
173
+ }
174
+ case "codeBlock": {
175
+ const code = (node.content || []).map((n) => n.text || "").join("");
176
+ return {
177
+ _type: "code",
178
+ _key: k(),
179
+ code,
180
+ language: attrStrOpt(node.attrs, "language"),
181
+ };
182
+ }
183
+ case "image": {
184
+ const provider = attrStrOpt(node.attrs, "provider");
185
+ return {
186
+ _type: "image",
187
+ _key: k(),
188
+ asset: {
189
+ _ref: attrStr(node.attrs, "mediaId"),
190
+ url: attrStr(node.attrs, "src"),
191
+ provider: provider && provider !== "local" ? provider : undefined,
192
+ },
193
+ alt: attrStrOpt(node.attrs, "alt"),
194
+ caption: attrStrOpt(node.attrs, "caption") ?? attrStrOpt(node.attrs, "title"),
195
+ width: attrNum(node.attrs, "width"),
196
+ height: attrNum(node.attrs, "height"),
197
+ displayWidth: attrNum(node.attrs, "displayWidth"),
198
+ displayHeight: attrNum(node.attrs, "displayHeight"),
199
+ };
200
+ }
201
+ case "horizontalRule":
202
+ return { _type: "break", _key: k(), style: "lineBreak" };
203
+ case "pluginBlock":
204
+ return {
205
+ _type: attrStr(node.attrs, "blockType") || "embed",
206
+ _key: k(),
207
+ id: attrStr(node.attrs, "id"),
208
+ };
209
+ default:
210
+ return null;
211
+ }
212
+ }
213
+
214
+ function convertPMList(items: PMNode[], listItem: "bullet" | "number"): PTTextBlock[] {
215
+ const blocks: PTTextBlock[] = [];
216
+ for (const item of items) {
217
+ if (item.type === "listItem") {
218
+ for (const child of item.content || []) {
219
+ if (child.type === "paragraph") {
220
+ const { children, markDefs } = convertInline(child.content || []);
221
+ if (children.length > 0) {
222
+ blocks.push({
223
+ _type: "block",
224
+ _key: k(),
225
+ style: "normal",
226
+ listItem,
227
+ level: 1,
228
+ children,
229
+ markDefs: markDefs.length > 0 ? markDefs : undefined,
230
+ });
231
+ }
232
+ }
233
+ }
234
+ }
235
+ }
236
+ return blocks;
237
+ }
238
+
239
+ function convertInline(nodes: PMNode[]): { children: PTSpan[]; markDefs: PTMarkDef[] } {
240
+ const children: PTSpan[] = [];
241
+ const markDefs: PTMarkDef[] = [];
242
+ const markDefMap = new Map<string, string>();
243
+
244
+ for (const node of nodes) {
245
+ if (node.type === "text" && node.text) {
246
+ const marks: string[] = [];
247
+ for (const mark of node.marks || []) {
248
+ const m = convertPMMark(mark, markDefs, markDefMap);
249
+ if (m) marks.push(m);
250
+ }
251
+ children.push({
252
+ _type: "span",
253
+ _key: k(),
254
+ text: node.text,
255
+ marks: marks.length > 0 ? marks : undefined,
256
+ });
257
+ } else if (node.type === "hardBreak") {
258
+ if (children.length > 0) {
259
+ const last = children.at(-1);
260
+ if (last) last.text += "\n";
261
+ } else {
262
+ children.push({ _type: "span", _key: k(), text: "\n" });
263
+ }
264
+ }
265
+ }
266
+
267
+ if (children.length === 0) {
268
+ children.push({ _type: "span", _key: k(), text: "" });
269
+ }
270
+ return { children, markDefs };
271
+ }
272
+
273
+ function convertPMMark(
274
+ mark: { type: string; attrs?: Record<string, unknown> },
275
+ markDefs: PTMarkDef[],
276
+ markDefMap: Map<string, string>,
277
+ ): string | null {
278
+ switch (mark.type) {
279
+ case "bold":
280
+ case "strong":
281
+ return "strong";
282
+ case "italic":
283
+ case "em":
284
+ return "em";
285
+ case "underline":
286
+ return "underline";
287
+ case "strike":
288
+ case "strikethrough":
289
+ return "strike-through";
290
+ case "code":
291
+ return "code";
292
+ case "link": {
293
+ const href = attrStr(mark.attrs, "href");
294
+ if (markDefMap.has(href)) return markDefMap.get(href)!;
295
+ const key = k();
296
+ markDefs.push({
297
+ _type: "link",
298
+ _key: key,
299
+ href,
300
+ blank: mark.attrs?.target === "_blank",
301
+ });
302
+ markDefMap.set(href, key);
303
+ return key;
304
+ }
305
+ default:
306
+ return mark.type;
307
+ }
308
+ }
309
+
310
+ // ── Portable Text → ProseMirror ────────────────────────────────────
311
+
312
+ function portableTextToPM(blocks: PTBlock[]): JSONContent {
313
+ if (!blocks || blocks.length === 0) return { type: "doc", content: [{ type: "paragraph" }] };
314
+
315
+ const content: JSONContent[] = [];
316
+ let i = 0;
317
+
318
+ while (i < blocks.length) {
319
+ const block = blocks[i];
320
+ if (!block) {
321
+ i++;
322
+ continue;
323
+ }
324
+ if (isPTTextBlock(block) && block.listItem) {
325
+ const listBlocks: PTTextBlock[] = [];
326
+ const listType = block.listItem;
327
+ while (i < blocks.length) {
328
+ const cur = blocks[i];
329
+ if (cur && isPTTextBlock(cur) && cur.listItem === listType) {
330
+ listBlocks.push(cur);
331
+ i++;
332
+ } else break;
333
+ }
334
+ content.push(convertPTList(listBlocks, listType));
335
+ } else {
336
+ const c = convertPTBlock(block);
337
+ if (c) content.push(c);
338
+ i++;
339
+ }
340
+ }
341
+
342
+ return { type: "doc", content: content.length > 0 ? content : [{ type: "paragraph" }] };
343
+ }
344
+
345
+ function convertPTBlock(block: PTBlock): JSONContent | null {
346
+ if (isPTTextBlock(block)) {
347
+ const { style = "normal", children, markDefs = [] } = block;
348
+ const pmContent = convertPTSpans(children, markDefs);
349
+
350
+ if (style === "blockquote") {
351
+ return {
352
+ type: "blockquote",
353
+ content: [
354
+ {
355
+ type: "paragraph",
356
+ content: pmContent.length > 0 ? pmContent : undefined,
357
+ },
358
+ ],
359
+ };
360
+ }
361
+ if (style?.startsWith("h")) {
362
+ const level = parseInt(style.substring(1), 10);
363
+ return {
364
+ type: "heading",
365
+ attrs: { level },
366
+ content: pmContent.length > 0 ? pmContent : undefined,
367
+ };
368
+ }
369
+ return {
370
+ type: "paragraph",
371
+ content: pmContent.length > 0 ? pmContent : undefined,
372
+ };
373
+ }
374
+ if (block._type === "code") {
375
+ const cb = block as PTBlock & { code?: string; language?: string };
376
+ return {
377
+ type: "codeBlock",
378
+ attrs: { language: cb.language || null },
379
+ content: cb.code ? [{ type: "text", text: cb.code }] : undefined,
380
+ };
381
+ }
382
+ if (block._type === "break") {
383
+ return { type: "horizontalRule" };
384
+ }
385
+ if (block._type === "image") {
386
+ const ib = block as PTBlock & {
387
+ asset?: { _ref?: string; url?: string; provider?: string };
388
+ alt?: string;
389
+ caption?: string;
390
+ width?: number;
391
+ height?: number;
392
+ displayWidth?: number;
393
+ displayHeight?: number;
394
+ };
395
+ return {
396
+ type: "image",
397
+ attrs: {
398
+ src: ib.asset?.url || `/_emdash/api/media/file/${ib.asset?._ref}`,
399
+ alt: ib.alt || "",
400
+ title: ib.caption || "",
401
+ caption: ib.caption || "",
402
+ mediaId: ib.asset?._ref,
403
+ provider: ib.asset?.provider,
404
+ width: ib.width,
405
+ height: ib.height,
406
+ displayWidth: ib.displayWidth,
407
+ displayHeight: ib.displayHeight,
408
+ },
409
+ };
410
+ }
411
+ // Unknown block types — treat as plugin blocks if they have an id
412
+ const embedBlock = block as { _type: string; url?: string; id?: string };
413
+ if (embedBlock.id || embedBlock.url) {
414
+ return {
415
+ type: "pluginBlock",
416
+ attrs: {
417
+ blockType: block._type,
418
+ id: embedBlock.id || embedBlock.url || "",
419
+ },
420
+ };
421
+ }
422
+ // Truly unknown — render as code-marked text
423
+ return {
424
+ type: "paragraph",
425
+ content: [{ type: "text", text: `[${block._type}]`, marks: [{ type: "code" }] }],
426
+ };
427
+ }
428
+
429
+ function convertPTList(items: PTTextBlock[], listType: "bullet" | "number"): JSONContent {
430
+ return {
431
+ type: listType === "bullet" ? "bulletList" : "orderedList",
432
+ content: items.map((item) => ({
433
+ type: "listItem",
434
+ content: [
435
+ {
436
+ type: "paragraph",
437
+ content: convertPTSpans(item.children, item.markDefs || []),
438
+ },
439
+ ],
440
+ })),
441
+ };
442
+ }
443
+
444
+ function convertPTSpans(spans: PTSpan[], markDefs: PTMarkDef[]): JSONContent[] {
445
+ const nodes: JSONContent[] = [];
446
+ const mdMap = new Map(markDefs.map((md) => [md._key, md]));
447
+
448
+ for (const span of spans) {
449
+ if (span._type !== "span") continue;
450
+ const parts = span.text.split("\n");
451
+ for (let i = 0; i < parts.length; i++) {
452
+ const text = parts[i];
453
+ if (text && text.length > 0) {
454
+ const marks = convertPTMarks(span.marks || [], mdMap);
455
+ const node: JSONContent = {
456
+ type: "text",
457
+ text,
458
+ };
459
+ if (marks.length > 0) node.marks = marks;
460
+ nodes.push(node);
461
+ }
462
+ if (i < parts.length - 1) nodes.push({ type: "hardBreak" });
463
+ }
464
+ }
465
+ return nodes;
466
+ }
467
+
468
+ type MarkJSON = { type: string; attrs?: Record<string, unknown>; [key: string]: unknown };
469
+
470
+ function convertPTMarks(marks: string[], markDefs: Map<string, PTMarkDef>): MarkJSON[] {
471
+ const pm: MarkJSON[] = [];
472
+ for (const mark of marks) {
473
+ switch (mark) {
474
+ case "strong":
475
+ pm.push({ type: "bold" });
476
+ break;
477
+ case "em":
478
+ pm.push({ type: "italic" });
479
+ break;
480
+ case "underline":
481
+ pm.push({ type: "underline" });
482
+ break;
483
+ case "strike-through":
484
+ pm.push({ type: "strike" });
485
+ break;
486
+ case "code":
487
+ pm.push({ type: "code" });
488
+ break;
489
+ default: {
490
+ const md = markDefs.get(mark);
491
+ if (md && md._type === "link") {
492
+ pm.push({
493
+ type: "link",
494
+ attrs: { href: md.href, target: md.blank ? "_blank" : null },
495
+ });
496
+ }
497
+ break;
498
+ }
499
+ }
500
+ }
501
+ return pm;
502
+ }
503
+
504
+ // ── Inline BubbleMenu ──────────────────────────────────────────────
505
+
506
+ function InlineBubbleMenu({ editor }: { editor: Editor }) {
507
+ const [showLinkInput, setShowLinkInput] = React.useState(false);
508
+ const [linkUrl, setLinkUrl] = React.useState("");
509
+ const inputRef = React.useRef<HTMLInputElement>(null);
510
+
511
+ React.useEffect(() => {
512
+ if (showLinkInput) {
513
+ const existingUrl = editor.getAttributes("link").href || "";
514
+ setLinkUrl(existingUrl);
515
+ setTimeout(() => inputRef.current?.focus(), 0);
516
+ }
517
+ }, [showLinkInput, editor]);
518
+
519
+ const handleSetLink = () => {
520
+ if (linkUrl.trim() === "") {
521
+ editor.chain().focus().extendMarkRange("link").unsetLink().run();
522
+ } else {
523
+ editor.chain().focus().extendMarkRange("link").setLink({ href: linkUrl.trim() }).run();
524
+ }
525
+ setShowLinkInput(false);
526
+ setLinkUrl("");
527
+ };
528
+
529
+ const handleRemoveLink = () => {
530
+ editor.chain().focus().extendMarkRange("link").unsetLink().run();
531
+ setShowLinkInput(false);
532
+ setLinkUrl("");
533
+ };
534
+
535
+ const handleKeyDown = (e: React.KeyboardEvent) => {
536
+ if (e.key === "Enter") {
537
+ e.preventDefault();
538
+ handleSetLink();
539
+ } else if (e.key === "Escape") {
540
+ setShowLinkInput(false);
541
+ setLinkUrl("");
542
+ editor.commands.focus();
543
+ }
544
+ };
545
+
546
+ return (
547
+ <BubbleMenu
548
+ editor={editor}
549
+ options={{ placement: "top", offset: 8, flip: true, shift: true }}
550
+ className="emdash-bubble-menu"
551
+ >
552
+ {showLinkInput ? (
553
+ <div style={{ display: "flex", alignItems: "center", gap: "4px" }}>
554
+ <input
555
+ ref={inputRef}
556
+ type="url"
557
+ placeholder="https://..."
558
+ value={linkUrl}
559
+ onChange={(e) => setLinkUrl(e.target.value)}
560
+ onKeyDown={handleKeyDown}
561
+ className="emdash-bubble-link-input"
562
+ />
563
+ <button
564
+ type="button"
565
+ className="emdash-bubble-btn"
566
+ onClick={handleSetLink}
567
+ title="Apply link"
568
+ >
569
+
570
+ </button>
571
+ {editor.isActive("link") && (
572
+ <button
573
+ type="button"
574
+ className="emdash-bubble-btn emdash-bubble-btn--danger"
575
+ onClick={handleRemoveLink}
576
+ title="Remove link"
577
+ >
578
+
579
+ </button>
580
+ )}
581
+ </div>
582
+ ) : (
583
+ <>
584
+ <button
585
+ type="button"
586
+ className={`emdash-bubble-btn ${editor.isActive("bold") ? "emdash-bubble-btn--active" : ""}`}
587
+ onClick={() => editor.chain().focus().toggleBold().run()}
588
+ title="Bold"
589
+ >
590
+ <strong>B</strong>
591
+ </button>
592
+ <button
593
+ type="button"
594
+ className={`emdash-bubble-btn ${editor.isActive("italic") ? "emdash-bubble-btn--active" : ""}`}
595
+ onClick={() => editor.chain().focus().toggleItalic().run()}
596
+ title="Italic"
597
+ >
598
+ <em>I</em>
599
+ </button>
600
+ <button
601
+ type="button"
602
+ className={`emdash-bubble-btn ${editor.isActive("underline") ? "emdash-bubble-btn--active" : ""}`}
603
+ onClick={() => editor.chain().focus().toggleUnderline().run()}
604
+ title="Underline"
605
+ >
606
+ <span style={{ textDecoration: "underline" }}>U</span>
607
+ </button>
608
+ <button
609
+ type="button"
610
+ className={`emdash-bubble-btn ${editor.isActive("strike") ? "emdash-bubble-btn--active" : ""}`}
611
+ onClick={() => editor.chain().focus().toggleStrike().run()}
612
+ title="Strikethrough"
613
+ >
614
+ <span style={{ textDecoration: "line-through" }}>S</span>
615
+ </button>
616
+ <button
617
+ type="button"
618
+ className={`emdash-bubble-btn ${editor.isActive("code") ? "emdash-bubble-btn--active" : ""}`}
619
+ onClick={() => editor.chain().focus().toggleCode().run()}
620
+ title="Code"
621
+ >
622
+ <span style={{ fontFamily: "monospace", fontSize: "13px" }}>&lt;/&gt;</span>
623
+ </button>
624
+ <span className="emdash-bubble-divider" />
625
+ <button
626
+ type="button"
627
+ className={`emdash-bubble-btn ${editor.isActive("link") ? "emdash-bubble-btn--active" : ""}`}
628
+ onClick={() => setShowLinkInput(true)}
629
+ title={editor.isActive("link") ? "Edit link" : "Add link"}
630
+ >
631
+ 🔗
632
+ </button>
633
+ </>
634
+ )}
635
+ </BubbleMenu>
636
+ );
637
+ }
638
+
639
+ // ── Slash Menu ──────────────────────────────────────────────────────
640
+
641
+ interface SlashCommandItem {
642
+ id: string;
643
+ title: string;
644
+ description: string;
645
+ icon: string;
646
+ command: (props: { editor: Editor; range: Range }) => void;
647
+ aliases?: string[];
648
+ }
649
+
650
+ const slashCommands: SlashCommandItem[] = [
651
+ {
652
+ id: "heading1",
653
+ title: "Heading 1",
654
+ description: "Large section heading",
655
+ icon: "H1",
656
+ aliases: ["h1", "title"],
657
+ command: ({ editor, range }) => {
658
+ editor.chain().focus().deleteRange(range).setNode("heading", { level: 1 }).run();
659
+ },
660
+ },
661
+ {
662
+ id: "heading2",
663
+ title: "Heading 2",
664
+ description: "Medium section heading",
665
+ icon: "H2",
666
+ aliases: ["h2", "subtitle"],
667
+ command: ({ editor, range }) => {
668
+ editor.chain().focus().deleteRange(range).setNode("heading", { level: 2 }).run();
669
+ },
670
+ },
671
+ {
672
+ id: "heading3",
673
+ title: "Heading 3",
674
+ description: "Small section heading",
675
+ icon: "H3",
676
+ aliases: ["h3"],
677
+ command: ({ editor, range }) => {
678
+ editor.chain().focus().deleteRange(range).setNode("heading", { level: 3 }).run();
679
+ },
680
+ },
681
+ {
682
+ id: "bulletList",
683
+ title: "Bullet List",
684
+ description: "Create a bullet list",
685
+ icon: "•",
686
+ aliases: ["ul", "unordered"],
687
+ command: ({ editor, range }) => {
688
+ editor.chain().focus().deleteRange(range).toggleBulletList().run();
689
+ },
690
+ },
691
+ {
692
+ id: "numberedList",
693
+ title: "Numbered List",
694
+ description: "Create a numbered list",
695
+ icon: "1.",
696
+ aliases: ["ol", "ordered"],
697
+ command: ({ editor, range }) => {
698
+ editor.chain().focus().deleteRange(range).toggleOrderedList().run();
699
+ },
700
+ },
701
+ {
702
+ id: "quote",
703
+ title: "Quote",
704
+ description: "Insert a blockquote",
705
+ icon: "\u201C",
706
+ aliases: ["blockquote", "cite"],
707
+ command: ({ editor, range }) => {
708
+ editor.chain().focus().deleteRange(range).toggleBlockquote().run();
709
+ },
710
+ },
711
+ {
712
+ id: "codeBlock",
713
+ title: "Code Block",
714
+ description: "Insert a code block",
715
+ icon: "</>",
716
+ aliases: ["code", "pre", "```"],
717
+ command: ({ editor, range }) => {
718
+ editor.chain().focus().deleteRange(range).toggleCodeBlock().run();
719
+ },
720
+ },
721
+ {
722
+ id: "divider",
723
+ title: "Divider",
724
+ description: "Insert a horizontal rule",
725
+ icon: "—",
726
+ aliases: ["hr", "---", "separator"],
727
+ command: ({ editor, range }) => {
728
+ editor.chain().focus().deleteRange(range).setHorizontalRule().run();
729
+ },
730
+ },
731
+ {
732
+ id: "image",
733
+ title: "Image",
734
+ description: "Insert an image",
735
+ icon: "🖼",
736
+ aliases: ["img", "photo", "picture"],
737
+ command: ({ editor, range }) => {
738
+ editor.chain().focus().deleteRange(range).run();
739
+ // Signal the component to open the media picker
740
+ document.dispatchEvent(new CustomEvent("emdash:open-media-picker"));
741
+ },
742
+ },
743
+ ];
744
+
745
+ interface SlashMenuState {
746
+ isOpen: boolean;
747
+ items: SlashCommandItem[];
748
+ selectedIndex: number;
749
+ clientRect: (() => DOMRect | null) | null;
750
+ range: Range | null;
751
+ }
752
+
753
+ const initialSlashMenuState: SlashMenuState = {
754
+ isOpen: false,
755
+ items: [],
756
+ selectedIndex: 0,
757
+ clientRect: null,
758
+ range: null,
759
+ };
760
+
761
+ function createSlashCommandsExtension(options: {
762
+ filterCommands: (query: string) => SlashCommandItem[];
763
+ onStateChange: React.Dispatch<React.SetStateAction<SlashMenuState>>;
764
+ getState: () => SlashMenuState;
765
+ }) {
766
+ const { filterCommands, onStateChange, getState } = options;
767
+
768
+ return Extension.create({
769
+ name: "slashCommands",
770
+
771
+ addProseMirrorPlugins() {
772
+ return [
773
+ Suggestion({
774
+ editor: this.editor,
775
+ char: "/",
776
+ startOfLine: true,
777
+ command: ({ editor, range, props }) => {
778
+ const item: unknown = props;
779
+ if (
780
+ typeof item === "object" &&
781
+ item !== null &&
782
+ "command" in item &&
783
+ typeof item.command === "function"
784
+ ) {
785
+ item.command({ editor, range });
786
+ }
787
+ },
788
+ items: ({ query }) => filterCommands(query),
789
+ render: () => {
790
+ return {
791
+ onStart: (props) => {
792
+ onStateChange({
793
+ isOpen: true,
794
+ items: props.items,
795
+ selectedIndex: 0,
796
+ clientRect: props.clientRect ?? null,
797
+ range: props.range,
798
+ });
799
+ },
800
+ onUpdate: (props) => {
801
+ onStateChange((prev) => ({
802
+ ...prev,
803
+ items: props.items,
804
+ selectedIndex: 0,
805
+ clientRect: props.clientRect ?? null,
806
+ range: props.range,
807
+ }));
808
+ },
809
+ onKeyDown: (props) => {
810
+ if (props.event.key === "Escape") {
811
+ onStateChange((prev) => ({ ...prev, isOpen: false }));
812
+ return true;
813
+ }
814
+ if (props.event.key === "ArrowUp") {
815
+ onStateChange((prev) => ({
816
+ ...prev,
817
+ selectedIndex: (prev.selectedIndex - 1 + prev.items.length) % prev.items.length,
818
+ }));
819
+ return true;
820
+ }
821
+ if (props.event.key === "ArrowDown") {
822
+ onStateChange((prev) => ({
823
+ ...prev,
824
+ selectedIndex: (prev.selectedIndex + 1) % prev.items.length,
825
+ }));
826
+ return true;
827
+ }
828
+ if (props.event.key === "Enter") {
829
+ const state = getState();
830
+ if (state.items.length > 0 && state.range) {
831
+ const item = state.items[state.selectedIndex];
832
+ if (item) {
833
+ item.command({ editor: this.editor, range: state.range });
834
+ onStateChange((prev) => ({ ...prev, isOpen: false }));
835
+ return true;
836
+ }
837
+ }
838
+ return false;
839
+ }
840
+ return false;
841
+ },
842
+ onExit: () => {
843
+ onStateChange((prev) => ({ ...prev, isOpen: false }));
844
+ },
845
+ };
846
+ },
847
+ }),
848
+ ];
849
+ },
850
+ });
851
+ }
852
+
853
+ function InlineSlashMenu({
854
+ state,
855
+ onCommand,
856
+ setSelectedIndex,
857
+ }: {
858
+ state: SlashMenuState;
859
+ onCommand: (item: SlashCommandItem) => void;
860
+ setSelectedIndex: (index: number) => void;
861
+ }) {
862
+ const containerRef = React.useRef<HTMLDivElement>(null);
863
+
864
+ // Track whether we have a positioned reference to avoid rendering at (0,0)
865
+ const [hasReference, setHasReference] = React.useState(false);
866
+
867
+ const { refs, floatingStyles } = useFloating({
868
+ open: state.isOpen && hasReference,
869
+ placement: "bottom-start",
870
+ middleware: [offset(8), flip(), shift({ padding: 8 })],
871
+ whileElementsMounted: autoUpdate,
872
+ });
873
+
874
+ React.useEffect(() => {
875
+ if (state.clientRect) {
876
+ const clientRectFn = state.clientRect;
877
+ refs.setReference({
878
+ getBoundingClientRect: () => clientRectFn() ?? new DOMRect(),
879
+ });
880
+ setHasReference(true);
881
+ } else {
882
+ setHasReference(false);
883
+ }
884
+ }, [state.clientRect, refs]);
885
+
886
+ // Reset reference tracking when menu closes
887
+ React.useEffect(() => {
888
+ if (!state.isOpen) setHasReference(false);
889
+ }, [state.isOpen]);
890
+
891
+ React.useEffect(() => {
892
+ if (!state.isOpen || !hasReference) return;
893
+ const container = containerRef.current;
894
+ if (!container) return;
895
+ const selected = container.querySelector(`[data-index="${state.selectedIndex}"]`);
896
+ if (selected instanceof HTMLElement) {
897
+ // Use scrollIntoView only within the menu container to avoid scrolling the page
898
+ const containerTop = container.scrollTop;
899
+ const containerBottom = containerTop + container.clientHeight;
900
+ const itemTop = selected.offsetTop;
901
+ const itemBottom = itemTop + selected.offsetHeight;
902
+ if (itemTop < containerTop) {
903
+ container.scrollTop = itemTop;
904
+ } else if (itemBottom > containerBottom) {
905
+ container.scrollTop = itemBottom - container.clientHeight;
906
+ }
907
+ }
908
+ }, [state.selectedIndex, state.isOpen, hasReference]);
909
+
910
+ if (!state.isOpen || !hasReference) return null;
911
+
912
+ return createPortal(
913
+ <div
914
+ ref={(node) => {
915
+ containerRef.current = node;
916
+ refs.setFloating(node);
917
+ }}
918
+ style={{
919
+ ...floatingStyles,
920
+ zIndex: 100,
921
+ borderRadius: "8px",
922
+ border: "1px solid #d1d5db",
923
+ background: "white",
924
+ padding: "4px",
925
+ boxShadow: "0 4px 12px rgba(0, 0, 0, 0.15)",
926
+ minWidth: "220px",
927
+ maxHeight: "300px",
928
+ overflowY: "auto",
929
+ }}
930
+ className="emdash-slash-menu"
931
+ >
932
+ {state.items.length === 0 ? (
933
+ <p
934
+ style={{
935
+ padding: "8px 12px",
936
+ fontSize: "13px",
937
+ color: "#9ca3af",
938
+ margin: 0,
939
+ }}
940
+ >
941
+ No results
942
+ </p>
943
+ ) : (
944
+ state.items.map((item, index) => (
945
+ <button
946
+ key={item.id}
947
+ type="button"
948
+ data-index={index}
949
+ style={{
950
+ display: "flex",
951
+ alignItems: "center",
952
+ gap: "12px",
953
+ width: "100%",
954
+ padding: "8px 12px",
955
+ fontSize: "13px",
956
+ borderRadius: "4px",
957
+ border: "none",
958
+ textAlign: "left",
959
+ cursor: "pointer",
960
+ background: index === state.selectedIndex ? "#f3f4f6" : "transparent",
961
+ }}
962
+ onClick={() => onCommand(item)}
963
+ onMouseEnter={() => setSelectedIndex(index)}
964
+ >
965
+ <span
966
+ style={{
967
+ width: "24px",
968
+ height: "24px",
969
+ display: "flex",
970
+ alignItems: "center",
971
+ justifyContent: "center",
972
+ flexShrink: 0,
973
+ fontSize: "14px",
974
+ fontWeight: 600,
975
+ color: "#6b7280",
976
+ background: "#f3f4f6",
977
+ borderRadius: "4px",
978
+ }}
979
+ >
980
+ {item.icon}
981
+ </span>
982
+ <span style={{ display: "flex", flexDirection: "column" }}>
983
+ <span style={{ fontWeight: 500 }}>{item.title}</span>
984
+ <span style={{ fontSize: "12px", color: "#9ca3af" }}>{item.description}</span>
985
+ </span>
986
+ </button>
987
+ ))
988
+ )}
989
+ </div>,
990
+ document.body,
991
+ );
992
+ }
993
+
994
+ // ── Media Picker ───────────────────────────────────────────────────
995
+
996
+ interface MediaItemData {
997
+ id: string;
998
+ filename: string;
999
+ mimeType: string;
1000
+ url: string;
1001
+ storageKey?: string;
1002
+ width?: number;
1003
+ height?: number;
1004
+ alt?: string;
1005
+ provider?: string;
1006
+ previewUrl?: string;
1007
+ meta?: Record<string, unknown>;
1008
+ }
1009
+
1010
+ interface ProviderInfo {
1011
+ id: string;
1012
+ name: string;
1013
+ icon?: string;
1014
+ capabilities: { browse: boolean; search: boolean; upload: boolean; delete: boolean };
1015
+ }
1016
+
1017
+ const API_BASE = "/_emdash/api";
1018
+
1019
+ async function ecFetch(url: string, init?: RequestInit): Promise<Response> {
1020
+ const base = new Headers(init?.headers);
1021
+ base.set("X-EmDash-Request", "1");
1022
+ return fetch(url, {
1023
+ credentials: "same-origin",
1024
+ ...init,
1025
+ headers: base,
1026
+ });
1027
+ }
1028
+
1029
+ function InlineMediaPicker({
1030
+ open,
1031
+ onClose,
1032
+ onSelect,
1033
+ }: {
1034
+ open: boolean;
1035
+ onClose: () => void;
1036
+ onSelect: (item: MediaItemData) => void;
1037
+ }) {
1038
+ const [providers, setProviders] = React.useState<ProviderInfo[]>([]);
1039
+ const [activeProvider, setActiveProvider] = React.useState("local");
1040
+ const [items, setItems] = React.useState<MediaItemData[]>([]);
1041
+ const [loading, setLoading] = React.useState(false);
1042
+ const [uploading, setUploading] = React.useState(false);
1043
+ const [selectedId, setSelectedId] = React.useState<string | null>(null);
1044
+ const fileInputRef = React.useRef<HTMLInputElement>(null);
1045
+
1046
+ // Fetch providers on open
1047
+ React.useEffect(() => {
1048
+ if (!open) return;
1049
+ setSelectedId(null);
1050
+ setActiveProvider("local");
1051
+ ecFetch(`${API_BASE}/media/providers`)
1052
+ .then((r) => r.json())
1053
+ .then((d) => setProviders(d.data.items ?? []))
1054
+ .catch(() => setProviders([]));
1055
+ }, [open]);
1056
+
1057
+ // Fetch items when provider changes
1058
+ React.useEffect(() => {
1059
+ if (!open) return;
1060
+ setLoading(true);
1061
+ setSelectedId(null);
1062
+
1063
+ const url =
1064
+ activeProvider === "local"
1065
+ ? `${API_BASE}/media?mimeType=image/&limit=50`
1066
+ : `${API_BASE}/media/providers/${activeProvider}?mimeType=image/&limit=50`;
1067
+
1068
+ void (async () => {
1069
+ try {
1070
+ const r = await ecFetch(url);
1071
+ const d = await r.json();
1072
+ const raw = d.data.items ?? [];
1073
+ // eslint-disable-next-line typescript-eslint(no-unsafe-type-assertion) -- API response items mapped to MediaItem shape
1074
+ const typedRaw = raw as Array<{
1075
+ id: string;
1076
+ filename?: string;
1077
+ mimeType?: string;
1078
+ url?: string;
1079
+ previewUrl?: string;
1080
+ storageKey?: string;
1081
+ width?: number;
1082
+ height?: number;
1083
+ alt?: string;
1084
+ meta?: Record<string, unknown>;
1085
+ }>;
1086
+ setItems(
1087
+ typedRaw.map((item) => ({
1088
+ id: item.id,
1089
+ filename: item.filename || "",
1090
+ mimeType: item.mimeType || "image/unknown",
1091
+ url:
1092
+ item.url ||
1093
+ item.previewUrl ||
1094
+ (item.storageKey ? `${API_BASE}/media/file/${item.storageKey}` : ""),
1095
+ storageKey: item.storageKey,
1096
+ width: item.width,
1097
+ height: item.height,
1098
+ alt: item.alt,
1099
+ provider: activeProvider === "local" ? undefined : activeProvider,
1100
+ previewUrl: item.previewUrl,
1101
+ meta: item.meta,
1102
+ })),
1103
+ );
1104
+ } catch {
1105
+ setItems([]);
1106
+ } finally {
1107
+ setLoading(false);
1108
+ }
1109
+ })();
1110
+ }, [open, activeProvider]);
1111
+
1112
+ const handleUpload = async (file: File) => {
1113
+ setUploading(true);
1114
+ try {
1115
+ // Detect dimensions
1116
+ const dims = await new Promise<{ width?: number; height?: number }>((resolve) => {
1117
+ if (!file.type.startsWith("image/")) return resolve({});
1118
+ const img = new window.Image();
1119
+ img.onload = () => {
1120
+ resolve({ width: img.naturalWidth, height: img.naturalHeight });
1121
+ URL.revokeObjectURL(img.src);
1122
+ };
1123
+ img.onerror = () => {
1124
+ resolve({});
1125
+ URL.revokeObjectURL(img.src);
1126
+ };
1127
+ img.src = URL.createObjectURL(file);
1128
+ });
1129
+
1130
+ let item: MediaItemData;
1131
+
1132
+ if (activeProvider === "local") {
1133
+ const formData = new FormData();
1134
+ formData.append("file", file);
1135
+ if (dims.width) formData.append("width", String(dims.width));
1136
+ if (dims.height) formData.append("height", String(dims.height));
1137
+ const res = await ecFetch(`${API_BASE}/media`, { method: "POST", body: formData });
1138
+ const data = await res.json();
1139
+ const unwrapped = data.data ?? data;
1140
+ if (!unwrapped.item) throw new Error("Upload failed");
1141
+ const raw = unwrapped.item;
1142
+ item = {
1143
+ id: raw.id,
1144
+ filename: raw.filename || file.name,
1145
+ mimeType: raw.mimeType || file.type,
1146
+ url: raw.url || raw.previewUrl || `${API_BASE}/media/file/${raw.storageKey}`,
1147
+ storageKey: raw.storageKey,
1148
+ width: raw.width || dims.width,
1149
+ height: raw.height || dims.height,
1150
+ alt: raw.alt,
1151
+ };
1152
+ } else {
1153
+ const formData = new FormData();
1154
+ formData.append("file", file);
1155
+ const res = await ecFetch(`${API_BASE}/media/providers/${activeProvider}`, {
1156
+ method: "POST",
1157
+ body: formData,
1158
+ });
1159
+ const data = await res.json();
1160
+ const unwrapped = data.data ?? data;
1161
+ if (!unwrapped.item) throw new Error("Upload failed");
1162
+ const raw = unwrapped.item;
1163
+ item = {
1164
+ id: raw.id,
1165
+ filename: raw.filename || file.name,
1166
+ mimeType: raw.mimeType || file.type,
1167
+ url: raw.previewUrl || "",
1168
+ width: raw.width || dims.width,
1169
+ height: raw.height || dims.height,
1170
+ alt: raw.alt,
1171
+ provider: activeProvider,
1172
+ previewUrl: raw.previewUrl,
1173
+ meta: raw.meta,
1174
+ };
1175
+ }
1176
+
1177
+ setItems((prev) => [item, ...prev]);
1178
+ setSelectedId(item.id);
1179
+ } catch (err) {
1180
+ console.error("Upload failed:", err);
1181
+ } finally {
1182
+ setUploading(false);
1183
+ }
1184
+ };
1185
+
1186
+ const handleConfirm = () => {
1187
+ const item = items.find((i) => i.id === selectedId);
1188
+ if (item) onSelect(item);
1189
+ };
1190
+
1191
+ const providerTabs = React.useMemo(() => {
1192
+ const tabs: Array<{ id: string; name: string; icon?: string }> = [
1193
+ { id: "local", name: "Library" },
1194
+ ];
1195
+ for (const p of providers) {
1196
+ if (p.id !== "local") tabs.push({ id: p.id, name: p.name, icon: p.icon });
1197
+ }
1198
+ return tabs;
1199
+ }, [providers]);
1200
+
1201
+ if (!open) return null;
1202
+
1203
+ return createPortal(
1204
+ <div
1205
+ style={{
1206
+ position: "fixed",
1207
+ inset: 0,
1208
+ zIndex: 10000,
1209
+ display: "flex",
1210
+ alignItems: "center",
1211
+ justifyContent: "center",
1212
+ background: "rgba(0,0,0,0.5)",
1213
+ fontFamily: "-apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif",
1214
+ }}
1215
+ onClick={(e) => {
1216
+ if (e.target === e.currentTarget) onClose();
1217
+ }}
1218
+ >
1219
+ <div
1220
+ style={{
1221
+ background: "white",
1222
+ borderRadius: "12px",
1223
+ width: "min(700px, 90vw)",
1224
+ maxHeight: "80vh",
1225
+ display: "flex",
1226
+ flexDirection: "column",
1227
+ overflow: "hidden",
1228
+ boxShadow: "0 20px 60px rgba(0,0,0,0.3)",
1229
+ }}
1230
+ className="emdash-media-picker"
1231
+ >
1232
+ {/* Header */}
1233
+ <div
1234
+ style={{
1235
+ display: "flex",
1236
+ alignItems: "center",
1237
+ justifyContent: "space-between",
1238
+ padding: "16px 20px",
1239
+ borderBottom: "1px solid #e5e7eb",
1240
+ }}
1241
+ >
1242
+ <span style={{ fontSize: "16px", fontWeight: 600 }}>Insert Image</span>
1243
+ <button
1244
+ type="button"
1245
+ onClick={onClose}
1246
+ style={{
1247
+ background: "none",
1248
+ border: "none",
1249
+ cursor: "pointer",
1250
+ fontSize: "18px",
1251
+ padding: "4px 8px",
1252
+ borderRadius: "4px",
1253
+ color: "#6b7280",
1254
+ }}
1255
+ aria-label="Close"
1256
+ >
1257
+
1258
+ </button>
1259
+ </div>
1260
+
1261
+ {/* Provider tabs */}
1262
+ {providerTabs.length > 1 && (
1263
+ <div
1264
+ style={{
1265
+ display: "flex",
1266
+ gap: "6px",
1267
+ padding: "12px 20px",
1268
+ borderBottom: "1px solid #e5e7eb",
1269
+ flexWrap: "wrap",
1270
+ }}
1271
+ >
1272
+ {providerTabs.map((tab) => (
1273
+ <button
1274
+ key={tab.id}
1275
+ type="button"
1276
+ onClick={() => setActiveProvider(tab.id)}
1277
+ style={{
1278
+ padding: "6px 14px",
1279
+ fontSize: "13px",
1280
+ fontWeight: 500,
1281
+ borderRadius: "6px",
1282
+ border: "none",
1283
+ cursor: "pointer",
1284
+ background: activeProvider === tab.id ? "#3b82f6" : "#f3f4f6",
1285
+ color: activeProvider === tab.id ? "white" : "#4b5563",
1286
+ }}
1287
+ >
1288
+ {tab.icon && (
1289
+ <span
1290
+ style={{ marginRight: "6px", display: "inline-flex", alignItems: "center" }}
1291
+ >
1292
+ {tab.icon.startsWith("data:") || tab.icon.startsWith("http") ? (
1293
+ <img src={tab.icon} alt="" style={{ width: "16px", height: "16px" }} />
1294
+ ) : (
1295
+ tab.icon
1296
+ )}
1297
+ </span>
1298
+ )}
1299
+ {tab.name}
1300
+ </button>
1301
+ ))}
1302
+ </div>
1303
+ )}
1304
+
1305
+ {/* Upload bar */}
1306
+ <div
1307
+ style={{
1308
+ display: "flex",
1309
+ alignItems: "center",
1310
+ justifyContent: "space-between",
1311
+ padding: "12px 20px",
1312
+ borderBottom: "1px solid #e5e7eb",
1313
+ }}
1314
+ >
1315
+ <span style={{ fontSize: "13px", color: "#6b7280" }}>
1316
+ {loading ? "Loading…" : `${items.length} image${items.length !== 1 ? "s" : ""}`}
1317
+ </span>
1318
+ <button
1319
+ type="button"
1320
+ onClick={() => fileInputRef.current?.click()}
1321
+ disabled={uploading}
1322
+ style={{
1323
+ padding: "6px 14px",
1324
+ fontSize: "13px",
1325
+ fontWeight: 500,
1326
+ borderRadius: "6px",
1327
+ border: "1px solid #d1d5db",
1328
+ cursor: uploading ? "not-allowed" : "pointer",
1329
+ background: "white",
1330
+ color: "#374151",
1331
+ opacity: uploading ? 0.6 : 1,
1332
+ }}
1333
+ >
1334
+ {uploading ? "Uploading…" : "Upload"}
1335
+ </button>
1336
+ <input
1337
+ ref={fileInputRef}
1338
+ type="file"
1339
+ accept="image/*"
1340
+ style={{ display: "none" }}
1341
+ onChange={(e) => {
1342
+ const file = e.target.files?.[0];
1343
+ if (file) void handleUpload(file);
1344
+ if (fileInputRef.current) fileInputRef.current.value = "";
1345
+ }}
1346
+ />
1347
+ </div>
1348
+
1349
+ {/* Grid */}
1350
+ <div
1351
+ style={{
1352
+ flex: 1,
1353
+ overflowY: "auto",
1354
+ padding: "16px 20px",
1355
+ minHeight: "250px",
1356
+ }}
1357
+ >
1358
+ {loading ? (
1359
+ <div
1360
+ style={{
1361
+ display: "flex",
1362
+ alignItems: "center",
1363
+ justifyContent: "center",
1364
+ height: "200px",
1365
+ color: "#9ca3af",
1366
+ fontSize: "14px",
1367
+ }}
1368
+ >
1369
+ Loading…
1370
+ </div>
1371
+ ) : items.length === 0 ? (
1372
+ <div
1373
+ style={{
1374
+ display: "flex",
1375
+ flexDirection: "column",
1376
+ alignItems: "center",
1377
+ justifyContent: "center",
1378
+ height: "200px",
1379
+ color: "#9ca3af",
1380
+ fontSize: "14px",
1381
+ textAlign: "center",
1382
+ }}
1383
+ >
1384
+ <div style={{ fontSize: "32px", marginBottom: "8px" }}>🖼</div>
1385
+ No images found
1386
+ <button
1387
+ type="button"
1388
+ onClick={() => fileInputRef.current?.click()}
1389
+ style={{
1390
+ marginTop: "12px",
1391
+ padding: "8px 16px",
1392
+ fontSize: "13px",
1393
+ borderRadius: "6px",
1394
+ border: "1px solid #d1d5db",
1395
+ background: "white",
1396
+ cursor: "pointer",
1397
+ color: "#374151",
1398
+ }}
1399
+ >
1400
+ Upload an image
1401
+ </button>
1402
+ </div>
1403
+ ) : (
1404
+ <div
1405
+ style={{
1406
+ display: "grid",
1407
+ gridTemplateColumns: "repeat(auto-fill, minmax(110px, 1fr))",
1408
+ gap: "10px",
1409
+ }}
1410
+ >
1411
+ {items.map((item) => {
1412
+ const isSelected = selectedId === item.id;
1413
+ const thumb = item.url || item.previewUrl || "";
1414
+ return (
1415
+ <button
1416
+ key={item.id}
1417
+ type="button"
1418
+ onClick={() => setSelectedId(item.id)}
1419
+ onDoubleClick={() => onSelect(item)}
1420
+ style={{
1421
+ position: "relative",
1422
+ aspectRatio: "1",
1423
+ borderRadius: "8px",
1424
+ border: isSelected ? "2px solid #3b82f6" : "2px solid transparent",
1425
+ overflow: "hidden",
1426
+ cursor: "pointer",
1427
+ padding: 0,
1428
+ background: "#f3f4f6",
1429
+ outline: isSelected ? "2px solid rgba(59,130,246,0.3)" : "none",
1430
+ outlineOffset: "1px",
1431
+ }}
1432
+ aria-label={item.filename}
1433
+ >
1434
+ {thumb ? (
1435
+ <img
1436
+ src={thumb}
1437
+ alt=""
1438
+ style={{ width: "100%", height: "100%", objectFit: "cover" }}
1439
+ />
1440
+ ) : (
1441
+ <div
1442
+ style={{
1443
+ width: "100%",
1444
+ height: "100%",
1445
+ display: "flex",
1446
+ alignItems: "center",
1447
+ justifyContent: "center",
1448
+ fontSize: "24px",
1449
+ }}
1450
+ >
1451
+ 🖼
1452
+ </div>
1453
+ )}
1454
+ {isSelected && (
1455
+ <div
1456
+ style={{
1457
+ position: "absolute",
1458
+ inset: 0,
1459
+ background: "rgba(59,130,246,0.15)",
1460
+ display: "flex",
1461
+ alignItems: "center",
1462
+ justifyContent: "center",
1463
+ }}
1464
+ >
1465
+ <div
1466
+ style={{
1467
+ width: "24px",
1468
+ height: "24px",
1469
+ borderRadius: "50%",
1470
+ background: "#3b82f6",
1471
+ color: "white",
1472
+ display: "flex",
1473
+ alignItems: "center",
1474
+ justifyContent: "center",
1475
+ fontSize: "14px",
1476
+ }}
1477
+ >
1478
+
1479
+ </div>
1480
+ </div>
1481
+ )}
1482
+ <div
1483
+ style={{
1484
+ position: "absolute",
1485
+ bottom: 0,
1486
+ left: 0,
1487
+ right: 0,
1488
+ background: "linear-gradient(transparent, rgba(0,0,0,0.6))",
1489
+ padding: "16px 6px 4px",
1490
+ }}
1491
+ >
1492
+ <div
1493
+ style={{
1494
+ fontSize: "11px",
1495
+ color: "white",
1496
+ overflow: "hidden",
1497
+ textOverflow: "ellipsis",
1498
+ whiteSpace: "nowrap",
1499
+ }}
1500
+ >
1501
+ {item.filename}
1502
+ </div>
1503
+ </div>
1504
+ </button>
1505
+ );
1506
+ })}
1507
+ </div>
1508
+ )}
1509
+ </div>
1510
+
1511
+ {/* Footer */}
1512
+ <div
1513
+ style={{
1514
+ display: "flex",
1515
+ justifyContent: "flex-end",
1516
+ gap: "8px",
1517
+ padding: "12px 20px",
1518
+ borderTop: "1px solid #e5e7eb",
1519
+ }}
1520
+ >
1521
+ <button
1522
+ type="button"
1523
+ onClick={onClose}
1524
+ style={{
1525
+ padding: "8px 16px",
1526
+ fontSize: "13px",
1527
+ fontWeight: 500,
1528
+ borderRadius: "6px",
1529
+ border: "1px solid #d1d5db",
1530
+ background: "white",
1531
+ cursor: "pointer",
1532
+ color: "#374151",
1533
+ }}
1534
+ >
1535
+ Cancel
1536
+ </button>
1537
+ <button
1538
+ type="button"
1539
+ onClick={handleConfirm}
1540
+ disabled={!selectedId}
1541
+ style={{
1542
+ padding: "8px 16px",
1543
+ fontSize: "13px",
1544
+ fontWeight: 500,
1545
+ borderRadius: "6px",
1546
+ border: "none",
1547
+ background: selectedId ? "#3b82f6" : "#93c5fd",
1548
+ color: "white",
1549
+ cursor: selectedId ? "pointer" : "not-allowed",
1550
+ }}
1551
+ >
1552
+ Insert
1553
+ </button>
1554
+ </div>
1555
+ </div>
1556
+ </div>,
1557
+ document.body,
1558
+ );
1559
+ }
1560
+
1561
+ // ── Component ──────────────────────────────────────────────────────
1562
+
1563
+ export interface InlinePortableTextEditorProps {
1564
+ value: PTBlock[];
1565
+ collection: string;
1566
+ entryId: string;
1567
+ field: string;
1568
+ }
1569
+
1570
+ export function InlinePortableTextEditor({
1571
+ value,
1572
+ collection,
1573
+ entryId,
1574
+ field,
1575
+ }: InlinePortableTextEditorProps) {
1576
+ const initialRef = React.useRef(value);
1577
+ const savingRef = React.useRef(false);
1578
+ const editorRef = React.useRef<ReturnType<typeof useEditor>>(null);
1579
+
1580
+ // Media picker state
1581
+ const [mediaPickerOpen, setMediaPickerOpen] = React.useState(false);
1582
+
1583
+ // Listen for the slash command's media picker event
1584
+ React.useEffect(() => {
1585
+ const handler = () => setMediaPickerOpen(true);
1586
+ document.addEventListener("emdash:open-media-picker", handler);
1587
+ return () => document.removeEventListener("emdash:open-media-picker", handler);
1588
+ }, []);
1589
+
1590
+ // Slash menu state — use ref to avoid re-creating the extension on state change
1591
+ const [slashMenuState, setSlashMenuState] = React.useState<SlashMenuState>(initialSlashMenuState);
1592
+ const slashMenuStateRef = React.useRef(slashMenuState);
1593
+ slashMenuStateRef.current = slashMenuState;
1594
+
1595
+ const filterCommandsRef = React.useRef((query: string): SlashCommandItem[] => {
1596
+ const q = query.toLowerCase();
1597
+ return slashCommands.filter(
1598
+ (cmd) =>
1599
+ cmd.title.toLowerCase().includes(q) ||
1600
+ cmd.description.toLowerCase().includes(q) ||
1601
+ cmd.aliases?.some((a) => a.toLowerCase().includes(q)),
1602
+ );
1603
+ });
1604
+
1605
+ const initialContent = React.useMemo(
1606
+ () => portableTextToPM(value || []),
1607
+ [], // Only compute once on mount
1608
+ );
1609
+
1610
+ const getBlocks = React.useCallback((): PTBlock[] => {
1611
+ const editor = editorRef.current;
1612
+ if (!editor) return initialRef.current;
1613
+ const json: unknown = editor.getJSON();
1614
+ if (!isPMNode(json)) return initialRef.current;
1615
+ return pmToPortableText(json);
1616
+ }, []);
1617
+
1618
+ const save = React.useCallback(async () => {
1619
+ if (savingRef.current) return;
1620
+
1621
+ const current = JSON.stringify(getBlocks());
1622
+ const initial = JSON.stringify(initialRef.current);
1623
+ if (current === initial) return;
1624
+
1625
+ savingRef.current = true;
1626
+ try {
1627
+ const res = await fetch(
1628
+ `/_emdash/api/content/${encodeURIComponent(collection)}/${encodeURIComponent(entryId)}`,
1629
+ {
1630
+ method: "PUT",
1631
+ credentials: "same-origin",
1632
+ headers: { "Content-Type": "application/json", "X-EmDash-Request": "1" },
1633
+ body: JSON.stringify({ data: { [field]: getBlocks() } }),
1634
+ },
1635
+ );
1636
+
1637
+ if (res.ok) {
1638
+ initialRef.current = getBlocks();
1639
+ document.dispatchEvent(new CustomEvent("emdash:save", { detail: { state: "saved" } }));
1640
+ document.dispatchEvent(
1641
+ new CustomEvent("emdash:content-changed", {
1642
+ detail: { collection, id: entryId },
1643
+ }),
1644
+ );
1645
+ } else {
1646
+ document.dispatchEvent(new CustomEvent("emdash:save", { detail: { state: "error" } }));
1647
+ console.error("Save failed:", res.status);
1648
+ }
1649
+ } catch (err) {
1650
+ document.dispatchEvent(new CustomEvent("emdash:save", { detail: { state: "error" } }));
1651
+ console.error("Save failed:", err);
1652
+ } finally {
1653
+ savingRef.current = false;
1654
+ }
1655
+ }, [collection, entryId, field, getBlocks]);
1656
+
1657
+ // Create slash commands extension once — uses refs to avoid re-render loop
1658
+ const slashCommandsExtension = React.useMemo(
1659
+ () =>
1660
+ createSlashCommandsExtension({
1661
+ filterCommands: (query: string) => filterCommandsRef.current(query),
1662
+ onStateChange: setSlashMenuState,
1663
+ getState: () => slashMenuStateRef.current,
1664
+ }),
1665
+ [],
1666
+ );
1667
+
1668
+ const editor = useEditor({
1669
+ extensions: [
1670
+ StarterKit.configure({
1671
+ heading: { levels: [1, 2, 3] },
1672
+ dropcursor: { color: "#3b82f6", width: 2 },
1673
+ }),
1674
+ Image.extend({
1675
+ addAttributes() {
1676
+ return {
1677
+ ...this.parent?.(),
1678
+ mediaId: { default: null },
1679
+ provider: { default: null },
1680
+ width: { default: null },
1681
+ height: { default: null },
1682
+ };
1683
+ },
1684
+ }),
1685
+ Underline,
1686
+ Link.configure({
1687
+ openOnClick: false,
1688
+ HTMLAttributes: { class: "underline text-blue-600 dark:text-blue-400" },
1689
+ }),
1690
+ Placeholder.configure({
1691
+ includeChildren: true,
1692
+ placeholder: ({ node }) => {
1693
+ if (node.type.name === "paragraph") return "Type / for commands...";
1694
+ return "";
1695
+ },
1696
+ }),
1697
+ TextAlign.configure({
1698
+ types: ["heading", "paragraph"],
1699
+ }),
1700
+ Focus.configure({
1701
+ className: "has-focus",
1702
+ mode: "all",
1703
+ }),
1704
+ Typography,
1705
+ slashCommandsExtension,
1706
+ ],
1707
+ content: initialContent,
1708
+ immediatelyRender: false,
1709
+ editorProps: {
1710
+ attributes: {
1711
+ class: "prose prose-sm sm:prose-base dark:prose-invert max-w-none emdash-inline-editor",
1712
+ },
1713
+ },
1714
+ onUpdate: () => {
1715
+ document.dispatchEvent(new CustomEvent("emdash:save", { detail: { state: "unsaved" } }));
1716
+ },
1717
+ });
1718
+
1719
+ // Store editor ref for getBlocks
1720
+ React.useEffect(() => {
1721
+ editorRef.current = editor;
1722
+ }, [editor]);
1723
+
1724
+ // Slash menu command handler
1725
+ const handleSlashCommand = React.useCallback(
1726
+ (item: SlashCommandItem) => {
1727
+ if (!editor || !slashMenuStateRef.current.range) return;
1728
+ item.command({ editor, range: slashMenuStateRef.current.range });
1729
+ setSlashMenuState((prev) => ({ ...prev, isOpen: false }));
1730
+ },
1731
+ [editor],
1732
+ );
1733
+
1734
+ // Handle media selection from the picker
1735
+ const handleMediaSelect = React.useCallback(
1736
+ (item: MediaItemData) => {
1737
+ if (!editor) return;
1738
+ const src =
1739
+ item.url || item.previewUrl || `/_emdash/api/media/file/${item.storageKey || item.id}`;
1740
+ editor
1741
+ .chain()
1742
+ .focus()
1743
+ .setImage({
1744
+ src,
1745
+ alt: item.alt || item.filename || "",
1746
+ mediaId: item.id,
1747
+ width: item.width,
1748
+ height: item.height,
1749
+ })
1750
+ .run();
1751
+ setMediaPickerOpen(false);
1752
+ void save();
1753
+ },
1754
+ [editor, save],
1755
+ );
1756
+
1757
+ // Save on blur — but not when interacting with slash menu or media picker
1758
+ const handleBlur = React.useCallback(
1759
+ (e: React.FocusEvent<HTMLDivElement>) => {
1760
+ if (mediaPickerOpen) return;
1761
+ const related = e.relatedTarget instanceof HTMLElement ? e.relatedTarget : null;
1762
+ if (related && e.currentTarget.contains(related)) return;
1763
+ // Don't save if focus moved to the slash menu (portalled to body)
1764
+ if (related?.closest(".emdash-slash-menu")) return;
1765
+ if (related?.closest(".emdash-media-picker")) return;
1766
+ save();
1767
+ },
1768
+ [save, mediaPickerOpen],
1769
+ );
1770
+
1771
+ if (!editor) return null;
1772
+
1773
+ return (
1774
+ <div onBlur={handleBlur}>
1775
+ <InlineBubbleMenu editor={editor} />
1776
+ <EditorContent editor={editor} />
1777
+ <InlineSlashMenu
1778
+ state={slashMenuState}
1779
+ onCommand={handleSlashCommand}
1780
+ setSelectedIndex={(index) =>
1781
+ setSlashMenuState((prev) => ({ ...prev, selectedIndex: index }))
1782
+ }
1783
+ />
1784
+ <InlineMediaPicker
1785
+ open={mediaPickerOpen}
1786
+ onClose={() => {
1787
+ setMediaPickerOpen(false);
1788
+ editor?.commands.focus();
1789
+ }}
1790
+ onSelect={handleMediaSelect}
1791
+ />
1792
+ <style>{`
1793
+ .emdash-bubble-menu {
1794
+ z-index: 100;
1795
+ display: flex;
1796
+ align-items: center;
1797
+ gap: 2px;
1798
+ padding: 4px;
1799
+ border-radius: 8px;
1800
+ border: 1px solid #d1d5db;
1801
+ background: white;
1802
+ box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
1803
+ font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
1804
+ }
1805
+ .emdash-bubble-btn {
1806
+ display: inline-flex;
1807
+ align-items: center;
1808
+ justify-content: center;
1809
+ width: 32px;
1810
+ height: 32px;
1811
+ border-radius: 6px;
1812
+ border: none;
1813
+ background: transparent;
1814
+ cursor: pointer;
1815
+ color: inherit;
1816
+ font-size: 14px;
1817
+ line-height: 1;
1818
+ padding: 0;
1819
+ }
1820
+ .emdash-bubble-btn:hover {
1821
+ background: #f3f4f6;
1822
+ }
1823
+ .emdash-bubble-btn--active {
1824
+ background: #dbeafe;
1825
+ color: #1d4ed8;
1826
+ }
1827
+ .emdash-bubble-btn--active:hover {
1828
+ background: #bfdbfe;
1829
+ }
1830
+ .emdash-bubble-btn--danger {
1831
+ color: #dc2626;
1832
+ }
1833
+ .emdash-bubble-divider {
1834
+ display: block;
1835
+ width: 1px;
1836
+ height: 20px;
1837
+ background: #d1d5db;
1838
+ margin: 0 4px;
1839
+ }
1840
+ .emdash-bubble-link-input {
1841
+ height: 28px;
1842
+ width: 200px;
1843
+ font-size: 13px;
1844
+ padding: 0 8px;
1845
+ border: 1px solid #d1d5db;
1846
+ border-radius: 4px;
1847
+ outline: none;
1848
+ background: white;
1849
+ color: inherit;
1850
+ font-family: inherit;
1851
+ }
1852
+ .emdash-bubble-link-input:focus {
1853
+ border-color: #3b82f6;
1854
+ }
1855
+ @media (prefers-color-scheme: dark) {
1856
+ .emdash-bubble-menu {
1857
+ background: #1f2937;
1858
+ border-color: #374151;
1859
+ color: #e5e7eb;
1860
+ }
1861
+ .emdash-bubble-btn:hover {
1862
+ background: #374151;
1863
+ }
1864
+ .emdash-bubble-btn--active {
1865
+ background: #1e3a5f;
1866
+ color: #93c5fd;
1867
+ }
1868
+ .emdash-bubble-btn--active:hover {
1869
+ background: #1e40af;
1870
+ }
1871
+ .emdash-bubble-divider {
1872
+ background: #4b5563;
1873
+ }
1874
+ .emdash-bubble-link-input {
1875
+ background: #111827;
1876
+ border-color: #4b5563;
1877
+ color: #e5e7eb;
1878
+ }
1879
+ .emdash-bubble-link-input:focus {
1880
+ border-color: #60a5fa;
1881
+ }
1882
+ .emdash-slash-menu {
1883
+ background: #1f2937 !important;
1884
+ border-color: #374151 !important;
1885
+ color: #e5e7eb !important;
1886
+ }
1887
+ .emdash-slash-menu button:hover,
1888
+ .emdash-slash-menu button[style*="background: rgb(243, 244, 246)"] {
1889
+ background: #374151 !important;
1890
+ }
1891
+ .emdash-media-picker {
1892
+ background: #1f2937 !important;
1893
+ color: #e5e7eb !important;
1894
+ }
1895
+ .emdash-media-picker button {
1896
+ color: #e5e7eb !important;
1897
+ }
1898
+ }
1899
+ .emdash-inline-editor:focus {
1900
+ outline: none;
1901
+ }
1902
+ `}</style>
1903
+ </div>
1904
+ );
1905
+ }