/
helpers.rtl.js
83 lines (72 loc) · 1.61 KB
/
helpers.rtl.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
'use strict';
var getRtlAdapter = function(rectX, width) {
return {
x: function (x) {
return rectX + rectX + width - x;
},
setWidth: function (w) {
width = w;
},
textAlign: function(align) {
if (align === 'center') {
return align;
}
return align === 'right' ? 'left' : 'right';
},
xPlus: function(x, value) {
return x - value;
},
leftForLtr: function(x, itemWidth) {
return x - itemWidth;
},
};
};
var getLtrAdapter = function() {
return {
x: function (x) {
return x;
},
setWidth: function (w) {
width = w;
},
textAlign: function(align) {
return align;
},
xPlus: function(x, value) {
return x + value;
},
leftForLtr: function(x, _itemWidth) {
return x;
},
};
};
var getAdapter = function(rtl, rectX, width) {
if (rtl) {
return getRtlAdapter(rectX, width);
}
return getLtrAdapter();
};
var overrideTextDirection = function(ctx, direction) {
delete ctx.prevTextDirection;
if (direction === 'ltr' || direction === 'rtl') {
var original = [
ctx.canvas.style.getPropertyValue('direction'),
ctx.canvas.style.getPropertyPriority('direction'),
];
ctx.canvas.style.setProperty('direction', direction, 'important');
ctx.prevTextDirection = original;
}
};
var restoreTextDirection = function(ctx) {
if (ctx.prevTextDirection === undefined) {
return;
}
var original = ctx.prevTextDirection;
delete ctx.prevTextDirection;
ctx.canvas.style.setProperty('direction', original[0], original[1]);
};
module.exports = {
getRtlAdapter: getAdapter,
overrideTextDirection: overrideTextDirection,
restoreTextDirection: restoreTextDirection,
};