-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
Image jumps when resizing in container that has padding #16304
Conversation
WalkthroughThe recent updates to CKEditor5 focus on enhancing image resizing functionalities. The changes include the addition of utility functions for resize calculations, optimization of test configurations to align with these updates, and a more efficient method for setting multiple styles simultaneously. Changes
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (invoked as PR comments)
Additionally, you can add CodeRabbit Configration File (
|
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.
Actionable comments posted: 1
Out of diff range and nitpick comments (1)
packages/ckeditor5-widget/src/utils.ts (1)
Line range hint
456-517
: Consider refactoringcalculateResizeHostAncestorWidth
to improve readability and maintainability.- const getElementComputedWidth = ( element: HTMLElement ) => { - const { width, paddingLeft, paddingRight } = element.ownerDocument.defaultView!.getComputedStyle( element! ); - return parseFloat( width ) - parseFloat( paddingLeft ) - parseFloat( paddingRight ); - }; - if ( isMaximumResizeHostAncestorElement( domResizeHost ) ) { - return getElementComputedWidth( domResizeHost ); - } - const domResizeHostParent = domResizeHost.parentElement; - if ( !domResizeHostParent ) { - return 0; - } - let parentWidth = getElementComputedWidth( domResizeHostParent! ); - if ( isMaximumResizeHostAncestorElement( domResizeHostParent ) ) { - return parentWidth; - } - const ancestorLevelLimit = 5; - let currentLevel = 0; - let checkedElement = domResizeHostParent!; - while ( isNaN( parentWidth ) ) { - checkedElement = checkedElement.parentElement!; - if ( ++currentLevel > ancestorLevelLimit ) { - return 0; - } - parentWidth = getElementComputedWidth( checkedElement ); - if ( isMaximumResizeHostAncestorElement( checkedElement ) ) { - break; - } - } - return parentWidth; + // Refactored code here
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.
Actionable comments posted: 2
packages/ckeditor5-image/tests/imageresize/utils/getselectedimagewidthinunits.js
Show resolved
Hide resolved
packages/ckeditor5-image/tests/imageresize/utils/getselectedimagewidthinunits.js
Show resolved
Hide resolved
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.
- The part of the fix with calculating ancestor width excluding padding is fine 👍
- The part with
isMaximumResizeHostAncestorElement()
is, in my opinion, only masking an underlying bug inImageResizeHandles
. I think this issue has nothing to do with pagination, this popped up in pagination samples only because they use a specific combination of editing root/paddings/wrappers in DOM. For instance, currently onmaster
this will also fail if you enable image resize in an inline editor:
Screen.Recording.2024-05-06.at.17.06.24.mov
Long story short, the entire solution that worked for me (relative to master
) is listed below.
diff --git a/packages/ckeditor5-image/src/imageresize/imageresizehandles.ts b/packages/ckeditor5-image/src/imageresize/imageresizehandles.ts
index f311ddaa70..479ec976ef 100644
--- a/packages/ckeditor5-image/src/imageresize/imageresizehandles.ts
+++ b/packages/ckeditor5-image/src/imageresize/imageresizehandles.ts
@@ -101,8 +101,7 @@ export default class ImageResizeHandles extends Plugin {
return domWidgetElement.querySelector( 'img' )!;
},
getResizeHost() {
- // Return the model image element parent to avoid setting an inline element (<a>/<span>) as a resize host.
- return domConverter.mapViewToDom( mapper.toViewElement( imageModel.parent as Element )! ) as HTMLElement;
+ return domConverter.mapViewToDom( mapper.toViewElement( imageModel as Element )! ) as HTMLElement;
},
isCentered() {
diff --git a/packages/ckeditor5-widget/src/utils.ts b/packages/ckeditor5-widget/src/utils.ts
index dc2eefaa8c..4b8a2e2b62 100644
--- a/packages/ckeditor5-widget/src/utils.ts
+++ b/packages/ckeditor5-widget/src/utils.ts
@@ -459,6 +459,12 @@ function addSelectionHandle( widgetElement: ViewContainerElement, writer: Downca
* @returns Width of ancestor element in pixels or 0 if no ancestor with a computed width has been found.
*/
export function calculateResizeHostAncestorWidth( domResizeHost: HTMLElement ): number {
+ const getElementComputedWidthExcludingPadding = ( element: HTMLElement ) => {
+ const { width, paddingLeft, paddingRight } = element.ownerDocument.defaultView!.getComputedStyle( element! );
+
+ return parseFloat( width ) - ( parseFloat( paddingLeft ) || 0 ) - ( parseFloat( paddingRight ) || 0 );
+ };
+
const domResizeHostParent = domResizeHost.parentElement;
if ( !domResizeHostParent ) {
@@ -466,7 +472,7 @@ export function calculateResizeHostAncestorWidth( domResizeHost: HTMLElement ):
}
// Need to use computed style as it properly excludes parent's paddings from the returned value.
- let parentWidth = parseFloat( domResizeHostParent!.ownerDocument.defaultView!.getComputedStyle( domResizeHostParent! ).width );
+ let parentWidth = getElementComputedWidthExcludingPadding( domResizeHostParent );
// Sometimes parent width cannot be accessed. If that happens we should go up in the elements tree
// and try to get width from next ancestor.
@@ -483,9 +489,7 @@ export function calculateResizeHostAncestorWidth( domResizeHost: HTMLElement ):
return 0;
}
- parentWidth = parseFloat(
- domResizeHostParent!.ownerDocument.defaultView!.getComputedStyle( checkedElement ).width
- );
+ parentWidth = getElementComputedWidthExcludingPadding( checkedElement );
}
return parentWidth;
I altered the behavior of getResizeHost()
in ImageResizeHandles
because I think it is incorrect. Please keep in mind that there are unit tests that would say otherwise (an aftermath of #9598)
ckeditor5/packages/ckeditor5-image/tests/imageresize/imageresizehandles.js
Lines 1042 to 1058 in 67d2d60
it( 'should set the list item as the resize host if an image is inside a to-do list', async () => { editor = await createEditor( { plugins: [ Image, ImageResizeEditing, ImageResizeHandles, LegacyTodoList, Paragraph ] } ); await setModelAndWaitForImages( editor, '<listItem listType="todo" listIndent="0">' + `[<imageInline linkHref="http://ckeditor.com" src="${ IMAGE_SRC_FIXTURE }" alt="alt text"></imageInline>]` + '</listItem>' ); const resizer = Array.from( editor.plugins.get( 'WidgetResize' )._resizers.values() )[ 0 ]; expect( resizer._getResizeHost().nodeName ).to.equal( 'LI' ); await editor.destroy(); } ); ckeditor5/packages/ckeditor5-image/tests/imageresize/imageresizehandles.js
Lines 998 to 1008 in 67d2d60
it( 'should set the paragraph as the resize host for an image wrapped with a link', async () => { await setModelAndWaitForImages( editor, '<paragraph>' + `[<imageInline linkHref="http://ckeditor.com" src="${ IMAGE_SRC_FIXTURE }" alt="alt text"></imageInline>]` + '</paragraph>' ); const resizer = Array.from( editor.plugins.get( 'WidgetResize' )._resizers.values() )[ 0 ]; expect( resizer._getResizeHost().nodeName ).to.equal( 'P' ); } );
so what we need to do now is figure out how (if) this affects these cases, adjust it if necessary, and then document it (because API docs are missing
getResizeHost: ( widgetWrapper: HTMLElement ) => HTMLElement; |
I briefly tested resizing images in to-do lists and linked images and it worked fine but maybe I didn't understand some use-cases. A QA session could also be useful.
let parentWidth = parseFloat( domResizeHostParent!.ownerDocument.defaultView!.getComputedStyle( domResizeHostParent! ).width ); | ||
let parentWidth = getElementComputedWidth( domResizeHostParent! ); | ||
|
||
if ( isMaximumResizeHostAncestorElement( domResizeHostParent ) ) { |
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.
This, in my opinion, is only masking the actual root of the issue in ImageResizeHandles
. Check out the current implementation at
getResizeHost() { |
Long story short, it returns paragraphs for inline images and editing roots for block images (that exist directly in a root). I think this is wrong. According to _getResizeHost()
in Resizer
class
Resize host is an object that receives dimensions which are the result of resizing.
ckeditor5/packages/ckeditor5-widget/src/widgetresize/resizer.ts
Lines 413 to 422 in 67d2d60
/** | |
* Obtains the resize host. | |
* | |
* Resize host is an object that receives dimensions which are the result of resizing. | |
*/ | |
private _getResizeHost(): HTMLElement { | |
const widgetWrapper = this._domResizerWrapper!.parentElement; | |
return this._options.getResizeHost( widgetWrapper! ); | |
} |
An editing root does not receive dimensions. <figure>
does (block image) or <span>
(inline image).
@oleq I applied your code suggestion and moved the ticket to QA. |
Have you been able to verify these to-do list/linked image use cases? |
@oleq I briefly checked that and there were no issues. |
* Docs: minor fixes. [short flow] * Shorten the clipboard guide. * Docs: formatting. [skip ci] * Docs: timestamp bump. [skip ci] * Docs: Unify the usage of quotation marks in the React guide. * Docs: Update the Vue guide to new installation methods. * Docs: Fix indentations in the React guide * UI animations do not respect user's preferences on reduced motion (ckeditor#16099) Feature: Editor UI should respect the user's preferences for reduced motion (WCAG 2.1, Success Criterion 2.3.3). Feature (utils): Implemented the `env#isMotionReduced` flag to discover reduced motion preferences. * Improved placeholders and font color grids in high contrast mode (ckeditor#16284) Feature (utils): Implemented the `env#isMediaForcedColors` property for forced colors detection (e.g. high contrast mode on Windows). See ckeditor#14907. Feature (ui): Implemented `ck-media-forced-colors` and `ck-media-default-colors` mixins for detecting forced colors (e.g. high contrast mode on Windows). See ckeditor#14907. Fix (theme-lark): The caret should be visible in a placeholder while in forced colors mode (e.g. high contrast mode on Windows). Improved the look of the placeholders in the forced colors mode. Closes ckeditor#14907. Fix (theme-lark): The color grid component should render as a grid of labels in the forced colors mode (e.g. high contrast mode on Windows). Closes ckeditor#14907. * Docs: import link fix. [short flow] * Use `dev-utils` with updated esbuild-loader * Fix issues caused by targeting ES2022 (and dropping some polyfills) * Image jumps when resizing in container that has padding (ckeditor#16304) Fix (image, widget): An image should not jump upon resizing in a container with padding. Closes ckeditor#14698 --------- Co-authored-by: Aleksander Nowodzinski <a.nowodzinski@cksource.com> * Name, role, value accessibility fixes (ckeditor#16295) Fix (restricted-editing): Improved the accessibility of the restricted editing dropdown by setting the correct ARIA role on the toolbar menu. Fix (minimap): Addressed the issue of the minimap `<iframe>` being unnecessarily exposed to assistive technologies. --------- Co-authored-by: Aleksander Nowodzinski <a.nowodzinski@cksource.com> * Add menu bar integration to multi root editor. (ckeditor#16280) Feature (editor-multi-root): Added menu bar support for multi-root editor. * Other: Updated translations. [skip ci] * Other: Updated translations. [skip ci] * Internal (build-*): Builds. * Add widget ESC shortuct accessibility dialog entry (ckeditor#16310) Other (widget): Added information to the Accessibility help dialog about a keyboard shortcut that moves focus from a nested editable back to the widget. --------- Co-authored-by: Aleksander Nowodzinski <a.nowodzinski@cksource.com> * Docs: Update the Next.js guide to new installation methods. * Docs: Update builder links in integrations. * No longer delay showing tooltip while focusing an element (ckeditor#16296) Fix (ui): `TooltipManager` tooltips should immediately show up when triggered by user focus for better responsiveness and accessibility. --------- Co-authored-by: Aleksander Nowodzinski <a.nowodzinski@cksource.com> * Mark mixin calls as pure * Add `/* #__PURE__ */` before mixin calls in global scope * Other: Updated translations. [skip ci] * Internal (build-*): Builds. * Docs: update Builder links. [short flow] * Docs: update Builder links. [short flow] * Use regular `dev-utils` * Bump `ckeditor5-dev-*` packages to `^40.0.0` * Use `declare` for `View.viewUid` property * Updated the Accessibility guide with VPAT for the latest editor version and new keystrokes added to the list (ckeditor#16324) Docs: Updated the Accessibility guide with VPAT for the latest editor version and new keystrokes added to the list. * Docs: testing helpers fix. [short flow] * Remove menu bar on editor destroy. (ckeditor#16329) Tests (editor-multi-root): Removing the menu bar element on editor destroy. * Docs: minor update. [skip ci] * Docs: minor update. [skip ci] * Add `declare` to few dynamically populated fields * Docs: lists install update. [short flow] * Docs: UI language link. [short flow] * Docs: API links. [short flow] * Docs: API links. [short flow] * Docs: restoring new imports. [short flow] * Update docs/tutorials/widgets/data-from-external-source.md Co-authored-by: Piotr Szczęśniak <szczesniakp@o2.pl> * Update docs/tutorials/widgets/implementing-a-block-widget.md Co-authored-by: Piotr Szczęśniak <szczesniakp@o2.pl> * Update docs/tutorials/widgets/implementing-a-block-widget.md Co-authored-by: Piotr Szczęśniak <szczesniakp@o2.pl> * Update docs/tutorials/widgets/implementing-an-inline-widget.md Co-authored-by: Piotr Szczęśniak <szczesniakp@o2.pl> * Docs: Updated the 2.5.7 section of the VPAT document. * Internal: Move the internal `generatePositions` function to a static method on `BalloonPanelView` class. * Docs: lotsa various fixes. [short flow] * Docs: Fix link to `BalloonPanelView.generatePositions` * Docs: lotsa various fixes. [short flow] * Docs: lotsa various fixes. [short flow] * Docs: Updated the 4.1.3 section of the VPAT document. * Docs: expanding snippets. [short flow] * Docs: crosslinking. [short flow] * Increased specificity of image-style-align CSS classes. * Internal: Vale evaluation. [skip ci] * Docs: post-tests fixes. [short flow] * Docs: Changelog. [skip ci] * Docs: language fixes. [short flow] * Docs: Corrected the changelog. [skip ci] * Docs: Added an intro section. [skip ci] * Docs: explaining Essentials. [short flow] * Docs: minor rearrangements. [short flow] * Increased specificity of almost all image-style-align CSS classes. * Docs: updating and examples update. [short flow] * Docs: await update. [short flow] * Docs: setup rearrangements. [short flow] * Docs: minor fix. [skip ci] * Docs: Fix typos in the React guide. * Docs: Update the Angular guide to new installation methods. * Docs: review fixes and more changes. [short flow] * Docs: removing the removing features section. [short flow] * Docs: minor technical fix. [short flow] * Docs: removing infoboxes. [short flow] * Docs: removing redundant guides. [short flow] * Docs: restoring the removal section. [short flow] * Docs: restoring an internal link. [short flow] * Changelog review. * Docs: Prepare a structure for the Vue 2 guide. * Docs: Unify subhedings in integrations. * Docs: unsupported packages infobox. * Docs: typos. [short flow] * Docs: quick start update. [short flow] * Docs: quick start update. [short flow] * Docs: quick start update. [short flow] * Docs: updating UI language guide. [short flow] * Update ui on close image ckbox dialog. * Docs: updste. [short flow] * Docs: updste. [short flow] * Docs: updste. [short flow] * Internal: Improve tree-shaking by adding `#__PURE__` comments before function calls in global scope * Update docs/getting-started/setup/ui-language.md Co-authored-by: Filip Sobol <filipsobol@users.noreply.github.com> * Docs: update. [short flow] * Docs: updating Builder link. [short flow] * Docs: updating API install commands. [short flow] * Docs: updating API install commands. [short flow] * Release: v41.4.0. * Docs: Add the component installation step to the Vue 3 guide. * Docs: Fix import in the Vue snippet. * Docs: Fix formatting in the Vue guide. * Docs: Add context to the Vue guide. * Docs: Update snippets in the Vue 3 guide. * Docs: Change the Angular guide structure. * Docs: Unify collaboration sections across integrations. * Docs: Fix typo. * Docs: Add the localization section to the Vue 2 guide. * Docs: Unify the localization section in Vue integrations. * The TooltipManager should verify if the tooltip is not already unpinned while trying to update its position. * Removed unnecessary non-null assertions. * Updated the GH link to point to the public repo. * Added info about the changes related to the silent release of NIM. See: ckeditor#16360 (comment) * Merge pull request ckeditor#16363 from ckeditor/cc/6219-update-race Fix (ui): Prevented editor error in a situation where tooltip was unpinned after it was already removed. This happened when "Unlink" button was pressed while the tooltip was shown. * Internal (build-*): Builds. * Docs: Changelog. [skip ci] * Docs: Changelog. [skip ci] * Internal: An empty commit to trigger the release. * Docs: Changelog. * Release: v41.4.1. * Docs: new methods READMEs. * Merge master. * Internal: Fixed wording in the `ImageResize` plugin description in `ckeditor5-metadata.json`. [skip ci] * Docs: updating main README. [short flow] * Docs: adding decoupled editor toolbar config. [short flow] * Add clearTimeout and test. * Docs: a typo. [skip ci] * Update docs/getting-started/setup/toolbar.md Co-authored-by: Piotr Szczęśniak <szczesniakp@o2.pl> * Docs: snippet update. [short flow] * Docs: Update the Laravel integration guide to new installation methods. * Added image style align left/right between image and image-inline. * Fix (utils): Prevented error thrown when `ckeditor5-utils/src/env.ts` is used in an environment without `window` global object. * Fixed lint issue. * Rename `globalVar` to `global`. * Docs (utils): Added a clarification remark to some properties in `env` object. * Docs (utils): Grammar and typo fix. * Internal (build-*): Builds. * Docs: Changelog. [skip ci] * Docs: Changelog (v41.4.2). * Release: v41.4.2. * Docs: removing builds. [short flow] * Docs: Unify the setup sections. * Docs: updates. [short flow] * Docs: Prepare a structure for the .NET integration. * Docs: Fix typo. * Docs: Update the .NET integration guide to new installation methods. * Docs: Fix contraction. * Other: Add `#__PURE__` comments before objects and arrays in global scope. * Internal: Vale evaluation. [skip ci] * Docs: updating VPAT. [short flow] * Instant pin tooltip on focus only if not hovered * Docs: updating Drupal guide. [short flow] * Docs: restoring a deleted file. [short flow] * Add `onAfterDestroy` callback to react docs snippet, minor wording changes * Docs: Minor fixes in the Quick Start * Docs: Minor fixes in the Angular integration * Docs: Minor fixes in the Next.js integration. * Docs: Minor fixes in the Vue 2 integration. * Docs: Minor fixes in the Vue integration. * Docs: Modify the structure of the Next integration. * Docs: Add new method to the React integration. * Docs: minor fixes. [short flow] * Core review fixes. * Docs: puntuation. [short flow] * Tweak plugins. * Change spinner animation keyframe name to be more unique. * Fix theme and CSS. * Fix watchdog setup. * Docs: general fixes. [skip ci] * Docs: general fixes. [skip ci] * Docs: relinking and cleanup. [short flow] * Code review fixes. * Docs: Unify the Builder naming. * Code review fixes. * Remove type. * Docs: Remove excessive articles. * Docs: Remove the excessive section from the Angular guide. * Docs: Preliminary work in the Block widget guide. * Docs: updating error codes. [short flow] * Docs: updating error codes. [short flow] * Docs: Update the Block widget tutorial to new installation methods. * Docs: Fix typos in the Block widget guide. * Docs: minor fixes. [short flow] * Docs: pre-release fixes. * Docs: integrations update. [short flow] * Docs: adding CTA. [short flow] * Docs: Preliminary work in the Inline widget tutorial. * Docs: minor update. [short flow] * Docs: minor update. [short flow] * Docs: minor update. [short flow] * Docs: Update the Inline widget tutorial to new installation methods. * Docs: a review. [short flow] * Docs: Builder infoboxes. [short flow] --------- Co-authored-by: godai78 <b.biedrzycki@cksource.com> Co-authored-by: Witek Socha <witek.soch@gmail.com> Co-authored-by: Bartek Biedrzycki <68123541+godai78@users.noreply.github.com> Co-authored-by: Mateusz Gorzeliński <mateusz.gorzelinski@gmail.com> Co-authored-by: Mateusz Bagiński <cziken58@gmail.com> Co-authored-by: Aleksander Nowodzinski <a.nowodzinski@cksource.com> Co-authored-by: Filip Sobol <filipsobol@users.noreply.github.com> Co-authored-by: Michał Remiszewski <118178939+mremiszewski@users.noreply.github.com> Co-authored-by: Kamil Piechaczek <pomek@users.noreply.github.com> Co-authored-by: Piotr Szczęśniak <szczesniakp@o2.pl> Co-authored-by: CKEditorBot <ckeditor-bot@cksource.com> Co-authored-by: Illia Sheremetov <i.sheremetov@cksource.com> Co-authored-by: Kuba Niegowski <j.niegowski@cksource.com> Co-authored-by: Szymon Cofalik <s.cofalik@cksource.com> Co-authored-by: Piotrek Koszuliński <pkoszulinski@gmail.com> Co-authored-by: Witek Socha <w.socha@cksource.com> Co-authored-by: Paweł Smyrek <p.smyrek@cksource.com> Co-authored-by: Kuba Niegowski <1232187+niegowski@users.noreply.github.com> Co-authored-by: Daniela Soto <dsoto@eagerworks.com>
Suggested merge commit message (convention)
Fix (widget): Image no longer jumps when resizing in container that has padding. Closes #14698