react-font-style
Version:
React font style editor
214 lines (173 loc) • 9.13 kB
JavaScript
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _react = require('react');
var React = _interopRequireWildcard(_react);
var _ButtonGroup = require('./ui/ButtonGroup');
var _ButtonGroup2 = _interopRequireDefault(_ButtonGroup);
var _IconButton = require('./ui/IconButton');
var _IconButton2 = _interopRequireDefault(_IconButton);
var _Dropdown = require('./ui/Dropdown');
var _Dropdown2 = _interopRequireDefault(_Dropdown);
var _FontFamilyDropdown = require('./ui/FontFamilyDropdown');
var _FontFamilyDropdown2 = _interopRequireDefault(_FontFamilyDropdown);
var _Palette = require('./Palette');
var _Palette2 = _interopRequireDefault(_Palette);
var _config = require('./config');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
var ReactFontStyle = function (_React$Component) {
_inherits(ReactFontStyle, _React$Component);
function ReactFontStyle(props) {
_classCallCheck(this, ReactFontStyle);
var _this = _possibleConstructorReturn(this, (ReactFontStyle.__proto__ || Object.getPrototypeOf(ReactFontStyle)).call(this, props));
_this.togglePalette = function (show) {
var showPalette = _this.state.showPalette;
var newShowPalette = void 0;
if (show === showPalette) {
newShowPalette = false;
} else {
newShowPalette = show;
}
_this.setState({ showPalette: newShowPalette });
};
_this.handleInlineStyleChange = function (type) {
var activeInlineStyle = _this.state.activeInlineStyle;
activeInlineStyle[type] = !activeInlineStyle[type];
_this.onChange(activeInlineStyle);
};
_this.handleChangeFontSize = function (label) {
_this.onChange({ activeFontSize: label });
};
_this.handleChangeFontFamily = function (label) {
_this.onChange({ activeFontFamily: label });
};
_this.handleLineHeight = function (label) {
_this.onChange({ activeLineHeight: label });
};
_this.handleAlign = function (label) {
_this.onChange({ activeAlign: label });
};
_this.handleColorChange = function (color) {
var showPalette = _this.state.showPalette;
var hex = color.hex;
if (showPalette === 'font') {
_this.onChange({ activeColor: hex });
} else if (showPalette === 'highlight') {
_this.onChange({ activeBackgroundColor: hex });
}
};
_this.onChange = function (updated) {
var newState = _extends({}, _this.state, updated);
var fontStyle = (0, _config.stateToStyle)(newState);
_this.setState(_extends({}, newState, { fontStyle: fontStyle }));
if (_this.props.onChange) {
_this.props.onChange(fontStyle);
}
};
var config = (0, _config.generateConfig)(props.customConfig);
var state = (0, _config.generateConfigState)(config, props.defaultStyle);
_this.state = _extends({}, state, {
showPalette: false,
config: config,
fontStyle: (0, _config.stateToStyle)(_extends({}, state, { config: config }))
});
return _this;
}
_createClass(ReactFontStyle, [{
key: 'render',
value: function render() {
var _this2 = this;
var _state = this.state,
activeInlineStyle = _state.activeInlineStyle,
activeFontSize = _state.activeFontSize,
activeColor = _state.activeColor,
activeBackgroundColor = _state.activeBackgroundColor,
activeLineHeight = _state.activeLineHeight,
activeAlign = _state.activeAlign,
activeFontFamily = _state.activeFontFamily,
showPalette = _state.showPalette,
config = _state.config,
fontStyle = _state.fontStyle;
var _props = this.props,
text = _props.text,
customizedPlugin = _props.customizedPlugin,
textComponent = _props.textComponent;
return React.createElement(
'div',
null,
config.display.includes('INLINE_STYLE_BUTTONS') ? React.createElement(
_ButtonGroup2.default,
null,
config.INLINE_STYLE_BUTTONS.map(function (item) {
return React.createElement(_IconButton2.default, { key: item.type,
iconName: item.type,
isActive: activeInlineStyle[item.type],
onMouseDown: function onMouseDown() {
return _this2.handleInlineStyleChange(item.type);
} });
})
) : null,
config.display.includes('FONT_COLOR_BUTTON') || config.display.includes('FONT_HIGHLIGHT_BUTTON') ? React.createElement(
_ButtonGroup2.default,
null,
config.display.includes('FONT_HIGHLIGHT_BUTTON') ? React.createElement(_IconButton2.default, { iconName: 'mark',
onMouseDown: function onMouseDown() {
return _this2.togglePalette('highlight');
},
isActive: showPalette === 'highlight' }) : null,
config.display.includes('FONT_COLOR_BUTTON') ? React.createElement(_IconButton2.default, { iconName: 'color',
onMouseDown: function onMouseDown() {
return _this2.togglePalette('font');
},
isActive: showPalette === 'font' }) : null
) : null,
config.display.includes('FONT_SIZE_DROPDOWN') ? React.createElement(_Dropdown2.default, { choices: config.FONT_SIZE_DROPDOWN,
selectedKey: activeFontSize,
onChange: this.handleChangeFontSize,
icon: 'font'
}) : null,
config.display.includes('FONT_FAMILY_DROPDOWN') ? React.createElement(_FontFamilyDropdown2.default, { choices: config.FONT_FAMILY_DROPDOWN,
selectedKey: activeFontFamily,
onChange: this.handleChangeFontFamily,
icon: 'font'
}) : null,
config.display.includes('LINE_HEIGHT_DROPDOWN') ? React.createElement(_Dropdown2.default, { choices: config.LINE_HEIGHT_DROPDOWN,
selectedKey: activeLineHeight,
icon: 'lineheight',
onChange: this.handleLineHeight
}) : null,
config.display.includes('ALIGN_DROPDOWN') ? React.createElement(_Dropdown2.default, { choices: config.ALIGN_DROPDOWN,
selectedKey: activeAlign,
icon: activeAlign === 'Align Left' ? 'align-left' : activeAlign === 'Align Center' ? 'align-center' : 'align-right',
onChange: this.handleAlign
}) : null,
customizedPlugin ? customizedPlugin : null,
showPalette ? React.createElement(_Palette2.default, { color: showPalette === 'font' ? activeColor : activeBackgroundColor,
onChange: this.handleColorChange }) : null,
textComponent ? textComponent : React.createElement(
'div',
{ style: { padding: '20px 10px', backgroundColor: '#eee' } },
React.createElement(
'div',
{ style: fontStyle },
text
)
)
);
}
}]);
return ReactFontStyle;
}(React.Component);
ReactFontStyle.defaultProps = {
defaultStyle: {},
text: 'ABCD'
};
exports.default = ReactFontStyle;
module.exports = exports['default'];