refactor: Remove duplicated method from contextmenu_items.ts. (#9289)

This commit is contained in:
Aaron Dodson
2025-08-12 08:55:54 -07:00
committed by GitHub
parent 79d3140495
commit fb63360b9f

View File

@@ -23,6 +23,7 @@ import {CommentIcon} from './icons/comment_icon.js';
import {Msg} from './msg.js'; import {Msg} from './msg.js';
import {StatementInput} from './renderers/zelos/zelos.js'; import {StatementInput} from './renderers/zelos/zelos.js';
import {Coordinate} from './utils/coordinate.js'; import {Coordinate} from './utils/coordinate.js';
import * as svgMath from './utils/svg_math.js';
import type {WorkspaceSvg} from './workspace_svg.js'; import type {WorkspaceSvg} from './workspace_svg.js';
function isFullBlockField(block?: BlockSvg) { function isFullBlockField(block?: BlockSvg) {
@@ -637,9 +638,9 @@ export function registerCommentCreate() {
const comment = new RenderedWorkspaceComment(workspace); const comment = new RenderedWorkspaceComment(workspace);
comment.setPlaceholderText(Msg['WORKSPACE_COMMENT_DEFAULT_TEXT']); comment.setPlaceholderText(Msg['WORKSPACE_COMMENT_DEFAULT_TEXT']);
comment.moveTo( comment.moveTo(
pixelsToWorkspaceCoords( svgMath.screenToWsCoordinates(
new Coordinate(location.x, location.y),
workspace, workspace,
new Coordinate(location.x, location.y),
), ),
); );
getFocusManager().focusNode(comment); getFocusManager().focusNode(comment);
@@ -652,40 +653,6 @@ export function registerCommentCreate() {
ContextMenuRegistry.registry.register(createOption); ContextMenuRegistry.registry.register(createOption);
} }
/**
* Converts pixel coordinates (relative to the window) to workspace coordinates.
*/
function pixelsToWorkspaceCoords(
pixelCoord: Coordinate,
workspace: WorkspaceSvg,
): Coordinate {
const injectionDiv = workspace.getInjectionDiv();
// Bounding rect coordinates are in client coordinates, meaning that they
// are in pixels relative to the upper left corner of the visible browser
// window. These coordinates change when you scroll the browser window.
const boundingRect = injectionDiv.getBoundingClientRect();
// The client coordinates offset by the injection div's upper left corner.
const clientOffsetPixels = new Coordinate(
pixelCoord.x - boundingRect.left,
pixelCoord.y - boundingRect.top,
);
// The offset in pixels between the main workspace's origin and the upper
// left corner of the injection div.
const mainOffsetPixels = workspace.getOriginOffsetInPixels();
// The position of the new comment in pixels relative to the origin of the
// main workspace.
const finalOffset = Coordinate.difference(
clientOffsetPixels,
mainOffsetPixels,
);
// The position of the new comment in main workspace coordinates.
finalOffset.scale(1 / workspace.scale);
return finalOffset;
}
/** Registers all block-scoped context menu items. */ /** Registers all block-scoped context menu items. */
function registerBlockOptions_() { function registerBlockOptions_() {
registerDuplicate(); registerDuplicate();