118 lines
4.4 KiB
JavaScript
118 lines
4.4 KiB
JavaScript
|
const formatTime = date => {
|
||
|
const year = date.getFullYear()
|
||
|
const month = date.getMonth() + 1
|
||
|
const day = date.getDate()
|
||
|
const hour = date.getHours()
|
||
|
const minute = date.getMinutes()
|
||
|
const second = date.getSeconds()
|
||
|
|
||
|
return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
*
|
||
|
* @param {*} date
|
||
|
*/
|
||
|
const formatDate = date => {
|
||
|
const year = date.getFullYear()
|
||
|
const month = date.getMonth() + 1
|
||
|
const day = date.getDate()
|
||
|
|
||
|
return [year, month, day].map(formatNumber).join('-')
|
||
|
}
|
||
|
|
||
|
const formatNumber = n => {
|
||
|
n = n.toString()
|
||
|
return n[1] ? n : '0' + n
|
||
|
}
|
||
|
|
||
|
//随机生成暖色系
|
||
|
const randomLightColor = () => {
|
||
|
// 暖色系的范围通常是红色和黄色较多,所以红色分量范围从 128 到 255
|
||
|
let red = Math.floor(Math.random() * 128) + 128;
|
||
|
// 绿色分量范围从 64 到 255
|
||
|
let green = Math.floor(Math.random() * 192) + 64;
|
||
|
// 蓝色分量范围从 0 到 128
|
||
|
let blue = Math.floor(Math.random() * 128);
|
||
|
|
||
|
// 将十进制的 RGB 分量转换为十六进制,并确保长度为 2 位
|
||
|
let redHex = red.toString(16).padStart(2, '0');
|
||
|
let greenHex = green.toString(16).padStart(2, '0');
|
||
|
let blueHex = blue.toString(16).padStart(2, '0');
|
||
|
|
||
|
// 组合成十六进制颜色代码
|
||
|
return '#' + redHex + greenHex + blueHex;
|
||
|
}
|
||
|
//随机生成冷色系
|
||
|
const randomDarkColor = () => {
|
||
|
// 冷色系通常蓝色和绿色较多,所以蓝色分量范围从 128 到 255
|
||
|
let blue = Math.floor(Math.random() * 128) + 128;
|
||
|
// 绿色分量范围从 128 到 255
|
||
|
let green = Math.floor(Math.random() * 128) + 128;
|
||
|
// 红色分量范围从 0 到 128
|
||
|
let red = Math.floor(Math.random() * 128);
|
||
|
|
||
|
// 将十进制的 RGB 分量转换为十六进制,并确保长度为 2 位
|
||
|
let redHex = red.toString(16).padStart(2, '0');
|
||
|
let greenHex = green.toString(16).padStart(2, '0');
|
||
|
let blueHex = blue.toString(16).padStart(2, '0');
|
||
|
|
||
|
// 组合成十六进制颜色代码
|
||
|
return '#' + redHex + greenHex + blueHex;
|
||
|
}
|
||
|
|
||
|
const randomRgbColor = () => { // 随机生成RGB颜色
|
||
|
var r = Math.floor(Math.random() * 256); // 随机生成256以内r值
|
||
|
var g = Math.floor(Math.random() * 256); // 随机生成256以内g值
|
||
|
var b = Math.floor(Math.random() * 256); // 随机生成256以内b值
|
||
|
return `rgb(${r},${g},${b})`; // 返回rgb(r,g,b)格式颜色
|
||
|
}
|
||
|
const randomRgbaColor = () => { // 随机生成RGBA颜色
|
||
|
var r = Math.floor(Math.random() * 256); // 随机生成256以内r值
|
||
|
var g = Math.floor(Math.random() * 256); // 随机生成256以内g值
|
||
|
var b = Math.floor(Math.random() * 256); // 随机生成256以内b值
|
||
|
var alpha = Math.random(); // 随机生成1以内a值
|
||
|
return `rgba(${r},${g},${b},${alpha})`; // 返回rgba(r,g,b,a)格式颜色
|
||
|
}
|
||
|
|
||
|
const formatRichText = (html) => {
|
||
|
let newContent = html.replace(/<img(^>)*>/gi, function (match, capture) {
|
||
|
match = match.replace(/style="(^")+"/gi, '').replace(/style='(^')+'/gi, '');
|
||
|
match = match.replace(/width="(^")+"/gi, '').replace(/width='(^')+'/gi, '');
|
||
|
match = match.replace(/height="(^")+"/gi, '').replace(/height='(^')+'/gi, '');
|
||
|
return match;
|
||
|
});
|
||
|
newContent = newContent.replace(/style="(^")+"/gi, function (match, capture) {
|
||
|
match = match.replace(/width:(^;)+;/gi, 'max-width:100%;').replace(/width:(^;)+;/gi, 'max-width:100%;');
|
||
|
return match;
|
||
|
});
|
||
|
newContent = newContent.replace(/<br(^>)*\/>/gi, '');
|
||
|
newContent = newContent.replace(/\<img/gi, '<img style="max-width:100%;height:auto;display:block;margin-top:0;margin-bottom:0;"');
|
||
|
return newContent;
|
||
|
}
|
||
|
/**
|
||
|
* 去除富文本字符串中img的style
|
||
|
* richText 富文本字符串
|
||
|
*/
|
||
|
const removeImgStyleFromRichText = (richText) => {
|
||
|
// 匹配 <img> 标签
|
||
|
let imgTags = richText.match(/<img[^>]*>/g);
|
||
|
if (imgTags) {
|
||
|
imgTags.forEach((imgTag) => {
|
||
|
// 去除 style 属性
|
||
|
let newImgTag = imgTag.replace(/ style="[^"]*"/g, '');
|
||
|
richText = richText.replace(imgTag, newImgTag);
|
||
|
});
|
||
|
}
|
||
|
return richText;
|
||
|
}
|
||
|
module.exports = {
|
||
|
formatTime: formatTime,
|
||
|
formatDate: formatDate,
|
||
|
randomRgb: randomRgbColor,
|
||
|
randomRgba: randomRgbaColor,
|
||
|
randomDark: randomDarkColor,
|
||
|
randomLight: randomLightColor,
|
||
|
formatHtml: formatRichText,
|
||
|
removeImgStyle: removeImgStyleFromRichText
|
||
|
}
|