@raketa-builder/builder 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/.turbo/turbo-build.log +8 -0
- package/.turbo/turbo-lint.log +13 -0
- package/CHANGELOG.md +7 -0
- package/dist/index.js +3247 -0
- package/package.json +31 -0
- package/src/Canvas.js +70 -0
- package/src/Container.js +15 -0
- package/src/EmbeddedVideo.js +143 -0
- package/src/ErrorBoundary/ErrorBoundary.js +28 -0
- package/src/ErrorBoundary/index.js +3 -0
- package/src/HostContext.js +5 -0
- package/src/LibraryContext.js +5 -0
- package/src/List.js +307 -0
- package/src/ListInput.js +13 -0
- package/src/MediaManager.js +15 -0
- package/src/PageBuilder.js +234 -0
- package/src/contexts/BuilderContext.js +8 -0
- package/src/dialogs/Dialog.js +49 -0
- package/src/dialogs/ReorderDialog.js +136 -0
- package/src/dialogs/SettingsDialog.js +180 -0
- package/src/forms/Button.js +33 -0
- package/src/forms/ButtonSettings.js +152 -0
- package/src/forms/LinkSettings.js +138 -0
- package/src/forms/RichText.js +22 -0
- package/src/forms/SelectMenu.js +68 -0
- package/src/forms/TextArea.js +11 -0
- package/src/forms/TextInput.js +31 -0
- package/src/helpers/humanize.js +9 -0
- package/src/helpers/imageSrc.js +17 -0
- package/src/helpers/widgetData.js +34 -0
- package/src/index.js +59 -0
- package/src/lib/AdminSidebar.js +178 -0
- package/src/lib/AdminWidget.js +132 -0
- package/src/lib/Image.js +78 -0
- package/src/lib/LanguageSwitcher.js +162 -0
- package/src/lib/Link.js +19 -0
- package/src/lib/SideNav.js +164 -0
- package/src/lib/SidebarItem.js +20 -0
- package/src/lists/index.js +41 -0
- package/src/pickers/ImagePicker/ImageInput.js +17 -0
- package/src/pickers/ImagePicker/ImagePicker.js +22 -0
- package/tsconfig.json +5 -0
package/package.json
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@raketa-builder/builder",
|
|
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-sortablejs": "2.0.11",
|
|
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
|
+
}
|
package/src/Canvas.js
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import styled from "styled-components";
|
|
3
|
+
import { H, P } from "@raketa-builder/ui";
|
|
4
|
+
|
|
5
|
+
import { ReactSortable } from "react-sortablejs";
|
|
6
|
+
import AdminWidget from "./lib/AdminWidget";
|
|
7
|
+
import ErrorBoundary from "./ErrorBoundary";
|
|
8
|
+
import LibraryContext from "./LibraryContext";
|
|
9
|
+
|
|
10
|
+
const EmptyCanvas = styled.div`
|
|
11
|
+
display: flex;
|
|
12
|
+
justify-content: center;
|
|
13
|
+
align-items: center;
|
|
14
|
+
height: 100vh;
|
|
15
|
+
`;
|
|
16
|
+
|
|
17
|
+
const Canvas = React.memo(
|
|
18
|
+
function Canvas({ widgets, onReorder, onEdit, onRemove, identifier }) {
|
|
19
|
+
const { library, adminLibrary } = React.useContext(LibraryContext);
|
|
20
|
+
|
|
21
|
+
if (widgets.length === 0) {
|
|
22
|
+
return (
|
|
23
|
+
<EmptyCanvas>
|
|
24
|
+
<div>
|
|
25
|
+
<H size="xxlarge">
|
|
26
|
+
It's always good to start with a clean slate.
|
|
27
|
+
</H>
|
|
28
|
+
<P>Use the sidebar to add content.</P>
|
|
29
|
+
</div>
|
|
30
|
+
</EmptyCanvas>
|
|
31
|
+
);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
return (
|
|
35
|
+
<ReactSortable
|
|
36
|
+
list={widgets}
|
|
37
|
+
setList={onReorder}
|
|
38
|
+
handle="[data-drag]"
|
|
39
|
+
direction="horizontal"
|
|
40
|
+
dragoverBubble
|
|
41
|
+
>
|
|
42
|
+
{widgets.map((widget) => (
|
|
43
|
+
<ErrorBoundary key={widget.widgetId}>
|
|
44
|
+
<AdminWidget
|
|
45
|
+
library={library}
|
|
46
|
+
adminLibrary={adminLibrary}
|
|
47
|
+
identifier={identifier}
|
|
48
|
+
widget={widget}
|
|
49
|
+
onEdit={onEdit}
|
|
50
|
+
onDelete={onRemove}
|
|
51
|
+
/>
|
|
52
|
+
</ErrorBoundary>
|
|
53
|
+
))}
|
|
54
|
+
</ReactSortable>
|
|
55
|
+
);
|
|
56
|
+
},
|
|
57
|
+
(props, nextProps) => {
|
|
58
|
+
// TODO: Make the comparision quicker
|
|
59
|
+
const themesAreEqual =
|
|
60
|
+
JSON.stringify(props.themes) === JSON.stringify(nextProps.themes);
|
|
61
|
+
const spacingsAreEqual =
|
|
62
|
+
JSON.stringify(props.spacings) === JSON.stringify(nextProps.spacings);
|
|
63
|
+
const widgetsAreEqual =
|
|
64
|
+
JSON.stringify(props.widgets) === JSON.stringify(nextProps.widgets);
|
|
65
|
+
|
|
66
|
+
return themesAreEqual && spacingsAreEqual && widgetsAreEqual;
|
|
67
|
+
}
|
|
68
|
+
);
|
|
69
|
+
|
|
70
|
+
export default Canvas;
|
package/src/Container.js
ADDED
|
@@ -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,143 @@
|
|
|
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
|
+
const matches = url.match(
|
|
74
|
+
/(youtu\.be\/|youtube\.com\/(watch\?(.*&)?v=|(embed|v)\/))([^\?&"'>]+)/
|
|
75
|
+
);
|
|
76
|
+
return matches !== "" && matches !== null ? matches[5] : "";
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
const vimeoId = (url) => {
|
|
80
|
+
const matches = url.match(/http(s?):\/\/(www\.)?vimeo\.com\/(\d+)($|\/)/);
|
|
81
|
+
return matches !== "" && matches !== null ? matches[3] : "";
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
const youkuId = (url) => {
|
|
85
|
+
const matches = url.match(
|
|
86
|
+
/http(s?):\/\/v\.youku\.com\/v_show\/id_([A-Za-z0-9=]+)\.html/
|
|
87
|
+
);
|
|
88
|
+
return matches !== "" && matches !== null ? matches[2] : "";
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
const wistiaId = (url) => {
|
|
92
|
+
const matches = url.match(
|
|
93
|
+
/http(s?):\/\/fast\.wistia\.net\/embed\/iframe\/(.*)($|\?)/
|
|
94
|
+
);
|
|
95
|
+
return matches !== "" && matches !== null ? matches[2] : "";
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
const videoId = (url) => {
|
|
99
|
+
if (videoType(url) === "youtube") return youtubeId(url);
|
|
100
|
+
if (videoType(url) === "vimeo") return vimeoId(url);
|
|
101
|
+
if (videoType(url) === "youku") return youkuId(url);
|
|
102
|
+
if (videoType(url) === "wistia") return wistiaId(url);
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
const VideoComponentType = (vType, noCookieDomains) => {
|
|
106
|
+
const components = {
|
|
107
|
+
youtube: YouTubeEmbed,
|
|
108
|
+
vimeo: VimeoEmbed,
|
|
109
|
+
youku: YoukuEmbed,
|
|
110
|
+
wistia: WistiaEmbed,
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
const noCookiesComponents = {
|
|
114
|
+
youtube: YouTubeNoCookieEmbed,
|
|
115
|
+
vimeo: VimeoEmbed,
|
|
116
|
+
youku: YoukuEmbed,
|
|
117
|
+
wistia: WistiaEmbed,
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
if (noCookieDomains) {
|
|
121
|
+
return noCookiesComponents[vType];
|
|
122
|
+
} else {
|
|
123
|
+
return components[vType];
|
|
124
|
+
}
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
const EmbeddedVideo = ({
|
|
128
|
+
videoUrl,
|
|
129
|
+
autoplay = false,
|
|
130
|
+
noCookieDomains = false,
|
|
131
|
+
}) => {
|
|
132
|
+
const vType = videoType(videoUrl);
|
|
133
|
+
const vId = videoId(videoUrl);
|
|
134
|
+
const VideoComponent = VideoComponentType(vType, noCookieDomains);
|
|
135
|
+
|
|
136
|
+
if (VideoComponent) {
|
|
137
|
+
return <VideoComponent videoId={vId} autoplay={autoplay} />;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
return null;
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
export default EmbeddedVideo;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import React, { Component } from "react";
|
|
2
|
+
|
|
3
|
+
class ErrorBoundary extends Component {
|
|
4
|
+
constructor(props) {
|
|
5
|
+
super(props);
|
|
6
|
+
|
|
7
|
+
this.state = {
|
|
8
|
+
hasError: false,
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
static getDerivedStateFromError(error) {
|
|
13
|
+
return {
|
|
14
|
+
hasError: true,
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
componentDidCatch(error, info) {
|
|
19
|
+
console.error(error, info);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
render() {
|
|
23
|
+
if (this.state.hasError) return null;
|
|
24
|
+
|
|
25
|
+
return this.props.children;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
export default ErrorBoundary;
|
package/src/List.js
ADDED
|
@@ -0,0 +1,307 @@
|
|
|
1
|
+
/* eslint-disable @next/next/no-img-element */
|
|
2
|
+
import React from "react";
|
|
3
|
+
import styled from "styled-components";
|
|
4
|
+
import { ReactSortable } from "react-sortablejs";
|
|
5
|
+
import { reset, buttonReset, Button, P, H } from "@raketa-builder/ui";
|
|
6
|
+
|
|
7
|
+
import {
|
|
8
|
+
add,
|
|
9
|
+
addAtBeggining,
|
|
10
|
+
removeByIndex,
|
|
11
|
+
updateFieldByIndex,
|
|
12
|
+
randomId,
|
|
13
|
+
} from "./lists";
|
|
14
|
+
|
|
15
|
+
const Handle = (props) => (
|
|
16
|
+
<HandleWrapper data-drag>
|
|
17
|
+
<img
|
|
18
|
+
{...props}
|
|
19
|
+
src="data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIyNCIgaGVpZ2h0PSIyNCIgdmlld0JveD0iMCAwIDI0IDI0IiBmaWxsPSJub25lIiBzdHJva2U9ImN1cnJlbnRDb2xvciIgc3Ryb2tlLXdpZHRoPSIyIiBzdHJva2UtbGluZWNhcD0icm91bmQiIHN0cm9rZS1saW5lam9pbj0icm91bmQiIGNsYXNzPSJmZWF0aGVyIGZlYXRoZXItbW9yZS12ZXJ0aWNhbCI+PGNpcmNsZSBjeD0iMTIiIGN5PSIxMiIgcj0iMSI+PC9jaXJjbGU+PGNpcmNsZSBjeD0iMTIiIGN5PSI1IiByPSIxIj48L2NpcmNsZT48Y2lyY2xlIGN4PSIxMiIgY3k9IjE5IiByPSIxIj48L2NpcmNsZT48L3N2Zz4="
|
|
20
|
+
alt="handle"
|
|
21
|
+
/>
|
|
22
|
+
</HandleWrapper>
|
|
23
|
+
);
|
|
24
|
+
|
|
25
|
+
const IconDelete = () => (
|
|
26
|
+
<img
|
|
27
|
+
src="data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIyNCIgaGVpZ2h0PSIyNCIgdmlld0JveD0iMCAwIDI0IDI0IiBmaWxsPSJub25lIiBzdHJva2U9ImN1cnJlbnRDb2xvciIgc3Ryb2tlLXdpZHRoPSIyIiBzdHJva2UtbGluZWNhcD0icm91bmQiIHN0cm9rZS1saW5lam9pbj0icm91bmQiIGNsYXNzPSJmZWF0aGVyIGZlYXRoZXItdHJhc2giPjxwb2x5bGluZSBwb2ludHM9IjMgNiA1IDYgMjEgNiI+PC9wb2x5bGluZT48cGF0aCBkPSJNMTkgNnYxNGEyIDIgMCAwIDEtMiAySDdhMiAyIDAgMCAxLTItMlY2bTMgMFY0YTIgMiAwIDAgMSAyLTJoNGEyIDIgMCAwIDEgMiAydjIiPjwvcGF0aD48L3N2Zz4="
|
|
28
|
+
style={{ width: "16px" }}
|
|
29
|
+
alt="delete"
|
|
30
|
+
/>
|
|
31
|
+
);
|
|
32
|
+
|
|
33
|
+
const IconMaximize = () => (
|
|
34
|
+
<img
|
|
35
|
+
src="data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIyNCIgaGVpZ2h0PSIyNCIgdmlld0JveD0iMCAwIDI0IDI0IiBmaWxsPSJub25lIiBzdHJva2U9IiMwMDAiIHN0cm9rZS13aWR0aD0iMiIgc3Ryb2tlLWxpbmVjYXA9InJvdW5kIiBzdHJva2UtbGluZWpvaW49InJvdW5kIiBjbGFzcz0iZmVhdGhlciBmZWF0aGVyLWNoZXZyb24tZG93biI+PHBvbHlsaW5lIHBvaW50cz0iNiA5IDEyIDE1IDE4IDkiPjwvcG9seWxpbmU+PC9zdmc+"
|
|
36
|
+
style={{ width: "16px" }}
|
|
37
|
+
alt="maximize"
|
|
38
|
+
/>
|
|
39
|
+
);
|
|
40
|
+
|
|
41
|
+
const IconMinimize = () => (
|
|
42
|
+
<img
|
|
43
|
+
src="data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIyNCIgaGVpZ2h0PSIyNCIgdmlld0JveD0iMCAwIDI0IDI0IiBmaWxsPSJub25lIiBzdHJva2U9IiMwMDAiIHN0cm9rZS13aWR0aD0iMiIgc3Ryb2tlLWxpbmVjYXA9InJvdW5kIiBzdHJva2UtbGluZWpvaW49InJvdW5kIiBjbGFzcz0iZmVhdGhlciBmZWF0aGVyLWNoZXZyb24tdXAiPjxwb2x5bGluZSBwb2ludHM9IjE4IDE1IDEyIDkgNiAxNSI+PC9wb2x5bGluZT48L3N2Zz4="
|
|
44
|
+
style={{ width: "16px" }}
|
|
45
|
+
alt="minimize"
|
|
46
|
+
/>
|
|
47
|
+
);
|
|
48
|
+
|
|
49
|
+
const ListWrapper = styled.div`
|
|
50
|
+
${reset};
|
|
51
|
+
margin-bottom: ${(props) => props.theme.font.base};
|
|
52
|
+
`;
|
|
53
|
+
|
|
54
|
+
const AddButtonWrapepr = styled.div`
|
|
55
|
+
${reset};
|
|
56
|
+
margin-bottom: ${(props) => props.theme.font.base};
|
|
57
|
+
`;
|
|
58
|
+
|
|
59
|
+
const HandleWrapper = styled.div`
|
|
60
|
+
display: flex;
|
|
61
|
+
align-items: center;
|
|
62
|
+
width: 16px;
|
|
63
|
+
padding-right: 8px;
|
|
64
|
+
cursor: move;
|
|
65
|
+
|
|
66
|
+
& > img {
|
|
67
|
+
width: 16px;
|
|
68
|
+
}
|
|
69
|
+
`;
|
|
70
|
+
|
|
71
|
+
const ItemWrapper = styled.div`
|
|
72
|
+
${reset};
|
|
73
|
+
margin-bottom: ${(props) => props.theme.font.base};
|
|
74
|
+
border: 1px solid ${(props) => props.theme.colors.gray};
|
|
75
|
+
border-radius: 5px;
|
|
76
|
+
`;
|
|
77
|
+
|
|
78
|
+
const TitleBar = styled.div`
|
|
79
|
+
${reset};
|
|
80
|
+
display: flex;
|
|
81
|
+
justify-content: space-between;
|
|
82
|
+
align-items: center;
|
|
83
|
+
width: 100%;
|
|
84
|
+
border-top-left-radius: 5px;
|
|
85
|
+
border-top-right-radius: 5px;
|
|
86
|
+
border-bottom: 1px solid ${(props) => props.theme.colors.gray};
|
|
87
|
+
padding: 8px;
|
|
88
|
+
background-color: #efefef;
|
|
89
|
+
`;
|
|
90
|
+
|
|
91
|
+
const TitleWrapper = styled.button`
|
|
92
|
+
${buttonReset};
|
|
93
|
+
display: flex;
|
|
94
|
+
justify-content: space-between;
|
|
95
|
+
align-items: center;
|
|
96
|
+
flex: 1;
|
|
97
|
+
padding: 0;
|
|
98
|
+
`;
|
|
99
|
+
|
|
100
|
+
const ContentWrapper = styled.div`
|
|
101
|
+
${reset};
|
|
102
|
+
padding: 8px;
|
|
103
|
+
`;
|
|
104
|
+
|
|
105
|
+
const ListButton = styled.button`
|
|
106
|
+
${buttonReset};
|
|
107
|
+
display: flex;
|
|
108
|
+
justify-content: center;
|
|
109
|
+
align-items: center;
|
|
110
|
+
`;
|
|
111
|
+
|
|
112
|
+
const Title = styled.span`
|
|
113
|
+
${reset};
|
|
114
|
+
font-weight: 700;
|
|
115
|
+
`;
|
|
116
|
+
|
|
117
|
+
const extractPrimaryField = (primaryField, item, idx) => {
|
|
118
|
+
if (!primaryField) {
|
|
119
|
+
return `Item ${idx + 1}`;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
const keys = primaryField.split(".");
|
|
123
|
+
|
|
124
|
+
if (keys.length === 1) {
|
|
125
|
+
return item[primaryField];
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
return primaryField.split(".").reduce((acc, key) => {
|
|
129
|
+
const newValue = (acc && acc[key]) || "";
|
|
130
|
+
|
|
131
|
+
return newValue;
|
|
132
|
+
}, item);
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
const Item = ({
|
|
136
|
+
idx,
|
|
137
|
+
items,
|
|
138
|
+
item,
|
|
139
|
+
alwaysOpened,
|
|
140
|
+
primaryField,
|
|
141
|
+
template,
|
|
142
|
+
handleRemove,
|
|
143
|
+
handleChangeField,
|
|
144
|
+
listItem,
|
|
145
|
+
}) => {
|
|
146
|
+
const [open, setOpen] = React.useState(alwaysOpened);
|
|
147
|
+
|
|
148
|
+
return (
|
|
149
|
+
<ItemWrapper>
|
|
150
|
+
<TitleBar>
|
|
151
|
+
<Handle />
|
|
152
|
+
|
|
153
|
+
<TitleWrapper type="button" onClick={() => setOpen(!open)}>
|
|
154
|
+
<Title>{extractPrimaryField(primaryField, item, idx)}</Title>
|
|
155
|
+
{!alwaysOpened && (
|
|
156
|
+
<ListButton type="button">
|
|
157
|
+
{open ? <IconMinimize /> : <IconMaximize />}
|
|
158
|
+
</ListButton>
|
|
159
|
+
)}
|
|
160
|
+
</TitleWrapper>
|
|
161
|
+
|
|
162
|
+
<div>
|
|
163
|
+
{(template || (!template && items.length !== 1)) && (
|
|
164
|
+
<ListButton type="button" danger onClick={() => handleRemove(idx)}>
|
|
165
|
+
<IconDelete />
|
|
166
|
+
</ListButton>
|
|
167
|
+
)}
|
|
168
|
+
</div>
|
|
169
|
+
</TitleBar>
|
|
170
|
+
|
|
171
|
+
{(alwaysOpened || open) && (
|
|
172
|
+
<ContentWrapper>
|
|
173
|
+
{listItem(
|
|
174
|
+
item,
|
|
175
|
+
(field, value) => handleChangeField(idx, field, value),
|
|
176
|
+
idx
|
|
177
|
+
)}
|
|
178
|
+
</ContentWrapper>
|
|
179
|
+
)}
|
|
180
|
+
</ItemWrapper>
|
|
181
|
+
);
|
|
182
|
+
};
|
|
183
|
+
|
|
184
|
+
const List = ({
|
|
185
|
+
label,
|
|
186
|
+
items: defaultItems,
|
|
187
|
+
listItem,
|
|
188
|
+
template,
|
|
189
|
+
primaryField,
|
|
190
|
+
onChangeList,
|
|
191
|
+
itemsAlwaysOpen = false,
|
|
192
|
+
}) => {
|
|
193
|
+
const [items, setItems] = React.useState(defaultItems);
|
|
194
|
+
|
|
195
|
+
const factory = (template) => {
|
|
196
|
+
if (!template) throw new Error("Template not specified for this list");
|
|
197
|
+
|
|
198
|
+
return Object.assign({}, template, { id: randomId() });
|
|
199
|
+
};
|
|
200
|
+
|
|
201
|
+
const handleChangeField = (idx, field, value) => {
|
|
202
|
+
const newItems = updateFieldByIndex(items, field, value, idx);
|
|
203
|
+
|
|
204
|
+
setItems(newItems);
|
|
205
|
+
|
|
206
|
+
notifyChange(newItems);
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
const notifyChange = (items) => {
|
|
210
|
+
onChangeList("list", items);
|
|
211
|
+
};
|
|
212
|
+
|
|
213
|
+
const handleAdd = () => {
|
|
214
|
+
const newItems = add(items, factory(template));
|
|
215
|
+
|
|
216
|
+
setItems(newItems);
|
|
217
|
+
|
|
218
|
+
notifyChange(newItems);
|
|
219
|
+
};
|
|
220
|
+
|
|
221
|
+
const handleAddAtBeggining = () => {
|
|
222
|
+
const newItems = addAtBeggining(items, factory(template));
|
|
223
|
+
|
|
224
|
+
setItems(newItems);
|
|
225
|
+
|
|
226
|
+
notifyChange(newItems);
|
|
227
|
+
};
|
|
228
|
+
|
|
229
|
+
const handleReorder = (items) => {
|
|
230
|
+
setItems(items);
|
|
231
|
+
|
|
232
|
+
notifyChange(items);
|
|
233
|
+
};
|
|
234
|
+
|
|
235
|
+
const handleRemove = (idx) => {
|
|
236
|
+
if (!template && items.length === 1) return;
|
|
237
|
+
|
|
238
|
+
const newItems = removeByIndex(items, idx);
|
|
239
|
+
|
|
240
|
+
setItems(newItems);
|
|
241
|
+
|
|
242
|
+
notifyChange(newItems);
|
|
243
|
+
};
|
|
244
|
+
|
|
245
|
+
if (!primaryField) console.warn("No primaryField provided for a list", items);
|
|
246
|
+
if (!label) console.warn("No label provided for a list", items);
|
|
247
|
+
|
|
248
|
+
return (
|
|
249
|
+
<ListWrapper>
|
|
250
|
+
{label && (
|
|
251
|
+
<H size="base" style={{ fontWeight: 700 }}>
|
|
252
|
+
{label}
|
|
253
|
+
</H>
|
|
254
|
+
)}
|
|
255
|
+
|
|
256
|
+
{items.length === 0 && <P>There are no items yet.</P>}
|
|
257
|
+
|
|
258
|
+
{items.length > 0 && (
|
|
259
|
+
<React.Fragment>
|
|
260
|
+
<AddButtonWrapepr>
|
|
261
|
+
<Button
|
|
262
|
+
type="button"
|
|
263
|
+
variant="success"
|
|
264
|
+
onClick={handleAddAtBeggining}
|
|
265
|
+
>
|
|
266
|
+
Add item
|
|
267
|
+
</Button>
|
|
268
|
+
</AddButtonWrapepr>
|
|
269
|
+
|
|
270
|
+
<ReactSortable
|
|
271
|
+
list={items}
|
|
272
|
+
setList={handleReorder}
|
|
273
|
+
handle="[data-drag]"
|
|
274
|
+
direction="horizontal"
|
|
275
|
+
dragoverBubble
|
|
276
|
+
>
|
|
277
|
+
{items.map((item, idx) => (
|
|
278
|
+
<Item
|
|
279
|
+
key={item.id}
|
|
280
|
+
data-id={idx}
|
|
281
|
+
idx={idx}
|
|
282
|
+
items={items}
|
|
283
|
+
item={item}
|
|
284
|
+
alwaysOpened={itemsAlwaysOpen}
|
|
285
|
+
primaryField={primaryField}
|
|
286
|
+
template={template}
|
|
287
|
+
handleRemove={handleRemove}
|
|
288
|
+
handleChangeField={handleChangeField}
|
|
289
|
+
listItem={listItem}
|
|
290
|
+
/>
|
|
291
|
+
))}
|
|
292
|
+
</ReactSortable>
|
|
293
|
+
</React.Fragment>
|
|
294
|
+
)}
|
|
295
|
+
|
|
296
|
+
<Button type="button" variant="success" onClick={handleAdd}>
|
|
297
|
+
Add item
|
|
298
|
+
</Button>
|
|
299
|
+
</ListWrapper>
|
|
300
|
+
);
|
|
301
|
+
};
|
|
302
|
+
|
|
303
|
+
List.defaultProps = {
|
|
304
|
+
items: [],
|
|
305
|
+
};
|
|
306
|
+
|
|
307
|
+
export default List;
|
package/src/ListInput.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import { ThemeProvider } from "styled-components";
|
|
3
|
+
import { theme } from "@raketa-builder/ui";
|
|
4
|
+
|
|
5
|
+
import List from "./List";
|
|
6
|
+
|
|
7
|
+
const ListInput = (props) => (
|
|
8
|
+
<ThemeProvider theme={theme}>
|
|
9
|
+
<List {...props} />
|
|
10
|
+
</ThemeProvider>
|
|
11
|
+
);
|
|
12
|
+
|
|
13
|
+
export default ListInput;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export default class MediaManager {
|
|
2
|
+
constructor() {
|
|
3
|
+
console.warn(
|
|
4
|
+
"MediaManager is no longer part of @raketa-builder/builder. Please use @raketa-builder/image-input. "
|
|
5
|
+
);
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
findAll(callback, params = {}) {}
|
|
9
|
+
|
|
10
|
+
uploadFile(file, callback) {}
|
|
11
|
+
|
|
12
|
+
update(image, params, callback) {}
|
|
13
|
+
|
|
14
|
+
destroy(image, callback) {}
|
|
15
|
+
}
|