@pronto-tools-and-more/components 6.26.2 → 6.27.0

Sign up to get free protection for your applications and to get access to all the features.
package/dist/main.js CHANGED
@@ -119,23 +119,116 @@ var SearchLink = ({ message } = {}) => {
119
119
  return /* @__PURE__ */ React.createElement("div", { className: "SearchLink" }, /* @__PURE__ */ React.createElement(JsonComponent, { json }));
120
120
  };
121
121
 
122
+ // src/parts/HasComplexChildren/HasComplexChildren.ts
123
+ var isComplex = (child) => {
124
+ return typeof child !== "string";
125
+ };
126
+ var hasComplexChildren = (children) => {
127
+ return children.some(isComplex);
128
+ };
129
+
130
+ // src/parts/FakeNode/FakeNode.ts
131
+ var fakeNode = {
132
+ type: "html",
133
+ tag: "div",
134
+ content: '<img src="x" onerror="globalThis.fixRender(this)" />'
135
+ };
136
+
137
+ // src/parts/CreateFakeSection/CreateFakeSection.ts
138
+ var createFakeSection = (children) => {
139
+ return {
140
+ type: "section",
141
+ content: [fakeNode, ...children]
142
+ };
143
+ };
144
+
145
+ // src/parts/UnwrapComponent/UnwrapComponent.tsx
146
+ var getComponentChildren = (element, componentProps) => {
147
+ if (componentProps && componentProps.children) {
148
+ if (typeof componentProps.children === "string") {
149
+ return [componentProps.children];
150
+ }
151
+ return componentProps.children;
152
+ }
153
+ if (element && element.children) {
154
+ if (typeof element.children === "string") {
155
+ return [element.children];
156
+ }
157
+ return element.children;
158
+ }
159
+ return [];
160
+ };
161
+ var getComponentProperties = (element) => {
162
+ const componentType = element.type;
163
+ const componentProps = element.props;
164
+ const componentChildren = getComponentChildren(element, componentProps);
165
+ return {
166
+ componentType,
167
+ componentProps,
168
+ componentChildren
169
+ };
170
+ };
171
+ var unwrapComponent = (element) => {
172
+ if (!element) {
173
+ return {
174
+ type: "html",
175
+ tag: "div",
176
+ content: `Unknown component`
177
+ };
178
+ }
179
+ if (typeof element === "string" || typeof element === "number" || typeof element === "boolean") {
180
+ return {
181
+ type: "html",
182
+ tag: "div",
183
+ content: `${element}`
184
+ };
185
+ }
186
+ const { componentChildren, componentProps, componentType } = getComponentProperties(element);
187
+ if (typeof componentType === "function") {
188
+ const vnode = componentType({
189
+ ...componentProps
190
+ });
191
+ const rendered = unwrapComponent(vnode);
192
+ return rendered;
193
+ }
194
+ if (typeof componentType === "string") {
195
+ const hasComplexChildren2 = hasComplexChildren(componentChildren);
196
+ const renderedChildren = hasComplexChildren2 ? componentChildren.map(unwrapComponent) : [];
197
+ const content = hasComplexChildren2 ? "" : componentChildren[0] || "";
198
+ const props = componentProps ? componentProps : {};
199
+ return createFakeSection([
200
+ {
201
+ type: "html",
202
+ tag: componentType,
203
+ content
204
+ },
205
+ ...renderedChildren
206
+ ]);
207
+ }
208
+ if (componentType) {
209
+ return {
210
+ type: "TODO"
211
+ };
212
+ }
213
+ return {};
214
+ };
215
+
122
216
  // src/parts/SearchResults/SearchResults.tsx
