Debian Codegraph @

extensions/VisualEditor/lib/ve/src/ce/ve.ce.NodeFactory.js

raw symbols SCIP
1 /*!
2 * VisualEditor ContentEditable NodeFactory class.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 /**
8 * ContentEditable node factory.
9 *
10 * @class
11 * @extends OO.Factory
12 * @constructor
13 */
14 ve.ce.NodeFactory = function VeCeNodeFactory() {
15 // Parent constructor
16 OO.Factory.call( this );
17 };
18
19 /* Inheritance */
20
21 OO.inheritClass( ve.ce.NodeFactory, OO.Factory );
22
23 /* Methods */
24
25 /**
26 * Get a plain text description of a node model.
27 *
28 * @param {ve.dm.Node} node Node to describe
29 * @return {string} Description of the node
30 * @throws {Error} Unknown node type
31 */
32 ve.ce.NodeFactory.prototype.getDescription = function ( node ) {
33 const type = node.constructor.static.name;
34 if ( Object.prototype.hasOwnProperty.call( this.registry, type ) ) {
35 return this.registry[ type ].static.getDescription( node );
36 }
37 throw new Error( 'Unknown node type: ' + type );
38 };
39
40 /**
41 * Check if a node type splits on Enter
42 *
43 * @param {string} type Node type
44 * @return {boolean} The node can have grandchildren
45 * @throws {Error} Unknown node type
46 */
47 ve.ce.NodeFactory.prototype.splitNodeOnEnter = function ( type ) {
48 if ( Object.prototype.hasOwnProperty.call( this.registry, type ) ) {
49 return this.registry[ type ].static.splitOnEnter;
50 }
51 throw new Error( 'Unknown node type: ' + type );
52 };
53
54 /**
55 * Create a view node from a model node.
56 *
57 * @param {ve.dm.Node} model Mode node
58 * @return {ve.ce.Node} View node
59 * @throws {Error} Unknown object name
60 */
61 ve.ce.NodeFactory.prototype.createFromModel = function ( model ) {
62 let type = model.getType();
63 if ( ve.dm.nodeFactory.isMetaData( type ) ) {
64 // Metadata never has an explicit view representation, so a generic
65 // ve.ce.MetaItem should be fine
66 type = 'meta';
67 }
68 return this.create( type, model );
69 };
70
71 /* Initialization */
72
73 // TODO: Move instantiation to a different file
74 ve.ce.nodeFactory = new ve.ce.NodeFactory();