-
Notifications
You must be signed in to change notification settings - Fork 3.8k
feat: mini workspace bubble #7096
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
BeksOmega
merged 9 commits into
google:develop
from
BeksOmega:feat/mini-workspace-bubble
May 19, 2023
Merged
Changes from 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
a2eb80a
feat: add properly sizing mini workspace bubble
BeksOmega 2db7a29
chore: add properly handling workspace options
BeksOmega 2bd8de3
fix: various sizing and option bugs
BeksOmega fee2c9f
fix: code related to dragging
BeksOmega ed21204
fix: remove adding flyout change listener
BeksOmega e5048ed
chore: add docs
BeksOmega 9e55a3c
fix: build
BeksOmega 52c6172
fix: PR comments'
BeksOmega fb5ccf4
chore: PR comments
BeksOmega File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,199 @@ | ||
/** | ||
* @license | ||
* Copyright 2023 Google LLC | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import {Abstract as AbstractEvent} from '../events/events_abstract.js'; | ||
import type {BlocklyOptions} from '../blockly_options.js'; | ||
import {Bubble} from './bubble.js'; | ||
import type {Coordinate} from '../utils/coordinate.js'; | ||
import * as dom from '../utils/dom.js'; | ||
import {Options} from '../options.js'; | ||
import {Svg} from '../utils/svg.js'; | ||
import type {Rect} from '../utils/rect.js'; | ||
import {Size} from '../utils/size.js'; | ||
import {WorkspaceSvg} from '../workspace_svg.js'; | ||
|
||
export class MiniWorkspaceBubble extends Bubble { | ||
private svgDialog: SVGElement; | ||
private miniWorkspace: WorkspaceSvg; | ||
private autoLayout = true; | ||
|
||
constructor( | ||
workspaceOptions: BlocklyOptions, | ||
protected readonly workspace: WorkspaceSvg, | ||
protected anchor: Coordinate, | ||
protected ownerRect?: Rect | ||
) { | ||
super(workspace, anchor, ownerRect); | ||
const options = new Options(workspaceOptions); | ||
this.validateWorkspaceOptions(options); | ||
|
||
this.svgDialog = dom.createSvgElement( | ||
Svg.SVG, | ||
{ | ||
'x': Bubble.BORDER_WIDTH, | ||
'y': Bubble.BORDER_WIDTH, | ||
}, | ||
this.contentContainer | ||
); | ||
workspaceOptions.parentWorkspace = this.workspace; | ||
this.miniWorkspace = this.newWorkspaceSvg(new Options(workspaceOptions)); | ||
cpcallen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const background = this.miniWorkspace.createDom('blocklyMutatorBackground'); | ||
this.svgDialog.appendChild(background); | ||
if (options.languageTree) { | ||
background.insertBefore( | ||
this.miniWorkspace.addFlyout(Svg.G), | ||
this.miniWorkspace.getCanvas() | ||
); | ||
const flyout = this.miniWorkspace.getFlyout(); | ||
flyout?.init(this.miniWorkspace); | ||
flyout?.show(options.languageTree); | ||
} | ||
|
||
this.miniWorkspace.addChangeListener(this.updateBubbleSize.bind(this)); | ||
this.miniWorkspace | ||
.getFlyout() | ||
?.getWorkspace() | ||
?.addChangeListener(this.updateBubbleSize.bind(this)); | ||
this.updateBubbleSize(); | ||
} | ||
|
||
dispose() { | ||
this.miniWorkspace.dispose(); | ||
super.dispose(); | ||
} | ||
|
||
/** @internal */ | ||
getWorkspace(): WorkspaceSvg { | ||
return this.miniWorkspace; | ||
} | ||
|
||
/** Adds a change listener to the mini workspace. */ | ||
addWorkspaceChangeListener(listener: (e: AbstractEvent) => void) { | ||
this.miniWorkspace.addChangeListener(listener); | ||
} | ||
BeksOmega marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/** | ||
* Validates the workspace options to make sure folks aren't trying to | ||
* enable things the miniworkspace doesn't support. | ||
*/ | ||
private validateWorkspaceOptions(options: Options) { | ||
if (options.hasCategories) { | ||
throw new Error( | ||
'The miniworkspace bubble does not support toolboxes with categories' | ||
); | ||
} | ||
if (options.hasTrashcan) { | ||
throw new Error('The miniworkspace bubble does not support trashcans'); | ||
} | ||
if ( | ||
options.zoomOptions.controls || | ||
options.zoomOptions.wheel || | ||
options.zoomOptions.pinch | ||
) { | ||
throw new Error('The miniworkspace bubble does not support zooming'); | ||
} | ||
if ( | ||
options.moveOptions.scrollbars || | ||
options.moveOptions.wheel || | ||
options.moveOptions.drag | ||
) { | ||
throw new Error( | ||
'The miniworkspace bubble does not scrolling/moving the workspace' | ||
); | ||
} | ||
if (options.horizontalLayout) { | ||
throw new Error( | ||
'The miniworkspace bubble does not support horizontal layouts' | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* Updates the size of this bubble to account for the size of the | ||
* mini workspace. | ||
*/ | ||
private updateBubbleSize() { | ||
if (this.miniWorkspace.isDragging()) return; | ||
|
||
const currSize = this.getSize(); | ||
const newSize = this.calculateWorkspaceSize(); | ||
if ( | ||
Math.abs(currSize.width - newSize.width) < 10 && | ||
Math.abs(currSize.height - newSize.height) < 10 | ||
BeksOmega marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) { | ||
// Only resize if the size has noticeably changed. | ||
return; | ||
} | ||
this.svgDialog.setAttribute('width', `${newSize.width}px`); | ||
this.svgDialog.setAttribute('height', `${newSize.height}px`); | ||
this.miniWorkspace.setCachedParentSvgSize(newSize.width, newSize.height); | ||
if (this.miniWorkspace.RTL) { | ||
this.miniWorkspace | ||
.getCanvas() | ||
.setAttribute('transform', `translate(${newSize.width}, 0)`); | ||
} | ||
BeksOmega marked this conversation as resolved.
Show resolved
Hide resolved
|
||
this.setSize( | ||
new Size( | ||
newSize.width + Bubble.DOUBLE_BORDER, | ||
newSize.height + Bubble.DOUBLE_BORDER | ||
), | ||
this.autoLayout | ||
); | ||
this.miniWorkspace.resize(); | ||
this.miniWorkspace.recordDragTargets(); | ||
} | ||
|
||
/** | ||
* Calculates the size of the mini workspace for use in resizing the bubble. | ||
*/ | ||
private calculateWorkspaceSize(): Size { | ||
const canvas = this.miniWorkspace.getCanvas(); | ||
const bbox = canvas.getBBox(); | ||
let width = this.miniWorkspace.RTL ? -bbox.x : bbox.width; | ||
BeksOmega marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let height = bbox.height + bbox.y + Bubble.DOUBLE_BORDER * 3; | ||
const flyout = this.miniWorkspace.getFlyout(); | ||
if (flyout) { | ||
const flyoutScrollMetrics = flyout | ||
.getWorkspace() | ||
.getMetricsManager() | ||
.getScrollMetrics(); | ||
BeksOmega marked this conversation as resolved.
Show resolved
Hide resolved
|
||
height = Math.max(height, flyoutScrollMetrics.height + 20); | ||
if (this.miniWorkspace.RTL) { | ||
width = Math.max(width, flyout.getWidth()); | ||
} else { | ||
width += flyout.getWidth(); | ||
} | ||
} | ||
width += Bubble.DOUBLE_BORDER * 3; | ||
BeksOmega marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return new Size(width, height); | ||
} | ||
|
||
/** | ||
* Move this bubble during a drag. | ||
* | ||
* @param newLoc The location to translate to, in workspace coordinates. | ||
* @internal | ||
*/ | ||
moveDuringDrag(newLoc: Coordinate): void { | ||
super.moveDuringDrag(newLoc); | ||
this.autoLayout = false; | ||
} | ||
|
||
/** @internal */ | ||
moveTo(x: number, y: number): void { | ||
super.moveTo(x, y); | ||
this.miniWorkspace.recordDragTargets(); | ||
} | ||
|
||
/** @internal */ | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
newWorkspaceSvg(options: Options): WorkspaceSvg { | ||
throw new Error( | ||
'The implementation of newWorkspaceSvg should be ' + | ||
'monkey-patched in by blockly.ts' | ||
); | ||
} | ||
cpcallen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.