This repository has been archived by the owner on Jan 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
tinymce.toolbar.js
163 lines (135 loc) · 4 KB
/
tinymce.toolbar.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/* global tinymce */
tinymce.PluginManager.add( 'toolbar', function( editor ) {
var each = tinymce.each,
Factory = tinymce.ui.Factory,
DOM = tinymce.DOM,
settings = editor.settings,
toolbar;
editor.on( 'keyup mouseup nodechange', function() {
if ( editor.selection.isCollapsed() ) {
toolbar.hide();
return;
}
setTimeout( function() {
var element = editor.selection.getNode();
if ( ! editor.selection.isCollapsed() &&
editor.selection.getContent().replace( /<[^>]+>/g, '' ).trim() &&
element.nodeName !== 'IMG' &&
element.nodeName !== 'HR' &&
element.id !== 'wp-title' &&
! editor.wp.getView( element ) ) {
if ( toolbar._visible ) {
toolbar.reposition();
} else {
toolbar.show();
}
} else {
toolbar.hide();
}
}, 50 );
} );
editor.on( 'blur', function() {
toolbar.hide();
} );
editor.on( 'PreInit', function() {
toolbar = Factory.create( {
type: 'floatpanel',
role: 'application',
classes: 'inline-toolbar-grp',
layout: 'stack',
autohide: true,
items: [
{
type: 'toolbar',
layout: 'flow',
items: editor.toolbarItems( settings.inlineToolbar )
}
]
} );
toolbar.on( 'show', function() {
this.reposition();
DOM.addClass( this.getEl(), 'mce-inline-toolbar-active' );
} );
toolbar.on( 'hide', function() {
DOM.removeClass( this.getEl(), 'mce-inline-toolbar-active' );
} );
DOM.bind( window, 'resize', function() {
toolbar.hide();
} );
toolbar.reposition = function() {
var toolbarEl = this.getEl(),
boundary = editor.selection.getRng().getBoundingClientRect(),
boundaryMiddle = ( boundary.left + boundary.right ) / 2,
windowWidth = window.innerWidth,
toolbarWidth, toolbarHalf,
margin = parseInt( DOM.getStyle( toolbarEl, 'margin-bottom', true ), 10),
top, left, className, iFramePos;
toolbarEl.className = ( ' ' + toolbarEl.className + ' ' ).replace( /\smce-arrow-\S+\s/g, ' ' ).slice( 1, -1 );
toolbarWidth = toolbarEl.offsetWidth;
toolbarHalf = toolbarWidth / 2;
if ( boundary.top < toolbarEl.offsetHeight ) {
className = ' mce-arrow-up';
top = boundary.bottom + margin;
} else {
className = ' mce-arrow-down';
top = boundary.top - toolbarEl.offsetHeight - margin;
}
left = boundaryMiddle - toolbarHalf;
iFramePos = DOM.getPos( editor.getContentAreaContainer().querySelector( 'iframe' ) );
top = top + iFramePos.y;
left = left + iFramePos.x;
if ( toolbarWidth >= windowWidth ) {
className += ' mce-arrow-full';
left = 0;
} else if ( ( left < 0 && boundary.left + toolbarWidth > windowWidth ) || ( left + toolbarWidth > windowWidth && boundary.right - toolbarWidth < 0 ) ) {
left = ( windowWidth - toolbarWidth ) / 2;
} else if ( left < 0 ) {
className += ' mce-arrow-left';
left = boundary.left;
left = left + iFramePos.x;
} else if ( left + toolbarWidth > windowWidth ) {
className += ' mce-arrow-right';
left = boundary.right - toolbarWidth;
left = left + iFramePos.x;
}
toolbarEl.className += className;
DOM.setStyles( toolbarEl, { 'left': left, 'top': top } );
return this;
};
toolbar.renderTo( document.body ).hide();
editor.shortcuts.add( 'Alt+F8', '', function() {
var item = toolbar.find( 'toolbar' )[0];
item && item.focus( true );
} );
toolbar.on( 'cancel', function() {
editor.focus();
} );
editor.inlineToolbar = toolbar;
} );
each( {
H1: 'Heading 1',
H2: 'Heading 2',
H3: 'Heading 3',
H4: 'Heading 4',
H5: 'Heading 5',
H6: 'Heading 6',
Pre: 'Preformatted'
}, function( text, name ) {
var nameLower = name.toLowerCase();
editor.addButton( nameLower, {
tooltip: text,
text: name,
onclick: function() {
editor.formatter.toggle( nameLower );
},
onPostRender: function() {
var self = this;
editor.on( 'nodeChange', function( event ) {
each( event.parents, function( node ) {
self.active( !! editor.formatter.matchNode( node, nameLower ) );
} );
} );
}
} );
} );
} );