@nitrogenbuilder/connector-payload 0.1.30 → 0.1.32

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.
@@ -81,6 +81,16 @@ export const NitrogenTemplates = {
81
81
  },
82
82
  },
83
83
  },
84
+ {
85
+ name: 'templateConditions',
86
+ type: 'json',
87
+ admin: {
88
+ description: 'Template conditions used by the Nitrogen editor',
89
+ components: {
90
+ Field: '@nitrogenbuilder/connector-payload/components/NitrogenDataViewer',
91
+ },
92
+ },
93
+ },
84
94
  ],
85
95
  },
86
96
  ],
@@ -1,3 +1,4 @@
1
1
  export declare const NitrogenEditButton: import("react").ComponentType<{
2
2
  collection: string;
3
+ routePattern?: string;
3
4
  }>;
@@ -1,4 +1,5 @@
1
1
  import React from 'react';
2
2
  export declare const NitrogenEditButtonRuntime: React.FC<{
3
3
  collection: string;
4
+ routePattern?: string;
4
5
  }>;
@@ -15,16 +15,44 @@ function getPreferredSiteUrl(data) {
15
15
  const fallbackUrl = isLocalAdmin ? data.frontendUrl : data.developmentUrl;
16
16
  return preferredUrl || fallbackUrl || '';
17
17
  }
