Report an issue
Class

CKEDITOR.plugins.textWatcher

class since 4.10.0

API exposed by the Text Watcher plugin.

Class implementing the text watcher — a base for features like autocomplete. It fires the matched and unmatched events based on changes in the text and the position of the caret in the editor.

To check whether the text matches some criteria, the text watcher uses a callback function which should return the matching text and a CKEDITOR.dom.range for that text.

Since the text watcher works on the DOM where searching for text is pretty complicated, it is usually recommended to use the CKEDITOR.plugins.textMatch.match function.

Example:

function textTestCallback( range ) {
    // You do not want to autocomplete a non-empty selection.
    if ( !range.collapsed ) {
        return null;
    }

    // Use the text match plugin which does the tricky job of doing
    // a text search in the DOM. The matchCallback function should return
    // a matching fragment of the text.
    return CKEDITOR.plugins.textMatch.match( range, matchCallback );
}

function matchCallback( text, offset ) {
    // Get the text before the caret.
    var left = text.slice( 0, offset ),
        // Will look for an '@' character followed by word characters.
        match = left.match( /@\w*$/ );

    if ( !match ) {
        return null;
    }
    return { start: match.index, end: offset };
}

// Initialize the text watcher.
var textWatcher = new CKEDITOR.plugins.textWatcher( editor, textTestCallback );
// Start listening.
textWatcher.attach();

 // Handle text matching.
textWatcher.on( 'matched', function( evt ) {
    autocomplete.setQuery( evt.data.text );
} );

Filtering

Properties

  • callback : Function

    readonly

    The callback passed to the CKEDITOR.plugins.textWatcher constructor.

  • editor : editor

    readonly

    The editor instance which the text watcher watches.

  • ignoreNext : Boolean

    readonly

    Whether the next check should be ignored. See the consumeNext method.

    Defaults to false

  • ignoredKeys : Number[]

    readonly

    Keys that should be ignored by the check method.

    Defaults to [16, 17, 18, 91, 35, 36, 37, 38, 39, 40, 33, 34]

  • lastMatched : String

    readonly

    The last matched text.

  • throttle : Number

    readonly

    Indicates throttle threshold mitigating text checks.

    Higher levels of the throttle threshold will create a delay for text watcher checks but also improve its performance.

    See the throttle feature for more information.

    Defaults to 0

  • _buffer : Object

    private

    The throttle buffer used to mitigate text checks.

  • _listeners : Array

    private

    Listeners registered by this text watcher.

    Defaults to []

Static properties

