All files index.tsx

94.29% Statements 66/70
72.09% Branches 31/43
87.5% Functions 14/16
92.45% Lines 49/53

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 1571x 1x 1x 1x     1x 1x   1x   76x 38x   38x 8x     38x 8x     38x 38x   38x 38x 36x 28x 28x 28x 28x       25x 28x 51x 102x 102x 102x       25x 3x 3x 3x                 25x 25x         25x                                                                       42x                                   28x       38x         4x     38x 1x     38x 16x 8x 8x       38x 14x     38x                         1x
import React, { useState, useReducer, useEffect } from "react";
import ReactEcharts from "echarts-for-react";
import * as R from "ramda";
import echarts from "echarts";
 
import { Anchor, Props, Shape, ShapeType } from "./types";
import { chartInitData, getPoint, getSides, getArrow } from "./utils";
import { reducer } from "./common";
 
const SYMBOL_SIZE = 12;
 
const markBoard: React.FC<Props> = ({ onChange, onReady, selected, showGrid = false, value, lineWidth = 2 }: Props) => {
    const [data, dispatch] = useReducer(reducer, { selected: 0, shapeList: value });
 
    useEffect(() => {
        R.equals(data.shapeList, value) || dispatch({ type: "LOAD", newShapeList: value });
    }, [value]);
 
    useEffect(() => {
        dispatch({ type: "CHANGE_SELECTED", shapeOrdinal: selected });
    }, [selected]);
 
    chartInitData.xAxis.splitLine.show = showGrid;
    chartInitData.yAxis.splitLine.show = showGrid;
 
    const [myChart, setMayChart] = useState<any | null>(null);
    useEffect(() => {
        if (myChart) {
            const winRatio = myChart._dom.clientWidth / myChart._dom.clientHeight;
            const { shapeList, selected } = data;
            const selectedShape = shapeList[selected];
            myChart.setOption({
                ...chartInitData,
                series: shapeList.map(
                    (item: Shape, index: number) => {
                        const coordsMapSpacing = coords => {
                            return coords?.map(i=>{
                                return i.map(v=>{
                                    let value = v > 1 ? 1 : v;
                                    value = value < 0 ? 0 : value;
                                    return value;
                                });
                            });
                        };
                        const getMarkLineData = (): object => {
                            const coords = item.anchors[0] ? getSides(item.anchors as [Anchor, Anchor], winRatio) : null;
                            const spacingCoords = coordsMapSpacing(coords);
                            return coords ? [
                                [{
                                    coord: spacingCoords[0]
                                },
                                {
                                    coord: spacingCoords[1]
                                }]
                            ] : null;
                        };
                        const [[startX, startY], [endX, endY]] = item.anchors[0] && item.anchors[1]? item.anchors : [[0,0],[0,0]];
                        const getMarkPointData = (): object => {
                            const coord = item.anchors[0] ? getArrow(item.anchors as [Anchor, Anchor]) : null;
                            return coord ? [{coord}]: null;
                        };
                        
                        return {
                            type: "line",
                            symbolSize: index === selected ? SYMBOL_SIZE : 0,
                            data: coordsMapSpacing(item.anchors),
                            lineStyle: {
                                color: item.color,
                                width: lineWidth,
                            },
                            itemStyle: {
                                color: item.color,
                            },
                            markPoint: item.type === "arrow" && {
                                silent: true,
                                symbol: "triangle",
                                symbolSize: R.max(18, lineWidth * 3.6),
                                symbolRotate: (endX-startX) > 0 ? Math.atan((-endY+startY)/(endX-startX)/winRatio)*180/Math.PI-90 : Math.atan((endY-startY)/(-endX+startX)/winRatio)*180/Math.PI+90,
                                data: getMarkPointData(),
                                animation: false
                            },
                            markLine: (item.type === "sides" || item.type === "sides_polygon") && {
                                silent: true,
                                symbol: ["circle", "triangle"],
                                symbolSize:  R.max(18, lineWidth * 3.6),
                                lineStyle: {
                                    type: "solid",
                                    width: lineWidth,
                                    clip: false,
                                },
                                data: getMarkLineData(),
                                animation: false,
                            },
                            clip: false,
                        };
                    }),
                graphic: selectedShape ? selectedShape.anchors.map(
                    (item: Anchor, dataIndex: number) => {
                        return {
                            type: "circle",
                            position: myChart.convertToPixel("grid", [item[0], item[1]]),
                            shape: { cx: 0, cy: 0, r: SYMBOL_SIZE },
                            invisible: true,
                            draggable: true,
                            ondrag: echarts["util"].curry(function (this: any, dataIndex: number) {
                                const location = myChart.convertFromPixel("grid", this.position);
                                dispatch({
                                    type: "MOVE_ANCHOR",
                                    location: [location[0], location[1]],
                                    anchorOrdinal: dataIndex
                                });
                            }, dataIndex),
                            z: 100
                        };
                    }) : null
            }, true);
            onChange && onChange(data);
        }
    }, [data]);
 
    const createShape = (opt: {
        shapeType: ShapeType;
        color?: string;
        data?: any;
    }): void => {
        dispatch({ type: "CREATE_SHAPE", shapeType: opt.shapeType, color: opt.color, data: opt.data });
    };
 
    const deleteShape = (shapeOrdinal: number): void => {
        dispatch({ type: "DELETE_SHAPE", shapeOrdinal });
    };
 
    useEffect(() => {
        if (myChart) {
            dispatch({ type: "LOAD", newShapeList: value, shapeOrdinal: selected });
            onReady && onReady({ createShape, deleteShape });
        }
    }, [myChart]);
 
    const editAnchor = R.curry((mode: string, location: Anchor) => {
        dispatch({ type: mode, location });
    });
 
    return <div style={{ height: "100%", width: "100%" }}
        onClick={R.compose(editAnchor("PUSH_ANCHOR"), getPoint)}
        onMouseMove={R.compose(editAnchor("MOVE_LAST_ANCHOR"), getPoint)}
        onDoubleClick={R.compose(editAnchor("OVER"), getPoint)}
        data-testid='MarkBoard'
    >
        <ReactEcharts
            onChartReady={setMayChart}
            option={chartInitData}
            style={{ height: "100%", width: "100%" }}
        />
    </div>;
};
export default markBoard;