|
| 1 | +import features from '../../../../feature-manager'; |
| 2 | +import waitFor from '../../../../helpers/wait-for'; |
| 3 | + |
| 4 | +import React from 'react'; |
| 5 | +import { render } from 'react-dom'; |
| 6 | +import { ColorPicker } from 'antd'; |
| 7 | +import $ from 'jquery'; |
| 8 | +import * as pageDetect from 'github-url-detection'; |
| 9 | + |
| 10 | +import './index.scss'; // 需要引入自定义的样式来覆盖antd ColorPicker的默认样式,后面展开说明 |
| 11 | + |
| 12 | +const featureId = features.getFeatureID(import.meta.url); |
| 13 | + |
| 14 | +// const CALENDAR_LEVEL_COLORS = [ |
| 15 | +// '#ebedf0', |
| 16 | +// '#ffedf9', |
| 17 | +// '#ffc3eb', |
| 18 | +// '#ff3ebf', |
| 19 | +// '#c70085', |
| 20 | +// ]; |
| 21 | +// |
| 22 | +// const changeLevelColor = (level: number, color: string) => { |
| 23 | +// const root = document.documentElement; |
| 24 | +// if (level === 0) { |
| 25 | +// root.style.setProperty(`--color-calendar-graph-day-bg`, color); |
| 26 | +// } else { |
| 27 | +// root.style.setProperty(`--color-calendar-graph-day-L${level}-bg`, color); |
| 28 | +// } |
| 29 | +// }; |
| 30 | + |
| 31 | +let colors = ['#ebedf0', '#ffedf9', '#ffc3eb', '#ff3ebf', '#c70085']; |
| 32 | + |
| 33 | +const changeLevelColor = async (level: number, color: string) => { |
| 34 | + const root = document.documentElement; |
| 35 | + if (level === 0) { |
| 36 | + root.style.setProperty(`--color-calendar-graph-day-bg`, color); |
| 37 | + } else { |
| 38 | + root.style.setProperty(`--color-calendar-graph-day-L${level}-bg`, color); |
| 39 | + } |
| 40 | + // Save to storage |
| 41 | + const newColors = [...colors]; |
| 42 | + newColors[level] = color; |
| 43 | + await chrome.storage.local.set({ |
| 44 | + calendar_level_colors: newColors, |
| 45 | + }); |
| 46 | +}; |
| 47 | + |
| 48 | +const replaceLegendToColorPicker = async ( |
| 49 | + level: number, |
| 50 | + defaultColor: string |
| 51 | +) => { |
| 52 | + const legendSelector = `#contribution-graph-legend-level-${level}`; // 选择器selector是用于定位DOM元素的字符串 |
| 53 | + await waitFor(() => $(legendSelector).length > 0); // init函数运行的时候,页面中某些元素不一定已经加载完毕,经过测试,日历图加载时机比较靠后,因此需要waitFor一下,不然后面的操作都是无用的 |
| 54 | + const $legend = $(legendSelector); |
| 55 | + const container = $('<div></div>'); |
| 56 | + render( |
| 57 | + <ColorPicker |
| 58 | + defaultValue={defaultColor} |
| 59 | + size="small" |
| 60 | + onChange={(color, hex) => changeLevelColor(level, hex)} |
| 61 | + />, // 选择新颜色后会调用changeLevelColor改变格子颜色 |
| 62 | + container[0] |
| 63 | + ); // 将React组件渲染为真实的DOM元素 |
| 64 | + $legend.replaceWith(container); // 使用jQuery的replaceWith方法将图例格子替换为ColorPicker |
| 65 | +}; |
| 66 | + |
| 67 | +// const init = async (): Promise<void> => { |
| 68 | +// for (let i = 0; i < CALENDAR_LEVEL_COLORS.length; i++) { |
| 69 | +// changeLevelColor(i, CALENDAR_LEVEL_COLORS[i]); // 初始化时就按照给定的颜色改变日历格子的颜色 |
| 70 | +// await replaceLegendToColorPicker(i, CALENDAR_LEVEL_COLORS[i]); |
| 71 | +// } |
| 72 | +// }; |
| 73 | + |
| 74 | +const init = async (): Promise<void> => { |
| 75 | + // Load colors from storage |
| 76 | + colors = |
| 77 | + (await chrome.storage.local.get('calendar_level_colors'))[ |
| 78 | + // (await localStorage.get('calendar_level_colors'))[ |
| 79 | + 'calendar_level_colors' |
| 80 | + ] || colors; |
| 81 | + |
| 82 | + for (let i = 0; i < colors.length; i++) { |
| 83 | + changeLevelColor(i, colors[i]); |
| 84 | + replaceLegendToColorPicker(i, colors[i]); |
| 85 | + } |
| 86 | +}; |
| 87 | + |
| 88 | +const restore = async () => { |
| 89 | + console.log('restore colorful-calendar'); |
| 90 | +}; |
| 91 | + |
| 92 | +features.add(featureId, { |
| 93 | + asLongAs: [pageDetect.isUserProfile], |
| 94 | + awaitDomReady: false, |
| 95 | + init, |
| 96 | + restore, |
| 97 | +}); |
0 commit comments