Skip to content

Commit e320c49

Browse files
committed
Adds support for LTR and RTL toolbar directions
Fixes #33
1 parent 4c8db76 commit e320c49

File tree

5 files changed

+56
-19
lines changed

5 files changed

+56
-19
lines changed

src/core/selection-region.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
CKEDITOR.SELECTION_TOP_TO_BOTTOM = 0;
99
CKEDITOR.SELECTION_BOTTOM_TO_TOP = 1;
10+
CKEDITOR.SELECTION_LEFT_TO_RIGHT = 2;
11+
CKEDITOR.SELECTION_RIGHT_TO_LEFT = 3;
1012

1113
/**
1214
* SelectionRegion utility class which provides metadata about the selection. The metadata may be the start and end

src/ui/yui/src/toolbars/toolbar-add.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ YUI.add('toolbar-add', function(Y) {
44
var Lang = Y.Lang,
55
YNode = Y.Node,
66

7+
ARROW_BOX_CLASSES = [
8+
'alloy-editor-arrow-box-top',
9+
'alloy-editor-arrow-box-bottom',
10+
'alloy-editor-arrow-box-left',
11+
'alloy-editor-arrow-box-right'
12+
],
13+
714
ToolbarAddTrigger,
815

916
/**
@@ -82,15 +89,15 @@ YUI.add('toolbar-add', function(Y) {
8289

8390
boundingBox = this.get('boundingBox');
8491

85-
if (this.get('editor').isSelectionEmpty()) {
86-
direction = CKEDITOR.SELECTION_BOTTOM_TO_TOP;
92+
if (this._isCurrentLineEmpty()) {
93+
direction = CKEDITOR.SELECTION_LEFT_TO_RIGHT;
8794
}
8895

89-
if (direction === CKEDITOR.SELECTION_TOP_TO_BOTTOM) {
90-
boundingBox.replaceClass('alloy-editor-arrow-box-bottom', 'alloy-editor-arrow-box-top');
91-
} else {
92-
boundingBox.replaceClass('alloy-editor-arrow-box-top', 'alloy-editor-arrow-box-bottom');
93-
}
96+
Y.Array.each(ARROW_BOX_CLASSES, function(item) {
97+
boundingBox.removeClass(item);
98+
});
99+
100+
boundingBox.addClass(ARROW_BOX_CLASSES[direction || 0]);
94101

95102
visible = this.get('visible');
96103

src/ui/yui/src/toolbars/toolbar-base.js

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ YUI.add('toolbar-base', function(Y) {
22
'use strict';
33

44
var Lang = Y.Lang,
5-
YArray = Y.Array;
5+
YArray = Y.Array,
6+
7+
EMPTY_LINE_REGEX = /\r?\n/;
68

79
function ToolbarBase() {}
810

@@ -66,6 +68,8 @@ YUI.add('toolbar-base', function(Y) {
6668
boundingBoxNode,
6769
height,
6870
transition,
71+
x,
72+
width,
6973
y;
7074

7175
transition = this.get('transition');
@@ -76,18 +80,23 @@ YUI.add('toolbar-base', function(Y) {
7680
boundingBoxNode = boundingBox.getDOMNode();
7781

7882
height = boundingBoxNode.offsetHeight;
83+
width = boundingBoxNode.offsetWidth;
7984

8085
// Change the original points where the Toolbar should be positioned.
8186
// The X will be the same, but we will extract or add the height of the
8287
// Toolbar to the Y point.
88+
if (direction === CKEDITOR.SELECTION_TOP_TO_BOTTOM || direction === CKEDITOR.SELECTION_BOTTOM_TO_TOP) {
8389

84-
if (direction === CKEDITOR.SELECTION_TOP_TO_BOTTOM) {
85-
y = xy[1] - height;
86-
} else {
87-
y = xy[1] + height;
90+
x = xy[0];
91+
y = (direction === CKEDITOR.SELECTION_TOP_TO_BOTTOM) ? (xy[1] - height) : (xy[1] + height);
92+
93+
} else if (direction === CKEDITOR.SELECTION_LEFT_TO_RIGHT || direction === CKEDITOR.SELECTION_RIGHT_TO_LEFT) {
94+
95+
x = (direction === CKEDITOR.SELECTION_LEFT_TO_RIGHT) ? (xy[0] - width) : (xy[0] + width);
96+
y = xy[1];
8897
}
8998

90-
this.set('xy', [xy[0], y]);
99+
this.set('xy', [x, y]);
91100

92101
transition.left = xy[0] + 'px';
93102
transition.top = xy[1] + 'px';
@@ -142,6 +151,21 @@ YUI.add('toolbar-base', function(Y) {
142151
return 'Button' + buttonName.substring(0, 1).toUpperCase() + buttonName.substring(1);
143152
},
144153

154+
/**
155+
* Detects if the current line is empty
156+
*
157+
* @method _isCurrentLineEmpty
158+
* @protected
159+
* @return {Boolean} True if the current line is empty.
160+
*/
161+
_isCurrentLineEmpty: function() {
162+
var currentLine;
163+
164+
currentLine = this.get('editor').getSelection().getRanges()[0].getCommonAncestor();
165+
166+
return EMPTY_LINE_REGEX.test(currentLine.getText());
167+
},
168+
145169
/**
146170
* Moves the Toolbar to a point in page coordinates. If transition was specified via the
147171
* {{#crossLink "ToolbarBase/transition:attribute"}}{{/crossLink}}, the movement will be

src/ui/yui/src/toolbars/toolbar-image.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ YUI.add('toolbar-image', function(Y) {
127127
if (element) {
128128
region = Y.DOM.region(element.$);
129129

130-
this.showAtPoint(region.left + (region.right - region.left) / 2, region.top);
130+
this.showAtPoint(region.left + (region.right - region.left) / 2, region.top, CKEDITOR.SELECTION_BOTTOM_TO_TOP);
131131
}
132132
},
133133

src/ui/yui/src/toolbars/toolbar-position.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,16 @@ YUI.add('toolbar-position', function(Y) {
9191

9292
gutter = this.get('gutter');
9393

94-
left = left - gutter.left - (bbDOMNode.offsetWidth / 2);
94+
if (direction === CKEDITOR.SELECTION_TOP_TO_BOTTOM || direction === CKEDITOR.SELECTION_BOTTOM_TO_TOP) {
95+
96+
left = left - gutter.left - (bbDOMNode.offsetWidth / 2);
97+
top = (direction === CKEDITOR.SELECTION_TOP_TO_BOTTOM) ? (top + gutter.top) : (top - bbDOMNode.offsetHeight - gutter.top);
98+
99+
} else if (direction === CKEDITOR.SELECTION_LEFT_TO_RIGHT || direction === CKEDITOR.SELECTION_RIGHT_TO_LEFT) {
100+
101+
left = (direction === CKEDITOR.SELECTION_LEFT_TO_RIGHT) ? (left + gutter.left + bbDOMNode.offsetHeight / 2) : (left - 3 * bbDOMNode.offsetHeight / 2 - gutter.left);
102+
top = top - gutter.top - (bbDOMNode.offsetHeight / 2);
95103

96-
if (direction === CKEDITOR.SELECTION_TOP_TO_BOTTOM) {
97-
top = top + gutter.top;
98-
} else {
99-
top = top - bbDOMNode.offsetHeight - gutter.top;
100104
}
101105

102106
return [left, top];

0 commit comments

Comments
 (0)