Skip to content

Commit a47ffcc

Browse files
authored
renderFromHTML API method (codex-team#654)
* renderFromHTML API method * Fix case when currentBlock is undefined * Clear editor before render * Handle single block processing * Update paragraph
1 parent 6bd857d commit a47ffcc

7 files changed

Lines changed: 77 additions & 45 deletions

File tree

‎dist/editor.js‎

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎docs/CHANGELOG.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
### 2.12.0
4+
5+
- `New` *API* - new `blocks` API method `renderFromHTML`
6+
37
### 2.11.11
48

59
- `New` — Add ability to pass configuration for internal Tools

‎docs/api.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ export interface API {
2222

2323
Methods that working with Blocks
2424

25+
`render(data)` - render passed JSON data
26+
27+
`renderFromHTML(data)` - parse and render passed HTML string (*not for production use*)
28+
2529
`swap(fromIndex, toIndex)` - swaps two Blocks by their positions
2630

2731
`delete(blockIndex?: Number)` - deletes Block with passed index

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@editorjs/editorjs",
3-
"version": "2.11.11",
3+
"version": "2.12.0",
44
"description": "Editor.js — Native JS, based on API and Open Source",
55
"main": "dist/editor.js",
66
"types": "./types/index.d.ts",

‎src/components/modules/api/blocks.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export default class BlocksAPI extends Module {
1818
return {
1919
clear: () => this.clear(),
2020
render: (data: OutputData) => this.render(data),
21+
renderFromHTML: (data: string) => this.renderFromHTML(data),
2122
delete: () => this.delete(),
2223
swap: (fromIndex: number, toIndex: number) => this.swap(fromIndex, toIndex),
2324
getBlockByIndex: (index: number) => this.getBlockByIndex(index),
@@ -113,6 +114,16 @@ export default class BlocksAPI extends Module {
113114
return this.Editor.Renderer.render(data.blocks);
114115
}
115116

117+
/**
118+
* Render passed HTML string
119+
* @param {string} data
120+
* @return {Promise<void>}
121+
*/
122+
public renderFromHTML(data: string): Promise<void> {
123+
this.Editor.BlockManager.clear();
124+
return this.Editor.Paste.processText(data, true);
125+
}
126+
116127
/**
117128
* Stretch Block's content
118129
* @param {number} index

‎src/components/modules/paste.ts‎

Lines changed: 45 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,41 @@ export default class Paste extends Module {
180180
}
181181
}
182182

183+
/**
184+
* Process pasted text and divide them into Blocks
185+
*
186+
* @param {string} data - text to process. Can be HTML or plain.
187+
* @param {boolean} isHTML - if passed string is HTML, this parameter should be true
188+
*/
189+
public async processText(data: string, isHTML: boolean = false) {
190+
const {Caret, BlockManager, Tools} = this.Editor;
191+
const dataToInsert = isHTML ? this.processHTML(data) : this.processPlain(data);
192+
193+
if (!dataToInsert.length) {
194+
return;
195+
}
196+
197+
if (dataToInsert.length === 1) {
198+
if (!dataToInsert[0].isBlock) {
199+
this.processInlinePaste(dataToInsert.pop());
200+
} else {
201+
this.processSingleBlock(dataToInsert.pop());
202+
}
203+
return;
204+
}
205+
206+
const isCurrentBlockInitial = BlockManager.currentBlock && Tools.isInitial(BlockManager.currentBlock.tool);
207+
const needToReplaceCurrentBlock = isCurrentBlockInitial && BlockManager.currentBlock.isEmpty;
208+
209+
await Promise.all(dataToInsert.map(
210+
async (content, i) => await this.insertBlock(content, i === 0 && needToReplaceCurrentBlock),
211+
));
212+
213+
if (BlockManager.currentBlock) {
214+
Caret.setToBlock(BlockManager.currentBlock, Caret.positions.END);
215+
}
216+
}
217+
183218
/**
184219
* Set onPaste callback handler
185220
*/
@@ -424,39 +459,6 @@ export default class Paste extends Module {
424459
};
425460
}
426461

427-
/**
428-
* Process pasted text and divide them into Blocks
429-
*
430-
* @param {string} data - text to process. Can be HTML or plain.
431-
* @param {boolean} isHTML - if passed string is HTML, this parameter should be true
432-
*/
433-
private async processText(data: string, isHTML: boolean = false) {
434-
const {Caret, BlockManager, Tools} = this.Editor;
435-
const dataToInsert = isHTML ? this.processHTML(data) : this.processPlain(data);
436-
437-
if (!dataToInsert.length) {
438-
return;
439-
}
440-
441-
if (dataToInsert.length === 1) {
442-
if (!dataToInsert[0].isBlock) {
443-
this.processInlinePaste(dataToInsert.pop());
444-
} else {
445-
this.processSingleBlock(dataToInsert.pop());
446-
}
447-
return;
448-
}
449-
450-
const isCurrentBlockInitial = Tools.isInitial(BlockManager.currentBlock.tool);
451-
const needToReplaceCurrentBlock = isCurrentBlockInitial && BlockManager.currentBlock.isEmpty;
452-
453-
await Promise.all(dataToInsert.map(
454-
async (content, i) => await this.insertBlock(content, i === 0 && needToReplaceCurrentBlock),
455-
));
456-
457-
Caret.setToBlock(BlockManager.currentBlock, Caret.positions.END);
458-
}
459-
460462
/**
461463
* Split HTML string to blocks and return it as array of Block data
462464
*
@@ -558,8 +560,12 @@ export default class Paste extends Module {
558560
/**
559561
* If pasted tool isn`t equal current Block or if pasted content contains block elements, insert it as new Block
560562
*/
561-
if (dataToInsert.tool !== currentBlock.name || !$.containsOnlyInlineElements(dataToInsert.content.innerHTML)) {
562-
this.insertBlock(dataToInsert, Tools.isInitial(currentBlock.tool) && currentBlock.isEmpty);
563+
if (
564+
!currentBlock ||
565+
dataToInsert.tool !== currentBlock.name ||
566+
!$.containsOnlyInlineElements(dataToInsert.content.innerHTML)
567+
) {
568+
this.insertBlock(dataToInsert, currentBlock && Tools.isInitial(currentBlock.tool) && currentBlock.isEmpty);
563569
return;
564570
}
565571

@@ -578,7 +584,7 @@ export default class Paste extends Module {
578584
const {BlockManager, Caret, Sanitizer, Tools} = this.Editor;
579585
const {content, tool} = dataToInsert;
580586

581-
const currentBlockIsInitial = Tools.isInitial(BlockManager.currentBlock.tool);
587+
const currentBlockIsInitial = BlockManager.currentBlock && Tools.isInitial(BlockManager.currentBlock.tool);
582588

583589
if (currentBlockIsInitial && content.textContent.length < Paste.PATTERN_PROCESSING_MAX_LENGTH) {
584590
const blockData = await this.processPattern(content.textContent);
@@ -597,10 +603,10 @@ export default class Paste extends Module {
597603
}
598604
}
599605

600-
const currentToolSanitizeConfig = Sanitizer.getInlineToolsConfig(BlockManager.currentBlock.name);
601-
602606
/** If there is no pattern substitute - insert string as it is */
603-
if (BlockManager.currentBlock.currentInput) {
607+
if (BlockManager.currentBlock && BlockManager.currentBlock.currentInput) {
608+
const currentToolSanitizeConfig = Sanitizer.getInlineToolsConfig(BlockManager.currentBlock.name);
609+
604610
document.execCommand('insertHTML', false, Sanitizer.clean(content.innerHTML, currentToolSanitizeConfig));
605611
} else {
606612
this.insertBlock(dataToInsert);

‎types/api/blocks.d.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ export interface Blocks {
1616
*/
1717
render(data: OutputData): Promise<void>;
1818

19+
/**
20+
* Render passed HTML string
21+
* @param {string} data
22+
* @return {Promise<void>}
23+
*/
24+
renderFromHTML(data: string): Promise<void>;
25+
1926
/**
2027
* Removes current Block
2128
*/

0 commit comments

Comments
 (0)