@cntrl-site/sdk-nextjs 0.10.1 → 0.11.1

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.
Binary file
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) => ((0, jsx_runtime_1.jsx)(Section_1.Section, { section: section, children: article.sections[i].items.map(item => ((0, jsx_runtime_1.jsx)(Item_1.Item, { item: item }, item.id))) }, section.id))) }), (0, jsx_runtime_1.jsx)(style_1.default, { id: id, children: `
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;
@@ -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
  };
package/lib/index.js CHANGED
@@ -14,9 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.useCntrlContext = exports.CntrlProvider = exports.YoutubeEmbedItem = exports.VimeoEmbedItem = exports.LayoutStyle = exports.VideoItem = exports.RichTextItem = exports.RectangleItem = exports.ImageItem = exports.Item = exports.Section = exports.Article = exports.Head = exports.Page = exports.RichTextConverter = exports.cntrlSdkContext = void 0;
18
- var defaultContext_1 = require("./provider/defaultContext");
19
- Object.defineProperty(exports, "cntrlSdkContext", { enumerable: true, get: function () { return defaultContext_1.cntrlSdkContext; } });
17
+ exports.cntrlSdkContext = exports.customSections = exports.customItems = exports.useCntrlContext = exports.CntrlProvider = exports.YoutubeEmbedItem = exports.VimeoEmbedItem = exports.LayoutStyle = exports.VideoItem = exports.RichTextItem = exports.RectangleItem = exports.ImageItem = exports.Item = exports.Section = exports.Article = exports.Head = exports.Page = exports.RichTextConverter = void 0;
20
18
  __exportStar(require("@cntrl-site/sdk"), exports);
21
19
  var RichTextConverter_1 = require("./utils/RichTextConverter/RichTextConverter");
22
20
  Object.defineProperty(exports, "RichTextConverter", { enumerable: true, get: function () { return RichTextConverter_1.RichTextConverter; } });
@@ -44,8 +42,11 @@ var VimeoEmbed_1 = require("./components/items/VimeoEmbed");
44
42
  Object.defineProperty(exports, "VimeoEmbedItem", { enumerable: true, get: function () { return VimeoEmbed_1.VimeoEmbedItem; } });
45
43
  var YoutubeEmbed_1 = require("./components/items/YoutubeEmbed");
46
44
  Object.defineProperty(exports, "YoutubeEmbedItem", { enumerable: true, get: function () { return YoutubeEmbed_1.YoutubeEmbedItem; } });
47
- // custom items
45
+ const defaultContext_1 = require("./provider/defaultContext");
48
46
  var CntrlProvider_1 = require("./provider/CntrlProvider");
49
47
  Object.defineProperty(exports, "CntrlProvider", { enumerable: true, get: function () { return CntrlProvider_1.CntrlProvider; } });
50
48
  var useCntrlContext_1 = require("./provider/useCntrlContext");
51
49
  Object.defineProperty(exports, "useCntrlContext", { enumerable: true, get: function () { return useCntrlContext_1.useCntrlContext; } });
50
+ exports.customItems = defaultContext_1.cntrlSdkContext.customItems;
51
+ exports.customSections = defaultContext_1.cntrlSdkContext.customSections;
52
+ exports.cntrlSdkContext = defaultContext_1.cntrlSdkContext;
@@ -2,10 +2,23 @@
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
  }
10
+ async resolveSectionData(sections) {
11
+ const resolvers = sections.map(section => {
12
+ const resolver = section.name ? this.customSections.getResolver(section.name) : undefined;
13
+ if (!resolver)
14
+ return;
15
+ return {
16
+ name: section.name,
17
+ resolver
18
+ };
19
+ }).filter(isDefined);
20
+ return Object.fromEntries(await Promise.all(resolvers.map(async ({ name, resolver }) => [name, await resolver()])));
21
+ }
9
22
  setTypePresets(typePresets) {
10
23
  this._typePresets = typePresets;
11
24
  }
@@ -20,3 +33,6 @@ class CntrlSdkContext {
20
33
  }
21
34
  }
22
35
  exports.CntrlSdkContext = CntrlSdkContext;
36
+ function isDefined(value) {
37
+ return value !== undefined;
38
+ }
@@ -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
- exports.cntrlSdkContext = new CntrlSdkContext_1.CntrlSdkContext(customItems);
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.10.1",
3
+ "version": "0.11.1",
4
4
  "description": "SDK for Next.js",
5
5
  "main": "lib/index.js",
6
- "types": "src/index.js",
6
+ "types": "src/index.ts",
7
7
  "scripts": {
8
8
  "test": "jest",
9
9
  "prebuild": "rm -rf ./lib",
@@ -21,7 +21,7 @@
21
21
  },
22
22
  "homepage": "https://github.com/cntrl-site/sdk-nextjs#readme",
23
23
  "dependencies": {
24
- "@cntrl-site/sdk": "^1.1.0",
24
+ "@cntrl-site/sdk": "^1.1.1",
25
25
  "html-react-parser": "^3.0.1",
26
26
  "styled-jsx": "^5.0.2"
27
27
  },
@@ -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
- <Section section={section} key={section.id}>
23
- {article.sections[i].items.map(item => (
24
- <Item item={item} key={item.id} />
25
- ))}
26
- </Section>
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;
@@ -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()}
package/src/index.ts CHANGED
@@ -1,5 +1,3 @@
1
- export { cntrlSdkContext } from './provider/defaultContext';
2
-
3
1
  export * from '@cntrl-site/sdk';
4
2
 
5
3
  export { RichTextConverter } from './utils/RichTextConverter/RichTextConverter';
@@ -17,7 +15,10 @@ export { LayoutStyle } from './components/LayoutStyle';
17
15
  export { VimeoEmbedItem } from './components/items/VimeoEmbed';
18
16
  export { YoutubeEmbedItem } from './components/items/YoutubeEmbed';
19
17
 
20
- // custom items
18
+ import { cntrlSdkContext as sdk } from './provider/defaultContext';
21
19
  export { CntrlProvider } from './provider/CntrlProvider';
22
20
  export type { CustomItemComponent } from './provider/CustomItemTypes';
23
21
  export { useCntrlContext } from './provider/useCntrlContext';
22
+ export const customItems = sdk.customItems;
23
+ export const customSections = sdk.customSections;
24
+ export const cntrlSdkContext = sdk;
@@ -1,13 +1,29 @@
1
1
  import { CustomItemRegistry } from './CustomItemRegistry';
2
- import { TLayout, TTypePresets, } from '@cntrl-site/sdk';
2
+ import { TArticleSection, 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
 
13
+ async resolveSectionData(sections: TArticleSection[]): Promise<Record<string, any>> {
14
+ const resolvers = sections.map(section => {
15
+ const resolver = section.name ? this.customSections.getResolver(section.name) : undefined;
16
+ if (!resolver) return;
17
+ return {
18
+ name: section.name,
19
+ resolver
20
+ };
21
+ }).filter(isDefined);
22
+ return Object.fromEntries(
23
+ await Promise.all(resolvers.map(async ({ name, resolver }) => [name, await resolver()]))
24
+ );
25
+ }
26
+
11
27
  setTypePresets(typePresets: TTypePresets) {
12
28
  this._typePresets = typePresets;
13
29
  }
@@ -24,3 +40,7 @@ export class CntrlSdkContext {
24
40
  return this._typePresets;
25
41
  }
26
42
  }
43
+
44
+ function isDefined<T>(value: T): value is Exclude<T, undefined> {
45
+ return value !== undefined;
46
+ }
@@ -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