Skip to content

Commit

Permalink
Make equation annotation button be the whole rectangle.
Browse files Browse the repository at this point in the history
  • Loading branch information
bgoldowsky committed Mar 27, 2024
1 parent 0e54623 commit 7df677c
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 10 deletions.
18 changes: 13 additions & 5 deletions src/plugins/graph/adornments/movable-line/movable-line.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { kInfinitePoint } from "../adornment-models";
import { LocationSetterContext, Point } from "../../graph-types";

import "./movable-line.scss";
import { kAnnotationNodeDefaultRadius } from "../../../../components/annotations/annotation-utilities";

function equationContainer(model: IMovableLineModel, subPlotKey: Record<string, string>, containerId: string) {
const classFromKey = model.classNameFromKey(subPlotKey),
Expand Down Expand Up @@ -84,11 +85,18 @@ export const MovableLine = observer(function MovableLine(props: IProps) {
}
}, [kHandle1Loc, kHandle2Loc]);

const positionEquation = useCallback((elt: Selection<HTMLElement, unknown, HTMLElement, any>, point: Point) => {
elt.style('left', `${point.x}px`)
const positionEquation = useCallback((equation: Selection<HTMLElement, unknown, HTMLElement, any>, point: Point) => {
const annotationId = getAnnotationId(instanceKey, "equation");
equation.style('left', `${point.x}px`)
.style('top', `${point.y}px`);
if (model.isVisible) {
annotationLocationSetter?.set(getAnnotationId(instanceKey, "equation"), point);
const equationNode = equation.node() as Element,
rect = equationNode?.getBoundingClientRect(),
width = rect.width || kAnnotationNodeDefaultRadius,
height = rect.height || kAnnotationNodeDefaultRadius;
annotationLocationSetter?.set(annotationId, point, { width, height });
} else {

Check warning on line 98 in src/plugins/graph/adornments/movable-line/movable-line.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/graph/adornments/movable-line/movable-line.tsx#L98

Added line #L98 was not covered by tests
annotationLocationSetter?.set(annotationId, undefined, undefined);
}
}, [annotationLocationSetter, instanceKey, model]);

Expand Down Expand Up @@ -144,9 +152,9 @@ export const MovableLine = observer(function MovableLine(props: IProps) {
.attr('cy', y);
const annotationId = getAnnotationId(instanceKey, "handle", index===1 ? "lower" : "upper");
if (model.isVisible) {
annotationLocationSetter?.set(annotationId, { x, y });
annotationLocationSetter?.set(annotationId, { x, y }, undefined);
} else {

Check warning on line 156 in src/plugins/graph/adornments/movable-line/movable-line.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/graph/adornments/movable-line/movable-line.tsx#L156

Added line #L156 was not covered by tests
annotationLocationSetter?.set(annotationId, undefined);
annotationLocationSetter?.set(annotationId, undefined, undefined);
}
}
}
Expand Down
16 changes: 15 additions & 1 deletion src/plugins/graph/components/graph-wrapper-component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,19 @@ export const GraphWrapperComponent: React.FC<ITileProps> = observer(function(pro
let coords;
if (objectType === "dot") {
coords = getDotCenter(objectId);
// Check location cache
} else if (content.annotationLocationCache.has(objectId)){
// Check location cache
const location = content.annotationLocationCache.get(objectId);

Check warning on line 117 in src/plugins/graph/components/graph-wrapper-component.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/graph/components/graph-wrapper-component.tsx#L117

Added line #L117 was not covered by tests
if (location) {
const size = content.annotationSizesCache.get(objectId);

Check warning on line 119 in src/plugins/graph/components/graph-wrapper-component.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/graph/components/graph-wrapper-component.tsx#L119

Added line #L119 was not covered by tests
if (size) { // This is a rectangle of defined width & height
const bbox = {

Check warning on line 121 in src/plugins/graph/components/graph-wrapper-component.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/graph/components/graph-wrapper-component.tsx#L121

Added line #L121 was not covered by tests
left: location.x + layout.getComputedBounds("plot").left,
top: location.y + layout.getComputedBounds("plot").top,
...size };
console.log('bbox', bbox);
return bbox;

Check warning on line 126 in src/plugins/graph/components/graph-wrapper-component.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/graph/components/graph-wrapper-component.tsx#L125-L126

Added lines #L125 - L126 were not covered by tests
}
return boundingBoxForPoint(location);

Check warning on line 128 in src/plugins/graph/components/graph-wrapper-component.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/graph/components/graph-wrapper-component.tsx#L128

Added line #L128 was not covered by tests
}
} else {
Expand All @@ -130,8 +139,13 @@ export const GraphWrapperComponent: React.FC<ITileProps> = observer(function(pro
getObjectButtonSVG: ({ classes, handleClick, objectId, objectType }) => {
let coords;
if (objectType === "dot") {
// Native graph object
coords = getDotCenter(objectId);
} else if (content.annotationSizesCache.has(objectId)) {
// Adornment object with rectangle shape; do not return SVG
return undefined;

Check warning on line 146 in src/plugins/graph/components/graph-wrapper-component.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/graph/components/graph-wrapper-component.tsx#L146

Added line #L146 was not covered by tests
} else if (content.annotationLocationCache.has(objectId)){
// Adornment object with dot shape
coords = content.annotationLocationCache.get(objectId);

Check warning on line 149 in src/plugins/graph/components/graph-wrapper-component.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/graph/components/graph-wrapper-component.tsx#L149

Added line #L149 was not covered by tests
} else if (objectType) {
const pos = getPositionFromAdornment(objectType, objectId);
Expand Down
4 changes: 3 additions & 1 deletion src/plugins/graph/graph-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ export type Point = { x: number, y: number };
export type CPLine = { slope: number, intercept: number, pivot1?: Point, pivot2?: Point };
export const kNullPoint = {x: -999, y: -999};

export interface RectSize { width: number, height: number }

export interface Rect {
x: number, y: number, width: number, height: number
}
Expand Down Expand Up @@ -113,7 +115,7 @@ export const GraphEditModes = ["none", "edit", "add"];
export type GraphEditMode = typeof GraphEditModes[number];

export interface ILocationSetterContext {
set: (id: string, location: Point|undefined) => void;
set: (id: string, location: Point|undefined, size: RectSize|undefined) => void;
}

export const LocationSetterContext = createContext<ILocationSetterContext|undefined>(undefined);
13 changes: 10 additions & 3 deletions src/plugins/graph/models/graph-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
import { GraphPlace } from "../imports/components/axis-graph-shared";
import {
GraphAttrRole, GraphEditMode, hoverRadiusFactor, kDefaultAxisLabel, kDefaultNumericAxisBounds, kGraphTileType,
PlotType, PlotTypes, Point, pointRadiusMax, pointRadiusSelectionAddend
PlotType, PlotTypes, Point, pointRadiusMax, pointRadiusSelectionAddend, RectSize
} from "../graph-types";
import { withoutUndo } from "../../../models/history/without-undo";
import { SharedModelType } from "../../../models/shared/shared-model";
Expand Down Expand Up @@ -88,7 +88,8 @@ export const GraphModel = TileContentModel
editingLayerId: undefined as string|undefined,
// Map from annotation IDs to their current locations.
// This allows adornments to flexibly give us these locations.
annotationLocationCache: new ObservableMap<string,Point>()
annotationLocationCache: new ObservableMap<string,Point>(),
annotationSizesCache: new ObservableMap<string,RectSize>()
}))
.preProcessSnapshot((snapshot: any) => {
const hasLayerAlready:boolean = (snapshot?.layers?.length || 0) > 0;
Expand Down Expand Up @@ -414,12 +415,18 @@ export const GraphModel = TileContentModel
setInteractionInProgress(value: boolean) {
self.interactionInProgress = value;
},
setAnnotationLocation(id: string, location: Point|undefined) {
setAnnotationLocation(id: string, location: Point|undefined, size: RectSize|undefined) {
if (location) {
self.annotationLocationCache.set(id, location);
} else {

Check warning on line 421 in src/plugins/graph/models/graph-model.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/graph/models/graph-model.ts#L421

Added line #L421 was not covered by tests
self.annotationLocationCache.delete(id);
}

if (size) {
self.annotationSizesCache.set(id, size);
} else {

Check warning on line 427 in src/plugins/graph/models/graph-model.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/graph/models/graph-model.ts#L427

Added line #L427 was not covered by tests
self.annotationSizesCache.delete(id);
}
}
}))
.actions(self => ({
Expand Down

0 comments on commit 7df677c

Please sign in to comment.