Skip to content
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

Add resize table column balloon editor #16104

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft

Add resize table column balloon editor #16104

wants to merge 1 commit into from

Conversation

Mati365
Copy link
Member

@Mati365 Mati365 commented Mar 27, 2024

Suggested merge commit message (convention)

Feature (table): Add "Resize Column" option in the table editor toolbar, offering an alternative to drag-and-drop resizing.


Additional information

Closes https://github.com/cksource/ckeditor5-commercial/issues/6023
Part of https://github.com/cksource/ckeditor5-commercial/pull/6133
Related to #16140

Screens:
image
image

Summary by CodeRabbit

  • New Features

    • Added functionality for resizing table columns in the CKEditor interface.
    • New UI components for adjusting table column widths, including labeled input fields and buttons.
    • Enhanced form validation and handling for table column adjustments.
  • Bug Fixes

    • Improved precision in calculating table cell widths using parseFloat.
  • Refactor

    • Updated module paths and reorganized code for better clarity and separation of concerns in table column resizing features.
  • Style

    • Introduced new styles for table column resizing forms.
    • Updated table cell border color to use CSS variables for better theme consistency.
  • Documentation

    • Enhanced user interface documentation related to new table column resizing features.

@Mati365 Mati365 changed the title Add resize column command skeleton Add resize column balloon editor Mar 27, 2024
@Mati365 Mati365 marked this pull request as ready for review March 29, 2024 11:48
@Mati365 Mati365 changed the title Add resize column balloon editor Add resize table column balloon editor Mar 29, 2024
@charlttsie
Copy link
Contributor

Focus is placed outside of the balloon after selecting the “Resize column” option and there’s no way to insert a column size value using keyboard only

Steps

  1. Open e.g. http://localhost:8125/ckeditor5/tests/manual/all-features.html
  2. Insert a table
  3. Use Alt + F10 to move focus to the table toolbar
  4. Navigate to the “Resize column” option and press enter to open the balloon

Result

Focus is outside of the “Column width in pixels” field and it’s not possible to insert a number into the field without using mouse:

1.mp4

@charlttsie
Copy link
Contributor

Table size balloon is left out after removing the table

Steps

  1. Open e.g. http://localhost:8125/ckeditor5/tests/manual/all-features.html
  2. Insert a table
  3. Select the “Resize column” option from the table toolbar
  4. Press Cmd + A to remove the content

Result

The balloon with table size is not removed:

2.mp4

@charlttsie
Copy link
Contributor

#16104 (comment) and #16104 (comment) look fixed 👍

@charlttsie
Copy link
Contributor

It's not possible to resize a column right after table insertion

Steps

  1. Open e.g. http://localhost:8125/ckeditor5/tests/manual/all-features.html (or any other manual test with table resize)
  2. Insert a table
  3. Look for the “Resize column” option in the table toolbar

Result

The option is greyed out. Resizing the table using resize handler activates the option:

3.mp4

@charlttsie
Copy link
Contributor

Resizing using the "Resize column" option doesn't work with Track Changes

Steps

  1. Open e.g. all-features-rtc.html
  2. Enable Track Changes and try to resize a table

Result

The "Resize column" option is greyed out even though it's active with TC off:

4.mp4

@charlttsie
Copy link
Contributor

Table column width changes when the column is selected

Steps

  1. Open e.g. http://localhost:8125/ckeditor5/tests/manual/all-features.html
  2. Insert a table
  3. Select a table column (either with the toolbar option or manually)
  4. Deselect the column

Result

The column gets narrower when selected:

5.mp4

Note

It's a regression - not reproducible on master

@kubaklatt
Copy link

The error when entering an incorrect number is misleading

The error suggests that it is possible to enter a number with a comma/dot (depends on the browser) - but this is not possible.

Steps:

  1. Open e.g. all-features-rtc.html
  2. Try entering a high number to trigger an error that shows the maximum possible value
  3. Enter suggested value

Result:

An error appears that the entered value is invalid

resize.mov

Notes:

When entering an incorrect value and switching focus from the input to something else, the input header overlaps with the incorrect value

overlaps.mov

@Mati365
Copy link
Member Author

Mati365 commented Apr 2, 2024

@kubaklatt / @charlttsie All reported issues should be fixed. Please switch to ck/6023 on commercial repo.

