ADD: add text to text block and improve draggable

This commit is contained in:
Patryk
2025-10-30 20:21:09 +01:00
parent 53b928827c
commit 899d1a1017
2 changed files with 70 additions and 39 deletions

View File

@@ -4,6 +4,7 @@
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://code.jquery.com/ui/1.13.2/themes/base/jquery-ui.css">
<link rel="stylesheet" href="style.css"> <link rel="stylesheet" href="style.css">
<title>DrawDiagrams</title> <title>DrawDiagrams</title>
</head> </head>
@@ -23,6 +24,7 @@
</div> </div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.13.2/jquery-ui.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@joint/core@4.0.1/dist/joint.js"></script> <script src="https://cdn.jsdelivr.net/npm/@joint/core@4.0.1/dist/joint.js"></script>
<script> <script>
$(document).ready(function () { $(document).ready(function () {
@@ -31,8 +33,8 @@
const paper = new joint.dia.Paper({ const paper = new joint.dia.Paper({
el: $('#paper'), el: $('#paper'),
model: graph, model: graph,
width: 1920, width: window.innerWidth,
height: 1080, height: window.innerHeight,
gridSize: 20, gridSize: 20,
}); });
//init paper - END //init paper - END
@@ -40,8 +42,8 @@
//create shape - START //create shape - START
function createRectangle(paperX, paperY) { function createRectangle(paperX, paperY) {
const rect = new joint.shapes.standard.Rectangle({ const rect = new joint.shapes.standard.Rectangle({
position: { x: paperX - 50, y: paperY - 30 }, position: { x: paperX - 40, y: paperY - 24 },
size: { width: 100, height: 60 }, size: { width: 80, height: 48 },
attrs: { attrs: {
body: { body: {
fill: 'transparent', fill: 'transparent',
@@ -58,8 +60,8 @@
function createDiamond(paperX, paperY) { function createDiamond(paperX, paperY) {
const rect = new joint.shapes.standard.Rectangle({ const rect = new joint.shapes.standard.Rectangle({
position: { x: paperX - 30, y: paperY - 30 }, position: { x: paperX - 24, y: paperY - 24 },
size: { width: 60, height: 60 }, size: { width: 48, height: 48 },
angle: 45, angle: 45,
attrs: { attrs: {
body: { body: {
@@ -77,8 +79,8 @@
function createEllipse(paperX, paperY) { function createEllipse(paperX, paperY) {
const rect = new joint.shapes.standard.Ellipse({ const rect = new joint.shapes.standard.Ellipse({
position: { x: paperX - 50, y: paperY - 30 }, position: { x: paperX - 40, y: paperY - 24 },
size: { width: 100, height: 60 }, size: { width: 80, height: 48 },
attrs: { attrs: {
body: { body: {
fill: 'transparent', fill: 'transparent',
@@ -95,13 +97,14 @@
function createText(paperX, paperY) { function createText(paperX, paperY) {
const textBlock = new joint.shapes.standard.TextBlock({ const textBlock = new joint.shapes.standard.TextBlock({
position: { x: paperX - 50, y: paperY - 20 }, position: { x: paperX - 40, y: paperY - 24 },
size: { width: 100, height: 40 }, size: { width: 80, height: 48 },
attrs: { attrs: {
body: { body: {
fill: 'transparent', fill: 'transparent',
stroke: '#000000', stroke: '#000000',
strokeWidth: 1 strokeWidth: 1,
strokeDasharray: '5,5'
}, },
label: { label: {
text: 'Tekst...', text: 'Tekst...',
@@ -113,41 +116,62 @@
} }
//create shape - END //create shape - END
//init test draggalble - START //init draggalble - START
$('.shape').attr('draggable', true); $('.shape').draggable({
$('.shape').on('dragstart', function (event) { helper: function () {
event.originalEvent.dataTransfer.setData('shape', $(this).data('type')); const $clone = $(this).clone();
$clone.css({
opacity: 0.8,
cursor: 'grabbing',
borderColor: '#000',
color: '#000',
});
return $clone;
},
cursor: 'move',
revert: 'invalid',
appendTo: 'body',
zIndex: 1000
}); });
$('#paper').on('dragover', function (event) { $('#paper').droppable({
event.preventDefault(); accept: '.shape',
}); drop: function (event, ui) {
const shapeType = ui.helper.data('type');
const containerOffset = $(this).offset();
const x = event.pageX - containerOffset.left;
const y = event.pageY - containerOffset.top;
$('#paper').on('drop', function (event) { const scale = paper.scale();
event.preventDefault(); const translate = paper.translate();
const shapeType = event.originalEvent.dataTransfer.getData('shape'); const paperX = (x - translate.tx) / scale.sx;
const paperY = (y - translate.ty) / scale.sy;
const containerOffset = $(this).offset(); if (shapeType === 'rectangle') {
const x = event.originalEvent.clientX - containerOffset.left; createRectangle(paperX, paperY);
const y = event.originalEvent.clientY - containerOffset.top; } else if (shapeType === 'ellipse') {
createEllipse(paperX, paperY);
const scale = paper.scale(); } else if (shapeType === 'diamond') {
const translate = paper.translate(); createDiamond(paperX, paperY);
const paperX = (x - translate.tx) / scale.sx; } else if (shapeType === 'text') {
const paperY = (y - translate.ty) / scale.sy; createText(paperX, paperY);
}
if (shapeType == 'rectangle') {
createRectangle(paperX, paperY);
} else if (shapeType == "ellipse") {
createEllipse(paperX, paperY)
} else if (shapeType == 'diamond') {
createDiamond(paperX, paperY);
} else if (shapeType == "text") {
createText(paperX, paperY);
} }
}); });
//init test draggable - END //init draggable - END
//add text to text block - START
paper.on('element:pointerdblclick', function (elementView) {
if (elementView.model.attributes.type === 'standard.TextBlock') {
const currentText = elementView.model.attr('label/text');
const newText = prompt('Edytuj tekst:', currentText);
if (newText != null) {
elementView.model.attr('label/text', newText);
}
}
});
//add text to text block - END
}); });
</script> </script>
</body> </body>

View File

@@ -1,3 +1,9 @@
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.shape-wrapper { .shape-wrapper {
display: grid; display: grid;
grid-template-columns: repeat(2, 1fr); grid-template-columns: repeat(2, 1fr);
@@ -32,6 +38,7 @@
display: flex; display: flex;
align-items: center; align-items: center;
justify-content: center; justify-content: center;
user-select: none;
} }
.shape { .shape {