From 82014d5f1f544557e59fba79d0ff1d6742144eff Mon Sep 17 00:00:00 2001 From: Bertil Varenhorst Date: Tue, 19 Nov 2024 08:21:51 +0100 Subject: [PATCH 1/5] solve #9 --- src/plantuml_gui/static/script.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/plantuml_gui/static/script.js b/src/plantuml_gui/static/script.js index 31fc7a9..e6ca592 100644 --- a/src/plantuml_gui/static/script.js +++ b/src/plantuml_gui/static/script.js @@ -2145,7 +2145,19 @@ async function renderPlantUml() { displayErrorMessage(`Error with fetch API: ${error.message}`, error); } + if (checkIfStateDiagram(pumlcontent)) { // If state diagram render without interactivity (not supported) + fetchSvgFromPlantUml().then((svgContent) => { + element.innerHTML = svgContent; + }) + toggleLoadingOverlay(); + } + else { + setHandlersForActivityDiagram(pumlcontent, element); + } + +} +async function setHandlersForActivityDiagram(pumlcontent, element) { fetchSvgFromPlantUml().then((svgContent) => { element.innerHTML = svgContent; const svg = element.querySelector('g'); @@ -3527,3 +3539,15 @@ function restoreeditor() { setPuml(history[historyPointer]) } } + +function checkIfStateDiagram(puml) { + const lines = puml.split('\n'); + for (let index = 0; index < lines.length; index++) { + const line = lines[index]; + const trimmedLine = line.trim(); + if (trimmedLine.startsWith('state')) { + return true + } + } + return false +} \ No newline at end of file From 2c3ec78a3fed5f3b7dbbcc95f70808fe22b4c27b Mon Sep 17 00:00:00 2001 From: Bertil Varenhorst Date: Tue, 19 Nov 2024 08:47:44 +0100 Subject: [PATCH 2/5] pre-commit fix Change-Id: Ic64b6f21a3043fac6b8c7ebfd1ebc6fed7d03856 --- src/plantuml_gui/static/script.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/plantuml_gui/static/script.js b/src/plantuml_gui/static/script.js index e6ca592..7baac46 100644 --- a/src/plantuml_gui/static/script.js +++ b/src/plantuml_gui/static/script.js @@ -3550,4 +3550,4 @@ function checkIfStateDiagram(puml) { } } return false -} \ No newline at end of file +} From 056e2083f03e7821aee6be9811c6c90306187033 Mon Sep 17 00:00:00 2001 From: Bertil Varenhorst Date: Tue, 19 Nov 2024 12:51:32 +0100 Subject: [PATCH 3/5] new diagram type check Change-Id: I7e0ad183ea3afb8f51843d7297769dde8846ae16 --- src/plantuml_gui/static/script.js | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/src/plantuml_gui/static/script.js b/src/plantuml_gui/static/script.js index 7baac46..9f6db63 100644 --- a/src/plantuml_gui/static/script.js +++ b/src/plantuml_gui/static/script.js @@ -2145,14 +2145,15 @@ async function renderPlantUml() { displayErrorMessage(`Error with fetch API: ${error.message}`, error); } - if (checkIfStateDiagram(pumlcontent)) { // If state diagram render without interactivity (not supported) + if (checkIfActivityDiagram(pumlcontent)) { + setHandlersForActivityDiagram(pumlcontent, element) + } + else { fetchSvgFromPlantUml().then((svgContent) => { element.innerHTML = svgContent; }) toggleLoadingOverlay(); - } - else { - setHandlersForActivityDiagram(pumlcontent, element); + } } @@ -3540,14 +3541,19 @@ function restoreeditor() { } } -function checkIfStateDiagram(puml) { +function checkIfActivityDiagram(puml) { + const activityKeywords = ["if", "while", "fork", "repeat", "switch", ":", "start", "end", "stop"]; + const notActivityKeywords = ["state", "actor", "boundary", "control", "entity", "database", "collections", "queue"] const lines = puml.split('\n'); - for (let index = 0; index < lines.length; index++) { - const line = lines[index]; - const trimmedLine = line.trim(); - if (trimmedLine.startsWith('state')) { - return true + + for (const line of lines) { + const trimmedLine = line.trim().toLowerCase(); + if (activityKeywords.some(keyword => trimmedLine.startsWith(keyword))) { + return true; + } + if (notActivityKeywords.some(keyword => trimmedLine.startsWith(keyword))) { + return false; } } - return false + return false; } From 16ee3acb2c6e62247c72e366b7bf9fa3f38e2277 Mon Sep 17 00:00:00 2001 From: Bertil Varenhorst Date: Tue, 19 Nov 2024 12:54:42 +0100 Subject: [PATCH 4/5] pre-commit fix Change-Id: I82215e89582769882377a50aeb6ceea901614ce4 --- src/plantuml_gui/static/script.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/plantuml_gui/static/script.js b/src/plantuml_gui/static/script.js index 9f6db63..a685d07 100644 --- a/src/plantuml_gui/static/script.js +++ b/src/plantuml_gui/static/script.js @@ -3545,14 +3545,14 @@ function checkIfActivityDiagram(puml) { const activityKeywords = ["if", "while", "fork", "repeat", "switch", ":", "start", "end", "stop"]; const notActivityKeywords = ["state", "actor", "boundary", "control", "entity", "database", "collections", "queue"] const lines = puml.split('\n'); - + for (const line of lines) { - const trimmedLine = line.trim().toLowerCase(); + const trimmedLine = line.trim().toLowerCase(); if (activityKeywords.some(keyword => trimmedLine.startsWith(keyword))) { - return true; + return true; } if (notActivityKeywords.some(keyword => trimmedLine.startsWith(keyword))) { - return false; + return false; } } return false; From fe1dd19044fb10a4193cb5c42b37833783484350 Mon Sep 17 00:00:00 2001 From: Bertil Varenhorst Date: Tue, 19 Nov 2024 13:13:47 +0100 Subject: [PATCH 5/5] resolve #6 Change-Id: I838fba364d2b99e0be3bc879eafbd9ca8296aff4 --- src/plantuml_gui/add.py | 14 +++++++++++++- tests/test_app.py | 37 +++++++++++++++++++++++++++++-------- 2 files changed, 42 insertions(+), 9 deletions(-) diff --git a/src/plantuml_gui/add.py b/src/plantuml_gui/add.py index 36b83b1..a7753d5 100644 --- a/src/plantuml_gui/add.py +++ b/src/plantuml_gui/add.py @@ -23,6 +23,7 @@ # SOFTWARE. +import re from typing import Literal @@ -46,7 +47,18 @@ def add( lines = puml.splitlines() if type == "activity": - lines.insert(index, ":Activity;") + activity_numbers = [] + for line in lines: + match = re.search(r":Activity (\d+)", line) + if match: + activity_numbers.append(int(match.group(1))) + + if activity_numbers: + next_activity_number = max(activity_numbers) + 1 + else: + next_activity_number = 1 + + lines.insert(index, f":Activity {next_activity_number};") if type == "connector": lines.insert(index, "(C)") diff --git a/tests/test_app.py b/tests/test_app.py index 9dc9c3d..0756843 100644 --- a/tests/test_app.py +++ b/tests/test_app.py @@ -286,7 +286,7 @@ def test_addtomergenoifmerge3(self, client): endif detach repeat -:Activity; +:Activity 1; :Activity; backward:Activity; repeat while (while ?) is (yes) not (no) @@ -329,7 +329,7 @@ def test_addtomergenoifmerge2(self, client): detach endif repeat -:Activity; +:Activity 1; :Activity; backward:Activity; repeat while (while ?) is (yes) not (no) @@ -372,7 +372,7 @@ def test_addtomergenoifmerge(self, client): :Activity; endif repeat -:Activity; +:Activity 1; :Activity; backward:Activity; repeat while (while ?) is (yes) not (no) @@ -447,7 +447,7 @@ def test_addtomerge2(self, client): else (no) :Activity; endif -:Activity; +:Activity 1; repeat :Activity; :Activity; @@ -519,7 +519,7 @@ def test_addactivitymerge(self, client): else (no) :Actaivity; endif -:Activity; +:Activity 1; :Activity; endif :Activity; @@ -1743,7 +1743,7 @@ def test_add_activity_connector2(self, client): note end note detach -:Activity; +:Activity 1; @endumll""" assert response.data.decode("utf-8") == expected_puml @@ -1950,7 +1950,7 @@ def test_addactivitybelowwithconnector(self, client): (A) detach stop -:Activity; +:Activity 1; @enduml""" assert response.data.decode("utf-8") == expected_puml @@ -3765,7 +3765,7 @@ def test_addtofork(self, client): fork again :action; end fork -:Activity; +:Activity 1; @enduml""" assert response.data.decode("utf-8") == expected_puml @@ -4377,6 +4377,27 @@ def test_addtoactivity(self, client): backward:Activity; repeat while (while ?) is (yes) not (no) :Activity; +@enduml""" + assert response.data.decode("utf-8") == expected_puml + + def test_addactivitytoactivity(self, client): + test_data = { + "plantuml": """@startuml +:Activity 1; +@enduml""", + "svg": """activity 1""", + "svgelement": """""", + "type": "activity", + } + with client: + response = client.post( + "/addToActivity", + data=json.dumps(test_data), + content_type="application/json", + ) + expected_puml = """@startuml +:Activity 1; +:Activity 2; @enduml""" assert response.data.decode("utf-8") == expected_puml