feat: 颜色转换工具&文件下载工具&元素滚动到视图工具

This commit is contained in:
baiqi 2023-07-13 17:17:10 +08:00 committed by 刘瑞斌
parent 21e36a1639
commit 7daec0dc0b
2 changed files with 47 additions and 0 deletions

15
frontend/src/utils/dom.ts Normal file
View File

@ -0,0 +1,15 @@
export interface ScrollToViewOptions {
behavior?: 'auto' | 'smooth';
block?: 'start' | 'center' | 'end' | 'nearest';
inline?: 'start' | 'center' | 'end' | 'nearest';
}
export function scrollIntoView(targetRef: HTMLElement | null, options: ScrollToViewOptions = {}) {
const scrollOptions: ScrollToViewOptions = {
behavior: options.behavior || 'smooth',
block: options.block || 'start',
inline: options.inline || 'nearest',
};
targetRef?.scrollIntoView(scrollOptions);
}

View File

@ -56,3 +56,35 @@ export const deepMerge = <T = any>(src: any = {}, target: any = {}): T => {
});
return src;
};
/**
* rgb hex
* @param rgb rgb颜色字符串
* @returns HEX 6
*/
export const rgbToHex = (rgb: string) => {
const matches = rgb.match(/^(\d+),\s*(\d+),\s*(\d+)$/);
if (!matches) {
return rgb;
}
const r = parseInt(matches[1], 10).toString(16).padStart(2, '0');
const g = parseInt(matches[2], 10).toString(16).padStart(2, '0');
const b = parseInt(matches[3], 10).toString(16).padStart(2, '0');
return `#${r}${g}${b}`;
};
/**
*
* @param content
* @param fileName
*/
export const downloadStringFile = (type: string, content: string, fileName: string) => {
const fileContent = `data:${type};charset=utf-8,${encodeURIComponent(content)}`;
const link = document.createElement('a');
link.setAttribute('href', fileContent);
link.setAttribute('download', fileName);
link.style.display = 'none';
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
};