BTA Text

根据蓝湖剪切板生成Bta-Text 组件

目前为 2024-08-01 提交的版本。查看 最新版本

// ==UserScript==
// @name         BTA Text
// @namespace    *://lanhuapp.com/*
// @version      0.6
// @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 = {
    '--v-color-zlv-1': '#fcfefe',
    '--v-color-zlv-2': '#f2fcfd',
    '--v-color-zlv-3': '#ddf7fa',
    '--v-color-zlv-4': '#bbeff4',
    '--v-color-zlv-5': '#98e6ef',
    '--v-color-zlv-6': '#76dee9',
    '--v-color-zlv-7': '#54d6e4',
    '--v-color-zlv-8': '#4bc0cd',
    '--v-color-zlv-9': '#494242',
    '--v-color-zlv-10': '#22565b',
    '--v-color-zlv-11': '#112b2e',
    '--v-color-zb-1': '#fafbfd',
    '--v-color-zb-2': '#ebf1f7',
    '--v-color-zb-3': '#ccdbea',
    '--v-color-zb-4': '#99b7d5',
    '--v-color-zb-5': '#6692c0',
    '--v-color-zb-6': '#336eab',
    '--v-color-zb-7': '#004287',
    '--v-color-zb-8': '#004287',
    '--v-color-zb-9': '#002c5a',
    '--v-color-zb-10': '#001e3c',
    '--v-color-zb-11': '#000f1e',
    '--v-color-grey-1': '#ffffff',
    '--v-color-grey-2': '#fafafa',
    '--v-color-grey-3': '#f5f5f5',
    '--v-color-grey-4': '#f2f2f2',
    '--v-color-grey-5': '#e0e0e0',
    '--v-color-grey-6': '#c4c4c4',
    '--v-color-grey-7': '#9e9e9e',
    '--v-color-grey-8': '#757575',
    '--v-color-grey-9': '#212121',
    '--v-color-grey-10': '#000000',
    '--v-color-blue-grey-1': '#ffffff',
    '--v-color-blue-grey-2': '#fafcff',
    '--v-color-blue-grey-3': '#f5f7fa',
    '--v-color-blue-grey-4': '#eef0f5',
    '--v-color-blue-grey-5': '#e1e3e8',
    '--v-color-blue-grey-6': '#b2b7bf',
    '--v-color-blue-grey-7': '#888f99',
    '--v-color-blue-grey-8': '#3b3e45',
    '--v-color-blue-grey-9': '#1f2126',
    '--v-color-blue-grey-10': '#0d0e12',
    '--v-color-primary-1': '#f7f9fc',
    '--v-color-primary-2': '#e6eeff',
    '--v-color-primary-3': '#ccdeff',
    '--v-color-primary-4': '#99bdff',
    '--v-color-primary-5': '#669cff',
    '--v-color-primary-6': '#337aff',
    '--v-color-primary-7': '#0055ff',
    '--v-color-primary-8': '#0048d9',
    '--v-color-primary-9': '#003399',
    '--v-color-primary-10': '#002266',
    '--v-color-primary-11': '#000c24',
    '--v-color-error-1': '#fffbfb',
    '--v-color-error-2': '#ffecee',
    '--v-color-error-3': '#ffd9dc',
    '--v-color-error-4': '#ffb3b9',
    '--v-color-error-5': '#ff8d97',
    '--v-color-error-6': '#ff6774',
    '--v-color-error-7': '#ff4252',
    '--v-color-error-8': '#e53b49',
    '--v-color-error-9': '#992731',
    '--v-color-error-10': '#661a20',
    '--v-color-error-11': '#330d10',
    '--v-color-success-1': '#fafefc',
    '--v-color-success-2': '#e6faf0',
    '--v-color-success-3': '#ccf5e2',
    '--v-color-success-4': '#99ecc5',
    '--v-color-success-5': '#66e3a8',
    '--v-color-success-6': '#33da8b',
    '--v-color-success-7': '#00d16e',
    '--v-color-success-8': '#00bb62',
    '--v-color-success-9': '#007d42',
    '--v-color-success-10': '#00532c',
    '--v-color-success-11': '#002916',
    '--v-color-warn-1': '#fffdfa',
    '--v-color-warn-2': '#fff5e6',
    '--v-color-warn-3': '#ffeccc',
    '--v-color-warn-4': '#ffda99',
    '--v-color-warn-5': '#ffc766',
    '--v-color-warn-6': '#ffb533',
    '--v-color-warn-7': '#ffa300',
    '--v-color-warn-8': '#e59200',
    '--v-color-warn-9': '#996100',
    '--v-color-warn-10': '#664100',
    '--v-color-warn-11': '#332000',
    '--v-color-purple-1': '#fefafe',
    '--v-color-purple-2': '#fee9fb',
    '--v-color-purple-3': '#fed3f7',
    '--v-color-purple-4': '#fda7ef',
    '--v-color-purple-5': '#fd7ce7',
    '--v-color-purple-6': '#fc50df',
    '--v-color-purple-7': '#fc25d8',
    '--v-color-purple-8': '#e221c1',
    '--v-color-purple-9': '#971681',
    '--v-color-purple-10': '#640e56',
    '--v-color-purple-11': '#32072b',
  };

  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',
  };

  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 === 48) {
      style = await navigator.clipboard.readText();
      const styleObj = format(style);
      createProps(styleObj, 'tm-text');
    }
    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, tag) {
    const tagName = tag || 'BtaText';
    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 = `<${tagName} ${propsStr.join(' ')} align="left" >${
      content?.innerText || 'XXXXXXX'
    }</${tagName}>`;
    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) {
    let 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);
    }
  }
})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址