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