@cntrl-site/sdk-nextjs 1.11.2 → 1.11.4-alpha.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.
Files changed (44) hide show
  1. package/lib/components/Article.js +4 -1
  2. package/lib/components/Section/Section.js +5 -3
  3. package/lib/components/StructuredBlockItem/BlockCaption/BlockCaption.js +19 -8
  4. package/lib/components/StructuredBlockItem/StructuredRichText/StructuredRichText.js +11 -18
  5. package/lib/components/StructuredBlockItem/StructuredTable/StructuredTable.js +10 -8
  6. package/lib/components/StructuredBlockItem/textStylesRule.js +24 -6
  7. package/lib/components/StructuredBlockItem/vw.js +8 -0
  8. package/lib/components/items/CodeEmbedItem/CodeEmbedItem.js +2 -2
  9. package/lib/components/items/ComponentItem/ComponentItem.js +3 -3
  10. package/lib/components/items/CompoundItem/CompoundItem.js +2 -2
  11. package/lib/components/items/EmbedVideoItem/VimeoEmbed.js +4 -4
  12. package/lib/components/items/EmbedVideoItem/YoutubeEmbed.js +5 -5
  13. package/lib/components/items/FileItem/ImageItem.js +2 -2
  14. package/lib/components/items/FileItem/VideoItem.js +3 -3
  15. package/lib/components/items/GroupItem/GroupItem.js +2 -2
  16. package/lib/components/items/LinkWrapper.js +33 -1
  17. package/lib/components/items/RectangleItem/RectangleItem.js +3 -3
  18. package/lib/utils/RichTextConverter/RichTextConverter.js +19 -9
  19. package/package.json +3 -3
  20. package/src/components/Article.test.tsx +52 -0
  21. package/src/components/Article.tsx +3 -0
  22. package/src/components/Section/Section.tsx +8 -4
  23. package/src/components/StructuredBlockItem/BlockCaption/BlockCaption.tsx +18 -8
  24. package/src/components/StructuredBlockItem/StructuredRichText/StructuredRichText.tsx +11 -14
  25. package/src/components/StructuredBlockItem/StructuredTable/StructuredTable.tsx +18 -14
  26. package/src/components/StructuredBlockItem/textStylesRule.ts +24 -6
  27. package/src/components/StructuredBlockItem/vw.ts +4 -0
  28. package/src/components/StructuredContent/StructuredContent.test.tsx +30 -3
  29. package/src/components/items/CodeEmbedItem/CodeEmbedItem.tsx +1 -1
  30. package/src/components/items/ComponentItem/ComponentItem.tsx +1 -0
  31. package/src/components/items/CompoundItem/CompoundItem.tsx +1 -1
  32. package/src/components/items/EmbedVideoItem/VimeoEmbed.tsx +1 -1
  33. package/src/components/items/EmbedVideoItem/YoutubeEmbed.tsx +1 -1
  34. package/src/components/items/FileItem/ImageItem.tsx +1 -1
  35. package/src/components/items/FileItem/VideoItem.tsx +1 -1
  36. package/src/components/items/GroupItem/GroupItem.tsx +1 -1
  37. package/src/components/items/LinkWrapper.tsx +43 -2
  38. package/src/components/items/RectangleItem/RectangleItem.tsx +1 -1
  39. package/src/utils/RichTextConverter/RichTextConverter.test.tsx +17 -0
  40. package/src/utils/RichTextConverter/RichTextConverter.tsx +23 -8
  41. package/.idea/codeStyles/codeStyleConfig.xml +0 -5
  42. package/.idea/modules.xml +0 -8
  43. package/.idea/sdk-nextjs.iml +0 -12
  44. package/.idea/vcs.xml +0 -6
@@ -1,15 +1,28 @@
1
- import React, { CSSProperties, ReactElement, ReactNode } from 'react';
1
+ import React, { CSSProperties, MouseEvent, ReactElement, ReactNode } from 'react';
2
2
 
3
3
  interface Props {
4
4
  url?: string;
5
5
  children: ReactElement | ReactNode[];
6
6
  target?: string;
7
+ download?: string;
7
8
  style?: CSSProperties;
8
9
  }
9
10
 
