| 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 | 1×
1×
1×
1×
1×
1516×
1×
355×
355×
355×
355×
355×
1×
161×
161×
161×
2×
159×
159×
159×
159×
1×
173×
173×
1×
172×
171×
171×
171×
171×
1×
16×
16×
16×
1×
15×
15×
15×
15×
15×
15×
1×
56×
56×
56×
1×
55×
55×
2×
53×
53×
53×
1×
72×
72×
72×
3×
69×
69×
69×
28×
26×
41×
41×
22×
21×
19×
69×
1×
12×
1×
11×
1×
833×
833×
1×
598×
598×
598×
598×
598×
598×
3×
598×
1×
598×
598×
509×
509×
119×
509×
443×
66×
509×
117×
117×
89×
61×
28×
598×
1×
54×
54×
54×
38×
16×
16×
54×
1×
7×
8×
1×
355×
355×
355×
316×
316×
316×
355×
1×
58×
58×
74×
74×
74×
74×
73×
73×
58×
1×
56×
56×
73×
73×
73×
73×
73×
56×
1×
53×
53×
1×
52×
52×
52×
52×
52×
68×
68×
68×
1×
196×
196×
1×
195×
195×
195×
1×
99×
99×
99×
20×
99×
5×
94×
94×
148×
148×
94×
94×
4×
5×
94×
1×
5×
5×
5×
5×
9×
9×
9×
5×
7×
7×
15×
15×
15×
15×
15×
7×
5×
1×
172×
172×
172×
172×
1137×
156×
981×
148×
833×
171×
1×
156×
30×
126×
126×
171×
171×
5×
251×
166×
1×
165×
126×
126×
171×
170×
5×
120×
1×
165×
165×
165×
641×
159×
4×
159×
160×
155×
4×
155×
5×
482×
165×
1×
155×
155×
137×
2×
135×
155×
1×
148×
148×
208×
148×
1×
40850×
1×
44×
1×
1×
| var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
define(["require", "exports", "../../models/index", "../../common/utils/block", "../../common/utils/common", "../../common/utils/transform", "../../common/constant", "./block-factory"], function (require, exports, index_1, block_1, common_1, transform_1, constants, block_factory_1) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var BlockService = (function () {
function BlockService(blocks) {
this.blocks = blocks;
}
BlockService.prototype.addBlock = function (options) {
var targetBlockModel = options.targetBlockId ? block_1.getBlockModelById(options.targetBlockId, this.blocks) : null;
var insertArray = targetBlockModel ? block_1.getParentBlocksArray(targetBlockModel.id, this.blocks) : this.blocks;
var indexToInsert = this.getIndexToAdjust(insertArray, targetBlockModel, options.isAfter);
insertArray.splice(indexToInsert, 0, options.block);
return options.block;
};
BlockService.prototype.removeBlock = function (_a) {
var blockId = _a.blockId;
var blockModel = block_1.getBlockModelById(blockId, this.blocks);
if (!blockModel) {
return { removedBlock: null, blockIndex: -1 };
}
var containerArray = block_1.getParentBlocksArray(blockId, this.blocks);
var blockIndex = block_1.getBlockIndexById(blockId, this.blocks);
var removedBlock = containerArray.splice(blockIndex, 1)[0];
return { removedBlock: removedBlock, blockIndex: blockIndex };
};
BlockService.prototype.updateBlock = function (blockId, properties) {
var blockModel = block_1.getBlockModelById(blockId, this.blocks);
if (!blockModel) {
return null;
}
var updatedBlock = this.mergeBlockProperties(blockModel, properties);
var containerArray = block_1.getParentBlocksArray(blockId, this.blocks);
var blockIndex = block_1.getBlockIndexById(blockId, containerArray);
containerArray.splice(blockIndex, 1, updatedBlock);
return updatedBlock;
};
BlockService.prototype.duplicateBlock = function (_a) {
var blockId = _a.blockId;
var blockModel = block_1.getBlockModelById(blockId, this.blocks);
if (!blockModel) {
return null;
}
var containerArray = block_1.getParentBlocksArray(blockId, this.blocks);
var blockIndex = block_1.getBlockIndexById(blockId, containerArray);
var blockToClone = containerArray[blockIndex];
var clonedBlock = common_1.decoupleReference(transform_1.sanitizeBlock(blockToClone));
var duplicatedBlockModel = this.generateNewIdsForBlock(clonedBlock);
return duplicatedBlockModel;
};
BlockService.prototype.moveBlocks = function (options) {
var blockIds = options.blockIds, toBlockId = options.toBlockId, _a = options.isMovingUp, isMovingUp = _a === void 0 ? false : _a;
var toBlockModel = block_1.getBlockModelById(toBlockId, this.blocks);
if (!toBlockModel) {
return [];
}
var fromEntries = this.gatherBlocksInfoForMove(blockIds);
if (fromEntries.length === 0) {
return [];
}
var movedBlocks = this.removeBlocksForMove(fromEntries);
this.insertBlocksAtTarget(movedBlocks, toBlockId, isMovingUp);
return movedBlocks;
};
BlockService.prototype.applyIndentation = function (options) {
var blockId = options.blockId, shouldDecrease = options.shouldDecrease;
var blockModel = block_1.getBlockModelById(blockId, this.blocks);
if (!blockModel) {
return null;
}
var blockIndex = block_1.getBlockIndexById(blockId, this.blocks);
var parentBlock = block_1.getBlockModelById(blockModel.parentId, this.blocks);
if (shouldDecrease) {
if (blockModel.indent > 0) {
blockModel.indent--;
}
}
else {
var adjacentBlockModel = parentBlock
? parentBlock.properties.children[blockIndex - 1]
: this.blocks[blockIndex - 1];
if (adjacentBlockModel) {
if (blockModel.indent <= adjacentBlockModel.indent) {
blockModel.indent++;
}
}
else {
blockModel.indent++;
}
}
return blockModel;
};
BlockService.prototype.applyLineBreak = function (insertOffset, contentModel) {
if (insertOffset < 0 || !contentModel) {
return;
}
contentModel.content =
contentModel.content.substring(0, insertOffset) +
'\n' +
contentModel.content.substring(insertOffset);
};
BlockService.prototype.updateContent = function (blockId, content) {
var block = block_1.getBlockModelById(blockId, this.blocks);
block.content = content;
};
BlockService.prototype.toggleContentStyles = function (content, format, force, formatIntent, value) {
var updatedContent = __assign({}, content);
var booleanStyles = ['bold', 'italic', 'underline', 'strikethrough', 'superscript', 'subscript', 'uppercase', 'lowercase'];
var togglePairs = ['superscript', 'subscript', 'uppercase', 'lowercase'];
var isBooleanStyle = booleanStyles.indexOf(format) !== -1;
var isTogglePair = togglePairs.indexOf(format) !== -1;
if (!updatedContent.properties) {
updatedContent.properties = { styles: {} };
}
if (!updatedContent.properties.styles) {
updatedContent.properties.styles = {};
}
var styles = updatedContent.properties.styles;
if (isBooleanStyle) {
var newValue = !styles[format];
if (force) {
newValue = formatIntent;
}
if (newValue) {
styles[format] = true;
}
else {
delete styles[format];
}
if (isTogglePair) {
var oppositeFormat = common_1.getInverseStyle(format);
delete styles[oppositeFormat];
}
}
else {
if (value) {
styles[format] = value;
}
else {
delete styles[format];
}
}
return updatedContent;
};
BlockService.prototype.applyLinkFormatting = function (content, linkData) {
var updatedContent;
var textContent = linkData.text ? linkData.text : content.content;
if (!linkData.shouldRemoveLink) {
updatedContent = block_factory_1.BlockFactory.createLinkContent({ content: textContent }, __assign({}, content.properties, { url: linkData.url }));
}
else {
delete content.properties.url;
updatedContent = block_factory_1.BlockFactory.createTextContent({
content: textContent
}, content.properties);
}
return updatedContent;
};
BlockService.prototype.assignParentIdToBlocks = function (blocks, parentId) {
blocks.forEach(function (block) {
block.parentId = parentId;
});
};
BlockService.prototype.getIndexToAdjust = function (insertionArray, targetBlock, isAfter) {
if (isAfter === void 0) { isAfter = true; }
var insertIndex = insertionArray.length;
if (targetBlock) {
var afterBlockIndex = block_1.getBlockIndexById(targetBlock.id, insertionArray);
Eif (afterBlockIndex !== -1) {
insertIndex = afterBlockIndex + (isAfter ? 1 : 0);
}
}
return insertIndex;
};
BlockService.prototype.gatherBlocksInfoForMove = function (fromBlockIds) {
var entries = [];
for (var _i = 0, fromBlockIds_1 = fromBlockIds; _i < fromBlockIds_1.length; _i++) {
var fromBlockId = fromBlockIds_1[_i];
var blockModel = block_1.getBlockModelById(fromBlockId, this.blocks);
var index = block_1.getBlockIndexById(fromBlockId, this.blocks);
if (index >= 0 && blockModel) {
var parent_1 = block_1.getBlockModelById(blockModel.parentId, this.blocks);
entries.push({ blockId: fromBlockId, model: blockModel, index: index, parent: parent_1 });
}
}
return entries.sort(function (a, b) { return b.index - a.index; });
};
BlockService.prototype.removeBlocksForMove = function (fromEntries) {
var allFromModels = [];
for (var _i = 0, fromEntries_1 = fromEntries; _i < fromEntries_1.length; _i++) {
var entry = fromEntries_1[_i];
var blockId = entry.blockId, index = entry.index, parent_2 = entry.parent;
var containerArray = block_1.getParentBlocksArray(blockId, this.blocks);
var moved = containerArray.splice(index, 1)[0];
allFromModels.push({ blockId: blockId, model: moved, parent: parent_2, index: index });
}
return allFromModels;
};
BlockService.prototype.insertBlocksAtTarget = function (movedBlocks, toBlockId, isMovingUp) {
var toBlockIndex = block_1.getBlockIndexById(toBlockId, this.blocks);
if (toBlockIndex < 0) {
return null;
}
var insertToArray = block_1.getParentBlocksArray(toBlockId, this.blocks);
var toBlockModel = block_1.getBlockModelById(toBlockId, insertToArray);
var toParentBlock = block_1.getBlockModelById(toBlockModel.parentId, this.blocks);
var insertIndex = block_1.getBlockIndexById(toBlockId, insertToArray) + (isMovingUp ? 0 : 1);
for (var _i = 0, movedBlocks_1 = movedBlocks; _i < movedBlocks_1.length; _i++) {
var entry = movedBlocks_1[_i];
entry.model.parentId = toParentBlock ? toParentBlock.id : '';
insertToArray.splice(insertIndex, 0, entry.model);
}
};
BlockService.prototype.replaceBlock = function (originalBlockId, newBlock) {
var originalBlock = block_1.getBlockModelById(originalBlockId, this.blocks);
if (!originalBlock) {
return;
}
var insertToArray = block_1.getParentBlocksArray(originalBlockId, this.blocks);
var indexToReplace = block_1.getBlockIndexById(originalBlock.id, insertToArray);
insertToArray.splice(indexToReplace, 1, newBlock);
};
BlockService.prototype.generateNewIdsForBlock = function (block, parentId) {
var _this = this;
block.id = common_1.generateUniqueId(constants.BLOCK_ID_PREFIX);
if (parentId) {
block.parentId = parentId;
}
if (block.blockType === index_1.BlockType.Table) {
return this.generateNewIdsForTableBlock(block);
}
Eif (block.content) {
block.content = block.content.map(function (content) {
content.id = common_1.generateUniqueId(constants.CONTENT_ID_PREFIX);
return content;
});
}
var children = (block.properties && block.properties.children)
? block.properties.children
: [];
if (children && children.length > 0) {
children = children.map(function (child) {
return _this.generateNewIdsForBlock(child, block.id);
});
}
return block;
};
BlockService.prototype.generateNewIdsForTableBlock = function (tableBlock) {
var _this = this;
var props = tableBlock.properties;
var columnIdMap = new Map();
props.columns = props.columns.map(function (col) {
var newId = common_1.generateUniqueId('col_');
columnIdMap.set(col.id, newId);
return __assign({}, col, { id: newId });
});
props.rows = props.rows.map(function (row) {
var newRowId = common_1.generateUniqueId('row_');
var newCells = row.cells.map(function (cell) {
var newCellId = common_1.generateUniqueId('cell_');
var newColumnId = columnIdMap.get(cell.columnId) || cell.columnId;
var newBlocks = cell.blocks.map(function (innerBlock) {
return _this.generateNewIdsForBlock(innerBlock, newCellId);
});
return __assign({}, cell, { id: newCellId, columnId: newColumnId, blocks: newBlocks });
});
return __assign({}, row, { id: newRowId, cells: newCells });
});
return tableBlock;
};
BlockService.prototype.mergeBlockProperties = function (block, properties) {
var _this = this;
var clonedBlock = common_1.decoupleReference(transform_1.sanitizeBlock(block));
var mergedBlock = __assign({}, clonedBlock);
Object.keys(properties).forEach(function (key) {
if (key === 'content' && properties.content) {
mergedBlock.content = _this.mergeContentArray(clonedBlock.content, properties.content);
}
else if ((key === 'properties' && properties.properties)) {
mergedBlock[key] = _this.mergePrimitiveTypes(clonedBlock[key], properties[key]);
}
else {
mergedBlock[key] = properties[key];
}
});
return mergedBlock;
};
BlockService.prototype.mergeContentArray = function (existingContent, newContent) {
if (newContent.length === 0) {
return [];
}
var mergedContent = existingContent.slice();
var _loop_1 = function (i) {
var newItem = newContent[i];
if (!newItem.id) {
return { value: [block_factory_1.BlockFactory.createContentFromPartial(newItem)] };
}
var index = mergedContent.findIndex(function (item) { return item.id === newItem.id; });
if (index === -1) {
throw new Error("Content with ID " + newItem.id + " not found");
}
mergedContent[index] = this_1.mergeContentModel(mergedContent[index], newItem);
};
var this_1 = this;
for (var i = 0; i < newContent.length; i++) {
var state_1 = _loop_1(i);
if (typeof state_1 === "object")
return state_1.value;
}
return mergedContent;
};
BlockService.prototype.mergeContentModel = function (existing, updates) {
var _this = this;
var merged = __assign({}, existing);
Object.keys(updates).forEach(function (key) {
if (key === 'properties' && updates.properties) {
if (!merged.properties) {
merged.properties = {};
}
Object.keys(updates.properties).forEach(function (prop) {
if (prop === 'styles' && updates.properties.styles) {
if (!merged.properties.styles) {
merged.properties.styles = {};
}
merged.properties.styles = _this.mergeStyleModel(merged.properties.styles, updates.properties.styles);
}
else {
merged[key] = updates[key];
}
});
}
else {
merged[key] = updates[key];
}
});
return merged;
};
BlockService.prototype.mergeStyleModel = function (existing, updates) {
var merged = __assign({}, existing);
Object.keys(updates).forEach(function (style) {
if (!updates[style]) {
delete merged[style];
}
else {
merged[style] = updates[style];
}
});
return merged;
};
BlockService.prototype.mergePrimitiveTypes = function (existing, updates) {
var merged = __assign({}, existing);
Object.keys(updates).forEach(function (key) {
merged[key] = updates[key];
});
return merged;
};
BlockService.prototype.getBlocks = function () {
return this.blocks;
};
BlockService.prototype.setBlocks = function (blocks) {
this.blocks = blocks;
};
return BlockService;
}());
exports.BlockService = BlockService;
});
|