1 |
|
2 | import * as React from "react";
|
3 | import type { IconProps } from "shared/src/types";
|
4 | import ToolbarIcon, { markAttrs } from "@canner/slate-icon-shared";
|
5 | import addMarkOverwrite from "@canner/slate-helper-mark-addoverwrite";
|
6 | import { haveMarks } from "@canner/slate-util-have";
|
7 | import { getMarkType } from "@canner/slate-util-get";
|
8 | import ColorPicker from "rc-color-picker";
|
9 | import hexRgb from "hex-rgb";
|
10 | import { FONTBGCOLOR } from "@canner/slate-constant/lib/marks";
|
11 | import omit from "lodash.omit";
|
12 | import commonMark from "@canner/slate-editor-renderer/lib/commonMark";
|
13 |
|
14 | import "rc-color-picker/assets/index.css";
|
15 |
|
16 | export const FontBgColorPlugin = opt => {
|
17 | const options = Object.assign(
|
18 | {
|
19 | type: FONTBGCOLOR,
|
20 | tagName: "span",
|
21 | backgroundColor: markAttrs.backgroundColor
|
22 | },
|
23 | opt
|
24 | );
|
25 |
|
26 | return {
|
27 | renderMark: props => {
|
28 | if (props.mark.type === options.type)
|
29 | return commonMark(options.tagName, omit(options, ["type", "tagName"]))(
|
30 | props
|
31 | );
|
32 | }
|
33 | };
|
34 | };
|
35 |
|
36 | export default class fontBgColor extends React.Component<
|
37 | IconProps,
|
38 | { color: Object }
|
39 | > {
|
40 | typeName: string;
|
41 | constructor(props: IconProps) {
|
42 | super(props);
|
43 | this.typeName = this.props.type || FONTBGCOLOR;
|
44 | this.state = {
|
45 | color: {}
|
46 | };
|
47 | }
|
48 |
|
49 | static defaultProps = {
|
50 | colorKey: "color"
|
51 | };
|
52 |
|
53 | onChange = (color: { color: string, alpha: number, open: boolean }) => {
|
54 | let { change, onChange, colorKey } = this.props;
|
55 |
|
56 |
|
57 | color.rgba = `rgba(${hexRgb(color.color, { format: "array" }).join(
|
58 | ","
|
59 | )}, ${color.alpha / 100})`;
|
60 | this.setState({ color });
|
61 | onChange(
|
62 | addMarkOverwrite(change, {
|
63 | type: this.typeName,
|
64 | data: { [colorKey]: color }
|
65 | })
|
66 | );
|
67 | };
|
68 |
|
69 | render() {
|
70 | const { icon, change, ...rest } = this.props;
|
71 | const isActive = haveMarks(change, this.typeName);
|
72 | let colorStyle = {};
|
73 |
|
74 | if (isActive) {
|
75 | const first = getMarkType(change, this.typeName)
|
76 | .first()
|
77 | .get("data");
|
78 | const color = first.get("color");
|
79 | const alpha = first.get("alpha");
|
80 |
|
81 | colorStyle = {
|
82 | fill: color,
|
83 | opacity: alpha
|
84 | };
|
85 | }
|
86 |
|
87 | return (
|
88 | <ColorPicker onChange={this.onChange}>
|
89 | <ToolbarIcon
|
90 | colorStyle={colorStyle}
|
91 | type={this.typeName}
|
92 | icon={icon || "Background"}
|
93 | isActive={isActive}
|
94 | {...rest}
|
95 | />
|
96 | </ColorPicker>
|
97 | );
|
98 | }
|
99 | }
|