shape.js
6.21 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
import Colorpicker from './tools/colorpicker';
import Range from './tools/range';
import Submenu from './submenuBase';
import templateHtml from './template/submenu/shape';
import { toInteger, assignmentForDestroy } from '../util';
import { defaultShapeStrokeValus } from '../consts';
const SHAPE_DEFAULT_OPTION = {
stroke: '#ffbb3b',
fill: '',
strokeWidth: 3,
};
/**
* Shape ui class
* @class
* @ignore
*/
class Shape extends Submenu {
constructor(subMenuElement, { locale, makeSvgIcon, menuBarPosition, usageStatistics }) {
super(subMenuElement, {
locale,
name: 'shape',
makeSvgIcon,
menuBarPosition,
templateHtml,
usageStatistics,
});
this.type = null;
this.options = SHAPE_DEFAULT_OPTION;
this._els = {
shapeSelectButton: this.selector('.tie-shape-button'),
shapeColorButton: this.selector('.tie-shape-color-button'),
strokeRange: new Range(
{
slider: this.selector('.tie-stroke-range'),
input: this.selector('.tie-stroke-range-value'),
},
defaultShapeStrokeValus
),
fillColorpicker: new Colorpicker(
this.selector('.tie-color-fill'),
'',
this.toggleDirection,
this.usageStatistics
),
strokeColorpicker: new Colorpicker(
this.selector('.tie-color-stroke'),
'#ffbb3b',
this.toggleDirection,
this.usageStatistics
),
};
this.colorPickerControls.push(this._els.fillColorpicker);
this.colorPickerControls.push(this._els.strokeColorpicker);
}
/**
* Destroys the instance.
*/
destroy() {
this._removeEvent();
this._els.strokeRange.destroy();
this._els.fillColorpicker.destroy();
this._els.strokeColorpicker.destroy();
assignmentForDestroy(this);
}
/**
* Add event for shape
* @param {Object} actions - actions for shape
* @param {Function} actions.changeShape - change shape mode
* @param {Function} actions.setDrawingShape - set dreawing shape
*/
addEvent(actions) {
this.eventHandler.shapeTypeSelected = this._changeShapeHandler.bind(this);
this.actions = actions;
this._els.shapeSelectButton.addEventListener('click', this.eventHandler.shapeTypeSelected);
this._els.strokeRange.on('change', this._changeStrokeRangeHandler.bind(this));
this._els.fillColorpicker.on('change', this._changeFillColorHandler.bind(this));
this._els.strokeColorpicker.on('change', this._changeStrokeColorHandler.bind(this));
this._els.fillColorpicker.on('changeShow', this.colorPickerChangeShow.bind(this));
this._els.strokeColorpicker.on('changeShow', this.colorPickerChangeShow.bind(this));
}
/**
* Remove event
* @private
*/
_removeEvent() {
this._els.shapeSelectButton.removeEventListener('click', this.eventHandler.shapeTypeSelected);
this._els.strokeRange.off();
this._els.fillColorpicker.off();
this._els.strokeColorpicker.off();
}
/**
* Set Shape status
* @param {Object} options - options of shape status
* @param {string} strokeWidth - stroke width
* @param {string} strokeColor - stroke color
* @param {string} fillColor - fill color
*/
setShapeStatus({ strokeWidth, strokeColor, fillColor }) {
this._els.strokeRange.value = strokeWidth;
this._els.strokeColorpicker.color = strokeColor;
this._els.fillColorpicker.color = fillColor;
this.options.stroke = strokeColor;
this.options.fill = fillColor;
this.options.strokeWidth = strokeWidth;
this.actions.setDrawingShape(this.type, { strokeWidth });
}
/**
* Executed when the menu starts.
*/
changeStartMode() {
this.actions.stopDrawingMode();
}
/**
* Returns the menu to its default state.
*/
changeStandbyMode() {
this.type = null;
this.actions.changeSelectableAll(true);
this._els.shapeSelectButton.classList.remove('circle');
this._els.shapeSelectButton.classList.remove('triangle');
this._els.shapeSelectButton.classList.remove('rect');
}
/**
* set range stroke max value
* @param {number} maxValue - expect max value for change
*/
setMaxStrokeValue(maxValue) {
let strokeMaxValue = maxValue;
if (strokeMaxValue <= 0) {
strokeMaxValue = defaultShapeStrokeValus.max;
}
this._els.strokeRange.max = strokeMaxValue;
}
/**
* Set stroke value
* @param {number} value - expect value for strokeRange change
*/
setStrokeValue(value) {
this._els.strokeRange.value = value;
this._els.strokeRange.trigger('change');
}
/**
* Get stroke value
* @returns {number} - stroke range value
*/
getStrokeValue() {
return this._els.strokeRange.value;
}
/**
* Change icon color
* @param {object} event - add button event object
* @private
*/
_changeShapeHandler(event) {
const button = event.target.closest('.tui-image-editor-button');
if (button) {
this.actions.stopDrawingMode();
this.actions.discardSelection();
const shapeType = this.getButtonType(button, ['circle', 'triangle', 'rect']);
if (this.type === shapeType) {
this.changeStandbyMode();
return;
}
this.changeStandbyMode();
this.type = shapeType;
event.currentTarget.classList.add(shapeType);
this.actions.changeSelectableAll(false);
this.actions.modeChange('shape');
}
}
/**
* Change stroke range
* @param {number} value - stroke range value
* @param {boolean} isLast - Is last change
* @private
*/
_changeStrokeRangeHandler(value, isLast) {
this.options.strokeWidth = toInteger(value);
this.actions.changeShape(
{
strokeWidth: value,
},
!isLast
);
this.actions.setDrawingShape(this.type, this.options);
}
/**
* Change shape color
* @param {string} color - fill color
* @private
*/
_changeFillColorHandler(color) {
color = color || 'transparent';
this.options.fill = color;
this.actions.changeShape({
fill: color,
});
}
/**
* Change shape stroke color
* @param {string} color - fill color
* @private
*/
_changeStrokeColorHandler(color) {
color = color || 'transparent';
this.options.stroke = color;
this.actions.changeShape({
stroke: color,
});
}
}
export default Shape;