10
- export const LinkWrapper: React.FC<Props> = ({ url, children, target, style }) => {
11
+ export const LinkWrapper: React.FC<Props> = ({ url, children, target, download, style }) => {
11
12
  const validUrl = url && buildValidUrl(url);
12
13
  const targetParams = target === '_blank' ? { target, rel: 'noreferrer' } : {};
14
+ if (url && validUrl && download !== undefined) {
15
+ return (
16
+ <a
17
+ href={validUrl}
18
+ style={style}
19
+ download={download}
20
+ onClick={(event) => downloadFile(event, validUrl, download)}
21
+ >
22
+ {children}
23
+ </a>
24
+ );
25
+ }
13
26
  return url ? (
14
27
  <a
15
28
  href={validUrl}
@@ -23,11 +36,39 @@ export const LinkWrapper: React.FC<Props> = ({ url, children, target, style }) =
23
36
  );
24
37
  };
25
38
 
39
+ function downloadFile(event: MouseEvent<HTMLAnchorElement>, url: string, name: string) {
40
+ if (event.metaKey || event.ctrlKey || event.shiftKey || event.altKey || event.button !== 0) return;
41
+ event.preventDefault();
42
+ fetch(url)
43
+ .then((response) => {
44
+ if (!response.ok) throw new Error(`Could not download ${url}: ${response.status}`);
45
+ return response.blob();
46
+ })
47
+ .then((blob) => {
48
+ const objectUrl = URL.createObjectURL(blob);
49
+ const link = document.createElement('a');
50
+ link.href = objectUrl;
51
+ link.download = name || fileNameOf(url);
52
+ document.body.appendChild(link);
53
+ link.click();
54
+ link.remove();
55
+ window.setTimeout(() => URL.revokeObjectURL(objectUrl), 0);
56
+ })
57
+ .catch(() => {
58
+ window.location.assign(url);
59
+ });
60
+ }
61
+
62
+ function fileNameOf(url: string): string {
63
+ return decodeURIComponent(url.split('?')[0].split('/').pop() ?? '');
64
+ }
65
+
26
66
  function buildValidUrl(url: string): string {
27
67
  const prefixes = [
28
68
  'http://',
29
69
  'https://',
30
70
  '/',
71
+ './',
31
72
  'mailto:',
32
73
  'tel:',
33
74
  'file:',
@@ -53,7 +53,7 @@ export const RectangleItem: FC<ItemProps<TRectangleItem>> = ({ item, sectionId,
53
53
  : 'transparent';
54
54
 
55
55
  return (
56
- <LinkWrapper url={item.link?.url} target={item.link?.target}>
56
+ <LinkWrapper url={item.link?.url} target={item.link?.target} download={item.link?.download}>
57
57
  <>
58
58
  <div
59
59
  className={`rectangle-${item.id}`}
@@ -80,6 +80,23 @@ describe('RichTextConverter', () => {
80
80
  expect(container.textContent).toBe('a');
81
81
  });
82
82
 
83
+ it('draws each paragraph in the element asked for, without its margins, a span as a block; a div unless asked', () => {
84
+ const source = text([{ text: 'Read on' }, { text: '' }]);
85
+ const [paragraphs, paragraphStyles] = converter.toHtml(source, layouts, { paragraphTag: 'p' });
86
+ const { container } = render(<>{paragraphs}</>);
87
+ expect([...container.children].map(child => child.tagName)).toEqual(['P', 'P']);
88
+ expect(paragraphStyles).toMatch(/\.rt_text-b0_1 \{ margin: 0; \}/);
89
+ expect(paragraphStyles).toMatch(/\.rt_text_br_1 \{ display: block; margin: 0; \}/);
90
+
91
+ const [lines, lineStyles] = converter.toHtml(source, layouts, { paragraphTag: 'span' });
92
+ expect([...render(<>{lines}</>).container.children].map(child => child.tagName)).toEqual(['SPAN', 'SPAN']);
93
+ expect(lineStyles).toMatch(/\.rt_text_br_1 \{ display: block; margin: 0; \}/);
94
+
95
+ const [divs, divStyles] = converter.toHtml(source, layouts);
96
+ expect([...render(<>{divs}</>).container.children].map(child => child.tagName)).toEqual(['DIV', 'DIV']);
97
+ expect(divStyles).not.toContain('margin: 0');
98
+ });
99
+
83
100
  it('leaves text without lists as it was', () => {
84
101
  const [content, styles] = converter.toHtml(text([{ text: 'Plain' }]), layouts);
85
102
  const { container } = render(<>{content}</>);
@@ -33,6 +33,12 @@ interface ToHtmlOptions {
33
33
  * scale would leave at the unscaled size, takes viewport units throughout.
34
34
  */
35
35
  isScaledToViewport?: boolean;
36
+ /**
37
+ * The element each paragraph is drawn in: a div unless said otherwise; a p for text read as
38
+ * paragraphs, or, inside a heading, which takes no paragraphs, a span drawn as a block. A p or a
39
+ * span keeps none of its own margins, so the text looks the same whichever it is drawn in.
40
+ */
41
+ paragraphTag?: 'div' | 'p' | 'span';
36
42
  }
37
43
 
38
44
  interface StyleGroup {
@@ -44,6 +50,7 @@ interface StyleGroup {
44
50
  interface EntitiesGroup {
45
51
  link?: string;
46
52
  target?: string;
53
+ download?: string;
47
54
  stylesGroup: StyleGroup[];
48
55
  start: number;
49
56
  end: number;
@@ -105,11 +112,13 @@ export class RichTextConverter {
105
112
  toHtml(
106
113
  richText: RichTextSource,
107
114
  layouts: Layout[],
108
- { isScaledToViewport = true }: ToHtmlOptions = {}
115
+ { isScaledToViewport = true, paragraphTag: Paragraph = 'div' }: ToHtmlOptions = {}
109
116
  ): [ReactNode[], string, string | undefined] {
110
117
  const { text, blocks = [] } = richText.commonParams;
111
118
  const hasLists = blocks.some(block => block.type in LIST_ITEM_CLASSES);
112
119
  const root: ReactElement[] = [];
120
+ // what a div is of itself, a paragraph drawn in another element is told: a block, without margins
121
+ const paragraphRules: string[] = [];
113
122
  const styleRules = layouts.reduce<Record<string, string[]>>((rec, layout) => {
114
123
  rec[layout.id] = [];
115
124
  return rec;
@@ -128,7 +137,8 @@ export class RichTextConverter {
128
137
  // an empty line keeps its height, as in the editor; a text never typed into comes without even its line break
129
138
  if (content === '' || content === '\n') {
130
139
  const id = `rt_${richText.id}_br_${blockIndex}`;
131
- root.push(<div key={id} className={`${id}${typeClass}`}><br /></div>);
140
+ root.push(<Paragraph key={id} className={`${id}${typeClass}`}><br /></Paragraph>);
141
+ if (Paragraph !== 'div') paragraphRules.push(`.${id} { display: block; margin: 0; }`);
132
142
  layouts.forEach(l => {
133
143
  const lhForLayout = currentLineHeight[l.id];
134
144
  if (lhForLayout === undefined) return;
@@ -203,7 +213,7 @@ export class RichTextConverter {
203
213
  offset = entity.end;
204
214
  }
205
215
  if (entity.link) {
206
- kids.push(<LinkWrapper key={entity.start} url={entity.link} target={entity.target}>{entityKids}</LinkWrapper>);
216
+ kids.push(<LinkWrapper key={entity.start} url={entity.link} target={entity.target} download={entity.download}>{entityKids}</LinkWrapper>);
207
217
  continue;
208
218
  }
209
219
  kids.push(...entityKids);
@@ -253,14 +263,19 @@ export class RichTextConverter {
253
263
  }
254
264
  }
255
265
  }
256
- root.push(<div key={blockClass} className={`${blockClass}${typeClass}`}>{kids}</div>);
266
+ root.push(<Paragraph key={blockClass} className={`${blockClass}${typeClass}`}>{kids}</Paragraph>);
267
+ // each layout shows or hides the paragraph itself
268
+ if (Paragraph !== 'div') paragraphRules.push(`.${blockClass} { margin: 0; }`);
257
269
  }
258
270
  }
259
- const styles = layouts.map(l => `
271
+ const styles = [
272
+ ...paragraphRules,
273
+ ...layouts.map(l => `
260
274
  ${getLayoutMediaQuery(l.id, layouts)} {
261
275
  ${styleRules[l.id].join('\n')}
262
276
  }
263
- `).join('\n');
277
+ `)
278
+ ].join('\n');
264
279
  return [
265
280
  root,
266
281
  styles,
@@ -317,7 +332,7 @@ export class RichTextConverter {
317
332
  stylesGroup: [],
318
333
  start,
319
334
  end,
320
- ...(entity && { link: entity.data?.url ?? '', target: entity.data?.target ?? '_self' })
335
+ ...(entity && { link: entity.data?.url ?? '', target: entity.data?.target ?? '_self', download: entity.data?.download })
321
336
  });
322
337
  }
323
338
  return entitiesGroups;
@@ -348,7 +363,7 @@ export class RichTextConverter {
348
363
  stylesGroup: styleGroups.filter(s => s.start >= start && s.end <= end),
349
364
  start,
350
365
  end,
351
- ...(entity && { link: entity.data?.url ?? '', target: entity.data?.target ?? '_self' })
366
+ ...(entity && { link: entity.data?.url ?? '', target: entity.data?.target ?? '_self', download: entity.data?.download })
352
367
  });
353
368
  }
354
369
 
@@ -1,5 +0,0 @@
1
- <component name="ProjectCodeStyleConfiguration">
2
- <state>
3
- <option name="PREFERRED_PROJECT_CODE_STYLE" value="Default" />
4
- </state>
5
- </component>
package/.idea/modules.xml DELETED
@@ -1,8 +0,0 @@
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>
@@ -1,12 +0,0 @@
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 DELETED
@@ -1,6 +0,0 @@
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>