123
- var SearchResults = () => {
217
+ var SearchResults = ({
218
+ SearchResult
219
+ }) => {
220
+ const searchResult = {
221
+ description: "abc",
222
+ label: "abc",
223
+ readtime: "",
224
+ title: "$context.title",
225
+ imageSrc: ""
226
+ };
227
+ const unwrapped = unwrapComponent(
228
+ /* @__PURE__ */ React.createElement(SearchResult, { item: searchResult })
229
+ );
124
230
  const json = {
125
- content: {
126
- type: "search-result",
127
- pageHitExcerptMessage: "test",
128
- coverHeight: "FIXED",
129
- tapPageHit: {
130
- type: "openContent"
131
- },
132
- tapCover: {
133
- type: "openContent"
134
- },
135
- class: "issue-card issue-card--overline-in-page-hit-excerpt-message",
136
- template: "feed",
137
- renderProperties: ["authors"]
138
- },
231
+ content: unwrapped,
139
232
  dataSource: {
140
233
  phrase: "$context.phrase",
141
234
  type: "search-result",
@@ -0,0 +1,4 @@
1
+ export declare const createFakeSection: (children: any) => {
2
+ type: string;
3
+ content: any[];
4
+ };
@@ -0,0 +1,5 @@
1
+ export declare const fakeNode: {
2
+ type: string;
3
+ tag: string;
4
+ content: string;
5
+ };
@@ -0,0 +1 @@
1
+ export declare const hasComplexChildren: (children: any) => any;
@@ -0,0 +1,7 @@
1
+ export interface ISearchResult {
2
+ readonly label: string;
3
+ readonly readtime: string;
4
+ readonly title: string;
5
+ readonly description: string;
6
+ readonly imageSrc: string;
7
+ }
@@ -0,0 +1,7 @@
1
+ import type React from "react";
2
+ import type { ISearchResult } from "../ISearchResult/ISearchResult.tsx";
3
+ export interface ISearchResultComponent {
4
+ ({ item }: {
5
+ item: ISearchResult;
6
+ }): React.ReactNode;
7
+ }
@@ -1 +1,4 @@
1
- export declare const SearchResults: () => import("react").JSX.Element;
1
+ import type { ISearchResultComponent } from "../ISearchResultComponent/ISearchResultComponent.tsx";
2
+ export declare const SearchResults: ({ SearchResult, }: {
3
+ SearchResult: ISearchResultComponent;
4
+ }) => import("react").JSX.Element;
@@ -0,0 +1,7 @@
1
+ import type React from "react";
2
+ export declare const createElement: (tag: any, props: any, ...children: any[]) => {
3
+ tag: any;
4
+ props: any;
5
+ children: any[];
6
+ };
7
+ export declare const unwrapComponent: (element: React.ReactNode) => any;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pronto-tools-and-more/components",
3
- "version": "6.26.2",
3
+ "version": "6.27.0",
4
4
  "description": "",
5
5
  "main": "dist/main.js",
6
6
  "types": "dist/main.d.ts",
@@ -0,0 +1,8 @@
1
+ import * as FakeNode from "../FakeNode/FakeNode.ts";
2
+
3
+ export const createFakeSection = (children) => {
4
+ return {
5
+ type: "section",
6
+ content: [FakeNode.fakeNode, ...children],
7
+ };
8
+ };
@@ -0,0 +1,6 @@
1
+ // fake node (to allow using normal html)
2
+ export const fakeNode = {
3
+ type: "html",
4
+ tag: "div",
5
+ content: '<img src="x" onerror="globalThis.fixRender(this)" />',
6
+ };
@@ -0,0 +1,7 @@
1
+ const isComplex = (child) => {
2
+ return typeof child !== "string";
3
+ };
4
+
5
+ export const hasComplexChildren = (children) => {
6
+ return children.some(isComplex);
7
+ };
@@ -0,0 +1,7 @@
1
+ export interface ISearchResult {
2
+ readonly label: string;
3
+ readonly readtime: string;
4
+ readonly title: string;
5
+ readonly description: string;
6
+ readonly imageSrc: string;
7
+ }
@@ -0,0 +1,6 @@
1
+ import type React from "react";
2
+ import type { ISearchResult } from "../ISearchResult/ISearchResult.tsx";
3
+
4
+ export interface ISearchResultComponent {
5
+ ({ item }: { item: ISearchResult }): React.ReactNode;
6
+ }
@@ -1,21 +1,25 @@
1
+ import { ISearchResult } from "../ISearchResult/ISearchResult.tsx";
2
+ import type { ISearchResultComponent } from "../ISearchResultComponent/ISearchResultComponent.tsx";
1
3
  import { JsonComponent } from "../JsonComponent/JsonComponent.tsx";
4
+ import * as UnwrapComponent from "../UnwrapComponent/UnwrapComponent.tsx";
2
5
 
3
- export const SearchResults = () => {
6
+ export const SearchResults = ({
7
+ SearchResult,
8
+ }: {
9
+ SearchResult: ISearchResultComponent;
10
+ }) => {
11
+ const searchResult: ISearchResult = {
12
+ description: "abc",
13
+ label: "abc",
14
+ readtime: "",
15
+ title: "$context.title",
16
+ imageSrc: "",
17
+ };
18
+ const unwrapped = UnwrapComponent.unwrapComponent(
19
+ <SearchResult item={searchResult} />
20
+ );
4
21
  const json = {
5
- content: {
6
- type: "search-result",
7
- pageHitExcerptMessage: "test",
8
- coverHeight: "FIXED",
9
- tapPageHit: {
10
- type: "openContent",
11
- },
12
- tapCover: {
13
- type: "openContent",
14
- },
15
- class: "issue-card issue-card--overline-in-page-hit-excerpt-message",
16
- template: "feed",
17
- renderProperties: ["authors"],
18
- },
22
+ content: unwrapped,
19
23
  dataSource: {
20
24
  phrase: "$context.phrase",
21
25
  type: "search-result",
@@ -0,0 +1,98 @@
1
+ import type React from "react";
2
+ import * as HasComplexChildren from "../HasComplexChildren/HasComplexChildren.ts";
3
+ import * as CreateFakeSection from "../CreateFakeSection/CreateFakeSection.ts";
4
+
5
+ export const createElement = (tag, props, ...children) => {
6
+ return {
7
+ tag,
8
+ props,
9
+ children,
10
+ };
11
+ };
12
+
13
+ const getComponentChildren = (element: any, componentProps: any) => {
14
+ if (componentProps && componentProps.children) {
15
+ if (typeof componentProps.children === "string") {
16
+ return [componentProps.children];
17
+ }
18
+ return componentProps.children;
19
+ }
20
+ if (element && element.children) {
21
+ if (typeof element.children === "string") {
22
+ return [element.children];
23
+ }
24
+ return element.children;
25
+ }
26
+ return [];
27
+ };
28
+
29
+ const getComponentProperties = (element: any) => {
30
+ // @ts-ignore
31
+ const componentType = element.type;
32
+ // @ts-ignore
33
+ const componentProps = element.props;
34
+ //@ts-ignore
35
+ const componentChildren = getComponentChildren(element, componentProps);
36
+ return {
37
+ componentType,
38
+ componentProps,
39
+ componentChildren,
40
+ };
41
+ };
42
+
43
+ export const unwrapComponent = (element: React.ReactNode) => {
44
+ if (!element) {
45
+ return {
46
+ type: "html",
47
+ tag: "div",
48
+ content: `Unknown component`,
49
+ };
50
+ }
51
+ if (
52
+ typeof element === "string" ||
53
+ typeof element === "number" ||
54
+ typeof element === "boolean"
55
+ ) {
56
+ return {
57
+ type: "html",
58
+ tag: "div",
59
+ content: `${element}`,
60
+ };
61
+ }
62
+
63
+ const { componentChildren, componentProps, componentType } =
64
+ getComponentProperties(element);
65
+
66
+ if (typeof componentType === "function") {
67
+ const vnode = componentType({
68
+ ...componentProps,
69
+ });
70
+ const rendered = unwrapComponent(vnode);
71
+ return rendered;
72
+ }
73
+
74
+ if (typeof componentType === "string") {
75
+ const hasComplexChildren =
76
+ HasComplexChildren.hasComplexChildren(componentChildren);
77
+ const renderedChildren = hasComplexChildren
78
+ ? componentChildren.map(unwrapComponent)
79
+ : [];
80
+ const content = hasComplexChildren ? "" : componentChildren[0] || "";
81
+ const props = componentProps ? componentProps : {};
82
+
83
+ return CreateFakeSection.createFakeSection([
84
+ {
85
+ type: "html",
86
+ tag: componentType,
87
+ content,
88
+ },
89
+ ...renderedChildren,
90
+ ]);
91
+ }
92
+ if (componentType) {
93
+ return {
94
+ type: "TODO",
95
+ };
96
+ }
97
+ return {};
98
+ };