Skip to content

Commit fb63360

Browse files
authored
refactor: Remove duplicated method from contextmenu_items.ts. (#9289)
1 parent 79d3140 commit fb63360

1 file changed

Lines changed: 3 additions & 36 deletions

File tree

core/contextmenu_items.ts

Lines changed: 3 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {CommentIcon} from './icons/comment_icon.js';
2323
import {Msg} from './msg.js';
2424
import {StatementInput} from './renderers/zelos/zelos.js';
2525
import {Coordinate} from './utils/coordinate.js';
26+
import * as svgMath from './utils/svg_math.js';
2627
import type {WorkspaceSvg} from './workspace_svg.js';
2728

2829
function isFullBlockField(block?: BlockSvg) {
@@ -637,9 +638,9 @@ export function registerCommentCreate() {
637638
const comment = new RenderedWorkspaceComment(workspace);
638639
comment.setPlaceholderText(Msg['WORKSPACE_COMMENT_DEFAULT_TEXT']);
639640
comment.moveTo(
640-
pixelsToWorkspaceCoords(
641-
new Coordinate(location.x, location.y),
641+
svgMath.screenToWsCoordinates(
642642
workspace,
643+
new Coordinate(location.x, location.y),
643644
),
644645
);
645646
getFocusManager().focusNode(comment);
@@ -652,40 +653,6 @@ export function registerCommentCreate() {
652653
ContextMenuRegistry.registry.register(createOption);
653654
}
654655

655-
/**
656-
* Converts pixel coordinates (relative to the window) to workspace coordinates.
657-
*/
658-
function pixelsToWorkspaceCoords(
659-
pixelCoord: Coordinate,
660-
workspace: WorkspaceSvg,
661-
): Coordinate {
662-
const injectionDiv = workspace.getInjectionDiv();
663-
// Bounding rect coordinates are in client coordinates, meaning that they
664-
// are in pixels relative to the upper left corner of the visible browser
665-
// window. These coordinates change when you scroll the browser window.
666-
const boundingRect = injectionDiv.getBoundingClientRect();
667-
668-
// The client coordinates offset by the injection div's upper left corner.
669-
const clientOffsetPixels = new Coordinate(
670-
pixelCoord.x - boundingRect.left,
671-
pixelCoord.y - boundingRect.top,
672-
);
673-
674-
// The offset in pixels between the main workspace's origin and the upper
675-
// left corner of the injection div.
676-
const mainOffsetPixels = workspace.getOriginOffsetInPixels();
677-
678-
// The position of the new comment in pixels relative to the origin of the
679-
// main workspace.
680-
const finalOffset = Coordinate.difference(
681-
clientOffsetPixels,
682-
mainOffsetPixels,
683-
);
684-
// The position of the new comment in main workspace coordinates.
685-
finalOffset.scale(1 / workspace.scale);
686-
return finalOffset;
687-
}
688-
689656
/** Registers all block-scoped context menu items. */
690657
function registerBlockOptions_() {
691658
registerDuplicate();

0 commit comments

Comments
 (0)