All files common.ts

74.29% Statements 52/70
64.47% Branches 49/76
100% Functions 1/1
70.97% Lines 44/62

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  1x 1x                                 1x       35x 35x 35x 35x   4x               4x   1x     1x       1x     8x     8x       8x   11x     11x 3x   11x 1x 1x 1x 1x     1x     10x 1x 1x 1x   9x         9x 9x 9x                                   3x 2x 2x     2x 1x   1x     1x 1x   2x   8x       8x   35x  
import { Shape, Anchor, ShapeType } from "./types";
import { createShape, isClose, setClose } from "./utils";
import * as R from "ramda";
 
type Payload = {
    selected: number;
    shapeList: Shape[];
};
type Action = {
    type: string;
    location?: Anchor;
    shapeType?: ShapeType;
    anchorOrdinal?: number;
    shapeOrdinal?: number;
    newShapeList?: Shape[];
    color?: string;
    data?: any;
};
 
export function reducer(
    payload: Payload,
    action: Action
): Payload {
    const { type, location, newShapeList, shapeOrdinal, shapeType, anchorOrdinal, color, data } = action;
    const { shapeList, selected } = payload;
    const selectedItem = shapeList[selected];
    switch (type) {
    case "CREATE_SHAPE":
        payload = {
            shapeList: [...shapeList, createShape({
                shapeType: shapeType ? shapeType : "line",
                color,
                data
            })],
            selected: shapeList.length
        };
        break;
    case "DELETE_SHAPE":
        Iif (typeof (shapeOrdinal) === "undefined") {
            break;
        }
        payload = {
            shapeList: R.remove(shapeOrdinal, 1, shapeList),
            selected: 0
        };
        break;
 
    case "CHANGE_SELECTED":
        Iif (typeof (shapeOrdinal) === "undefined") {
            break;
        }
        payload = {
            ...payload,
            selected: shapeOrdinal
        };
        break;
    case "PUSH_ANCHOR":
        Iif (!selectedItem || selectedItem.over || !location) {
            break;
        }
        if (selectedItem.anchors.length < 1) {
            selectedItem.anchors.push(location);
        }
        if ((selectedItem.type === "polygon" || selectedItem.type === "sides_polygon") && selectedItem.anchors.length > 3) {
            selectedItem.anchors = setClose(selectedItem.anchors);
            selectedItem.over = isClose(selectedItem.anchors);
            Eif (selectedItem.over) {
                payload = {
                    ...payload
                };
                break;
            }
        }
        if (selectedItem.type === "sides" && selectedItem.anchors.length > 1) {
            selectedItem.over = true;
            payload = { ...payload };
            break;
        }
        Iif (selectedItem.type === "arrow" && selectedItem.anchors.length > 1) {
            selectedItem.over = true;
            payload = { ...payload };
            break;
        }
        selectedItem.anchors.push(location);
        payload = { ...payload };
        break;
 
    case "MOVE_LAST_ANCHOR":
        if (selectedItem && !selectedItem.over && selectedItem.anchors.length > 0) {
            selectedItem.anchors = R.update(-1, location, selectedItem.anchors);
            payload = { ...payload };
        }
        break;
    case "MOVE_ANCHOR":
        if (selectedItem && selectedItem.anchors.length > 0 && typeof anchorOrdinal === "number") {
            selectedItem.anchors = R.update(anchorOrdinal, location, selectedItem.anchors);
            if ((selectedItem.type === "polygon" || selectedItem.type === "sides_polygon") && selectedItem.over && action.anchorOrdinal === selectedItem.anchors.length - 1) {
                selectedItem.anchors = R.update(0, location, selectedItem.anchors);
            }
            payload = { ...payload };
        }
        break;
    case "OVER":
        if (selectedItem && !selectedItem.over && selectedItem.anchors.length > 0) {
            selectedItem.anchors = R.slice(0, -2, selectedItem.anchors);
            Iif ((selectedItem.type === "polygon" || selectedItem.type === "sides_polygon") && selectedItem.anchors.length > 3) {
                selectedItem.anchors = R.update(-1, R.head(selectedItem.anchors), selectedItem.anchors);
            }
            if ((selectedItem.type === "polygon" || selectedItem.type === "sides_polygon") && selectedItem.anchors.length <= 3) {
                break;
            }
            Iif (selectedItem.type === "line" && selectedItem.anchors.length < 2) {
                break;
            }
            selectedItem.over = true;
            payload = { ...payload };
        }
        break;
    case "LOAD":
        payload = {
            selected: !shapeOrdinal || shapeOrdinal < 0 || shapeOrdinal >= newShapeList.length ? 0 : shapeOrdinal,
            shapeList: newShapeList
        };
        break;
    }
    return payload;
}