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

Replace first cell content if input while on cell selection #2030

Merged
merged 7 commits into from
Aug 17, 2023

Conversation

Andres-CT98
Copy link
Contributor

@Andres-CT98 Andres-CT98 commented Aug 15, 2023

Description:

Fix of #2023

Writing on a selected cell, or group of cells, does not replace the content of the first cell as expected.

Fix

On the TableCellSelection plugin, the handle key down event used to end the cell selection when typing. Now it makes only the first cell selected and creates a new selection of all its children.

Cases tested:

  1. Create a table
  2. Select one or more cells
  3. Type something

Before: The place where the cursor was placed is where the typed text will be inserted.

After: The first cell in the selection, top left one, will be cleared and the typed text will be inserted.

Warnings and implications

  • Keys like the function keys and Caps Lock will end cell selections. This behaviour exits before this change. However for the case of typing, after pressing said keys the full contents of the cell will still be selected, so typing and replacing the content should not be affected.

@Andres-CT98 Andres-CT98 linked an issue Aug 15, 2023 that may be closed by this pull request
if (!this.state.startedSelection) {
this.handleInputEvent(this.editor);
} else {
event.rawEvent.preventDefault();
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is Input event preventable?

Copy link
Collaborator

Choose a reason for hiding this comment

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

It seems we just need to handle keydown event, it will be good enough. I tried to comment out the handling code of input event, and it still works well

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There is an issue with undo that is mitigated by using the Input method. It only happens if you type one character and do undo, the inputted character and the last written thing will both be undone. This issue is present today and not introduced by this change.

The reason I clear the full cell is that I do not know if select is enough to cover all possible elements in the cell for deletion. And for clearing I want to make sure it is an input and not other keys like caps lock.

Copy link
Collaborator

Choose a reason for hiding this comment

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

About the undo issue, can you provide a full end to end repro step? I want to understand more about it. Thanks

Copy link
Contributor Author

Choose a reason for hiding this comment

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

  1. Create a table
  2. Fill it with values, remembering which one was the last one
  3. Make a cell selection
  4. Type one character
  5. Undo

Notice that the typed character and the last filled value are both undone. The only one undone should be the typed one.
tableUndoWrong

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ok, in that case we do need to call addUndoSnapshot() when type with table selection, but only need to do once.

Copy link
Collaborator

Choose a reason for hiding this comment

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

And I think it should be good enough to just select everything inside the table cell when keydown. Because:

  1. User made a table selection, then type
  2. User selected everything in a table cell, then type

Both of them should get the same result. So making a selection to the first select cell means we convert 2 to 1, so that is the right operation.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have been trying both selection types, but each have an issue. editor.select with the Td as parameter has the above issue, while editor.select with the table and coordinates as parameter has an issue of replacing everything in the cell when typing. For example if the cell has multiple lines, only the first one is replaced.

private handleInputEvent(editor: IEditor) {
// If typing on any cell selection, clear the first cell
const range = editor.getSelectionRangeEx();
if (range.type == SelectionRangeTypes.TableSelection) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Actually, when I debug your code, Input event happens after KeyDown event. Since you have already changed selection when KeyDown event, it will fail the check of table selection range type, so will go directly to line 149, so code from line 142 to line 146 is never executed.

image

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Interesting. I assumed it would retain the selection type.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Because you have called editor.select(), that will change selection type.

Copy link
Collaborator

@JiuqingSong JiuqingSong Aug 16, 2023

Choose a reason for hiding this comment

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

And because of that, the executing result is correct now. But if line 142-146 are executed, the new typed content will be removed, just because when Input event is triggered, the new typed content is already added into the view.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have noticed that, would it work if I did the deletion before the Input event? I would need to add it to PluginEventType.

Copy link
Collaborator

Choose a reason for hiding this comment

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

My feeling is you just need to keep you current code to select the content of first selected cell, (and call addUndoSnapshot() in a proper place), then let browser handle everything else, so no need to handle Input event at all.

Because Input event is triggered after type, so anything there is too late.

And maybe only need to handle character types (you can use isCharacterValue function to check) so for other keys like caps lock, leave it to browser

@JiuqingSong
Copy link
Collaborator

I made the following change locally to make undo work.

  1. In handleKeydownEvent.ts:
    image

  2. In UndoPlugin.ts:
    image

The problem is although we added new undo snapshot, but the flag "hasNewContenet" is also cleared. So we need to set it back when there is really input.

@@ -26,7 +26,7 @@ export function handleKeyUpEvent(
!state.preventKeyUp &&
IGNORE_KEY_UP_KEYS.indexOf(which) == -1
) {
clearState(state, editor);
editor.addUndoSnapshot(() => clearState(state, editor));
Copy link
Contributor

@BryanValverdeU BryanValverdeU Aug 16, 2023

Choose a reason for hiding this comment

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

Using Arrow Keys, Escape or some other keys that do not add content will add an unneeded undo snapshot here.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Makes sense. In that case, my change in handleKeydownEvent.ts to addUndoSnapshot() is not needed

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I can use isCharacterValue here to only add the snapshot if it is true.

@Andres-CT98 Andres-CT98 merged commit f03f701 into master Aug 17, 2023
@Andres-CT98 Andres-CT98 deleted the u/acampostams/fix-select-cell-replace-text branch August 17, 2023 19:34
Andres-CT98 added a commit that referenced this pull request Aug 18, 2023
* Content Model: Improve cache behavior (#1999)

* Content Model: Improve cache behavior

* fix build

* fix comment

* Skip Trigger plugin event on plain text paste (#2011)

* init

* remove unneeded change

* Update return type

* Fix 218869: Do not allow dragging on readonly content (#2010)

* Fix 218869: Do not allow dragging readonly content

* fix test

* Content Model: Fix 194024 and 220289 (#2012)

* Content Model: Fix 221290 Support float for image (#2013)

* Content Model: improve formatWithContentModel 1 (#2001)

* Content Model: Improve cache behavior

* fix build

* Content Model: improve formatWithContentModel

* Content Model: improve formatWithContentModel 2 (#2002)

* Content Model: Improve cache behavior

* fix build

* Content Model: improve formatWithContentModel

* Content Model: improve formatWithContentModel 2

* fix format

* WIP

* fix handles

* MergeModel, do not inherit the styles of table when splitting the param (#2016)

* init

* init

* address comment

* update test names

* fixes

* Demo site: Fix insert link button in Content Model ribbon (#2018)

* Content Model: insertEntity API (#1800)

* Content Model insertEntity

* improve

* improve

* Content Model: Improve cache behavior

* fix build

* Content Model: improve formatWithContentModel

* Content Model: improve formatWithContentModel 2

* Improve

* fix build

* fix build

* improve

* add test

* add test

* add test

* add test

* fix dark color

* fix test

* fix build and test

* crop

* fix xase

* check cell exist

* Fix #1752, rename header to heading (#2020)

* fix cell empty cells

* fix flipped image

* Update logic to decide if we need to merge a table on paste. (#2022)

* Fix TableSelectionCopy

* update unit tests

* Fix 2

* address comment

* Content Model: Rename a test file (#2029)

* Fix Triple clicking a single cell selecting more than one (#2024)

* fix triple click, optimisation

* Remove `display: flex` style on paste (#2031)

* init

* Fix

* Replace first cell content if input while on cell selection (#2030)

* select first cell content and empty, add undo if change

* Content Model: Fix 222135 (#2035)

* Fix 222135

* fix build

* Content Model: Fix 219312 (#2036)

* Fix regression when creating the BeforePasteEvent (#2039)

* init

* fix build

* Content Model: Fix 220050 (#2037)

* Content Model: Fix 220050

* Fix build

* improve

* improve

* Simplify the domToModel call in `paste.ts` (#2040)

* add more changes

* fix build

* fix test

* Content Model: Support vertical-align for image (#2041)

* Content Model: Support vertical-align for image

* fix build and test

---------

Co-authored-by: Bryan Valverde U <[email protected]>

* version bump

---------

Co-authored-by: Jiuqing Song <[email protected]>
Co-authored-by: Bryan Valverde U <[email protected]>
Co-authored-by: Júlia Roldi <[email protected]>
Co-authored-by: Julia Roldi <[email protected]>
ianeli1 added a commit that referenced this pull request Sep 11, 2023
* Content Model: Improve cache behavior (#1999)

* Content Model: Improve cache behavior

* fix build

* fix comment

* Skip Trigger plugin event on plain text paste (#2011)

* init

* remove unneeded change

* Update return type

* Fix 218869: Do not allow dragging on readonly content (#2010)

* Fix 218869: Do not allow dragging readonly content

* fix test

* Content Model: Fix 194024 and 220289 (#2012)

* Content Model: Fix 221290 Support float for image (#2013)

* Content Model: improve formatWithContentModel 1 (#2001)

* Content Model: Improve cache behavior

* fix build

* Content Model: improve formatWithContentModel

* Content Model: improve formatWithContentModel 2 (#2002)

* Content Model: Improve cache behavior

* fix build

* Content Model: improve formatWithContentModel

* Content Model: improve formatWithContentModel 2

* fix format

* WIP

* fix handles

* MergeModel, do not inherit the styles of table when splitting the param (#2016)

* init

* init

* address comment

* update test names

* fixes

* Demo site: Fix insert link button in Content Model ribbon (#2018)

* Content Model: insertEntity API (#1800)

* Content Model insertEntity

* improve

* improve

* Content Model: Improve cache behavior

* fix build

* Content Model: improve formatWithContentModel

* Content Model: improve formatWithContentModel 2

* Improve

* fix build

* fix build

* improve

* add test

* add test

* add test

* add test

* fix dark color

* fix test

* fix build and test

* crop

* fix xase

* check cell exist

* Fix #1752, rename header to heading (#2020)

* fix cell empty cells

* fix flipped image

* Update logic to decide if we need to merge a table on paste. (#2022)

* Fix TableSelectionCopy

* update unit tests

* Fix 2

* address comment

* Content Model: Rename a test file (#2029)

* Fix Triple clicking a single cell selecting more than one (#2024)

* fix triple click, optimisation

* Remove `display: flex` style on paste (#2031)

* init

* Fix

* Replace first cell content if input while on cell selection (#2030)

* select first cell content and empty, add undo if change

* Content Model: Fix 222135 (#2035)

* Fix 222135

* fix build

* Content Model: Fix 219312 (#2036)

* Fix regression when creating the BeforePasteEvent (#2039)

* init

* fix build

* Content Model: Fix 220050 (#2037)

* Content Model: Fix 220050

* Fix build

* improve

* improve

* Simplify the domToModel call in `paste.ts` (#2040)

* add more changes

* fix build

* fix test

* Content Model: Support vertical-align for image (#2041)

* Content Model: Support vertical-align for image

* fix build and test

---------

Co-authored-by: Bryan Valverde U <[email protected]>

* Remove deprecated colors from borders, text and background. (#2045)

* Support more border styles

* init

* Revert  unrelated change

* Fix dependencies

* address comments

* try fix build

* reselection

* add space

* Content Model: Improve insertEntity (#2047)

* Content Model: Improve insertEntity

* fix test

* Content Model: Fix selection of entity (#2051)

* Content Model: Always return size in pt when getFormatState (#2052)

* Content Model: trigger ShadowEdit events (#2053)

* Content Model: trigger ShadowEdit events

* improve

* Content Model: Add solid paragraph in new table cell (#2055)

* Content Model: Do color transform for entity when copy/paste (#2056)

* Content Model: Do color transform for entity when copy/paste

* Call normalizeContentModel

* Content Model: Paste plain text applies current format (#2057)

* Content Model: Paste plain text applies current format

* fix build

* Content Model: Fix a regression of shadow edit (#2058)

* Update versions

* Fix double import

* Double import 2

---------

Co-authored-by: Jiuqing Song <[email protected]>
Co-authored-by: Bryan Valverde U <[email protected]>
Co-authored-by: Júlia Roldi <[email protected]>
Co-authored-by: Julia Roldi <[email protected]>
Co-authored-by: Andres-CT98 <[email protected]>
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.

Writing on a selected cell does not replace all text as expected
3 participants