@ckeditor/ckeditor5-heading 41.1.0 → 41.3.0-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/heading.js +1 -1
- package/dist/content-index.css +4 -0
- package/dist/editor-index.css +16 -0
- package/dist/index.css +26 -0
- package/dist/index.css.map +1 -0
- package/dist/index.js +881 -0
- package/dist/index.js.map +1 -0
- package/dist/types/augmentation.d.ts +30 -0
- package/dist/types/heading.d.ts +32 -0
- package/dist/types/headingbuttonsui.d.ts +51 -0
- package/dist/types/headingcommand.d.ts +48 -0
- package/dist/types/headingconfig.d.ts +145 -0
- package/dist/types/headingediting.d.ts +42 -0
- package/dist/types/headingui.d.ts +22 -0
- package/dist/types/index.d.ts +16 -0
- package/dist/types/title.d.ts +115 -0
- package/dist/types/utils.d.ts +18 -0
- package/lang/translations/he.po +1 -1
- package/package.json +3 -2
- package/src/headingconfig.d.ts +37 -2
package/dist/index.js
ADDED
|
@@ -0,0 +1,881 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
|
|
3
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
4
|
+
*/
|
|
5
|
+
import { Command, Plugin, icons } from '@ckeditor/ckeditor5-core/dist/index.js';
|
|
6
|
+
import { Paragraph } from '@ckeditor/ckeditor5-paragraph/dist/index.js';
|
|
7
|
+
import { first, priorities, Collection } from '@ckeditor/ckeditor5-utils/dist/index.js';
|
|
8
|
+
import { ViewModel, createDropdown, addListToDropdown, ButtonView } from '@ckeditor/ckeditor5-ui/dist/index.js';
|
|
9
|
+
import { DowncastWriter, enablePlaceholder, hidePlaceholder, needsPlaceholder, showPlaceholder } from '@ckeditor/ckeditor5-engine/dist/index.js';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
|
|
13
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
14
|
+
*/
|
|
15
|
+
/**
|
|
16
|
+
* @module heading/headingcommand
|
|
17
|
+
*/
|
|
18
|
+
/**
|
|
19
|
+
* The heading command. It is used by the {@link module:heading/heading~Heading heading feature} to apply headings.
|
|
20
|
+
*/
|
|
21
|
+
class HeadingCommand extends Command {
|
|
22
|
+
/**
|
|
23
|
+
* Creates an instance of the command.
|
|
24
|
+
*
|
|
25
|
+
* @param editor Editor instance.
|
|
26
|
+
* @param modelElements Names of the element which this command can apply in the model.
|
|
27
|
+
*/
|
|
28
|
+
constructor(editor, modelElements) {
|
|
29
|
+
super(editor);
|
|
30
|
+
this.modelElements = modelElements;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* @inheritDoc
|
|
34
|
+
*/
|
|
35
|
+
refresh() {
|
|
36
|
+
const block = first(this.editor.model.document.selection.getSelectedBlocks());
|
|
37
|
+
this.value = !!block && this.modelElements.includes(block.name) && block.name;
|
|
38
|
+
this.isEnabled = !!block && this.modelElements.some(heading => checkCanBecomeHeading(block, heading, this.editor.model.schema));
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Executes the command. Applies the heading to the selected blocks or, if the first selected
|
|
42
|
+
* block is a heading already, turns selected headings (of this level only) to paragraphs.
|
|
43
|
+
*
|
|
44
|
+
* @param options.value Name of the element which this command will apply in the model.
|
|
45
|
+
* @fires execute
|
|
46
|
+
*/
|
|
47
|
+
execute(options) {
|
|
48
|
+
const model = this.editor.model;
|
|
49
|
+
const document = model.document;
|
|
50
|
+
const modelElement = options.value;
|
|
51
|
+
model.change(writer => {
|
|
52
|
+
const blocks = Array.from(document.selection.getSelectedBlocks())
|
|
53
|
+
.filter(block => {
|
|
54
|
+
return checkCanBecomeHeading(block, modelElement, model.schema);
|
|
55
|
+
});
|
|
56
|
+
for (const block of blocks) {
|
|
57
|
+
if (!block.is('element', modelElement)) {
|
|
58
|
+
writer.rename(block, modelElement);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Checks whether the given block can be replaced by a specific heading.
|
|
66
|
+
*
|
|
67
|
+
* @param block A block to be tested.
|
|
68
|
+
* @param heading Command element name in the model.
|
|
69
|
+
* @param schema The schema of the document.
|
|
70
|
+
*/
|
|
71
|
+
function checkCanBecomeHeading(block, heading, schema) {
|
|
72
|
+
return schema.checkChild(block.parent, heading) && !schema.isObject(block);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
|
|
77
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
78
|
+
*/
|
|
79
|
+
/**
|
|
80
|
+
* @module heading/headingediting
|
|
81
|
+
*/
|
|
82
|
+
const defaultModelElement = 'paragraph';
|
|
83
|
+
/**
|
|
84
|
+
* The headings engine feature. It handles switching between block formats – headings and paragraph.
|
|
85
|
+
* This class represents the engine part of the heading feature. See also {@link module:heading/heading~Heading}.
|
|
86
|
+
* It introduces `heading1`-`headingN` commands which allow to convert paragraphs into headings.
|
|
87
|
+
*/
|
|
88
|
+
class HeadingEditing extends Plugin {
|
|
89
|
+
/**
|
|
90
|
+
* @inheritDoc
|
|
91
|
+
*/
|
|
92
|
+
static get pluginName() {
|
|
93
|
+
return 'HeadingEditing';
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* @inheritDoc
|
|
97
|
+
*/
|
|
98
|
+
constructor(editor) {
|
|
99
|
+
super(editor);
|
|
100
|
+
editor.config.define('heading', {
|
|
101
|
+
options: [
|
|
102
|
+
{ model: 'paragraph', title: 'Paragraph', class: 'ck-heading_paragraph' },
|
|
103
|
+
{ model: 'heading1', view: 'h2', title: 'Heading 1', class: 'ck-heading_heading1' },
|
|
104
|
+
{ model: 'heading2', view: 'h3', title: 'Heading 2', class: 'ck-heading_heading2' },
|
|
105
|
+
{ model: 'heading3', view: 'h4', title: 'Heading 3', class: 'ck-heading_heading3' }
|
|
106
|
+
]
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* @inheritDoc
|
|
111
|
+
*/
|
|
112
|
+
static get requires() {
|
|
113
|
+
return [Paragraph];
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* @inheritDoc
|
|
117
|
+
*/
|
|
118
|
+
init() {
|
|
119
|
+
const editor = this.editor;
|
|
120
|
+
const options = editor.config.get('heading.options');
|
|
121
|
+
const modelElements = [];
|
|
122
|
+
for (const option of options) {
|
|
123
|
+
// Skip paragraph - it is defined in required Paragraph feature.
|
|
124
|
+
if (option.model === 'paragraph') {
|
|
125
|
+
continue;
|
|
126
|
+
}
|
|
127
|
+
// Schema.
|
|
128
|
+
editor.model.schema.register(option.model, {
|
|
129
|
+
inheritAllFrom: '$block'
|
|
130
|
+
});
|
|
131
|
+
editor.conversion.elementToElement(option);
|
|
132
|
+
modelElements.push(option.model);
|
|
133
|
+
}
|
|
134
|
+
this._addDefaultH1Conversion(editor);
|
|
135
|
+
// Register the heading command for this option.
|
|
136
|
+
editor.commands.add('heading', new HeadingCommand(editor, modelElements));
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* @inheritDoc
|
|
140
|
+
*/
|
|
141
|
+
afterInit() {
|
|
142
|
+
// If the enter command is added to the editor, alter its behavior.
|
|
143
|
+
// Enter at the end of a heading element should create a paragraph.
|
|
144
|
+
const editor = this.editor;
|
|
145
|
+
const enterCommand = editor.commands.get('enter');
|
|
146
|
+
const options = editor.config.get('heading.options');
|
|
147
|
+
if (enterCommand) {
|
|
148
|
+
this.listenTo(enterCommand, 'afterExecute', (evt, data) => {
|
|
149
|
+
const positionParent = editor.model.document.selection.getFirstPosition().parent;
|
|
150
|
+
const isHeading = options.some(option => positionParent.is('element', option.model));
|
|
151
|
+
if (isHeading && !positionParent.is('element', defaultModelElement) && positionParent.childCount === 0) {
|
|
152
|
+
data.writer.rename(positionParent, defaultModelElement);
|
|
153
|
+
}
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Adds default conversion for `h1` -> `heading1` with a low priority.
|
|
159
|
+
*
|
|
160
|
+
* @param editor Editor instance on which to add the `h1` conversion.
|
|
161
|
+
*/
|
|
162
|
+
_addDefaultH1Conversion(editor) {
|
|
163
|
+
editor.conversion.for('upcast').elementToElement({
|
|
164
|
+
model: 'heading1',
|
|
165
|
+
view: 'h1',
|
|
166
|
+
// With a `low` priority, `paragraph` plugin autoparagraphing mechanism is executed. Make sure
|
|
167
|
+
// this listener is called before it. If not, `h1` will be transformed into a paragraph.
|
|
168
|
+
converterPriority: priorities.low + 1
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
|
|
175
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
176
|
+
*/
|
|
177
|
+
/**
|
|
178
|
+
* Returns heading options as defined in `config.heading.options` but processed to consider
|
|
179
|
+
* the editor localization, i.e. to display {@link module:heading/headingconfig~HeadingOption}
|
|
180
|
+
* in the correct language.
|
|
181
|
+
*
|
|
182
|
+
* Note: The reason behind this method is that there is no way to use {@link module:utils/locale~Locale#t}
|
|
183
|
+
* when the user configuration is defined because the editor does not exist yet.
|
|
184
|
+
*/
|
|
185
|
+
function getLocalizedOptions(editor) {
|
|
186
|
+
const t = editor.t;
|
|
187
|
+
const localizedTitles = {
|
|
188
|
+
'Paragraph': t('Paragraph'),
|
|
189
|
+
'Heading 1': t('Heading 1'),
|
|
190
|
+
'Heading 2': t('Heading 2'),
|
|
191
|
+
'Heading 3': t('Heading 3'),
|
|
192
|
+
'Heading 4': t('Heading 4'),
|
|
193
|
+
'Heading 5': t('Heading 5'),
|
|
194
|
+
'Heading 6': t('Heading 6')
|
|
195
|
+
};
|
|
196
|
+
return editor.config.get('heading.options').map(option => {
|
|
197
|
+
const title = localizedTitles[option.title];
|
|
198
|
+
if (title && title != option.title) {
|
|
199
|
+
option.title = title;
|
|
200
|
+
}
|
|
201
|
+
return option;
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
|
|
207
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
208
|
+
*/
|
|
209
|
+
/**
|
|
210
|
+
* @module heading/headingui
|
|
211
|
+
*/
|
|
212
|
+
/**
|
|
213
|
+
* The headings UI feature. It introduces the `headings` dropdown.
|
|
214
|
+
*/
|
|
215
|
+
class HeadingUI extends Plugin {
|
|
216
|
+
/**
|
|
217
|
+
* @inheritDoc
|
|
218
|
+
*/
|
|
219
|
+
static get pluginName() {
|
|
220
|
+
return 'HeadingUI';
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* @inheritDoc
|
|
224
|
+
*/
|
|
225
|
+
init() {
|
|
226
|
+
const editor = this.editor;
|
|
227
|
+
const t = editor.t;
|
|
228
|
+
const options = getLocalizedOptions(editor);
|
|
229
|
+
const defaultTitle = t('Choose heading');
|
|
230
|
+
const accessibleLabel = t('Heading');
|
|
231
|
+
// Register UI component.
|
|
232
|
+
editor.ui.componentFactory.add('heading', locale => {
|
|
233
|
+
const titles = {};
|
|
234
|
+
const itemDefinitions = new Collection();
|
|
235
|
+
const headingCommand = editor.commands.get('heading');
|
|
236
|
+
const paragraphCommand = editor.commands.get('paragraph');
|
|
237
|
+
const commands = [headingCommand];
|
|
238
|
+
for (const option of options) {
|
|
239
|
+
const def = {
|
|
240
|
+
type: 'button',
|
|
241
|
+
model: new ViewModel({
|
|
242
|
+
label: option.title,
|
|
243
|
+
class: option.class,
|
|
244
|
+
role: 'menuitemradio',
|
|
245
|
+
withText: true
|
|
246
|
+
})
|
|
247
|
+
};
|
|
248
|
+
if (option.model === 'paragraph') {
|
|
249
|
+
def.model.bind('isOn').to(paragraphCommand, 'value');
|
|
250
|
+
def.model.set('commandName', 'paragraph');
|
|
251
|
+
commands.push(paragraphCommand);
|
|
252
|
+
}
|
|
253
|
+
else {
|
|
254
|
+
def.model.bind('isOn').to(headingCommand, 'value', value => value === option.model);
|
|
255
|
+
def.model.set({
|
|
256
|
+
commandName: 'heading',
|
|
257
|
+
commandValue: option.model
|
|
258
|
+
});
|
|
259
|
+
}
|
|
260
|
+
// Add the option to the collection.
|
|
261
|
+
itemDefinitions.add(def);
|
|
262
|
+
titles[option.model] = option.title;
|
|
263
|
+
}
|
|
264
|
+
const dropdownView = createDropdown(locale);
|
|
265
|
+
addListToDropdown(dropdownView, itemDefinitions, {
|
|
266
|
+
ariaLabel: accessibleLabel,
|
|
267
|
+
role: 'menu'
|
|
268
|
+
});
|
|
269
|
+
dropdownView.buttonView.set({
|
|
270
|
+
ariaLabel: accessibleLabel,
|
|
271
|
+
ariaLabelledBy: undefined,
|
|
272
|
+
isOn: false,
|
|
273
|
+
withText: true,
|
|
274
|
+
tooltip: accessibleLabel
|
|
275
|
+
});
|
|
276
|
+
dropdownView.extendTemplate({
|
|
277
|
+
attributes: {
|
|
278
|
+
class: [
|
|
279
|
+
'ck-heading-dropdown'
|
|
280
|
+
]
|
|
281
|
+
}
|
|
282
|
+
});
|
|
283
|
+
dropdownView.bind('isEnabled').toMany(commands, 'isEnabled', (...areEnabled) => {
|
|
284
|
+
return areEnabled.some(isEnabled => isEnabled);
|
|
285
|
+
});
|
|
286
|
+
dropdownView.buttonView.bind('label').to(headingCommand, 'value', paragraphCommand, 'value', (value, para) => {
|
|
287
|
+
const whichModel = value || para && 'paragraph';
|
|
288
|
+
if (typeof whichModel === 'boolean') {
|
|
289
|
+
return defaultTitle;
|
|
290
|
+
}
|
|
291
|
+
// If none of the commands is active, display default title.
|
|
292
|
+
if (!titles[whichModel]) {
|
|
293
|
+
return defaultTitle;
|
|
294
|
+
}
|
|
295
|
+
return titles[whichModel];
|
|
296
|
+
});
|
|
297
|
+
// Execute command when an item from the dropdown is selected.
|
|
298
|
+
this.listenTo(dropdownView, 'execute', evt => {
|
|
299
|
+
const { commandName, commandValue } = evt.source;
|
|
300
|
+
editor.execute(commandName, commandValue ? { value: commandValue } : undefined);
|
|
301
|
+
editor.editing.view.focus();
|
|
302
|
+
});
|
|
303
|
+
return dropdownView;
|
|
304
|
+
});
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
/**
|
|
309
|
+
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
|
|
310
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
311
|
+
*/
|
|
312
|
+
/**
|
|
313
|
+
* @module heading/heading
|
|
314
|
+
*/
|
|
315
|
+
/**
|
|
316
|
+
* The headings feature.
|
|
317
|
+
*
|
|
318
|
+
* For a detailed overview, check the {@glink features/headings Headings feature} guide
|
|
319
|
+
* and the {@glink api/heading package page}.
|
|
320
|
+
*
|
|
321
|
+
* This is a "glue" plugin which loads the {@link module:heading/headingediting~HeadingEditing heading editing feature}
|
|
322
|
+
* and {@link module:heading/headingui~HeadingUI heading UI feature}.
|
|
323
|
+
*
|
|
324
|
+
* @extends module:core/plugin~Plugin
|
|
325
|
+
*/
|
|
326
|
+
class Heading extends Plugin {
|
|
327
|
+
/**
|
|
328
|
+
* @inheritDoc
|
|
329
|
+
*/
|
|
330
|
+
static get requires() {
|
|
331
|
+
return [HeadingEditing, HeadingUI];
|
|
332
|
+
}
|
|
333
|
+
/**
|
|
334
|
+
* @inheritDoc
|
|
335
|
+
*/
|
|
336
|
+
static get pluginName() {
|
|
337
|
+
return 'Heading';
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
/**
|
|
342
|
+
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
|
|
343
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
344
|
+
*/
|
|
345
|
+
/**
|
|
346
|
+
* @module heading/headingbuttonsui
|
|
347
|
+
*/
|
|
348
|
+
const defaultIcons = {
|
|
349
|
+
heading1: icons.heading1,
|
|
350
|
+
heading2: icons.heading2,
|
|
351
|
+
heading3: icons.heading3,
|
|
352
|
+
heading4: icons.heading4,
|
|
353
|
+
heading5: icons.heading5,
|
|
354
|
+
heading6: icons.heading6
|
|
355
|
+
};
|
|
356
|
+
/**
|
|
357
|
+
* The `HeadingButtonsUI` plugin defines a set of UI buttons that can be used instead of the
|
|
358
|
+
* standard drop down component.
|
|
359
|
+
*
|
|
360
|
+
* This feature is not enabled by default by the {@link module:heading/heading~Heading} plugin and needs to be
|
|
361
|
+
* installed manually to the editor configuration.
|
|
362
|
+
*
|
|
363
|
+
* Plugin introduces button UI elements, which names are same as `model` property from {@link module:heading/headingconfig~HeadingOption}.
|
|
364
|
+
*
|
|
365
|
+
* ```ts
|
|
366
|
+
* ClassicEditor
|
|
367
|
+
* .create( {
|
|
368
|
+
* plugins: [ ..., Heading, Paragraph, HeadingButtonsUI, ParagraphButtonUI ]
|
|
369
|
+
* heading: {
|
|
370
|
+
* options: [
|
|
371
|
+
* { model: 'paragraph', title: 'Paragraph', class: 'ck-heading_paragraph' },
|
|
372
|
+
* { model: 'heading1', view: 'h2', title: 'Heading 1', class: 'ck-heading_heading1' },
|
|
373
|
+
* { model: 'heading2', view: 'h3', title: 'Heading 2', class: 'ck-heading_heading2' },
|
|
374
|
+
* { model: 'heading3', view: 'h4', title: 'Heading 3', class: 'ck-heading_heading3' }
|
|
375
|
+
* ]
|
|
376
|
+
* },
|
|
377
|
+
* toolbar: [ 'paragraph', 'heading1', 'heading2', 'heading3' ]
|
|
378
|
+
* } )
|
|
379
|
+
* .then( ... )
|
|
380
|
+
* .catch( ... );
|
|
381
|
+
* ```
|
|
382
|
+
*
|
|
383
|
+
* NOTE: The `'paragraph'` button is defined in by the {@link module:paragraph/paragraphbuttonui~ParagraphButtonUI} plugin
|
|
384
|
+
* which needs to be loaded manually as well.
|
|
385
|
+
*
|
|
386
|
+
* It is possible to use custom icons by providing `icon` config option in {@link module:heading/headingconfig~HeadingOption}.
|
|
387
|
+
* For the default configuration standard icons are used.
|
|
388
|
+
*/
|
|
389
|
+
class HeadingButtonsUI extends Plugin {
|
|
390
|
+
/**
|
|
391
|
+
* @inheritDoc
|
|
392
|
+
*/
|
|
393
|
+
init() {
|
|
394
|
+
const options = getLocalizedOptions(this.editor);
|
|
395
|
+
options
|
|
396
|
+
.filter(item => item.model !== 'paragraph')
|
|
397
|
+
.map(item => this._createButton(item));
|
|
398
|
+
}
|
|
399
|
+
/**
|
|
400
|
+
* Creates single button view from provided configuration option.
|
|
401
|
+
*/
|
|
402
|
+
_createButton(option) {
|
|
403
|
+
const editor = this.editor;
|
|
404
|
+
editor.ui.componentFactory.add(option.model, locale => {
|
|
405
|
+
const view = new ButtonView(locale);
|
|
406
|
+
const command = editor.commands.get('heading');
|
|
407
|
+
view.label = option.title;
|
|
408
|
+
view.icon = option.icon || defaultIcons[option.model];
|
|
409
|
+
view.tooltip = true;
|
|
410
|
+
view.isToggleable = true;
|
|
411
|
+
view.bind('isEnabled').to(command);
|
|
412
|
+
view.bind('isOn').to(command, 'value', value => value == option.model);
|
|
413
|
+
view.on('execute', () => {
|
|
414
|
+
editor.execute('heading', { value: option.model });
|
|
415
|
+
editor.editing.view.focus();
|
|
416
|
+
});
|
|
417
|
+
return view;
|
|
418
|
+
});
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
/**
|
|
423
|
+
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
|
|
424
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
425
|
+
*/
|
|
426
|
+
/**
|
|
427
|
+
* @module heading/title
|
|
428
|
+
*/
|
|
429
|
+
// A list of element names that should be treated by the Title plugin as title-like.
|
|
430
|
+
// This means that an element of a type from this list will be changed to a title element
|
|
431
|
+
// when it is the first element in the root.
|
|
432
|
+
const titleLikeElements = new Set(['paragraph', 'heading1', 'heading2', 'heading3', 'heading4', 'heading5', 'heading6']);
|
|
433
|
+
/**
|
|
434
|
+
* The Title plugin.
|
|
435
|
+
*
|
|
436
|
+
* It splits the document into `Title` and `Body` sections.
|
|
437
|
+
*/
|
|
438
|
+
class Title extends Plugin {
|
|
439
|
+
constructor() {
|
|
440
|
+
super(...arguments);
|
|
441
|
+
/**
|
|
442
|
+
* A reference to an empty paragraph in the body
|
|
443
|
+
* created when there is no element in the body for the placeholder purposes.
|
|
444
|
+
*/
|
|
445
|
+
this._bodyPlaceholder = new Map();
|
|
446
|
+
}
|
|
447
|
+
/**
|
|
448
|
+
* @inheritDoc
|
|
449
|
+
*/
|
|
450
|
+
static get pluginName() {
|
|
451
|
+
return 'Title';
|
|
452
|
+
}
|
|
453
|
+
/**
|
|
454
|
+
* @inheritDoc
|
|
455
|
+
*/
|
|
456
|
+
static get requires() {
|
|
457
|
+
return ['Paragraph'];
|
|
458
|
+
}
|
|
459
|
+
/**
|
|
460
|
+
* @inheritDoc
|
|
461
|
+
*/
|
|
462
|
+
init() {
|
|
463
|
+
const editor = this.editor;
|
|
464
|
+
const model = editor.model;
|
|
465
|
+
// To use the schema for disabling some features when the selection is inside the title element
|
|
466
|
+
// it is needed to create the following structure:
|
|
467
|
+
//
|
|
468
|
+
// <title>
|
|
469
|
+
// <title-content>The title text</title-content>
|
|
470
|
+
// </title>
|
|
471
|
+
//
|
|
472
|
+
// See: https://github.com/ckeditor/ckeditor5/issues/2005.
|
|
473
|
+
model.schema.register('title', { isBlock: true, allowIn: '$root' });
|
|
474
|
+
model.schema.register('title-content', { isBlock: true, allowIn: 'title', allowAttributes: ['alignment'] });
|
|
475
|
+
model.schema.extend('$text', { allowIn: 'title-content' });
|
|
476
|
+
// Disallow all attributes in `title-content`.
|
|
477
|
+
model.schema.addAttributeCheck(context => {
|
|
478
|
+
if (context.endsWith('title-content $text')) {
|
|
479
|
+
return false;
|
|
480
|
+
}
|
|
481
|
+
});
|
|
482
|
+
// Because `title` is represented by two elements in the model
|
|
483
|
+
// but only one in the view, it is needed to adjust Mapper.
|
|
484
|
+
editor.editing.mapper.on('modelToViewPosition', mapModelPositionToView(editor.editing.view));
|
|
485
|
+
editor.data.mapper.on('modelToViewPosition', mapModelPositionToView(editor.editing.view));
|
|
486
|
+
// Conversion.
|
|
487
|
+
editor.conversion.for('downcast').elementToElement({ model: 'title-content', view: 'h1' });
|
|
488
|
+
editor.conversion.for('downcast').add(dispatcher => dispatcher.on('insert:title', (evt, data, conversionApi) => {
|
|
489
|
+
conversionApi.consumable.consume(data.item, evt.name);
|
|
490
|
+
}));
|
|
491
|
+
// Custom converter is used for data v -> m conversion to avoid calling post-fixer when setting data.
|
|
492
|
+
// See https://github.com/ckeditor/ckeditor5/issues/2036.
|
|
493
|
+
editor.data.upcastDispatcher.on('element:h1', dataViewModelH1Insertion, { priority: 'high' });
|
|
494
|
+
editor.data.upcastDispatcher.on('element:h2', dataViewModelH1Insertion, { priority: 'high' });
|
|
495
|
+
editor.data.upcastDispatcher.on('element:h3', dataViewModelH1Insertion, { priority: 'high' });
|
|
496
|
+
// Take care about correct `title` element structure.
|
|
497
|
+
model.document.registerPostFixer(writer => this._fixTitleContent(writer));
|
|
498
|
+
// Create and take care of correct position of a `title` element.
|
|
499
|
+
model.document.registerPostFixer(writer => this._fixTitleElement(writer));
|
|
500
|
+
// Create element for `Body` placeholder if it is missing.
|
|
501
|
+
model.document.registerPostFixer(writer => this._fixBodyElement(writer));
|
|
502
|
+
// Prevent from adding extra at the end of the document.
|
|
503
|
+
model.document.registerPostFixer(writer => this._fixExtraParagraph(writer));
|
|
504
|
+
// Attach `Title` and `Body` placeholders to the empty title and/or content.
|
|
505
|
+
this._attachPlaceholders();
|
|
506
|
+
// Attach Tab handling.
|
|
507
|
+
this._attachTabPressHandling();
|
|
508
|
+
}
|
|
509
|
+
/**
|
|
510
|
+
* Returns the title of the document. Note that because this plugin does not allow any formatting inside
|
|
511
|
+
* the title element, the output of this method will be a plain text, with no HTML tags.
|
|
512
|
+
*
|
|
513
|
+
* It is not recommended to use this method together with features that insert markers to the
|
|
514
|
+
* data output, like comments or track changes features. If such markers start in the title and end in the
|
|
515
|
+
* body, the result of this method might be incorrect.
|
|
516
|
+
*
|
|
517
|
+
* @param options Additional configuration passed to the conversion process.
|
|
518
|
+
* See {@link module:engine/controller/datacontroller~DataController#get `DataController#get`}.
|
|
519
|
+
* @returns The title of the document.
|
|
520
|
+
*/
|
|
521
|
+
getTitle(options = {}) {
|
|
522
|
+
const rootName = options.rootName ? options.rootName : undefined;
|
|
523
|
+
const titleElement = this._getTitleElement(rootName);
|
|
524
|
+
const titleContentElement = titleElement.getChild(0);
|
|
525
|
+
return this.editor.data.stringify(titleContentElement, options);
|
|
526
|
+
}
|
|
527
|
+
/**
|
|
528
|
+
* Returns the body of the document.
|
|
529
|
+
*
|
|
530
|
+
* Note that it is not recommended to use this method together with features that insert markers to the
|
|
531
|
+
* data output, like comments or track changes features. If such markers start in the title and end in the
|
|
532
|
+
* body, the result of this method might be incorrect.
|
|
533
|
+
*
|
|
534
|
+
* @param options Additional configuration passed to the conversion process.
|
|
535
|
+
* See {@link module:engine/controller/datacontroller~DataController#get `DataController#get`}.
|
|
536
|
+
* @returns The body of the document.
|
|
537
|
+
*/
|
|
538
|
+
getBody(options = {}) {
|
|
539
|
+
const editor = this.editor;
|
|
540
|
+
const data = editor.data;
|
|
541
|
+
const model = editor.model;
|
|
542
|
+
const rootName = options.rootName ? options.rootName : undefined;
|
|
543
|
+
const root = editor.model.document.getRoot(rootName);
|
|
544
|
+
const view = editor.editing.view;
|
|
545
|
+
const viewWriter = new DowncastWriter(view.document);
|
|
546
|
+
const rootRange = model.createRangeIn(root);
|
|
547
|
+
const viewDocumentFragment = viewWriter.createDocumentFragment();
|
|
548
|
+
// Find all markers that intersects with body.
|
|
549
|
+
const bodyStartPosition = model.createPositionAfter(root.getChild(0));
|
|
550
|
+
const bodyRange = model.createRange(bodyStartPosition, model.createPositionAt(root, 'end'));
|
|
551
|
+
const markers = new Map();
|
|
552
|
+
for (const marker of model.markers) {
|
|
553
|
+
const intersection = bodyRange.getIntersection(marker.getRange());
|
|
554
|
+
if (intersection) {
|
|
555
|
+
markers.set(marker.name, intersection);
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
// Convert the entire root to view.
|
|
559
|
+
data.mapper.clearBindings();
|
|
560
|
+
data.mapper.bindElements(root, viewDocumentFragment);
|
|
561
|
+
data.downcastDispatcher.convert(rootRange, markers, viewWriter, options);
|
|
562
|
+
// Remove title element from view.
|
|
563
|
+
viewWriter.remove(viewWriter.createRangeOn(viewDocumentFragment.getChild(0)));
|
|
564
|
+
// view -> data
|
|
565
|
+
return editor.data.processor.toData(viewDocumentFragment);
|
|
566
|
+
}
|
|
567
|
+
/**
|
|
568
|
+
* Returns the `title` element when it is in the document. Returns `undefined` otherwise.
|
|
569
|
+
*/
|
|
570
|
+
_getTitleElement(rootName) {
|
|
571
|
+
const root = this.editor.model.document.getRoot(rootName);
|
|
572
|
+
for (const child of root.getChildren()) {
|
|
573
|
+
if (isTitle(child)) {
|
|
574
|
+
return child;
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
/**
|
|
579
|
+
* Model post-fixer callback that ensures that `title` has only one `title-content` child.
|
|
580
|
+
* All additional children should be moved after the `title` element and renamed to a paragraph.
|
|
581
|
+
*/
|
|
582
|
+
_fixTitleContent(writer) {
|
|
583
|
+
let changed = false;
|
|
584
|
+
for (const rootName of this.editor.model.document.getRootNames()) {
|
|
585
|
+
const title = this._getTitleElement(rootName);
|
|
586
|
+
// If there is no title in the content it will be created by `_fixTitleElement` post-fixer.
|
|
587
|
+
// If the title has just one element, then it is correct. No fixing.
|
|
588
|
+
if (!title || title.maxOffset === 1) {
|
|
589
|
+
continue;
|
|
590
|
+
}
|
|
591
|
+
const titleChildren = Array.from(title.getChildren());
|
|
592
|
+
// Skip first child because it is an allowed element.
|
|
593
|
+
titleChildren.shift();
|
|
594
|
+
for (const titleChild of titleChildren) {
|
|
595
|
+
writer.move(writer.createRangeOn(titleChild), title, 'after');
|
|
596
|
+
writer.rename(titleChild, 'paragraph');
|
|
597
|
+
}
|
|
598
|
+
changed = true;
|
|
599
|
+
}
|
|
600
|
+
return changed;
|
|
601
|
+
}
|
|
602
|
+
/**
|
|
603
|
+
* Model post-fixer callback that creates a title element when it is missing,
|
|
604
|
+
* takes care of the correct position of it and removes additional title elements.
|
|
605
|
+
*/
|
|
606
|
+
_fixTitleElement(writer) {
|
|
607
|
+
let changed = false;
|
|
608
|
+
const model = this.editor.model;
|
|
609
|
+
for (const modelRoot of this.editor.model.document.getRoots()) {
|
|
610
|
+
const titleElements = Array.from(modelRoot.getChildren()).filter(isTitle);
|
|
611
|
+
const firstTitleElement = titleElements[0];
|
|
612
|
+
const firstRootChild = modelRoot.getChild(0);
|
|
613
|
+
// When title element is at the beginning of the document then try to fix additional title elements (if there are any).
|
|
614
|
+
if (firstRootChild.is('element', 'title')) {
|
|
615
|
+
if (titleElements.length > 1) {
|
|
616
|
+
fixAdditionalTitleElements(titleElements, writer, model);
|
|
617
|
+
changed = true;
|
|
618
|
+
}
|
|
619
|
+
continue;
|
|
620
|
+
}
|
|
621
|
+
// When there is no title in the document and first element in the document cannot be changed
|
|
622
|
+
// to the title then create an empty title element at the beginning of the document.
|
|
623
|
+
if (!firstTitleElement && !titleLikeElements.has(firstRootChild.name)) {
|
|
624
|
+
const title = writer.createElement('title');
|
|
625
|
+
writer.insert(title, modelRoot);
|
|
626
|
+
writer.insertElement('title-content', title);
|
|
627
|
+
changed = true;
|
|
628
|
+
continue;
|
|
629
|
+
}
|
|
630
|
+
if (titleLikeElements.has(firstRootChild.name)) {
|
|
631
|
+
// Change the first element in the document to the title if it can be changed (is title-like).
|
|
632
|
+
changeElementToTitle(firstRootChild, writer, model);
|
|
633
|
+
}
|
|
634
|
+
else {
|
|
635
|
+
// Otherwise, move the first occurrence of the title element to the beginning of the document.
|
|
636
|
+
writer.move(writer.createRangeOn(firstTitleElement), modelRoot, 0);
|
|
637
|
+
}
|
|
638
|
+
fixAdditionalTitleElements(titleElements, writer, model);
|
|
639
|
+
changed = true;
|
|
640
|
+
}
|
|
641
|
+
return changed;
|
|
642
|
+
}
|
|
643
|
+
/**
|
|
644
|
+
* Model post-fixer callback that adds an empty paragraph at the end of the document
|
|
645
|
+
* when it is needed for the placeholder purposes.
|
|
646
|
+
*/
|
|
647
|
+
_fixBodyElement(writer) {
|
|
648
|
+
let changed = false;
|
|
649
|
+
for (const rootName of this.editor.model.document.getRootNames()) {
|
|
650
|
+
const modelRoot = this.editor.model.document.getRoot(rootName);
|
|
651
|
+
if (modelRoot.childCount < 2) {
|
|
652
|
+
const placeholder = writer.createElement('paragraph');
|
|
653
|
+
writer.insert(placeholder, modelRoot, 1);
|
|
654
|
+
this._bodyPlaceholder.set(rootName, placeholder);
|
|
655
|
+
changed = true;
|
|
656
|
+
}
|
|
657
|
+
}
|
|
658
|
+
return changed;
|
|
659
|
+
}
|
|
660
|
+
/**
|
|
661
|
+
* Model post-fixer callback that removes a paragraph from the end of the document
|
|
662
|
+
* if it was created for the placeholder purposes and is not needed anymore.
|
|
663
|
+
*/
|
|
664
|
+
_fixExtraParagraph(writer) {
|
|
665
|
+
let changed = false;
|
|
666
|
+
for (const rootName of this.editor.model.document.getRootNames()) {
|
|
667
|
+
const root = this.editor.model.document.getRoot(rootName);
|
|
668
|
+
const placeholder = this._bodyPlaceholder.get(rootName);
|
|
669
|
+
if (shouldRemoveLastParagraph(placeholder, root)) {
|
|
670
|
+
this._bodyPlaceholder.delete(rootName);
|
|
671
|
+
writer.remove(placeholder);
|
|
672
|
+
changed = true;
|
|
673
|
+
}
|
|
674
|
+
}
|
|
675
|
+
return changed;
|
|
676
|
+
}
|
|
677
|
+
/**
|
|
678
|
+
* Attaches the `Title` and `Body` placeholders to the title and/or content.
|
|
679
|
+
*/
|
|
680
|
+
_attachPlaceholders() {
|
|
681
|
+
const editor = this.editor;
|
|
682
|
+
const t = editor.t;
|
|
683
|
+
const view = editor.editing.view;
|
|
684
|
+
const sourceElement = editor.sourceElement;
|
|
685
|
+
const titlePlaceholder = editor.config.get('title.placeholder') || t('Type your title');
|
|
686
|
+
const bodyPlaceholder = editor.config.get('placeholder') ||
|
|
687
|
+
sourceElement && sourceElement.tagName.toLowerCase() === 'textarea' && sourceElement.getAttribute('placeholder') ||
|
|
688
|
+
t('Type or paste your content here.');
|
|
689
|
+
// Attach placeholder to the view title element.
|
|
690
|
+
editor.editing.downcastDispatcher.on('insert:title-content', (evt, data, conversionApi) => {
|
|
691
|
+
const element = conversionApi.mapper.toViewElement(data.item);
|
|
692
|
+
element.placeholder = titlePlaceholder;
|
|
693
|
+
enablePlaceholder({
|
|
694
|
+
view,
|
|
695
|
+
element,
|
|
696
|
+
keepOnFocus: true
|
|
697
|
+
});
|
|
698
|
+
});
|
|
699
|
+
// Attach placeholder to first element after a title element and remove it if it's not needed anymore.
|
|
700
|
+
// First element after title can change, so we need to observe all changes keep placeholder in sync.
|
|
701
|
+
const bodyViewElements = new Map();
|
|
702
|
+
// This post-fixer runs after the model post-fixer, so we can assume that the second child in view root will always exist.
|
|
703
|
+
view.document.registerPostFixer(writer => {
|
|
704
|
+
let hasChanged = false;
|
|
705
|
+
for (const viewRoot of view.document.roots) {
|
|
706
|
+
// `viewRoot` can be empty despite the model post-fixers if the model root was detached.
|
|
707
|
+
if (viewRoot.isEmpty) {
|
|
708
|
+
continue;
|
|
709
|
+
}
|
|
710
|
+
// If `viewRoot` is not empty, then we can expect at least two elements in it.
|
|
711
|
+
const body = viewRoot.getChild(1);
|
|
712
|
+
const oldBody = bodyViewElements.get(viewRoot.rootName);
|
|
713
|
+
// If body element has changed we need to disable placeholder on the previous element and enable on the new one.
|
|
714
|
+
if (body !== oldBody) {
|
|
715
|
+
if (oldBody) {
|
|
716
|
+
hidePlaceholder(writer, oldBody);
|
|
717
|
+
writer.removeAttribute('data-placeholder', oldBody);
|
|
718
|
+
}
|
|
719
|
+
writer.setAttribute('data-placeholder', bodyPlaceholder, body);
|
|
720
|
+
bodyViewElements.set(viewRoot.rootName, body);
|
|
721
|
+
hasChanged = true;
|
|
722
|
+
}
|
|
723
|
+
// Then we need to display placeholder if it is needed.
|
|
724
|
+
// See: https://github.com/ckeditor/ckeditor5/issues/8689.
|
|
725
|
+
if (needsPlaceholder(body, true) && viewRoot.childCount === 2 && body.name === 'p') {
|
|
726
|
+
hasChanged = showPlaceholder(writer, body) ? true : hasChanged;
|
|
727
|
+
}
|
|
728
|
+
else {
|
|
729
|
+
// Or hide if it is not needed.
|
|
730
|
+
hasChanged = hidePlaceholder(writer, body) ? true : hasChanged;
|
|
731
|
+
}
|
|
732
|
+
}
|
|
733
|
+
return hasChanged;
|
|
734
|
+
});
|
|
735
|
+
}
|
|
736
|
+
/**
|
|
737
|
+
* Creates navigation between the title and body sections using <kbd>Tab</kbd> and <kbd>Shift</kbd>+<kbd>Tab</kbd> keys.
|
|
738
|
+
*/
|
|
739
|
+
_attachTabPressHandling() {
|
|
740
|
+
const editor = this.editor;
|
|
741
|
+
const model = editor.model;
|
|
742
|
+
// Pressing <kbd>Tab</kbd> inside the title should move the caret to the body.
|
|
743
|
+
editor.keystrokes.set('TAB', (data, cancel) => {
|
|
744
|
+
model.change(writer => {
|
|
745
|
+
const selection = model.document.selection;
|
|
746
|
+
const selectedElements = Array.from(selection.getSelectedBlocks());
|
|
747
|
+
if (selectedElements.length === 1 && selectedElements[0].is('element', 'title-content')) {
|
|
748
|
+
const root = selection.getFirstPosition().root;
|
|
749
|
+
const firstBodyElement = root.getChild(1);
|
|
750
|
+
writer.setSelection(firstBodyElement, 0);
|
|
751
|
+
cancel();
|
|
752
|
+
}
|
|
753
|
+
});
|
|
754
|
+
});
|
|
755
|
+
// Pressing <kbd>Shift</kbd>+<kbd>Tab</kbd> at the beginning of the body should move the caret to the title.
|
|
756
|
+
editor.keystrokes.set('SHIFT + TAB', (data, cancel) => {
|
|
757
|
+
model.change(writer => {
|
|
758
|
+
const selection = model.document.selection;
|
|
759
|
+
if (!selection.isCollapsed) {
|
|
760
|
+
return;
|
|
761
|
+
}
|
|
762
|
+
const selectedElement = first(selection.getSelectedBlocks());
|
|
763
|
+
const selectionPosition = selection.getFirstPosition();
|
|
764
|
+
const root = editor.model.document.getRoot(selectionPosition.root.rootName);
|
|
765
|
+
const title = root.getChild(0);
|
|
766
|
+
const body = root.getChild(1);
|
|
767
|
+
if (selectedElement === body && selectionPosition.isAtStart) {
|
|
768
|
+
writer.setSelection(title.getChild(0), 0);
|
|
769
|
+
cancel();
|
|
770
|
+
}
|
|
771
|
+
});
|
|
772
|
+
});
|
|
773
|
+
}
|
|
774
|
+
}
|
|
775
|
+
/**
|
|
776
|
+
* A view-to-model converter for the h1 that appears at the beginning of the document (a title element).
|
|
777
|
+
*
|
|
778
|
+
* @see module:engine/conversion/upcastdispatcher~UpcastDispatcher#event:element
|
|
779
|
+
* @param evt An object containing information about the fired event.
|
|
780
|
+
* @param data An object containing conversion input, a placeholder for conversion output and possibly other values.
|
|
781
|
+
* @param conversionApi Conversion interface to be used by the callback.
|
|
782
|
+
*/
|
|
783
|
+
function dataViewModelH1Insertion(evt, data, conversionApi) {
|
|
784
|
+
const modelCursor = data.modelCursor;
|
|
785
|
+
const viewItem = data.viewItem;
|
|
786
|
+
if (!modelCursor.isAtStart || !modelCursor.parent.is('element', '$root')) {
|
|
787
|
+
return;
|
|
788
|
+
}
|
|
789
|
+
if (!conversionApi.consumable.consume(viewItem, { name: true })) {
|
|
790
|
+
return;
|
|
791
|
+
}
|
|
792
|
+
const modelWriter = conversionApi.writer;
|
|
793
|
+
const title = modelWriter.createElement('title');
|
|
794
|
+
const titleContent = modelWriter.createElement('title-content');
|
|
795
|
+
modelWriter.append(titleContent, title);
|
|
796
|
+
modelWriter.insert(title, modelCursor);
|
|
797
|
+
conversionApi.convertChildren(viewItem, titleContent);
|
|
798
|
+
conversionApi.updateConversionResult(title, data);
|
|
799
|
+
}
|
|
800
|
+
/**
|
|
801
|
+
* Maps position from the beginning of the model `title` element to the beginning of the view `h1` element.
|
|
802
|
+
*
|
|
803
|
+
* ```html
|
|
804
|
+
* <title>^<title-content>Foo</title-content></title> -> <h1>^Foo</h1>
|
|
805
|
+
* ```
|
|
806
|
+
*/
|
|
807
|
+
function mapModelPositionToView(editingView) {
|
|
808
|
+
return (evt, data) => {
|
|
809
|
+
const positionParent = data.modelPosition.parent;
|
|
810
|
+
if (!positionParent.is('element', 'title')) {
|
|
811
|
+
return;
|
|
812
|
+
}
|
|
813
|
+
const modelTitleElement = positionParent.parent;
|
|
814
|
+
const viewElement = data.mapper.toViewElement(modelTitleElement);
|
|
815
|
+
data.viewPosition = editingView.createPositionAt(viewElement, 0);
|
|
816
|
+
evt.stop();
|
|
817
|
+
};
|
|
818
|
+
}
|
|
819
|
+
/**
|
|
820
|
+
* @returns Returns true when given element is a title. Returns false otherwise.
|
|
821
|
+
*/
|
|
822
|
+
function isTitle(element) {
|
|
823
|
+
return element.is('element', 'title');
|
|
824
|
+
}
|
|
825
|
+
/**
|
|
826
|
+
* Changes the given element to the title element.
|
|
827
|
+
*/
|
|
828
|
+
function changeElementToTitle(element, writer, model) {
|
|
829
|
+
const title = writer.createElement('title');
|
|
830
|
+
writer.insert(title, element, 'before');
|
|
831
|
+
writer.insert(element, title, 0);
|
|
832
|
+
writer.rename(element, 'title-content');
|
|
833
|
+
model.schema.removeDisallowedAttributes([element], writer);
|
|
834
|
+
}
|
|
835
|
+
/**
|
|
836
|
+
* Loops over the list of title elements and fixes additional ones.
|
|
837
|
+
*
|
|
838
|
+
* @returns Returns true when there was any change. Returns false otherwise.
|
|
839
|
+
*/
|
|
840
|
+
function fixAdditionalTitleElements(titleElements, writer, model) {
|
|
841
|
+
let hasChanged = false;
|
|
842
|
+
for (const title of titleElements) {
|
|
843
|
+
if (title.index !== 0) {
|
|
844
|
+
fixTitleElement(title, writer, model);
|
|
845
|
+
hasChanged = true;
|
|
846
|
+
}
|
|
847
|
+
}
|
|
848
|
+
return hasChanged;
|
|
849
|
+
}
|
|
850
|
+
/**
|
|
851
|
+
* Changes given title element to a paragraph or removes it when it is empty.
|
|
852
|
+
*/
|
|
853
|
+
function fixTitleElement(title, writer, model) {
|
|
854
|
+
const child = title.getChild(0);
|
|
855
|
+
// Empty title should be removed.
|
|
856
|
+
// It is created as a result of pasting to the title element.
|
|
857
|
+
if (child.isEmpty) {
|
|
858
|
+
writer.remove(title);
|
|
859
|
+
return;
|
|
860
|
+
}
|
|
861
|
+
writer.move(writer.createRangeOn(child), title, 'before');
|
|
862
|
+
writer.rename(child, 'paragraph');
|
|
863
|
+
writer.remove(title);
|
|
864
|
+
model.schema.removeDisallowedAttributes([child], writer);
|
|
865
|
+
}
|
|
866
|
+
/**
|
|
867
|
+
* Returns true when the last paragraph in the document was created only for the placeholder
|
|
868
|
+
* purpose and it's not needed anymore. Returns false otherwise.
|
|
869
|
+
*/
|
|
870
|
+
function shouldRemoveLastParagraph(placeholder, root) {
|
|
871
|
+
if (!placeholder || !placeholder.is('element', 'paragraph') || placeholder.childCount) {
|
|
872
|
+
return false;
|
|
873
|
+
}
|
|
874
|
+
if (root.childCount <= 2 || root.getChild(root.childCount - 1) !== placeholder) {
|
|
875
|
+
return false;
|
|
876
|
+
}
|
|
877
|
+
return true;
|
|
878
|
+
}
|
|
879
|
+
|
|
880
|
+
export { Heading, HeadingButtonsUI, HeadingEditing, HeadingUI, Title };
|
|
881
|
+
//# sourceMappingURL=index.js.map
|