Skip to content

Commit 5eb8962

Browse files
authored
first commit (#622)
1 parent 94e9649 commit 5eb8962

File tree

1 file changed

+116
-0
lines changed

1 file changed

+116
-0
lines changed

apps/roam/src/components/canvas/DiscourseRelationShape/DiscourseRelationUtil.tsx

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,64 @@ export const createAllReferencedNodeUtils = (
431431
const atTranslationStart = shapeAtTranslationStart.get(initialShape);
432432
if (!atTranslationStart) return;
433433

434+
const bindings = getArrowBindings(this.editor, shape);
435+
436+
// If both ends are bound, convert translation to bend changes instead of moving the arrow
437+
if (bindings.start && bindings.end) {
438+
const shapePageTransform = this.editor.getShapePageTransform(
439+
shape.id,
440+
)!;
441+
const pageDelta = Vec.Sub(
442+
shapePageTransform.applyToPoint(shape),
443+
atTranslationStart.pagePosition,
444+
);
445+
446+
const initialBindings = getArrowBindings(this.editor, initialShape);
447+
const { start: initialStart, end: initialEnd } =
448+
getArrowTerminalsInArrowSpace(
449+
this.editor,
450+
initialShape,
451+
initialBindings,
452+
);
453+
454+
const delta = Vec.Sub(initialEnd, initialStart);
455+
const v = Vec.Per(delta);
456+
const med = Vec.Med(initialEnd, initialStart);
457+
458+
const initialPageTransform = this.editor.getShapePageTransform(
459+
initialShape.id,
460+
)!;
461+
const arrowSpaceDelta = Vec.Rot(
462+
pageDelta,
463+
-initialPageTransform.rotation(),
464+
);
465+
466+
const translatedMidpoint = Vec.Add(med, arrowSpaceDelta);
467+
const A = Vec.Sub(med, v);
468+
const B = Vec.Add(med, v);
469+
const point = Vec.NearestPointOnLineSegment(
470+
A,
471+
B,
472+
translatedMidpoint,
473+
false,
474+
);
475+
476+
// Calculate new bend based on distance from midpoint
477+
let newBend = Vec.Dist(point, med);
478+
if (Vec.Clockwise(point, initialEnd, med)) {
479+
newBend *= -1;
480+
}
481+
482+
return {
483+
id: shape.id,
484+
type: shape.type,
485+
x: initialShape.x,
486+
y: initialShape.y,
487+
props: { bend: newBend },
488+
};
489+
}
490+
491+
// If not both ends are bound, use normal translation behavior
434492
const shapePageTransform = this.editor.getShapePageTransform(shape.id)!;
435493
const pageDelta = Vec.Sub(
436494
shapePageTransform.applyToPoint(shape),
@@ -866,6 +924,64 @@ export const createAllRelationShapeUtils = (
866924
const atTranslationStart = shapeAtTranslationStart.get(initialShape);
867925
if (!atTranslationStart) return;
868926

927+
const bindings = getArrowBindings(this.editor, shape);
928+
929+
// If both ends are bound, convert translation to bend changes instead of moving the arrow
930+
if (bindings.start && bindings.end) {
931+
const shapePageTransform = this.editor.getShapePageTransform(
932+
shape.id,
933+
)!;
934+
const pageDelta = Vec.Sub(
935+
shapePageTransform.applyToPoint(shape),
936+
atTranslationStart.pagePosition,
937+
);
938+
939+
const initialBindings = getArrowBindings(this.editor, initialShape);
940+
const { start: initialStart, end: initialEnd } =
941+
getArrowTerminalsInArrowSpace(
942+
this.editor,
943+
initialShape,
944+
initialBindings,
945+
);
946+
947+
const delta = Vec.Sub(initialEnd, initialStart);
948+
const v = Vec.Per(delta);
949+
const med = Vec.Med(initialEnd, initialStart);
950+
951+
const initialPageTransform = this.editor.getShapePageTransform(
952+
initialShape.id,
953+
)!;
954+
const arrowSpaceDelta = Vec.Rot(
955+
pageDelta,
956+
-initialPageTransform.rotation(),
957+
);
958+
959+
const translatedMidpoint = Vec.Add(med, arrowSpaceDelta);
960+
const A = Vec.Sub(med, v);
961+
const B = Vec.Add(med, v);
962+
const point = Vec.NearestPointOnLineSegment(
963+
A,
964+
B,
965+
translatedMidpoint,
966+
false,
967+
);
968+
969+
// Calculate new bend based on distance from midpoint
970+
let newBend = Vec.Dist(point, med);
971+
if (Vec.Clockwise(point, initialEnd, med)) {
972+
newBend *= -1;
973+
}
974+
975+
return {
976+
id: shape.id,
977+
type: shape.type,
978+
x: initialShape.x,
979+
y: initialShape.y,
980+
props: { bend: newBend },
981+
};
982+
}
983+
984+
// If not both ends are bound, use normal translation behavior
869985
const shapePageTransform = this.editor.getShapePageTransform(shape.id)!;
870986
const pageDelta = Vec.Sub(
871987
shapePageTransform.applyToPoint(shape),

0 commit comments

Comments
 (0)