This repository has been archived by the owner on Jun 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1330 from ckeditor/t/1329
Feature: Introduced composition observer. Closes #1329.
- Loading branch information
Showing
8 changed files
with
249 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/** | ||
* @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved. | ||
* For licensing, see LICENSE.md. | ||
*/ | ||
|
||
/** | ||
* @module engine/view/observer/compositionobserver | ||
*/ | ||
|
||
import DomEventObserver from './domeventobserver'; | ||
|
||
/** | ||
* {@link module:engine/view/document~Document#event:compositionstart Compositionstart}, | ||
* {@link module:engine/view/document~Document#event:compositionupdate compositionupdate} and | ||
* {@link module:engine/view/document~Document#event:compositionend compositionend} events observer. | ||
* | ||
* Note that this observer is attached by the {@link module:engine/view/view~View} and is available by default. | ||
* | ||
* @extends module:engine/view/observer/domeventobserver~DomEventObserver | ||
*/ | ||
export default class CompositionObserver extends DomEventObserver { | ||
constructor( view ) { | ||
super( view ); | ||
|
||
this.domEventType = [ 'compositionstart', 'compositionupdate', 'compositionend' ]; | ||
const document = this.document; | ||
|
||
document.on( 'compositionstart', () => { | ||
document.isComposing = true; | ||
} ); | ||
|
||
document.on( 'compositionend', () => { | ||
document.isComposing = false; | ||
} ); | ||
} | ||
|
||
onDomEvent( domEvent ) { | ||
this.fire( domEvent.type, domEvent ); | ||
} | ||
} | ||
|
||
/** | ||
* Fired when composition starts inside one of the editables. | ||
* | ||
* Introduced by {@link module:engine/view/observer/compositionobserver~CompositionObserver}. | ||
* | ||
* Note that because {@link module:engine/view/observer/compositionobserver~CompositionObserver} is attached by the | ||
* {@link module:engine/view/view~View} this event is available by default. | ||
* | ||
* @see module:engine/view/observer/compositionobserver~CompositionObserver | ||
* @event module:engine/view/document~Document#event:compositionstart | ||
* @param {module:engine/view/observer/domeventdata~DomEventData} data Event data. | ||
*/ | ||
|
||
/** | ||
* Fired when composition is updated inside one of the editables. | ||
* | ||
* Introduced by {@link module:engine/view/observer/compositionobserver~CompositionObserver}. | ||
* | ||
* Note that because {@link module:engine/view/observer/compositionobserver~CompositionObserver} is attached by the | ||
* {@link module:engine/view/view~View} this event is available by default. | ||
* | ||
* @see module:engine/view/observer/compositionobserver~CompositionObserver | ||
* @event module:engine/view/document~Document#event:compositionupdate | ||
* @param {module:engine/view/observer/domeventdata~DomEventData} data Event data. | ||
*/ | ||
|
||
/** | ||
* Fired when composition ends inside one of the editables. | ||
* | ||
* Introduced by {@link module:engine/view/observer/compositionobserver~CompositionObserver}. | ||
* | ||
* Note that because {@link module:engine/view/observer/compositionobserver~CompositionObserver} is attached by the | ||
* {@link module:engine/view/view~View} this event is available by default. | ||
* | ||
* @see module:engine/view/observer/compositionobserver~CompositionObserver | ||
* @event module:engine/view/document~Document#event:compositionend | ||
* @param {module:engine/view/observer/domeventdata~DomEventData} data Event data. | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<div id="editor"> | ||
<p>foo bar</p> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/** | ||
* @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved. | ||
* For licensing, see LICENSE.md. | ||
*/ | ||
|
||
/* globals console, window, document */ | ||
|
||
import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classiceditor'; | ||
import Typing from '@ckeditor/ckeditor5-typing/src/typing'; | ||
import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph'; | ||
|
||
ClassicEditor | ||
.create( document.querySelector( '#editor' ), { | ||
plugins: [ Typing, Paragraph ] | ||
} ) | ||
.then( editor => { | ||
window.editor = editor; | ||
|
||
const view = editor.editing.view; | ||
const viewDocument = view.document; | ||
|
||
viewDocument.on( 'compositionstart', ( evt, data ) => console.log( 'compositionstart', data ) ); | ||
viewDocument.on( 'compositionupdate', ( evt, data ) => console.log( 'compositionupdate', data ) ); | ||
viewDocument.on( 'compositionend', ( evt, data ) => console.log( 'compositionend', data ) ); | ||
|
||
view.focus(); | ||
} ) | ||
.catch( err => { | ||
console.error( err.stack ); | ||
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
* Expected initialization: `{}foo bar`. | ||
* Check whether composition events are logged to the console with proper data: | ||
* `compositionstart`, | ||
* `compositionupdate`, | ||
* `compositionend` | ||
|
||
**Composition events are fired while typing:** | ||
* Hiragana, | ||
* Spanish-ISO: accent `' + a`, | ||
* MacOS: long `a` press (accent balloon) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/** | ||
* @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved. | ||
* For licensing, see LICENSE.md. | ||
*/ | ||
|
||
/* globals document */ | ||
import CompositionObserver from '../../../src/view/observer/compositionobserver'; | ||
import View from '../../../src/view/view'; | ||
|
||
describe( 'CompositionObserver', () => { | ||
let view, viewDocument, observer; | ||
|
||
beforeEach( () => { | ||
view = new View(); | ||
viewDocument = view.document; | ||
observer = view.getObserver( CompositionObserver ); | ||
} ); | ||
|
||
afterEach( () => { | ||
view.destroy(); | ||
} ); | ||
|
||
it( 'should define domEventType', () => { | ||
expect( observer.domEventType ).to.deep.equal( [ 'compositionstart', 'compositionupdate', 'compositionend' ] ); | ||
} ); | ||
|
||
describe( 'onDomEvent', () => { | ||
it( 'should fire compositionstart with the right event data', () => { | ||
const spy = sinon.spy(); | ||
|
||
viewDocument.on( 'compositionstart', spy ); | ||
|
||
observer.onDomEvent( { type: 'compositionstart', target: document.body } ); | ||
|
||
expect( spy.calledOnce ).to.be.true; | ||
|
||
const data = spy.args[ 0 ][ 1 ]; | ||
expect( data.domTarget ).to.equal( document.body ); | ||
} ); | ||
|
||
it( 'should fire compositionupdate with the right event data', () => { | ||
const spy = sinon.spy(); | ||
|
||
viewDocument.on( 'compositionupdate', spy ); | ||
|
||
observer.onDomEvent( { type: 'compositionupdate', target: document.body } ); | ||
|
||
expect( spy.calledOnce ).to.be.true; | ||
|
||
const data = spy.args[ 0 ][ 1 ]; | ||
expect( data.domTarget ).to.equal( document.body ); | ||
} ); | ||
|
||
it( 'should fire compositionend with the right event data', () => { | ||
const spy = sinon.spy(); | ||
|
||
viewDocument.on( 'compositionend', spy ); | ||
|
||
observer.onDomEvent( { type: 'compositionend', target: document.body } ); | ||
|
||
expect( spy.calledOnce ).to.be.true; | ||
|
||
const data = spy.args[ 0 ][ 1 ]; | ||
expect( data.domTarget ).to.equal( document.body ); | ||
} ); | ||
} ); | ||
|
||
describe( 'handle isComposing property of the document', () => { | ||
let domMain; | ||
|
||
beforeEach( () => { | ||
domMain = document.createElement( 'div' ); | ||
} ); | ||
|
||
it( 'should set isComposing to true on compositionstart', () => { | ||
observer.onDomEvent( { type: 'compositionstart', target: domMain } ); | ||
|
||
expect( viewDocument.isComposing ).to.equal( true ); | ||
} ); | ||
|
||
it( 'should set isComposing to false on compositionend', () => { | ||
observer.onDomEvent( { type: 'compositionstart', target: domMain } ); | ||
|
||
expect( viewDocument.isComposing ).to.equal( true ); | ||
|
||
observer.onDomEvent( { type: 'compositionend', target: domMain } ); | ||
|
||
expect( viewDocument.isComposing ).to.equal( false ); | ||
} ); | ||
|
||
it( 'should not change isComposing on compositionupdate during composition', () => { | ||
observer.onDomEvent( { type: 'compositionstart', target: domMain } ); | ||
|
||
expect( viewDocument.isComposing ).to.equal( true ); | ||
|
||
observer.onDomEvent( { type: 'compositionupdate', target: domMain } ); | ||
|
||
expect( viewDocument.isComposing ).to.equal( true ); | ||
} ); | ||
|
||
it( 'should not change isComposing on compositionupdate outside composition', () => { | ||
expect( viewDocument.isComposing ).to.equal( false ); | ||
|
||
observer.onDomEvent( { type: 'compositionupdate', target: domMain } ); | ||
|
||
expect( viewDocument.isComposing ).to.equal( false ); | ||
} ); | ||
} ); | ||
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters