-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add download button to dashboard sankey chart
closes #36
- Loading branch information
Showing
11 changed files
with
170 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
export const getCssVariables = (el: HTMLElement) => { | ||
const variableNames = new Set( | ||
Array.from(document.styleSheets) | ||
.flatMap((v) => Array.from(v.cssRules)) | ||
.filter((rule) => rule instanceof CSSStyleRule) | ||
.flatMap((rule) => Array.from((rule as CSSStyleRule).style)) | ||
.filter((key) => key.startsWith('--')) | ||
); | ||
|
||
// Recursively resolve all variables | ||
const elementStyle = getComputedStyle(el); | ||
|
||
const resolveVariable = (variable: string): string => { | ||
const value = elementStyle.getPropertyValue(variable).trim(); | ||
const innerVariables = value.matchAll(/var\((--[^,)]+)(?:,([^)]+))?\)/g); | ||
let resolved = value; | ||
|
||
for (const [match, name, fallback] of innerVariables) { | ||
resolved = resolved.replace(match, resolveVariable(name) || fallback || ''); | ||
} | ||
|
||
return value; | ||
}; | ||
|
||
return new Map<string, string>([...variableNames].map((name) => [name, resolveVariable(name)])); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
export const downloadFile = (content: string, fileName: string, contentType: string): void => { | ||
downloadBlob(new Blob([content], { type: contentType }), fileName); | ||
}; | ||
|
||
export const downloadBlob = (data: Blob, fileName: string): void => { | ||
const link = document.createElement('a'); | ||
link.style.display = 'none'; | ||
document.body.appendChild(link); | ||
|
||
link.href = URL.createObjectURL(data); | ||
link.download = fileName; | ||
link.click(); | ||
|
||
document.body.removeChild(link); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
export const svgToPNG = (svgString: string, scale = 1) => { | ||
const img = new Image(); | ||
const svg = new Blob([svgString], { type: 'image/svg+xml;charset=utf-8' }); | ||
const url = URL.createObjectURL(svg); | ||
|
||
return new Promise<Blob>((resolve, reject) => { | ||
img.onload = () => { | ||
const canvas = document.createElement('canvas'); | ||
const width = img.width * scale; | ||
const height = img.height * scale; | ||
|
||
canvas.width = width; | ||
canvas.height = height; | ||
|
||
const ctx = canvas.getContext('2d'); | ||
ctx?.drawImage(img, 0, 0, width, height); | ||
URL.revokeObjectURL(url); | ||
|
||
canvas.toBlob((v) => (v ? resolve(v) : reject(new Error('Failed to convert canvas to PNG blob'))), 'image/png'); | ||
}; | ||
|
||
img.onerror = () => reject(new Error('Failed to load image for PNG conversion')); | ||
img.src = url; | ||
}); | ||
}; |