UNPKG

7.75 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = exports.FontBgColorPlugin = void 0;
7
8var React = _interopRequireWildcard(require("react"));
9
10var _slateIconShared = _interopRequireWildcard(require("@canner/slate-icon-shared"));
11
12var _slateHelperMarkAddoverwrite = _interopRequireDefault(require("@canner/slate-helper-mark-addoverwrite"));
13
14var _slateUtilHave = require("@canner/slate-util-have");
15
16var _slateUtilGet = require("@canner/slate-util-get");
17
18var _rcColorPicker = _interopRequireDefault(require("rc-color-picker"));
19
20var _hexRgb = _interopRequireDefault(require("hex-rgb"));
21
22var _marks = require("@canner/slate-constant/lib/marks");
23
24var _lodash = _interopRequireDefault(require("lodash.omit"));
25
26var _commonMark = _interopRequireDefault(require("@canner/slate-editor-renderer/lib/commonMark"));
27
28require("rc-color-picker/assets/index.css");
29
30function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
31
32function _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)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }
33
34function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
35
36function _extends() { _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; }; return _extends.apply(this, arguments); }
37
38function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
39
40function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
41
42function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
43
44function _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); } }
45
46function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
47
48function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
49
50function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
51
52function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
53
54function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
55
56function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
57
58function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
59
60var FontBgColorPlugin = function FontBgColorPlugin(opt) {
61 var options = Object.assign({
62 type: _marks.FONTBGCOLOR,
63 tagName: "span",
64 backgroundColor: _slateIconShared.markAttrs.backgroundColor
65 }, opt);
66 return {
67 renderMark: function renderMark(props) {
68 if (props.mark.type === options.type) return (0, _commonMark.default)(options.tagName, (0, _lodash.default)(options, ["type", "tagName"]))(props);
69 }
70 };
71};
72
73exports.FontBgColorPlugin = FontBgColorPlugin;
74
75var fontBgColor =
76/*#__PURE__*/
77function (_React$Component) {
78 _inherits(fontBgColor, _React$Component);
79
80 function fontBgColor(props) {
81 var _this;
82
83 _classCallCheck(this, fontBgColor);
84
85 _this = _possibleConstructorReturn(this, _getPrototypeOf(fontBgColor).call(this, props));
86
87 _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "typeName", void 0);
88
89 _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "onChange", function (color) {
90 var _this$props = _this.props,
91 change = _this$props.change,
92 onChange = _this$props.onChange,
93 colorKey = _this$props.colorKey; // $FlowFixMe
94
95 color.rgba = "rgba(".concat((0, _hexRgb.default)(color.color, {
96 format: "array"
97 }).join(","), ", ").concat(color.alpha / 100, ")");
98
99 _this.setState({
100 color: color
101 });
102
103 onChange((0, _slateHelperMarkAddoverwrite.default)(change, {
104 type: _this.typeName,
105 data: _defineProperty({}, colorKey, color)
106 }));
107 });
108
109 _this.typeName = _this.props.type || _marks.FONTBGCOLOR;
110 _this.state = {
111 color: {}
112 };
113 return _this;
114 }
115
116 _createClass(fontBgColor, [{
117 key: "render",
118 value: function render() {
119 var _this$props2 = this.props,
120 icon = _this$props2.icon,
121 change = _this$props2.change,
122 rest = _objectWithoutProperties(_this$props2, ["icon", "change"]);
123
124 var isActive = (0, _slateUtilHave.haveMarks)(change, this.typeName);
125 var colorStyle = {};
126
127 if (isActive) {
128 var first = (0, _slateUtilGet.getMarkType)(change, this.typeName).first().get("data");
129 var color = first.get("color");
130 var alpha = first.get("alpha");
131 colorStyle = {
132 fill: color,
133 opacity: alpha
134 };
135 }
136
137 return React.createElement(_rcColorPicker.default, {
138 onChange: this.onChange
139 }, React.createElement(_slateIconShared.default, _extends({
140 colorStyle: colorStyle,
141 type: this.typeName,
142 icon: icon || "Background",
143 isActive: isActive
144 }, rest)));
145 }
146 }]);
147
148 return fontBgColor;
149}(React.Component);
150
151exports.default = fontBgColor;
152
153_defineProperty(fontBgColor, "defaultProps", {
154 colorKey: "color"
155});
\No newline at end of file