utils.ts 3.8 KB
export function suffixSize(val){

    if (typeof val === 'number') {
        return `${val}px`
    } else {
        val = String(val)

        if (/^[\d]+$/.test(val)) {
            return `${val}px`
        }

        if (val.indexOf('px') > 0) {
            return val
        }

        return val
    }
}

export function flatten(array) {
    let flattend = [];
    (function flat(array) {
        array.forEach(function (el) {
            if (Array.isArray(el)) flat(el);
            else flattend.push(el);
        });
    })(array);
    return flattend;
}

export const DEFAULT_SIZE = { // 不同类型的默认宽高
    rect: [130, 40],
    ellipse: [100, 50],
    diamond: [140, 50],
    circle: [80, 80],
    triangle: [80, 80],
    star: [80, 80],
    // TAG: 自定义节点 - 自定义属性
    activity: [200, 80],
    control: [130, 40],
}

export function formatPos(option: { x: number, y: number, size: [number, number], shape: string }): { x: number, y: number, size: [number, number], shape } {

    const {x, y} = option
    option = {...option}
    option.size = option.size || DEFAULT_SIZE[option.shape]
    let [width, height] = option.size

    let originPos = {
        x: width / 2,
        y: height / 2,
    }
    let targetPos = {
        x, y
    }

    const grid = 5
    let durx = targetPos.x - originPos.x
    let dury = targetPos.y - originPos.y

    if (durx % grid > grid / 2) {
        durx = Math.ceil(durx / grid) * grid
    } else {
        durx = Math.floor(durx / grid) * grid
    }
    if (dury % grid > grid / 2) {
        dury = Math.ceil(dury / grid) * grid
    } else {
        dury = Math.floor(dury / grid) * grid
    }

    targetPos.x = durx + originPos.x
    targetPos.y = dury + originPos.y

    return {
        ...targetPos,
        size: option.size,
        shape: option.shape,
    }
}
// TAG: 自定义节点 - 挂载额外属性到model上
export function formatNodeModel(model, activityConfig) {
    let {shape, size, activity} = model

    /**
     * {
        "id": "123456",
        "x": 590,
        "y": 100,
        "text": "广告宣传",
        "desc": "通过广告短频宣传",
        "activity": "advertisement", // 自定义节点的类型
        }
     */

    if (!!activity) {
        shape = 'activity'
    }

    model.size = size || DEFAULT_SIZE[shape]

    /**
     * advertisement: {
        text: "广告宣传1",
        desc: "通过广告宣传新品",
        color: "#9283ed",
        img: "https://cdn.ipadbiz.cn/oa/advertisement-node.svg"
        }
     */

    if (!!activity && !!activityConfig[activity]) {
        // 把activity的配置挂到model上
        model.shape = 'activity'
        model.img = activityConfig[activity].img
        model.color = activityConfig[activity].color

    }
}

/**
 * 自定义节点 Control - 挂载额外属性到model上
 *
 * @param {any} model - The node model to format.
 * @param {object} controlConfig - The configuration for the control.
 */
export function formatNodeModel_control(model: any, controlConfig: object) {
    let {shape, size, control} = model

    /**
     * {
        "id": "123456",
        "x": 590,
        "y": 100,
        "text": "广告宣传",
        "desc": "通过广告短频宣传",
        "control": "advertisement", // 自定义节点的类型
        }
     */

    if (!!control) {
        shape = 'control'
    }

    model.size = size || DEFAULT_SIZE[shape]

    /**
     * advertisement: {
        text: "广告宣传1",
        desc: "通过广告宣传新品",
        color: "#9283ed",
        img: "https://cdn.ipadbiz.cn/oa/advertisement-node.svg"
        }
     */

    if (!!control && !!controlConfig[control]) {
        // 把 control 的配置挂到model上
        model.shape = 'control'
        model.img = controlConfig[control].img
        model.color = controlConfig[control].color
    }

}