1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | import { Plugin } from 'ckeditor5/src/core.js';
|
9 | import { first } from 'ckeditor5/src/utils.js';
|
10 | import { DowncastWriter, enablePlaceholder, hidePlaceholder, needsPlaceholder, showPlaceholder } from 'ckeditor5/src/engine.js';
|
11 |
|
12 |
|
13 |
|
14 | const titleLikeElements = new Set(['paragraph', 'heading1', 'heading2', 'heading3', 'heading4', 'heading5', 'heading6']);
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | export default class Title extends Plugin {
|
21 | constructor() {
|
22 | super(...arguments);
|
23 | |
24 |
|
25 |
|
26 |
|
27 | this._bodyPlaceholder = new Map();
|
28 | }
|
29 | |
30 |
|
31 |
|
32 | static get pluginName() {
|
33 | return 'Title';
|
34 | }
|
35 | |
36 |
|
37 |
|
38 | static get requires() {
|
39 | return ['Paragraph'];
|
40 | }
|
41 | |
42 |
|
43 |
|
44 | init() {
|
45 | const editor = this.editor;
|
46 | const model = editor.model;
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 | model.schema.register('title', { isBlock: true, allowIn: '$root' });
|
56 | model.schema.register('title-content', { isBlock: true, allowIn: 'title', allowAttributes: ['alignment'] });
|
57 | model.schema.extend('$text', { allowIn: 'title-content' });
|
58 |
|
59 | model.schema.addAttributeCheck(context => {
|
60 | if (context.endsWith('title-content $text')) {
|
61 | return false;
|
62 | }
|
63 | });
|
64 |
|
65 |
|
66 | editor.editing.mapper.on('modelToViewPosition', mapModelPositionToView(editor.editing.view));
|
67 | editor.data.mapper.on('modelToViewPosition', mapModelPositionToView(editor.editing.view));
|
68 |
|
69 | editor.conversion.for('downcast').elementToElement({ model: 'title-content', view: 'h1' });
|
70 | editor.conversion.for('downcast').add(dispatcher => dispatcher.on('insert:title', (evt, data, conversionApi) => {
|
71 | conversionApi.consumable.consume(data.item, evt.name);
|
72 | }));
|
73 |
|
74 |
|
75 | editor.data.upcastDispatcher.on('element:h1', dataViewModelH1Insertion, { priority: 'high' });
|
76 | editor.data.upcastDispatcher.on('element:h2', dataViewModelH1Insertion, { priority: 'high' });
|
77 | editor.data.upcastDispatcher.on('element:h3', dataViewModelH1Insertion, { priority: 'high' });
|
78 |
|
79 | model.document.registerPostFixer(writer => this._fixTitleContent(writer));
|
80 |
|
81 | model.document.registerPostFixer(writer => this._fixTitleElement(writer));
|
82 |
|
83 | model.document.registerPostFixer(writer => this._fixBodyElement(writer));
|
84 |
|
85 | model.document.registerPostFixer(writer => this._fixExtraParagraph(writer));
|
86 |
|
87 | this._attachPlaceholders();
|
88 |
|
89 | this._attachTabPressHandling();
|
90 | }
|
91 | |
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 | getTitle(options = {}) {
|
104 | const rootName = options.rootName ? options.rootName : undefined;
|
105 | const titleElement = this._getTitleElement(rootName);
|
106 | const titleContentElement = titleElement.getChild(0);
|
107 | return this.editor.data.stringify(titleContentElement, options);
|
108 | }
|
109 | |
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 | getBody(options = {}) {
|
121 | const editor = this.editor;
|
122 | const data = editor.data;
|
123 | const model = editor.model;
|
124 | const rootName = options.rootName ? options.rootName : undefined;
|
125 | const root = editor.model.document.getRoot(rootName);
|
126 | const view = editor.editing.view;
|
127 | const viewWriter = new DowncastWriter(view.document);
|
128 | const rootRange = model.createRangeIn(root);
|
129 | const viewDocumentFragment = viewWriter.createDocumentFragment();
|
130 |
|
131 | const bodyStartPosition = model.createPositionAfter(root.getChild(0));
|
132 | const bodyRange = model.createRange(bodyStartPosition, model.createPositionAt(root, 'end'));
|
133 | const markers = new Map();
|
134 | for (const marker of model.markers) {
|
135 | const intersection = bodyRange.getIntersection(marker.getRange());
|
136 | if (intersection) {
|
137 | markers.set(marker.name, intersection);
|
138 | }
|
139 | }
|
140 |
|
141 | data.mapper.clearBindings();
|
142 | data.mapper.bindElements(root, viewDocumentFragment);
|
143 | data.downcastDispatcher.convert(rootRange, markers, viewWriter, options);
|
144 |
|
145 | viewWriter.remove(viewWriter.createRangeOn(viewDocumentFragment.getChild(0)));
|
146 |
|
147 | return editor.data.processor.toData(viewDocumentFragment);
|
148 | }
|
149 | |
150 |
|
151 |
|
152 | _getTitleElement(rootName) {
|
153 | const root = this.editor.model.document.getRoot(rootName);
|
154 | for (const child of root.getChildren()) {
|
155 | if (isTitle(child)) {
|
156 | return child;
|
157 | }
|
158 | }
|
159 | }
|
160 | |
161 |
|
162 |
|
163 |
|
164 | _fixTitleContent(writer) {
|
165 | let changed = false;
|
166 | for (const rootName of this.editor.model.document.getRootNames()) {
|
167 | const title = this._getTitleElement(rootName);
|
168 |
|
169 |
|
170 | if (!title || title.maxOffset === 1) {
|
171 | continue;
|
172 | }
|
173 | const titleChildren = Array.from(title.getChildren());
|
174 |
|
175 | titleChildren.shift();
|
176 | for (const titleChild of titleChildren) {
|
177 | writer.move(writer.createRangeOn(titleChild), title, 'after');
|
178 | writer.rename(titleChild, 'paragraph');
|
179 | }
|
180 | changed = true;
|
181 | }
|
182 | return changed;
|
183 | }
|
184 | |
185 |
|
186 |
|
187 |
|
188 | _fixTitleElement(writer) {
|
189 | let changed = false;
|
190 | const model = this.editor.model;
|
191 | for (const modelRoot of this.editor.model.document.getRoots()) {
|
192 | const titleElements = Array.from(modelRoot.getChildren()).filter(isTitle);
|
193 | const firstTitleElement = titleElements[0];
|
194 | const firstRootChild = modelRoot.getChild(0);
|
195 |
|
196 | if (firstRootChild.is('element', 'title')) {
|
197 | if (titleElements.length > 1) {
|
198 | fixAdditionalTitleElements(titleElements, writer, model);
|
199 | changed = true;
|
200 | }
|
201 | continue;
|
202 | }
|
203 |
|
204 |
|
205 | if (!firstTitleElement && !titleLikeElements.has(firstRootChild.name)) {
|
206 | const title = writer.createElement('title');
|
207 | writer.insert(title, modelRoot);
|
208 | writer.insertElement('title-content', title);
|
209 | changed = true;
|
210 | continue;
|
211 | }
|
212 | if (titleLikeElements.has(firstRootChild.name)) {
|
213 |
|
214 | changeElementToTitle(firstRootChild, writer, model);
|
215 | }
|
216 | else {
|
217 |
|
218 | writer.move(writer.createRangeOn(firstTitleElement), modelRoot, 0);
|
219 | }
|
220 | fixAdditionalTitleElements(titleElements, writer, model);
|
221 | changed = true;
|
222 | }
|
223 | return changed;
|
224 | }
|
225 | |
226 |
|
227 |
|
228 |
|
229 | _fixBodyElement(writer) {
|
230 | let changed = false;
|
231 | for (const rootName of this.editor.model.document.getRootNames()) {
|
232 | const modelRoot = this.editor.model.document.getRoot(rootName);
|
233 | if (modelRoot.childCount < 2) {
|
234 | const placeholder = writer.createElement('paragraph');
|
235 | writer.insert(placeholder, modelRoot, 1);
|
236 | this._bodyPlaceholder.set(rootName, placeholder);
|
237 | changed = true;
|
238 | }
|
239 | }
|
240 | return changed;
|
241 | }
|
242 | |
243 |
|
244 |
|
245 |
|
246 | _fixExtraParagraph(writer) {
|
247 | let changed = false;
|
248 | for (const rootName of this.editor.model.document.getRootNames()) {
|
249 | const root = this.editor.model.document.getRoot(rootName);
|
250 | const placeholder = this._bodyPlaceholder.get(rootName);
|
251 | if (shouldRemoveLastParagraph(placeholder, root)) {
|
252 | this._bodyPlaceholder.delete(rootName);
|
253 | writer.remove(placeholder);
|
254 | changed = true;
|
255 | }
|
256 | }
|
257 | return changed;
|
258 | }
|
259 | |
260 |
|
261 |
|
262 | _attachPlaceholders() {
|
263 | const editor = this.editor;
|
264 | const t = editor.t;
|
265 | const view = editor.editing.view;
|
266 | const sourceElement = editor.sourceElement;
|
267 | const titlePlaceholder = editor.config.get('title.placeholder') || t('Type your title');
|
268 | const bodyPlaceholder = editor.config.get('placeholder') ||
|
269 | sourceElement && sourceElement.tagName.toLowerCase() === 'textarea' && sourceElement.getAttribute('placeholder') ||
|
270 | t('Type or paste your content here.');
|
271 |
|
272 | editor.editing.downcastDispatcher.on('insert:title-content', (evt, data, conversionApi) => {
|
273 | const element = conversionApi.mapper.toViewElement(data.item);
|
274 | element.placeholder = titlePlaceholder;
|
275 | enablePlaceholder({
|
276 | view,
|
277 | element,
|
278 | keepOnFocus: true
|
279 | });
|
280 | });
|
281 |
|
282 |
|
283 | const bodyViewElements = new Map();
|
284 |
|
285 | view.document.registerPostFixer(writer => {
|
286 | let hasChanged = false;
|
287 | for (const viewRoot of view.document.roots) {
|
288 |
|
289 | if (viewRoot.isEmpty) {
|
290 | continue;
|
291 | }
|
292 |
|
293 | const body = viewRoot.getChild(1);
|
294 | const oldBody = bodyViewElements.get(viewRoot.rootName);
|
295 |
|
296 | if (body !== oldBody) {
|
297 | if (oldBody) {
|
298 | hidePlaceholder(writer, oldBody);
|
299 | writer.removeAttribute('data-placeholder', oldBody);
|
300 | }
|
301 | writer.setAttribute('data-placeholder', bodyPlaceholder, body);
|
302 | bodyViewElements.set(viewRoot.rootName, body);
|
303 | hasChanged = true;
|
304 | }
|
305 |
|
306 |
|
307 | if (needsPlaceholder(body, true) && viewRoot.childCount === 2 && body.name === 'p') {
|
308 | hasChanged = showPlaceholder(writer, body) ? true : hasChanged;
|
309 | }
|
310 | else {
|
311 |
|
312 | hasChanged = hidePlaceholder(writer, body) ? true : hasChanged;
|
313 | }
|
314 | }
|
315 | return hasChanged;
|
316 | });
|
317 | }
|
318 | |
319 |
|
320 |
|
321 | _attachTabPressHandling() {
|
322 | const editor = this.editor;
|
323 | const model = editor.model;
|
324 |
|
325 | editor.keystrokes.set('TAB', (data, cancel) => {
|
326 | model.change(writer => {
|
327 | const selection = model.document.selection;
|
328 | const selectedElements = Array.from(selection.getSelectedBlocks());
|
329 | if (selectedElements.length === 1 && selectedElements[0].is('element', 'title-content')) {
|
330 | const root = selection.getFirstPosition().root;
|
331 | const firstBodyElement = root.getChild(1);
|
332 | writer.setSelection(firstBodyElement, 0);
|
333 | cancel();
|
334 | }
|
335 | });
|
336 | });
|
337 |
|
338 | editor.keystrokes.set('SHIFT + TAB', (data, cancel) => {
|
339 | model.change(writer => {
|
340 | const selection = model.document.selection;
|
341 | if (!selection.isCollapsed) {
|
342 | return;
|
343 | }
|
344 | const selectedElement = first(selection.getSelectedBlocks());
|
345 | const selectionPosition = selection.getFirstPosition();
|
346 | const root = editor.model.document.getRoot(selectionPosition.root.rootName);
|
347 | const title = root.getChild(0);
|
348 | const body = root.getChild(1);
|
349 | if (selectedElement === body && selectionPosition.isAtStart) {
|
350 | writer.setSelection(title.getChild(0), 0);
|
351 | cancel();
|
352 | }
|
353 | });
|
354 | });
|
355 | }
|
356 | }
|
357 |
|
358 |
|
359 |
|
360 |
|
361 |
|
362 |
|
363 |
|
364 |
|
365 | function dataViewModelH1Insertion(evt, data, conversionApi) {
|
366 | const modelCursor = data.modelCursor;
|
367 | const viewItem = data.viewItem;
|
368 | if (!modelCursor.isAtStart || !modelCursor.parent.is('element', '$root')) {
|
369 | return;
|
370 | }
|
371 | if (!conversionApi.consumable.consume(viewItem, { name: true })) {
|
372 | return;
|
373 | }
|
374 | const modelWriter = conversionApi.writer;
|
375 | const title = modelWriter.createElement('title');
|
376 | const titleContent = modelWriter.createElement('title-content');
|
377 | modelWriter.append(titleContent, title);
|
378 | modelWriter.insert(title, modelCursor);
|
379 | conversionApi.convertChildren(viewItem, titleContent);
|
380 | conversionApi.updateConversionResult(title, data);
|
381 | }
|
382 |
|
383 |
|
384 |
|
385 |
|
386 |
|
387 |
|
388 |
|
389 | function mapModelPositionToView(editingView) {
|
390 | return (evt, data) => {
|
391 | const positionParent = data.modelPosition.parent;
|
392 | if (!positionParent.is('element', 'title')) {
|
393 | return;
|
394 | }
|
395 | const modelTitleElement = positionParent.parent;
|
396 | const viewElement = data.mapper.toViewElement(modelTitleElement);
|
397 | data.viewPosition = editingView.createPositionAt(viewElement, 0);
|
398 | evt.stop();
|
399 | };
|
400 | }
|
401 |
|
402 |
|
403 |
|
404 | function isTitle(element) {
|
405 | return element.is('element', 'title');
|
406 | }
|
407 |
|
408 |
|
409 |
|
410 | function changeElementToTitle(element, writer, model) {
|
411 | const title = writer.createElement('title');
|
412 | writer.insert(title, element, 'before');
|
413 | writer.insert(element, title, 0);
|
414 | writer.rename(element, 'title-content');
|
415 | model.schema.removeDisallowedAttributes([element], writer);
|
416 | }
|
417 |
|
418 |
|
419 |
|
420 |
|
421 |
|
422 | function fixAdditionalTitleElements(titleElements, writer, model) {
|
423 | let hasChanged = false;
|
424 | for (const title of titleElements) {
|
425 | if (title.index !== 0) {
|
426 | fixTitleElement(title, writer, model);
|
427 | hasChanged = true;
|
428 | }
|
429 | }
|
430 | return hasChanged;
|
431 | }
|
432 |
|
433 |
|
434 |
|
435 | function fixTitleElement(title, writer, model) {
|
436 | const child = title.getChild(0);
|
437 |
|
438 |
|
439 | if (child.isEmpty) {
|
440 | writer.remove(title);
|
441 | return;
|
442 | }
|
443 | writer.move(writer.createRangeOn(child), title, 'before');
|
444 | writer.rename(child, 'paragraph');
|
445 | writer.remove(title);
|
446 | model.schema.removeDisallowedAttributes([child], writer);
|
447 | }
|
448 |
|
449 |
|
450 |
|
451 |
|
452 | function shouldRemoveLastParagraph(placeholder, root) {
|
453 | if (!placeholder || !placeholder.is('element', 'paragraph') || placeholder.childCount) {
|
454 | return false;
|
455 | }
|
456 | if (root.childCount <= 2 || root.getChild(root.childCount - 1) !== placeholder) {
|
457 | return false;
|
458 | }
|
459 | return true;
|
460 | }
|