index.jsx 1.5 KB
Newer Older
wuhao's avatar
wuhao committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
/* eslint-disable eqeqeq */
import { useModel } from '@umijs/max';
import { Image } from 'antd';
import { useRef,useState  } from 'react';

function Limit({ content, style={} }) {
  const [preview, setpreview] = useState({
    visible: false,
  });
  const containerRef = useRef();

  const handleClick = (e) => {
    if (e.target.tagName !== 'IMG') {
      return;
    }
    let srcarr = [],
      current = 0;
    if (containerRef?.current) {
      const imgTags = containerRef.current.querySelectorAll('img');
      srcarr = Array.from(imgTags)?.map((el, i) => {
        if (el.src === e.target.src) {
          current = i + 1;
        }
        return el.src;
      });
    }
    setpreview((s) => ({
      current: current,
      urls: srcarr,
      visible: true,
    }));
  };

  return (
    <div style={style}>
      <div
        style={{
          display: 'none',
        }}
      >
        <Image.PreviewGroup
          preview={{
            visible: preview?.visible,
            current: preview?.current-1,
            onVisibleChange: (vis) =>
              setpreview((s) => ({
                ...s,
                visible: vis,
              })),
          }}
        >
          {preview?.urls?.map?.((it) => (
            <Image src={it} key={it} />
          ))}
        </Image.PreviewGroup>
      </div>
      <div
        onClick={handleClick}
        ref={containerRef}
        dangerouslySetInnerHTML={{ __html: content }}
        className="limit"
      ></div>
    </div>
  );
}

export default Limit;