PointsBuild前端点位辅助创建工具

这几天写链路图,边写边想写这东西这么消耗体力,就想写个小工具出来能加快效率,但奈何比较懒,想着链路图应该很快能调好,可发现调了一个多小时,线还没开始,麻了!
链路图效果
思考了下,工具写起来应该比较简单,然后花了点时间写了这个PointsBuild,废话不多说,上代码::

import React, { useState, useRef } from 'react';
import Clipboard from 'clipboard';

/**
 * 链路图点位工具
 * @param {number} cartesianMaxY y轴最大值,默认为0,表示DOM定位方式
 * @param {number} width 占位宽度
 * @param {number} height 占位高度
 * @param {url} background 占位背景
 * @param {json} style 占位style
 * @param {bool} notice 是否提示复制信息
 * @param {string} formatter 数据输出格式,使用${x}${y}占位
 * @param {bool} multi 多点连续输出,默认单点输出
 */

const listenerCatch = (obj, e, hander) => {
  if (obj.addEventListener) {
    obj.addEventListener(e, hander, false);
  } else if (obj.attachEvent) {
    e = 'on' + e;
    obj.attachEvent(e, hander);
  }
};
const cancelEvent = (e) => {
  if (e.preventDefault) {
    e.preventDefault();
    e.stopPropagation();
  } else {
    e.returnValue = false;
    e.cancelBubble = true;
  }
};

const PointsBuild = ({
  cartesianMaxY = 0,
  width = 100,
  height = 100,
  background = null,
  style = {},
  notice = true,
  formatter = '',
  multi = false,
}) => {
  const [top, setTop] = useState(0);
  const [left, setLeft] = useState(0);
  const [mesWidth, setWidth] = useState(0);
  const [pointer, points, ref] = [useRef({ x: 0, y: 0 }), useRef(''), useRef(null)];

  const drag = (e) => {
    !pointer.current.x && !pointer.current.y && listenerCatch(document, 'mousemove', mousemove);
    pointer.current = { x: e.pageX, y: e.pageY };
  };

  const mousemove = (e) => {
    cancelEvent(e);
    let [x, y] = [e.pageX, e.pageY];
    if (ref.current.offsetLeft <= 5 && ref.current.offsetTop <= 5) points.current = '';
    setLeft((l) => l + x - pointer.current.x);
    setTop((t) => t + y - pointer.current.y);
    pointer.current = { x: x, y: y };
  };

  const copy = (e) => {
    let text;
    if (formatter) {
      let [x, y] = cartesianMaxY ? [left + width / 2, cartesianMaxY - top - height / 2] : [le
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值