opacacms 0.1.1 → 0.1.3

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 (213) hide show
  1. package/dist/schema/index.js +9 -0
  2. package/package.json +36 -1
  3. package/bun.lock +0 -34
  4. package/global.d.ts +0 -11
  5. package/src/admin/api-client.ts +0 -63
  6. package/src/admin/auth-client.ts +0 -40
  7. package/src/admin/custom-field.ts +0 -179
  8. package/src/admin/index.ts +0 -15
  9. package/src/admin/react.tsx +0 -72
  10. package/src/admin/router.ts +0 -9
  11. package/src/admin/stores/admin-queries.ts +0 -121
  12. package/src/admin/stores/auth.ts +0 -61
  13. package/src/admin/stores/column-visibility.ts +0 -67
  14. package/src/admin/stores/config.ts +0 -15
  15. package/src/admin/stores/media.ts +0 -95
  16. package/src/admin/stores/query.ts +0 -13
  17. package/src/admin/stores/ui.ts +0 -29
  18. package/src/admin/ui/admin-client.tsx +0 -283
  19. package/src/admin/ui/admin-layout.tsx +0 -276
  20. package/src/admin/ui/components/ColumnVisibilityToggle.tsx +0 -141
  21. package/src/admin/ui/components/DataDetailSheet.tsx +0 -141
  22. package/src/admin/ui/components/DataDetailView.tsx +0 -175
  23. package/src/admin/ui/components/Table.tsx +0 -67
  24. package/src/admin/ui/components/fields/ArrayField.tsx +0 -166
  25. package/src/admin/ui/components/fields/BlocksField.tsx +0 -202
  26. package/src/admin/ui/components/fields/BooleanField.tsx +0 -50
  27. package/src/admin/ui/components/fields/CollapsibleField.tsx +0 -75
  28. package/src/admin/ui/components/fields/DateField.tsx +0 -45
  29. package/src/admin/ui/components/fields/FileField.tsx +0 -322
  30. package/src/admin/ui/components/fields/GroupField.tsx +0 -50
  31. package/src/admin/ui/components/fields/JoinField.tsx +0 -23
  32. package/src/admin/ui/components/fields/NumberField.tsx +0 -46
  33. package/src/admin/ui/components/fields/RadioField.tsx +0 -62
  34. package/src/admin/ui/components/fields/RelationshipField.tsx +0 -278
  35. package/src/admin/ui/components/fields/RowField.tsx +0 -40
  36. package/src/admin/ui/components/fields/SelectField.tsx +0 -59
  37. package/src/admin/ui/components/fields/TabsField.tsx +0 -101
  38. package/src/admin/ui/components/fields/TextAreaField.tsx +0 -54
  39. package/src/admin/ui/components/fields/TextField.tsx +0 -49
  40. package/src/admin/ui/components/fields/VirtualField.tsx +0 -53
  41. package/src/admin/ui/components/fields/index.tsx +0 -371
  42. package/src/admin/ui/components/fields/richtext-editor/index.tsx +0 -211
  43. package/src/admin/ui/components/fields/richtext-editor/nodes/ImageComponent.tsx +0 -142
  44. package/src/admin/ui/components/fields/richtext-editor/nodes/ImageNode.tsx +0 -95
  45. package/src/admin/ui/components/fields/richtext-editor/plugins/ComponentPickerPlugin.tsx +0 -226
  46. package/src/admin/ui/components/fields/richtext-editor/plugins/EditableSyncPlugin.tsx +0 -16
  47. package/src/admin/ui/components/fields/richtext-editor/plugins/NotionToolbarPlugin.tsx +0 -184
  48. package/src/admin/ui/components/fields/richtext-editor/plugins/SimpleToolbarPlugin.tsx +0 -240
  49. package/src/admin/ui/components/fields/richtext-editor/plugins/ValueSyncPlugin.tsx +0 -40
  50. package/src/admin/ui/components/fields/utils.ts +0 -1
  51. package/src/admin/ui/components/link.tsx +0 -41
  52. package/src/admin/ui/components/media/AssetManagerModal.tsx +0 -334
  53. package/src/admin/ui/components/toast.tsx +0 -72
  54. package/src/admin/ui/components/ui/accordion.tsx +0 -51
  55. package/src/admin/ui/components/ui/alert-dialog.tsx +0 -98
  56. package/src/admin/ui/components/ui/blocks.tsx +0 -32
  57. package/src/admin/ui/components/ui/breadcrumbs.tsx +0 -59
  58. package/src/admin/ui/components/ui/button.tsx +0 -26
  59. package/src/admin/ui/components/ui/collapsible.tsx +0 -124
  60. package/src/admin/ui/components/ui/dialog.tsx +0 -79
  61. package/src/admin/ui/components/ui/group.tsx +0 -20
  62. package/src/admin/ui/components/ui/index.ts +0 -17
  63. package/src/admin/ui/components/ui/input.tsx +0 -12
  64. package/src/admin/ui/components/ui/join.tsx +0 -53
  65. package/src/admin/ui/components/ui/label.tsx +0 -11
  66. package/src/admin/ui/components/ui/radio-group.tsx +0 -75
  67. package/src/admin/ui/components/ui/relationship-detail-sheet.tsx +0 -122
  68. package/src/admin/ui/components/ui/relationship.tsx +0 -58
  69. package/src/admin/ui/components/ui/scroll-area.tsx +0 -19
  70. package/src/admin/ui/components/ui/select.tsx +0 -187
  71. package/src/admin/ui/components/ui/separator.tsx +0 -21
  72. package/src/admin/ui/components/ui/sheet.tsx +0 -106
  73. package/src/admin/ui/components/ui/tabs.tsx +0 -116
  74. package/src/admin/ui/components/ui/utils.ts +0 -3
  75. package/src/admin/ui/hooks/use-debounce.ts +0 -15
  76. package/src/admin/ui/styles/_locale-switcher.scss +0 -33
  77. package/src/admin/ui/styles/accordion.scss +0 -60
  78. package/src/admin/ui/styles/animations.scss +0 -41
  79. package/src/admin/ui/styles/asset-manager.scss +0 -547
  80. package/src/admin/ui/styles/badge.scss +0 -13
  81. package/src/admin/ui/styles/base.scss +0 -22
  82. package/src/admin/ui/styles/button.scss +0 -161
  83. package/src/admin/ui/styles/card.scss +0 -13
  84. package/src/admin/ui/styles/collapsible.scss +0 -75
  85. package/src/admin/ui/styles/data-detail.scss +0 -92
  86. package/src/admin/ui/styles/dialog.scss +0 -102
  87. package/src/admin/ui/styles/empty-state.scss +0 -22
  88. package/src/admin/ui/styles/group.scss +0 -19
  89. package/src/admin/ui/styles/index.scss +0 -33
  90. package/src/admin/ui/styles/input.scss +0 -80
  91. package/src/admin/ui/styles/label.scss +0 -12
  92. package/src/admin/ui/styles/layout.scss +0 -56
  93. package/src/admin/ui/styles/lexical.scss +0 -469
  94. package/src/admin/ui/styles/loading.scss +0 -102
  95. package/src/admin/ui/styles/media-registry.scss +0 -597
  96. package/src/admin/ui/styles/pagination.scss +0 -20
  97. package/src/admin/ui/styles/radio-group.scss +0 -66
  98. package/src/admin/ui/styles/row.scss +0 -17
  99. package/src/admin/ui/styles/scrollbar.scss +0 -36
  100. package/src/admin/ui/styles/select.scss +0 -121
  101. package/src/admin/ui/styles/separator.scss +0 -14
  102. package/src/admin/ui/styles/sheet.scss +0 -152
  103. package/src/admin/ui/styles/sidebar.scss +0 -148
  104. package/src/admin/ui/styles/switch.scss +0 -59
  105. package/src/admin/ui/styles/table.scss +0 -207
  106. package/src/admin/ui/styles/tabs.scss +0 -62
  107. package/src/admin/ui/styles/toast.scss +0 -45
  108. package/src/admin/ui/styles/variables.scss +0 -24
  109. package/src/admin/ui/views/collection-list-view.tsx +0 -720
  110. package/src/admin/ui/views/dashboard-view.tsx +0 -263
  111. package/src/admin/ui/views/document-edit-view.tsx +0 -384
  112. package/src/admin/ui/views/global-edit-view.tsx +0 -226
  113. package/src/admin/ui/views/init-view.tsx +0 -182
  114. package/src/admin/ui/views/login-view.tsx +0 -123
  115. package/src/admin/ui/views/media-registry-view.tsx +0 -1104
  116. package/src/admin/ui/views/settings-view.tsx +0 -729
  117. package/src/admin/webcomponent.tsx +0 -15
  118. package/src/auth/index.ts +0 -194
  119. package/src/auth/migrations.ts +0 -87
  120. package/src/auth/premissions.ts +0 -46
  121. package/src/cli/commands/generate-types.ts +0 -116
  122. package/src/cli/commands/init.ts +0 -95
  123. package/src/cli/commands/migrate-commands.ts +0 -160
  124. package/src/cli/commands/seed-command.ts +0 -11
  125. package/src/cli/d1-mock.ts +0 -101
  126. package/src/cli/index.test.ts +0 -84
  127. package/src/cli/index.ts +0 -183
  128. package/src/cli/r2-mock.ts +0 -217
  129. package/src/cli/seeding.ts +0 -409
  130. package/src/client.ts +0 -181
  131. package/src/config-utils.ts +0 -102
  132. package/src/config.ts +0 -49
  133. package/src/db/adapter.ts +0 -53
  134. package/src/db/better-sqlite.ts +0 -630
  135. package/src/db/bun-sqlite.ts +0 -646
  136. package/src/db/d1.ts +0 -711
  137. package/src/db/index.ts +0 -2
  138. package/src/db/kysely/data-mapper.ts +0 -142
  139. package/src/db/kysely/field-mapper.ts +0 -148
  140. package/src/db/kysely/migration-generator.ts +0 -223
  141. package/src/db/kysely/query-builder.ts +0 -92
  142. package/src/db/kysely/schema-builder.ts +0 -439
  143. package/src/db/kysely/sql-utils.ts +0 -13
  144. package/src/db/migration.ts +0 -40
  145. package/src/db/postgres.ts +0 -621
  146. package/src/db/sqlite.ts +0 -658
  147. package/src/db/system-schema.ts +0 -121
  148. package/src/index.ts +0 -11
  149. package/src/runtimes/README.md +0 -59
  150. package/src/runtimes/bun.ts +0 -49
  151. package/src/runtimes/cloudflare-workers.ts +0 -38
  152. package/src/runtimes/next.ts +0 -26
  153. package/src/runtimes/node.ts +0 -52
  154. package/src/schema/collection.ts +0 -184
  155. package/src/schema/fields/base.ts +0 -164
  156. package/src/schema/fields/index.ts +0 -427
  157. package/src/schema/global.ts +0 -145
  158. package/src/schema/index.ts +0 -4
  159. package/src/schema/infer.ts +0 -72
  160. package/src/server/admin-router.ts +0 -20
  161. package/src/server/admin.ts +0 -142
  162. package/src/server/assets.ts +0 -306
  163. package/src/server/collection-router.ts +0 -55
  164. package/src/server/handlers.ts +0 -722
  165. package/src/server/middlewares/admin.ts +0 -27
  166. package/src/server/middlewares/auth.ts +0 -89
  167. package/src/server/middlewares/context.ts +0 -17
  168. package/src/server/middlewares/cors.ts +0 -24
  169. package/src/server/middlewares/database-init.ts +0 -74
  170. package/src/server/middlewares/rate-limit.ts +0 -71
  171. package/src/server/router.ts +0 -47
  172. package/src/server/setup-middlewares.ts +0 -58
  173. package/src/server/system-router.ts +0 -35
  174. package/src/server.ts +0 -9
  175. package/src/storage/adapters/cloudflare-r2.ts +0 -136
  176. package/src/storage/adapters/local.ts +0 -146
  177. package/src/storage/adapters/s3.ts +0 -186
  178. package/src/storage/errors.ts +0 -46
  179. package/src/storage/index.ts +0 -6
  180. package/src/storage/types.ts +0 -39
  181. package/src/types.ts +0 -605
  182. package/src/utils/lexical.ts +0 -37
  183. package/src/utils/logger.ts +0 -73
  184. package/src/validation.ts +0 -429
  185. package/src/validator.ts +0 -179
  186. package/test/admin-custom-field.test.ts +0 -162
  187. package/test/admin-react-field.test.tsx +0 -134
  188. package/test/api-features.test.ts +0 -78
  189. package/test/api.test.ts +0 -178
  190. package/test/auth.test.ts +0 -62
  191. package/test/cli-integration.test.ts +0 -148
  192. package/test/cli.test.ts +0 -25
  193. package/test/db/postgres.test.ts +0 -95
  194. package/test/db/sqlite-filter.test.ts +0 -53
  195. package/test/db/sqlite.test.ts +0 -82
  196. package/test/engine-features.test.ts +0 -79
  197. package/test/globals.test.ts +0 -74
  198. package/test/integration-tmp/db-app/opacacms.config.ts +0 -15
  199. package/test/integration-tmp/my-sqlite-app/opacacms.config.ts +0 -25
  200. package/test/integration-tmp/my-test-app/index.ts +0 -8
  201. package/test/integration-tmp/my-test-app/opacacms.config.ts +0 -16
  202. package/test/integration-tmp/my-test-app/package.json +0 -12
  203. package/test/populate.test.ts +0 -79
  204. package/test/runtimes.test.ts +0 -43
  205. package/test/schema-builder.test.ts +0 -107
  206. package/test/schema-features.test.ts +0 -63
  207. package/test/seeding.test.ts +0 -68
  208. package/test/storage/local.test.ts +0 -72
  209. package/test/storage/s3.test.ts +0 -60
  210. package/test/structural-data.test.ts +0 -100
  211. package/test/test-setup.ts +0 -11
  212. package/test/validation.test.ts +0 -162
  213. package/tsconfig.json +0 -42
