Skip to content

Commit

Permalink
Merge pull request #25 from Ericsson/issue#9
Browse files Browse the repository at this point in the history
solve #9
  • Loading branch information
vareb authored Nov 28, 2024
2 parents b24fa23 + fe1dd19 commit 3dfb7b6
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 9 deletions.
14 changes: 13 additions & 1 deletion src/plantuml_gui/add.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
# SOFTWARE.


import re
from typing import Literal


Expand All @@ -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)")
Expand Down
30 changes: 30 additions & 0 deletions src/plantuml_gui/static/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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;
}
37 changes: 29 additions & 8 deletions tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -447,7 +447,7 @@ def test_addtomerge2(self, client):
else (no)
:Activity;
endif
:Activity;
:Activity 1;
repeat
:Activity;
:Activity;
Expand Down Expand Up @@ -519,7 +519,7 @@ def test_addactivitymerge(self, client):
else (no)
:Actaivity;
endif
:Activity;
:Activity 1;
:Activity;
endif
:Activity;
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -1950,7 +1950,7 @@ def test_addactivitybelowwithconnector(self, client):
(A)
detach
stop
:Activity;
:Activity 1;
@enduml"""
assert response.data.decode("utf-8") == expected_puml

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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": """<rect fill="#F1F1F1" height="33.9688" rx="12.5" ry="12.5" style="stroke:#181818;stroke-width:0.5;" width="75" x="11" y="11"></rect><text fill="#000000" font-family="sans-serif" font-size="12" lengthAdjust="spacing" textLength="55" x="21" y="31.9688" style="pointer-events: none;">activity 1</text>""",
"svgelement": """<rect fill="#F1F1F1" height="33.9688" rx="12.5" ry="12.5" style="stroke:#181818;stroke-width:0.5;" width="75" x="11" y="11"></rect>""",
"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

Expand Down

0 comments on commit 3dfb7b6

Please sign in to comment.