-
Notifications
You must be signed in to change notification settings - Fork 603
/
actions.js
424 lines (352 loc) · 12.5 KB
/
actions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
// @flow
import invariant from "invariant";
import dotProp from "dot-prop-immutable";
import gql from "graphql-tag";
import { isEqual, pick, debounce, cloneDeep, merge as _merge } from "lodash";
import {
createAction,
addMiddleware,
addReducer,
addHigherOrderReducer
} from "webiny-app-page-builder/editor/redux";
import { getPlugin, getPlugins } from "webiny-plugins";
import {
getPage,
getElementWithChildren,
getParentElementWithChildren
} from "webiny-app-page-builder/editor/selectors";
import { updateChildPaths } from "webiny-app-page-builder/editor/utils";
import undoable from "./history";
export const DRAG_START = `Drag start`;
export const DRAG_END = `Drag end`;
export const ELEMENT_CREATED = `Element created`;
export const ELEMENT_DROPPED = `Element dropped`;
export const TOGGLE_PLUGIN = `Toggle plugin`;
export const DEACTIVATE_PLUGIN = `Deactivate plugin`;
export const FOCUS_SLATE_EDITOR = `Focus slate editor`;
export const BLUR_SLATE_EDITOR = `Blur slate editor`;
export const HIGHLIGHT_ELEMENT = `Highlight element`;
export const ACTIVATE_ELEMENT = `Activate element`;
export const DEACTIVATE_ELEMENT = `Deactivate element`;
export const UPDATE_ELEMENT = `Update element`;
export const DELETE_ELEMENT = `Delete element`;
export const FLATTEN_ELEMENTS = `Flatten elements`;
export const SETUP_EDITOR = `Setup editor`;
export const UPDATE_REVISION = `Update revision`;
export const SAVING_REVISION = `Save revision`;
export const START_SAVING = `Started saving`;
export const FINISH_SAVING = `Finished saving`;
/***************** HISTORY REDUCER *****************/
const horStatePath = "page.content";
addHigherOrderReducer(
[
UPDATE_ELEMENT,
DELETE_ELEMENT,
ELEMENT_DROPPED,
SETUP_EDITOR,
"@@redux-undo/UNDO",
"@@redux-undo/REDO",
"@@redux-undo/INIT"
],
horStatePath,
() => {
return undoable(
(state = [], action, { statePath, reducer }) => {
// Get original reducer state path
const relativeStatePath =
statePath !== horStatePath ? statePath.replace(horStatePath + ".", "") : null;
const relativeStateSlice = relativeStatePath
? dotProp.get(state, relativeStatePath)
: state;
// Execute original reducer
const newState = reducer(relativeStateSlice, action);
// Assign new data to HOR state
return relativeStatePath
? dotProp.set(state, relativeStatePath, newState)
: newState;
},
{
initTypes: ["@@redux-undo/INIT"],
ignoreInitialState: true,
filter: action => {
if (action.payload && action.payload.history === false) {
return false;
}
return true;
}
}
);
}
);
addReducer(
["@@redux-undo/UNDO", "@@redux-undo/REDO", "@@redux-undo/INIT"],
"page.content",
state => state
);
/***************** EDITOR ACTIONS *****************/
addReducer([SETUP_EDITOR], null, (state, action) => {
return { ...state, ...action.payload };
});
export const togglePlugin = createAction(TOGGLE_PLUGIN);
addReducer([TOGGLE_PLUGIN], "ui.plugins", (state, action) => {
const { name, params, closeOtherInGroup = false } = action.payload;
const plugin = getPlugin(name);
if (!plugin) {
return state;
}
let typePlugins = dotProp.get(state, plugin.type);
if (!Array.isArray(typePlugins)) {
typePlugins = [];
}
const alreadyActive = typePlugins.findIndex(pl => pl.name === plugin.name);
if (alreadyActive > -1) {
typePlugins = dotProp.delete(typePlugins, alreadyActive);
} else {
if (closeOtherInGroup) {
typePlugins = [{ name, params }];
} else {
typePlugins.push({ name, params });
}
}
return dotProp.set(state, `${plugin.type}`, typePlugins);
});
export const deactivatePlugin = createAction(DEACTIVATE_PLUGIN);
addReducer([DEACTIVATE_PLUGIN], "ui.plugins", (state, action) => {
const { name } = action.payload;
const plugin = getPlugin(name);
if (!plugin) {
return state;
}
let typePlugins = dotProp.get(state, plugin.type);
if (!Array.isArray(typePlugins)) {
typePlugins = [];
}
const alreadyActive = typePlugins.findIndex(pl => pl.name === plugin.name);
if (alreadyActive > -1) {
typePlugins = dotProp.delete(typePlugins, alreadyActive);
}
return dotProp.set(state, `${plugin.type}`, typePlugins);
});
export const highlightElement = createAction(HIGHLIGHT_ELEMENT, { log: false });
addReducer([HIGHLIGHT_ELEMENT], "ui.highlightElement", (state, action) => {
return action.payload.element ? action.payload.element : null;
});
export const activateElement = createAction(ACTIVATE_ELEMENT);
addReducer([ACTIVATE_ELEMENT], "ui.activeElement", (state, action) => {
return action.payload.element;
});
export const deactivateElement = createAction(DEACTIVATE_ELEMENT);
addReducer([DEACTIVATE_ELEMENT], "ui.activeElement", () => null);
export const focusSlateEditor = createAction(FOCUS_SLATE_EDITOR);
addReducer([FOCUS_SLATE_EDITOR], "ui.slateFocused", () => true);
export const blurSlateEditor = createAction(BLUR_SLATE_EDITOR);
addReducer([BLUR_SLATE_EDITOR], "ui.slateFocused", () => false);
export const dragStart = createAction(DRAG_START);
addReducer([DRAG_START], "ui.dragging", () => true);
export const dragEnd = createAction(DRAG_END);
addReducer([DRAG_END], "ui.dragging", () => false);
export const elementCreated = createAction(ELEMENT_CREATED);
export const updateElement = createAction(UPDATE_ELEMENT);
addReducer(
[UPDATE_ELEMENT],
action => {
const { element } = action.payload;
if (element.type === "document") {
return "page.content";
}
// .slice(2) removes `0.` from the beginning of the generated path
return "page.content." + action.payload.element.path.replace(/\./g, ".elements.").slice(2);
},
(state, action) => {
const { element, merge = false } = action.payload;
if (element.elements && typeof element.elements[0] === "string") {
delete element["elements"];
}
updateChildPaths(element);
if (!merge) {
return { ...state, ...element };
}
return _merge({}, state, element);
}
);
export const deleteElement = createAction(DELETE_ELEMENT);
addMiddleware([DELETE_ELEMENT], ({ store, next, action }) => {
next(action);
store.dispatch(deactivateElement());
const state = store.getState();
let { element } = action.payload;
let parent = getParentElementWithChildren(state, element.id);
// Remove child from parent
// $FlowFixMe
if (!parent) {
return;
}
const index = parent.elements.findIndex(el => el.id === element.id);
parent = dotProp.delete(parent, "elements." + index);
store.dispatch(updateElement({ element: parent }));
// Execute `onChildDeleted` if defined
const plugin = getPlugin(parent.type);
if (!plugin) {
return;
}
if (typeof plugin.onChildDeleted === "function") {
plugin.onChildDeleted({ element: parent, child: element });
}
});
export const dropElement = createAction(ELEMENT_DROPPED);
addMiddleware([ELEMENT_DROPPED], ({ store, next, action }) => {
next(action);
const state = store.getState();
const target = getElementWithChildren(state, action.payload.target.id);
if (!target) {
return;
}
const plugin = getPlugins("pb-page-element").find(pl => pl.elementType === target.type);
if (!plugin) {
return;
}
invariant(
plugin.onReceived,
"To accept drops, element plugin must implement `onReceived` function"
);
let { source } = action.payload;
if (source.path) {
source = getElementWithChildren(state, source.id);
}
const targetPlugin = getPlugins("pb-page-element").find(pl => pl.elementType === target.type);
if (!targetPlugin) {
return;
}
targetPlugin.onReceived({
source,
target,
position: action.payload.target.position
});
});
export const updateRevision = createAction(UPDATE_REVISION);
addMiddleware([UPDATE_REVISION], ({ store, next, action }) => {
next(action);
if (action.payload.history === false) {
return;
}
const { onFinish } = action.meta;
store.dispatch(saveRevision(null, { onFinish }));
});
addReducer([UPDATE_REVISION], "page", (state, action) => {
return { ...state, ...action.payload };
});
// Flatten page content
const flattenContent = el => {
let els = {};
el.elements =
Array.isArray(el.elements) &&
el.elements.map(child => {
els = { ...els, ...flattenContent(child) };
return child.id;
});
els[el.id] = el;
return els;
};
addReducer([FLATTEN_ELEMENTS], "elements", (state, action) => {
return action.payload;
});
addMiddleware(
[UPDATE_ELEMENT, DELETE_ELEMENT, "@@redux-undo/UNDO", "@@redux-undo/REDO", "@@redux-undo/INIT"],
({ store, next, action }) => {
const result = next(action);
const state = store.getState();
if (state.page.content) {
const content = dotProp.get(state, "page.content.present") || null;
if (!content) {
return result;
}
const elements = flattenContent(cloneDeep(content));
store.dispatch({ type: FLATTEN_ELEMENTS, payload: elements, meta: { log: true } });
}
return result;
}
);
/************************* SAVE REVISION *************************/
let lastSavedRevision = null;
const dataChanged = revision => {
if (!lastSavedRevision) {
return true;
}
const { content, ...other } = revision;
const { content: lastContent, ...lastOther } = lastSavedRevision;
return !isEqual(content, lastContent) || !isEqual(other, lastOther);
};
export const saveRevision = createAction(SAVING_REVISION);
let debouncedSave = null;
addMiddleware(
[UPDATE_REVISION, UPDATE_ELEMENT, DELETE_ELEMENT, "@@redux-undo/UNDO", "@@redux-undo/REDO"],
({ store, next, action }) => {
next(action);
const { onFinish } = action.meta || {};
if (action.type === UPDATE_ELEMENT && action.payload.history === false) {
return;
}
const page = getPage(store.getState());
if (page.locked) {
return;
}
if (debouncedSave) {
debouncedSave.cancel();
}
debouncedSave = debounce(() => store.dispatch(saveRevision(null, { onFinish })), 1000);
debouncedSave();
}
);
const startSaving = { type: START_SAVING, payload: { progress: true } };
const finishSaving = { type: FINISH_SAVING, payload: { progress: false } };
addReducer([START_SAVING, FINISH_SAVING], "ui.saving", (state, action) => {
return action.payload.progress;
});
addMiddleware([SAVING_REVISION], ({ store, next, action }) => {
next(action);
const data: Object = getPage(store.getState());
if (data.locked) {
return;
}
// Construct page payload
const revision = pick(data, ["title", "snippet", "url", "settings"]);
revision.content = data.content.present;
revision.category = data.category.id;
// Check if API call is necessary
if (!dataChanged(revision)) {
return;
}
lastSavedRevision = revision;
const updateRevision = gql`
mutation UpdateRevision($id: ID!, $data: PbUpdatePageInput!) {
pageBuilder {
updateRevision(id: $id, data: $data) {
data {
id
content
title
published
savedOn
}
error {
code
message
data
}
}
}
}
`;
store.dispatch(startSaving);
action.meta.client
.mutate({ mutation: updateRevision, variables: { id: data.id, data: revision } })
.then(data => {
store.dispatch(finishSaving);
action.meta.onFinish && action.meta.onFinish();
return data;
})
.catch(err => {
store.dispatch(finishSaving);
console.log(err); // eslint-disable-line
});
});