Skip to content

Commit 2987fcc

Browse files
committed
Added test to check the current block id in the nested editor
1 parent 8a6da99 commit 2987fcc

File tree

2 files changed

+188
-0
lines changed

2 files changed

+188
-0
lines changed
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
import type {
2+
API,
3+
EditorConfig,
4+
BaseTool,
5+
BlockAPI,
6+
BlockToolConstructorOptions,
7+
OutputData
8+
} from '../../../../types';
9+
import EditorJS from '../../../../types';
10+
11+
export interface NestedEditorToolConfig {
12+
editorLibrary: typeof EditorJS,
13+
editorTools?: EditorConfig["tools"],
14+
editorTunes?: EditorConfig["tunes"],
15+
}
16+
17+
export interface NestedEditorToolData {
18+
nestedEditor: OutputData,
19+
}
20+
21+
/**
22+
* Simplified Header for testing
23+
*/
24+
export class NestedEditorTool implements BaseTool {
25+
private api: API;
26+
private readOnly: boolean;
27+
private block: BlockAPI;
28+
29+
private config: NestedEditorToolConfig;
30+
private _data: NestedEditorToolData;
31+
32+
private nestedEditor?: EditorJS;
33+
34+
/**
35+
* This flag tells core that current tool supports the read-only mode
36+
* @link https://editorjs.io/tools-api#isreadonlysupported
37+
*/
38+
static get isReadOnlySupported(): boolean {
39+
return true;
40+
}
41+
42+
/**
43+
* With this option, Editor.js won't handle Enter keydowns
44+
* @link https://editorjs.io/tools-api#enablelinebreaks
45+
*/
46+
static get enableLineBreaks(): boolean {
47+
return true;
48+
}
49+
50+
/**
51+
*
52+
* @param options - constructor options
53+
*/
54+
constructor({ data, api, config, readOnly, block }: BlockToolConstructorOptions) {
55+
this.api = api;
56+
this._data = data;
57+
this.config = {
58+
editorLibrary: config.editorLibrary,
59+
editorTools: config.editorTools || {},
60+
editorTunes: config.editorTunes || [],
61+
};
62+
this.readOnly = readOnly;
63+
this.block = block;
64+
}
65+
66+
/**
67+
* Return Tool's view
68+
*/
69+
public render(): HTMLElement {
70+
const rootNode = document.createElement('div');
71+
rootNode.classList.add(this.api.styles.block);
72+
73+
const editorNode = document.createElement('div');
74+
editorNode.style.border = '1px solid'
75+
editorNode.style.padding = '12px 20px'
76+
rootNode.appendChild(editorNode);
77+
78+
this.nestedEditor = new this.config.editorLibrary({
79+
holder: editorNode,
80+
data: this.data.nestedEditor,
81+
tools: this.config.editorTools,
82+
tunes: this.config.editorTunes,
83+
minHeight: 150,
84+
readOnly: this.readOnly,
85+
onChange: () => {
86+
this.block.dispatchChange()
87+
}
88+
});
89+
90+
return rootNode;
91+
}
92+
93+
/**
94+
* Return Tool data
95+
*/
96+
get data(): NestedEditorToolData {
97+
return this._data;
98+
}
99+
100+
/**
101+
* Stores all Tool's data
102+
*/
103+
set data(data: NestedEditorToolData) {
104+
this._data.nestedEditor = data.nestedEditor || {};
105+
if (this.nestedEditor) {
106+
this.nestedEditor.blocks.render(this.data.nestedEditor);
107+
}
108+
}
109+
110+
/**
111+
* Extracts Block data from the UI
112+
*/
113+
public async save(): Promise<NestedEditorToolData> {
114+
let savedData: OutputData = { blocks: [] }
115+
if (this.nestedEditor) {
116+
savedData = await this.nestedEditor.save();
117+
}
118+
119+
return {
120+
nestedEditor: savedData
121+
};
122+
}
123+
}

test/cypress/tests/modules/Ui.cy.ts

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { createEditorWithTextBlocks } from '../../support/utils/createEditorWithTextBlocks';
22
import type EditorJS from '../../../../types/index';
3+
import { NestedEditorTool } from '../../fixtures/tools/NestedEditorTool';
34

45
describe('Ui module', function () {
56
describe('documentKeydown', function () {
@@ -139,5 +140,69 @@ describe('Ui module', function () {
139140
expect(currentBlockIndex).to.eq(1);
140141
});
141142
});
143+
144+
it('click on block of nested editor should also set the block of parent editor as current', function () {
145+
cy.window()
146+
.then((window) => {
147+
const editorJsClass = window.EditorJS;
148+
149+
cy.createEditor({
150+
tools: {
151+
nestedEditor: {
152+
class: NestedEditorTool,
153+
config: {
154+
editorLibrary: editorJsClass
155+
}
156+
},
157+
},
158+
data: {
159+
blocks: [
160+
{
161+
id: 'block1',
162+
type: 'paragraph',
163+
data: {
164+
text: 'First block of parent editor',
165+
},
166+
},
167+
{
168+
id: 'block2',
169+
type: 'paragraph',
170+
data: {
171+
text: 'Second block of parent editor',
172+
},
173+
},
174+
{
175+
id: 'block3',
176+
type: 'nestedEditor',
177+
data: {
178+
nestedEditor: {
179+
blocks: [
180+
{
181+
id: 'nestedBlock1',
182+
type: 'paragraph',
183+
data: {
184+
text: 'First block of nested editor',
185+
},
186+
}
187+
]
188+
}
189+
}
190+
}
191+
],
192+
},
193+
}).as('editorInstance');
194+
});
195+
196+
cy.get('[data-id=nestedBlock1]')
197+
.find('.ce-paragraph')
198+
.click();
199+
200+
cy.get<EditorJS>('@editorInstance')
201+
.then(async (editor) => {
202+
const currentBlockIndex = editor.blocks.getCurrentBlockIndex();
203+
204+
expect(currentBlockIndex).to.eq(2); // 3 block in numbering from 0
205+
});
206+
});
142207
});
143208
});

0 commit comments

Comments
 (0)