-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Expand file tree
/
Copy pathblocks.js
More file actions
91 lines (76 loc) · 2.17 KB
/
blocks.js
File metadata and controls
91 lines (76 loc) · 2.17 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
( function( wp ) {
var _blocks, _controls;
_blocks = {};
_controls = {};
var _elementMap = {};
wp.blocks = {
registerBlock: function( settings ) {
// Note, elements should probably only be registered by core.
// Maybe for each block, we should offer to extend the settings (add buttons).
var namespace = settings.namespace || 'elements';
var id = namespace + ':' + settings.name;
_blocks[ id ] = settings;
_blocks[ id ]._id = id;
if ( settings.elements ) {
settings.elements.forEach( function( element ) {
_elementMap[ element ] = id;
} );
}
},
registerControl: function( name, settings ) {
_controls[ name ] = settings;
},
getBlockSettings: function( name ) {
return _blocks[ name ];
},
getControlSettings: function( name ) {
return _controls[ name ];
},
getBlockSettingsByElement: function( element ) {
var id = element.getAttribute( 'data-wp-block-type' );
if ( ! id ) {
id = _elementMap[ element.nodeName.toLowerCase() ];
}
return this.getBlockSettings( id );
},
getBlocks: function() {
return _blocks;
},
getControls: function() {
return _controls;
},
getSelectedBlocks: function() {
var editor = window.tinyMCE.activeEditor;
var selection = window.getSelection();
var startNode = editor.selection.getStart();
var endNode = editor.selection.getEnd();
var rootNode = editor.getBody();
var blocks = [];
while ( startNode.parentNode !== rootNode ) {
startNode = startNode.parentNode;
}
while ( endNode.parentNode !== rootNode ) {
endNode = endNode.parentNode;
}
if ( startNode.compareDocumentPosition( endNode ) & Node.DOCUMENT_POSITION_FOLLOWING ) {
while ( startNode ) {
blocks.push( startNode );
if ( startNode === endNode ) {
break;
}
startNode = startNode.nextSibling;
}
} else {
blocks.push( startNode );
}
// Handle tripple click selection.
if ( ! selection.isCollapsed && selection.focusOffset === 0 && selection.focusNode === endNode ) {
blocks.pop();
}
return blocks;
},
getSelectedBlock: function() {
return wp.blocks.getSelectedBlocks()[0];
}
};
} )( window.wp = window.wp || {} );