@@ -0,0 +1,9 @@
1
+ import"../chunk-8sqjbsgt.js";
2
+ export {
3
+ GlobalBuilder,
4
+ Global,
5
+ FieldBuilder,
6
+ Field,
7
+ CollectionBuilder,
8
+ Collection
9
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "opacacms",
3
- "version": "0.1.1",
3
+ "version": "0.1.3",
4
4
  "sideEffects": false,
5
5
  "scripts": {
6
6
  "build": "bun run ../../scripts/build.ts && tsc --emitDeclarationOnly",
@@ -16,12 +16,47 @@
16
16
  },
17
17
  "typesVersions": {
18
18
  "*": {
19
+ "server": [
20
+ "./dist/server.d.ts"
21
+ ],
22
+ "admin": [
23
+ "./dist/admin/index.d.ts"
24
+ ],
25
+ "admin/wc": [
26
+ "./dist/admin/webcomponent.d.ts"
27
+ ],
28
+ "client": [
29
+ "./dist/client.d.ts"
30
+ ],
31
+ "schema": [
32
+ "./dist/schema/index.d.ts"
33
+ ],
34
+ "db/*": [
35
+ "./dist/db/*.d.ts"
36
+ ],
37
+ "db": [
38
+ "./dist/db/index.d.ts"
39
+ ],
40
+ "storage/*": [
41
+ "./dist/storage/adapters/*.d.ts"
42
+ ],
43
+ "storage": [
44
+ "./dist/storage/index.d.ts"
45
+ ],
46
+ "runtimes/*": [
47
+ "./dist/runtimes/*.d.ts"
48
+ ],
19
49
  "*": [
20
50
  "./dist/*.d.ts",
21
51
  "./dist/*/index.d.ts"
22
52
  ]
23
53
  }
24
54
  },
55
+ "files": [
56
+ "dist",
57
+ "README.md",
58
+ "LICENSE"
59
+ ],
25
60
  "module": "dist/index.js",
26
61
  "type": "module",
27
62
  "bin": {
package/bun.lock DELETED
@@ -1,34 +0,0 @@
1
- {
2
- "lockfileVersion": 1,
3
- "configVersion": 1,
4
- "workspaces": {
5
- "": {
6
- "name": "opacacms",
7
- "dependencies": {
8
- "hono": "^4.11.4",
9
- "zod": "^4.3.5",
10
- },
11
- "devDependencies": {
12
- "@types/bun": "latest",
13
- },
14
- "peerDependencies": {
15
- "typescript": "^5",
16
- },
17
- },
18
- },
19
- "packages": {
20
- "@types/bun": ["@types/bun@1.3.6", "", { "dependencies": { "bun-types": "1.3.6" } }, "sha512-uWCv6FO/8LcpREhenN1d1b6fcspAB+cefwD7uti8C8VffIv0Um08TKMn98FynpTiU38+y2dUO55T11NgDt8VAA=="],
21
-
22
- "@types/node": ["@types/node@25.0.9", "", { "dependencies": { "undici-types": "~7.16.0" } }, "sha512-/rpCXHlCWeqClNBwUhDcusJxXYDjZTyE8v5oTO7WbL8eij2nKhUeU89/6xgjU7N4/Vh3He0BtyhJdQbDyhiXAw=="],
23
-
24
- "bun-types": ["bun-types@1.3.6", "", { "dependencies": { "@types/node": "*" } }, "sha512-OlFwHcnNV99r//9v5IIOgQ9Uk37gZqrNMCcqEaExdkVq3Avwqok1bJFmvGMCkCE0FqzdY8VMOZpfpR3lwI+CsQ=="],
25
-
26
- "hono": ["hono@4.11.4", "", {}, "sha512-U7tt8JsyrxSRKspfhtLET79pU8K+tInj5QZXs1jSugO1Vq5dFj3kmZsRldo29mTBfcjDRVRXrEZ6LS63Cog9ZA=="],
27
-
28
- "typescript": ["typescript@5.9.3", "", { "bin": { "tsc": "bin/tsc", "tsserver": "bin/tsserver" } }, "sha512-jl1vZzPDinLr9eUt3J/t7V6FgNEw9QjvBPdysz9KfQDD41fQrC2Y4vKQdiaUpFT4bXlb1RHhLpp8wtm6M5TgSw=="],
29
-
30
- "undici-types": ["undici-types@7.16.0", "", {}, "sha512-Zz+aZWSj8LE6zoxD+xrjh4VfkIG8Ya6LvYkZqtUQGJPZjYl53ypCaUwWqo7eI0x66KBGeRo+mlBEkMSeSZ38Nw=="],
31
-
32
- "zod": ["zod@4.3.5", "", {}, "sha512-k7Nwx6vuWx1IJ9Bjuf4Zt1PEllcwe7cls3VNzm4CQ1/hgtFUK2bRNG3rvnpPUhFjmqJKAKtjV576KnUkHocg/g=="],
33
- }
34
- }
package/global.d.ts DELETED
@@ -1,11 +0,0 @@
1
- import "hono";
2
- import type { OpacaConfig, User } from "./src/types";
3
- import type { Auth } from "./src/auth/index";
4
-
5
- declare module "hono" {
6
- interface ContextVariableMap {
7
- config: OpacaConfig;
8
- auth: Auth;
9
- user: User;
10
- }
11
- }
@@ -1,63 +0,0 @@
1
- import ky from "ky";
2
-
3
- let client: typeof ky | null = null;
4
- let currentBaseURL: string | null = null;
5
-
6
- export const configureAPI = (serverUrl: string) => {
7
- if (!serverUrl || !serverUrl.startsWith("http")) return;
8
- const url = serverUrl;
9
- const baseURL = url.replace(/\/$/, "");
10
- if (client && currentBaseURL === baseURL) return;
11
-
12
- currentBaseURL = baseURL;
13
- client = ky.create({
14
- prefixUrl: baseURL,
15
- credentials: "include",
16
- retry: 0,
17
- hooks: {
18
- afterResponse: [
19
- async (_request, _options, response) => {
20
- if (response.status === 401 || response.status === 403) {
21
- // Se o auth handler do backend falhar globalmente em qualquer requisição da API (sessão morreu/expirada)
22
- // Lançamos a revalidação da store local para despachar o usuário pra tela de login.
23
- try {
24
- const { $session } = await import("./stores/auth");
25
- const { notify } = await import("./stores/ui");
26
- $session.set(null);
27
- notify("Session expired. Please log in again.", "error");
28
- } catch (e) {}
29
- }
30
- },
31
- ],
32
- },
33
- });
34
- };
35
-
36
- export const getCurrentBaseURL = () => {
37
- if (!currentBaseURL) {
38
- throw new Error("API client not configured. Did you provide a valid serverUrl?");
39
- }
40
- return currentBaseURL as string;
41
- };
42
-
43
- /**
44
- * A proxy for ky that allows us to configure the baseURL late
45
- * (e.g. after we have the serverURL from props).
46
- * @param prop: string
47
- */
48
- export const api = new Proxy({} as typeof ky, {
49
- get(_, prop) {
50
- if (!client) {
51
- // No auto-fallback for web components.
52
- throw new Error("API client not configured. Did you provide a valid serverUrl?");
53
- }
54
-
55
- if (!client) throw new Error("API client not configured");
56
-
57
- const c = client;
58
- if (c) {
59
- return c[prop as keyof typeof ky];
60
- }
61
- return undefined;
62
- },
63
- });
@@ -1,40 +0,0 @@
1
- import { createAuthClient } from "better-auth/client";
2
- import { adminClient } from "better-auth/client/plugins";
3
- import { logger } from "@/utils/logger";
4
-
5
- let client: ReturnType<typeof createAuthClient> | null = null;
6
- let currentURL: string | null = null;
7
-
8
- export const configureAuth = (serverUrl: string) => {
9
- if (!serverUrl || !serverUrl.startsWith("http")) return;
10
- const url = serverUrl;
11
- const baseURL = url.replace(/\/$/, "") + "/api/auth";
12
- if (client && currentURL === baseURL) return;
13
-
14
- currentURL = baseURL;
15
- client = createAuthClient({
16
- baseURL,
17
- autoRefresh: true,
18
- fetchOptions: {
19
- credentials: "include",
20
- },
21
- plugins: [adminClient()],
22
- });
23
- };
24
-
25
- /**
26
- * A proxy for the authClient that allows us to configure it late
27
- * (e.g. after we have the user's config with serverURL).
28
- */
29
- export const authClient = new Proxy({} as ReturnType<typeof createAuthClient>, {
30
- get(_, prop) {
31
- if (!client) {
32
- // No auto-fallback to origin for web components.
33
- // AdminClient will call configureAuth when it has the correct prop.
34
- throw new Error("Auth client not configured. Did you provide a valid serverUrl?");
35
- }
36
- return (client as ReturnType<typeof createAuthClient>)[
37
- prop as keyof ReturnType<typeof createAuthClient>
38
- ];
39
- },
40
- });
@@ -1,179 +0,0 @@
1
- export interface CustomFieldProps<TValue = any, TConfig = Record<string, any>> {
2
- /** The current value of the field from the CMS database */
3
- value: TValue;
4
- /** The full schema configuration of the field */
5
- fieldConfig: TConfig;
6
- /** Whether the field is currently disabled by access control */
7
- disabled: boolean;
8
- /** Whether the field is currently read-only by access control */
9
- readOnly: boolean;
10
- /** A validation error message from the CMS, if any */
11
- error?: string;
12
- /** The current full state of the parent form or row */
13
- parentData?: Record<string, unknown>;
14
- /** Callback to notify the CMS that the data has changed */
15
- onChange: (value: TValue) => void;
16
- }
17
-
18
- export interface CustomFieldAdapter {
19
- /**
20
- * Called exactly once when the field is inserted into the CMS Admin UI.
21
- * Use this to render your React root, Vue app, or Vanilla DOM nodes.
22
- *
23
- * @param container The DOM element acting as the wrapper for your component.
24
- * @param props The initial properties (value, config, onChange) from the CMS.
25
- */
26
- mount: (container: HTMLElement, props: CustomFieldProps) => void;
27
-
28
- /**
29
- * Called whenever the CMS pushes new data to the field (e.g. form resets,
30
- * external value changes).
31
- *
32
- * @param container The same DOM element wrapper.
33
- * @param props The updated properties.
34
- */
35
- update?: (container: HTMLElement, props: CustomFieldProps) => void;
36
-
37
- /**
38
- * Called exactly once when the field is removed from the DOM (e.g., navigating away).
39
- * Use this to unmount your React root or Vue app to prevent memory leaks.
40
- *
41
- * @param container The same DOM element wrapper.
42
- */
43
- unmount?: (container: HTMLElement) => void;
44
- }
45
-
46
- /**
47
- * A built-in utility to register framework-agnostic Custom Web Components for the OpacaCMS Admin UI.
48
- *
49
- * It acts as an adapter, translating the Web Component lifecycle (`connectedCallback`, `disconnectedCallback`,
50
- * property setters) into a simple `mount` / `update` / `unmount` paradigm that can easily wrap React, Vue, Angular,
51
- * or plain Javascript applications.
52
- *
53
- * @example
54
- * ```tsx
55
- * import { defineCustomField } from 'opacacms/admin';
56
- * import { createRoot } from 'react-dom/client';
57
- *
58
- * defineCustomField('my-react-picker', {
59
- * mount: (container, props) => {
60
- * (container as any)._root = createRoot(container);
61
- * (container as any)._root.render(<MyPicker {...props} />);
62
- * },
63
- * update: (container, props) => {
64
- * (container as any)._root.render(<MyPicker {...props} />);
65
- * },
66
- * unmount: (container) => {
67
- * (container as any)._root.unmount();
68
- * }
69
- * });
70
- * ```
71
- *
72
- * @param tagName The tag name to register in the browser (e.g., `opaca-color-picker`). Must contain a hyphen.
73
- * @param adapter The adapter containing `mount`, optional `update`, and optional `unmount` lifecycle methods.
74
- */
75
- export function defineCustomField(tagName: string, adapter: CustomFieldAdapter) {
76
- if (typeof window === "undefined") return;
77
-
78
- if (customElements.get(tagName)) {
79
- console.warn(`[OpacaCMS] Custom field tag "${tagName}" is already defined.`);
80
- return;
81
- }
82
-
83
- class OpacaCustomFieldElement extends HTMLElement {
84
- private _value: any;
85
- private _fieldConfig: any;
86
- private _error?: string;
87
- private _parentData?: Record<string, unknown>;
88
- private _isMounted = false;
89
-
90
- // Helper to abstract the complex CustomEvent away from the user's DX
91
- private handleChange = (newValue: any) => {
92
- this.dispatchEvent(
93
- new CustomEvent("opacachange", {
94
- detail: { value: newValue },
95
- bubbles: true,
96
- composed: true,
97
- }),
98
- );
99
- };
100
-
101
- private getProps(): CustomFieldProps {
102
- return {
103
- value: this._value,
104
- fieldConfig: this._fieldConfig,
105
- disabled: this.getAttribute("data-disabled") === "true",
106
- readOnly: this.getAttribute("data-readonly") === "true",
107
- error: this._error,
108
- parentData: this._parentData,
109
- onChange: this.handleChange,
110
- };
111
- }
112
-
113
- set error(err: string | undefined) {
114
- if (this._error !== err) {
115
- this._error = err;
116
- if (this._isMounted && adapter.update) {
117
- adapter.update(this, this.getProps());
118
- }
119
- }
120
- }
121
-
122
- get error() {
123
- return this._error;
124
- }
125
-
126
- set parentData(data: Record<string, unknown> | undefined) {
127
- this._parentData = data;
128
- if (this._isMounted && adapter.update) {
129
- adapter.update(this, this.getProps());
130
- }
131
- }
132
-
133
- get parentData() {
134
- return this._parentData;
135
- }
136
-
137
- set value(val: any) {
138
- const oldValue = this._value;
139
- this._value = val;
140
- // Only call update if the value actually changed and the component is mounted
141
- if (this._isMounted && adapter.update && oldValue !== val) {
142
- adapter.update(this, this.getProps());
143
- }
144
- }
145
-
146
- get value() {
147
- return this._value;
148
- }
149
-
150
- set fieldConfig(config: any) {
151
- this._fieldConfig = config;
152
- if (this._isMounted && adapter.update) {
153
- adapter.update(this, this.getProps());
154
- }
155
- }
156
-
157
- get fieldConfig() {
158
- return this._fieldConfig;
159
- }
160
-
161
- connectedCallback() {
162
- if (!this._isMounted) {
163
- adapter.mount(this, this.getProps());
164
- this._isMounted = true;
165
- }
166
- }
167
-
168
- disconnectedCallback() {
169
- if (this._isMounted) {
170
- if (adapter.unmount) {
171
- adapter.unmount(this);
172
- }
173
- this._isMounted = false;
174
- }
175
- }
176
- }
177
-
178
- customElements.define(tagName, OpacaCustomFieldElement);
179
- }
@@ -1,15 +0,0 @@
1
- export {
2
- type CustomFieldAdapter,
3
- type CustomFieldProps,
4
- defineCustomField,
5
- } from "./custom-field";
6
- export { defineReactField } from "./react";
7
- export * from "./router";
8
- export * from "./stores/config";
9
- export * from "./stores/query";
10
- export * from "./stores/ui";
11
- export { AdminClient } from "./ui/admin-client";
12
- export { AdminLayout } from "./ui/admin-layout";
13
- export { Breadcrumbs } from "./ui/components/ui/breadcrumbs";
14
- export { Link } from "./ui/components/link";
15
- export { Toast, ToastContainer } from "./ui/components/toast";
@@ -1,72 +0,0 @@
1
- import React from 'react';
2
- import { createRoot, type Root } from 'react-dom/client';
3
- import { type CustomFieldProps, defineCustomField } from './custom-field';
4
-
5
- /**
6
- * A helper utility to quickly register a React component as an OpacaCMS Custom Field.
7
- *
8
- * This function abstracts away the boilerplate of converting a React component into a Custom Element,
9
- * managing the `createRoot` mounting process, and properly passing down props (`value`, `error`, `onChange`, etc.).
10
- *
11
- * @example
12
- * ```tsx
13
- * import { defineReactField } from 'opacacms/admin/react';
14
- *
15
- * const MyPicker = ({ value, onChange, error }) => (
16
- * <div>
17
- * <input value={value || ''} onChange={e => onChange(e.target.value)} />
18
- * {error && <span style={{ color: 'red' }}>{error}</span>}
19
- * </div>
20
- * );
21
- *
22
- * defineReactField('my-react-picker', MyPicker);
23
- * ```
24
- *
25
- * @param tagName The custom element tag name (must contain a hyphen, e.g., 'opaca-custom-picker').
26
- * @param Component The React functional or class component to render.
27
- */
28
- interface OpacaContainer extends HTMLElement {
29
- _opacaReactRoot?: Root;
30
- }
31
-
32
- export function defineReactField<TValue = any, TConfig = Record<string, any>>(
33
- tagName: string,
34
- Component: React.ComponentType<CustomFieldProps<TValue, TConfig>>,
35
- ) {
36
- defineCustomField(tagName, {
37
- mount: (container, props) => {
38
- const el = container as OpacaContainer;
39
- const root = createRoot(el);
40
- el._opacaReactRoot = root;
41
- root.render(
42
- React.createElement(
43
- Component,
44
- props as CustomFieldProps<TValue, TConfig>,
45
- ),
46
- );
47
- },
48
- update: (container, props) => {
49
- const el = container as OpacaContainer;
50
- const root = el._opacaReactRoot;
51
- if (root) {
52
- root.render(
53
- React.createElement(
54
- Component,
55
- props as CustomFieldProps<TValue, TConfig>,
56
- ),
57
- );
58
- }
59
- },
60
- unmount: (container) => {
61
- const el = container as OpacaContainer;
62
- const root = el._opacaReactRoot;
63
- if (root) {
64
- // Schedule unmount to avoid synchronous React 18+ unmount warnings during DOM teardown
65
- setTimeout(() => {
66
- root.unmount();
67
- delete el._opacaReactRoot;
68
- }, 0);
69
- }
70
- },
71
- });
72
- }
@@ -1,9 +0,0 @@
1
- import { createRouter } from "@nanostores/router";
2
-
3
- export const $router = createRouter({
4
- dashboard: "/admin",
5
- collections: "/admin/collections/:slug",
6
- document: "/admin/collections/:slug/:id",
7
- globals: "/admin/globals/:slug",
8
- settings: "/admin/settings",
9
- });
@@ -1,121 +0,0 @@
1
- import { atom, computed } from "nanostores";
2
- import type { PaginatedResult } from "../../types";
3
- import { api } from "../api-client";
4
- import { createFetcherStore, createMutatorStore } from "./query";
5
-
6
- // --- Collection List ---
7
- export interface CollectionQueryParams {
8
- slug: string;
9
- page?: number;
10
- limit?: number;
11
- sort?: string;
12
- search?: string;
13
- searchField?: string;
14
- filter?: Record<string, unknown>;
15
- populate?: string;
16
- }
17
-
18
- export const $collectionQuery = atom<CollectionQueryParams | null>(null);
19
-
20
- const $collectionUrl = computed($collectionQuery, (params) => {
21
- if (!params) return null;
22
-
23
- const searchParams = new URLSearchParams();
24
- if (params.page) searchParams.set("page", params.page.toString());
25
- if (params.limit) searchParams.set("limit", params.limit.toString());
26
- if (params.sort) searchParams.set("sort", params.sort);
27
-
28
- if (params.filter) {
29
- for (const [key, value] of Object.entries(params.filter)) {
30
- if (typeof value === "object" && value !== null) {
31
- // Handle nested filter operators like { status: { equals: 'draft' } }
32
- for (const [op, val] of Object.entries(value)) {
33
- searchParams.set(`${key}[${op}]`, String(val));
34
- }
35
- } else {
36
- searchParams.set(`${key}[equals]`, String(value));
37
- }
38
- }
39
- }
40
-
41
- if (params.search && params.searchField) {
42
- searchParams.set(`${params.searchField}[like]`, `%${params.search}%`);
43
- } else if (params.search) {
44
- searchParams.set("search", params.search);
45
- }
46
-
47
- if (params.populate) {
48
- searchParams.set("populate", params.populate);
49
- }
50
-
51
- const query = searchParams.toString();
52
- return `api/${params.slug}${query ? `?${query}` : ""}`;
53
- });
54
-
55
- export const $collectionData = createFetcherStore<PaginatedResult<Record<string, unknown>>>([
56
- $collectionUrl,
57
- ]);
58
-
59
- // --- Document Edit ---
60
- export interface DocumentQueryParams {
61
- slug: string;
62
- id: string;
63
- }
64
-
65
- export const $documentQuery = atom<DocumentQueryParams | null>(null);
66
-
67
- const $documentUrl = computed($documentQuery, (params) => {
68
- if (!params) return null;
69
- return `api/${params.slug}/${params.id}`;
70
- });
71
-
72
- export const $documentData = createFetcherStore<Record<string, unknown>>([$documentUrl]);
73
-
74
- export interface SaveDocumentParams {
75
- slug: string;
76
- id: string;
77
- body: Record<string, unknown>;
78
- }
79
-
80
- export const $saveDocument = createMutatorStore<SaveDocumentParams, Record<string, unknown>>(
81
- async ({ data, revalidate }) => {
82
- const { slug, id, body } = data;
83
- const method = id === "create" ? "post" : "patch";
84
- const url = `api/${slug}${id === "create" ? "" : `/${id}`}`;
85
-
86
- const res = await api[method](url, { json: body }).json<Record<string, unknown>>();
87
-
88
- // Revalidate the list and the document
89
- revalidate(`api/${slug}`);
90
- revalidate(`api/${slug}/${id}`);
91
-
92
- return res;
93
- },
94
- );
95
-
96
- // --- Global Edit ---
97
- export const $globalQuery = atom<string | null>(null);
98
-
99
- const $globalUrl = computed($globalQuery, (slug) => {
100
- if (!slug) return null;
101
- return `api/globals/${slug}`;
102
- });
103
-
104
- export const $globalData = createFetcherStore<Record<string, unknown>>([$globalUrl]);
105
-
106
- export interface SaveGlobalParams {
107
- slug: string;
108
- body: Record<string, unknown>;
109
- }
110
-
111
- export const $saveGlobal = createMutatorStore<SaveGlobalParams, Record<string, unknown>>(
112
- async ({ data, revalidate }) => {
113
- const { slug, body } = data;
114
- const res = await api
115
- .patch(`api/globals/${slug}`, { json: body })
116
- .json<Record<string, unknown>>();
117
-
118
- revalidate(`api/globals/${slug}`);
119
- return res;
120
- },
121
- );
@@ -1,61 +0,0 @@
1
- import { atom, computed } from "nanostores";
2
- import { authClient } from "../auth-client";
3
- import { notify } from "./ui";
4
-
5
- export const $session = atom<any>(null);
6
- export const $isAuthPending = atom(true);
7
-
8
- export const $user = computed($session, (session) => session?.user ?? null);
9
- export const $isAuthenticated = computed($session, (session) => !!session);
10
-
11
- // Synchronize session
12
- export async function syncSession() {
13
- $isAuthPending.set(true);
14
- try {
15
- const res = await authClient.getSession();
16
- $session.set(res.data);
17
- } catch (err) {
18
- console.error("[OpacaCMS] Failed to sync session:", err);
19
- } finally {
20
- $isAuthPending.set(false);
21
- }
22
- }
23
-
24
- export async function login(data: Record<string, string>) {
25
- $isAuthPending.set(true);
26
- try {
27
- const res = await authClient.signIn.email({
28
- email: data.email as string,
29
- password: data.password as string,
30
- });
31
-
32
- if (res.error) {
33
- throw new Error(res.error.message || "Invalid credentials");
34
- }
35
-
36
- // Force re-fetch the session from the server immediately after successful sign-in
37
- // so the Nano Store reacts and the AdminClient flips from LoginView to DashboardView.
38
- await syncSession();
39
-
40
- notify("Logged in successfully", "success");
41
- return res;
42
- } catch (error) {
43
- notify(error instanceof Error ? error.message : "Login failed", "error");
44
- throw error;
45
- } finally {
46
- $isAuthPending.set(false);
47
- }
48
- }
49
-
50
- export async function logout() {
51
- $isAuthPending.set(true);
52
- try {
53
- await authClient.signOut();
54
- $session.set(null);
55
- notify("Logged out successfully");
56
- } catch {
57
- notify("Logout failed", "error");
58
- } finally {
59
- $isAuthPending.set(false);
60
- }
61
- }