feat: follow utility

This commit is contained in:
aarthificial
2022-03-20 17:26:17 +01:00
parent 4db62d8a65
commit fddfc67a42
2 changed files with 42 additions and 1 deletions

40
src/utils/follow.ts Normal file
View File

@@ -0,0 +1,40 @@
import {Node} from 'konva/lib/Node';
import {Center, flipOrigin, Origin} from '../types';
import {getOriginDelta, isLayoutNode} from '../components/ILayoutNode';
export interface FollowSubscription {
dispose(): void;
target(newTarget: Node): void;
}
export function follow(
source: Node,
target: Node,
direction?: Origin,
): FollowSubscription {
direction ??= isLayoutNode(source)
? flipOrigin(source.getOrigin(), Center.Vertical)
: Origin.TopLeft;
const update = () => {
const rect = target.getClientRect({relativeTo: target.getLayer()});
const offset = getOriginDelta(rect, Origin.TopLeft, direction);
source.position({
x: rect.x + offset.x,
y: rect.y + offset.y,
});
};
target.on('absoluteTransformChange', update);
update();
return {
dispose: () => target.off('absoluteTransformChange', update),
target: (newTarget: Node) => {
target.off('absoluteTransformChange', update);
target = newTarget;
target.on('absoluteTransformChange', update);
update();
},
};
}

View File

@@ -1 +1,2 @@
export * from './pop';
export * from './pop';
export * from './follow';