Debian Codegraph @

extensions/VisualEditor/lib/ve/src/ce/nodes/ve.ce.HeadingNode.js

raw symbols SCIP
1 /*!
2 * VisualEditor ContentEditable HeadingNode class.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 /**
8 * ContentEditable heading node.
9 *
10 * @class
11 * @extends ve.ce.ContentBranchNode
12 * @constructor
13 * @param {ve.dm.HeadingNode} model Model to observe
14 * @param {Object} [config] Configuration options
15 */
16 ve.ce.HeadingNode = function VeCeHeadingNode() {
17 // Parent constructor
18 ve.ce.HeadingNode.super.apply( this, arguments );
19
20 // Events
21 this.model.connect( this, { update: 'onUpdate' } );
22 };
23
24 /* Inheritance */
25
26 OO.inheritClass( ve.ce.HeadingNode, ve.ce.ContentBranchNode );
27
28 /* Static Properties */
29
30 ve.ce.HeadingNode.static.name = 'heading';
31
32 /* Methods */
33
34 /**
35 * @inheritdoc
36 */
37 ve.ce.HeadingNode.prototype.initialize = function () {
38 ve.ce.HeadingNode.super.prototype.initialize.call( this );
39 this.$element.addClass( 've-ce-headingNode' );
40 };
41
42 /**
43 * Get the HTML tag name.
44 *
45 * Tag name is selected based on the model's level attribute.
46 *
47 * @return {string} HTML tag name
48 * @throws {Error} If level is invalid
49 */
50 ve.ce.HeadingNode.prototype.getTagName = function () {
51 const level = this.model.getAttribute( 'level' ),
52 types = { 1: 'h1', 2: 'h2', 3: 'h3', 4: 'h4', 5: 'h5', 6: 'h6' };
53
54 if ( !Object.prototype.hasOwnProperty.call( types, level ) ) {
55 throw new Error( 'Invalid level' );
56 }
57 return types[ level ];
58 };
59
60 /**
61 * Handle model update events.
62 *
63 * If the level changed since last update the DOM wrapper will be replaced with an appropriate one.
64 */
65 ve.ce.HeadingNode.prototype.onUpdate = function () {
66 this.updateTagName();
67 };
68
69 /* Registration */
70
71 ve.ce.nodeFactory.register( ve.ce.HeadingNode );