@raketa-builder/image-input 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/package.json ADDED
@@ -0,0 +1,31 @@
1
+ {
2
+ "name": "@raketa-builder/image-input",
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
+ "styled-components": "^5.1.0",
14
+ "react": "18.2.0",
15
+ "react-dom": "18.2.0"
16
+ },
17
+ "dependencies": {
18
+ "react-dropzone": "^11.0.1",
19
+ "ui": "*"
20
+ },
21
+ "devDependencies": {
22
+ "@types/react": "17.0.37",
23
+ "@types/react-dom": "17.0.11",
24
+ "eslint": "7.32.0",
25
+ "eslint-config-custom": "*",
26
+ "react": "18.2.0",
27
+ "tsconfig": "*",
28
+ "tsup": "6.5.0",
29
+ "typescript": "4.5.2"
30
+ }
31
+ }
@@ -0,0 +1,87 @@
1
+ const serialize = (params) =>
2
+ Object.keys(params)
3
+ .map((k) => encodeURIComponent(k) + "=" + encodeURIComponent(params[k]))
4
+ .join("&");
5
+
6
+ const get = (url, params = {}, opts = {}) =>
7
+ fetch(`${url}?${serialize(params)}`, opts);
8
+
9
+ const patch = (url, params = {}, opts = {}) =>
10
+ fetch(
11
+ url,
12
+ Object.assign(
13
+ {
14
+ method: "PATCH",
15
+ credentials: "same-origin",
16
+ body: JSON.stringify(params),
17
+ },
18
+ opts
19
+ )
20
+ );
21
+
22
+ const post = (url, params = {}, opts = {}) =>
23
+ fetch(
24
+ url,
25
+ Object.assign(
26
+ {
27
+ method: "POST",
28
+ credentials: "same-origin",
29
+ body: JSON.stringify(params),
30
+ },
31
+ opts
32
+ )
33
+ );
34
+
35
+ const del = (url, params = {}, opts = {}) =>
36
+ fetch(
37
+ url,
38
+ Object.assign(
39
+ {
40
+ method: "DELETE",
41
+ credentials: "same-origin",
42
+ body: JSON.stringify(params),
43
+ },
44
+ opts
45
+ )
46
+ );
47
+
48
+ export default class MediaManager {
49
+ constructor(baseUrl) {
50
+ this.baseUrl = baseUrl;
51
+ }
52
+
53
+ findAll(callback, params = {}) {
54
+ get(`${this.baseUrl}images`, { ...params, page_size: 100 })
55
+ .then((r) => r.json())
56
+ .then((images) => callback(images));
57
+ }
58
+
59
+ uploadFile(file, callback) {
60
+ const body = new FormData();
61
+ body.append("image[asset]", file);
62
+
63
+ post(`${this.baseUrl}images`, {}, { body })
64
+ .then((r) => r.json())
65
+ .then((image) => callback(image));
66
+ }
67
+
68
+ update(image, params, callback) {
69
+ patch(
70
+ `${this.baseUrl}images/${image.id}`,
71
+ { image: params },
72
+ { headers: { "content-type": "application/json" } }
73
+ )
74
+ .then((r) => r.json())
75
+ .then((image) => callback(image));
76
+ }
77
+
78
+ destroy(image, callback) {
79
+ del(
80
+ `${this.baseUrl}images/${image.id}`,
81
+ {},
82
+ { headers: { "content-type": "application/json" } }
83
+ )
84
+ .then((r) => r.json())
85
+ .then((image) => callback(image));
86
+ }
87
+ }
@@ -0,0 +1,5 @@
1
+ import React from "react";
2
+
3
+ const MediaManagerContext = React.createContext({});
4
+
5
+ export default MediaManagerContext;
@@ -0,0 +1,52 @@
1
+ import React from "react";
2
+ import styled from "styled-components";
3
+ import { Modal, Button } from "@raketa-builder/ui";
4
+
5
+ const ModalContent = styled.div`
6
+ max-height: 75vh;
7
+ overflow-y: auto;
8
+ `;
9
+
10
+ const ModalFooter = styled.div`
11
+ padding-top: ${(props) => props.theme.font.base};
12
+ border-top: 1px solid ${(props) => props.theme.colors.gray};
13
+ `;
14
+
15
+ const Dialog = ({
16
+ open,
17
+ onClose,
18
+ title,
19
+ children,
20
+ primaryLabel,
21
+ secondaryLabel = "Cancel",
22
+ onPrimary,
23
+ width,
24
+ dialogSize,
25
+ }) => (
26
+ <Modal title={title} onClose={onClose}>
27
+ <ModalContent>{children}</ModalContent>
28
+
29
+ {(primaryLabel || secondaryLabel) && (
30
+ <ModalFooter>
31
+ {primaryLabel && (
32
+ <Button type="button" variant="primary" onClick={onPrimary}>
33
+ {primaryLabel}
34
+ </Button>
35
+ )}
36
+
37
+ {secondaryLabel && (
38
+ <Button type="button" variant="secondary" onClick={onClose}>
39
+ {secondaryLabel}
40
+ </Button>
41
+ )}
42
+ </ModalFooter>
43
+ )}
44
+ </Modal>
45
+ );
46
+
47
+ Dialog.defaultProps = {
48
+ children: [],
49
+ dialogSize: "",
50
+ };
51
+
52
+ export default Dialog;
@@ -0,0 +1,50 @@
1
+ import React from "react";
2
+ import { FormGroup, Label, Input, Textarea, P } from "@raketa-builder/ui";
3
+ import { randomString } from "../lists";
4
+
5
+ const TextField = (props) => {
6
+ const id = randomString();
7
+ const { label, hint, multiline, onChange } = props;
8
+
9
+ const inputProps = Object.assign({}, props);
10
+ delete inputProps.label;
11
+ delete inputProps.hint;
12
+ delete inputProps.multiline;
13
+ delete inputProps.onChange;
14
+
15
+ const Component = multiline ? Textarea : Input;
16
+
17
+ return (
18
+ <FormGroup>
19
+ {label ? (
20
+ <Label htmlFor={id} error={props.error}>
21
+ {label}
22
+ </Label>
23
+ ) : (
24
+ ""
25
+ )}
26
+ <Component
27
+ id={id}
28
+ onChange={(e) => onChange(e.target.value)}
29
+ {...inputProps}
30
+ />
31
+ {hint ? <P>{hint}</P> : ""}
32
+ </FormGroup>
33
+ );
34
+ };
35
+
36
+ const TextInput = ({ label, onChange, value, placeholder }) => (
37
+ <TextField
38
+ label={label}
39
+ value={value}
40
+ placeholder={placeholder}
41
+ onChange={onChange}
42
+ />
43
+ );
44
+
45
+ TextInput.defaultProps = {
46
+ value: "",
47
+ placeholder: "",
48
+ };
49
+
50
+ export default TextInput;
@@ -0,0 +1,5 @@
1
+ const imgPlaceholder = (size) => {
2
+ return `https://placeholder.raketa.cloud/images/${size}`;
3
+ };
4
+
5
+ export default imgPlaceholder;
@@ -0,0 +1,17 @@
1
+ const imgageSrc = (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 imgageSrc;
package/src/index.js ADDED
@@ -0,0 +1,17 @@
1
+ import ImagePicker from "./pickers/ImagePicker/ImagePicker";
2
+ import ImageInput from "./pickers/ImagePicker/ImageInput";
3
+ import Img from "./lib/Image";
4
+ import imageSrc from "./helpers/imageSrc";
5
+ import imagePlaceholder from "./helpers/imagePlaceholder";
6
+ import MediaManager from "./MediaManager";
7
+ import MediaManagerContext from "./MediaManagerContext";
8
+
9
+ export {
10
+ ImagePicker,
11
+ ImageInput,
12
+ Img,
13
+ imageSrc,
14
+ imagePlaceholder,
15
+ MediaManager,
16
+ MediaManagerContext,
17
+ };
@@ -0,0 +1,58 @@
1
+ import React from "react";
2
+ import styled from "styled-components";
3
+ import { H, Button } from "@raketa-builder/ui";
4
+
5
+ import Dropzone from "react-dropzone";
6
+
7
+ const StyledDropzone = styled.div`
8
+ position: relative;
9
+ margin-bottom: 16px;
10
+ height: 350px;
11
+ color: #666;
12
+ background-color: #efefef;
13
+ border: 2px dashed #ddd;
14
+ `;
15
+
16
+ const DropzoneLabel = styled.div`
17
+ position: absolute;
18
+ top: 0;
19
+ right: 0;
20
+ bottom: 0;
21
+ left: 0;
22
+ padding: 16px;
23
+ `;
24
+
25
+ const Dropbox = ({ buttonLabel, onDrop }) => {
26
+ const dropzoneRef = React.createRef();
27
+
28
+ const handleOpen = () => dropzoneRef.current.open();
29
+
30
+ return (
31
+ <React.Fragment>
32
+ <StyledDropzone>
33
+ <Dropzone ref={dropzoneRef} onDrop={onDrop}>
34
+ {({ getRootProps, getInputProps }) => (
35
+ <DropzoneLabel {...getRootProps()}>
36
+ <H size="large">Select files from your computer</H>
37
+ <small>or drag and drop here</small>
38
+ <input {...getInputProps()} />
39
+ </DropzoneLabel>
40
+ )}
41
+ </Dropzone>
42
+ </StyledDropzone>
43
+
44
+ <Button
45
+ type="button"
46
+ variant="success"
47
+ onClick={handleOpen}
48
+ style={{ marginBottom: "1em" }}
49
+ >
50
+ {buttonLabel}
51
+ </Button>
52
+ </React.Fragment>
53
+ );
54
+ };
55
+
56
+ Dropbox.defaultProps = { buttonLabel: "Select files..." };
57
+
58
+ export default Dropbox;
@@ -0,0 +1,69 @@
1
+ /* eslint-disable @next/next/no-img-element */
2
+ import React from "react";
3
+
4
+ const Img = ({
5
+ src,
6
+ variant,
7
+ className,
8
+ onLoad,
9
+ alt = null,
10
+ title = null,
11
+ ...props
12
+ }) => {
13
+ if (!src) {
14
+ console.warn(
15
+ `WARNING: Image with src null or undefined will not be rendered: ${variant} ${
16
+ className || ""
17
+ }`
18
+ );
19
+ return null;
20
+ }
21
+
22
+ const imgAlt = alt !== null ? alt : src.alt;
23
+ const imgTitle = title !== null ? title : "";
24
+
25
+ if (typeof src === "object" && !("name" in src))
26
+ console.warn(`WARNING: Image with empty {}: ${variant} ${className || ""}`);
27
+
28
+ if (
29
+ typeof src === "object" &&
30
+ "name" in src &&
31
+ (src.name.includes(".svg") || src.name.includes(".gif"))
32
+ )
33
+ return (
34
+ <img
35
+ src={src.urls.original}
36
+ alt={imgAlt !== null ? imgAlt : src.name}
37
+ className={className}
38
+ onLoad={onLoad}
39
+ title={imgTitle}
40
+ {...props}
41
+ />
42
+ );
43
+ if (typeof src === "object" && "name" in src)
44
+ return (
45
+ <img
46
+ src={src.urls[variant]}
47
+ alt={imgAlt !== null ? imgAlt : src.name}
48
+ className={className}
49
+ onLoad={onLoad}
50
+ title={imgTitle}
51
+ {...props}
52
+ />
53
+ );
54
+ if (typeof src === "string")
55
+ return (
56
+ <img
57
+ src={src}
58
+ alt={src}
59
+ className={className}
60
+ onLoad={onLoad}
61
+ title={imgTitle}
62
+ {...props}
63
+ />
64
+ );
65
+
66
+ return null;
67
+ };
68
+
69
+ export default Img;
@@ -0,0 +1,30 @@
1
+ import React from "react";
2
+ import styled from "styled-components";
3
+
4
+ const ProgressWrapper = styled.div`
5
+ height: 2em;
6
+ background-color: #efefef;
7
+ `;
8
+
9
+ const Progress = styled.div`
10
+ padding-left: 0.5em;
11
+ height: 2.4em;
12
+ font-size: 0.85em;
13
+ line-height: 2.4em;
14
+ color: #fff;
15
+ background-color: #4cae4c;
16
+ `;
17
+
18
+ const ProgressBar = ({ total, done }) => {
19
+ const progress = (100 / total) * done;
20
+
21
+ return (
22
+ <ProgressWrapper>
23
+ <Progress style={{ width: `${progress}%` }}>
24
+ {done}/{total}
25
+ </Progress>
26
+ </ProgressWrapper>
27
+ );
28
+ };
29
+
30
+ export default ProgressBar;
@@ -0,0 +1,39 @@
1
+ const add = (items, item) => [...items, item];
2
+ const addAt = (items, item, idx) => console.log("TODO: Implement me");
3
+ const removeByIndex = (items, idx) => [
4
+ ...items.slice(0, idx),
5
+ ...items.slice(idx + 1),
6
+ ];
7
+ const removeById = (items, id, idField = "id") =>
8
+ removeByIndex(
9
+ items,
10
+ items.findIndex((current) => current[idField] === id)
11
+ );
12
+ const updateFieldByIndex = (items, field, value, idx) => [
13
+ ...items.slice(0, idx),
14
+ Object.assign({}, items[idx], { [field]: value }),
15
+ ...items.slice(idx + 1),
16
+ ];
17
+ const updateFieldById = (items, field, value, id, idField = "id") =>
18
+ updateFieldByIndex(
19
+ items,
20
+ field,
21
+ value,
22
+ items.findIndex((current) => current[idField] === id)
23
+ );
24
+ const reorder = (items, order) => order.map((idx) => items[idx]);
25
+
26
+ const randomId = () => Math.floor(Math.random() * (999 - 100 + 1)) + 100;
27
+ const randomString = (length) => Math.random().toString(36).substring(length);
28
+
29
+ export {
30
+ add,
31
+ addAt,
32
+ removeByIndex,
33
+ removeById,
34
+ updateFieldByIndex,
35
+ updateFieldById,
36
+ reorder,
37
+ randomId,
38
+ randomString,
39
+ };