@charlttsie
Copy link
Contributor

We've finished testing with @kubaklatt and @marcelmroz and the PR looks good. We haven't found anything new, while the previously reported issues don't reproduce anymore. Thanks @Mati365 for the quick fixes! 🎉

@Mati365 Mati365 force-pushed the ck/6023 branch 2 times, most recently from 9f68734 to 6fadb2e Compare April 8, 2024 10:27
Copy link
Contributor

@Dumluregn Dumluregn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Changing the size of the furthest right column sets different result:
Screen.Recording.2024-04-10.at.12.49.18.mov
  1. The "Resize column" button is always visible in the dropdown, even if the table column resize feature is not loaded in the editor.
image
  1. I'd try to implement a live preview when changing the column widths. Without it user has to guess a few times until they find the expected size. It should work like the other table properties, so apply only view changes until the change is applied and after committing the new value, propagate change to the model - this way we only have one undo step.

@Mati365
Copy link
Member Author

Mati365 commented Apr 11, 2024

@Dumluregn

  1. Fixed
  2. Fixed
  3. I added validation with proper messages instead (so it's normalized to common behavior of other such forms according to other tickets related to VPAT). I believe that solution that you described is better in terms of UX, but it's also much harder to implement knowing that there will be a lot of edge cases with incorrect positioning of the balloon. Let's keep this in mind in further UX improvements.

Copy link
Contributor

@Dumluregn Dumluregn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall, it's really cool it's all working and that you did quite a lot of cleaning in the TableColumnResizeEditing plugin 👏🏻

Besides some necessary cleanup that I wrote about below, I noticed two more issues:

  1. When a selected cell spans through a few columns, the width of the last one is shown in the form and resized if you change the width. I think it should be the first one.
Screen.Recording.2024-04-12.at.12.20.25.mov
  1. A problem with undo. If you change the width twice and undo once, then open the resize form, then you'll see the wrong value.
Screen.Recording.2024-04-12.at.12.25.34.mov

@Mati365
Copy link
Member Author

Mati365 commented Apr 15, 2024

@Dumluregn

  1. Actually, it's correct behavior according to column resize implementation. It's because when you start to resize the right corner of such colspan you actually resize the top row column element with much smaller width. It's counterintuitive, but at this moment it's not possible to change this behavior without breaking a lot of other edge cases.
  2. Fixed. Table resize operations seem to trigger refresh() only on selection change (which occurs quite often in mouse operations). I added refresh call before loading initial values to form. Seems to be working correctly.

Rest of CR remarks should be fixed. Can you check again?

Copy link
Contributor

@Dumluregn Dumluregn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

Please just report the two issues I mentioned in the meantime that we're not fixing right now:

  1. Live preview;
  2. Unintuitive behavior with merged cells.

@Mati365
Copy link
Member Author

Mati365 commented Apr 16, 2024

No changes, rebased.

@charlttsie
Copy link
Contributor

Maximum column size allowed value is 1px higher from the size that can be set

Steps

  1. Open e.g. http://localhost:8125/ckeditor5-table/tests/manual/tablecolumnresize.html
  2. In the table column resize balloon, insert the highest possible value and save
  3. Open table column resize balloon once again

Result

The allowed value was 1px higher from the actual size that's been set:

1px.mp4

@charlttsie
Copy link
Contributor

For a single-column table, minimum column size allowed is 1px higher from the size that can be set

Steps

  1. Open e.g. http://localhost:8125/ckeditor5-table/tests/manual/tablecolumnresize.html
  2. Insert a table that has one column and at least 2 rows
  3. In the table column resize balloon, insert the lowest possible value and save
  4. Open table column resize balloon once again

Result

The column used to have 40px but now has 39px:

minimum-size-one-column.mp4

@charlttsie
Copy link
Contributor

For a single-column table, the value is sometimes set to 1px lower from what's been inserted

Seems related to: #16104 (comment), but this scenario doesn't involve the validation issue

Steps

  1. Open e.g. http://localhost:8125/ckeditor5-table/tests/manual/tablecolumnresize.html
  2. Insert a table that has one column and at least 2 rows
  3. Open table column resize balloon and change the column size

Result

Sometimes, but not always (see 460px at 00:12 in the screencast), the value after reopening the resize balloon differs from what's been inserted:

size-1px.mp4

@charlttsie
Copy link
Contributor

FYI: #16249 - Table column size changes when resizing browser window after column resize was used

The above issue also occurs on master, but it affects the pull request in such a way that a column width smaller than allowed can be achieved by resizing the browser window:

minimum-value

@charlttsie
Copy link
Contributor

FYI: #16251 - Clicking on a resize handle in a nested table extends the tables to the full editor width

The above issue also reproduces when resizing using the balloon:

resize-balloon-nested.mp4

Copy link
Contributor

coderabbitai bot commented Apr 19, 2024

Walkthrough

The updates to the CKEditor5's table package focus on enhancing table column resizing capabilities. Changes include the introduction of new UI components, commands, and utilities for resizing table columns, alongside necessary dependency adjustments and code refactoring. These modifications aim to improve user interaction with table columns in the CKEditor environment, making the resizing process more intuitive and precise.

Changes

File(s) Change Summary
contexts.json, src/.../tablecolumnresize.ts, src/.../tablecolumnresize/commands/..., src/.../tablecolumnresizeediting.ts, src/.../tablecolumnresize/tablecolumnresizeui.ts, src/.../tablecolumnresize/tablecolumnresizeutils.ts, src/.../tablecolumnresize/ui/..., tests/.../tablecolumnresize/..., theme/tableresizecolumnform.css Introduced and enhanced functionalities for resizing table columns, including new UI components, commands, and utilities.
package.json Updated dependencies, specifically adding @ckeditor/ckeditor5-ui and cleaning up duplicate entries.
src/index.ts, src/.../augmentation.ts, src/tableui.ts, tests/.../tableui.js Updated exports and internal logic to support new table column resizing features.
src/.../tablecolumnresize/utils.ts, theme/table.css, ckeditor5-theme-lark/theme/.../tableediting.css Improved precision in utility functions and updated CSS for better visual feedback and consistency.

Recent Review Details

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between e719712 and 4083eda.
Files selected for processing (22)
  • packages/ckeditor5-table/lang/contexts.json (2 hunks)
  • packages/ckeditor5-table/package.json (2 hunks)
  • packages/ckeditor5-table/src/augmentation.ts (5 hunks)
  • packages/ckeditor5-table/src/index.ts (2 hunks)
  • packages/ckeditor5-table/src/tablecolumnresize.ts (2 hunks)
  • packages/ckeditor5-table/src/tablecolumnresize/commands/tablecolumnresizecommand.ts (1 hunks)
  • packages/ckeditor5-table/src/tablecolumnresize/commands/tablewidthscommand.ts (1 hunks)
  • packages/ckeditor5-table/src/tablecolumnresize/tablecolumnresizeediting.ts (6 hunks)
  • packages/ckeditor5-table/src/tablecolumnresize/tablecolumnresizeui.ts (1 hunks)
  • packages/ckeditor5-table/src/tablecolumnresize/tablecolumnresizeutils.ts (1 hunks)
  • packages/ckeditor5-table/src/tablecolumnresize/ui/tablecolumnresizeformview.ts (1 hunks)
  • packages/ckeditor5-table/src/tablecolumnresize/utils.ts (1 hunks)
  • packages/ckeditor5-table/src/tableui.ts (3 hunks)
  • packages/ckeditor5-table/tests/tablecolumnresize/commands/tablecolumnresizecommand.js (1 hunks)
  • packages/ckeditor5-table/tests/tablecolumnresize/commands/tablewidthscommand.js (1 hunks)
  • packages/ckeditor5-table/tests/tablecolumnresize/tablecolumnresizeediting.js (2 hunks)
  • packages/ckeditor5-table/tests/tablecolumnresize/tablecolumnresizeui.js (1 hunks)
  • packages/ckeditor5-table/tests/tablecolumnresize/ui/tablecolumnresizeformview.js (1 hunks)
  • packages/ckeditor5-table/tests/tableui.js (5 hunks)
  • packages/ckeditor5-table/theme/table.css (1 hunks)
  • packages/ckeditor5-table/theme/tableresizecolumnform.css (1 hunks)
  • packages/ckeditor5-theme-lark/theme/ckeditor5-table/tableediting.css (1 hunks)
Files skipped from review due to trivial changes (3)
  • packages/ckeditor5-table/src/tablecolumnresize/commands/tablewidthscommand.ts
  • packages/ckeditor5-table/tests/tablecolumnresize/commands/tablewidthscommand.js
  • packages/ckeditor5-table/theme/tableresizecolumnform.css
Additional comments not posted (45)
packages/ckeditor5-table/src/tablecolumnresize.ts (2)

13-13: Ensure that TableColumnResizeUI is properly utilized within the module.


27-27: Confirm that all required modules are correctly listed and utilized.

packages/ckeditor5-table/package.json (1)

17-17: Confirm the correct version and usage of the @ckeditor/ckeditor5-ui dependency.

packages/ckeditor5-table/theme/table.css (1)

29-29: Ensure the new CSS variable for cell border color is defined and consistent across the theme.

Verification successful

The verification process has confirmed that the CSS variable --ck-color-cell-border is defined in the packages/ckeditor5-table/theme/table.css file and is consistently used within the same file for styling the border of table cells. This ensures that the variable is both defined and applied correctly within the theme, aligning with the expectations set in the review comment.

  • The variable is defined with the value hsl(0, 0%, 75%).
  • It is used in a border property as border: 1px solid var(--ck-color-cell-border);.

This consistency in the definition and usage within the theme file confirms that the changes are correctly implemented.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Search for the definition of --ck-color-cell-border in the theme files to ensure consistency.
rg --type css '--ck-color-cell-border' packages/ckeditor5-table/theme/

Length of output: 495



Script:

#!/bin/bash
# Correctly search for the definition of --ck-color-cell-border in the theme files to ensure consistency.
rg -e '--ck-color-cell-border' --type css packages/ckeditor5-table/theme/

Length of output: 249

packages/ckeditor5-table/tests/tablecolumnresize/commands/tablecolumnresizecommand.js (1)

56-63: Ensure the test cases cover all scenarios for resizing table columns, including edge cases.

packages/ckeditor5-table/src/tablecolumnresize/commands/tablecolumnresizecommand.ts (1)

83-88: Confirm that the resizeColumnUsingResizer method handles all edge cases, such as minimum and maximum column widths.

packages/ckeditor5-table/src/augmentation.ts (3)

22-22: Ensure that TableColumnResizeUI is correctly integrated and utilized within the module.


33-33: Confirm that TableColumnResizeUtils is properly declared and used.


61-61: Verify that the TableColumnResizeCommand is correctly implemented and integrated.

packages/ckeditor5-table/src/index.ts (2)

31-33: Confirm that all new modules related to table column resizing are correctly exported.


65-65: Verify that the TableColumnResizeCommand type is correctly exported and used.

packages/ckeditor5-table/lang/contexts.json (4)

7-7: New label added for the resize table column button.


68-68: Label for column width input added. Ensure it is clear and understandable in the context of the UI.


69-69: Error message for empty column width. Good addition for user feedback.


70-70: Error message for incorrect column width. It's important to provide clear error messages to guide the user.

packages/ckeditor5-table/src/tablecolumnresize/tablecolumnresizeui.ts (2)

165-169: Integration of TableColumnResizeUI with dropdown options. This conditional addition is well-implemented and checks for plugin existence before adding.


Line range hint 294-304: Ensure that commands are executed correctly from the dropdown and focus behavior is managed appropriately. The use of type assertions (as any) should be avoided if possible for better type safety.

Consider refining the type definitions to avoid using as any and ensure type safety.

packages/ckeditor5-table/src/tablecolumnresize/ui/tablecolumnresizeformview.ts (2)

201-201: Label for the input field is set correctly and matches the context of resizing table columns.


209-225: Form validation logic is implemented. It checks for empty input and incorrect values, providing appropriate error messages.

packages/ckeditor5-table/tests/tablecolumnresize/ui/tablecolumnresizeformview.js (1)

252-258: Unit test for submitting the form. It correctly simulates the submit event and checks if the event handler is called.

packages/ckeditor5-table/src/tableui.ts (2)

165-169: Proper integration of the TableColumnResizeUI plugin into the table column dropdown. This conditional addition enhances the functionality based on the availability of the plugin.


294-304: Handling of command execution and focus behavior from dropdown events. The implementation ensures that the appropriate commands are executed and focus is managed correctly, except when toggling a switch button.

packages/ckeditor5-table/tests/tablecolumnresize/tablecolumnresizeui.js (9)

18-54: Ensure proper cleanup of DOM elements and editor instances in tests to prevent memory leaks and side effects.

Consider adding checks or hooks that ensure all dynamically created elements and event listeners are properly removed after each test to avoid potential memory leaks and ensure test isolation.


61-63: Good practice to verify plugin names in tests.

This ensures that the plugin integration does not break due to changes in the plugin's naming conventions.


65-87: Validate the button's enabled state under various conditions.

These tests check the button's enabled state based on the presence of tables and command states, which is crucial for ensuring the UI responds correctly to different editor states.


102-114: Test the balloon panel's visibility upon button click.

This test ensures that the balloon panel behaves as expected when the resize button is clicked, which is essential for user interaction.


116-127: Ensure default values are correctly set in the UI.

This test verifies that the form is populated with the correct default values, which is important for a good user experience.


129-140: Check the order of function calls related to CSS transitions.

This test ensures that CSS transitions are disabled before adding the form to the balloon to prevent unwanted animations, which enhances the UX.


143-193: Robust error handling in form submissions.

These tests ensure that the form correctly handles errors, such as empty or incorrect values, and resets errors appropriately, which is crucial for robust error handling in user inputs.


208-248: Comprehensive tests for the balloon panel form behavior.

These tests cover various scenarios including synchronization of input values with command values, handling of form visibility, and command execution on form submission, ensuring the form behaves correctly across different user interactions.


299-334: Ensure proper handling of close events in the balloon panel.

These tests verify that the balloon panel closes appropriately upon events like ESC key press and mouse clicks, which is important for intuitive UI behavior.

packages/ckeditor5-table/src/tablecolumnresize/utils.ts (1)

316-316: Improve precision in width calculations by using parseFloat.

This change ensures that the width calculations are more accurate by considering decimal points, which is crucial for precise layout adjustments.

packages/ckeditor5-table/src/tablecolumnresize/tablecolumnresizeediting.ts (4)

91-91: Correctly specify plugin dependencies.

Specifying the required plugins ensures that the TableColumnResizeEditing plugin has all the necessary functionalities available, which is crucial for its operation.


443-458: Handle mouse events for initiating column resizing.

This logic correctly initiates the resizing process when the mouse is down on a resizer element, which is essential for user interaction with the column resizing feature.


482-486: Dynamically update column widths during resizing.

This implementation ensures that column widths are updated in real-time as the user drags the resizer, providing immediate visual feedback, which enhances the user experience.


500-500: Properly conclude the resizing process.

This method ensures that the resizing process is properly concluded, either by applying or reverting changes, which is crucial for maintaining the integrity of the table's layout.

packages/ckeditor5-table/src/tablecolumnresize/tablecolumnresizeutils.ts (4)

29-42: Ensure consistent naming for plugins to avoid confusion.

The class TableColumnResizeUtils is named similarly to TableUtils, which is good for consistency. However, ensure that all utility classes follow a similar naming convention to avoid confusion among developers.


44-63: Review the method signature and parameters for clarity.

The method resizeColumnUsingResizer could benefit from a more descriptive parameter name than compensateWidthWhenMoveCenteredTable. Consider renaming it to something more intuitive, like adjustWidthForCenterAlignment.


82-93: Optimize the sorting algorithm for performance.

The sorting of elements in getSmallestSelectedColumnResizer uses Math.sign, which is fine for small datasets. However, for a large number of elements, consider using a more efficient sorting algorithm or leveraging native browser APIs that might optimize this operation.


191-248: Ensure consistency in method documentation.

The documentation for assignColumnWidth is detailed, but it lacks consistency in formatting and clarity compared to other methods in the class. Revise the documentation to match the clarity and format of other method comments in the class.

packages/ckeditor5-table/tests/tableui.js (3)

14-14: Ensure new imports are used appropriately.

The import of TableColumnResize is added. Verify that this import is utilized properly in the tests to ensure that the new table column resizing feature is covered.


36-40: Review asynchronous setup in tests for potential issues.

The beforeEach setup function is now asynchronous and uses the createEditor function. Verify that this change does not introduce race conditions or other timing issues in the test execution, especially since editor creation is a complex operation involving multiple asynchronous steps.


352-377: Add tests for new UI functionality.

The new test suite for "resize table column integration" is a good addition. It checks for the presence of the resize column entry in the dropdown when the TableColumnResize plugin is present, which is crucial for validating the integration of the new feature.

packages/ckeditor5-table/tests/tablecolumnresize/tablecolumnresizeediting.js (2)

49-49: Updated import path for TableWidthsCommand looks correct.


727-727: Typo correction in the description from 'widhtStrategy' to 'widthStrategy' is accurate.


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?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

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)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 5

