Inline Text Editing + AutoSize

by Roman Bruckner

HTML

<html>
<head>
    <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/jointjs/3.7.6/joint.css" />
</head>
<body>
      <!-- content -->
      <div id="paper"></div>

      <!-- dependencies -->
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.20/lodash.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.4.0/backbone.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jointjs/3.7.6/joint.js"></script>
    
    </body>
</html>

JavaScript

var graph = new joint.dia.Graph({}, {
  cellNamespace: joint.shapes
});

const paper = new joint.dia.Paper({
  el: document.getElementById('paper'),
  width: 800,
  height: 900,
  model: graph,
  cellViewNamespace: joint.shapes,
  preventDefaultBlankAction: false,
});

var el1 = new joint.shapes.standard.Rectangle({
  position: {
    x: 10,
    y: 10
  },
  size: {
    width: 100,
    height: 100
  },
  attrs: {
    label: {
      text: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
      fill: 'white',
      fontSize: 14,
      fontFamily: 'sans-serif',
      lineHeight: 18,      
      textWrap: {
        ellipsis: true,
        width: -10,
        height: -10,
      }
    },
    body: {
      fill: 'gray'
    }
  }
});
var el2 = new joint.shapes.standard.Rectangle({
  position: {
    x: 100,
    y: 200
  },
  size: {
    width: 100,
    height: 100
  },
  attrs: {
    label: {
      text: 'Curabitur maximus sagittis justo quis imperdiet.',
      fill: 'yellow',
      fontSize: 14,
      fontFamily: 'sans-serif',
      lineHeight: 18,
      textWrap: {
        ellipsis: true,
        width: -10,
        height: -10
      }
    },
    body: {
      fill: 'blue'
    }
  }
});

var l1 = new joint.shapes.standard.Link({
  source: {
    id: el1.id
  },
  target: {
    id: el2.id
  }
});

graph.addCells([el1, el2, l1]);

paper.on('element:pointerdblclick', function(elementView) {

  const element = elementView.model;
  const bbox = element.getBBox();
  const textarea = document.createElement('textarea');

  // Position & Size
  textarea.style.position = 'absolute';
  textarea.style.boxSizing = 'border-box';
  textarea.style.width = bbox.width + 'px';
  textarea.style.height = bbox.height + 'px';
  textarea.style.transform = V.matrixToTransformString(paper.matrix().translate(bbox.x, bbox.y));
  textarea.style.transformOrigin = '0 0';

  // Content
  const textPath = 'label/text';
  textarea.value = element.attr(textPath);

  // Styling
 ...