From 10a710127ad0f67044b45e2eb952d855792a5cce Mon Sep 17 00:00:00 2001 From: Zsolt Viczian Date: Sun, 23 Jan 2022 17:41:18 +0100 Subject: [PATCH] updates scripts --- ea-scripts/Add Next Step in Process.md | 24 ++-- ea-scripts/Normalize Selected Arrows.md | 153 +++++++++++------------- ea-scripts/index.md | 4 +- 3 files changed, 91 insertions(+), 90 deletions(-) diff --git a/ea-scripts/Add Next Step in Process.md b/ea-scripts/Add Next Step in Process.md index edb18a7..1ce01db 100644 --- a/ea-scripts/Add Next Step in Process.md +++ b/ea-scripts/Add Next Step in Process.md @@ -54,7 +54,8 @@ const elements = ea.getViewSelectedElements(); const isFirst = (!elements || elements.length === 0); const width = ea.measureText("w".repeat(wrapLineLen)).width; -console.log(width,fixWidth); + +let id = ""; if(!isFirst) { const fromElement = ea.getLargestElement(elements); @@ -71,8 +72,8 @@ if(!isFirst) { ea.style.strokeSharpness = el.strokeSharpness; } - - const id = ea.addText( + + id = ea.addText( fromElement.x, fromElement.y+fromElement.height+gapBetweenElements, text, @@ -80,8 +81,9 @@ if(!isFirst) { wrapAt: wrapLineLen, textAlign: "center", box: "rectangle", - boxPadding: textPadding, - ...fixWidth?{width: width}:null + ...fixWidth + ? {width: width, boxPadding:0} + : {boxPadding: textPadding} } ); @@ -96,9 +98,9 @@ if(!isFirst) { numberOfPoints: linePoints } ); - ea.addElementsToView(false); + await ea.addElementsToView(false); } else { - ea.addText( + id = ea.addText( 0, 0, text, @@ -110,5 +112,11 @@ if(!isFirst) { ...fixWidth?{width: width}:null } ); - ea.addElementsToView(true); + await ea.addElementsToView(true); } + +const API = ea.getExcalidrawAPI(); +st = API.getAppState(); +st.selectedElementIds = {}; +st.selectedElementIds[id] = true; +API.updateScene({appState: st}); diff --git a/ea-scripts/Normalize Selected Arrows.md b/ea-scripts/Normalize Selected Arrows.md index c03b6de..7901e65 100644 --- a/ea-scripts/Normalize Selected Arrows.md +++ b/ea-scripts/Normalize Selected Arrows.md @@ -1,81 +1,74 @@ -/* -![](https://raw.githubusercontent.com/zsviczian/obsidian-excalidraw-plugin/master/images/scripts-download-raw.jpg) - -Download this file and save to your Obsidian Vault including the first line, or open it in "Raw" and copy the entire contents to Obsidian. - -![](https://raw.githubusercontent.com/zsviczian/obsidian-excalidraw-plugin/master/images/scripts-normalize-selected-arrows.png) - -This script will reset the start and end positions of the selected arrows. The arrow will point to the center of the connected box and will have a gap of 8px from the box. - -Tips: If you are drawing a flowchart, you can use `Normalize Selected Arrows` script to correct the position of the start and end points of the arrows, then use `Elbow connectors` script, and you will get the perfect connecting line! - -See documentation for more details: -https://zsviczian.github.io/obsidian-excalidraw-plugin/ExcalidrawScriptsEngine.html - -```javascript -*/ -const selectedIndividualArrows = ea.getMaximumGroups(ea.getViewSelectedElements()) - .reduce((result, group) => (group.length === 1 && (group[0].type === 'arrow' || group[0].type === 'line')) ? - [...result, group[0]] : result, []); - -const allElements = ea.getViewElements(); -for(const arrow of selectedIndividualArrows) { - const startBindingEl = allElements.filter(el => el.id === (arrow.startBinding||{}).elementId)[0]; - const endBindingEl = allElements.filter(el => el.id === (arrow.endBinding||{}).elementId)[0]; - - if(startBindingEl) { - recalculateStartPointOfLine(arrow, startBindingEl, endBindingEl); - } - if(endBindingEl) { - recalculateEndPointOfLine(arrow, endBindingEl, startBindingEl); - } -} - -ea.copyViewElementsToEAforEditing(selectedIndividualArrows); -ea.addElementsToView(); - -function recalculateStartPointOfLine(line, el, elB) { - const aX = el.x + el.width/2; - const bX = (line.points.length <=2 && elB) ? elB.x + elB.width/2 : line.x + line.points[1][0]; - const aY = el.y + el.height/2; - const bY = (line.points.length <=2 && elB) ? elB.y + elB.height/2 : line.y + line.points[1][1]; - - line.startBinding.gap = 8; - line.startBinding.focus = 0; - const intersectA = ea.intersectElementWithLine( - el, - [bX, bY], - [aX, aY], - line.startBinding.gap - ); - - if(intersectA.length > 0) { - line.points[0] = [0, 0]; - for(var i = 1; i 0) { - line.points[line.points.length - 1] = [intersectA[0][0] - line.x, intersectA[0][1] - line.y]; - } +/* +![](https://raw.githubusercontent.com/zsviczian/obsidian-excalidraw-plugin/master/images/scripts-normalize-selected-arrows.png) + +This script will reset the start and end positions of the selected arrows. The arrow will point to the center of the connected box and will have a gap of 8px from the box. + +Tips: If you are drawing a flowchart, you can use `Normalize Selected Arrows` script to correct the position of the start and end points of the arrows, then use `Elbow connectors` script, and you will get the perfect connecting line! + +```javascript +*/ +const selectedIndividualArrows = ea.getMaximumGroups(ea.getViewSelectedElements()) + .reduce((result, group) => (group.length === 1 && (group[0].type === 'arrow' || group[0].type === 'line')) ? + [...result, group[0]] : result, []); + +const allElements = ea.getViewElements(); +for(const arrow of selectedIndividualArrows) { + const startBindingEl = allElements.filter(el => el.id === (arrow.startBinding||{}).elementId)[0]; + const endBindingEl = allElements.filter(el => el.id === (arrow.endBinding||{}).elementId)[0]; + + if(startBindingEl) { + recalculateStartPointOfLine(arrow, startBindingEl, endBindingEl); + } + if(endBindingEl) { + recalculateEndPointOfLine(arrow, endBindingEl, startBindingEl); + } +} + +ea.copyViewElementsToEAforEditing(selectedIndividualArrows); +ea.addElementsToView(); + +function recalculateStartPointOfLine(line, el, elB) { + const aX = el.x + el.width/2; + const bX = (line.points.length <=2 && elB) ? elB.x + elB.width/2 : line.x + line.points[1][0]; + const aY = el.y + el.height/2; + const bY = (line.points.length <=2 && elB) ? elB.y + elB.height/2 : line.y + line.points[1][1]; + + line.startBinding.gap = 8; + line.startBinding.focus = 0; + const intersectA = ea.intersectElementWithLine( + el, + [bX, bY], + [aX, aY], + line.startBinding.gap + ); + + if(intersectA.length > 0) { + line.points[0] = [0, 0]; + for(var i = 1; i 0) { + line.points[line.points.length - 1] = [intersectA[0][0] - line.x, intersectA[0][1] - line.y]; + } } \ No newline at end of file diff --git a/ea-scripts/index.md b/ea-scripts/index.md index 262eef9..551770d 100644 --- a/ea-scripts/index.md +++ b/ea-scripts/index.md @@ -187,9 +187,9 @@ https://raw.githubusercontent.com/zsviczian/obsidian-excalidraw-plugin/master/ea ## Normalize Selected Arrows ```excalidraw-script-install -https://raw.githubusercontent.com/zsviczian/obsidian-excalidraw-plugin/master/ea-scripts/Normalize%20Selected%20Arrows +https://raw.githubusercontent.com/zsviczian/obsidian-excalidraw-plugin/master/ea-scripts/Normalize%20Selected%20Arrows.md ``` -
Author@1-2-3
SourceFile on GitHub
DescriptionThis script will reset the start and end positions of the selected arrows. The arrow will point to the center of the connected box and will have a gap of 8px from the box.
+
Author@1-2-3
SourceFile on GitHub
DescriptionThis script will reset the start and end positions of the selected arrows. The arrow will point to the center of the connected box and will have a gap of 8px from the box.
## OCR - Optical Character Recognition ```excalidraw-script-install