@nitrogenbuilder/connector-payload 0.1.2 → 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.
@@ -0,0 +1,10 @@
1
+ import React from "react";
2
+ export declare const LockedJsonField: React.FC<{
3
+ path: string;
4
+ field: {
5
+ name: string;
6
+ admin?: {
7
+ description?: string;
8
+ };
9
+ };
10
+ }>;
@@ -0,0 +1,38 @@
1
+ "use client";
2
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
3
+ import { useState } from "react";
4
+ import { useField } from "@payloadcms/ui";
5
+ export const LockedJsonField = ({ path, field }) => {
6
+ const [unlocked, setUnlocked] = useState(false);
7
+ const { value, setValue } = useField({ path });
8
+ const displayValue = value ? JSON.stringify(value, null, 2) : "";
9
+ return (_jsxs("div", { style: { marginBottom: "1.5rem" }, children: [_jsxs("div", { style: { display: "flex", alignItems: "center", justifyContent: "space-between", marginBottom: "0.5rem" }, children: [_jsx("label", { style: { fontWeight: 500 }, children: field.name }), _jsx("button", { type: "button", onClick: () => setUnlocked(!unlocked), style: {
10
+ padding: "4px 12px",
11
+ fontSize: "12px",
12
+ background: unlocked ? "#ef4444" : "#6b7280",
13
+ color: "#fff",
14
+ border: "none",
15
+ borderRadius: "4px",
16
+ cursor: "pointer",
17
+ }, children: unlocked ? "Lock" : "Unlock Edit" })] }), _jsx("textarea", { value: displayValue, readOnly: !unlocked, onChange: (e) => {
18
+ if (!unlocked)
19
+ return;
20
+ try {
21
+ setValue(JSON.parse(e.target.value));
22
+ }
23
+ catch {
24
+ // Allow typing invalid JSON while editing
25
+ }
26
+ }, style: {
27
+ width: "100%",
28
+ minHeight: "120px",
29
+ fontFamily: "monospace",
30
+ fontSize: "12px",
31
+ padding: "8px",
32
+ background: unlocked ? "#1a1a2e" : "#111",
33
+ color: unlocked ? "#e2e8f0" : "#888",
34
+ border: `1px solid ${unlocked ? "#ef4444" : "#333"}`,
35
+ borderRadius: "4px",
36
+ resize: "vertical",
37
+ } }), field.admin?.description && (_jsx("p", { style: { fontSize: "12px", color: "#888", marginTop: "4px" }, children: field.admin.description }))] }));
38
+ };
@@ -1,9 +1,10 @@
1
1
  "use client";
2
- import { jsx as _jsx } from "react/jsx-runtime";
2
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
3
3
  import { useState, useEffect } from "react";
