@vonaffenfels/slate-editor 1.0.1
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/.babelrc +44 -0
- package/README.md +5 -0
- package/componentLoader.js +52 -0
- package/dist/BlockEditor.css +93 -0
- package/dist/BlockEditor.js +2 -0
- package/dist/BlockEditor.js.LICENSE.txt +61 -0
- package/dist/Renderer.js +2 -0
- package/dist/Renderer.js.LICENSE.txt +15 -0
- package/dist/fromHTML.js +1 -0
- package/dist/index.css +93 -0
- package/dist/index.js +2 -0
- package/dist/index.js.LICENSE.txt +69 -0
- package/dist/toHTML.js +2 -0
- package/dist/toHTML.js.LICENSE.txt +23 -0
- package/dist/toText.js +2 -0
- package/dist/toText.js.LICENSE.txt +23 -0
- package/package.json +79 -0
- package/postcss.config.js +7 -0
- package/scss/demo.scss +142 -0
- package/scss/editor.scss +394 -0
- package/scss/storybook.scss +66 -0
- package/scss/toolbar.scss +160 -0
- package/src/BlockEditor.js +252 -0
- package/src/Blocks/EmptyBlock.js +12 -0
- package/src/Blocks/EmptyWrapper.js +5 -0
- package/src/Blocks/ErrorBoundary.js +41 -0
- package/src/Blocks/LayoutBlock.js +179 -0
- package/src/Blocks/LayoutSlot.js +61 -0
- package/src/Context/StorybookContext.js +7 -0
- package/src/Nodes/Default.js +151 -0
- package/src/Nodes/Element.js +72 -0
- package/src/Nodes/Leaf.js +40 -0
- package/src/Nodes/Storybook.js +170 -0
- package/src/Nodes/StorybookDisplay.js +118 -0
- package/src/Nodes/Text.js +67 -0
- package/src/Renderer.js +41 -0
- package/src/Serializer/Html.js +43 -0
- package/src/Serializer/Serializer.js +318 -0
- package/src/Serializer/Text.js +18 -0
- package/src/Serializer/ads.js +175 -0
- package/src/Serializer/index.js +4 -0
- package/src/SidebarEditor/SidebarEditorField.js +249 -0
- package/src/SidebarEditor.css +90 -0
- package/src/SidebarEditor.js +236 -0
- package/src/Storybook.js +152 -0
- package/src/Toolbar/Align.js +65 -0
- package/src/Toolbar/Block.js +121 -0
- package/src/Toolbar/Element.js +49 -0
- package/src/Toolbar/Formats.js +60 -0
- package/src/Toolbar/Insert.js +29 -0
- package/src/Toolbar/Layout.js +333 -0
- package/src/Toolbar/Link.js +165 -0
- package/src/Toolbar/Toolbar.js +164 -0
- package/src/Tools/Margin.js +52 -0
- package/src/dev/App.js +61 -0
- package/src/dev/draftToSlate.json +3148 -0
- package/src/dev/index.css +3 -0
- package/src/dev/index.html +11 -0
- package/src/dev/index.js +5 -0
- package/src/dev/sampleValue1.json +4295 -0
- package/src/dev/sampleValue2.json +0 -0
- package/src/dev/sampleValueValid.json +411 -0
- package/src/dev/testComponents/TestStory.js +9 -0
- package/src/dev/testComponents/TestStory.stories.js +172 -0
- package/src/dev/testSampleValue.json +747 -0
- package/src/fromHTML.js +5 -0
- package/src/index.js +9 -0
- package/src/plugins/ListItem.js +49 -0
- package/src/plugins/SoftBreak.js +24 -0
- package/src/toHTML.js +7 -0
- package/src/toText.js +7 -0
- package/src/util.js +20 -0
- package/storyLoader.js +46 -0
- package/tailwind.config.js +5 -0
- package/webpack.config.build.js +53 -0
- package/webpack.config.dev.js +61 -0
- package/webpack.config.js +117 -0
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
.toolbar {
|
|
2
|
+
background-color: rgba(255, 255, 255, 0.95);
|
|
3
|
+
border-bottom: 1px solid rgba(0, 0, 0, 0.3);
|
|
4
|
+
display: none;
|
|
5
|
+
flex-direction: row;
|
|
6
|
+
justify-content: space-between;
|
|
7
|
+
padding: 5px;
|
|
8
|
+
position: absolute;
|
|
9
|
+
z-index: 500;
|
|
10
|
+
|
|
11
|
+
&.toolbar-static {
|
|
12
|
+
display: block;
|
|
13
|
+
position: fixed;
|
|
14
|
+
width: 100%;
|
|
15
|
+
max-width: 100%;
|
|
16
|
+
top: 0px;
|
|
17
|
+
left: 0px;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
&.active {
|
|
21
|
+
display: block;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
.toolbar-btns {
|
|
25
|
+
display: flex;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
.toolbar-btn {
|
|
29
|
+
border-radius: 3px;
|
|
30
|
+
color: black;
|
|
31
|
+
cursor: pointer;
|
|
32
|
+
font-weight: bold;
|
|
33
|
+
margin-right: 5px;
|
|
34
|
+
padding: 5px 7px;
|
|
35
|
+
z-index: 500;
|
|
36
|
+
position: relative;
|
|
37
|
+
display: block;
|
|
38
|
+
|
|
39
|
+
&.active {
|
|
40
|
+
background-color: rgba(0, 0, 0, 0.3);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
&:last-child {
|
|
44
|
+
margin-right: 0px;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
&:hover, &:active {
|
|
48
|
+
background-color: rgba(0, 0, 0, 0.2);
|
|
49
|
+
|
|
50
|
+
.toolbar-btn-config {
|
|
51
|
+
display: block;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
.toolbar-btn-config {
|
|
56
|
+
position: absolute;
|
|
57
|
+
top: 28px;
|
|
58
|
+
left: 0px;
|
|
59
|
+
display: none;
|
|
60
|
+
background-color: rgba(255, 255, 255, 0.95);
|
|
61
|
+
border-bottom: 1px solid rgba(0, 0, 0, 0.3);
|
|
62
|
+
padding: 5px 7px;
|
|
63
|
+
|
|
64
|
+
.toolbar-btn-config-inner {
|
|
65
|
+
display: flex;
|
|
66
|
+
flex-direction: column;
|
|
67
|
+
|
|
68
|
+
.toolbar-btn-config-row {
|
|
69
|
+
min-width: 250px;
|
|
70
|
+
padding: 5px 7px;
|
|
71
|
+
display: flex;
|
|
72
|
+
flex-direction: row;
|
|
73
|
+
|
|
74
|
+
.toolbar-form-label {
|
|
75
|
+
flex-grow: 0;
|
|
76
|
+
padding: 0 7px 0 0;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
.toolbar-form-input {
|
|
80
|
+
flex-grow: 1;
|
|
81
|
+
|
|
82
|
+
input {
|
|
83
|
+
padding: 5px;
|
|
84
|
+
border: 1px solid grey;
|
|
85
|
+
background-color: white;
|
|
86
|
+
color: black;
|
|
87
|
+
|
|
88
|
+
&[type="checkbox"] {
|
|
89
|
+
&:checked {
|
|
90
|
+
color: white;
|
|
91
|
+
background-color: black;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
.toolbar-btn {
|
|
98
|
+
display: flex;
|
|
99
|
+
flex-grow: 1;
|
|
100
|
+
border: 1px solid black;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
.toolbar-btn-expand {
|
|
108
|
+
z-index: 500;
|
|
109
|
+
display: block;
|
|
110
|
+
max-height: 28px;
|
|
111
|
+
overflow: hidden;
|
|
112
|
+
|
|
113
|
+
.toolbar-btn-expand-inner {
|
|
114
|
+
display: flex;
|
|
115
|
+
flex-direction: column;
|
|
116
|
+
|
|
117
|
+
.toolbar-btn.active {
|
|
118
|
+
order: -1;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
&:hover {
|
|
123
|
+
.toolbar-btn-expand-inner {
|
|
124
|
+
background-color: rgba(255, 255, 255, 0.95);
|
|
125
|
+
border-bottom: 1px solid rgba(0, 0, 0, 0.3);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
overflow: visible;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
.toolbar-btn-expand-right {
|
|
135
|
+
z-index: 500;
|
|
136
|
+
display: block;
|
|
137
|
+
max-height: 28px;
|
|
138
|
+
overflow: hidden;
|
|
139
|
+
|
|
140
|
+
.toolbar-btn-expand-inner {
|
|
141
|
+
left: 100%;
|
|
142
|
+
display: flex;
|
|
143
|
+
flex-direction: column;
|
|
144
|
+
|
|
145
|
+
.toolbar-btn.active {
|
|
146
|
+
order: -1;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
&:hover {
|
|
151
|
+
.toolbar-btn-expand-inner {
|
|
152
|
+
background-color: rgba(255, 255, 255, 0.95);
|
|
153
|
+
border-bottom: 1px solid rgba(0, 0, 0, 0.3);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
overflow: visible;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
}
|
|
160
|
+
}
|
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
import React, {
|
|
2
|
+
useCallback, useMemo, useState, useRef, useContext, useEffect,
|
|
3
|
+
} from 'react';
|
|
4
|
+
import {
|
|
5
|
+
Slate, Editable, withReact, ReactEditor,
|
|
6
|
+
} from 'slate-react';
|
|
7
|
+
import {
|
|
8
|
+
createEditor, Element as SlateElement, Node, Text, Transforms,
|
|
9
|
+
} from 'slate';
|
|
10
|
+
import {withHistory} from 'slate-history';
|
|
11
|
+
import {Leaf} from "./Nodes/Leaf";
|
|
12
|
+
import {Toolbar} from "./Toolbar/Toolbar";
|
|
13
|
+
import {SoftBreakPlugin} from "./plugins/SoftBreak";
|
|
14
|
+
import classNames from "classnames";
|
|
15
|
+
import {Element} from "./Nodes/Element";
|
|
16
|
+
import "../scss/editor.scss";
|
|
17
|
+
import {StorybookContext} from "./Context/StorybookContext";
|
|
18
|
+
import {ListItemPlugin} from "./plugins/ListItem";
|
|
19
|
+
import ErrorBoundary from "../src/Blocks/ErrorBoundary";
|
|
20
|
+
import SidebarEditor from './SidebarEditor';
|
|
21
|
+
|
|
22
|
+
export default function BlockEditor({
|
|
23
|
+
onChange,
|
|
24
|
+
contentfulSdk,
|
|
25
|
+
elementPropsMap,
|
|
26
|
+
value,
|
|
27
|
+
storybookComponentLoader,
|
|
28
|
+
storybookComponentDataLoader,
|
|
29
|
+
storybookStories,
|
|
30
|
+
}) {
|
|
31
|
+
const storybookContext = useContext(StorybookContext);
|
|
32
|
+
const storybookTarget = useRef(null);
|
|
33
|
+
const [isStorybookActive, setStorybookActive] = useState(false);
|
|
34
|
+
const [selectedStorybookElement, setSelectedStorybookElement] = useState(null);
|
|
35
|
+
const renderElement = useCallback((props) => {
|
|
36
|
+
return <Element
|
|
37
|
+
{...props}
|
|
38
|
+
editor={editor}
|
|
39
|
+
storybookComponentLoader={storybookComponentLoader}
|
|
40
|
+
storybookComponentDataLoader={storybookComponentDataLoader}
|
|
41
|
+
elementPropsMap={elementPropsMap}
|
|
42
|
+
onStorybookElementClick={(element, attributes) => setSelectedStorybookElement({
|
|
43
|
+
...element,
|
|
44
|
+
editorAttributes: attributes,
|
|
45
|
+
})}
|
|
46
|
+
onElementClick={() => setSelectedStorybookElement(null)}/>;
|
|
47
|
+
}, []);
|
|
48
|
+
const renderLeaf = useCallback(props => <Leaf {...props} elementPropsMap={elementPropsMap}/>, []);
|
|
49
|
+
const emptyValue = [
|
|
50
|
+
{
|
|
51
|
+
type: "paragraph",
|
|
52
|
+
children: [
|
|
53
|
+
{text: ""},
|
|
54
|
+
],
|
|
55
|
+
},
|
|
56
|
+
];
|
|
57
|
+
const resetEditor = () => {
|
|
58
|
+
if (confirm("This action will delete all data in the editor, are you sure?")) {
|
|
59
|
+
onChange(emptyValue);
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
const onSlateChange = (newValue) => {
|
|
64
|
+
if (newValue.length > 0) {
|
|
65
|
+
onChange(newValue);
|
|
66
|
+
} else {
|
|
67
|
+
onChange(emptyValue);
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
const editor = useMemo(() => {
|
|
72
|
+
const editor = withHistory(withReact(createEditor()));
|
|
73
|
+
const {
|
|
74
|
+
normalizeNode,
|
|
75
|
+
apply,
|
|
76
|
+
} = editor;
|
|
77
|
+
|
|
78
|
+
editor.isVoid = (node) => {
|
|
79
|
+
return node.type === "storybook" || node.type === "divider";
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
editor.isInline = (node) => {
|
|
83
|
+
if (node.isInline) {
|
|
84
|
+
return true;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const inlineTypes = [
|
|
88
|
+
"link",
|
|
89
|
+
];
|
|
90
|
+
|
|
91
|
+
return inlineTypes.includes(node.type);
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
editor.apply = (operation) => {
|
|
95
|
+
if (operation.type === "remove_node") {
|
|
96
|
+
if (operation.node) {
|
|
97
|
+
if (operation.node.type === "layout-slot" || operation.node.type === "storybook") {
|
|
98
|
+
// prevent slots & elements from beeing deleted by typing!
|
|
99
|
+
if (!operation.node.isDeleted) {
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
apply(operation);
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
editor.normalizeNode = entry => {
|
|
110
|
+
const [node, path] = entry;
|
|
111
|
+
|
|
112
|
+
if (SlateElement.isElement(node)) {
|
|
113
|
+
// only text and inlines in paragraph!
|
|
114
|
+
if (node.type === 'paragraph') {
|
|
115
|
+
for (const [child, childPath] of Node.children(editor, path)) {
|
|
116
|
+
if (SlateElement.isElement(child) && !editor.isInline(child)) {
|
|
117
|
+
Transforms.unwrapNodes(editor, {at: childPath});
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// only paragraph in layout-slot no text
|
|
124
|
+
if (node.type === 'layout-slot') {
|
|
125
|
+
for (const [child, childPath] of Node.children(editor, path)) {
|
|
126
|
+
if (Text.isText(child)) {
|
|
127
|
+
Transforms.wrapNodes(editor, {type: "paragraph"}, {at: childPath});
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
normalizeNode(entry);
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
return editor;
|
|
137
|
+
}, []);
|
|
138
|
+
|
|
139
|
+
const handleSidebarEditorChange = newStorybookElement => {
|
|
140
|
+
let node = ReactEditor.toSlateNode(editor, selectedStorybookElement.editorAttributes.ref.current);
|
|
141
|
+
let path = ReactEditor.findPath(editor, node);
|
|
142
|
+
|
|
143
|
+
console.log({newStorybookElement});
|
|
144
|
+
|
|
145
|
+
let newNodeProps = {
|
|
146
|
+
block: newStorybookElement.block,
|
|
147
|
+
isEmpty: false,
|
|
148
|
+
isInline: selectedStorybookElement.isInline,
|
|
149
|
+
attributes: newStorybookElement.attributes,
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
Transforms.setNodes(editor, newNodeProps, {at: path});
|
|
153
|
+
|
|
154
|
+
onSlateChange(editor.children);
|
|
155
|
+
setSelectedStorybookElement({
|
|
156
|
+
...newNodeProps,
|
|
157
|
+
editorAttributes: selectedStorybookElement.editorAttributes,
|
|
158
|
+
});
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
const handleSidebarDeleteClick = (element) => {
|
|
162
|
+
let node = ReactEditor.toSlateNode(editor, element.editorAttributes.ref.current);
|
|
163
|
+
let path = ReactEditor.findPath(editor, node);
|
|
164
|
+
|
|
165
|
+
Transforms.setNodes(editor, {isDeleted: true}, {at: path});
|
|
166
|
+
Transforms.removeNodes(editor, {at: path});
|
|
167
|
+
|
|
168
|
+
setSelectedStorybookElement(null);
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
const handleSidebarMoveClick = (element, direction) => {
|
|
172
|
+
let node = ReactEditor.toSlateNode(editor, element.editorAttributes.ref.current);
|
|
173
|
+
let path = ReactEditor.findPath(editor, node);
|
|
174
|
+
|
|
175
|
+
// Initially up
|
|
176
|
+
let to = [path[0] + 1];
|
|
177
|
+
|
|
178
|
+
if (direction === "down") {
|
|
179
|
+
to = [path[0] - 1];
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
try {
|
|
183
|
+
Transforms.moveNodes(editor, {
|
|
184
|
+
from: [path[0]],
|
|
185
|
+
match: node => {
|
|
186
|
+
return node.type === "storybook";
|
|
187
|
+
},
|
|
188
|
+
to,
|
|
189
|
+
mode: "highest",
|
|
190
|
+
voids: false,
|
|
191
|
+
});
|
|
192
|
+
} catch (e) {
|
|
193
|
+
console.error(e);
|
|
194
|
+
}
|
|
195
|
+
};
|
|
196
|
+
|
|
197
|
+
return (
|
|
198
|
+
<div className={classNames({
|
|
199
|
+
"block-editor-wrapper": true,
|
|
200
|
+
"storybook-active": isStorybookActive,
|
|
201
|
+
})}>
|
|
202
|
+
<div className="storybook-target" ref={storybookTarget}/>
|
|
203
|
+
<div className="slate-editor">
|
|
204
|
+
<StorybookContext.Provider value={{
|
|
205
|
+
...storybookContext,
|
|
206
|
+
target: storybookTarget,
|
|
207
|
+
contentfulSdk: contentfulSdk,
|
|
208
|
+
setActive: setStorybookActive,
|
|
209
|
+
isActive: isStorybookActive,
|
|
210
|
+
}}>
|
|
211
|
+
<Slate
|
|
212
|
+
editor={editor}
|
|
213
|
+
value={value}
|
|
214
|
+
onChange={onSlateChange}
|
|
215
|
+
>
|
|
216
|
+
<Toolbar hover={false}/>
|
|
217
|
+
<ErrorBoundary
|
|
218
|
+
name="editor"
|
|
219
|
+
fallback={(
|
|
220
|
+
<>
|
|
221
|
+
Invalid Content! Call Support!
|
|
222
|
+
</>
|
|
223
|
+
)}>
|
|
224
|
+
<Editable
|
|
225
|
+
renderLeaf={renderLeaf}
|
|
226
|
+
renderElement={renderElement}
|
|
227
|
+
onKeyDown={(event) => {
|
|
228
|
+
if (event.code === "Backspace") {
|
|
229
|
+
// event.preventDefault();
|
|
230
|
+
// return false;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
if (!ListItemPlugin.onKeyDown(event, editor)) {
|
|
234
|
+
SoftBreakPlugin.onKeyDown(event, editor);
|
|
235
|
+
}
|
|
236
|
+
}}
|
|
237
|
+
/>
|
|
238
|
+
</ErrorBoundary>
|
|
239
|
+
</Slate>
|
|
240
|
+
</StorybookContext.Provider>
|
|
241
|
+
</div>
|
|
242
|
+
{!!selectedStorybookElement && <SidebarEditor
|
|
243
|
+
sdk={contentfulSdk}
|
|
244
|
+
storybookElement={selectedStorybookElement}
|
|
245
|
+
onChange={handleSidebarEditorChange}
|
|
246
|
+
storybookStories={storybookStories}
|
|
247
|
+
onClose={() => setSelectedStorybookElement(null)}
|
|
248
|
+
onDelete={element => handleSidebarDeleteClick(element)}
|
|
249
|
+
onMove={(element, direction) => handleSidebarMoveClick(element, direction)} />}
|
|
250
|
+
</div>
|
|
251
|
+
);
|
|
252
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
|
|
3
|
+
export const EmptyBlock = () => {
|
|
4
|
+
if (process.env.NEXT_PUBLIC_VERCEL_ENV === "production") {
|
|
5
|
+
// hide this in production
|
|
6
|
+
return null;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
return <span className="editor-empty-element" style={{minHeight: 200, display: "inline-block"}}>
|
|
10
|
+
Unbekanntes Element
|
|
11
|
+
</span>;
|
|
12
|
+
};
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
|
|
3
|
+
export default class ErrorBoundary extends React.Component {
|
|
4
|
+
constructor(props) {
|
|
5
|
+
super(props);
|
|
6
|
+
this.state = {
|
|
7
|
+
hasError: false,
|
|
8
|
+
};
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
static getDerivedStateFromError(error) {
|
|
12
|
+
return {hasError: true};
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
componentDidCatch(error, errorInfo) {
|
|
16
|
+
const groupName = `Error in ${this.props.name}`;
|
|
17
|
+
console.groupCollapsed(groupName);
|
|
18
|
+
console.error("Error", error);
|
|
19
|
+
console.error("Info", errorInfo);
|
|
20
|
+
console.groupEnd(groupName);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
render() {
|
|
24
|
+
if (this.state.hasError) {
|
|
25
|
+
|
|
26
|
+
if (this.props.fallback) {
|
|
27
|
+
return this.props.fallback;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
if (this.props.isEditor) {
|
|
31
|
+
return <div className="storybook-element-error">
|
|
32
|
+
<p>Error in Element {this.props.name}</p>
|
|
33
|
+
</div>;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return this.props.children;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import {FontAwesomeIcon} from "@fortawesome/react-fontawesome";
|
|
3
|
+
import {
|
|
4
|
+
faArrowDown, faArrowUp, faBorderAll, faBorderNone, faCompress, faTimes,
|
|
5
|
+
} from "@fortawesome/free-solid-svg-icons";
|
|
6
|
+
import {Transforms} from "slate";
|
|
7
|
+
import {faExpand} from "@fortawesome/free-solid-svg-icons/faExpand";
|
|
8
|
+
import {ReactEditor} from "slate-react";
|
|
9
|
+
import classNames from "classnames";
|
|
10
|
+
import {ToolMargin} from "../Tools/Margin";
|
|
11
|
+
|
|
12
|
+
export const LayoutBlock = ({
|
|
13
|
+
children,
|
|
14
|
+
element,
|
|
15
|
+
editor,
|
|
16
|
+
attributes,
|
|
17
|
+
classNameArticle = "article-width",
|
|
18
|
+
classNameSite = "site-width",
|
|
19
|
+
onElementClick = () => {},
|
|
20
|
+
...props
|
|
21
|
+
}) => {
|
|
22
|
+
function onRemoveClick(e) {
|
|
23
|
+
let node = ReactEditor.toSlateNode(editor, attributes.ref.current);
|
|
24
|
+
let fromPath = ReactEditor.findPath(editor, node);
|
|
25
|
+
|
|
26
|
+
if (!fromPath) {
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
try {
|
|
31
|
+
Transforms.removeNodes(editor, {at: fromPath});
|
|
32
|
+
} catch (e) {
|
|
33
|
+
console.error(e);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const moveUp = () => {
|
|
38
|
+
let node = ReactEditor.toSlateNode(editor, attributes.ref.current);
|
|
39
|
+
let path = ReactEditor.findPath(editor, node);
|
|
40
|
+
|
|
41
|
+
if (!path[0]) {
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
try {
|
|
46
|
+
Transforms.moveNodes(editor, {
|
|
47
|
+
from: [path[0]],
|
|
48
|
+
match: node => {
|
|
49
|
+
return node.type === "layout";
|
|
50
|
+
},
|
|
51
|
+
to: [path[0] - 1],
|
|
52
|
+
mode: "highest",
|
|
53
|
+
voids: false,
|
|
54
|
+
});
|
|
55
|
+
} catch (e) {
|
|
56
|
+
console.error(e);
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
const moveDown = () => {
|
|
61
|
+
let node = ReactEditor.toSlateNode(editor, attributes.ref.current);
|
|
62
|
+
let path = ReactEditor.findPath(editor, node);
|
|
63
|
+
|
|
64
|
+
try {
|
|
65
|
+
Transforms.moveNodes(editor, {
|
|
66
|
+
from: [path[0]],
|
|
67
|
+
match: node => {
|
|
68
|
+
return node.type === "layout";
|
|
69
|
+
},
|
|
70
|
+
to: [path[0] + 1],
|
|
71
|
+
mode: "highest",
|
|
72
|
+
});
|
|
73
|
+
} catch (e) {
|
|
74
|
+
console.error(e);
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
const switchSpacing = (e) => {
|
|
79
|
+
let node = ReactEditor.toSlateNode(editor, attributes.ref.current);
|
|
80
|
+
let fromPath = ReactEditor.findPath(editor, node);
|
|
81
|
+
|
|
82
|
+
if (!fromPath) {
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
let spacing = !(element?.attributes?.spacing);
|
|
87
|
+
|
|
88
|
+
Transforms.setNodes(editor, {
|
|
89
|
+
type: "layout",
|
|
90
|
+
attributes: {
|
|
91
|
+
...(element.attributes || {}),
|
|
92
|
+
spacing: spacing,
|
|
93
|
+
},
|
|
94
|
+
}, {at: fromPath});
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
const switchSize = (e) => {
|
|
98
|
+
let node = ReactEditor.toSlateNode(editor, attributes.ref.current);
|
|
99
|
+
let fromPath = ReactEditor.findPath(editor, node);
|
|
100
|
+
|
|
101
|
+
if (!fromPath) {
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
let width = element?.attributes?.width || "article";
|
|
106
|
+
|
|
107
|
+
if (width === "article") {
|
|
108
|
+
width = "full";
|
|
109
|
+
} else {
|
|
110
|
+
width = "article";
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
Transforms.setNodes(editor, {
|
|
114
|
+
type: "layout",
|
|
115
|
+
attributes: {
|
|
116
|
+
...(element.attributes || {}),
|
|
117
|
+
width: width,
|
|
118
|
+
},
|
|
119
|
+
}, {at: fromPath});
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
const onMarginChange = (value) => {
|
|
123
|
+
let node = ReactEditor.toSlateNode(editor, attributes.ref.current);
|
|
124
|
+
let path = ReactEditor.findPath(editor, node);
|
|
125
|
+
|
|
126
|
+
Transforms.setNodes(editor, {
|
|
127
|
+
type: "layout",
|
|
128
|
+
attributes: {
|
|
129
|
+
...(element.attributes || {}),
|
|
130
|
+
margin: value,
|
|
131
|
+
},
|
|
132
|
+
}, {at: path});
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
return <div className="options-wrapper" onClick={() => onElementClick(element)}>
|
|
136
|
+
<div className="options-container">
|
|
137
|
+
<span className="options-container-option options-container-option-remove" onClick={onRemoveClick}>
|
|
138
|
+
<FontAwesomeIcon icon={faTimes} size="lg"/>
|
|
139
|
+
</span>
|
|
140
|
+
{/*
|
|
141
|
+
<span className="options-container-option options-container-option-move-up" onClick={moveUp}>
|
|
142
|
+
<FontAwesomeIcon icon={faArrowUp} size="lg"/>
|
|
143
|
+
</span>
|
|
144
|
+
<span className="options-container-option options-container-option-move-down" onClick={moveDown}>
|
|
145
|
+
<FontAwesomeIcon icon={faArrowDown} size="lg"/>
|
|
146
|
+
</span>
|
|
147
|
+
*/}
|
|
148
|
+
<span className="options-container-option options-container-option-expand" onClick={switchSize}>
|
|
149
|
+
{element?.attributes?.width === "full" ? (
|
|
150
|
+
<FontAwesomeIcon icon={faCompress} size="lg"/>
|
|
151
|
+
) : (
|
|
152
|
+
<FontAwesomeIcon icon={faExpand} size="lg"/>
|
|
153
|
+
)}
|
|
154
|
+
</span>
|
|
155
|
+
<span className="options-container-option options-container-option-expand" onClick={switchSpacing}>
|
|
156
|
+
{element?.attributes?.spacing === false ? (
|
|
157
|
+
<FontAwesomeIcon icon={faBorderNone} size="lg"/>
|
|
158
|
+
) : (
|
|
159
|
+
<FontAwesomeIcon icon={faBorderAll} size="lg"/>
|
|
160
|
+
)}
|
|
161
|
+
</span>
|
|
162
|
+
<ToolMargin margin={element?.attributes?.margin} onChange={onMarginChange}/>
|
|
163
|
+
</div>
|
|
164
|
+
<div
|
|
165
|
+
className={`layout-block layout-grid layout-grid-cols-${children.length} ` + classNames({
|
|
166
|
+
[classNameArticle + " article-width"]: element?.attributes?.width === "article" || !element?.attributes?.width,
|
|
167
|
+
[classNameSite + " site-width"]: element?.attributes?.width === "full",
|
|
168
|
+
"space-x-4": element?.attributes?.spacing,
|
|
169
|
+
"editor-mt-large": element?.attributes?.margin?.top,
|
|
170
|
+
"editor-mr-large": element?.attributes?.margin?.right,
|
|
171
|
+
"editor-mb-large": element?.attributes?.margin?.bottom,
|
|
172
|
+
"editor-ml-large": element?.attributes?.margin?.left,
|
|
173
|
+
})}
|
|
174
|
+
{...attributes}
|
|
175
|
+
>
|
|
176
|
+
{children}
|
|
177
|
+
</div>
|
|
178
|
+
</div>;
|
|
179
|
+
};
|