diff --git a/src/DiagramModel.js b/src/DiagramModel.js index 4eab3f4..71dcfed 100644 --- a/src/DiagramModel.js +++ b/src/DiagramModel.js @@ -1,8 +1,5 @@ import DiagramNode from "./DiagramNode"; - -var generateId = function() { - return Math.trunc(Math.random() * 1000); -}; +import { generateId } from "./utils/Identifier"; /** * @class DiagramModel diff --git a/src/DiagramNode.js b/src/DiagramNode.js index de97d14..eaec476 100644 --- a/src/DiagramNode.js +++ b/src/DiagramNode.js @@ -1,6 +1,4 @@ -var generateId = function() { - return Math.trunc(Math.random() * 1000); -}; +import { generateId } from "./utils/Identifier"; /** * @class DiagramNode diff --git a/src/components/Diagram.vue b/src/components/Diagram.vue index 0a8b8c0..0426ef2 100644 --- a/src/components/Diagram.vue +++ b/src/components/Diagram.vue @@ -97,9 +97,7 @@ import DiagramNode from "./DiagramNode"; import DiagramLink from "./DiagramLink"; import DiagramPort from "./DiagramPort"; -var generateId = function() { - return Math.trunc(Math.random() * 1000); -}; +import { generateId } from "./../utils/Identifier"; function getAbsoluteXY(element) { var viewportElement = document.documentElement; @@ -253,12 +251,10 @@ export default { links[this.draggedItem.linkIndex].points[ this.draggedItem.pointIndex - ].x = - coords.x; + ].x = coords.x; links[this.draggedItem.linkIndex].points[ this.draggedItem.pointIndex - ].y = - coords.y; + ].y = coords.y; this.updateLinksPositions(); } else { let coords = this.convertXYtoViewPort(this.mouseX, this.mouseY); diff --git a/src/utils/Identifier.js b/src/utils/Identifier.js new file mode 100644 index 0000000..cf19a62 --- /dev/null +++ b/src/utils/Identifier.js @@ -0,0 +1,15 @@ +/** + * Generate a UUID (v1) + * @param {Integer} c clock-seq-and-reserved clock-seq-low + * @return {String} The UUID + * http://www.rfcreader.com/#rfc4122_line385 allows random instead of MAC address + * https://www.famkruithof.net/uuid/uuidgen + * https://realityripple.com/Tools/UnUUID/ + */ +export function generateId(c = 9999) { + const t = ((Date.now() + 12219292800000) * 1e4).toString(16); + const n = crypto.getRandomValues(new Uint8Array(6)).reduce((sum, x, i) => { + return sum + (i === 0 ? x | 1 : x).toString(16).padStart(2, "0"); + }, ""); + return `${t.slice(-8)}-${t.slice(-12, -8)}-1${t.slice(0, 3)}-${c}-${n}`; +}