This repository has been archived by the owner on Jun 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathutils.js
76 lines (65 loc) · 2.38 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/**
* @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.
*/
/**
* @module image/imagecaption/utils
*/
import ModelElement from '@ckeditor/ckeditor5-engine/src/model/element';
import { attachPlaceholder } from '@ckeditor/ckeditor5-engine/src/view/placeholder';
import { toWidgetEditable } from '@ckeditor/ckeditor5-widget/src/utils';
const captionSymbol = Symbol( 'imageCaption' );
/**
* Returns a function that creates a caption editable element for the given {@link module:engine/view/document~Document}.
*
* @param {module:engine/view/view~View} view
* @param {String} placeholderText The text to be displayed when the caption is empty.
* @return {Function}
*/
export function captionElementCreator( view, placeholderText ) {
return writer => {
const editable = writer.createEditableElement( 'figcaption' );
writer.setCustomProperty( captionSymbol, true, editable );
attachPlaceholder( view, editable, placeholderText );
return toWidgetEditable( editable, writer );
};
}
/**
* Returns `true` if a given view element is the image caption editable.
*
* @param {module:engine/view/element~Element} viewElement
* @return {Boolean}
*/
export function isCaption( viewElement ) {
return !!viewElement.getCustomProperty( captionSymbol );
}
/**
* Returns the caption model element from a given image element. Returns `null` if no caption is found.
*
* @param {module:engine/model/element~Element} imageModelElement
* @return {module:engine/model/element~Element|null}
*/
export function getCaptionFromImage( imageModelElement ) {
for ( const node of imageModelElement.getChildren() ) {
if ( node instanceof ModelElement && node.name == 'caption' ) {
return node;
}
}
return null;
}
/**
* {@link module:engine/view/matcher~Matcher} pattern. Checks if a given element is a `<figcaption>` element that is placed
* inside the image `<figure>` element.
*
* @param {module:engine/view/element~Element} element
* @returns {Object|null} Returns the object accepted by {@link module:engine/view/matcher~Matcher} or `null` if the element
* cannot be matched.
*/
export function matchImageCaption( element ) {
const parent = element.parent;
// Convert only captions for images.
if ( element.name == 'figcaption' && parent && parent.name == 'figure' && parent.hasClass( 'image' ) ) {
return { name: true };
}
return null;
}