Spaces:
Running
Running
File size: 4,903 Bytes
b456468 | 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 | import forEach from 'tui-code-snippet/collection/forEach';
import Colorpicker from '@/ui/tools/colorpicker';
import Submenu from '@/ui/submenuBase';
import templateHtml from '@/ui/template/submenu/icon';
import { isSupportFileApi, assignmentForDestroy } from '@/util';
import { defaultIconPath, eventNames, selectorNames } from '@/consts';
/**
* Icon ui class
* @class
* @ignore
*/
class Icon extends Submenu {
constructor(subMenuElement, { locale, makeSvgIcon, menuBarPosition, usageStatistics }) {
super(subMenuElement, {
locale,
name: 'icon',
makeSvgIcon,
menuBarPosition,
templateHtml,
usageStatistics,
});
this.iconType = null;
this._iconMap = {};
this._els = {
registerIconButton: this.selector('.tie-icon-image-file'),
addIconButton: this.selector('.tie-icon-add-button'),
iconColorpicker: new Colorpicker(this.selector('.tie-icon-color'), {
defaultColor: '#ffbb3b',
toggleDirection: this.toggleDirection,
usageStatistics: this.usageStatistics,
}),
};
this.colorPickerInputBox = this._els.iconColorpicker.colorpickerElement.querySelector(
selectorNames.COLOR_PICKER_INPUT_BOX
);
}
/**
* Destroys the instance.
*/
destroy() {
this._removeEvent();
this._els.iconColorpicker.destroy();
assignmentForDestroy(this);
}
/**
* Add event for icon
* @param {Object} actions - actions for icon
* @param {Function} actions.registerCustomIcon - register icon
* @param {Function} actions.addIcon - add icon
* @param {Function} actions.changeColor - change icon color
*/
addEvent(actions) {
const registerIcon = this._registerIconHandler.bind(this);
const addIcon = this._addIconHandler.bind(this);
this.eventHandler = {
registerIcon,
addIcon,
};
this.actions = actions;
this._els.iconColorpicker.on('change', this._changeColorHandler.bind(this));
this._els.registerIconButton.addEventListener('change', registerIcon);
this._els.addIconButton.addEventListener('click', addIcon);
this.colorPickerInputBox.addEventListener(
eventNames.FOCUS,
this._onStartEditingInputBox.bind(this)
);
this.colorPickerInputBox.addEventListener(
eventNames.BLUR,
this._onStopEditingInputBox.bind(this)
);
}
/**
* Remove event
* @private
*/
_removeEvent() {
this._els.iconColorpicker.off();
this._els.registerIconButton.removeEventListener('change', this.eventHandler.registerIcon);
this._els.addIconButton.removeEventListener('click', this.eventHandler.addIcon);
this.colorPickerInputBox.removeEventListener(
eventNames.FOCUS,
this._onStartEditingInputBox.bind(this)
);
this.colorPickerInputBox.removeEventListener(
eventNames.BLUR,
this._onStopEditingInputBox.bind(this)
);
}
/**
* Clear icon type
*/
clearIconType() {
this._els.addIconButton.classList.remove(this.iconType);
this.iconType = null;
}
/**
* Register default icon
*/
registerDefaultIcon() {
forEach(defaultIconPath, (path, type) => {
this.actions.registerDefaultIcons(type, path);
});
}
/**
* Set icon picker color
* @param {string} iconColor - rgb color string
*/
setIconPickerColor(iconColor) {
this._els.iconColorpicker.color = iconColor;
}
/**
* Returns the menu to its default state.
*/
changeStandbyMode() {
this.clearIconType();
this.actions.cancelAddIcon();
}
/**
* Change icon color
* @param {string} color - color for change
* @private
*/
_changeColorHandler(color) {
color = color || 'transparent';
this.actions.changeColor(color);
}
/**
* Change icon color
* @param {object} event - add button event object
* @private
*/
_addIconHandler(event) {
const button = event.target.closest('.tui-image-editor-button');
if (button) {
const iconType = button.getAttribute('data-icontype');
const iconColor = this._els.iconColorpicker.color;
this.actions.discardSelection();
this.actions.changeSelectableAll(false);
this._els.addIconButton.classList.remove(this.iconType);
this._els.addIconButton.classList.add(iconType);
if (this.iconType === iconType) {
this.changeStandbyMode();
} else {
this.actions.addIcon(iconType, iconColor);
this.iconType = iconType;
}
}
}
/**
* register icon
* @param {object} event - file change event object
* @private
*/
_registerIconHandler(event) {
let imgUrl;
if (!isSupportFileApi) {
alert('This browser does not support file-api');
}
const [file] = event.target.files;
if (file) {
imgUrl = URL.createObjectURL(file);
this.actions.registerCustomIcon(imgUrl, file);
}
}
}
export default Icon;
|