Skip to content

Commit eb0a59c

Browse files
committed
Merge data overrides with actual block data when inserting a block
1 parent f2fe90b commit eb0a59c

File tree

10 files changed

+108
-94
lines changed

10 files changed

+108
-94
lines changed

src/components/modules/api/blocks.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -228,15 +228,15 @@ export default class BlocksAPI extends Module {
228228
* @param {boolean?} needToFocus - flag to focus inserted Block
229229
* @param replace - pass true to replace the Block existed under passed index
230230
*/
231-
public insert = (
231+
public insert = async (
232232
type: string = this.config.defaultBlock,
233233
data: BlockToolData = {},
234234
config: ToolConfig = {},
235235
index?: number,
236236
needToFocus?: boolean,
237237
replace?: boolean
238-
): BlockAPIInterface => {
239-
const insertedBlock = this.Editor.BlockManager.insert({
238+
): Promise<BlockAPIInterface> => {
239+
const insertedBlock = await this.Editor.BlockManager.insert({
240240
tool: type,
241241
data,
242242
index,
@@ -267,7 +267,7 @@ export default class BlocksAPI extends Module {
267267
* @param id - id of the block to update
268268
* @param data - the new data
269269
*/
270-
public update = (id: string, data: BlockToolData): void => {
270+
public update = async (id: string, data: BlockToolData): Promise<void> => {
271271
const { BlockManager } = this.Editor;
272272
const block = BlockManager.getBlockById(id);
273273

@@ -278,8 +278,7 @@ export default class BlocksAPI extends Module {
278278
}
279279

280280
const blockIndex = BlockManager.getBlockIndex(block);
281-
282-
BlockManager.insert({
281+
await BlockManager.insert({
283282
id: block.id,
284283
tool: block.name,
285284
data,

src/components/modules/blockEvents.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -191,13 +191,13 @@ export default class BlockEvents extends Module {
191191
return;
192192
}
193193

194-
BlockSelection.copySelectedBlocks(event).then(() => {
194+
BlockSelection.copySelectedBlocks(event).then(async () => {
195195
const selectionPositionIndex = BlockManager.removeSelectedBlocks();
196196

197197
/**
198198
* Insert default block in place of removed ones
199199
*/
200-
const insertedBlock = BlockManager.insertDefaultBlockAtIndex(selectionPositionIndex, true);
200+
const insertedBlock = await BlockManager.insertDefaultBlockAtIndex(selectionPositionIndex, true);
201201

202202
Caret.setToBlock(insertedBlock, Caret.positions.START);
203203

@@ -211,7 +211,7 @@ export default class BlockEvents extends Module {
211211
*
212212
* @param {KeyboardEvent} event - keydown
213213
*/
214-
private enter(event: KeyboardEvent): void {
214+
private async enter(event: KeyboardEvent): Promise<void> {
215215
const { BlockManager, UI } = this.Editor;
216216
const currentBlock = BlockManager.currentBlock;
217217

@@ -250,7 +250,7 @@ export default class BlockEvents extends Module {
250250
* Split the Current Block into two blocks
251251
* Renew local current node after split
252252
*/
253-
newCurrent = this.Editor.BlockManager.split();
253+
newCurrent = await this.Editor.BlockManager.split();
254254
}
255255

256256
this.Editor.Caret.setToBlock(newCurrent);

src/components/modules/blockManager.ts

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { BlockToolData, PasteEvent } from '../../../types';
1515
import { BlockTuneData } from '../../../types/block-tunes/block-tune-data';
1616
import BlockAPI from '../block/api';
1717
import { BlockMutationType } from '../../../types/events/block/mutation-type';
18+
import BlockTool from '../tools/block';
1819

1920
/**
2021
* @typedef {BlockManager} BlockManager
@@ -232,23 +233,23 @@ export default class BlockManager extends Module {
232233
*
233234
* @returns {Block}
234235
*/
235-
public composeBlock({
236+
public async composeBlock({
236237
tool: name,
237238
data = {},
238239
id = undefined,
239240
tunes: tunesData = {},
240-
}: {tool: string; id?: string; data?: BlockToolData; tunes?: {[name: string]: BlockTuneData}}): Block {
241+
}: { tool: string; id?: string; data?: BlockToolData; tunes?: { [name: string]: BlockTuneData } }): Promise<Block> {
241242
const readOnly = this.Editor.ReadOnly.isEnabled;
242243
const tool = this.Editor.Tools.blockTools.get(name);
244+
const actialData = await this.composeBlockData(tool, data);
243245
const block = new Block({
244246
id,
245-
data,
247+
data: actialData,
246248
tool,
247249
api: this.Editor.API,
248250
readOnly,
249251
tunesData,
250252
});
251-
252253
if (!readOnly) {
253254
this.bindBlockEvents(block);
254255
}
@@ -269,7 +270,7 @@ export default class BlockManager extends Module {
269270
*
270271
* @returns {Block}
271272
*/
272-
public insert({
273+
public async insert({
273274
id = undefined,
274275
tool = this.config.defaultBlock,
275276
data = {},
@@ -284,15 +285,14 @@ export default class BlockManager extends Module {
284285
index?: number;
285286
needToFocus?: boolean;
286287
replace?: boolean;
287-
tunes?: {[name: string]: BlockTuneData};
288-
} = {}): Block {
288+
tunes?: { [name: string]: BlockTuneData };
289+
} = {}): Promise<Block> {
289290
let newIndex = index;
290291

291292
if (newIndex === undefined) {
292293
newIndex = this.currentBlockIndex + (replace ? 0 : 1);
293294
}
294-
295-
const block = this.composeBlock({
295+
const block = await this.composeBlock({
296296
id,
297297
tool,
298298
data,
@@ -339,7 +339,7 @@ export default class BlockManager extends Module {
339339
public replace({
340340
tool = this.config.defaultBlock,
341341
data = {},
342-
}): Block {
342+
}): Promise<Block> {
343343
return this.insert({
344344
tool,
345345
data,
@@ -355,12 +355,12 @@ export default class BlockManager extends Module {
355355
* @param {PasteEvent} pasteEvent - pasted data
356356
* @param {boolean} replace - should replace current block
357357
*/
358-
public paste(
358+
public async paste(
359359
toolName: string,
360360
pasteEvent: PasteEvent,
361361
replace = false
362-
): Block {
363-
const block = this.insert({
362+
): Promise<Block> {
363+
const block = await this.insert({
364364
tool: toolName,
365365
replace,
366366
});
@@ -384,8 +384,8 @@ export default class BlockManager extends Module {
384384
*
385385
* @returns {Block} inserted Block
386386
*/
387-
public insertDefaultBlockAtIndex(index: number, needToFocus = false): Block {
388-
const block = this.composeBlock({ tool: this.config.defaultBlock });
387+
public async insertDefaultBlockAtIndex(index: number, needToFocus = false): Promise<Block> {
388+
const block = await this.composeBlock({ tool: this.config.defaultBlock });
389389

390390
this._blocks[index] = block;
391391

@@ -410,7 +410,7 @@ export default class BlockManager extends Module {
410410
*
411411
* @returns {Block}
412412
*/
413-
public insertAtEnd(): Block {
413+
public insertAtEnd(): Promise<Block> {
414414
/**
415415
* Define new value for current block index
416416
*/
@@ -534,7 +534,7 @@ export default class BlockManager extends Module {
534534
*
535535
* @returns {Block}
536536
*/
537-
public split(): Block {
537+
public split(): Promise<Block> {
538538
const extractedFragment = this.Editor.Caret.extractFragmentFromCaretPosition();
539539
const wrapper = $.make('div');
540540

@@ -881,4 +881,24 @@ export default class BlockManager extends Module {
881881

882882
return block;
883883
}
884+
885+
/**
886+
* Retrieves default block data by creating fake block.
887+
* Merges retrieved data with specified data object.
888+
*
889+
* @param tool - block's tool
890+
* @param dataOverrides - object containing overrides for default block data
891+
*/
892+
private async composeBlockData(tool: BlockTool, dataOverrides = {}): Promise<BlockToolData> {
893+
const block = new Block({
894+
tool,
895+
api: this.Editor.API,
896+
readOnly: true,
897+
data: {},
898+
tunesData: {},
899+
});
900+
const blockData = await block.data;
901+
902+
return Object.assign(blockData, dataOverrides);
903+
}
884904
}

src/components/modules/caret.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -336,9 +336,9 @@ export default class Caret extends Module {
336336
if (lastBlock.tool.isDefault && lastBlock.isEmpty) {
337337
this.setToBlock(lastBlock);
338338
} else {
339-
const newBlock = this.Editor.BlockManager.insertAtEnd();
340-
341-
this.setToBlock(newBlock);
339+
this.Editor.BlockManager.insertAtEnd().then(newBlock => {
340+
this.setToBlock(newBlock);
341+
});
342342
}
343343
}
344344

@@ -390,7 +390,7 @@ export default class Caret extends Module {
390390
*
391391
* @returns {boolean}
392392
*/
393-
public navigateNext(): boolean {
393+
public async navigateNext(): Promise<boolean> {
394394
const { BlockManager } = this.Editor;
395395
const { currentBlock, nextContentfulBlock } = BlockManager;
396396
const { nextInput } = currentBlock;
@@ -417,7 +417,7 @@ export default class Caret extends Module {
417417
* If there is no nextBlock, but currentBlock is not default,
418418
* insert new default block at the end and navigate to it
419419
*/
420-
nextBlock = BlockManager.insertAtEnd();
420+
nextBlock = await BlockManager.insertAtEnd();
421421
}
422422

423423
if (isAtEnd) {

src/components/modules/paste.ts

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66
PasteEvent,
77
PasteEventDetail
88
} from '../../../types';
9-
import Block from '../block';
109
import { SavedData } from '../../../types/data-formats';
1110
import { clean, sanitizeBlocks } from '../utils/sanitizer';
1211
import BlockTool from '../tools/block';
@@ -112,12 +111,12 @@ export default class Paste extends Module {
112111
/**
113112
* Tags` substitutions parameters
114113
*/
115-
private toolsTags: {[tag: string]: TagSubstitute} = {};
114+
private toolsTags: { [tag: string]: TagSubstitute } = {};
116115

117116
/**
118117
* Store tags to substitute by tool name
119118
*/
120-
private tagsByTool: {[tools: string]: string[]} = {};
119+
private tagsByTool: { [tools: string]: string[] } = {};
121120

122121
/** Patterns` substitutions parameters */
123122
private toolsPatterns: PatternSubstitute[] = [];
@@ -186,7 +185,7 @@ export default class Paste extends Module {
186185
this.insertEditorJSData(JSON.parse(editorJSData));
187186

188187
return;
189-
} catch (e) {} // Do nothing and continue execution as usual if error appears
188+
} catch (e) { } // Do nothing and continue execution as usual if error appears
190189
}
191190

192191
/**
@@ -449,7 +448,7 @@ export default class Paste extends Module {
449448
private async processFiles(items: FileList): Promise<void> {
450449
const { BlockManager } = this.Editor;
451450

452-
let dataToInsert: {type: string; event: PasteEvent}[];
451+
let dataToInsert: { type: string; event: PasteEvent }[];
453452

454453
dataToInsert = await Promise.all(
455454
Array
@@ -473,7 +472,7 @@ export default class Paste extends Module {
473472
*
474473
* @param {File} file - file to process
475474
*/
476-
private async processFile(file: File): Promise<{event: PasteEvent; type: string}> {
475+
private async processFile(file: File): Promise<{ event: PasteEvent; type: string }> {
477476
const extension = _.getFileExtension(file);
478477

479478
const foundConfig = Object
@@ -576,7 +575,7 @@ export default class Paste extends Module {
576575
* @returns {PasteData[]}
577576
*/
578577
private processPlain(plain: string): PasteData[] {
579-
const { defaultBlock } = this.config as {defaultBlock: string};
578+
const { defaultBlock } = this.config as { defaultBlock: string };
580579

581580
if (!plain) {
582581
return [];
@@ -652,9 +651,9 @@ export default class Paste extends Module {
652651
BlockManager.currentBlock.tool.isDefault &&
653652
BlockManager.currentBlock.isEmpty;
654653

655-
const insertedBlock = BlockManager.paste(blockData.tool, blockData.event, needToReplaceCurrentBlock);
656-
657-
Caret.setToBlock(insertedBlock, Caret.positions.END);
654+
BlockManager.paste(blockData.tool, blockData.event, needToReplaceCurrentBlock).then(insertedBlock => {
655+
Caret.setToBlock(insertedBlock, Caret.positions.END);
656+
});
658657

659658
return;
660659
}
@@ -681,7 +680,7 @@ export default class Paste extends Module {
681680
*
682681
* @returns {Promise<{event: PasteEvent, tool: string}>}
683682
*/
684-
private async processPattern(text: string): Promise<{event: PasteEvent; tool: string}> {
683+
private async processPattern(text: string): Promise<{ event: PasteEvent; tool: string }> {
685684
const pattern = this.toolsPatterns.find((substitute) => {
686685
const execResult = substitute.pattern.exec(text);
687686

@@ -718,18 +717,16 @@ export default class Paste extends Module {
718717
private insertBlock(data: PasteData, canReplaceCurrentBlock = false): void {
719718
const { BlockManager, Caret } = this.Editor;
720719
const { currentBlock } = BlockManager;
721-
let block: Block;
722720

723721
if (canReplaceCurrentBlock && currentBlock && currentBlock.isEmpty) {
724-
block = BlockManager.paste(data.tool, data.event, true);
725-
Caret.setToBlock(block, Caret.positions.END);
726-
727-
return;
722+
BlockManager.paste(data.tool, data.event, true).then(block => {
723+
Caret.setToBlock(block, Caret.positions.END);
724+
});
725+
} else {
726+
BlockManager.paste(data.tool, data.event).then(block => {
727+
Caret.setToBlock(block, Caret.positions.END);
728+
});
728729
}
729-
730-
block = BlockManager.paste(data.tool, data.event);
731-
732-
Caret.setToBlock(block, Caret.positions.END);
733730
}
734731

735732
/**
@@ -754,13 +751,13 @@ export default class Paste extends Module {
754751
needToReplaceCurrentBlock = isCurrentBlockDefault && BlockManager.currentBlock.isEmpty;
755752
}
756753

757-
const block = BlockManager.insert({
754+
BlockManager.insert({
758755
tool,
759756
data,
760757
replace: needToReplaceCurrentBlock,
758+
}).then(block => {
759+
Caret.setToBlock(block, Caret.positions.END);
761760
});
762-
763-
Caret.setToBlock(block, Caret.positions.END);
764761
});
765762
}
766763

src/components/modules/renderer.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export default class Renderer extends Module {
7777

7878
if (Tools.available.has(tool)) {
7979
try {
80-
BlockManager.insert({
80+
await BlockManager.insert({
8181
id,
8282
tool,
8383
data,
@@ -105,12 +105,11 @@ export default class Renderer extends Module {
105105
stubData.title = toolboxTitle || stubData.title;
106106
}
107107

108-
const stub = BlockManager.insert({
108+
const stub = await BlockManager.insert({
109109
id,
110110
tool: Tools.stubTool,
111111
data: stubData,
112-
});
113-
112+
})
114113
stub.stretched = true;
115114

116115
_.log(`Tool «${tool}» is not found. Check 'tools' property at your initial Editor.js config.`, 'warn');

0 commit comments

Comments
 (0)