@cntrl-site/sdk-nextjs 0.10.1 → 0.11.0
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/cntrl-site-sdk-nextjs-0.10.2.tgz +0 -0
- package/lib/components/Article.js +5 -2
- package/lib/components/Page.js +2 -2
- package/lib/components/Section.js +6 -3
- package/lib/provider/CntrlSdkContext.js +2 -1
- package/lib/provider/CustomSectionRegistry.js +19 -0
- package/lib/provider/defaultContext.js +3 -1
- package/package.json +2 -2
- package/src/components/Article.tsx +14 -8
- package/src/components/Page.tsx +5 -2
- package/src/components/Section.tsx +7 -3
- package/src/provider/CntrlSdkContext.ts +2 -0
- package/src/provider/CustomSectionRegistry.ts +25 -0
- package/src/provider/defaultContext.ts +3 -1
- package/cntrl-site-sdk-nextjs-0.10.0.tgz +0 -0
- package/cntrl-site-sdk-nextjs-0.10.1.tgz +0 -0
- package/cntrl-site-sdk-nextjs-0.9.0.tgz +0 -0
|
Binary file
|
|
@@ -11,11 +11,14 @@ const Section_1 = require("./Section");
|
|
|
11
11
|
const Item_1 = require("./Item");
|
|
12
12
|
const useArticleRectObserver_1 = require("../utils/ArticleRectManager/useArticleRectObserver");
|
|
13
13
|
const ArticleRectContext_1 = require("../provider/ArticleRectContext");
|
|
14
|
-
const Article = ({ article }) => {
|
|
14
|
+
const Article = ({ article, sectionData }) => {
|
|
15
15
|
const articleRef = (0, react_1.useRef)(null);
|
|
16
16
|
const articleRectObserver = (0, useArticleRectObserver_1.useArticleRectObserver)(articleRef.current);
|
|
17
17
|
const id = (0, react_1.useId)();
|
|
18
|
-
return ((0, jsx_runtime_1.jsxs)(ArticleRectContext_1.ArticleRectContext.Provider, { value: articleRectObserver, children: [(0, jsx_runtime_1.jsx)("div", { className: "article", ref: articleRef, children: article.sections.map((section, i) =>
|
|
18
|
+
return ((0, jsx_runtime_1.jsxs)(ArticleRectContext_1.ArticleRectContext.Provider, { value: articleRectObserver, children: [(0, jsx_runtime_1.jsx)("div", { className: "article", ref: articleRef, children: article.sections.map((section, i) => {
|
|
19
|
+
const data = section.name ? sectionData[section.name] : {};
|
|
20
|
+
return ((0, jsx_runtime_1.jsx)(Section_1.Section, { section: section, data: data, children: article.sections[i].items.map(item => ((0, jsx_runtime_1.jsx)(Item_1.Item, { item: item }, item.id))) }, section.id));
|
|
21
|
+
}) }), (0, jsx_runtime_1.jsx)(style_1.default, { id: id, children: `
|
|
19
22
|
.article {
|
|
20
23
|
position: relative;
|
|
21
24
|
overflow: clip;
|
package/lib/components/Page.js
CHANGED
|
@@ -13,11 +13,11 @@ const Head_1 = require("./Head");
|
|
|
13
13
|
const useCntrlContext_1 = require("../provider/useCntrlContext");
|
|
14
14
|
const generateTypePresetStyles_1 = require("../utils/generateTypePresetStyles/generateTypePresetStyles");
|
|
15
15
|
const Keyframes_1 = require("../provider/Keyframes");
|
|
16
|
-
const Page = ({ article, project, meta, keyframes }) => {
|
|
16
|
+
const Page = ({ article, project, meta, keyframes, sectionData }) => {
|
|
17
17
|
const afterBodyOpen = (0, html_react_parser_1.default)(project.html.afterBodyOpen);
|
|
18
18
|
const beforeBodyClose = (0, html_react_parser_1.default)(project.html.beforeBodyClose);
|
|
19
19
|
const keyframesRepo = (0, react_1.useMemo)(() => new Keyframes_1.Keyframes(keyframes), [keyframes]);
|
|
20
20
|
const { typePresets, layouts } = (0, useCntrlContext_1.useCntrlContext)();
|
|
21
|
-
return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)(Head_1.CNTRLHead, { project: project, meta: meta }), afterBodyOpen, (0, jsx_runtime_1.jsx)(KeyframesContext_1.KeyframesContext.Provider, { value: keyframesRepo, children: (0, jsx_runtime_1.jsx)(Article_1.Article, { article: article }) }), beforeBodyClose, typePresets && typePresets.presets.length > 0 && ((0, jsx_runtime_1.jsx)("style", { children: (0, generateTypePresetStyles_1.generateTypePresetStyles)(typePresets, layouts) }))] }));
|
|
21
|
+
return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)(Head_1.CNTRLHead, { project: project, meta: meta }), afterBodyOpen, (0, jsx_runtime_1.jsx)(KeyframesContext_1.KeyframesContext.Provider, { value: keyframesRepo, children: (0, jsx_runtime_1.jsx)(Article_1.Article, { article: article, sectionData: sectionData }) }), beforeBodyClose, typePresets && typePresets.presets.length > 0 && ((0, jsx_runtime_1.jsx)("style", { children: (0, generateTypePresetStyles_1.generateTypePresetStyles)(typePresets, layouts) }))] }));
|
|
22
22
|
};
|
|
23
23
|
exports.Page = Page;
|
|
@@ -10,10 +10,11 @@ const style_1 = __importDefault(require("styled-jsx/style"));
|
|
|
10
10
|
const sdk_1 = require("@cntrl-site/sdk");
|
|
11
11
|
const useCntrlContext_1 = require("../provider/useCntrlContext");
|
|
12
12
|
const useSectionColor_1 = require("./useSectionColor");
|
|
13
|
-
const Section = ({ section, children }) => {
|
|
13
|
+
const Section = ({ section, data, children }) => {
|
|
14
14
|
const id = (0, react_1.useId)();
|
|
15
|
-
const { layouts } = (0, useCntrlContext_1.useCntrlContext)();
|
|
15
|
+
const { layouts, customSections } = (0, useCntrlContext_1.useCntrlContext)();
|
|
16
16
|
const backgroundColor = (0, useSectionColor_1.useSectionColor)(section.color);
|
|
17
|
+
const SectionComponent = section.name ? customSections.getComponent(section.name) : undefined;
|
|
17
18
|
const getSectionVisibilityStyles = () => {
|
|
18
19
|
return layouts
|
|
19
20
|
.sort((a, b) => a.startsWith - b.startsWith)
|
|
@@ -28,6 +29,8 @@ const Section = ({ section, children }) => {
|
|
|
28
29
|
}`;
|
|
29
30
|
}, '');
|
|
30
31
|
};
|
|
32
|
+
if (SectionComponent)
|
|
33
|
+
return (0, jsx_runtime_1.jsx)(SectionComponent, { data: data, children: children });
|
|
31
34
|
return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)("div", { className: `section-${section.id}`, id: section.name, style: {
|
|
32
35
|
backgroundColor: backgroundColor
|
|
33
36
|
}, children: children }), (0, jsx_runtime_1.jsx)(style_1.default, { id: id, children: `
|
|
@@ -35,7 +38,7 @@ const Section = ({ section, children }) => {
|
|
|
35
38
|
.section-${section.id} {
|
|
36
39
|
height: ${getSectionHeight(height)};
|
|
37
40
|
position: relative;
|
|
38
|
-
|
|
41
|
+
}`))}
|
|
39
42
|
${getSectionVisibilityStyles()}
|
|
40
43
|
` })] }));
|
|
41
44
|
};
|
|
@@ -2,8 +2,9 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.CntrlSdkContext = void 0;
|
|
4
4
|
class CntrlSdkContext {
|
|
5
|
-
constructor(customItems) {
|
|
5
|
+
constructor(customItems, customSections) {
|
|
6
6
|
this.customItems = customItems;
|
|
7
|
+
this.customSections = customSections;
|
|
7
8
|
this._layouts = [];
|
|
8
9
|
}
|
|
9
10
|
setTypePresets(typePresets) {
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CustomSectionRegistry = void 0;
|
|
4
|
+
class CustomSectionRegistry {
|
|
5
|
+
constructor() {
|
|
6
|
+
this.definitions = new Map();
|
|
7
|
+
}
|
|
8
|
+
define(name, section) {
|
|
9
|
+
this.definitions.set(name, section);
|
|
10
|
+
return this;
|
|
11
|
+
}
|
|
12
|
+
getComponent(name) {
|
|
13
|
+
return this.definitions.get(name)?.component;
|
|
14
|
+
}
|
|
15
|
+
getResolver(name) {
|
|
16
|
+
return this.definitions.get(name)?.dataResolver;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
exports.CustomSectionRegistry = CustomSectionRegistry;
|
|
@@ -3,5 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.cntrlSdkContext = void 0;
|
|
4
4
|
const CntrlSdkContext_1 = require("./CntrlSdkContext");
|
|
5
5
|
const CustomItemRegistry_1 = require("./CustomItemRegistry");
|
|
6
|
+
const CustomSectionRegistry_1 = require("./CustomSectionRegistry");
|
|
6
7
|
const customItems = new CustomItemRegistry_1.CustomItemRegistry();
|
|
7
|
-
|
|
8
|
+
const customSections = new CustomSectionRegistry_1.CustomSectionRegistry();
|
|
9
|
+
exports.cntrlSdkContext = new CntrlSdkContext_1.CntrlSdkContext(customItems, customSections);
|
package/package.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cntrl-site/sdk-nextjs",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.11.0",
|
|
4
4
|
"description": "SDK for Next.js",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
|
-
"types": "src/index.
|
|
6
|
+
"types": "src/index.ts",
|
|
7
7
|
"scripts": {
|
|
8
8
|
"test": "jest",
|
|
9
9
|
"prebuild": "rm -rf ./lib",
|
|
@@ -8,9 +8,10 @@ import { ArticleRectContext } from '../provider/ArticleRectContext';
|
|
|
8
8
|
|
|
9
9
|
interface Props {
|
|
10
10
|
article: TArticle;
|
|
11
|
+
sectionData: Record<SectionName, any>;
|
|
11
12
|
}
|
|
12
13
|
|
|
13
|
-
export const Article: FC<Props> = ({ article }) => {
|
|
14
|
+
export const Article: FC<Props> = ({ article, sectionData }) => {
|
|
14
15
|
const articleRef = useRef<HTMLDivElement | null>(null);
|
|
15
16
|
const articleRectObserver = useArticleRectObserver(articleRef.current);
|
|
16
17
|
const id = useId();
|
|
@@ -18,13 +19,16 @@ export const Article: FC<Props> = ({ article }) => {
|
|
|
18
19
|
return (
|
|
19
20
|
<ArticleRectContext.Provider value={articleRectObserver}>
|
|
20
21
|
<div className="article" ref={articleRef}>
|
|
21
|
-
{article.sections.map((section, i) =>
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
22
|
+
{article.sections.map((section, i) => {
|
|
23
|
+
const data = section.name ? sectionData[section.name] : {};
|
|
24
|
+
return (
|
|
25
|
+
<Section section={section} key={section.id} data={data}>
|
|
26
|
+
{article.sections[i].items.map(item => (
|
|
27
|
+
<Item item={item} key={item.id} />
|
|
28
|
+
))}
|
|
29
|
+
</Section>
|
|
30
|
+
);
|
|
31
|
+
})}
|
|
28
32
|
</div>
|
|
29
33
|
<JSXStyle id={id}>{`
|
|
30
34
|
.article {
|
|
@@ -35,3 +39,5 @@ export const Article: FC<Props> = ({ article }) => {
|
|
|
35
39
|
</ArticleRectContext.Provider>
|
|
36
40
|
);
|
|
37
41
|
};
|
|
42
|
+
|
|
43
|
+
type SectionName = string;
|
package/src/components/Page.tsx
CHANGED
|
@@ -13,9 +13,10 @@ export interface PageProps {
|
|
|
13
13
|
project: TProject;
|
|
14
14
|
meta: TMeta;
|
|
15
15
|
keyframes: TKeyframeAny[];
|
|
16
|
+
sectionData: Record<SectionName, any>;
|
|
16
17
|
}
|
|
17
18
|
|
|
18
|
-
export const Page: FC<PageProps> = ({ article, project, meta, keyframes }) => {
|
|
19
|
+
export const Page: FC<PageProps> = ({ article, project, meta, keyframes, sectionData }) => {
|
|
19
20
|
const afterBodyOpen = HTMLReactParser(project.html.afterBodyOpen);
|
|
20
21
|
const beforeBodyClose = HTMLReactParser(project.html.beforeBodyClose);
|
|
21
22
|
const keyframesRepo = useMemo(() => new Keyframes(keyframes), [keyframes]);
|
|
@@ -25,7 +26,7 @@ export const Page: FC<PageProps> = ({ article, project, meta, keyframes }) => {
|
|
|
25
26
|
<CNTRLHead project={project} meta={meta} />
|
|
26
27
|
{afterBodyOpen}
|
|
27
28
|
<KeyframesContext.Provider value={keyframesRepo}>
|
|
28
|
-
<Article article={article} />
|
|
29
|
+
<Article article={article} sectionData={sectionData} />
|
|
29
30
|
</KeyframesContext.Provider>
|
|
30
31
|
{beforeBodyClose}
|
|
31
32
|
{typePresets && typePresets.presets.length > 0 && (
|
|
@@ -34,3 +35,5 @@ export const Page: FC<PageProps> = ({ article, project, meta, keyframes }) => {
|
|
|
34
35
|
</>
|
|
35
36
|
);
|
|
36
37
|
};
|
|
38
|
+
|
|
39
|
+
type SectionName = string;
|
|
@@ -15,12 +15,14 @@ type SectionChild = ReactElement<any, any>;
|
|
|
15
15
|
interface Props {
|
|
16
16
|
section: TArticleSection;
|
|
17
17
|
children: SectionChild[];
|
|
18
|
+
data?: any;
|
|
18
19
|
}
|
|
19
20
|
|
|
20
|
-
export const Section: FC<Props> = ({ section, children }) => {
|
|
21
|
+
export const Section: FC<Props> = ({ section, data, children }) => {
|
|
21
22
|
const id = useId();
|
|
22
|
-
const { layouts } = useCntrlContext();
|
|
23
|
+
const { layouts, customSections } = useCntrlContext();
|
|
23
24
|
const backgroundColor = useSectionColor(section.color);
|
|
25
|
+
const SectionComponent = section.name ? customSections.getComponent(section.name) : undefined;
|
|
24
26
|
const getSectionVisibilityStyles = () => {
|
|
25
27
|
return layouts
|
|
26
28
|
.sort((a, b) => a.startsWith - b.startsWith)
|
|
@@ -36,6 +38,8 @@ export const Section: FC<Props> = ({ section, children }) => {
|
|
|
36
38
|
}, '');
|
|
37
39
|
};
|
|
38
40
|
|
|
41
|
+
if (SectionComponent) return <SectionComponent data={data}>{children}</SectionComponent>;
|
|
42
|
+
|
|
39
43
|
return (
|
|
40
44
|
<>
|
|
41
45
|
<div
|
|
@@ -53,7 +57,7 @@ export const Section: FC<Props> = ({ section, children }) => {
|
|
|
53
57
|
.section-${section.id} {
|
|
54
58
|
height: ${getSectionHeight(height)};
|
|
55
59
|
position: relative;
|
|
56
|
-
|
|
60
|
+
}`
|
|
57
61
|
))
|
|
58
62
|
}
|
|
59
63
|
${getSectionVisibilityStyles()}
|
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
import { CustomItemRegistry } from './CustomItemRegistry';
|
|
2
2
|
import { TLayout, TTypePresets, } from '@cntrl-site/sdk';
|
|
3
|
+
import { CustomSectionRegistry } from './CustomSectionRegistry';
|
|
3
4
|
|
|
4
5
|
export class CntrlSdkContext {
|
|
5
6
|
private _typePresets?: TTypePresets;
|
|
6
7
|
private _layouts: TLayout[] = [];
|
|
7
8
|
constructor(
|
|
8
9
|
public readonly customItems: CustomItemRegistry,
|
|
10
|
+
public readonly customSections: CustomSectionRegistry
|
|
9
11
|
) {}
|
|
10
12
|
|
|
11
13
|
setTypePresets(typePresets: TTypePresets) {
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import React, { PropsWithChildren } from 'react';
|
|
2
|
+
|
|
3
|
+
export class CustomSectionRegistry {
|
|
4
|
+
private definitions: Map<string, CustomSection<any>> = new Map();
|
|
5
|
+
|
|
6
|
+
define<TData>(name: string, section: CustomSection<TData>): this {
|
|
7
|
+
this.definitions.set(name, section);
|
|
8
|
+
return this;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
getComponent(name: string): CustomSectionComponent<any> | undefined {
|
|
12
|
+
return this.definitions.get(name)?.component;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
getResolver(name: string): (() => Promise<any>) | undefined {
|
|
16
|
+
return this.definitions.get(name)?.dataResolver;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
type CustomSection<TData = {}> = {
|
|
21
|
+
component: CustomSectionComponent<TData>,
|
|
22
|
+
dataResolver?: () => Promise<TData>
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
type CustomSectionComponent<TData> = React.FC<PropsWithChildren<{ data: TData }>>;
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { CntrlSdkContext } from './CntrlSdkContext';
|
|
2
2
|
import { CustomItemRegistry } from './CustomItemRegistry';
|
|
3
|
+
import { CustomSectionRegistry } from './CustomSectionRegistry';
|
|
3
4
|
|
|
4
5
|
const customItems = new CustomItemRegistry();
|
|
6
|
+
const customSections = new CustomSectionRegistry();
|
|
5
7
|
|
|
6
|
-
export const cntrlSdkContext = new CntrlSdkContext(customItems);
|
|
8
|
+
export const cntrlSdkContext = new CntrlSdkContext(customItems, customSections);
|
|
Binary file
|
|
Binary file
|
|
Binary file
|