@nitrogenbuilder/connector-payload 0.1.31 → 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.
- package/dist/components/NitrogenEditButton.d.ts +1 -0
- package/dist/components/NitrogenEditButtonRuntime.d.ts +1 -0
- package/dist/components/NitrogenEditButtonRuntime.js +62 -6
- package/dist/components/NitrogenViewButton.d.ts +1 -0
- package/dist/components/NitrogenViewButtonRuntime.d.ts +1 -0
- package/dist/components/NitrogenViewButtonRuntime.js +60 -3
- package/dist/index.js +1 -1
- package/package.json +1 -1
|
@@ -15,16 +15,44 @@ function getPreferredSiteUrl(data) {
|
|
|
15
15
|
const fallbackUrl = isLocalAdmin ? data.frontendUrl : data.developmentUrl;
|
|
16
16
|
return preferredUrl || fallbackUrl || '';
|
|
17
17
|
}
|
|
18
|
-
|
|
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
|
|
77
|
-
?
|
|
78
|
-
:
|
|
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 &&
|
|
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,
|
|
@@ -12,12 +12,40 @@ function getPreferredSiteUrl(data) {
|
|
|
12
12
|
const fallbackUrl = isLocalAdmin ? data.frontendUrl : data.developmentUrl;
|
|
13
13
|
return preferredUrl || fallbackUrl || '';
|
|
14
14
|
}
|
|
15
|
-
|
|
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
|
-
|
|
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" }));
|
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/package.json
CHANGED