Виджет настраиваемого узла React-диаграмм не обновляется на engine.repaintCanvas () - PullRequest
0 голосов
/ 03 октября 2019

Я скопировал демонстрационный проект реагировать на диаграммы и сделал некоторые изменения в TSCustomNodeWidget.tsx, и когда я вызываю engine.repaintCanvas (), он не обновляет установленные мной значения (тогда как имя DefaultNodeModel делает).

TSCustomNodeModel.ts

import { NodeModel, DefaultPortModel, DefaultNodeModel } from '@projectstorm/react-diagrams';
import { BaseModelOptions } from '@projectstorm/react-canvas-core';

export interface TSCustomNodeModelOptions extends BaseModelOptions {
    color?: string;
    value?: number;
    name?: string;
}

export class TSCustomNodeModel extends DefaultNodeModel {
    color: string;
    name: string;
    value: number;

    constructor(options: TSCustomNodeModelOptions = {}) {
        super({
            ...options,
            type: 'ts-custom-node'
        });
        this.color = options.color || 'red';
        this.value = options.value || 0;
        this.name = options.name || 'name';

        // setup an in and out port
        this.addPort(
            new DefaultPortModel({
                in: true,
                name: 'IN'
            })
        );
        this.addPort(
            new DefaultPortModel({
                in: false,
                name: 'OUT'
            })
        );
    }

    serialize() {
        return {
            ...super.serialize(),
            color: this.color
        };
    }

    deserialize(event:any): void {
        super.deserialize(event);
        this.color = event.data.color;
    }
}

TSCustomNodeFactory.tsx

import * as React from 'react';
import { TSCustomNodeModel } from './TSCustomNodeModel';
import { TSCustomNodeWidget } from './TSCustomNodeWidget';
import { AbstractReactFactory } from '@projectstorm/react-canvas-core';
import { DiagramEngine } from '@projectstorm/react-diagrams-core';

export class TSCustomNodeFactory extends AbstractReactFactory<TSCustomNodeModel, DiagramEngine> {
    constructor() {
        super('ts-custom-node');
    }

    generateModel(initialConfig:any) {
        return new TSCustomNodeModel();
    }

    generateReactWidget(event:any): JSX.Element {
        return <TSCustomNodeWidget engine={this.engine as DiagramEngine} node={event.model} />;
    }
}

TSCustomNodeWidget.tsx

import * as React from 'react';
import { DiagramEngine, PortWidget } from '@projectstorm/react-diagrams-core';
import { TSCustomNodeModel } from './TSCustomNodeModel';



export interface TSCustomNodeWidgetProps {
    node: TSCustomNodeModel;
    engine: DiagramEngine;
}

export interface TSCustomNodeWidgetState {}

export class TSCustomNodeWidget extends React.Component<TSCustomNodeWidgetProps, TSCustomNodeWidgetState> {
    constructor(props: TSCustomNodeWidgetProps) {
        super(props);
        this.state = {};
    }

    render() {
        return (
            <div className="custom-node">
                <PortWidget engine={this.props.engine} port={this.props.node.getPort('IN')}>
                    <div className="circle-port" />
                </PortWidget>
                <PortWidget engine={this.props.engine} port={this.props.node.getPort('OUT')}>
                    <div className="circle-port" />
                </PortWidget>
                <div className="custom-node-color" 
                    style={{ backgroundColor: this.props.node.color }}>
                    {this.props.node.value}
                </div>
            </div>
        );
    }
}

Настройка App.tsx

    // create an instance of the engine with all the defaults
    const engine = createEngine();

    const state = engine.getStateMachine().getCurrentState();
    if (state instanceof DefaultDiagramState) {
        state.dragNewLink.config.allowLooseLinks = false;
    }

    const model = new DiagramModel();
    engine.getNodeFactories().registerFactory(new TSCustomNodeFactory());

    model.setGridSize(5);
    engine.setModel(model);

Добавление узла с помощью кнопки (repaintCanvas работает)

    function addConstNode() {
        const node = new TSCustomNodeModel({
            name: "CONST",
            value: 0
        });
        node.setPosition(50, 50);

        model.addAll(node);
        engine.repaintCanvas();   //This works

        return node;
    }

Обновление узла (repaintCanvas не работает)

currentNode.value = value;
...
engine.repaintCanvas();

...