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

fix/1273: Delete or backspace in a empty classic text block removes it #2482

Merged
merged 2 commits into from
Aug 24, 2017

Conversation

BoardJames
Copy link

This pull request fixes issue #1273

Pressing delete or backspace inside an empty classic text block will now remove it.

@codecov
Copy link

codecov bot commented Aug 21, 2017

Codecov Report

Merging #2482 into master will increase coverage by 5.63%.
The diff coverage is 7.14%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #2482      +/-   ##
==========================================
+ Coverage   26.93%   32.57%   +5.63%     
==========================================
  Files         158      171      +13     
  Lines        4897     5744     +847     
  Branches      814     1027     +213     
==========================================
+ Hits         1319     1871     +552     
- Misses       3035     3253     +218     
- Partials      543      620      +77
Impacted Files Coverage Δ
blocks/library/freeform/old-editor.js 1.51% <7.14%> (+1.51%) ⬆️
blocks/inspector-controls/range-control/index.js 83.33% <0%> (-16.67%) ⬇️
blocks/library/paragraph/index.js 38.46% <0%> (-8.6%) ⬇️
blocks/library/audio/index.js 28% <0%> (-8.37%) ⬇️
blocks/api/paste/index.js 91.66% <0%> (-8.34%) ⬇️
blocks/library/video/index.js 25% <0%> (-5.77%) ⬇️
blocks/library/latest-posts/index.js 10% <0%> (ø) ⬆️
editor/sidebar/last-revision/index.js 0% <0%> (ø) ⬆️
blocks/editable/tinymce.js 0% <0%> (ø) ⬆️
editor/index.js 0% <0%> (ø) ⬆️
... and 25 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 5125ee1...c0933be. Read the comment docs.

@@ -67,6 +70,16 @@ export default class OldEditor extends Component {
} );
} );

editor.on( 'keydown', ( event ) => {
if ( ( event.keyCode === BACKSPACE || event.keyCode === DELETE ) &&
/^\n?$/.test( editor.getContent( { format: 'text' } ) ) ) {
Copy link
Member

Choose a reason for hiding this comment

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

How performant is { format: 'text' }? Does it need to run TinyMCE's serializer? If so, might be worryingly non-performant to occur on every keypress.

Copy link
Author

Choose a reason for hiding this comment

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

It is literally a call to body.innerText falling back to body.textContent so no it does not run the serializer. I think it should be fairly low cost.

Copy link
Author

@BoardJames BoardJames Aug 24, 2017

Choose a reason for hiding this comment

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

Though it does fire the 'BeforeGetContent' and 'GetContent' event by default. I could turn that off by passing { format: 'text', no_events: true } .

Copy link
Author

Choose a reason for hiding this comment

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

I rewrote the empty check to test the node counts first just in case.

@BoardJames BoardJames merged commit f3427c8 into master Aug 24, 2017
@BoardJames BoardJames deleted the fix/1273-delete-empty-classic-text-on-backspace branch August 24, 2017 23:23
// <p><br data-mce-bogus="1"></p>
// avoid expensive checks for large documents
const body = editor.getBody();
if ( body.childNodes > 1 ) {
Copy link
Member

Choose a reason for hiding this comment

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

Isn't childNodes a NodeList. In other words, shouldn't this be checking childNodes.length, not childNodes itself?

Copy link
Author

@BoardJames BoardJames Aug 25, 2017

Choose a reason for hiding this comment

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

Yes it should! Working on a fix.

Copy link
Author

Choose a reason for hiding this comment

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

PR to fix here: #2532

Copy link
Member

Choose a reason for hiding this comment

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

@EphoxJames Could editor.dom.isEmpty not be used?

Copy link
Author

@BoardJames BoardJames Aug 28, 2017

Choose a reason for hiding this comment

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

If I'm reading the source correctly I don't believe it would do what we want. If we called it passing in the paragraph tag of <p><br/></p> then it would return true but if we called it with the body of the editor then it would return false (effectively passing the div tag of <div><p><br/></p></div> ) because it would consider the p tag to be content.
It would also do a lot more work internally.

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.

4 participants