draw.js
3.85 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
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
164
165
import { assignmentForDestroy, getRgb } from '../util';
import Colorpicker from './tools/colorpicker';
import Range from './tools/range';
import Submenu from './submenuBase';
import templateHtml from './template/submenu/draw';
import { defaultDrawRangeValus } from '../consts';
const DRAW_OPACITY = 0.7;
/**
* Draw ui class
* @class
* @ignore
*/
class Draw extends Submenu {
constructor(subMenuElement, { locale, makeSvgIcon, menuBarPosition, usageStatistics }) {
super(subMenuElement, {
locale,
name: 'draw',
makeSvgIcon,
menuBarPosition,
templateHtml,
usageStatistics,
});
this._els = {
lineSelectButton: this.selector('.tie-draw-line-select-button'),
drawColorPicker: new Colorpicker(
this.selector('.tie-draw-color'),
'#00a9ff',
this.toggleDirection,
this.usageStatistics
),
drawRange: new Range(
{
slider: this.selector('.tie-draw-range'),
input: this.selector('.tie-draw-range-value'),
},
defaultDrawRangeValus
),
};
this.type = null;
this.color = this._els.drawColorPicker.color;
this.width = this._els.drawRange.value;
}
/**
* Destroys the instance.
*/
destroy() {
this._removeEvent();
this._els.drawColorPicker.destroy();
this._els.drawRange.destroy();
assignmentForDestroy(this);
}
/**
* Add event for draw
* @param {Object} actions - actions for crop
* @param {Function} actions.setDrawMode - set draw mode
*/
addEvent(actions) {
this.eventHandler.changeDrawType = this._changeDrawType.bind(this);
this.actions = actions;
this._els.lineSelectButton.addEventListener('click', this.eventHandler.changeDrawType);
this._els.drawColorPicker.on('change', this._changeDrawColor.bind(this));
this._els.drawRange.on('change', this._changeDrawRange.bind(this));
}
/**
* Remove event
* @private
*/
_removeEvent() {
this._els.lineSelectButton.removeEventListener('click', this.eventHandler.changeDrawType);
this._els.drawColorPicker.off();
this._els.drawRange.off();
}
/**
* set draw mode - action runner
*/
setDrawMode() {
this.actions.setDrawMode(this.type, {
width: this.width,
color: getRgb(this.color, DRAW_OPACITY),
});
}
/**
* Returns the menu to its default state.
*/
changeStandbyMode() {
this.type = null;
this.actions.stopDrawingMode();
this.actions.changeSelectableAll(true);
this._els.lineSelectButton.classList.remove('free');
this._els.lineSelectButton.classList.remove('line');
}
/**
* Executed when the menu starts.
*/
changeStartMode() {
this.type = 'free';
this._els.lineSelectButton.classList.add('free');
this.setDrawMode();
}
/**
* Change draw type event
* @param {object} event - line select event
* @private
*/
_changeDrawType(event) {
const button = event.target.closest('.tui-image-editor-button');
if (button) {
const lineType = this.getButtonType(button, ['free', 'line']);
this.actions.discardSelection();
if (this.type === lineType) {
this.changeStandbyMode();
return;
}
this.changeStandbyMode();
this.type = lineType;
this._els.lineSelectButton.classList.add(lineType);
this.setDrawMode();
}
}
/**
* Change drawing color
* @param {string} color - select drawing color
* @private
*/
_changeDrawColor(color) {
this.color = color || 'transparent';
if (!this.type) {
this.changeStartMode();
} else {
this.setDrawMode();
}
}
/**
* Change drawing Range
* @param {number} value - select drawing range
* @private
*/
_changeDrawRange(value) {
this.width = value;
if (!this.type) {
this.changeStartMode();
} else {
this.setDrawMode();
}
}
}
export default Draw;