Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions libs/map-editor/src/lib/ai/annotations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,18 @@ export function annotationColour(mark: MapAIAnnotation): string {
);
}

/** Every point a mark covers in viewBox units: its path, its box corners and an arrow's tip. */
export function annotationPoints(mark: MapAIAnnotation): [number, number][] {
const points: [number, number][] = [...(mark.points || [])];
if (mark.x !== undefined && mark.y !== undefined) {
points.push([mark.x, mark.y]);
points.push([mark.x + (mark.width || 0), mark.y + (mark.height || 0)]);
}
if (mark.x2 !== undefined && mark.y2 !== undefined)
points.push([mark.x2, mark.y2]);
return points.filter(([x, y]) => Number.isFinite(x) && Number.isFinite(y));
}

/** The same marks on the canvas overlay and the uploaded snapshot, in viewBox units. */
export function drawAnnotations(
overlay: SVGElement,
Expand Down
19 changes: 5 additions & 14 deletions libs/map-editor/src/lib/ai/context.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { MapDocument, stripEditorAttributes, ViewBox } from '../map-document';
import { MapIndex, MapIndexEntry } from '../map-index';
import { formatDeclarations, StyleRule } from '../style-resolution';
import { annotationPoints } from './annotations';
import { snapshotSize, SnapshotSize } from './snapshot';
import {
MAP_AI_CONTEXT_SCHEMA,
Expand Down Expand Up @@ -721,25 +722,15 @@ export function buildContext(
.slice(0, MAX_MENTIONS)
.map((entry) => entry.anchor);
const box = doc.view_box;
const outside = (options.annotations || []).some((mark) => {
const points = [...(mark.points || [])];
if (mark.x !== undefined && mark.y !== undefined) {
points.push([mark.x, mark.y]);
points.push([
mark.x + (mark.width || 0),
mark.y + (mark.height || 0),
]);
}
if (mark.x2 !== undefined && mark.y2 !== undefined)
points.push([mark.x2, mark.y2]);
return points.some(
const outside = (options.annotations || []).some((mark) =>
annotationPoints(mark).some(
([x, y]) =>
x < box.x ||
y < box.y ||
x > box.x + box.width ||
y > box.y + box.height,
);
});
),
);
const include_root =
!!root_entry &&
(outside || /\b(expand|extend|enlarge)\b/.test(instruction));
Expand Down
18 changes: 2 additions & 16 deletions libs/map-editor/src/lib/ai/snapshot.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { SVG_NS } from '../constants';
import { MapDocument, stripEditorAttributes, ViewBox } from '../map-document';
import { drawAnnotations } from './annotations';
import { annotationPoints, drawAnnotations } from './annotations';
import { MapAIAnnotation } from './types';

export const SNAPSHOT_LONG_SIDE = 2048;
Expand Down Expand Up @@ -65,21 +65,7 @@ export function snapshotMarkup(
options: SnapshotMarkupOptions = {},
): SnapshotMarkup {
const bounds = { ...doc.view_box };
const points = (options.annotations || [])
.flatMap((mark) => {
const result = [...(mark.points || [])];
if (mark.x !== undefined && mark.y !== undefined) {
result.push([mark.x, mark.y]);
result.push([
mark.x + (mark.width || 0),
mark.y + (mark.height || 0),
]);
}
if (mark.x2 !== undefined && mark.y2 !== undefined)
result.push([mark.x2, mark.y2]);
return result;
})
.filter(([x, y]) => Number.isFinite(x) && Number.isFinite(y));
const points = (options.annotations || []).flatMap(annotationPoints);
for (const [x, y] of points) {
const right = Math.max(bounds.x + bounds.width, x);
const bottom = Math.max(bounds.y + bounds.height, y);
Expand Down