DataPartFoundation.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import BaseFoundation, { DefaultAdapter } from "../base/foundation";
  2. import ColorPickerFoundation, { ColorPickerProps } from "./foundation";
  3. import split from "./utils/split";
  4. import { HsvaColor, RgbaColor } from "./interface";
  5. type Value = ColorPickerProps['value']
  6. export interface DataPartBaseProps {
  7. currentColor: Value;
  8. defaultFormat: 'hex' | 'rgba' | 'hsva';
  9. width: number;
  10. alpha?: boolean;
  11. foundation: ColorPickerFoundation;
  12. eyeDropper: boolean
  13. }
  14. export interface DataPartBaseState {
  15. format: 'hex' | 'rgba' | 'hsva';
  16. inputValue: string
  17. }
  18. export interface DataPartAdapter<P = Record<string, any>, S = Record<string, any>> extends DefaultAdapter<P, S> {
  19. getColorPickerFoundation: () => ColorPickerFoundation
  20. }
  21. class DataPartFoundation extends BaseFoundation<DataPartAdapter<DataPartBaseProps, DataPartBaseState>, DataPartBaseProps, DataPartBaseState> {
  22. constructor(adapter: DataPartAdapter<DataPartBaseProps, DataPartBaseState>) {
  23. super({
  24. ...adapter
  25. });
  26. }
  27. getInputValue = () => {
  28. const { currentColor } = this._adapter.getProps();
  29. const { format } = this._adapter.getStates();
  30. const rgba = currentColor.rgba;
  31. const hsva = currentColor.hsva;
  32. const hex = currentColor.hex;
  33. if (format === 'rgba') {
  34. return `${rgba.r},${rgba.g},${rgba.b}`;
  35. } else if (format === 'hsva') {
  36. return `${hsva.h},${hsva.s},${hsva.v}`;
  37. } else {
  38. return hex.slice(0, 7);
  39. }
  40. }
  41. getValueByInputValue = (value: string) => {
  42. const { format } = this.getStates();
  43. if (format === 'rgba') {
  44. const result = split(value, format);
  45. if (result) {
  46. return result as RgbaColor;
  47. }
  48. } else if (format === 'hsva') {
  49. const result = split(value, format);
  50. if (result) {
  51. return result as HsvaColor;
  52. }
  53. } else if (format === 'hex') {
  54. // hack chrome bug, format mismatch with w3c.
  55. if (!value.startsWith('#')) {
  56. value = '#' + value;
  57. }
  58. if (/#[\d\w]{6,8}/.test(value)) {
  59. return value;
  60. }
  61. }
  62. return false;
  63. }
  64. handlePickValueWithStraw = async () => {
  65. const colorPickerFoundation = this._adapter.getColorPickerFoundation();
  66. if (!window['EyeDropper']) {
  67. return;
  68. }
  69. //@ts-ignore
  70. const eyeDropper = new EyeDropper();
  71. try {
  72. const result = await eyeDropper.open();
  73. const color = result['sRGBHex'];
  74. if (color.startsWith("#")) {
  75. colorPickerFoundation.handleChange(color, 'hex');
  76. } else if (color.startsWith('rgba')) {
  77. const rgba = ColorPickerFoundation.rgbaStringToRgba(color);
  78. rgba.a = 1;
  79. colorPickerFoundation.handleChange(rgba, 'rgba');
  80. }
  81. } catch (e) {
  82. }
  83. }
  84. handleInputValueChange = (value: string) => {
  85. this._adapter.setState({ inputValue: value });
  86. }
  87. handleFormatChange = (format: DataPartBaseState['format']) => {
  88. this._adapter.setState({ format });
  89. }
  90. }
  91. export default DataPartFoundation;