fix update embed #637. new function: addLabelToLine()

This commit is contained in:
Zsolt Viczian
2022-05-28 08:29:59 +02:00
parent 5ff8caa04f
commit 461eeafd80
5 changed files with 52 additions and 5 deletions

View File

@@ -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",

View File

@@ -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
*/

View File

@@ -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();

View File

@@ -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;",

3
src/types.d.ts vendored
View File

@@ -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