From 0cb778b063401bcc571c95e25865f7f510bac2b3 Mon Sep 17 00:00:00 2001 From: Kir Ax Date: Tue, 2 Mar 2021 21:25:00 +0700 Subject: [PATCH 1/3] Add check for elem existance to interactive SVG; reindent JS-code --- .../sourceforge/plantuml/svg/SvgGraphics.java | 180 +++++++++--------- 1 file changed, 92 insertions(+), 88 deletions(-) diff --git a/src/net/sourceforge/plantuml/svg/SvgGraphics.java b/src/net/sourceforge/plantuml/svg/SvgGraphics.java index 6eaf91e49..d5f78cacf 100644 --- a/src/net/sourceforge/plantuml/svg/SvgGraphics.java +++ b/src/net/sourceforge/plantuml/svg/SvgGraphics.java @@ -5,12 +5,12 @@ * (C) Copyright 2009-2020, Arnaud Roques * * Project Info: http://plantuml.com - * + * * If you like this project or if you find it useful, you can support us at: - * + * * http://plantuml.com/patreon (only 1$ per month!) * http://plantuml.com/paypal - * + * * This file is part of PlantUML. * * PlantUML is free software; you can redistribute it and/or modify it @@ -30,7 +30,7 @@ * * * Original Author: Arnaud Roques - * + * * */ package net.sourceforge.plantuml.svg; @@ -184,111 +184,115 @@ public class SvgGraphics { final Element script = document.createElement("script"); script.setTextContent( "function addItemToMapOfLists(mapOfLists, name, item) {" + "\n" + - " // mapOfLists = {" + "\n" + - " // 'key1': [item1, item2, ...]," + "\n" + - " // 'key2': [item3, item4, ...]," + "\n" + - " // }" + "\n" + - " if (mapOfLists[name].length > 0) {" + "\n" + - " if (!mapOfLists[name].includes(item)) {" + "\n" + - " mapOfLists[name].push(item);" + "\n" + - " }" + "\n" + - " } else {" + "\n" + - " mapOfLists[name] = [item];" + "\n" + - " }" + "\n" + - "}" + "\n" + + " // mapOfLists = {" + "\n" + + " // 'key1': [item1, item2, ...]," + "\n" + + " // 'key2': [item3, item4, ...]," + "\n" + + " // }" + "\n" + + " if (mapOfLists[name].length > 0) {" + "\n" + + " if (!mapOfLists[name].includes(item)) {" + "\n" + + " mapOfLists[name].push(item);" + "\n" + + " }" + "\n" + + " } else {" + "\n" + + " mapOfLists[name] = [item];" + "\n" + + " }" + "\n" + + "}" + "\n" + "" + "\n" + - "function main() {" + "\n" + - " let elems = Array.from(document.getElementsByClassName('elem'));" + "\n" + - " let links = Array.from(document.getElementsByClassName('link'));" + "\n" + + "function main() {" + "\n" + + " let elems = Array.from(document.getElementsByClassName('elem'));" + "\n" + + " let links = Array.from(document.getElementsByClassName('link'));" + "\n" + "" + "\n" + - " let elemsMap = {};" + "\n" + - " let linkedElems = {};" + "\n" + - " let linkedLinks = {};" + "\n" + + " let elemsMap = {};" + "\n" + + " let linkedElems = {};" + "\n" + + " let linkedLinks = {};" + "\n" + "" + "\n" + - " elems.forEach(elem => {" + "\n" + - " let name = elem.classList[1];" + "\n" + - " elemsMap[name] = elem;" + "\n" + - " linkedElems[name] = [];" + "\n" + - " linkedLinks[name] = [];" + "\n" + - " });" + "\n" + + " elems.forEach(elem => {" + "\n" + + " let name = elem.classList[1];" + "\n" + + " elemsMap[name] = elem;" + "\n" + + " linkedElems[name] = [];" + "\n" + + " linkedLinks[name] = [];" + "\n" + + " });" + "\n" + "" + "\n" + - " links.forEach(link => {" + "\n" + - " let name1 = link.classList[1];" + "\n" + - " let name2 = link.classList[2];" + "\n" + + " links.forEach(link => {" + "\n" + + " let name1 = link.classList[1];" + "\n" + + " let name2 = link.classList[2];" + "\n" + "" + "\n" + - " let elem1 = elemsMap[name1];" + "\n" + - " let elem2 = elemsMap[name2];" + "\n" + + " if (elemsMap[name1]) {" + "\n" + + " if (elemsMap[name2]) {" + "\n" + + " let elem1 = elemsMap[name1];" + "\n" + + " let elem2 = elemsMap[name2];" + "\n" + "" + "\n" + - " addItemToMapOfLists(linkedElems, name1, elem2);" + "\n" + - " addItemToMapOfLists(linkedElems, name2, elem1);" + "\n" + + " addItemToMapOfLists(linkedElems, name1, elem2);" + "\n" + + " addItemToMapOfLists(linkedElems, name2, elem1);" + "\n" + "" + "\n" + - " addItemToMapOfLists(linkedLinks, name1, link);" + "\n" + - " addItemToMapOfLists(linkedLinks, name2, link);" + "\n" + - " });" + "\n" + + " addItemToMapOfLists(linkedLinks, name1, link);" + "\n" + + " addItemToMapOfLists(linkedLinks, name2, link);" + "\n" + + " }" + "\n" + + " }" + "\n" + + " });" + "\n" + "" + "\n" + - " let selectedElems = [];" + "\n" + - " let selectedLinks = [];" + "\n" + - " let selectedElemName = null;" + "\n" + + " let selectedElems = [];" + "\n" + + " let selectedLinks = [];" + "\n" + + " let selectedElemName = null;" + "\n" + "" + "\n" + - " function clearSelected() {" + "\n" + - " selectedElems.forEach(item => {" + "\n" + - " item.classList.remove('selected');" + "\n" + - " });" + "\n" + - " selectedElems = [];" + "\n" + + " function clearSelected() {" + "\n" + + " selectedElems.forEach(item => {" + "\n" + + " item.classList.remove('selected');" + "\n" + + " });" + "\n" + + " selectedElems = [];" + "\n" + "" + "\n" + - " selectedLinks.forEach(item => {" + "\n" + - " item.classList.remove('selected');" + "\n" + - " });" + "\n" + - " selectedLinks = [];" + "\n" + - " }" + "\n" + + " selectedLinks.forEach(item => {" + "\n" + + " item.classList.remove('selected');" + "\n" + + " });" + "\n" + + " selectedLinks = [];" + "\n" + + " }" + "\n" + "" + "\n" + - " function selectAll() {" + "\n" + - " selectedElemName = null;" + "\n" + + " function selectAll() {" + "\n" + + " selectedElemName = null;" + "\n" + "" + "\n" + - " selectedElems = Array.from(elems);" + "\n" + - " selectedElems.forEach(item => {" + "\n" + - " item.classList.add('selected');" + "\n" + - " });" + "\n" + + " selectedElems = Array.from(elems);" + "\n" + + " selectedElems.forEach(item => {" + "\n" + + " item.classList.add('selected');" + "\n" + + " });" + "\n" + "" + "\n" + - " selectedLinks = Array.from(links);" + "\n" + - " selectedLinks.forEach(item => {" + "\n" + - " item.classList.add('selected');" + "\n" + - " });" + "\n" + - " }" + "\n" + + " selectedLinks = Array.from(links);" + "\n" + + " selectedLinks.forEach(item => {" + "\n" + + " item.classList.add('selected');" + "\n" + + " });" + "\n" + + " }" + "\n" + "" + "\n" + - " function selectElem(elemName) {" + "\n" + - " if (elemName === selectedElemName) {" + "\n" + - " selectAll();" + "\n" + + " function selectElem(elemName) {" + "\n" + + " if (elemName === selectedElemName) {" + "\n" + + " selectAll();" + "\n" + "" + "\n" + - " } else {" + "\n" + - " clearSelected();" + "\n" + - " selectedElemName = elemName;" + "\n" + + " } else {" + "\n" + + " clearSelected();" + "\n" + + " selectedElemName = elemName;" + "\n" + "" + "\n" + - " elemsMap[elemName].classList.add('selected');" + "\n" + - " selectedElems.push(elemsMap[elemName]);" + "\n" + + " elemsMap[elemName].classList.add('selected');" + "\n" + + " selectedElems.push(elemsMap[elemName]);" + "\n" + "" + "\n" + - " linkedElems[elemName].forEach(linkedElem => {" + "\n" + - " selectedElems.push(linkedElem);" + "\n" + - " linkedElem.classList.add('selected');" + "\n" + - " });" + "\n" + + " linkedElems[elemName].forEach(linkedElem => {" + "\n" + + " selectedElems.push(linkedElem);" + "\n" + + " linkedElem.classList.add('selected');" + "\n" + + " });" + "\n" + "" + "\n" + - " linkedLinks[elemName].forEach(linkedLink => {" + "\n" + - " selectedLinks.push(linkedLink);" + "\n" + - " linkedLink.classList.add('selected');" + "\n" + - " });" + "\n" + - " }" + "\n" + - " }" + "\n" + + " linkedLinks[elemName].forEach(linkedLink => {" + "\n" + + " selectedLinks.push(linkedLink);" + "\n" + + " linkedLink.classList.add('selected');" + "\n" + + " });" + "\n" + + " }" + "\n" + + " }" + "\n" + "" + "\n" + - " Object.keys(elemsMap).forEach(name => {" + "\n" + - " elemsMap[name].onclick = () => { selectElem(name); };" + "\n" + - " });" + "\n" + + " Object.keys(elemsMap).forEach(name => {" + "\n" + + " elemsMap[name].onclick = () => { selectElem(name); };" + "\n" + + " });" + "\n" + "" + "\n" + - " selectAll();" + "\n" + - "}" + "\n" + + " selectAll();" + "\n" + + "}" + "\n" + "" + "\n" + - "document.addEventListener('DOMContentLoaded', (event) => {" + "\n" + - " main();" + "\n" + - "});" + "document.addEventListener('DOMContentLoaded', (event) => {" + "\n" + + " main();" + "\n" + + "});" ); return script; } From 6928eb806b0fd6878f93a8575de8a2e96c0d2dcd Mon Sep 17 00:00:00 2001 From: Kir Ax Date: Sat, 6 Mar 2021 17:03:24 +0700 Subject: [PATCH 2/3] Fix JS for interactive SVG: place assignment before using --- svg/default.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/svg/default.js b/svg/default.js index 0ad651b35..27da2a695 100644 --- a/svg/default.js +++ b/svg/default.js @@ -28,11 +28,11 @@ function main() { }); links.forEach(link => { + let name1 = link.classList[1]; + let name2 = link.classList[2]; + if (elemsMap[name1]) { if (elemsMap[name2]) { - let name1 = link.classList[1]; - let name2 = link.classList[2]; - let elem1 = elemsMap[name1]; let elem2 = elemsMap[name2]; @@ -107,4 +107,4 @@ function main() { document.addEventListener('DOMContentLoaded', (event) => { main(); -}); \ No newline at end of file +}); From aa547f85ed7d3cd576618aa550ad2669cd97ac00 Mon Sep 17 00:00:00 2001 From: Kir Ax Date: Sat, 6 Mar 2021 17:06:53 +0700 Subject: [PATCH 3/3] Bring back whitespaces to doc text --- src/net/sourceforge/plantuml/svg/SvgGraphics.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/net/sourceforge/plantuml/svg/SvgGraphics.java b/src/net/sourceforge/plantuml/svg/SvgGraphics.java index e9284e1c2..ecbaabe8f 100644 --- a/src/net/sourceforge/plantuml/svg/SvgGraphics.java +++ b/src/net/sourceforge/plantuml/svg/SvgGraphics.java @@ -5,12 +5,12 @@ * (C) Copyright 2009-2020, Arnaud Roques * * Project Info: http://plantuml.com - * + * * If you like this project or if you find it useful, you can support us at: - * + * * http://plantuml.com/patreon (only 1$ per month!) * http://plantuml.com/paypal - * + * * This file is part of PlantUML. * * PlantUML is free software; you can redistribute it and/or modify it @@ -30,7 +30,7 @@ * * * Original Author: Arnaud Roques - * + * * */ package net.sourceforge.plantuml.svg;