-
-
Notifications
You must be signed in to change notification settings - Fork 18
Refactor canvas elements: registry-driven controls #7621
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
Draft
hatton
wants to merge
1
commit into
master
Choose a base branch
from
BL-15770RefactorCanvas
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
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
1,221 changes: 692 additions & 529 deletions
1,221
src/BloomBrowserUI/bookEdit/js/CanvasElementContextControls.tsx
Large diffs are not rendered by default.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
src/BloomBrowserUI/bookEdit/js/CanvasElementKeyboardProvider.ts
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
425 changes: 118 additions & 307 deletions
425
src/BloomBrowserUI/bookEdit/js/CanvasElementManager.ts
Large diffs are not rendered by default.
Oops, something went wrong.
43 changes: 43 additions & 0 deletions
43
src/BloomBrowserUI/bookEdit/js/CanvasElementManagerPublicFunctions.ts
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,43 @@ | ||
| // Small public helpers that other modules can call without importing the full | ||
| // CanvasElementManager class. | ||
| // | ||
| // This file exists to keep CanvasElementManager.ts smaller and to reduce coupling | ||
| // between the page bundle and toolbox UI code. | ||
|
|
||
| import { kCanvasToolId } from "../toolbox/toolIds"; | ||
| import { getToolboxBundleExports } from "./bloomFrames"; | ||
| import { kImageContainerClass } from "./bloomImages"; | ||
|
|
||
| // This is just for debugging. It produces a string that describes the canvas element, generally | ||
| // well enough to identify it in console.log. | ||
| export const canvasElementDescription = ( | ||
| e: Element | null | undefined, | ||
| ): string => { | ||
| const elt = e as HTMLElement; | ||
| if (!elt) { | ||
| return "no canvas element"; | ||
| } | ||
| const result = | ||
| "canvas element at (" + elt.style.left + ", " + elt.style.top + ") "; | ||
| const imageContainer = elt.getElementsByClassName(kImageContainerClass)[0]; | ||
| if (imageContainer) { | ||
| const img = (imageContainer as HTMLElement).getElementsByTagName( | ||
| "img", | ||
| )[0]; | ||
| if (img) { | ||
| return result + "with image : " + img.getAttribute("src"); | ||
| } | ||
| } | ||
| const videoSrc = elt.getElementsByTagName("source")[0]; | ||
| if (videoSrc) { | ||
| return result + "with video " + videoSrc.getAttribute("src"); | ||
| } | ||
| // Enhance: look for videoContainer similarly | ||
| return result + "with text " + elt.innerText; | ||
| }; | ||
|
|
||
| export const showCanvasTool = () => { | ||
| getToolboxBundleExports() | ||
| ?.getTheOneToolbox() | ||
| .activateToolFromId(kCanvasToolId); | ||
| }; |
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
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
30 changes: 30 additions & 0 deletions
30
src/BloomBrowserUI/bookEdit/js/canvasElementManager/CanvasElementAlternates.ts
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,30 @@ | ||
| // Helper functions extracted from CanvasElementManager. | ||
| // | ||
| // This module handles saving canvas element state as the current-language alternate | ||
| // (primarily for bubbles). Keeping it separate helps reduce the size and coupling | ||
| // of CanvasElementManager. | ||
|
|
||
| export const saveStateOfCanvasElementAsCurrentLangAlternate = ( | ||
| canvasElement: HTMLElement, | ||
| canvasElementLangIn?: string, | ||
| ): void => { | ||
| const canvasElementLang = | ||
| canvasElementLangIn ?? GetSettings().languageForNewTextBoxes; | ||
|
|
||
| const editable = Array.from( | ||
| canvasElement.getElementsByClassName("bloom-editable"), | ||
| ).find((e) => e.getAttribute("lang") === canvasElementLang); | ||
| if (editable) { | ||
| const bubbleData = canvasElement.getAttribute("data-bubble") ?? ""; | ||
| const bubbleDataObj = JSON.parse(bubbleData.replace(/`/g, '"')); | ||
| const alternate = { | ||
| lang: canvasElementLang, | ||
| style: canvasElement.getAttribute("style") ?? "", | ||
| tails: bubbleDataObj.tails as object[], | ||
| }; | ||
| editable.setAttribute( | ||
| "data-bubble-alternate", | ||
| JSON.stringify(alternate).replace(/"/g, "`"), | ||
| ); | ||
| } | ||
| }; | ||
172 changes: 172 additions & 0 deletions
172
src/BloomBrowserUI/bookEdit/js/canvasElementManager/CanvasElementGeometry.ts
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,172 @@ | ||
| // Helper functions extracted from CanvasElementManager. | ||
| // | ||
| // These are geometry/pixel conversion utilities used by the editable-page bundle | ||
| // when positioning and hit-testing canvas elements. They intentionally avoid taking | ||
| // a dependency on the full CanvasElementManager class to help keep that file smaller | ||
| // and reduce import coupling. | ||
|
|
||
| import { Point, PointScaling } from "../point"; | ||
| import { reportError } from "../../../lib/errorHandler"; | ||
|
|
||
| export const convertPointFromViewportToElementFrame = ( | ||
| pointRelativeToViewport: Point, | ||
| element: Element, | ||
| ): Point => { | ||
| const referenceBounds = element.getBoundingClientRect(); | ||
| const origin = new Point( | ||
| referenceBounds.left, | ||
| referenceBounds.top, | ||
| PointScaling.Scaled, | ||
| "BoundingClientRect (Relative to viewport)", | ||
| ); | ||
|
|
||
| const border = getLeftAndTopBorderWidths(element); | ||
| const padding = getLeftAndTopPaddings(element); | ||
| const borderAndPadding = border.add(padding); | ||
|
|
||
| const scroll = getScrollAmount(element); | ||
| if (scroll.length() > 0.001) { | ||
| const error = new Error( | ||
| `Assert failed. container.scroll expected to be (0, 0), but it was: (${scroll.getScaledX()}, ${scroll.getScaledY()})`, | ||
| ); | ||
| reportError(error.message, error.stack || ""); | ||
| } | ||
|
|
||
| return pointRelativeToViewport.subtract(origin).subtract(borderAndPadding); | ||
| }; | ||
|
|
||
| export const getLeftAndTopBorderWidths = (element: Element): Point => { | ||
| return new Point( | ||
| element.clientLeft, | ||
| element.clientTop, | ||
| PointScaling.Unscaled, | ||
| "Element ClientLeft/Top (Unscaled)", | ||
| ); | ||
| }; | ||
|
|
||
| export const getRightAndBottomBorderWidths = ( | ||
| element: Element, | ||
| styleInfo?: CSSStyleDeclaration, | ||
| ): Point => { | ||
| if (!styleInfo) { | ||
| styleInfo = window.getComputedStyle(element); | ||
| } | ||
|
|
||
| const borderRight: number = extractNumber( | ||
| styleInfo.getPropertyValue("border-right-width"), | ||
| ); | ||
| const borderBottom: number = extractNumber( | ||
| styleInfo.getPropertyValue("border-bottom-width"), | ||
| ); | ||
|
|
||
| return new Point( | ||
| borderRight, | ||
| borderBottom, | ||
| PointScaling.Unscaled, | ||
| "Element ClientRight/Bottom (Unscaled)", | ||
| ); | ||
| }; | ||
|
|
||
| export const getCombinedBorderWidths = ( | ||
| element: Element, | ||
| styleInfo?: CSSStyleDeclaration, | ||
| ): Point => { | ||
| if (!styleInfo) { | ||
| styleInfo = window.getComputedStyle(element); | ||
| } | ||
|
|
||
| return getLeftAndTopBorderWidths(element).add( | ||
| getRightAndBottomBorderWidths(element, styleInfo), | ||
| ); | ||
| }; | ||
|
|
||
| export const getPadding = ( | ||
| side: string, | ||
| styleInfo: CSSStyleDeclaration, | ||
| ): number => { | ||
| const propertyKey = `padding-${side}`; | ||
| const paddingString = styleInfo.getPropertyValue(propertyKey); | ||
| return extractNumber(paddingString); | ||
| }; | ||
|
|
||
| export const getLeftAndTopPaddings = ( | ||
| element: Element, | ||
| styleInfo?: CSSStyleDeclaration, | ||
| ): Point => { | ||
| if (!styleInfo) { | ||
| styleInfo = window.getComputedStyle(element); | ||
| } | ||
|
|
||
| return new Point( | ||
| getPadding("left", styleInfo), | ||
| getPadding("top", styleInfo), | ||
| PointScaling.Unscaled, | ||
| "CSSStyleDeclaration padding", | ||
| ); | ||
| }; | ||
|
|
||
| export const getRightAndBottomPaddings = ( | ||
| element: Element, | ||
| styleInfo?: CSSStyleDeclaration, | ||
| ): Point => { | ||
| if (!styleInfo) { | ||
| styleInfo = window.getComputedStyle(element); | ||
| } | ||
|
|
||
| return new Point( | ||
| getPadding("right", styleInfo), | ||
| getPadding("bottom", styleInfo), | ||
| PointScaling.Unscaled, | ||
| "Padding", | ||
| ); | ||
| }; | ||
|
|
||
| export const getCombinedPaddings = ( | ||
| element: Element, | ||
| styleInfo?: CSSStyleDeclaration, | ||
| ): Point => { | ||
| if (!styleInfo) { | ||
| styleInfo = window.getComputedStyle(element); | ||
| } | ||
|
|
||
| return getLeftAndTopPaddings(element, styleInfo).add( | ||
| getRightAndBottomPaddings(element, styleInfo), | ||
| ); | ||
| }; | ||
|
|
||
| export const getCombinedBordersAndPaddings = (element: Element): Point => { | ||
| const styleInfo = window.getComputedStyle(element); | ||
| const borders = getCombinedBorderWidths(element); | ||
| const paddings = getCombinedPaddings(element, styleInfo); | ||
| return borders.add(paddings); | ||
| }; | ||
|
|
||
| export const getScrollAmount = (element: Element): Point => { | ||
| return new Point( | ||
| element.scrollLeft, | ||
| element.scrollTop, | ||
| PointScaling.Unscaled, | ||
| "Element ScrollLeft/Top (Unscaled)", | ||
| ); | ||
| }; | ||
|
|
||
| export const extractNumber = (text: string | undefined | null): number => { | ||
| if (!text) { | ||
| return 0; | ||
| } | ||
|
|
||
| let i = 0; | ||
| for (i = 0; i < text.length; ++i) { | ||
| const c = text.charAt(i); | ||
| if ((c < "0" || c > "9") && c !== "-" && c !== "+" && c !== ".") { | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| let numberStr = ""; | ||
| if (i > 0) { | ||
| numberStr = text.substring(0, i); | ||
| } | ||
|
|
||
| return Number(numberStr); | ||
| }; |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔴 Missing GetSettings() type declaration in CanvasElementAlternates.ts causes TypeScript error
The new file
CanvasElementAlternates.tsuses the globally injectedGetSettings()function at line 12 but lacks the required triple-slash reference directive to declare it.Click to expand
Problem
The file calls
GetSettings().languageForNewTextBoxesbutGetSettingsis not declared in scope. This function is injected by C# at runtime, but TypeScript needs the type declaration to compile.Evidence
Other files in the codebase that use
GetSettings()include the reference directive:CanvasElementManager.ts:8:/// <reference path="./collectionSettings.d.ts"/>BloomSourceBubbles.tsx:10:/// <reference path="../js/collectionSettings.d.ts"/>StyleEditor.ts:11:/// <reference path="../js/collectionSettings.d.ts"/>PlaceholderProvider.ts:5:/// <reference path="./collectionSettings.d.ts" />BloomHintBubbles.ts:5:/// <reference path="./collectionSettings.d.ts" />Impact
This will cause a TypeScript compilation error:
Cannot find name 'GetSettings'.Recommendation: Add the triple-slash reference directive at the top of the file:
/// <reference path="../collectionSettings.d.ts"/>Was this helpful? React with 👍 or 👎 to provide feedback.