diff --git a/package.json b/package.json index f45644d..7ba4f3e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "obsidian-excalidraw-plugin", - "version": "1.6.29", + "version": "1.6.33", "description": "This is an Obsidian.md plugin that lets you view and edit Excalidraw drawings", "main": "lib/index.js", "types": "lib/index.d.ts", diff --git a/src/ExcalidrawAutomate.ts b/src/ExcalidrawAutomate.ts index 84ff46c..c15eb73 100644 --- a/src/ExcalidrawAutomate.ts +++ b/src/ExcalidrawAutomate.ts @@ -948,7 +948,7 @@ export class ExcalidrawAutomate implements ExcalidrawAutomateInterface { endArrowHead?: "triangle"|"dot"|"arrow"|"bar"|null; padding?: number; }, - ): void { + ): string { if (!(this.elementsDict[objectA] && this.elementsDict[objectB])) { return; } @@ -1032,7 +1032,7 @@ export class ExcalidrawAutomate implements ExcalidrawAutomateInterface { aY + (i * (bY - aY)) / (numAP - 1), ]); } - this.addArrow(points, { + return this.addArrow(points, { startArrowHead: formatting?.startArrowHead, endArrowHead: formatting?.endArrowHead, startObjectId: objectA, @@ -1040,6 +1040,45 @@ export class ExcalidrawAutomate implements ExcalidrawAutomateInterface { }); }; + /** + * Adds a text label to a line or arrow. Currently only works with a straight (2 point - start & end - line) + * @param lineId id of the line or arrow object in elementsDict + * @param label the label text + * @returns undefined (if unsuccessful) or the id of the new text element + */ + addLabelToLine(lineId: string, label: string): string { + const line = this.elementsDict[lineId]; + if(!line || !["arrow","line"].includes(line.type) || line.points.length !== 2) { + return; + } + + let angle = Math.atan2(line.points[1][1],line.points[1][0]); + + const size = this.measureText(label); + let delta = size.height/6; + + if(angle < 0) { + if(angle < -Math.PI/2) { + angle+= Math.PI; + } else { + delta = -delta; + } + } else { + if(angle > Math.PI/2) { + angle-= Math.PI; + delta = -delta; + } + } + this.style.angle = angle; + const id = this.addText( + line.x+line.points[1][0]/2-size.width/2+delta, + line.y+line.points[1][1]/2-5*size.height/6, + label + ); + this.style.angle = 0; + return id; + } + /** * clear elementsDict and imagesDict only */ diff --git a/src/ExcalidrawView.ts b/src/ExcalidrawView.ts index 6dd15b3..fe8d006 100644 --- a/src/ExcalidrawView.ts +++ b/src/ExcalidrawView.ts @@ -1250,6 +1250,7 @@ export default class ExcalidrawView extends TextFileView { } if (this.activeLoader) { this.activeLoader.terminate = true; + this.activeLoader = null; } this.nextLoader = null; api.resetScene(); diff --git a/src/dialogs/SuggesterInfo.ts b/src/dialogs/SuggesterInfo.ts index 5239cf5..5049a22 100644 --- a/src/dialogs/SuggesterInfo.ts +++ b/src/dialogs/SuggesterInfo.ts @@ -236,10 +236,16 @@ export const EXCALIDRAW_AUTOMATE_INFO: SuggesterInfo[] = [ }, { field: "connectObjects", - code: "connectObjects(objectA: string, connectionA: ConnectionPoint, objectB: string, connectionB: ConnectionPoint, formatting?: {numberOfPoints?: number; startArrowHead?: string; endArrowHead?: string; padding?: number;},): void;", + code: "connectObjects(objectA: string, connectionA: ConnectionPoint, objectB: string, connectionB: ConnectionPoint, formatting?: {numberOfPoints?: number; startArrowHead?: string; endArrowHead?: string; padding?: number;},): string;", desc: 'type ConnectionPoint = "top" | "bottom" | "left" | "right" | null\nWhen null is passed as ConnectionPoint then Excalidraw will automatically decide\nnumberOfPoints is the number of points on the line. Default is 0 i.e. line will only have a start and end point.\nArrowHead: "triangle"|"dot"|"arrow"|"bar"|null', after: "", }, + { + field: "addLabelToLine", + code: "addLabelToLine(lineId: string, label: string): string;", + desc: 'Adds a text label to a line or arrow. Currently only works with a simple straight 2-point (start & end) line', + after: "", + }, { field: "clear", code: "clear(): void;", diff --git a/src/types.d.ts b/src/types.d.ts index f0bbb4e..7c974b9 100644 --- a/src/types.d.ts +++ b/src/types.d.ts @@ -114,7 +114,8 @@ export interface ExcalidrawAutomateInterface { endArrowHead?: string; //"triangle"|"dot"|"arrow"|"bar"|null padding?: number; }, - ): void; + ): string; + addLabelToLine(lineId: string, label:string): string; clear(): void; //clear elementsDict and imagesDict only reset(): void; //clear() + reset all style values to default isExcalidrawFile(f: TFile): boolean; //returns true if MD file is an Excalidraw file