@cntrl-site/sdk-nextjs 0.0.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.
@@ -0,0 +1,15 @@
1
+ <component name="InspectionProjectProfileManager">
2
+ <profile version="1.0">
3
+ <option name="myName" value="Project Default" />
4
+ <inspection_tool class="HtmlUnknownAttribute" enabled="true" level="WARNING" enabled_by_default="true">
5
+ <option name="myValues">
6
+ <value>
7
+ <list size="1">
8
+ <item index="0" class="java.lang.String" itemvalue="jsx" />
9
+ </list>
10
+ </value>
11
+ </option>
12
+ <option name="myCustomValuesEnabled" value="true" />
13
+ </inspection_tool>
14
+ </profile>
15
+ </component>
@@ -0,0 +1,8 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="ProjectModuleManager">
4
+ <modules>
5
+ <module fileurl="file://$PROJECT_DIR$/.idea/sdk-nextjs.iml" filepath="$PROJECT_DIR$/.idea/sdk-nextjs.iml" />
6
+ </modules>
7
+ </component>
8
+ </project>
@@ -0,0 +1,12 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <module type="WEB_MODULE" version="4">
3
+ <component name="NewModuleRootManager">
4
+ <content url="file://$MODULE_DIR$">
5
+ <excludeFolder url="file://$MODULE_DIR$/temp" />
6
+ <excludeFolder url="file://$MODULE_DIR$/.tmp" />
7
+ <excludeFolder url="file://$MODULE_DIR$/tmp" />
8
+ </content>
9
+ <orderEntry type="inheritedJdk" />
10
+ <orderEntry type="sourceFolder" forTests="false" />
11
+ </component>
12
+ </module>
package/.idea/vcs.xml ADDED
@@ -0,0 +1,6 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="VcsDirectoryMappings">
4
+ <mapping directory="$PROJECT_DIR$" vcs="Git" />
5
+ </component>
6
+ </project>
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 CNTRL
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1 @@
1
+ # sdk-nextjs
package/TODO.md ADDED
@@ -0,0 +1 @@
1
+ 1. Uninstall `@types/isomorphic-fetch` package and run `npm run build` - results in a TS compile error
Binary file
package/jest.config.js ADDED
@@ -0,0 +1,6 @@
1
+ /** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
2
+ module.exports = {
3
+ preset: 'ts-jest',
4
+ testEnvironment: 'node',
5
+ rootDir: './src'
6
+ };
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Article = void 0;
4
+ const jsx_runtime_1 = require("react/jsx-runtime");
5
+ const Section_1 = require("./Section");
6
+ const Item_1 = require("./Item");
7
+ const Article = ({ article, layouts }) => {
8
+ return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)("div", { className: "article", children: article.sections.map((section, i) => ((0, jsx_runtime_1.jsx)(Section_1.Section, { section: section, layouts: layouts, children: article.sections[i].items.map(item => ((0, jsx_runtime_1.jsx)(Item_1.Item, { layouts: layouts, item: item }, item.id))) }, section.id))) }), (0, jsx_runtime_1.jsx)("style", { jsx: true, children: `
9
+ .article {
10
+ position: relative;
11
+ overflow-x: hidden;
12
+ }
13
+ ` })] }));
14
+ };
15
+ exports.Article = Article;
@@ -0,0 +1,46 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Item = void 0;
4
+ const jsx_runtime_1 = require("react/jsx-runtime");
5
+ const core_1 = require("@cntrl-site/core");
6
+ const sdk_1 = require("@cntrl-site/sdk");
7
+ const RectangleItem_1 = require("./items/RectangleItem");
8
+ const ImageItem_1 = require("./items/ImageItem");
9
+ const VideoItem_1 = require("./items/VideoItem");
10
+ const RichTextItem_1 = require("./items/RichTextItem");
11
+ const itemsMap = {
12
+ [core_1.ArticleItemType.Rectangle]: RectangleItem_1.RectangleItem,
13
+ [core_1.ArticleItemType.Image]: ImageItem_1.ImageItem,
14
+ [core_1.ArticleItemType.Video]: VideoItem_1.VideoItem,
15
+ [core_1.ArticleItemType.RichText]: RichTextItem_1.RichTextItem
16
+ };
17
+ const noop = () => null;
18
+ const Item = ({ item, layouts }) => {
19
+ const layoutValues = [item.area];
20
+ if (item.layoutParams) {
21
+ layoutValues.push(item.layoutParams);
22
+ }
23
+ const sizingAxis = parseSizing(item.commonParams.sizing);
24
+ const ItemComponent = itemsMap[item.type] || noop;
25
+ return ((0, jsx_runtime_1.jsxs)("div", { className: `item-${item.id}`, children: [(0, jsx_runtime_1.jsx)(ItemComponent, { item: item, layouts: layouts }), (0, jsx_runtime_1.jsx)("style", { jsx: true, children: `
26
+ ${(0, sdk_1.getLayoutStyles)(layouts, layoutValues, ([area, layoutParams]) => (`
27
+ .item-${item.id} {
28
+ position: absolute;
29
+ top: ${area.top * 100}vw;
30
+ left: ${layoutParams?.fullwidth ? 0 : area.left * 100}vw;
31
+ width: ${layoutParams?.fullwidth ? '100vw' : sizingAxis.x === core_1.ArticleItemSizingType.Manual ? `${area.width * 100}vw` : 'auto'};
32
+ height: ${sizingAxis.y === core_1.ArticleItemSizingType.Manual ? `${area.height * 100}vw` : 'auto'};
33
+ z-index: ${area.zIndex};
34
+ transform: rotate(${area.angle}deg);
35
+ }
36
+ `))}
37
+ ` })] }));
38
+ };
39
+ exports.Item = Item;
40
+ const parseSizing = (sizing) => {
41
+ const axisSizing = sizing.split(' ');
42
+ return {
43
+ y: axisSizing[0],
44
+ x: axisSizing[1] ? axisSizing[1] : axisSizing[0]
45
+ };
46
+ };
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.LinkWrapper = void 0;
4
+ const jsx_runtime_1 = require("react/jsx-runtime");
5
+ const LinkWrapper = ({ url, children }) => (url ? (0, jsx_runtime_1.jsx)("a", { href: url, target: "_blank", rel: "noreferrer", children: children }) : (0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, { children: children }));
6
+ exports.LinkWrapper = LinkWrapper;
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Section = void 0;
4
+ const jsx_runtime_1 = require("react/jsx-runtime");
5
+ const sdk_1 = require("@cntrl-site/sdk");
6
+ const Section = ({ section, layouts, children }) => ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)("div", { className: `section-${section.id}`, children: children }), (0, jsx_runtime_1.jsx)("style", { jsx: true, children: `
7
+ ${(0, sdk_1.getLayoutStyles)(layouts, [section.height], ([height]) => (`
8
+ .section-${section.id} {
9
+ height: ${height * 100}vw;
10
+ }`))}
11
+ ` })] }));
12
+ exports.Section = Section;
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ImageItem = void 0;
4
+ const jsx_runtime_1 = require("react/jsx-runtime");
5
+ const sdk_1 = require("@cntrl-site/sdk");
6
+ const LinkWrapper_1 = require("../LinkWrapper");
7
+ const ImageItem = ({ item, layouts }) => ((0, jsx_runtime_1.jsx)(LinkWrapper_1.LinkWrapper, { url: item.link?.url, children: (0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)("div", { className: `image-wrapper-${item.id}`, children: (0, jsx_runtime_1.jsx)("img", { className: "image", src: item.commonParams.url }) }), (0, jsx_runtime_1.jsx)("style", { jsx: true, children: `
8
+ ${(0, sdk_1.getLayoutStyles)(layouts, [item.layoutParams], ([{ strokeColor, radius, strokeWidth, opacity }]) => (`
9
+ .image-wrapper-${item.id} {
10
+ position: absolute;
11
+ width: 100%;
12
+ height: 100%;
13
+ border-style: solid;
14
+ box-sizing: border-box;
15
+ border-color: ${strokeColor};
16
+ border-radius: ${radius * 100}vw;
17
+ opacity: ${opacity};
18
+ border-width: ${strokeWidth * 100}vw;
19
+ }`))}
20
+ .image {
21
+ width: 100%;
22
+ height: 100%;
23
+ opacity: 1;
24
+ object-fit: cover;
25
+ pointer-events: none;
26
+ }
27
+ ` })] }) }));
28
+ exports.ImageItem = ImageItem;
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RectangleItem = void 0;
4
+ const jsx_runtime_1 = require("react/jsx-runtime");
5
+ const LinkWrapper_1 = require("../LinkWrapper");
6
+ const sdk_1 = require("@cntrl-site/sdk");
7
+ const RectangleItem = ({ item, layouts }) => ((0, jsx_runtime_1.jsx)(LinkWrapper_1.LinkWrapper, { url: item.link?.url, children: (0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)("div", { className: `rectangle-${item.id}` }), (0, jsx_runtime_1.jsx)("style", { jsx: true, children: `
8
+ ${(0, sdk_1.getLayoutStyles)(layouts, [item.layoutParams], ([{ strokeColor, fillColor, radius, strokeWidth }]) => (`
9
+ .rectangle-${item.id} {
10
+ position: absolute;
11
+ width: 100%;
12
+ height: 100%;
13
+ border-style: solid;
14
+ box-sizing: border-box;
15
+ border-color: ${strokeColor};
16
+ background-color: ${fillColor};
17
+ border-radius: ${radius * 100}vw;
18
+ border-width: ${strokeWidth * 100}vw;
19
+ }`))}
20
+ ` })] }) }));
21
+ exports.RectangleItem = RectangleItem;
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.RichTextItem = void 0;
7
+ const jsx_runtime_1 = require("react/jsx-runtime");
8
+ //@ts-ignore
9
+ const style_1 = __importDefault(require("styled-jsx/style"));
10
+ const RichTextConverter_1 = require("../../utils/RichTextConverter");
11
+ const richTextConv = new RichTextConverter_1.RichTextConverter();
12
+ const RichTextItem = ({ item, layouts }) => {
13
+ const [content, styles] = richTextConv.toHtml(item, layouts);
14
+ return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)("div", { className: "rich-text", children: content }), (0, jsx_runtime_1.jsx)(style_1.default, { id: item.id, children: styles })] }));
15
+ };
16
+ exports.RichTextItem = RichTextItem;
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.VideoItem = void 0;
4
+ const jsx_runtime_1 = require("react/jsx-runtime");
5
+ const LinkWrapper_1 = require("../LinkWrapper");
6
+ const sdk_1 = require("@cntrl-site/sdk");
7
+ const VideoItem = ({ item, layouts }) => ((0, jsx_runtime_1.jsx)(LinkWrapper_1.LinkWrapper, { url: item.link?.url, children: (0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)("div", { className: `video-wrapper-${item.id}`, children: (0, jsx_runtime_1.jsx)("video", { autoPlay: true, muted: true, loop: true, playsInline: true, className: "video", children: (0, jsx_runtime_1.jsx)("source", { src: item.commonParams.url }) }) }), (0, jsx_runtime_1.jsx)("style", { jsx: true, children: `
8
+ ${(0, sdk_1.getLayoutStyles)(layouts, [item.layoutParams], ([{ strokeColor, radius, strokeWidth, opacity }]) => (`
9
+ .video-wrapper-${item.id} {
10
+ position: absolute;
11
+ width: 100%;
12
+ height: 100%;
13
+ border-style: solid;
14
+ box-sizing: border-box;
15
+ border-color: ${strokeColor};
16
+ border-radius: ${radius * 100}vw;
17
+ border-width: ${strokeWidth * 100}vw;
18
+ opacity: ${opacity};
19
+ }`))}
20
+ .video {
21
+ width: 100%;
22
+ height: 100%;
23
+ opacity: 1;
24
+ object-fit: cover;
25
+ pointer-events: none;
26
+ }
27
+ ` })] }) }));
28
+ exports.VideoItem = VideoItem;
package/lib/index.js ADDED
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.VideoItem = exports.RichTextItem = exports.RectangleItem = exports.ImageItem = exports.Item = exports.Section = exports.Article = exports.RichTextConverter = void 0;
4
+ var RichTextConverter_1 = require("./utils/RichTextConverter");
5
+ Object.defineProperty(exports, "RichTextConverter", { enumerable: true, get: function () { return RichTextConverter_1.RichTextConverter; } });
6
+ var Article_1 = require("./components/Article");
7
+ Object.defineProperty(exports, "Article", { enumerable: true, get: function () { return Article_1.Article; } });
8
+ var Section_1 = require("./components/Section");
9
+ Object.defineProperty(exports, "Section", { enumerable: true, get: function () { return Section_1.Section; } });
10
+ var Item_1 = require("./components/Item");
11
+ Object.defineProperty(exports, "Item", { enumerable: true, get: function () { return Item_1.Item; } });
12
+ var ImageItem_1 = require("./components/items/ImageItem");
13
+ Object.defineProperty(exports, "ImageItem", { enumerable: true, get: function () { return ImageItem_1.ImageItem; } });
14
+ var RectangleItem_1 = require("./components/items/RectangleItem");
15
+ Object.defineProperty(exports, "RectangleItem", { enumerable: true, get: function () { return RectangleItem_1.RectangleItem; } });
16
+ var RichTextItem_1 = require("./components/items/RichTextItem");
17
+ Object.defineProperty(exports, "RichTextItem", { enumerable: true, get: function () { return RichTextItem_1.RichTextItem; } });
18
+ var VideoItem_1 = require("./components/items/VideoItem");
19
+ Object.defineProperty(exports, "VideoItem", { enumerable: true, get: function () { return VideoItem_1.VideoItem; } });
@@ -0,0 +1,196 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RichTextConverter = exports.FontStyles = void 0;
4
+ const jsx_runtime_1 = require("react/jsx-runtime");
5
+ const core_1 = require("@cntrl-site/core");
6
+ exports.FontStyles = {
7
+ 'normal': {},
8
+ 'bold': { 'font-weight': 'bold' },
9
+ 'italic': { 'font-style': 'italic' }
10
+ };
11
+ class RichTextConverter {
12
+ toHtml(richText, layouts) {
13
+ const { text, blocks = [] } = richText.commonParams;
14
+ const root = [];
15
+ const styleRules = layouts.reduce((rec, layout) => {
16
+ rec[layout.id] = [];
17
+ return rec;
18
+ }, {});
19
+ for (let blockIndex = 0; blockIndex < blocks.length; blockIndex++) {
20
+ const block = blocks[blockIndex];
21
+ const content = text.slice(block.start, block.end);
22
+ const entities = block.entities.sort((a, b) => a.start - b.start) ?? [];
23
+ if (content.length === 0) {
24
+ root.push((0, jsx_runtime_1.jsx)("div", { style: { lineHeight: 0 }, children: (0, jsx_runtime_1.jsx)("br", {}) }));
25
+ continue;
26
+ }
27
+ const newStylesGroup = layouts.map(({ id: layoutId }) => {
28
+ const params = (0, core_1.getClosestLayoutValue)(richText.layoutParams, layouts, layoutId);
29
+ const styles = params.styles
30
+ .filter(s => s.start >= block.start && s.end <= block.end)
31
+ .map(s => ({ ...s, start: s.start - block.start, end: s.end - block.start }));
32
+ return ({
33
+ layout: layoutId,
34
+ styles: this.normalizeStyles(styles, entities)
35
+ });
36
+ });
37
+ const sameLayouts = groupBy(newStylesGroup, (item) => RichTextConverter.serializeRanges(item.styles ?? []));
38
+ for (const group of Object.values(sameLayouts)) {
39
+ const blockClass = `rt_${richText.id}-b${blockIndex}_${layouts.map(l => group.some(g => g.layout === l.id) ? '1' : '0').join('')}`;
40
+ const kids = [];
41
+ layouts.forEach(l => {
42
+ styleRules[l.id].push(`
43
+ .${blockClass} {
44
+ display: ${group.some(g => g.layout === l.id) ? 'block' : 'none'};
45
+ text-align: ${(0, core_1.getClosestLayoutValue)(richText.layoutParams, layouts, l.id).textAlign};
46
+ line-height: 0;
47
+ }
48
+ `);
49
+ });
50
+ const item = group[0];
51
+ const entitiesGroups = this.groupEntities(entities, item.styles) ?? [];
52
+ let offset = 0;
53
+ for (const entity of entitiesGroups) {
54
+ const entityKids = [];
55
+ if (offset < entity.start) {
56
+ kids.push(content.slice(offset, entity.start));
57
+ offset = entity.start;
58
+ }
59
+ for (const style of entity.stylesGroup) {
60
+ if (offset < style.start) {
61
+ entityKids.push(content.slice(offset, style.start));
62
+ }
63
+ entityKids.push((0, jsx_runtime_1.jsx)("span", { className: `s-${style.start}-${style.end}`, children: content.slice(style.start, style.end) }, style.start));
64
+ offset = style.end;
65
+ }
66
+ if (offset < entity.end) {
67
+ entityKids.push(content.slice(offset, entity.end));
68
+ offset = entity.end;
69
+ }
70
+ if (entity.link) {
71
+ kids.push((0, jsx_runtime_1.jsx)("a", { target: "_blank", href: entity.link, rel: "noreferrer", children: entityKids }, entity.start));
72
+ continue;
73
+ }
74
+ kids.push(...entityKids);
75
+ }
76
+ if (offset < content.length) {
77
+ kids.push(content.slice(offset));
78
+ }
79
+ for (const item of group) {
80
+ const entitiesGroups = this.groupEntities(entities, item.styles) ?? [];
81
+ for (const entitiesGroup of entitiesGroups) {
82
+ if (!entitiesGroup.stylesGroup)
83
+ continue;
84
+ for (const styleGroup of entitiesGroup.stylesGroup) {
85
+ styleRules[item.layout].push(`
86
+ .${blockClass} .s-${styleGroup.start}-${styleGroup.end} {
87
+ ${styleGroup.styles.map(s => RichTextConverter.fromDraftToInline(s)).join('\n')}
88
+ }
89
+ `);
90
+ }
91
+ }
92
+ }
93
+ root.push((0, jsx_runtime_1.jsx)("div", { className: blockClass, children: kids }, blockClass));
94
+ }
95
+ }
96
+ const styles = layouts.map(l => `
97
+ ${(0, core_1.getLayoutMediaQuery)(l.id, layouts)} {
98
+ ${styleRules[l.id].join('\n')}
99
+ }
100
+ `).join('\n');
101
+ return [
102
+ root,
103
+ styles
104
+ ];
105
+ }
106
+ static serializeRanges(ranges) {
107
+ return ranges.map(r => `${r.start},${r.end}`).join(' ');
108
+ }
109
+ normalizeStyles(styles, entities) {
110
+ const styleGroups = [];
111
+ const dividers = [...styles, ...entities].reduce((ds, s) => {
112
+ ds.add(s.start);
113
+ ds.add(s.end);
114
+ return ds;
115
+ }, new Set());
116
+ if (dividers.size === 0)
117
+ return;
118
+ const edges = Array.from(dividers).sort((a, b) => a - b);
119
+ for (let i = 0; i < edges.length - 1; i += 1) {
120
+ const start = edges[i];
121
+ const end = edges[i + 1];
122
+ const applied = styles.filter(s => Math.max(s.start, start) < Math.min(s.end, end));
123
+ if (applied.length === 0)
124
+ continue;
125
+ styleGroups.push({
126
+ start,
127
+ end,
128
+ styles: applied.map(s => ({ name: s.style, value: s.value }))
129
+ });
130
+ }
131
+ return styleGroups;
132
+ }
133
+ groupEntities(entities, styleGroups) {
134
+ const entitiesGroups = [];
135
+ if (!styleGroups)
136
+ return;
137
+ if (entities.length === 0) {
138
+ entitiesGroups.push({
139
+ stylesGroup: styleGroups,
140
+ start: styleGroups[0].start,
141
+ end: styleGroups[styleGroups.length - 1].end
142
+ });
143
+ return entitiesGroups;
144
+ }
145
+ const start = entities[0].start < styleGroups[0].start ? entities[0].start : styleGroups[0].start;
146
+ const end = entities[entities.length - 1].end > styleGroups[styleGroups.length - 1].end ? entities[entities.length - 1].end : styleGroups[styleGroups.length - 1].end;
147
+ const entitiesDividers = entities.reduce((ds, s) => {
148
+ ds.add(s.start);
149
+ ds.add(s.end);
150
+ return ds;
151
+ }, new Set([start, end]));
152
+ const entityDividers = Array.from(entitiesDividers).sort((a, b) => a - b);
153
+ for (let i = 0; i < entityDividers.length - 1; i += 1) {
154
+ const start = entityDividers[i];
155
+ const end = entityDividers[i + 1];
156
+ const entity = entities.find(e => e.start === start);
157
+ entitiesGroups.push({
158
+ stylesGroup: styleGroups.filter(s => s.start >= start && s.end <= end),
159
+ start,
160
+ end,
161
+ ...(entity && { link: entity.data.url })
162
+ });
163
+ }
164
+ return entitiesGroups;
165
+ }
166
+ static fromDraftToInline(draftStyle) {
167
+ const { value, name } = draftStyle;
168
+ const map = {
169
+ 'COLOR': { 'color': value },
170
+ 'TYPEFACE': { 'font-family': `"${value}"` },
171
+ 'FONTSTYLE': value ? { ...exports.FontStyles[value] } : {},
172
+ 'FONTWEIGHT': { 'font-weight': value },
173
+ 'FONTSIZE': { 'font-size': `${parseFloat(value) * 100}vw` },
174
+ 'LINEHEIGHT': { 'line-height': `${parseFloat(value) * 100}vw` },
175
+ 'LETTERSPACING': { 'letter-spacing': `${parseFloat(value) * 100}vw` },
176
+ 'WORDSPACING': { 'word-spacing': `${parseFloat(value) * 100}vw` },
177
+ 'TEXTTRANSFORM': value ? { 'text-transform': value } : { 'text-transform': core_1.TextTransform.None },
178
+ 'VERTICALALIGN': value ? { 'vertical-align': value } : { 'vertical-align': core_1.VerticalAlign.Unset },
179
+ 'TEXTDECORATION': { 'text-decoration': value }
180
+ };
181
+ const css = map[name];
182
+ return Object.entries(css).filter(([, value]) => !!value).map(([prop, value]) => `${prop}: ${value};`).join('\n');
183
+ }
184
+ }
185
+ exports.RichTextConverter = RichTextConverter;
186
+ function groupBy(items, getKey) {
187
+ const groups = {};
188
+ for (const item of items) {
189
+ const key = getKey(item);
190
+ if (!groups[key]) {
191
+ groups[key] = [];
192
+ }
193
+ groups[key].push(item);
194
+ }
195
+ return groups;
196
+ }
package/package.json ADDED
@@ -0,0 +1,42 @@
1
+ {
2
+ "name": "@cntrl-site/sdk-nextjs",
3
+ "version": "0.0.1",
4
+ "description": "SDK for Next.js",
5
+ "main": "lib/index.js",
6
+ "types": "src/index.js",
7
+ "scripts": {
8
+ "test": "jest",
9
+ "prebuild": "rm -rf ./lib",
10
+ "build": "tsc --project tsconfig.build.json",
11
+ "prepublishOnly": "NODE_ENV=production npm run build"
12
+ },
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "git+https://github.com/cntrl-site/sdk-nextjs.git"
16
+ },
17
+ "author": "arsen@momdesign.nyc",
18
+ "license": "MIT",
19
+ "bugs": {
20
+ "url": "https://github.com/cntrl-site/sdk-nextjs/issues"
21
+ },
22
+ "homepage": "https://github.com/cntrl-site/sdk-nextjs#readme",
23
+ "dependencies": {
24
+ "@cntrl-site/core": "^1.0.6",
25
+ "@cntrl-site/sdk": "^0.0.1",
26
+ "styled-jsx": "^5.0.2"
27
+ },
28
+ "peerDependencies": {
29
+ "react": "^18.1.0",
30
+ "react-dom": "^18.1.0"
31
+ },
32
+ "devDependencies": {
33
+ "@tsconfig/node16": "^1.0.3",
34
+ "@tsconfig/recommended": "^1.0.1",
35
+ "@types/isomorphic-fetch": "^0.0.36",
36
+ "@types/react": "^18.0.15",
37
+ "typescript": "^4.7.4"
38
+ },
39
+ "directories": {
40
+ "lib": "lib"
41
+ }
42
+ }
@@ -0,0 +1,31 @@
1
+ import { FC } from 'react';
2
+ import { TArticle, TLayout } from '@cntrl-site/core';
3
+ import { Section } from './Section';
4
+ import { Item } from './Item';
5
+
6
+ interface Props {
7
+ article: TArticle;
8
+ layouts: TLayout[];
9
+ }
10
+
11
+ export const Article: FC<Props> = ({ article, layouts }) => {
12
+ return (
13
+ <>
14
+ <div className="article">
15
+ {article.sections.map((section, i) => (
16
+ <Section section={section} key={section.id} layouts={layouts}>
17
+ {article.sections[i].items.map(item => (
18
+ <Item layouts={layouts} item={item} key={item.id} />
19
+ ))}
20
+ </Section>
21
+ ))}
22
+ </div>
23
+ <style jsx>{`
24
+ .article {
25
+ position: relative;
26
+ overflow-x: hidden;
27
+ }
28
+ `}</style>
29
+ </>
30
+ );
31
+ };
@@ -0,0 +1,68 @@
1
+ import { ComponentType, FC } from 'react';
2
+ import {
3
+ ArticleItemType,
4
+ ArticleItemSizingType as SizingType,
5
+ TArticleItemAny,
6
+ TLayout
7
+ } from '@cntrl-site/core';
8
+ import { getLayoutStyles } from '@cntrl-site/sdk';
9
+ import { RectangleItem } from './items/RectangleItem';
10
+ import { ImageItem } from './items/ImageItem';
11
+ import { VideoItem } from './items/VideoItem';
12
+ import { RichTextItem } from './items/RichTextItem';
13
+
14
+ export interface ItemProps<I extends TArticleItemAny> {
15
+ layouts: TLayout[];
16
+ item: I;
17
+ }
18
+
19
+ const itemsMap: Record<ArticleItemType, ComponentType<ItemProps<any>>> = {
20
+ [ArticleItemType.Rectangle]: RectangleItem,
21
+ [ArticleItemType.Image]: ImageItem,
22
+ [ArticleItemType.Video]: VideoItem,
23
+ [ArticleItemType.RichText]: RichTextItem
24
+ };
25
+
26
+ const noop = () => null;
27
+
28
+ export const Item: FC<ItemProps<TArticleItemAny>> = ({ item, layouts }) => {
29
+ const layoutValues: Record<string, any>[] = [item.area];
30
+ if (item.layoutParams) {
31
+ layoutValues.push(item.layoutParams);
32
+ }
33
+
34
+ const sizingAxis = parseSizing(item.commonParams.sizing);
35
+ const ItemComponent = itemsMap[item.type] || noop;
36
+
37
+ return (
38
+ <div className={`item-${item.id}`}>
39
+ <ItemComponent item={item} layouts={layouts} />
40
+ <style jsx>{`
41
+ ${getLayoutStyles(layouts, layoutValues, ([area, layoutParams]) => (`
42
+ .item-${item.id} {
43
+ position: absolute;
44
+ top: ${area.top * 100}vw;
45
+ left: ${layoutParams?.fullwidth ? 0 : area.left * 100}vw;
46
+ width: ${layoutParams?.fullwidth ? '100vw' : sizingAxis.x === SizingType.Manual ? `${area.width * 100}vw` : 'auto'};
47
+ height: ${sizingAxis.y === SizingType.Manual ? `${area.height * 100}vw` : 'auto'};
48
+ z-index: ${area.zIndex};
49
+ transform: rotate(${area.angle}deg);
50
+ }
51
+ `))}
52
+ `}</style>
53
+ </div>
54
+ );
55
+ };
56
+
57
+ const parseSizing = (sizing: string): Axis => {
58
+ const axisSizing = sizing.split(' ');
59
+ return {
60
+ y: axisSizing[0],
61
+ x: axisSizing[1] ? axisSizing[1] : axisSizing[0]
62
+ } as Axis;
63
+ };
64
+
65
+ interface Axis {
66
+ x: SizingType;
67
+ y: SizingType;
68
+ }
@@ -0,0 +1,9 @@
1
+ import React, { ReactElement } from 'react';
2
+
3
+ interface Props {
4
+ url?: string;
5
+ children: ReactElement;
6
+ }
7
+
8
+ export const LinkWrapper: React.FC<Props> = ({ url, children }) => (
9
+ url ? <a href={url} target="_blank" rel="noreferrer">{children}</a> : <>{children}</>);
@@ -0,0 +1,28 @@
1
+ import { FC, ReactElement } from 'react';
2
+ import { TLayout, TArticleSection } from '@cntrl-site/core';
3
+ import { getLayoutStyles } from '@cntrl-site/sdk';
4
+
5
+ type SectionChild = ReactElement<any, any>;
6
+
7
+ interface Props {
8
+ section: TArticleSection;
9
+ layouts: TLayout[];
10
+ children: SectionChild[];
11
+ }
12
+
13
+ export const Section: FC<Props> = ({ section, layouts, children }) => (
14
+ <>
15
+ <div className={`section-${section.id}`}>
16
+ {children}
17
+ </div>
18
+ <style jsx>{`
19
+ ${
20
+ getLayoutStyles(layouts, [section.height], ([height]) => (`
21
+ .section-${section.id} {
22
+ height: ${height * 100}vw;
23
+ }`
24
+ ))
25
+ }
26
+ `}</style>
27
+ </>
28
+ );
@@ -0,0 +1,38 @@
1
+ import { FC } from 'react';
2
+ import { TImageItem } from '@cntrl-site/core';
3
+ import { getLayoutStyles } from '@cntrl-site/sdk';
4
+ import { ItemProps } from '../Item';
5
+ import { LinkWrapper } from '../LinkWrapper';
6
+
7
+ export const ImageItem: FC<ItemProps<TImageItem>> = ({ item, layouts }) => (
8
+ <LinkWrapper url={item.link?.url}>
9
+ <>
10
+ <div className={`image-wrapper-${item.id}`}>
11
+ <img className="image" src={item.commonParams.url} />
12
+ </div>
13
+ <style jsx>{`
14
+ ${getLayoutStyles(layouts, [item.layoutParams], ([{ strokeColor, radius, strokeWidth, opacity }]) => (`
15
+ .image-wrapper-${item.id} {
16
+ position: absolute;
17
+ width: 100%;
18
+ height: 100%;
19
+ border-style: solid;
20
+ box-sizing: border-box;
21
+ border-color: ${strokeColor};
22
+ border-radius: ${radius * 100}vw;
23
+ opacity: ${opacity};
24
+ border-width: ${strokeWidth * 100}vw;
25
+ }`
26
+ ))
27
+ }
28
+ .image {
29
+ width: 100%;
30
+ height: 100%;
31
+ opacity: 1;
32
+ object-fit: cover;
33
+ pointer-events: none;
34
+ }
35
+ `}</style>
36
+ </>
37
+ </LinkWrapper>
38
+ );
@@ -0,0 +1,28 @@
1
+ import { FC } from 'react';
2
+ import { TRectangleItem } from '@cntrl-site/core';
3
+ import { ItemProps } from '../Item';
4
+ import { LinkWrapper } from '../LinkWrapper';
5
+ import { getLayoutStyles } from '@cntrl-site/sdk';
6
+
7
+ export const RectangleItem: FC<ItemProps<TRectangleItem>> = ({ item, layouts }) => (
8
+ <LinkWrapper url={item.link?.url}>
9
+ <>
10
+ <div className={`rectangle-${item.id}`} />
11
+ <style jsx>{`
12
+ ${getLayoutStyles(layouts, [item.layoutParams], ([{ strokeColor, fillColor, radius, strokeWidth }]) => (`
13
+ .rectangle-${item.id} {
14
+ position: absolute;
15
+ width: 100%;
16
+ height: 100%;
17
+ border-style: solid;
18
+ box-sizing: border-box;
19
+ border-color: ${strokeColor};
20
+ background-color: ${fillColor};
21
+ border-radius: ${radius * 100}vw;
22
+ border-width: ${strokeWidth * 100}vw;
23
+ }`
24
+ ))}
25
+ `}</style>
26
+ </>
27
+ </LinkWrapper>
28
+ );
@@ -0,0 +1,20 @@
1
+ import { FC } from 'react';
2
+ import { TRichTextItem } from '@cntrl-site/core';
3
+ //@ts-ignore
4
+ import JSXStyle from 'styled-jsx/style';
5
+ import { ItemProps } from '../Item';
6
+ import { RichTextConverter } from '../../utils/RichTextConverter';
7
+
8
+ const richTextConv = new RichTextConverter();
9
+
10
+ export const RichTextItem: FC<ItemProps<TRichTextItem>> = ({ item, layouts }) => {
11
+ const [content, styles] = richTextConv.toHtml(item, layouts);
12
+ return (
13
+ <>
14
+ <div className="rich-text">{content}</div>
15
+ <JSXStyle id={item.id}>
16
+ {styles}
17
+ </JSXStyle>
18
+ </>
19
+ );
20
+ };
@@ -0,0 +1,40 @@
1
+ import { FC } from 'react';
2
+ import { TVideoItem } from '@cntrl-site/core';
3
+ import { ItemProps } from '../Item';
4
+ import { LinkWrapper } from '../LinkWrapper';
5
+ import { getLayoutStyles } from '@cntrl-site/sdk';
6
+
7
+ export const VideoItem: FC<ItemProps<TVideoItem>> = ({ item, layouts }) => (
8
+ <LinkWrapper url={item.link?.url}>
9
+ <>
10
+ <div className={`video-wrapper-${item.id}`}>
11
+ <video autoPlay muted loop playsInline className="video">
12
+ <source src={item.commonParams.url} />
13
+ </video>
14
+ </div>
15
+ <style jsx>{`
16
+ ${getLayoutStyles(layouts, [item.layoutParams], ([{ strokeColor, radius, strokeWidth, opacity }]) => (`
17
+ .video-wrapper-${item.id} {
18
+ position: absolute;
19
+ width: 100%;
20
+ height: 100%;
21
+ border-style: solid;
22
+ box-sizing: border-box;
23
+ border-color: ${strokeColor};
24
+ border-radius: ${radius * 100}vw;
25
+ border-width: ${strokeWidth * 100}vw;
26
+ opacity: ${opacity};
27
+ }`
28
+ ))
29
+ }
30
+ .video {
31
+ width: 100%;
32
+ height: 100%;
33
+ opacity: 1;
34
+ object-fit: cover;
35
+ pointer-events: none;
36
+ }
37
+ `}</style>
38
+ </>
39
+ </LinkWrapper>
40
+ );
package/src/index.ts ADDED
@@ -0,0 +1,9 @@
1
+ export { RichTextConverter } from './utils/RichTextConverter';
2
+
3
+ export { Article } from './components/Article';
4
+ export { Section } from './components/Section';
5
+ export { Item } from './components/Item';
6
+ export { ImageItem } from './components/items/ImageItem';
7
+ export { RectangleItem } from './components/items/RectangleItem';
8
+ export { RichTextItem } from './components/items/RichTextItem';
9
+ export { VideoItem } from './components/items/VideoItem';
@@ -0,0 +1 @@
1
+ /// <reference types="styled-jsx" />
@@ -0,0 +1,228 @@
1
+ import { ReactElement, ReactNode } from 'react';
2
+ import {
3
+ TLayout,
4
+ RichText,
5
+ TRichTextItem,
6
+ TextTransform,
7
+ VerticalAlign,
8
+ getClosestLayoutValue,
9
+ getLayoutMediaQuery
10
+ } from '@cntrl-site/core';
11
+
12
+ interface StyleGroup {
13
+ start: number;
14
+ end: number;
15
+ styles: DraftStyle[];
16
+ }
17
+
18
+ interface EntitiesGroup {
19
+ link?: string;
20
+ stylesGroup: StyleGroup[];
21
+ start: number;
22
+ end: number;
23
+ }
24
+
25
+ interface DraftStyle {
26
+ name: string;
27
+ value?: string;
28
+ }
29
+
30
+ export const FontStyles: Record<string, Record<string, string>> = {
31
+ 'normal': {},
32
+ 'bold': { 'font-weight': 'bold' },
33
+ 'italic': { 'font-style': 'italic' }
34
+ };
35
+
36
+ export class RichTextConverter {
37
+ toHtml(
38
+ richText: TRichTextItem,
39
+ layouts: TLayout[]
40
+ ): [ReactNode[], string] {
41
+ const { text, blocks = [] } = richText.commonParams;
42
+ const root: ReactElement[] = [];
43
+ const styleRules = layouts.reduce<Record<string, string[]>>((rec, layout) => {
44
+ rec[layout.id] = [];
45
+ return rec;
46
+ }, {});
47
+
48
+ for (let blockIndex = 0; blockIndex < blocks.length; blockIndex++) {
49
+ const block = blocks[blockIndex];
50
+ const content = text.slice(block.start, block.end);
51
+ const entities = block.entities!.sort((a, b) => a.start - b.start) ?? [];
52
+ if (content.length === 0) {
53
+ root.push(<div style={{ lineHeight: 0 }}><br /></div>);
54
+ continue;
55
+ }
56
+ const newStylesGroup = layouts.map(({ id: layoutId }) => {
57
+ const params = getClosestLayoutValue(richText.layoutParams, layouts, layoutId);
58
+ const styles = params.styles!
59
+ .filter(s => s.start >= block.start && s.end <= block.end)
60
+ .map(s => ({ ...s, start: s.start - block.start, end: s.end - block.start }));
61
+ return ({
62
+ layout: layoutId,
63
+ styles: this.normalizeStyles(styles, entities)
64
+ });
65
+ });
66
+ const sameLayouts = groupBy(newStylesGroup, (item) => RichTextConverter.serializeRanges(item.styles ?? []));
67
+ for (const group of Object.values(sameLayouts)) {
68
+ const blockClass = `rt_${richText.id}-b${blockIndex}_${layouts.map(l => group.some(g => g.layout === l.id) ? '1' : '0').join('')}`;
69
+ const kids: ReactNode[] = [];
70
+ layouts.forEach(l => {
71
+ styleRules[l.id].push(`
72
+ .${blockClass} {
73
+ display: ${group.some(g => g.layout === l.id) ? 'block' : 'none'};
74
+ text-align: ${getClosestLayoutValue(richText.layoutParams, layouts, l.id).textAlign};
75
+ line-height: 0;
76
+ }
77
+ `);
78
+ });
79
+ const item = group[0];
80
+ const entitiesGroups = this.groupEntities(entities, item.styles) ?? [];
81
+ let offset = 0;
82
+ for (const entity of entitiesGroups) {
83
+ const entityKids: ReactNode[] = [];
84
+ if (offset < entity.start) {
85
+ kids.push(content.slice(offset, entity.start));
86
+ offset = entity.start;
87
+ }
88
+ for (const style of entity.stylesGroup) {
89
+ if (offset < style.start) {
90
+ entityKids.push(content.slice(offset, style.start));
91
+ }
92
+ entityKids.push(<span key={style.start} className={`s-${style.start}-${style.end}`}>{content.slice(style.start, style.end)}</span>);
93
+ offset = style.end;
94
+ }
95
+ if (offset < entity.end) {
96
+ entityKids.push(content.slice(offset, entity.end));
97
+ offset = entity.end;
98
+ }
99
+ if (entity.link) {
100
+ kids.push(<a key={entity.start} target="_blank" href={entity.link} rel="noreferrer">{entityKids}</a>);
101
+ continue;
102
+ }
103
+ kids.push(...entityKids);
104
+ }
105
+ if (offset < content.length) {
106
+ kids.push(content.slice(offset));
107
+ }
108
+ for (const item of group) {
109
+ const entitiesGroups = this.groupEntities(entities, item.styles) ?? [];
110
+ for (const entitiesGroup of entitiesGroups) {
111
+ if (!entitiesGroup.stylesGroup) continue;
112
+ for (const styleGroup of entitiesGroup.stylesGroup) {
113
+ styleRules[item.layout].push(`
114
+ .${blockClass} .s-${styleGroup.start}-${styleGroup.end} {
115
+ ${styleGroup.styles.map(s => RichTextConverter.fromDraftToInline(s)).join('\n')}
116
+ }
117
+ `);
118
+ }
119
+ }
120
+ }
121
+ root.push(<div key={blockClass} className={blockClass}>{kids}</div>);
122
+ }
123
+ }
124
+ const styles = layouts.map(l => `
125
+ ${getLayoutMediaQuery(l.id, layouts)} {
126
+ ${styleRules[l.id].join('\n')}
127
+ }
128
+ `).join('\n');
129
+ return [
130
+ root,
131
+ styles
132
+ ];
133
+ }
134
+
135
+ private static serializeRanges(ranges: { start: number; end: number; }[]): string {
136
+ return ranges.map(r => `${r.start},${r.end}`).join(' ');
137
+ }
138
+
139
+ private normalizeStyles(styles: RichText.Style[], entities: RichText.Entity[]): StyleGroup[] | undefined {
140
+ const styleGroups: StyleGroup[] = [];
141
+ const dividers = [...styles, ...entities].reduce((ds, s) => {
142
+ ds.add(s.start);
143
+ ds.add(s.end);
144
+ return ds;
145
+ }, new Set<number>());
146
+ if (dividers.size === 0) return;
147
+ const edges = Array.from(dividers).sort((a, b) => a - b);
148
+ for (let i = 0; i < edges.length - 1; i += 1) {
149
+ const start = edges[i];
150
+ const end = edges[i + 1];
151
+ const applied = styles.filter(s => Math.max(s.start, start) < Math.min(s.end, end));
152
+ if (applied.length === 0) continue;
153
+ styleGroups.push({
154
+ start,
155
+ end,
156
+ styles: applied.map(s => ({ name: s.style, value: s.value }))
157
+ })
158
+ }
159
+
160
+ return styleGroups;
161
+ }
162
+
163
+ private groupEntities(entities: RichText.Entity[], styleGroups?: StyleGroup[]): EntitiesGroup[] | undefined {
164
+ const entitiesGroups: EntitiesGroup[] = [];
165
+ if (!styleGroups) return;
166
+ if (entities.length === 0) {
167
+ entitiesGroups.push({
168
+ stylesGroup: styleGroups,
169
+ start: styleGroups[0].start,
170
+ end: styleGroups[styleGroups.length - 1].end
171
+ });
172
+ return entitiesGroups;
173
+ }
174
+ const start = entities[0].start < styleGroups[0].start ? entities[0].start : styleGroups[0].start;
175
+ const end = entities[entities.length - 1].end > styleGroups[styleGroups.length - 1].end ? entities[entities.length - 1].end : styleGroups[styleGroups.length - 1].end;
176
+ const entitiesDividers = entities.reduce((ds, s) => {
177
+ ds.add(s.start);
178
+ ds.add(s.end);
179
+ return ds;
180
+ }, new Set<number>([start, end]));
181
+ const entityDividers = Array.from(entitiesDividers).sort((a, b) => a - b);
182
+
183
+ for (let i = 0; i < entityDividers.length - 1; i += 1) {
184
+ const start = entityDividers[i];
185
+ const end = entityDividers[i + 1];
186
+ const entity = entities.find(e => e.start === start);
187
+ entitiesGroups.push({
188
+ stylesGroup: styleGroups.filter(s => s.start >= start && s.end <= end),
189
+ start,
190
+ end,
191
+ ...(entity && { link: entity.data.url })
192
+ });
193
+ }
194
+
195
+ return entitiesGroups;
196
+ }
197
+
198
+ private static fromDraftToInline(draftStyle: DraftStyle): string {
199
+ const { value, name } = draftStyle;
200
+ const map: Record<string, Record<string, string | undefined>> = {
201
+ 'COLOR': { 'color': value },
202
+ 'TYPEFACE': { 'font-family': `"${value}"` },
203
+ 'FONTSTYLE': value ? { ...FontStyles[value] } : {},
204
+ 'FONTWEIGHT': { 'font-weight': value },
205
+ 'FONTSIZE': { 'font-size': `${parseFloat(value!) * 100}vw` },
206
+ 'LINEHEIGHT': { 'line-height': `${parseFloat(value!) * 100}vw` },
207
+ 'LETTERSPACING': { 'letter-spacing': `${parseFloat(value!) * 100}vw` },
208
+ 'WORDSPACING': { 'word-spacing': `${parseFloat(value!) * 100}vw` },
209
+ 'TEXTTRANSFORM': value ? { 'text-transform': value as TextTransform } : { 'text-transform': TextTransform.None },
210
+ 'VERTICALALIGN': value ? { 'vertical-align': value as VerticalAlign } : { 'vertical-align': VerticalAlign.Unset },
211
+ 'TEXTDECORATION': { 'text-decoration': value }
212
+ };
213
+ const css = map[name];
214
+ return Object.entries(css).filter(([, value]) => !!value).map(([prop, value]) => `${prop}: ${value};`).join('\n');
215
+ }
216
+ }
217
+
218
+ function groupBy<I>(items: I[], getKey: (item: I) => PropertyKey): Record<PropertyKey, I[]> {
219
+ const groups: Record<PropertyKey, I[]> = {};
220
+ for (const item of items) {
221
+ const key = getKey(item);
222
+ if (!groups[key]) {
223
+ groups[key] = [];
224
+ }
225
+ groups[key]!.push(item);
226
+ }
227
+ return groups;
228
+ }