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/src/plantuml_gui/static/script.js b/src/plantuml_gui/static/script.js index 31fc7a9..a685d07 100644 --- a/src/plantuml_gui/static/script.js +++ b/src/plantuml_gui/static/script.js @@ -2145,7 +2145,20 @@ async function renderPlantUml() { displayErrorMessage(`Error with fetch API: ${error.message}`, error); } + if (checkIfActivityDiagram(pumlcontent)) { + setHandlersForActivityDiagram(pumlcontent, element) + } + else { + fetchSvgFromPlantUml().then((svgContent) => { + element.innerHTML = svgContent; + }) + toggleLoadingOverlay(); + + } +} + +async function setHandlersForActivityDiagram(pumlcontent, element) { fetchSvgFromPlantUml().then((svgContent) => { element.innerHTML = svgContent; const svg = element.querySelector('g'); @@ -3527,3 +3540,20 @@ function restoreeditor() { setPuml(history[historyPointer]) } } + +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(); + if (activityKeywords.some(keyword => trimmedLine.startsWith(keyword))) { + return true; + } + if (notActivityKeywords.some(keyword => trimmedLine.startsWith(keyword))) { + return false; + } + } + return false; +} 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