Comment on lines +104 to +128
public prepareColumnResize( resizerElement: ViewElement ): ResizingData | null {
const { editing } = this.editor;

const viewTable = resizerElement.findAncestor( 'table' )!;
const editingView = editing.view;
const columnWidthsInPx = calculateResizerColumnWidth( this.editor, resizerElement );

if ( !columnWidthsInPx ) {
return null;
}

// Insert colgroup for the table that is resized for the first time.
if ( !Array.from( viewTable.getChildren() ).find( viewCol => viewCol.is( 'element', 'colgroup' ) ) ) {
editingView.change( viewWriter => {
insertColgroupElement( viewWriter, columnWidthsInPx, viewTable );
} );
}

const resizingData = getResizingData( this.editor, resizerElement, columnWidthsInPx );

// At this point we change only the editor view - we don't want other users to see our changes yet,
// so we can't apply them in the model.
this.editor.editing.view.change( writer => applyResizingAttributesToTable( writer, viewTable, resizingData ) );

return resizingData;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Handle potential exceptions in DOM operations.

The method prepareColumnResize performs several DOM manipulations without any exception handling. Consider adding try-catch blocks around these operations to handle potential errors gracefully, especially since DOM operations can fail due to various reasons like incorrect selectors or unexpected HTML structure changes.

Comment on lines +259 to +334
public endResize( resizingData: ResizingData, readOnly?: boolean ): void {
const editor = this.editor;
const editingView = editor.editing.view;

const {
viewResizer,
modelTable,
viewFigure,
viewColgroup
} = resizingData.elements;

const tableColumnGroup = getColumnGroupElement( modelTable );
const viewColumns: Array<ViewElement> = Array
.from( viewColgroup!.getChildren() )
.filter( ( column: ViewNode ): column is ViewElement => column.is( 'view:element' ) );

const columnWidthsAttributeOld = tableColumnGroup ?
getTableColumnsWidths( tableColumnGroup )! :
null;

const columnWidthsAttributeNew = viewColumns.map( column => column.getStyle( 'width' ) );

const isColumnWidthsAttributeChanged = !isEqual( columnWidthsAttributeOld, columnWidthsAttributeNew );

const tableWidthAttributeOld = modelTable.getAttribute( 'tableWidth' ) as string;
const tableWidthAttributeNew = viewFigure.getStyle( 'width' )!;

const isTableWidthAttributeChanged = tableWidthAttributeOld !== tableWidthAttributeNew;

if ( isColumnWidthsAttributeChanged || isTableWidthAttributeChanged ) {
if ( readOnly ) {
// In read-only mode revert all changes in the editing view. The model is not touched so it does not need to be restored.
// This case can occur if the read-only mode kicks in during the resizing process.
editingView.change( writer => {
// If table had resized columns before, restore the previous column widths.
// Otherwise clean up the view from the temporary column resizing markup.
if ( columnWidthsAttributeOld ) {
for ( const viewCol of viewColumns ) {
writer.setStyle( 'width', columnWidthsAttributeOld.shift()!, viewCol );
}
} else {
writer.remove( viewColgroup! );
}

if ( isTableWidthAttributeChanged ) {
// If the whole table was already resized before, restore the previous table width.
// Otherwise clean up the view from the temporary table resizing markup.
if ( tableWidthAttributeOld ) {
writer.setStyle( 'width', tableWidthAttributeOld, viewFigure );
} else {
writer.removeStyle( 'width', viewFigure );
}
}

// If a table and its columns weren't resized before,
// prune the remaining common resizing markup.
if ( !columnWidthsAttributeOld && !tableWidthAttributeOld ) {
writer.removeClass(
'ck-table-resized',
[ ... viewFigure.getChildren() as IterableIterator<ViewElement> ].find( element => element.name === 'table' )!
);
}
} );
} else {
editor.execute( 'resizeTableWidth', {
table: modelTable,
tableWidth: `${ toPrecision( tableWidthAttributeNew ) }%`,
columnWidths: columnWidthsAttributeNew
} );
}
}

editingView.change( writer => {
writer.removeClass( 'ck-table-column-resizer__active', viewResizer );
} );
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider refactoring to improve readability.

The method endResize is quite lengthy and handles multiple responsibilities. Consider breaking it down into smaller, more focused methods to improve readability and maintainability. For example, handling view restoration and command execution could be separated into different methods.

Comment on lines +421 to +437
function insertColgroupElement(
viewWriter: DowncastWriter,
columnWidthsInPx: Array<number>,
viewTable: ViewElement
): void {
const colgroup = viewWriter.createContainerElement( 'colgroup' );

for ( let i = 0; i < columnWidthsInPx.length; i++ ) {
const viewColElement = viewWriter.createEmptyElement( 'col' );
const columnWidthInPc = `${ toPrecision( columnWidthsInPx[ i ] / sumArray( columnWidthsInPx ) * 100 ) }%`;

viewWriter.setStyle( 'width', columnWidthInPc, viewColElement );
viewWriter.insert( viewWriter.createPositionAt( colgroup, 'end' ), viewColElement );
}

viewWriter.insert( viewWriter.createPositionAt( viewTable, 0 ), colgroup );
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Validate input parameters for safety.

The function insertColgroupElement does not validate the input parameters columnWidthsInPx and viewTable. Ensure that these parameters are validated to prevent runtime errors if unexpected values are passed.

Comment on lines +740 to +745
async function createEditor( config ) {
return ClassicTestEditor.create( element, {
plugins: [ TableEditing, TableUI ],
...config
} );
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactor to improve code reuse and maintainability.

The createEditor function is a good refactor that simplifies editor creation with configurable plugins. Consider using this pattern in other test files as well to improve code reuse and maintainability across the project.

Comment on lines +18 to +23
/*
* Fixes the problem where surrounding cells cover the focused cell's border.
* It does not fix the problem in all places but the UX is improved.
* See https://github.com/ckeditor/ckeditor5-table/issues/29.
*/
border-color: var(--ck-color-cell-border);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tip

Codebase Verification

The search results indicate that the CSS variable --ck-color-cell-border is used in tableediting.css but there is no definition for it within the theme files. This suggests that the variable might not be defined, which could lead to inconsistencies in the theme's appearance. It is recommended to define this variable or verify its definition in a global stylesheet if it exists.

  • packages/ckeditor5-theme-lark/theme/ckeditor5-table/tableediting.css: Variable used but not defined in the theme files.
Analysis chain

Ensure the new border color variable is defined and consistent with the theme.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Search for the definition of --ck-color-cell-border in the theme files to ensure consistency.
rg --type css '--ck-color-cell-border' packages/ckeditor5-theme-lark/theme/

Length of output: 500



Script:

#!/bin/bash
# Corrected script to search for the definition of --ck-color-cell-border in the theme files to ensure consistency.
rg -e '--ck-color-cell-border' --type css packages/ckeditor5-theme-lark/theme/

Length of output: 193

@kubaklatt
Copy link

It is not possible to resize a table inside another table in a specific case

Steps

  1. Open e.g. http://localhost:8125/ckeditor5-table/tests/manual/tablecolumnresize.html
  2. Insert a table that has at least two columns
  3. Manually extend the first column as much as possible
  4. Add a 1x1 table to the first column
  5. Resize the column size of the 1x1 table to an exact value
  6. Try again to resize the column size of the 1x1 table to whatever value

Result

It is not possible to resize a 1x1 table, no matter what value we enter. The suggestions as to what value we can enter are wrong in this case.

table.mov

@charlttsie
Copy link
Contributor

@Mati365
We've finished this round of tests with @marcelmroz and @kubaklatt and have found a few issue, which are reported above. Please let us know when ready for a retest, and will take another look 👀

@Mati365 Mati365 marked this pull request as draft April 24, 2024 06:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants