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 widget plugin when keystorke is pressed #4072

Merged
merged 6 commits into from
Jun 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Fixed Issues:
* [#4008](https://github.com/ckeditor/ckeditor4/issues/4008): Fixed: [Remove Format](https://ckeditor.com/cke4/addon/removeformat) doesn't work with collapsed selection.
* [#909](https://github.com/ckeditor/ckeditor4/issues/909): Fixed: [Table Resize](https://ckeditor.com/cke4/addon/tableresize) plugin does not work when editor is placed in absolutely positioned container. Thanks to [Roland Petto](https://github.com/arpi68)!
* [#1959](https://github.com/ckeditor/ckeditor4/issues/1959): Fixed: [Table Resize](https://ckeditor.com/cke4/addon/tableresize) plugin does not work in [maximized](https://ckeditor.com/cke4/addon/maximize) editor when [Div Editing Area](https://ckeditor.com/cke4/addon/divarea) feature enabled. Thanks to [Roland Petto](https://github.com/arpi68)!
* [#3998](https://github.com/ckeditor/ckeditor4/issues/3998): Fixed: Error thrown when switching to [Source Mode](https://ckeditor.com/cke4/addon/sourcearea) via custom `Ctrl` + `Enter` [keystroke](https://ckeditor.com/docs/ckeditor4/latest/api/CKEDITOR_editor.html#method-setKeystroke) with [Widget](https://ckeditor.com/cke4/addon/widget) plugin present.

## CKEditor 4.14

Expand Down
12 changes: 9 additions & 3 deletions plugins/widget/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,10 @@
* This method is triggered by the {@link #event-checkSelection} event.
*/
checkSelection: function() {
if ( !this.editor.getSelection() ) {
return;
}

var sel = this.editor.getSelection(),
selectedElement = sel.getSelectedElement(),
updater = stateUpdater( this ),
Expand Down Expand Up @@ -2916,10 +2920,12 @@
// It's not possible to manually create selection which starts inside one widget and ends in another,
// so we are skipping this case to simplify implementation (#3498).
function fixCrossContentSelection() {
var selection = editor.getSelection(),
ranges = selection && selection.getRanges(),
range = ranges[ 0 ];
var selection = editor.getSelection();
if ( !selection ) {
return;
}

var range = selection.getRanges()[ 0 ];
if ( !range || range.collapsed ) {
return;
}
Expand Down
81 changes: 81 additions & 0 deletions tests/plugins/widget/keystroke.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/* bender-tags: widgetcore, 3998 */
/* bender-ckeditor-plugins: wysiwygarea,sourcearea,widget */

( function() {
'use strict';

function pressCtrlEnter( editor ) {
editor.editable().fire( 'keydown', new CKEDITOR.dom.event( { keyCode: CKEDITOR.CTRL + 13 } ) );
}

var currentError = null;
window.onerror = function( error ) {
currentError = error;
};

bender.editors = {
wysiwyg: {
config: {
keystrokes: [
[ CKEDITOR.CTRL + 13 /*Enter*/, 'source' ]
]
}
},
source: {
config: {
keystrokes: [
[ CKEDITOR.CTRL + 13 /*Enter*/, 'source' ]
],
startupMode: 'source'
}
}
};

bender.test( {
tearDown: function() {
currentError = null;
},

// (#3998)
'test change wysiwyg mode to source mode by pressing keystroke': function() {
var editor = this.editors.wysiwyg;

assert.areSame( 'wysiwyg', editor.mode, 'Wysiwyg mode is active at start.' );

editor.on( 'mode', function() {
resume( function() {
assert.areSame( 'source', editor.mode, 'Source mode is active (changed from wysiwyg mode).' );

if ( currentError ) {
assert.fail( currentError );
}
} );
} );

pressCtrlEnter( editor );

wait();
},

// (#3998)
'test change source mode to wysiwyg mode by pressing keystroke': function() {
var editor = this.editors.source;

assert.areSame( 'source', editor.mode, 'Source mode is active at start.' );

editor.on( 'mode', function() {
resume( function() {
assert.areSame( 'wysiwyg', editor.mode, 'Wysiwyg mode is active (changed from source mode).' );

if ( currentError ) {
assert.fail( currentError );
}
} );
} );

pressCtrlEnter( editor );

wait();
}
} );
} )();
11 changes: 11 additions & 0 deletions tests/plugins/widget/manual/keystroke.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<div id="editor">
Hello world.
</div>

<script>
CKEDITOR.replace( 'editor', {
keystrokes: [
[ CKEDITOR.CTRL + 13 /*Enter*/, 'source' ]
]
} );
</script>
29 changes: 29 additions & 0 deletions tests/plugins/widget/manual/keystroke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
@bender-tags: widget, bug, 4.14.1, 3998
@bender-ui: collapsed
@bender-ckeditor-plugins: toolbar, wysiwygarea, sourcearea, widget

1. Open browser dev console.

1. Place cursor inside editor.

1. Press <kbd>CTRL</kbd> + <kbd>Enter</kbd> key.

### Expected

Source mode is opened and no error appears in dev console.

### Unexpected

Error appears in dev console or source mode is not opened.

1. Place cursor inside editor.

1. Press <kbd>CTRL</kbd> + <kbd>Enter</kbd> key.

### Expected

Editor switches to WYSIWYG mode. No error appears in dev console.

### Unexpected

Error appears in dev console or WYSIWYG mode is not opened.