Code Monkey home page Code Monkey logo

react-gojs's Introduction

⚠️ react-gojs is no longer under active development. Now, Northwoods provides an official React integration for GoJS: https://github.com/NorthwoodsSoftware/gojs-react. I will not add new features to this lib, only bug fixes. The migration to the official library is pretty easy. I migrated my example here.

NpmVersion npm Build Status Coverage Status TypeScript

react-gojs

react-gojs is a GoJS React integration.

Install

Install it from npm. It has peer dependencies of react and react-dom, which will have to be installed as well.

npm install --save react-gojs

or:

yarn add react-gojs

Usage

Import GojsDiagram in your React component:

import GojsDiagram from 'react-gojs';

To create a GoJS diagram, just use the GojsDiagram React component:

<GojsDiagram
    diagramId="myDiagramDiv"
    model={this.props.model}
    createDiagram={this.createDiagram}
    className="myDiagram"
    onModelChange={this.modelChangedhandler}
    updateDiagramProps={this.updateDiagramProps}
/>

GojsDiagram is a generic React component which is responsible for rendering and updating (when the model changes) the diagram. The render step is based on the model and the go.Diagram object provided as props. It acts as a go.Diagram wrapper.

GojsDiagram props:

  • diagramId: id required by GoJS.
  • model: generic model containing nodes and links.

Model type: DiagramModel<N extends BaseNodeModel, L extends LinkModel>

Example (Typescript / Javascript):

const model = {
    nodeDataArray: [
        { key: 'Alpha', color: 'lightblue' },
        { key: 'Beta', color: 'orange' },
        { key: 'Gamma', color: 'lightgreen' },
        { key: 'Delta', color: 'pink' },
        { key: 'Omega', color: 'grey' }
    ],
    linkDataArray: [
        { from: 'Alpha', to: 'Beta' },
        { from: 'Alpha', to: 'Gamma' },
        { from: 'Beta', to: 'Delta' },
        { from: 'Gamma', to: 'Omega' }
    ]
};
  • createDiagram: method called by the React component to create the customized GoJS diagram object. It enables you to customize the look and feel.

Typescript example:

const createDiagram = (diagramId: string): Diagram => {
    const $ = go.GraphObject.make;

    const myDiagram: Diagram = $(go.Diagram, diagramId, {
        initialContentAlignment: go.Spot.LeftCenter
    });

    myDiagram.nodeTemplate = $(
        go.Node,
        'Auto',
        $(go.Shape, 'RoundedRectangle', { strokeWidth: 0 }, new go.Binding('fill', 'color')),
        $(go.TextBlock, { margin: 8 }, new go.Binding('text', 'key'))
    );

    return myDiagram;
};

Javascript (ES6) example:

const createDiagram = diagramId => {
    const $ = go.GraphObject.make;

    const myDiagram = $(go.Diagram, diagramId, {
        initialContentAlignment: go.Spot.LeftCenter
    });

    myDiagram.nodeTemplate = $(
        go.Node,
        'Auto',
        $(go.Shape, 'RoundedRectangle', { strokeWidth: 0 }, new go.Binding('fill', 'color')),
        $(go.TextBlock, { margin: 8 }, new go.Binding('text', 'key'))
    );

    return myDiagram;
};
  • className: css applied to the div containing our diagram. You should define at least width and height.

Example:

.myDiagram {
    width: 70%;
    height: 400px;
}
  • onModelChange: the onModelChange event occurs when the diagram model has changed (add/remove nodes/links from the UI). This event is very useful to keep your model (provided as prop) in sync with the diagram.

For example, in a Redux environment, the diagram model should be immutable (and stored in the redux store). The onModelChange handler can dispatch actions to update the model.

  • updateDiagramProps (optional parameter): Method allows to update/modify Diagram properties dynamically once the diagram has been rendered. It gives more control to the user, as it is a user-defined. Basic implementation of the method.

Example 1:

const updateDiagramProps = (myDiagram: Diagram): void => {
	myDiagram.layout = go.GraphObject.make(go.LayeredDigraphLayout, { direction: 90 });

	// User can add more properties here.
};

Example 2:

const updateDiagramProps = (myDiagram: Diagram): void => {
	// Empty method.
};

Examples

  • Typescript: You can find a react / redux / react-gojs example + live demo here.

  • Javascript (ES6): You can find a react / react-gojs example + live demo here.

Contributing

Build and Test

yarn install
yarn build
yarn test

Submit a Pull Request

  1. Fork it!
  2. Create your feature branch: git checkout -b my-new-feature
  3. Fix lint errors: yarn lint
  4. Commit your changes: git commit -am 'Add some feature'
  5. Push to the branch: git push origin my-new-feature
  6. Submit a pull request

License

Apache License V2

react-gojs's People

Contributors

dljcollette avatar fredericcarre avatar julien-molina avatar maurelio1234 avatar sagaritrockz avatar sjoerd-otten-nl avatar soshimozi avatar victorwinberg avatar xcomponentadmin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

react-gojs's Issues

modelChangedEvent Add triggered twice: on drag enter & drop

Hi! Awesome integration!

With a palette and a diagram where allowDrop is true, when the user drags a node from the palette onto the diagram, the add event is triggered twice. Once when the cursor enters the diagram and again on the drop.

function createDiagram(id) {
  const diagram = $(go.Diagram, id, {
    allowDrop: true, // permit accepting drag-and-drops
  })
 //...
}

