@raketa-builder/support 0.1.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/CHANGELOG.md ADDED
@@ -0,0 +1,7 @@
1
+ # @raketa-builder/image-input
2
+
3
+ ## 0.1.0
4
+
5
+ ### Minor Changes
6
+
7
+ - First release of @raketa-builder/builder & @raketa-builder/image-input
package/package.json ADDED
@@ -0,0 +1,30 @@
1
+ {
2
+ "name": "@raketa-builder/support",
3
+ "version": "0.1.0",
4
+ "main": "./dist/index.js",
5
+ "types": "./dist/index.d.ts",
6
+ "license": "MIT",
7
+ "scripts": {
8
+ "lint": "TIMING=1 eslint \"**/*.js\"",
9
+ "build": "tsup src/index.js --format cjs --loader \".js=jsx\"",
10
+ "dev": "tsup src/index.js --format cjs --loader \".js=jsx\" --watch"
11
+ },
12
+ "peerDependencies": {
13
+ "react": "18.2.0",
14
+ "react-dom": "18.2.0"
15
+ },
16
+ "dependencies": {
17
+ "markdown-it": "13.0.1",
18
+ "markdown-it-attrs": "4.1.4"
19
+ },
20
+ "devDependencies": {
21
+ "@types/react": "17.0.37",
22
+ "@types/react-dom": "17.0.11",
23
+ "eslint": "7.32.0",
24
+ "eslint-config-custom": "*",
25
+ "react": "18.2.0",
26
+ "tsconfig": "*",
27
+ "tsup": "6.5.0",
28
+ "typescript": "4.5.2"
29
+ }
30
+ }
@@ -0,0 +1,15 @@
1
+ import React from "react";
2
+
3
+ const Container = ({ settings, className, children }) => {
4
+ const { containerID, ...restSettings } = settings || {};
5
+
6
+ const cssClasses = [...Object.values(restSettings), className].join(" ");
7
+
8
+ return (
9
+ <div id={containerID} className={cssClasses}>
10
+ {children}
11
+ </div>
12
+ );
13
+ };
14
+
15
+ export default Container;
@@ -0,0 +1,150 @@
1
+ import React from "react";
2
+
3
+ const VimeoEmbed = ({ videoId, autoplay }) => (
4
+ <iframe
5
+ frameBorder="0"
6
+ allowFullScreen="0"
7
+ src={`https://player.vimeo.com/video/${videoId}?color=ffffff&title=0&byline=0&portrait=0&autoplay=${
8
+ autoplay === true ? "1" : "0"
9
+ }`}
10
+ className="content"
11
+ allow="autoplay"
12
+ />
13
+ );
14
+
15
+ const YoukuEmbed = ({ videoId, autoplay }) => (
16
+ <iframe
17
+ height="0"
18
+ width="0"
19
+ src={`https://player.youku.com/embed/${videoId}?autoplay=${
20
+ autoplay === true ? "1" : "0"
21
+ }`}
22
+ frameBorder="0"
23
+ allowFullScreen
24
+ className="content"
25
+ allow="autoplay"
26
+ />
27
+ );
28
+
29
+ const YouTubeEmbed = ({ videoId, autoplay }) => (
30
+ <iframe
31
+ frameBorder="0"
32
+ allowFullScreen
33
+ src={`https://youtube.com/embed/${videoId}?autoplay=${
34
+ autoplay === true ? "1" : "0"
35
+ }`}
36
+ className="content"
37
+ allow="autoplay"
38
+ />
39
+ );
40
+
41
+ const YouTubeNoCookieEmbed = ({ videoId, autoplay }) => (
42
+ <iframe
43
+ frameBorder="0"
44
+ allowFullScreen
45
+ src={`https://www.youtube-nocookie.com/embed/${videoId}?autoplay=${
46
+ autoplay === true ? "1" : "0"
47
+ }`}
48
+ className="content"
49
+ allow="autoplay"
50
+ />
51
+ );
52
+
53
+ const WistiaEmbed = ({ videoId, autoplay }) => (
54
+ <iframe
55
+ frameBorder="0"
56
+ allowFullScreen
57
+ src={`https://fast.wistia.net/embed/iframe/${videoId}${
58
+ autoplay ? "?autoPlay=true" : ""
59
+ }`}
60
+ className="content"
61
+ allow="autoplay"
62
+ />
63
+ );
64
+
65
+ const videoType = (url) => {
66
+ if (url.includes("youtu")) return "youtube";
67
+ if (url.includes("vimeo")) return "vimeo";
68
+ if (url.includes("youku")) return "youku";
69
+ if (url.includes("wistia")) return "wistia";
70
+ };
71
+
72
+ const youtubeId = (url) => {
73
+ let matches = url.match(
74
+ /(youtu\.be\/|youtube\.com\/(watch\?(.*&)?v=|(embed|v)\/))([^\?&"'>]+)/
75
+ );
76
+
77
+ if (matches && matches[5]) {
78
+ return matches[5];
79
+ }
80
+
81
+ matches = url.match(/youtu\.be\/|youtube\.com\/shorts\/(?<id>.*)(\?.*)?/);
82
+
83
+ return matches?.groups?.id || "";
84
+ };
85
+
86
+ const vimeoId = (url) => {
87
+ const matches = url.match(/http(s?):\/\/(www\.)?vimeo\.com\/(\d+)($|\/)/);
88
+ return matches !== "" && matches !== null ? matches[3] : "";
89
+ };
90
+
91
+ const youkuId = (url) => {
92
+ const matches = url.match(
93
+ /http(s?):\/\/v\.youku\.com\/v_show\/id_([A-Za-z0-9=]+)\.html/
94
+ );
95
+ return matches !== "" && matches !== null ? matches[2] : "";
96
+ };
97
+
98
+ const wistiaId = (url) => {
99
+ const matches = url.match(
100
+ /http(s?):\/\/fast\.wistia\.net\/embed\/iframe\/(.*)($|\?)/
101
+ );
102
+ return matches !== "" && matches !== null ? matches[2] : "";
103
+ };
104
+
105
+ const videoId = (url) => {
106
+ if (videoType(url) === "youtube") return youtubeId(url);
107
+ if (videoType(url) === "vimeo") return vimeoId(url);
108
+ if (videoType(url) === "youku") return youkuId(url);
109
+ if (videoType(url) === "wistia") return wistiaId(url);
110
+ };
111
+
112
+ const VideoComponentType = (vType, noCookieDomains) => {
113
+ const components = {
114
+ youtube: YouTubeEmbed,
115
+ vimeo: VimeoEmbed,
116
+ youku: YoukuEmbed,
117
+ wistia: WistiaEmbed,
118
+ };
119
+
120
+ const noCookiesComponents = {
121
+ youtube: YouTubeNoCookieEmbed,
122
+ vimeo: VimeoEmbed,
123
+ youku: YoukuEmbed,
124
+ wistia: WistiaEmbed,
125
+ };
126
+
127
+ if (noCookieDomains) {
128
+ return noCookiesComponents[vType];
129
+ } else {
130
+ return components[vType];
131
+ }
132
+ };
133
+
134
+ const EmbeddedVideo = ({
135
+ videoUrl,
136
+ autoplay = false,
137
+ noCookieDomains = false,
138
+ }) => {
139
+ const vType = videoType(videoUrl);
140
+ const vId = videoId(videoUrl);
141
+ const VideoComponent = VideoComponentType(vType, noCookieDomains);
142
+
143
+ if (VideoComponent) {
144
+ return <VideoComponent videoId={vId} autoplay={autoplay} />;
145
+ }
146
+
147
+ return null;
148
+ };
149
+
150
+ export default EmbeddedVideo;
package/src/Image.js ADDED
@@ -0,0 +1,123 @@
1
+ import React from "react";
2
+
3
+ const getWebp = (url) => {
4
+ if (!url) return null;
5
+
6
+ const segments = url.split(".");
7
+ segments[segments.length - 1] = "webp";
8
+
9
+ return segments.join(".");
10
+ };
11
+
12
+ const getDimensions = (src, variant) => {
13
+ if (typeof src === "object" && src?.name?.includes(".svg")) {
14
+ const width = src.width;
15
+ const height = src.height;
16
+
17
+ return [width, height];
18
+ }
19
+
20
+ if (typeof src === "object") {
21
+ const width = src.sizes[variant]?.width || null;
22
+ const height = src.sizes[variant]?.height || null;
23
+
24
+ return [width, height];
25
+ }
26
+
27
+ return [null, null];
28
+ };
29
+
30
+ const getAlt = (src) => {
31
+ const type = typeof src;
32
+
33
+ if (type === "object" && src.alt) {
34
+ return src.alt;
35
+ }
36
+
37
+ if (type === "object") {
38
+ const imageObj = src.image || src;
39
+
40
+ const altSetting = (imageObj?.settings || []).find(
41
+ (setting) => setting.key === "alt"
42
+ );
43
+
44
+ if (altSetting) {
45
+ return altSetting.value;
46
+ }
47
+ }
48
+
49
+ return "";
50
+ };
51
+
52
+ const getSrc = (src, variant) => {
53
+ if (typeof src === "object") {
54
+ if (src?.name?.includes(".svg") || src?.name?.includes(".gif")) {
55
+ return [null, src.urls?.original];
56
+ }
57
+
58
+ const fallback = src.urls[variant];
59
+ const webp = getWebp(fallback);
60
+
61
+ return [webp, fallback];
62
+ }
63
+
64
+ return [null, src];
65
+ };
66
+
67
+ const getImageObjOrSrc = (value) => {
68
+ const type = typeof value;
69
+
70
+ if (type === "object" && value.hasOwnProperty("image")) {
71
+ return value.image;
72
+ }
73
+
74
+ return value;
75
+ };
76
+
77
+ const Image = ({ variant, src, alt, loading, ...props }) => {
78
+ if (!variant) {
79
+ console.error("Image with no variant", { variant, src, alt, props });
80
+ return null;
81
+ }
82
+
83
+ if (!src) {
84
+ console.error("Image with no src", { variant, src, alt, props });
85
+ return null;
86
+ }
87
+
88
+ const imageObjOrSrc = getImageObjOrSrc(src);
89
+
90
+ if (
91
+ imageObjOrSrc &&
92
+ typeof imageObjOrSrc === "object" &&
93
+ !imageObjOrSrc.sizes
94
+ ) {
95
+ console.error("Image with no sizes", { variant, src, alt, props });
96
+ return null;
97
+ }
98
+
99
+ const [width, height] = getDimensions(imageObjOrSrc, variant);
100
+ const altAttr = alt || getAlt(src);
101
+ const [webp, fallback] = getSrc(imageObjOrSrc, variant);
102
+
103
+ return (
104
+ <picture>
105
+ {webp && <source srcSet={encodeURI(webp)} type="image/webp" />}
106
+ <source srcSet={encodeURI(fallback)} type={src.mime_type} />
107
+ <img
108
+ src={fallback}
109
+ alt={altAttr}
110
+ width={width}
111
+ height={height}
112
+ loading={loading}
113
+ {...props}
114
+ />
115
+ </picture>
116
+ );
117
+ };
118
+
119
+ Image.defaultProps = {
120
+ loading: "lazy",
121
+ };
122
+
123
+ export default Image;
@@ -0,0 +1,5 @@
1
+ const ImagePlaceholder = (size) => {
2
+ return `https://placeholder.raketa.cloud/images/${size}`;
3
+ };
4
+
5
+ export default ImagePlaceholder;
@@ -0,0 +1,17 @@
1
+ const imageSrc = (src, variant) => {
2
+ if (typeof src === "object" && !("name" in src))
3
+ console.warn(`WARNING: Image with empty {}: ${variant}`);
4
+
5
+ if (
6
+ typeof src === "object" &&
7
+ "name" in src &&
8
+ (src.name.includes(".svg") || src.name.includes(".gif"))
9
+ )
10
+ return src.urls.original;
11
+ if (typeof src === "object" && "name" in src) return src.urls[variant];
12
+ if (typeof src === "string") return src;
13
+
14
+ return null;
15
+ };
16
+
17
+ export default imageSrc;
package/src/index.js ADDED
@@ -0,0 +1,8 @@
1
+ import Image from "./Image";
2
+ import imageSrc from "./ImageSrc";
3
+ import imagePlaceholder from "./ImagePlaceholder";
4
+ import Container from "./Container";
5
+ import EmbeddedVideo from "./EmbeddedVideo";
6
+ import toHTML from "./toHTML";
7
+
8
+ export { Image, imageSrc, imagePlaceholder, Container, EmbeddedVideo, toHTML };
package/src/toHTML.js ADDED
@@ -0,0 +1,19 @@
1
+ import MarkdownIt from "markdown-it";
2
+ import markdownItAttrs from "markdown-it-attrs";
3
+
4
+ const parser = new MarkdownIt({
5
+ html: true,
6
+ breaks: true,
7
+ }).use(markdownItAttrs, {
8
+ // optional, these are default options
9
+ leftDelimiter: "{",
10
+ rightDelimiter: "}",
11
+ allowedAttributes: [], // empty array = all attributes are allowed
12
+ });
13
+
14
+ const toHTML = (string, inline = false) => {
15
+ if (!string) return;
16
+ return inline ? parser.renderInline(string) : parser.render(string);
17
+ };
18
+
19
+ export default toHTML;
package/tsconfig.json ADDED
@@ -0,0 +1,5 @@
1
+ {
2
+ "extends": "tsconfig/react-library.json",
3
+ "include": ["src"],
4
+ "exclude": ["dist", "build", "node_modules"]
5
+ }