moved example descriptions into example controls

development
mcyph 2021-03-25 17:12:58 +11:00
parent c2635745ef
commit ce15731a1b
1 changed files with 87 additions and 86 deletions

View File

@ -22,9 +22,7 @@ class Control extends React.Component {
return ( return (
<> <>
<h1>Control</h1> <h1>Control</h1>
This example demonstrates adding This example demonstrates adding controls to specific cells in a graph.
controls to specific cells in a graph.
<div <div
ref={el => { ref={el => {
this.el = el; this.el = el;
@ -46,10 +44,6 @@ class Control extends React.Component {
}; };
componentDidMount = () => { componentDidMount = () => {
// Creates the graph inside the given container
const graph = new mxGraph(this.el);
graph.setPanning(true);
// Specifies the URL and size of the new control // Specifies the URL and size of the new control
const deleteImage = new mxImage( const deleteImage = new mxImage(
'editors/images/overlays/forbidden.png', 'editors/images/overlays/forbidden.png',
@ -57,10 +51,9 @@ class Control extends React.Component {
16 16
); );
// Overridden to add an additional control to the state at creation time class MyCustomCellRenderer extends mxCellRenderer {
const mxCellRendererCreateControl = mxCellRenderer.prototype.createControl; createControl(state) {
mxCellRenderer.prototype.createControl = function(state) { super.createControl(state);
mxCellRendererCreateControl.apply(this, arguments);
const { graph } = state.view; const { graph } = state.view;
@ -87,10 +80,10 @@ class Control extends React.Component {
state.deleteControl.destroy(); state.deleteControl.destroy();
state.deleteControl = null; state.deleteControl = null;
} }
}; }
getDeleteControlBounds(state) {
// Helper function to compute the bounds of the control // Helper function to compute the bounds of the control
const getDeleteControlBounds = function(state) {
if (state.deleteControl != null) { if (state.deleteControl != null) {
const oldScale = state.deleteControl.scale; const oldScale = state.deleteControl.scale;
const w = state.deleteControl.bounds.width / oldScale; const w = state.deleteControl.bounds.width / oldScale;
@ -111,17 +104,15 @@ class Control extends React.Component {
h * s h * s
); );
} }
return null; return null;
}; }
redrawControl(state) {
// Overridden to update the scale and bounds of the control // Overridden to update the scale and bounds of the control
const mxCellRendererRedrawControl = mxCellRenderer.prototype.redrawControl; super.redrawControl(state);
mxCellRenderer.prototype.redrawControl = function(state) {
mxCellRendererRedrawControl.apply(this, arguments);
if (state.deleteControl != null) { if (state.deleteControl != null) {
const bounds = getDeleteControlBounds(state); const bounds = this.getDeleteControlBounds(state);
const s = state.view.scale; const s = state.view.scale;
if ( if (
@ -133,18 +124,28 @@ class Control extends React.Component {
state.deleteControl.redraw(); state.deleteControl.redraw();
} }
} }
}; }
destroy(state) {
// Overridden to remove the control if the state is destroyed // Overridden to remove the control if the state is destroyed
const mxCellRendererDestroy = mxCellRenderer.prototype.destroy; super.destroy(state);
mxCellRenderer.prototype.destroy = function(state) {
mxCellRendererDestroy.apply(this, arguments);
if (state.deleteControl != null) { if (state.deleteControl != null) {
state.deleteControl.destroy(); state.deleteControl.destroy();
state.deleteControl = null; state.deleteControl = null;
} }
}; }
}
class MyCustomGraph extends mxGraph {
createCellRenderer() {
return new MyCustomCellRenderer();
}
}
// Creates the graph inside the given container
const graph = new MyCustomGraph(this.el);
graph.setPanning(true);
// Uncomment the following if you want the container // Uncomment the following if you want the container
// to fit the size of the graph // to fit the size of the graph