Edit React GoJs

applyIncrementalJson cannot change Model properties

This error appears with GoJS 2.1.x due to an API change.
As we don't want to do significant developments in the repository, I will downgrade goJS to the latest 2.0.x version.
If you want to use the latest goJS version, just use the official gojs-react library.

GojsDiagram does not render in a pure component

I copied the example project into a new one and put the GojsDiagram into a pure component.

const NormDiagram = ({ model, createDiagram, modelChangeHandler, ...props }) => {

  return <div className="row">
    <Global styles={ css`
      canvas {
        outline: none;
        -webkit-tap-highlight-color: rgba(255, 255, 255, 0); /* mobile webkit */
      }
      ` }
    />
    <div className="col-md-10 col-md-offset-1">
      <GojsDiagram
        css={ css`
          width: 70%;
          flex: 1 1 auto;
          margin: auto;
        ` }
        key="gojsDiagram"
        diagramId="myDiagramDiv"
        model={ model }
        createDiagram={ createDiagram }
        className="myDiagram"
        onModelChange={ modelChangeHandler }
      />
    </div>
  </div>
}

A lot API is missing :(

Event such as click a node, I didn't see it. I think just use GoJS would be fine; however, this repo show a good sample for using gojs.

Issue with setting additional model variables.

I am trying to set additional model state variables but for some reason the additional variables are not carried into the gojs canvas. the variables I'm trying to set are copiesArrays , copiesArrayObject, Below is where i'm trying to set the variables.

render() {
return [

];
}

Multiple links between two nodes not being rendered after init

I'm trying to add multiple links between two nodes after initialisation via:

<GojsDiagram model={myModel} linkKeyProperty="key" />

I do have linkKeyProperty="key" defined as described in #83. My model looks initially looks like this:

{
  nodeDataArray: [{type: "Company", key: "id1"}, {type: "Person", , key: "id2"}],
  linkDataArray: [{from: "id1", key: "id1-id2-worksAt", text: "WorksAt", to: "id2", type: "WorksAt"}]
}

After updating after initialisation, it looks like this:

{
  nodeDataArray: [{type: "Company", key: "id1"}, {type: "Person", , key: "id2"}],
  linkDataArray: [{from: "id1", key: "id1-id2-worksAt", to: "id2", type: "WorksAt"}, {from: "id1", key: "id1-id2-buysAt", to: "id2", type: "BuysAt"}]
}

I was expecting that the diagram would render the second node but it does only do that when it was already included on initial render.

It did find applyAddRemoveLinksFromModel in GojsDiagram.tsx is checking for el.from === e.from && el.to === e.to, which means that it will not return a second link added to the model which has the same from and to as and existing link. A possible fix could be checking for the unique identifier linkKeyProperty, like el.from === e.from && el.to === e.to && el[this.props.linkKeyProperty] === e[this.props.linkKeyProperty]. The array linksToAdd now includes an object representing a second link added after initialisation. However, it still doesn't render in the diagram after calling (this.myDiagram.model as GojsModel).addLinkDataCollection(linksToAdd); I can't look for a possible bug in the source of this file since it's minified.

Could you confirm that after (this.myDiagram.model as GojsModel).addLinkDataCollection(linksToAdd); the diagram should render the new link? What could be the reason that the second link is not being rendered?

Migrate from travis to circleci

We want to use the same CI on every project (open source and internally).
As we mainly use circleci, we want to migrate this project to circleci

Links doesn't get updated after model changes

I was playing with model binding properties to change the look and feeling about the nodes and edges and i realise that for nodes is working but it isn't for links.
Any idea why it isn't my links updating?

Check https://codesandbox.io/embed/kxq6zx47y7 a way to reproduce this issue. Just click into the button to generate a random look and feeling for all nodes and links.

Edit: added same example in vanilla gojs(https://codesandbox.io/embed/9opn15521w), but as you could see it is working. So something related with how model links get updates.
Edit2: after checking more in deep how this lib works i notice that maybe it could be a gojs problem, check following example(https://codesandbox.io/embed/5j9jwyo4k) into Diagram.js method refreshModel, i use applyIncrementalJson method and then you see that links somehow are not updated

modelData

It doesn't look like modelData is supported within the model object. Any chance this is a priority in the near future?

Add an onModelChange event

Add a callback to notify when the diagram model has changed internally.
This callback is very useful to keep your model (provided as prop) in sync with the diagram (especially in designer perspective)

How to access the created diagram object

I want to have buttons to control the zoom levels of my canvas.
I want to call mydiagram.commandHandler.increaseZoom() to achieve that. How can I access the diagram created in this case?

ModelChangeEvent not being recognised on importing diagram from json file

I m importing diagram from a json file, after which ModelChangeEvent is not being recognised.

 // import data from json file 
    public importFromFile = () => {
        // Make a request for data from file
        axios.get('/file.json' )
            .then((response) => {
                const newModel = new go.GraphLinksModel(response.data.stages, response.data.links);
                const newDiagram = this.state.diagram;
                newDiagram.model = newModel;
                this.setState({
                    diagram: newDiagram
                });
            })
            .catch((error) => {
                console.log(error);
            });
    }

I cloned react / redux / react-gojs example.
Rest of the code works fine If i don’t upload diagram from file and just add a node the diagram it recognizes
ModelChangeEvent.

Fix npm ignore

Some techincal files should not be included in our npm package.
For example, .prettierrc, travis.yml etc...

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.