Repository navigation
Expand file tree
/
Copy pathsaver.js
More file actions
180 lines (112 loc) · 4.04 KB
/
Copy pathsaver.js
File metadata and controls
180 lines (112 loc) · 4.04 KB
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
/**
* Codex Editor Saver
*
* @author Codex Team
* @version 1.0.2
*/
let editor = codex.editor;
module.exports = (function (saver) {
/**
* Saves blocks
* @private
*/
saver.saveBlocks = function () {
/** Save html content of redactor to memory */
editor.state.html = editor.nodes.redactor.innerHTML;
/** Empty jsonOutput state */
editor.state.jsonOutput = [];
Promise.resolve()
.then(function () {
return editor.nodes.redactor.childNodes;
})
/** Making a sequence from separate blocks */
.then(editor.saver.makeQueue)
.then(function () {
// editor.nodes.textarea.innerHTML = editor.state.html;
})
.catch( function (error) {
editor.core.log(error);
});
};
saver.makeQueue = function (blocks) {
var queue = Promise.resolve();
for(var index = 0; index < blocks.length; index++) {
/** Add node to sequence at specified index */
editor.saver.getBlockData(queue, blocks, index);
}
};
/** Gets every block and makes From Data */
saver.getBlockData = function (queue, blocks, index) {
queue.then(function () {
return editor.saver.getNodeAsync(blocks, index);
})
.then(editor.saver.makeFormDataFromBlocks);
};
/**
* Asynchronously returns block data from blocksList by index
* @return Promise to node
*/
saver.getNodeAsync = function (blocksList, index) {
return Promise.resolve().then(function () {
return blocksList[index];
});
};
saver.makeFormDataFromBlocks = function (block) {
var pluginName = block.dataset.tool,
id = block.dataset.id;
/** Check for plugin existance */
if (!editor.tools[pluginName]) {
throw Error(`Plugin «${pluginName}» not found`);
}
/** Check for plugin having render method */
if (typeof editor.tools[pluginName].save != 'function') {
throw Error(`Plugin «${pluginName}» must have save method`);
}
/** Result saver */
var blockContent = block.childNodes[0],
pluginsContent = blockContent.childNodes[0],
savedData = editor.tools[pluginName].save(pluginsContent),
output;
output = {
id : id,
type: pluginName,
data: savedData
};
if (editor.tools[pluginName].validate) {
var result = editor.tools[pluginName].validate(savedData);
/**
* Do not allow invalid data
*/
if (!result)
return;
}
/** Marks Blocks that will be in main page */
output.cover = block.classList.contains(editor.ui.className.BLOCK_IN_FEED_MODE);
editor.state.jsonOutput.push(output);
};
saver.saveComments = function () {
var fields = editor.nodes.commentsSidebar.querySelectorAll('.ce-comments-field');
for (var i = 0 ; i < fields.length; i++) {
var comments = fields[i].querySelectorAll('.ce-comment'),
commentsData = [];
for (var j = 0; j < comments.length; j++) {
var text = comments[j].querySelector('.ce-comment__text'),
time = comments[j].querySelector('.ce-comment__time');
if (!text) continue;
var comment = {
edited : comments[j].dataset.edited,
text : text.textContent,
time : time.textContent
};
commentsData.push(comment);
}
if (!commentsData.length) continue;
var fieldData = {
blockId : fields[i].dataset.blockId,
comments : commentsData
};
editor.state.comments.push(fieldData);
}
};
return saver;
})({});