Inline Text Editing
by Roman Bruckner
HTML
<html>
<head>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/jointjs/3.3.0/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.3.0/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: 10,
textWrap: {
ellipsis: true
}
},
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,
textWrap: {
ellipsis: true
}
},
body: {
fill: 'blue'
}
}
});
var el3 = new joint.shapes.standard.Rectangle({
position: {
x: 300,
y: 100
},
size: {
width: 200,
height: 50
},
attrs: {
label: {
text: 'Morbi ac urna quis arcu suscipit vestibulum.',
fontSize: 14,
fill: 'black',
textWrap: {
ellipsis: true
}
},
body: {
fill: 'white'
}
}
});
var l1 = new joint.shapes.standard.Link({
source: {
id: el1.id
},
target: {
id: el2.id
},
labels: [{
attrs: {
text: {
text: 'Label'
}
}
}]
});
graph.addCells([el1, el2, el3, l1]);
paper.on('link:pointerdblclick', function(linkView, evt) {
const labelNode = linkView.findLabelNode(0, 'rect');
if (!labelNode.parentNode.contains(evt.target)) return;
const link = linkView.model;
const bbox = V(labelNode).getBBox({
target: paper.svg
});
bbox.width = Math.max(bbox.width, 100);
bbox.height = Math.max(bbox.height, 20);
const textarea =...