|
| 1 | +import { LeetCode, LeetCodeCN } from "leetcode-query"; |
| 2 | +import { Generator } from "../card"; |
| 3 | +import { Item } from "../item"; |
| 4 | +import { Extension } from "../types"; |
| 5 | + |
| 6 | +export async function HeatmapExtension(generator: Generator): Promise<Extension> { |
| 7 | + const pre_counts = new Promise<Record<string, number>>((resolve) => { |
| 8 | + if (generator.config.site === "us") { |
| 9 | + const lc = new LeetCode(); |
| 10 | + lc.once("receive-graphql", async (res) => { |
| 11 | + try { |
| 12 | + const { data } = (await res.json()) as { |
| 13 | + data: { user: { calendar: { calendar: string } } }; |
| 14 | + }; |
| 15 | + resolve(JSON.parse(data.user.calendar.calendar)); |
| 16 | + } catch (e) { |
| 17 | + resolve({}); |
| 18 | + } |
| 19 | + }); |
| 20 | + lc.graphql({ |
| 21 | + operationName: "calendar", |
| 22 | + query: `query calendar($username: String!, $year: Int) { user: matchedUser(username: $username) { calendar: userCalendar(year: $year) { calendar: submissionCalendar } } }`, |
| 23 | + variables: { username: generator.config.username }, |
| 24 | + }); |
| 25 | + } else { |
| 26 | + const lc = new LeetCodeCN(); |
| 27 | + lc.once("receive-graphql", async (res) => { |
| 28 | + try { |
| 29 | + const { data } = (await res.json()) as { |
| 30 | + data: { calendar: { calendar: string } }; |
| 31 | + }; |
| 32 | + resolve(JSON.parse(data.calendar.calendar)); |
| 33 | + } catch (e) { |
| 34 | + resolve({}); |
| 35 | + } |
| 36 | + }); |
| 37 | + lc.graphql( |
| 38 | + { |
| 39 | + operationName: "calendar", |
| 40 | + query: `query calendar($username: String!, $year: Int) { calendar: userCalendar(userSlug: $username, year: $year) { calendar: submissionCalendar } }`, |
| 41 | + variables: { username: generator.config.username }, |
| 42 | + }, |
| 43 | + "/graphql/noj-go/", |
| 44 | + ); |
| 45 | + } |
| 46 | + }); |
| 47 | + |
| 48 | + return async function Heatmap(generator, data, body, styles) { |
| 49 | + if (generator.config.height < 320) { |
| 50 | + generator.config.height = 320; |
| 51 | + } |
| 52 | + |
| 53 | + const counts = await pre_counts; |
| 54 | + const today = Math.floor(Date.now() / 86400_000) * 86400; |
| 55 | + const width = generator.config.width - 40; |
| 56 | + const wrap = +(width / 52).toFixed(2); |
| 57 | + const block = wrap * 0.9; |
| 58 | + |
| 59 | + const extension = new Item("g", { |
| 60 | + id: "ext-heatmap", |
| 61 | + style: { transform: `translate(0px, 200px)` }, |
| 62 | + children: [ |
| 63 | + new Item("line", { |
| 64 | + attr: { x1: 10, y1: 0, x2: generator.config.width - 10, y2: 0 }, |
| 65 | + style: { stroke: "var(--bg-1)", "stroke-width": 1 }, |
| 66 | + }), |
| 67 | + new Item("text", { |
| 68 | + content: "Heatmap (Last 52 Weeks)", |
| 69 | + id: "ext-heatmap-title", |
| 70 | + style: { |
| 71 | + transform: `translate(20px, 20px)`, |
| 72 | + fill: "var(--text-0)", |
| 73 | + opacity: generator.config.animation !== false ? 0 : 1, |
| 74 | + animation: |
| 75 | + generator.config.animation !== false |
| 76 | + ? "fade_in 1 0.3s 1.7s forwards" |
| 77 | + : "", |
| 78 | + }, |
| 79 | + }), |
| 80 | + ], |
| 81 | + }); |
| 82 | + |
| 83 | + const blocks = new Item("g", { |
| 84 | + id: "ext-heatmap-blocks", |
| 85 | + children: [], |
| 86 | + style: { |
| 87 | + transform: `translate(20px, 35px)`, |
| 88 | + opacity: generator.config.animation !== false ? 0 : 1, |
| 89 | + animation: |
| 90 | + generator.config.animation !== false ? "fade_in 1 0.3s 1.9s forwards" : "", |
| 91 | + }, |
| 92 | + }); |
| 93 | + extension.children?.push(blocks); |
| 94 | + |
| 95 | + for (let i = 0; i < 364; i++) { |
| 96 | + const count = counts[today - i * 86400] || 0; |
| 97 | + const opacity = calc_opacity(count); |
| 98 | + |
| 99 | + blocks.children?.push( |
| 100 | + new Item("rect", { |
| 101 | + attr: { |
| 102 | + class: `ext-heatmap-${count}`, |
| 103 | + }, |
| 104 | + style: { |
| 105 | + transform: `translate(${width - wrap * (Math.floor(i / 7) + 1)}px, ${ |
| 106 | + wrap * (6 - (i % 7)) |
| 107 | + }px)`, |
| 108 | + fill: `var(--color-1)`, |
| 109 | + opacity: opacity, |
| 110 | + width: `${block}px`, |
| 111 | + height: `${block}px`, |
| 112 | + stroke: "var(--color-0)", |
| 113 | + "stroke-width": +(i === 0), |
| 114 | + rx: 2, |
| 115 | + }, |
| 116 | + }), |
| 117 | + ); |
| 118 | + } |
| 119 | + |
| 120 | + const from = new Date((today - 86400 * 364) * 1000); |
| 121 | + const to = new Date(today * 1000); |
| 122 | + extension.children?.push( |
| 123 | + new Item("text", { |
| 124 | + content: `${from.getFullYear()}.${from.getMonth() + 1}.${from.getDate()}`, |
| 125 | + id: "ext-heatmap-from", |
| 126 | + style: { |
| 127 | + transform: `translate(20px, 110px)`, |
| 128 | + fill: "var(--text-0)", |
| 129 | + opacity: generator.config.animation !== false ? 0 : 1, |
| 130 | + animation: |
| 131 | + generator.config.animation !== false ? "fade_in 1 0.3s 2.1s forwards" : "", |
| 132 | + "font-size": "10px", |
| 133 | + }, |
| 134 | + }), |
| 135 | + new Item("text", { |
| 136 | + content: `${to.getFullYear()}.${to.getMonth() + 1}.${to.getDate()}`, |
| 137 | + id: "ext-heatmap-to", |
| 138 | + style: { |
| 139 | + transform: `translate(${generator.config.width - 20}px, 110px)`, |
| 140 | + fill: "var(--text-0)", |
| 141 | + opacity: generator.config.animation !== false ? 0 : 1, |
| 142 | + animation: |
| 143 | + generator.config.animation !== false ? "fade_in 1 0.3s 2.1s forwards" : "", |
| 144 | + "font-size": "10px", |
| 145 | + "text-anchor": "end", |
| 146 | + }, |
| 147 | + }), |
| 148 | + ); |
| 149 | + |
| 150 | + body["ext-heatmap"] = () => extension; |
| 151 | + }; |
| 152 | +} |
| 153 | + |
| 154 | +function calc_opacity(count: number, max = 8): number { |
| 155 | + return Math.sin(Math.min(1, (count + 0.5) / max) * Math.PI * 0.5); |
| 156 | +} |
0 commit comments