// ==UserScript==
// @name BTA Text
// @namespace *://lanhuapp.com/*
// @version 0.5
// @description 根据蓝湖剪切板生成Bta-Text 组件
// @author Bajn
// @match *://lanhuapp.com/*
// @match *://*.iconfont.cn/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=mozilla.org
// @grant none
// @license MIT
// ==/UserScript==
(function () {
"use strict";
const colorMap = {
"#FFFFFF": "grey-1",
"#FAFAFA": "grey-2",
"#F5F5F5": "grey-3",
"#F2F2F2": "grey-4",
"#E0E0E0": "grey-5",
"#C4C4C4": "grey-6",
"#9E9E9E": "grey-7",
"#757575": "grey-8",
"#212121": "grey-9",
"#000000": "grey-10",
"#FAFCFF": "blue-grey-2",
"#F5F7FA": "blue-grey-3",
"#EEF0F5": "blue-grey-4",
"#E1E3E8": "blue-grey-5",
"#B2B7BF": "blue-grey-6",
"#888F99": "blue-grey-7",
"#3B3E45": "blue-grey-8",
"#1F2126": "blue-grey-9",
"#0D0E12": "blue-grey-10",
"#F7F9FC": "primary-1",
"#E6EEFF": "primary-2",
"#CCDEFF": "primary-3",
"#99BDFF": "primary-4",
"#669CFF": "primary-5",
"#337AFF": "primary-6",
"#0055FF": "primary-7",
"#0048D9": "primary-8",
"#003399": "primary-9",
"#002266": "primary-10",
"#000C24": "primary-11",
"#FFFBFB": "error-1",
"#FFECEE": "error-2",
"#FFD9DC": "error-3",
"#FFB3B9": "error-4",
"#FF8D97": "error-5",
"#FF6774": "error-6",
"#FF4252": "error-7",
"#E53B49": "error-8",
"#992731": "error-9",
"#661A20": "error-10",
"#330D10": "error-11",
"#FAFEFC": "success-1",
"#E6FAF0": "success-2",
"#CCF5E2": "success-3",
"#99ECC5": "success-4",
"#66E3A8": "success-5",
"#33DA8B": "success-6",
"#00D16E": "success-7",
"#00BB62": "success-8",
"#007D42": "success-9",
"#00532C": "success-10",
"#002916": "success-11",
"#FFFDFA": "warn-1",
"#FFF5E6": "warn-2",
"#FFECCC": "warn-3",
"#FFDA99": "warn-4",
"#FFC766": "warn-5",
"#FFB533": "warn-6",
"#FFA300": "warn-7",
"#E59200": "warn-8",
"#996100": "warn-9",
"#664100": "warn-10",
"#332000": "warn-11",
};
const toHex = (n) => `${n > 15 ? '' : 0}${n.toString(16)}`;
const toHexString = (colorObj) => {
const { r, g, b, a = 1 } = colorObj;
return `#${toHex(r)}${toHex(g)}${toHex(b)}${a === 1 ? '' : toHex(Math.floor(a * 255))}`;
};
function isRGBColor(color) {
const rgbColorRegex = /^rgba?\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)(?:\s*,\s*(\d+(?:\.\d+)?)\s*)?\)$/i;
const match = color.match(rgbColorRegex);
if (match) {
const [, r, g, b, a] = match;
const alpha = a !== undefined ? parseFloat(a) : 1.0;
return {
r: parseInt(r, 10),
g: parseInt(g, 10),
b: parseInt(b, 10),
a: alpha
};
}
return null;
}
const fontFamily = {
// 粗体
"PingFangSC-Medium, PingFang SC": "--v-font-family-bold",
// 数字字体
"WEMONum-Bold, WEMONum": "--v-font-family-number",
// 普通字体
"PingFangSC-Regular, PingFang SC": "--v-font-family",
};
const BtaTextContent = "";
let style = "";
document.addEventListener("keydown", async function (e) {
if (e.keyCode === 187) {
style = await navigator.clipboard.readText();
const styleObj = format(style);
createProps(styleObj);
}
if (e.keyCode === 189) {
createIcon();
}
});
/** 复制 BTA text start */
function format(str) {
const rows = str.replace(/;|px/gi, "").split("\n");
const styleObj = {};
rows.forEach((row) => {
const sp = row.split(":");
styleObj[sp[0].trim()] = sp[1].trim();
});
return styleObj;
}
function createProps(styleObj) {
const props = {};
Object.keys(styleObj).forEach((attr) => {
const value = styleObj[attr];
if (attr === "font-size") {
props.size = `{${value}}`;
}
if (attr === "font-weight") {
if (Number(value) >= 500 || value === "bold") {
props.bold = "";
}
}
if (attr === "color") {
let newVal = value
const rgbVal = isRGBColor(newVal)
if(rgbVal){
newVal = toHexString({
r:rgbVal.r,
g:rgbVal.b,
b:rgbVal.b,
a:1
})
if(rgbVal.a){
props.colorOpacity = `{${rgbVal.a}}`
}
}
if(!colorMap[newVal]){
alert(newVal+': 不在色卡中')
}
props.color = `"${colorMap[newVal] || newVal}"`;
}
if (attr === "line-height") {
const size = styleObj["font-size"];
const isSingleLine = Number(value) / size <= 1.25;
if (!isSingleLine) {
props.multipleLines = "";
}
}
});
const propsStr = Object.keys(props).map((key) => {
const value = props[key];
if (value === "") {
return `${key}`;
} else {
return `${key}=${props[key]}`;
}
});
const content = document.querySelector(".item_one.item_content");
const btaTextRes = `<BtaText ${propsStr.join(" ")} align="left" >${
content?.innerText || "XXXXXXX"
}</BtaText>`;
console.log(btaTextRes);
navigator.clipboard.writeText(btaTextRes).then((res) => {
displayMessage("success", "BTA TEXT 复制成功。", 1500);
});
}
/** 复制 BTA text end */
async function createIcon() {
const type = await navigator.clipboard.readText();
await navigator.clipboard.writeText(
`<BtaIcon type={"${type.replace(
"icon-",
""
)}"} color={'primary'} size={24}/>`
);
displayMessage("success", "BTA ICON 复制成功。", 1500);
}
/** message 弹窗 **/
function displayMessage(type, data, time) {
var lunbo = document.createElement("div");
if (type == "success") {
lunbo.style.backgroundColor = "rgba(0, 209, 110, 0.9)";
} else if (type == "error") {
lunbo.style.backgroundColor = "#990000";
} else if (type == "info") {
lunbo.style.backgroundColor = " #e6b800";
} else {
console.log("入参type错误");
return;
}
lunbo.id = "lunbo";
lunbo.style.position = "fixed";
lunbo.style.width = "200px";
lunbo.style.height = "60px";
lunbo.style.transform = "translate(-50%, -50%)";
lunbo.style.zIndex = "999999";
lunbo.style.left = "50%";
lunbo.style.top = "25%";
lunbo.style.color = "white";
lunbo.style.fontSize = "16px";
lunbo.style.borderRadius = "20px";
lunbo.style.textAlign = "center";
lunbo.style.lineHeight = "60px";
if (document.getElementById("lunbo") == null) {
document.body.appendChild(lunbo);
lunbo.innerHTML = data;
setTimeout(function () {
document.body.removeChild(lunbo);
}, time);
}
}
})();