This repository has been archived by the owner on Jun 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathheading.js
259 lines (206 loc) · 7.48 KB
/
heading.js
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
/**
* @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.
*/
/* globals document */
import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
import Heading from '../src/heading';
import HeadingEngine from '../src/headingengine';
import DropdownView from '@ckeditor/ckeditor5-ui/src/dropdown/dropdownview';
import { add } from '@ckeditor/ckeditor5-utils/src/translation-service';
import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
import { setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
add( 'pl', {
'Choose heading': 'Wybierz nagłówek',
'Paragraph': 'Akapit',
'Heading': 'Nagłówek',
'Heading 1': 'Nagłówek 1',
'Heading 2': 'Nagłówek 2',
} );
testUtils.createSinonSandbox();
describe( 'Heading', () => {
let editor, editorElement, dropdown;
beforeEach( () => {
editorElement = document.createElement( 'div' );
document.body.appendChild( editorElement );
return ClassicTestEditor
.create( editorElement, {
plugins: [ Heading ],
toolbar: [ 'heading' ]
} )
.then( newEditor => {
editor = newEditor;
dropdown = editor.ui.componentFactory.create( 'headings' );
// Set data so the commands will be enabled.
setData( editor.document, '<paragraph>f{}oo</paragraph>' );
} );
} );
afterEach( () => {
editorElement.remove();
return editor.destroy();
} );
it( 'should be loaded', () => {
expect( editor.plugins.get( Heading ) ).to.be.instanceOf( Heading );
} );
it( 'should load HeadingEngine', () => {
expect( editor.plugins.get( HeadingEngine ) ).to.be.instanceOf( HeadingEngine );
} );
describe( 'init()', () => {
it( 'should register options feature component', () => {
const dropdown = editor.ui.componentFactory.create( 'headings' );
expect( dropdown ).to.be.instanceOf( DropdownView );
expect( dropdown.buttonView.isEnabled ).to.be.true;
expect( dropdown.buttonView.isOn ).to.be.undefined;
expect( dropdown.buttonView.label ).to.equal( 'Paragraph' );
expect( dropdown.buttonView.tooltip ).to.equal( 'Heading' );
} );
it( 'should execute format command on model execute event', () => {
const executeSpy = testUtils.sinon.spy( editor, 'execute' );
const dropdown = editor.ui.componentFactory.create( 'headings' );
dropdown.commandName = 'paragraph';
dropdown.fire( 'execute' );
sinon.assert.calledOnce( executeSpy );
sinon.assert.calledWithExactly( executeSpy, 'paragraph' );
} );
it( 'should focus view after command execution', () => {
const focusSpy = testUtils.sinon.spy( editor.editing.view, 'focus' );
const dropdown = editor.ui.componentFactory.create( 'headings' );
dropdown.commandName = 'paragraph';
dropdown.fire( 'execute' );
sinon.assert.calledOnce( focusSpy );
} );
it( 'should add custom CSS class to dropdown', () => {
const dropdown = editor.ui.componentFactory.create( 'headings' );
expect( dropdown.element.classList.contains( 'ck-heading-dropdown' ) ).to.be.true;
} );
describe( 'model to command binding', () => {
let commands;
beforeEach( () => {
commands = {};
editor.config.get( 'heading.options' ).forEach( ( { modelElement } ) => {
commands[ modelElement ] = editor.commands.get( modelElement );
} );
} );
it( 'isEnabled', () => {
for ( const name in commands ) {
commands[ name ].isEnabled = false;
}
expect( dropdown.buttonView.isEnabled ).to.be.false;
commands.heading2.isEnabled = true;
expect( dropdown.buttonView.isEnabled ).to.be.true;
} );
it( 'label', () => {
for ( const name in commands ) {
commands[ name ].value = false;
}
expect( dropdown.buttonView.label ).to.equal( 'Choose heading' );
commands.heading2.value = true;
expect( dropdown.buttonView.label ).to.equal( 'Heading 2' );
} );
} );
describe( 'localization', () => {
let commands, editor, dropdown;
beforeEach( () => {
return localizedEditor( [
{ modelElement: 'paragraph', title: 'Paragraph' },
{ modelElement: 'heading1', viewElement: 'h2', title: 'Heading 1' },
{ modelElement: 'heading2', viewElement: 'h3', title: 'Heading 2' }
] );
} );
it( 'does not alter the original config', () => {
expect( editor.config.get( 'heading.options' ) ).to.deep.equal( [
{ modelElement: 'paragraph', title: 'Paragraph' },
{ modelElement: 'heading1', viewElement: 'h2', title: 'Heading 1' },
{ modelElement: 'heading2', viewElement: 'h3', title: 'Heading 2' }
] );
} );
it( 'works for the #buttonView', () => {
const buttonView = dropdown.buttonView;
// Setting manually paragraph.value to `false` because there might be some content in editor
// after initialisation (for example empty <p></p> inserted when editor is empty).
commands.paragraph.value = false;
expect( buttonView.label ).to.equal( 'Wybierz nagłówek' );
expect( buttonView.tooltip ).to.equal( 'Nagłówek' );
commands.paragraph.value = true;
expect( buttonView.label ).to.equal( 'Akapit' );
commands.paragraph.value = false;
commands.heading1.value = true;
expect( buttonView.label ).to.equal( 'Nagłówek 1' );
} );
it( 'works for the listView#items in the panel', () => {
const listView = dropdown.listView;
expect( listView.items.map( item => item.label ) ).to.deep.equal( [
'Akapit',
'Nagłówek 1',
'Nagłówek 2'
] );
} );
it( 'allows custom titles', () => {
return localizedEditor( [
{ modelElement: 'paragraph', title: 'Custom paragraph title' },
{ modelElement: 'heading1', title: 'Custom heading1 title' }
] ).then( () => {
const listView = dropdown.listView;
expect( listView.items.map( item => item.label ) ).to.deep.equal( [
'Custom paragraph title',
'Custom heading1 title',
] );
} );
} );
it( 'translates default using the the locale', () => {
return localizedEditor( [
{ modelElement: 'paragraph', title: 'Paragraph' }
] ).then( () => {
const listView = dropdown.listView;
expect( listView.items.map( item => item.label ) ).to.deep.equal( [
'Akapit'
] );
} );
} );
function localizedEditor( options ) {
const editorElement = document.createElement( 'div' );
document.body.appendChild( editorElement );
return ClassicTestEditor
.create( editorElement, {
plugins: [ Heading ],
toolbar: [ 'heading' ],
lang: 'pl',
heading: {
options
}
} )
.then( newEditor => {
editor = newEditor;
dropdown = editor.ui.componentFactory.create( 'headings' );
commands = {};
editor.config.get( 'heading.options' ).forEach( ( { modelElement } ) => {
commands[ modelElement ] = editor.commands.get( modelElement );
} );
editorElement.remove();
return editor.destroy();
} );
}
} );
describe( 'class', () => {
it( 'is set for the listView#items in the panel', () => {
const listView = dropdown.listView;
expect( listView.items.map( item => item.class ) ).to.deep.equal( [
'ck-heading_paragraph',
'ck-heading_heading1',
'ck-heading_heading2',
'ck-heading_heading3'
] );
} );
it( 'reflects the #value of the commands', () => {
const listView = dropdown.listView;
setData( editor.document, '<heading2>f{}oo</heading2>' );
expect( listView.items.map( item => item.isActive ) ).to.deep.equal( [
false,
false,
true,
false
] );
} );
} );
} );
} );