18
- export const NitrogenEditButtonRuntime = ({ collection }) => {
18
+ function normalizeRoutePattern(routePattern) {
19
+ if (!routePattern)
20
+ return undefined;
21
+ return routePattern.startsWith('/') ? routePattern : `/${routePattern}`;
22
+ }
23
+ function trimSlashes(value) {
24
+ return value.replace(/^\/+|\/+$/g, '');
25
+ }
26
+ function buildRelativePath(slug, routePattern, templateSlug) {
27
+ const normalizedRoutePattern = normalizeRoutePattern(routePattern);
28
+ if (normalizedRoutePattern) {
29
+ if (normalizedRoutePattern.includes('[...slug]')) {
30
+ return normalizedRoutePattern.replace('[...slug]', slug);
31
+ }
32
+ if (normalizedRoutePattern.includes('[slug]')) {
33
+ return normalizedRoutePattern.replace('[slug]', slug);
34
+ }
35
+ return normalizedRoutePattern.endsWith('/')
36
+ ? `${normalizedRoutePattern}${slug}`
37
+ : normalizedRoutePattern;
38
+ }
39
+ if (templateSlug) {
40
+ return `/${trimSlashes(templateSlug)}/${trimSlashes(slug)}`;
41
+ }
42
+ return `/${trimSlashes(slug)}`;
43
+ }
44
+ export const NitrogenEditButtonRuntime = ({ collection, routePattern }) => {
19
45
  const [id, setId] = useState(undefined);
20
46
  const [token, setToken] = useState(undefined);
21
47
  const [slug, setSlug] = useState(undefined);
22
48
  const [frontendUrl, setFrontendUrl] = useState(undefined);
23
49
  const [instanceUrl, setInstanceUrl] = useState(undefined);
24
50
  const [authorId, setAuthorId] = useState(undefined);
51
+ const [templateSlug, setTemplateSlug] = useState(undefined);
25
52
  useEffect(() => {
26
53
  const segments = window.location.pathname.split('/').filter(Boolean);
27
54
  const docId = segments.length >= 4 ? segments[3] : undefined;
55
+ const shouldResolveTemplateSlug = collection !== 'nitrogen-templates' && !routePattern;
28
56
  if (docId) {
29
57
  setId(docId);
30
58
  fetch(`/api/${collection}/${docId}`)
@@ -38,6 +66,29 @@ export const NitrogenEditButtonRuntime = ({ collection }) => {
38
66
  console.error('Failed to fetch document:', err);
39
67
  });
40
68
  }
69
+ setTemplateSlug(undefined);
70
+ if (shouldResolveTemplateSlug) {
71
+ const params = new URLSearchParams();
72
+ params.set('where[associatedCollection][equals]', collection);
73
+ params.set('where[status][equals]', 'published');
74
+ params.set('limit', '1');
75
+ params.set('depth', '0');
76
+ fetch(`/api/nitrogen-templates?${params.toString()}`)
77
+ .then((res) => res.json())
78
+ .then((data) => {
79
+ const doc = Array.isArray(data?.docs) ? data.docs[0] : undefined;
80
+ if (typeof doc?.slug === 'string') {
81
+ setTemplateSlug(doc.slug);
82
+ }
83
+ else {
84
+ setTemplateSlug(null);
85
+ }
86
+ })
87
+ .catch((err) => {
88
+ console.error('Failed to fetch nitrogen template:', err);
89
+ setTemplateSlug(null);
90
+ });
91
+ }
41
92
  fetch('/api/users/me')
42
93
  .then((res) => res.json())
43
94
  .then((data) => {
@@ -65,7 +116,7 @@ export const NitrogenEditButtonRuntime = ({ collection }) => {
65
116
  .catch((err) => {
66
117
  console.error('Failed to fetch nitrogen settings:', err);
67
118
  });
68
- }, [collection]);
119
+ }, [collection, routePattern]);
69
120
  if (!id || !token || !authorId)
70
121
  return null;
71
122
  const editorBase = instanceUrl ?? '';
@@ -73,13 +124,18 @@ export const NitrogenEditButtonRuntime = ({ collection }) => {
73
124
  const encodedAuthorId = encodeURIComponent(authorId);
74
125
  const developmentFlags = isDevelopmentMode ? '&development=true' : '';
75
126
  const editHref = `${editorBase}/nitrogen-editor?token=${encodeURIComponent(token)}&collection=${encodeURIComponent(collection)}&pageId=${id}&authorId=${encodedAuthorId}&author=${encodedAuthorId}${developmentFlags}`;
76
- const viewHref = collection === 'nitrogen-templates'
77
- ? `${frontendUrl}/nitrogen-templates/${slug}`
78
- : `${frontendUrl}/${slug}`;
127
+ const viewRelativePath = collection === 'nitrogen-templates'
128
+ ? `/nitrogen-templates/${trimSlashes(slug || '')}`
129
+ : buildRelativePath(slug || '', routePattern, templateSlug ?? undefined);
130
+ const viewHref = `${frontendUrl}${viewRelativePath}`;
79
131
  const editButtonVars = isDevelopmentMode
80
132
  ? nitrogenEditDevButtonVars
81
133
  : nitrogenEditLiveButtonVars;
82
- return (_jsxs("div", { style: nitrogenButtonGroupStyle, children: [slug && frontendUrl && (_jsx(Button, { buttonStyle: "primary", el: "anchor", margin: false, newTab: true, url: viewHref, extraButtonProps: {
134
+ return (_jsxs("div", { style: nitrogenButtonGroupStyle, children: [slug &&
135
+ frontendUrl &&
136
+ (collection === 'nitrogen-templates' ||
137
+ routePattern ||
138
+ templateSlug !== undefined) && (_jsx(Button, { buttonStyle: "primary", el: "anchor", margin: false, newTab: true, url: viewHref, extraButtonProps: {
83
139
  style: nitrogenViewButtonVars,
84
140
  }, children: collection === 'nitrogen-templates' ? 'View Template' : 'View Page' })), _jsx(Button, { buttonStyle: "primary", el: "anchor", margin: false, newTab: true, url: editHref, extraButtonProps: {
85
141
  style: editButtonVars,
@@ -1,3 +1,4 @@
1
1
  export declare const NitrogenViewButton: import("react").ComponentType<{
2
2
  collection: string;
3
+ routePattern?: string;
3
4
  }>;
@@ -1,4 +1,5 @@
1
1
  import React from 'react';
2
2
  export declare const NitrogenViewButtonRuntime: React.FC<{
3
3
  collection: string;
4
+ routePattern?: string;
4
5
  }>;
@@ -12,12 +12,40 @@ function getPreferredSiteUrl(data) {
12
12
  const fallbackUrl = isLocalAdmin ? data.frontendUrl : data.developmentUrl;
13
13
  return preferredUrl || fallbackUrl || '';
14
14
  }
15
- export const NitrogenViewButtonRuntime = ({ collection }) => {
15
+ function normalizeRoutePattern(routePattern) {
16
+ if (!routePattern)
17
+ return undefined;
18
+ return routePattern.startsWith('/') ? routePattern : `/${routePattern}`;
19
+ }
20
+ function trimSlashes(value) {
21
+ return value.replace(/^\/+|\/+$/g, '');
22
+ }
23
+ function buildRelativePath(slug, routePattern, templateSlug) {
24
+ const normalizedRoutePattern = normalizeRoutePattern(routePattern);
25
+ if (normalizedRoutePattern) {
26
+ if (normalizedRoutePattern.includes('[...slug]')) {
27
+ return normalizedRoutePattern.replace('[...slug]', slug);
28
+ }
29
+ if (normalizedRoutePattern.includes('[slug]')) {
30
+ return normalizedRoutePattern.replace('[slug]', slug);
31
+ }
32
+ return normalizedRoutePattern.endsWith('/')
33
+ ? `${normalizedRoutePattern}${slug}`
34
+ : normalizedRoutePattern;
35
+ }
36
+ if (templateSlug) {
37
+ return `/${trimSlashes(templateSlug)}/${trimSlashes(slug)}`;
38
+ }
39
+ return `/${trimSlashes(slug)}`;
40
+ }
41
+ export const NitrogenViewButtonRuntime = ({ collection, routePattern }) => {
16
42
  const [slug, setSlug] = useState(undefined);
17
43
  const [siteUrl, setSiteUrl] = useState(undefined);
44
+ const [templateSlug, setTemplateSlug] = useState(undefined);
18
45
  useEffect(() => {
19
46
  const segments = window.location.pathname.split('/').filter(Boolean);
20
47
  const docId = segments.length >= 4 ? segments[3] : undefined;
48
+ const shouldResolveTemplateSlug = collection !== 'nitrogen-templates' && !routePattern;
21
49
  if (!docId)
22
50
  return;
23
51
  fetch(`/api/${collection}/${docId}`)
@@ -30,6 +58,29 @@ export const NitrogenViewButtonRuntime = ({ collection }) => {
30
58
  .catch((err) => {
31
59
  console.error('Failed to fetch document:', err);
32
60
  });
61
+ setTemplateSlug(undefined);
62
+ if (shouldResolveTemplateSlug) {
63
+ const params = new URLSearchParams();
64
+ params.set('where[associatedCollection][equals]', collection);
65
+ params.set('where[status][equals]', 'published');
66
+ params.set('limit', '1');
67
+ params.set('depth', '0');
68
+ fetch(`/api/nitrogen-templates?${params.toString()}`)
69
+ .then((res) => res.json())
70
+ .then((data) => {
71
+ const doc = Array.isArray(data?.docs) ? data.docs[0] : undefined;
72
+ if (typeof doc?.slug === 'string') {
73
+ setTemplateSlug(doc.slug);
74
+ }
75
+ else {
76
+ setTemplateSlug(null);
77
+ }
78
+ })
79
+ .catch((err) => {
80
+ console.error('Failed to fetch nitrogen template:', err);
81
+ setTemplateSlug(null);
82
+ });
83
+ }
33
84
  fetch('/api/globals/nitrogen-settings')
34
85
  .then((res) => res.json())
35
86
  .then((data) => {
@@ -41,11 +92,17 @@ export const NitrogenViewButtonRuntime = ({ collection }) => {
41
92
  .catch((err) => {
42
93
  console.error('Failed to fetch nitrogen settings:', err);
43
94
  });
44
- }, [collection]);
95
+ }, [collection, routePattern]);
45
96
  if (!slug || !siteUrl) {
46
97
  return null;
47
98
  }
48
- const href = `${siteUrl}/${slug}`;
99
+ if (collection !== 'nitrogen-templates' && !routePattern && templateSlug === undefined) {
100
+ return null;
101
+ }
102
+ const relativePath = collection === 'nitrogen-templates'
103
+ ? `/nitrogen-templates/${trimSlashes(slug)}`
104
+ : buildRelativePath(slug, routePattern, templateSlug ?? undefined);
105
+ const href = `${siteUrl}${relativePath}`;
49
106
  return (_jsx(Button, { buttonStyle: "primary", el: "anchor", margin: false, newTab: true, url: href, extraButtonProps: {
50
107
  style: nitrogenViewButtonVars,
51
108
  }, children: "View Page" }));
@@ -3,6 +3,9 @@ import { buildPermalink, buildRelativePermalink } from '../collection-registry';
3
3
  function isContentTemplateCollection(collection) {
4
4
  return !!collection && collection !== 'header' && collection !== 'footer';
5
5
  }
6
+ function getTemplateConditions(doc) {
7
+ return doc.templateConditions ?? { conditionGroups: [] };
8
+ }
6
9
  function getTemplatePreviewTarget(doc, settings, associatedCollection, associatedDoc) {
7
10
  const previewCollection = associatedDoc && isContentTemplateCollection(associatedCollection)
8
11
  ? associatedCollection
@@ -65,6 +68,7 @@ export const templatesEndpoints = [
65
68
  permalink: previewTarget.permalink,
66
69
  relative_permalink: previewTarget.relative_permalink,
67
70
  postType: associatedCollection || '',
71
+ templateConditions: getTemplateConditions(doc),
68
72
  headerTemplate: isContentTemplateCollection(associatedCollection) ? headerTemplate : null,
69
73
  footerTemplate: isContentTemplateCollection(associatedCollection) ? footerTemplate : null,
70
74
  });
@@ -151,6 +155,7 @@ export const templatesEndpoints = [
151
155
  permalink: previewTarget.permalink,
152
156
  relative_permalink: previewTarget.relative_permalink,
153
157
  postType: associatedCollection || '',
158
+ templateConditions: getTemplateConditions(doc),
154
159
  headerTemplate: isContentTemplateCollection(associatedCollection) ? headerTemplate : null,
155
160
  footerTemplate: isContentTemplateCollection(associatedCollection) ? footerTemplate : null,
156
161
  });
@@ -172,10 +177,13 @@ export const templatesEndpoints = [
172
177
  const id = routeParams?.id;
173
178
  const body = (await req.json?.());
174
179
  const data = (body?.data && typeof body.data === 'object') ? body.data : body;
175
- if (!data?.title) {
176
- return Response.json({ error: 'Title is required' }, { status: 400 });
180
+ if (!data) {
181
+ return Response.json({ error: 'No update data provided' }, { status: 400 });
182
+ }
183
+ const updateData = {};
184
+ if (data.title !== undefined) {
185
+ updateData.title = data.title;
177
186
  }
178
- const updateData = { title: data.title };
179
187
  if (data.author) {
180
188
  updateData.author = data.author;
181
189
  }
@@ -189,6 +197,12 @@ export const templatesEndpoints = [
189
197
  if (data.settings !== undefined) {
190
198
  updateData.templateSettings = data.settings;
191
199
  }
200
+ if (data.conditions !== undefined) {
201
+ updateData.templateConditions = data.conditions;
202
+ }
203
+ if (Object.keys(updateData).length === 0) {
204
+ return Response.json({ error: 'No update data provided' }, { status: 400 });
205
+ }
192
206
  await payload.update({
193
207
  collection: 'nitrogen-templates',
194
208
  id,
package/dist/index.js CHANGED
@@ -120,7 +120,7 @@ export const nitrogenConnectorPlugin = (options = {}) => (incomingConfig) => {
120
120
  ...beforeControls,
121
121
  {
122
122
  path: "@nitrogenbuilder/connector-payload/components/NitrogenEditButton#NitrogenEditButton",
123
- clientProps: { collection: slug },
123
+ clientProps: { collection: slug, routePattern },
124
124
  },
125
125
  ],
126
126
  },
package/dist/types.d.ts CHANGED
@@ -28,6 +28,7 @@ export interface NitrogenTemplateDoc extends NitrogenDocBase {
28
28
  templateType?: string;
29
29
  associatedCollection?: string;
30
30
  templateSettings?: JsonObject;
31
+ templateConditions?: JsonObject;
31
32
  }
32
33
  export interface NitrogenSettingsGlobal {
33
34
  licenseKey?: string;
@@ -99,6 +100,7 @@ export interface TemplateUpdateRequestBody {
99
100
  data_content?: string | JsonObject[];
100
101
  templateType?: string;
101
102
  settings?: JsonObject;
103
+ conditions?: JsonObject;
102
104
  }
103
105
  interface TemplateUpdateData {
104
106
  title?: string;
@@ -106,6 +108,7 @@ interface TemplateUpdateData {
106
108
  data?: string | JsonObject[];
107
109
  templateType?: string;
108
110
  settings?: JsonObject;
111
+ conditions?: JsonObject;
109
112
  }
110
113
  export interface SlugLookupRequestBody {
111
114
  slug?: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nitrogenbuilder/connector-payload",
3
- "version": "0.1.30",
3
+ "version": "0.1.32",
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",