4
4
  export const NitrogenEditButton = ({ collection }) => {
5
5
  const [id, setId] = useState(undefined);
6
6
  const [token, setToken] = useState(undefined);
7
+ const [hasDevelopmentUrl, setHasDevelopmentUrl] = useState(false);
7
8
  useEffect(() => {
8
9
  const segments = window.location.pathname.split("/").filter(Boolean);
9
10
  // segments: ["admin", "collections", "{collection-slug}", "{id}"]
@@ -17,6 +18,9 @@ export const NitrogenEditButton = ({ collection }) => {
17
18
  if (data.connectorToken) {
18
19
  setToken(data.connectorToken);
19
20
  }
21
+ if (data.developmentUrl) {
22
+ setHasDevelopmentUrl(true);
23
+ }
20
24
  })
21
25
  .catch((err) => {
22
26
  console.error("Failed to fetch nitrogen settings:", err);
@@ -26,18 +30,24 @@ export const NitrogenEditButton = ({ collection }) => {
26
30
  return null;
27
31
  // Determine which query param to use based on collection type
28
32
  const param = collection === "nitrogen-templates" ? "templateId" : "pageId";
29
- // Always pass the collection slug so the editor knows exactly which collection to query.
30
- const href = `/nitrogen-editor?token=${encodeURIComponent(token)}&collection=${encodeURIComponent(collection)}&${param}=${id}`;
31
- return (_jsx("a", { href: href, target: "_blank", rel: "noopener noreferrer", style: {
32
- display: "inline-flex",
33
- alignItems: "center",
34
- gap: "8px",
35
- padding: "8px 16px",
36
- background: "#6366f1",
37
- color: "#fff",
38
- borderRadius: "4px",
39
- textDecoration: "none",
40
- fontSize: "14px",
41
- fontWeight: 500,
42
- }, children: "Edit with Nitrogen" }));
33
+ const baseHref = `/nitrogen-editor?token=${encodeURIComponent(token)}&collection=${encodeURIComponent(collection)}&${param}=${id}`;
34
+ const buttonStyle = {
35
+ display: "inline-flex",
36
+ alignItems: "center",
37
+ gap: "8px",
38
+ padding: "8px 16px",
39
+ background: "#6366f1",
40
+ color: "#fff",
41
+ borderRadius: "4px",
42
+ textDecoration: "none",
43
+ fontSize: "14px",
44
+ fontWeight: 500,
45
+ };
46
+ if (!hasDevelopmentUrl) {
47
+ return (_jsx("a", { href: baseHref, target: "_blank", rel: "noopener noreferrer", style: buttonStyle, children: "Edit with Nitrogen" }));
48
+ }
49
+ return (_jsxs("div", { style: { display: "flex", gap: "8px" }, children: [_jsx("a", { href: baseHref, target: "_blank", rel: "noopener noreferrer", style: buttonStyle, children: "Edit (Live)" }), _jsx("a", { href: `${baseHref}&development=true`, target: "_blank", rel: "noopener noreferrer", style: {
50
+ ...buttonStyle,
51
+ background: "#374151",
52
+ }, children: "Edit (Dev)" })] }));
43
53
  };
@@ -0,0 +1,4 @@
1
+ import React from "react";
2
+ export declare const NitrogenViewButton: React.FC<{
3
+ collection: string;
4
+ }>;
@@ -0,0 +1,52 @@
1
+ "use client";
2
+ import { jsx as _jsx } from "react/jsx-runtime";
3
+ import { useState, useEffect } from "react";
4
+ export const NitrogenViewButton = ({ collection }) => {
5
+ const [slug, setSlug] = useState(undefined);
6
+ const [frontendUrl, setFrontendUrl] = useState(undefined);
7
+ useEffect(() => {
8
+ // Get the document ID from the URL
9
+ const segments = window.location.pathname.split("/").filter(Boolean);
10
+ const docId = segments.length >= 4 ? segments[3] : undefined;
11
+ if (!docId)
12
+ return;
13
+ // Fetch the document to get its slug
14
+ fetch(`/api/${collection}/${docId}`)
15
+ .then((res) => res.json())
16
+ .then((data) => {
17
+ if (data.slug) {
18
+ setSlug(data.slug);
19
+ }
20
+ })
21
+ .catch((err) => {
22
+ console.error("Failed to fetch document:", err);
23
+ });
24
+ // Fetch nitrogen settings to get the frontend URL
25
+ fetch("/api/globals/nitrogen-settings")
26
+ .then((res) => res.json())
27
+ .then((data) => {
28
+ const url = data.developmentUrl || data.frontendUrl;
29
+ if (url) {
30
+ setFrontendUrl(url.replace(/\/$/, ""));
31
+ }
32
+ })
33
+ .catch((err) => {
34
+ console.error("Failed to fetch nitrogen settings:", err);
35
+ });
36
+ }, [collection]);
37
+ if (!slug || !frontendUrl)
38
+ return null;
39
+ const href = `${frontendUrl}/${slug}`;
40
+ return (_jsx("a", { href: href, target: "_blank", rel: "noopener noreferrer", style: {
41
+ display: "inline-flex",
42
+ alignItems: "center",
43
+ gap: "8px",
44
+ padding: "8px 16px",
45
+ background: "#10b981",
46
+ color: "#fff",
47
+ borderRadius: "4px",
48
+ textDecoration: "none",
49
+ fontSize: "14px",
50
+ fontWeight: 500,
51
+ }, children: "View Page" }));
52
+ };
package/dist/index.d.ts CHANGED
@@ -15,6 +15,7 @@ export declare const nitrogenConnectorPlugin: (options?: NitrogenConnectorPlugin
15
15
  export { NitrogenTemplates } from "./collections/NitrogenTemplates";
16
16
  export { NitrogenSettings } from "./globals/NitrogenSettings";
17
17
  export { NitrogenEditButton } from "./components/NitrogenEditButton";
18
+ export { NitrogenViewButton } from "./components/NitrogenViewButton";
18
19
  export { buildDynamicData, getNitrogenSettings } from "./endpoints/helpers";
19
20
  export { createCollectionEndpoints } from "./endpoints/collection-endpoints";
20
21
  export { nitrogen } from "@nitrogenbuilder/client-core";
package/dist/index.js CHANGED
@@ -12,25 +12,23 @@ const nitrogenRequiredFields = [
12
12
  {
13
13
  name: "nitrogenData",
14
14
  type: "json",
15
- admin: { description: "Nitrogen page builder JSON module tree" },
15
+ admin: {
16
+ description: "Nitrogen page builder JSON module tree",
17
+ components: {
18
+ Field: "nitrogen-connector-payload/components/LockedJsonField#LockedJsonField",
19
+ },
20
+ },
16
21
  },
17
22
  {
18
23
  name: "pageSettings",
19
24
  type: "json",
20
- admin: { description: "Page-level Nitrogen settings" },
21
- },
22
- { name: "title", type: "text", required: true },
23
- { name: "slug", type: "text", required: true, unique: true, index: true },
24
- {
25
- name: "status",
26
- type: "select",
27
- options: [
28
- { label: "Draft", value: "draft" },
29
- { label: "Published", value: "published" },
30
- ],
31
- defaultValue: "draft",
25
+ admin: {
26
+ description: "Page-level Nitrogen settings",
27
+ components: {
28
+ Field: "nitrogen-connector-payload/components/LockedJsonField#LockedJsonField",
29
+ },
30
+ },
32
31
  },
33
- { name: "author", type: "relationship", relationTo: "users" },
34
32
  ];
35
33
  export const nitrogenConnectorPlugin = (options = {}) => (incomingConfig) => {
36
34
  if (options.disabled) {
@@ -111,7 +109,11 @@ export const nitrogenConnectorPlugin = (options = {}) => (incomingConfig) => {
111
109
  beforeDocumentControls: [
112
110
  ...beforeControls,
113
111
  {
114
- path: "@nitrogenbuilder/connector-payload/components/NitrogenEditButton#NitrogenEditButton",
112
+ path: "nitrogen-connector-payload/components/NitrogenViewButton#NitrogenViewButton",
113
+ clientProps: { collection: slug },
114
+ },
115
+ {
116
+ path: "nitrogen-connector-payload/components/NitrogenEditButton#NitrogenEditButton",
115
117
  clientProps: { collection: slug },
116
118
  },
117
119
  ],
@@ -132,6 +134,7 @@ export { NitrogenTemplates } from "./collections/NitrogenTemplates";
132
134
  export { NitrogenSettings } from "./globals/NitrogenSettings";
133
135
  // Editor exports
134
136
  export { NitrogenEditButton } from "./components/NitrogenEditButton";
137
+ export { NitrogenViewButton } from "./components/NitrogenViewButton";
135
138
  // Helper exports for consumers building custom frontend routes
136
139
  export { buildDynamicData, getNitrogenSettings } from "./endpoints/helpers";
137
140
  // Collection endpoint factory for consumers who need custom endpoint generation
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nitrogenbuilder/connector-payload",
3
- "version": "0.1.2",
3
+ "version": "0.1.3",
4
4
  "description": "Nitrogen page builder connector plugin for Payload CMS 3.x",
5
5
  "author": "Leonardo Dentzien <leo@torchmedia.ca>",
6
6
  "type": "module",