Methods

  • constructor( editor, callback, [ throttle ] ) → textWatcher

    Creates the text watcher instance.

    Parameters

    editor : editor

    The editor instance to watch in.

    callback : Function

    Callback executed when the text watcher thinks that something might have changed.

    Properties
    range : range

    The range representing the caret position.

    [ return ] : Object

    Matching text data (null if nothing matches).

    Properties
    text : String

    The matching text.

    range : range

    A range in the DOM for the text that matches.

    Defaults to null

    [ throttle ] : Number

    Throttle interval, see throttle.

    Defaults to 0

    Returns

    textWatcher
  • attach() → textWatcher

    chainable

    Attaches the text watcher to the editor.

    Returns

    textWatcher

    this

  • capture()

    Register event handler under the capturing stage on supported target.

  • check( [ evt ] )

    Triggers a text check. Fires the matched and unmatched events. The matched event will not be fired twice in a row for the same text unless the text watcher is reset.

    Parameters

    [ evt ] : event | eventInfo
  • consumeNext() → textWatcher

    chainable

    Ignores the next check.

    Returns

    textWatcher

    this

  • define( name, meta )

    Predefine some intrinsic properties on a specific event name.

    Parameters

    name : String

    The event name

    meta : Object
    Properties
    [ errorProof ]

    Whether the event firing should catch error thrown from a per listener call.

    Defaults to false

  • destroy()

    Destroys the text watcher instance. The DOM event listeners will be cleaned up.

  • fire( eventName, [ data ], [ editor ] ) → Boolean | Object

    Fires an specific event in the object. All registered listeners are called at this point.

    someObject.on( 'someEvent', function() { ... } );
    someObject.on( 'someEvent', function() { ... } );
    someObject.fire( 'someEvent' );             // Both listeners are called.
    
    someObject.on( 'someEvent', function( event ) {
        alert( event.data );                    // 'Example'
    } );
    someObject.fire( 'someEvent', 'Example' );
    

    Parameters

    eventName : String

    The event name to fire.

    [ data ] : Object

    Data to be sent as the CKEDITOR.eventInfo.data when calling the listeners.

    [ editor ] : editor

    The editor instance to send as the CKEDITOR.eventInfo.editor when calling the listener.

    Returns

    Boolean | Object

    A boolean indicating that the event is to be canceled, or data returned by one of the listeners.

  • fireOnce( eventName, [ data ], [ editor ] ) → Boolean | Object

    Fires an specific event in the object, releasing all listeners registered to that event. The same listeners are not called again on successive calls of it or of fire.

    someObject.on( 'someEvent', function() { ... } );
    someObject.fire( 'someEvent' );         // Above listener called.
    someObject.fireOnce( 'someEvent' );     // Above listener called.
    someObject.fire( 'someEvent' );         // No listeners called.
    

    Parameters

    eventName : String

    The event name to fire.

    [ data ] : Object

    Data to be sent as the CKEDITOR.eventInfo.data when calling the listeners.

    [ editor ] : editor

    The editor instance to send as the CKEDITOR.eventInfo.editor when calling the listener.

    Returns

    Boolean | Object

    A booloan indicating that the event is to be canceled, or data returned by one of the listeners.

  • hasListeners( eventName ) → Boolean

    Checks if there is any listener registered to a given event.

    var myListener = function() { ... };
    someObject.on( 'someEvent', myListener );
    alert( someObject.hasListeners( 'someEvent' ) );    // true
    alert( someObject.hasListeners( 'noEvent' ) );      // false
    

    Parameters

    eventName : String

    The event name.

    Returns

    Boolean
  • on( eventName, listenerFunction, [ scopeObj ], [ listenerData ], [ priority ] ) → Object

    Registers a listener to a specific event in the current object.

    someObject.on( 'someEvent', function() {
        alert( this == someObject );        // true
    } );
    
    someObject.on( 'someEvent', function() {
        alert( this == anotherObject );     // true
    }, anotherObject );
    
    someObject.on( 'someEvent', function( event ) {
        alert( event.listenerData );        // 'Example'
    }, null, 'Example' );
    
    someObject.on( 'someEvent', function() { ... } );                       // 2nd called
    someObject.on( 'someEvent', function() { ... }, null, null, 100 );      // 3rd called
    someObject.on( 'someEvent', function() { ... }, null, null, 1 );        // 1st called
    

    Parameters

    eventName : String

    The event name to which listen.

    listenerFunction : Function

    The function listening to the event. A single CKEDITOR.eventInfo object instanced is passed to this function containing all the event data.

    [ scopeObj ] : Object

    The object used to scope the listener call (the this object). If omitted, the current object is used.

    [ listenerData ] : Object

    Data to be sent as the CKEDITOR.eventInfo.listenerData when calling the listener.

    [ priority ] : Number

    The listener priority. Lower priority listeners are called first. Listeners with the same priority value are called in registration order.

    Defaults to 10

    Returns

    Object

    An object containing the removeListener function, which can be used to remove the listener at any time.

  • once()

    Similiar with on but the listener will be called only once upon the next event firing.

  • removeAllListeners()

    Remove all existing listeners on this object, for cleanup purpose.

  • removeListener( eventName, listenerFunction )

    Unregisters a listener function from being called at the specified event. No errors are thrown if the listener has not been registered previously.

    var myListener = function() { ... };
    someObject.on( 'someEvent', myListener );
    someObject.fire( 'someEvent' );                 // myListener called.
    someObject.removeListener( 'someEvent', myListener );
    someObject.fire( 'someEvent' );                 // myListener not called.
    

    Parameters

    eventName : String

    The event name.

    listenerFunction : Function

    The listener function to unregister.

  • unmatch() → textWatcher

    chainable

    Resets the state and fires the unmatched event.

    Returns

    textWatcher

    this

Events

  • matched( evt )

    Event fired when the text is no longer matching.

    Parameters

    evt : eventInfo
    Properties
    data : Object

    The value returned by the callback.

    Properties
    text : String
    range : range
  • unmatched( evt )

    Event fired when the text stops matching.

    Parameters

    evt : eventInfo