From aeb86c7d2d3c471269f69ae4106bdc3641693274 Mon Sep 17 00:00:00 2001 From: Sophie Nguyen <123401167+SophieNguyen113@users.noreply.github.com> Date: Fri, 22 Sep 2023 17:12:04 -0400 Subject: [PATCH 1/2] Feat: Added an API to programmatically scroll the workspace Feat: Added an API to programmatically scroll the workspace --- core/workspace_svg.ts | 98 +++++++++++++++++++++---------------------- 1 file changed, 49 insertions(+), 49 deletions(-) diff --git a/core/workspace_svg.ts b/core/workspace_svg.ts index 00ebcbdc1a3..364bb094837 100644 --- a/core/workspace_svg.ts +++ b/core/workspace_svg.ts @@ -2185,57 +2185,57 @@ export class WorkspaceSvg extends Workspace implements IASTNodeLocationSvg { } /** - * Scroll the workspace to a specified offset (in pixels), keeping in the - * workspace bounds. See comment on workspaceSvg.scrollX for more detail on - * the meaning of these values. - * - * @param x Target X to scroll to. - * @param y Target Y to scroll to. - * @internal - */ - scroll(x: number, y: number) { - this.hideChaff(/* opt_onlyClosePopups= */ true); - - // Keep scrolling within the bounds of the content. - const metrics = this.getMetrics(); - // Canvas coordinates (aka scroll coordinates) have inverse directionality - // to workspace coordinates so we have to inverse them. - x = Math.min(x, -metrics.scrollLeft); - y = Math.min(y, -metrics.scrollTop); - const maxXDisplacement = Math.max( - 0, - metrics.scrollWidth - metrics.viewWidth, - ); - const maxXScroll = metrics.scrollLeft + maxXDisplacement; - const maxYDisplacement = Math.max( - 0, - metrics.scrollHeight - metrics.viewHeight, + * Scroll the workspace to a specified offset (in pixels), keeping in the + * workspace bounds. See comment on workspaceSvg.scrollX for more detail on + * the meaning of these values. + * + * @param x Target X to scroll to. + * @param y Target Y to scroll to. + */ +scroll(x: number, y: number) { + this.hideChaff(/* opt_onlyClosePopups= */ true); + + // Keep scrolling within the bounds of the content. + const metrics = this.getMetrics(); + // Canvas coordinates (aka scroll coordinates) have inverse directionality + // to workspace coordinates so we have to inverse them. + x = Math.min(x, -metrics.scrollLeft); + y = Math.min(y, -metrics.scrollTop); + const maxXDisplacement = Math.max( + 0, + metrics.scrollWidth - metrics.viewWidth, + ); + const maxXScroll = metrics.scrollLeft + maxXDisplacement; + const maxYDisplacement = Math.max( + 0, + metrics.scrollHeight - metrics.viewHeight, + ); + const maxYScroll = metrics.scrollTop + maxYDisplacement; + x = Math.max(x, -maxXScroll); + y = Math.max(y, -maxYScroll); + this.scrollX = x; + this.scrollY = y; + + if (this.scrollbar) { + // The content position (displacement from the content's top-left to the + // origin) plus the scroll position (displacement from the view's top-left + // to the origin) gives us the distance from the view's top-left to the + // content's top-left. Then we negate this so we get the displacement from + // the content's top-left to the view's top-left, matching the + // directionality of the scrollbars. + this.scrollbar.set( + -(x + metrics.scrollLeft), + -(y + metrics.scrollTop), + false, ); - const maxYScroll = metrics.scrollTop + maxYDisplacement; - x = Math.max(x, -maxXScroll); - y = Math.max(y, -maxYScroll); - this.scrollX = x; - this.scrollY = y; - - if (this.scrollbar) { - // The content position (displacement from the content's top-left to the - // origin) plus the scroll position (displacement from the view's top-left - // to the origin) gives us the distance from the view's top-left to the - // content's top-left. Then we negate this so we get the displacement from - // the content's top-left to the view's top-left, matching the - // directionality of the scrollbars. - this.scrollbar.set( - -(x + metrics.scrollLeft), - -(y + metrics.scrollTop), - false, - ); - } - // We have to shift the translation so that when the canvas is at 0, 0 the - // workspace origin is not underneath the toolbox. - x += metrics.absoluteLeft; - y += metrics.absoluteTop; - this.translate(x, y); } + // We have to shift the translation so that when the canvas is at 0, 0 the + // workspace origin is not underneath the toolbox. + x += metrics.absoluteLeft; + y += metrics.absoluteTop; + this.translate(x, y); +} + /** * Find the block on this workspace with the specified ID. From f1c275b7669841ce5a4883ad1bf7fd8fd5678149 Mon Sep 17 00:00:00 2001 From: Sophie Nguyen <123401167+SophieNguyen113@users.noreply.github.com> Date: Sat, 23 Sep 2023 01:20:13 +0000 Subject: [PATCH 2/2] Feat: Added an API to programmatically scroll the workspace --- core/workspace_svg.ts | 97 +++++++++++++++++++++---------------------- 1 file changed, 48 insertions(+), 49 deletions(-) diff --git a/core/workspace_svg.ts b/core/workspace_svg.ts index 364bb094837..6df044cde7a 100644 --- a/core/workspace_svg.ts +++ b/core/workspace_svg.ts @@ -2185,57 +2185,56 @@ export class WorkspaceSvg extends Workspace implements IASTNodeLocationSvg { } /** - * Scroll the workspace to a specified offset (in pixels), keeping in the - * workspace bounds. See comment on workspaceSvg.scrollX for more detail on - * the meaning of these values. - * - * @param x Target X to scroll to. - * @param y Target Y to scroll to. - */ -scroll(x: number, y: number) { - this.hideChaff(/* opt_onlyClosePopups= */ true); - - // Keep scrolling within the bounds of the content. - const metrics = this.getMetrics(); - // Canvas coordinates (aka scroll coordinates) have inverse directionality - // to workspace coordinates so we have to inverse them. - x = Math.min(x, -metrics.scrollLeft); - y = Math.min(y, -metrics.scrollTop); - const maxXDisplacement = Math.max( - 0, - metrics.scrollWidth - metrics.viewWidth, - ); - const maxXScroll = metrics.scrollLeft + maxXDisplacement; - const maxYDisplacement = Math.max( - 0, - metrics.scrollHeight - metrics.viewHeight, - ); - const maxYScroll = metrics.scrollTop + maxYDisplacement; - x = Math.max(x, -maxXScroll); - y = Math.max(y, -maxYScroll); - this.scrollX = x; - this.scrollY = y; - - if (this.scrollbar) { - // The content position (displacement from the content's top-left to the - // origin) plus the scroll position (displacement from the view's top-left - // to the origin) gives us the distance from the view's top-left to the - // content's top-left. Then we negate this so we get the displacement from - // the content's top-left to the view's top-left, matching the - // directionality of the scrollbars. - this.scrollbar.set( - -(x + metrics.scrollLeft), - -(y + metrics.scrollTop), - false, + * Scroll the workspace to a specified offset (in pixels), keeping in the + * workspace bounds. See comment on workspaceSvg.scrollX for more detail on + * the meaning of these values. + * + * @param x Target X to scroll to. + * @param y Target Y to scroll to. + */ + scroll(x: number, y: number) { + this.hideChaff(/* opt_onlyClosePopups= */ true); + + // Keep scrolling within the bounds of the content. + const metrics = this.getMetrics(); + // Canvas coordinates (aka scroll coordinates) have inverse directionality + // to workspace coordinates so we have to inverse them. + x = Math.min(x, -metrics.scrollLeft); + y = Math.min(y, -metrics.scrollTop); + const maxXDisplacement = Math.max( + 0, + metrics.scrollWidth - metrics.viewWidth, ); - } - // We have to shift the translation so that when the canvas is at 0, 0 the - // workspace origin is not underneath the toolbox. - x += metrics.absoluteLeft; - y += metrics.absoluteTop; - this.translate(x, y); -} + const maxXScroll = metrics.scrollLeft + maxXDisplacement; + const maxYDisplacement = Math.max( + 0, + metrics.scrollHeight - metrics.viewHeight, + ); + const maxYScroll = metrics.scrollTop + maxYDisplacement; + x = Math.max(x, -maxXScroll); + y = Math.max(y, -maxYScroll); + this.scrollX = x; + this.scrollY = y; + if (this.scrollbar) { + // The content position (displacement from the content's top-left to the + // origin) plus the scroll position (displacement from the view's top-left + // to the origin) gives us the distance from the view's top-left to the + // content's top-left. Then we negate this so we get the displacement from + // the content's top-left to the view's top-left, matching the + // directionality of the scrollbars. + this.scrollbar.set( + -(x + metrics.scrollLeft), + -(y + metrics.scrollTop), + false, + ); + } + // We have to shift the translation so that when the canvas is at 0, 0 the + // workspace origin is not underneath the toolbox. + x += metrics.absoluteLeft; + y += metrics.absoluteTop; + this.translate(x, y); + } /** * Find the block on this workspace with the specified ID.