Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Excerpt

1. Initialization Options

There are several  several options letting you initialize WProofreader functionality in your web app, namely using: 

  • autoSearch option (config WEBSPELLCHECKER_CONFIG and inline data attributes);
  • init() method;
  • autocreate (inline data attributes).

The WEBSPELLCHECKER_CONFIG (further CONFIG) can be added in a script or loaded from the file as mentioned in Get Started with WProofreader Server (autoSearch) guide for Server version and Get Started with WProofreader Cloud (autoSearch)

After you have initialized WProofreader, you can customize the above-mentioned settings using WebSpellChecker WProofreader API.

Tip

For details, refer to Get Started with WProofreader Cloud AutoSearch and Get Started with WProofreader Server AutoSearch guides.

Below you can find the samples showing how different WProofreader initialization approaches work depending on the integration and expected launch behavior.

Example 1: Initializing WProofreader using autoSearch 

info

Table of Contents
maxLevel4
stylenone

1. Initializing using autoSearch 

The autoSearch feature enables detecting new editable fields on the page and proofreading the text they contain automatically

on hover selection

in focus. If you choose this option, WProofreader is enabled in the selected WYSIWYG editor or HTML editable control, and no additional actions are required. 

  • autoSearch is useful when your web page has multiple controls, as you can add one script which is enabled in the areas in focus automatically compared with creating an init() method for each control. From
the
  • a performance point of view, autoSearch also has
a
  • better performance as the grammar and spelling and style are checked only in active area compared with checking in all controls like in init(). 
  • autoSearch for a dynamic page where some fields can be hidden or added at a certain point is enabled automatically and releases the memory, helping to avoid
possible 
  • possible memory leaks. 

Initialization using inline attributes is suitable and useful if you want to have a single script with basic

options

option definitions. 

When you initialize WProofreader using this method, you need to declare the container where WProofreader should be started in init () method. The options are to be obtained from the settings.

Example 1.1. Initializing using CONFIG 

This is an example of the WProofreader initialization with the autoSearch functionality turned on. All the configuration options are defined using CONFIG.

Note

Please note that in case of initialization using attributes, a number of API parameters are not supported. Thus, you cannot modify such parameters as actionItems, suggestionsCount, and moreSuggestionsCount described further in this section. When initializing WProofreader using CONFIG, all options are supported.

Scenario A: Initializing WProofreader with autoSearch functionality and defining configuration options in CONFIG

Code Block
languagejs
themeEmacs
<script>
    window.WEBSPELLCHECKER_CONFIG = {
		autoSearch:true,
		lang: 'uk_UA',
		...

    	};
</script>

<script type="text/javascript" src="http(s)://your_host_name/wscservice/wscbundle/wscbundle.js"></script>
Scneario B: Initializing WProofreader with autoSearch functionality and defining configuration options using inline data attributes

Example 1.2. Initializing using inline data attributes

This is an example of the WProofreader initialization with the autoSearch functionality turned on. All the configuration options are defined using inline data attributes. In this case, the naming of the options are formed as follows: data-wsc-option_name=”value”. For details, refer to the list of available options in WProofreader API documentation.

Code Block
languagejs
themeEmacs
<script
	data-wsc-autosearch="true"
	data-wsc-lang="uk_UA"
	...
    src="https://your_host_name/wscservice/wscbundle/wscbundle.js">
</script>

Example 2: Initializing WProofreader using autoSearch and init() method

When you initialize WProofreader using this method, in init () method we declare the container where WProofreader should be started. The options are to be obtained from the settings.

Info
Note

When initializing using data attributes, a number of API parameters with array or number type are not supported. Thus, you cannot modify such parameters as actionItems, suggestionsCount, and moreSuggestionsCount described further in this section. When initializing WProofreader using CONFIG, all options are supported.

2. Initializing using init() method

The init() method is a reasonable choice when you clearly know in which control you want to initialize

in

WProofreader. More than that you are aware of your page controls, and additional controls do not appear dynamically. 

  • If your web
application
  • page has several
text areas
  • editable elements where you want to initialize WProofreader, as soon as the user loads the page, WProofreader is enabled automatically. It is not required to select the area to enable grammar and spelling check in it compared with autoSearch method. 
  • With dynamic web page load using init() method, administrator needs to be monitor when it’s necessary to start WProofreader.

Example 2.1. Initializing in HTML element using init() method

This is an example of WProofreader initialization in HTML contenteditable element (div) using init() method

in a defined container on the page and specifying

. The configuration options are specified directly in the init() function

:

.

Code Block
languagejs
themeEmacs
<script type="text/javascript" src="http(s)://your_host_name/wscservice/wscbundle/wscbundle.js"></script>

<div contenteditable id="container1">
This	<p>This sampl text is aimed at demonstrating the work of WProofreader in a contenteditable div element.</p>
</div>
  
<script>
    var instance1 = WEBSPELLCHECKER.init({
		container: document.getElementById("container1"),
		lang: 'uk_UA',
		...
	    });
</script>

Example

3: Initialization

2.2. Initializating in multiple HTML elements using init() method

in a defined container

This is an example of WProofreader initialization in HTML contenteditable element (div) and HTML editable element (textarea) using init() method. The configuration options for both controls are defined separately in a single CONFIG.

Info

When you initialize WProofreader using this method, in init () method we declare the container where WProofreader should be started. The options are to be obtained from the settings.

The CONFIG can be added in a script or loaded from the file as mentioned in Get Started with WProofreader Cloud AutoS-search and Get Started with WProofreader Server AutoS-search guides as well as added into inline data attributes.

Initializing WProofreader using init() method in a defined container(s) on the page and specifying configuration options in CONFIG is shown in the sample below. You can see two containers, namely, div and textarea which use a single configuration script:

Code Block
languagejs
themeEmacs
<script>
    	window.WEBSPELLCHECKER_CONFIG = {
		lang: 'uk_UA',
		...
    };
</script>

<script type="text/javascript" src="http(s)://your_host_name/wscservice/wscbundle/wscbundle.js"></script>

<div contenteditable id="container1">
This	<p>This sampl text is aimed at demonstrating the work of WProofreader in a contenteditable div element.</p>
</div>

<script>
    var instance1 = WEBSPELLCHECKER.init({
		container: document.getElementById("container1"),
	    });
</script>

<textarea id="container2" type="text">This sampl text is aimed at demonstrating the work of WProofreader in a textarea textform element.
</textarea>
  
<script>
    var instance2 = WEBSPELLCHECKER.init({
		container: document.getElementById("container2"),
	    });
</script>

Example

4: Initializing WProofreader by redefining options priority

2.3. Initializing in WYSIWYG editors using init() method

There is an option to explicitly initialize WProofreader in such rich text editors as Froala Editor 3, CKEditor 4, and CKEditor 5 using init() method. However, it is strongly recommended to use this method with autoSearch and autoDestroy options in CONFIG.

Turned on/enabled autoDestroy parameter will be monitoring the state of the WEBSPELLCHECKER instance and handling its destroy after removal (deleted or hidden) of an editable container from the page. At the same time enabled autoSearch parameter will restore an instance when it is needed. It can be useful for example, when switching to the code editing mode in the editor, WEBSPELLCHECKER instance must be deleted and then restored after returning back to the editor. Thus, it is the autoSearch that restores it.

Excerpt Include
_include_initializing_wproofreader_using_init_in_ckeditor4_55x
_include_initializing_wproofreader_using_init_in_ckeditor4_55x
nopaneltrue

Excerpt Include
_include_initializing_wproofreader_using_init_in_ckeditor5_55x
_include_initializing_wproofreader_using_init_in_ckeditor5_55x
nopaneltrue

Excerpt Include
_include_initializing_wproofreader_using_init_in_froala_editor_55x
_include_initializing_wproofreader_using_init_in_froala_editor_55x
nopaneltrue

3. Initializing using data-wsc-autocreate

Initializing WProofreader in HTML controls using data-wsc-autocreate="true" and specifying configuration options in CONFIG. This approach is similar to initializing WProofreader using init() method.

Example 3.1. Initializing in HTML elements using data-wsc-autocreate

This is an example of WProofreader initialization in HTML contenteditable element (div) and HTML editable element (textarea) using data-wsc-autocreate="true". The configuration options are to be defined separately in CONFIG. 

Note

Even though though we have a CONFIG where we defined Ukrainian as a priority the language (Ukraine)language for check, the language for textarea will be taken obtained  from the data-wsc-lang="es_ES" setting. It will have higher priority and, thus, will rewrite the CONFIG settings in the sample below.

Code Block
languagejs
themeEmacs
<script>
    window.WEBSPELLCHECKER_CONFIG = {
        lang:’uk_UA’,       
    };
</script>

<script type="text/javascript" src="http(s)://your_host_name/wscservice/wscbundle/wscbundle.js"></script>

<div contenteditable data-wsc-autocreate="true">
This	<p>This sampl text is aimed at demonstrating the work of WProofreader in a plain textarea element.</p>
</div>

<textarea type="text" data-wsc-autocreate="true" data-wsc-lang="es_ES">This sampl text is aimed at demonstrating the work of WProofreader in a textarea textform element.
</textarea>

Initializing WProofreader using init() method using autoSearch and autoDestroy in WYSIWYG editors such as Froala Editor 3, CKEditor 4 and CKEditor 5 and specifying the options in CONFIG.

Code Block
languagejs
themeEmacs
new FroalaEditor('#froala-editor', {
        events: {
            'initialized': function() {
                WEBSPELLCHECKER.init({
                    container: this.el
                });
            }
        }
    });
</script>```
CKEditor 4:
```CKEDITOR.on('instanceReady', function(event) {
    WEBSPELLCHECKER.init({
        container: event.editor.window.getFrame().$
    });
});```
CKEditor 5:
```ClassicEditor
    .create(document.querySelector('#editor5'))
    .then(editor => {
        WEBSPELLCHECKER.init({
            container: editor.ui._editableElements.get('main')
        });
    });``

Froala Editor code sample:

Code Block
languagejs
themeEmacs
<div id="froala-editor">
    <p>These are an examples of a sentences with two mispelled words and grammar problems. Just type text with misspelling to see how it works.</p>
</div>

<!-- Include the WEBSPELLCHECKER_CONFIG variable. -->
<script>
    window.WEBSPELLCHECKER_CONFIG = {
autoSearch: true,
autoDestroy: true,
...	
    };
</script>
<script type="text/javascript" src="http(s)://your_host_name/wscservice/wscbundle/wscbundle.js"></script>
<script>
    new FroalaEditor('#froala-editor', {
        events: {
            'initialized': function() {
                WEBSPELLCHECKER.init({
                    container: this.el
                });
            }
        }
    });
</script>

Example 5: Using init() method with autoSearch and autoDestroy in WYSIWYG editors

When initializing WProofreader using init() method with autoSearch and autoDestroy in WYSIWYG editors such as Froala Editor 3, CKEditor 4, and CKEditor 5 and specifying the options in CONFIG, the autoDestroy parameter turns on/off the auto-destroy functionality of a WebSpellChecker instance. By default, it is enabled for WProofreader initialized using autoSearch. It can be used to avoid monitoring the state of the instance and handling its destroying after removal of an editable container from the page.

It can be useful for example, when we have created our instance, and later we don’t need to worry to remove it in certain cases. Deleting an instance from the page takes place when an edited element is deleted or hidden. When switching to the code editing mode in the editor we need to delete our instance and then to restore it after returning back to the editor. Thus, it is the autoSearch that restores it.

Code Block
languagejs
themeEmacs
new FroalaEditor('#froala-editor', {
        events: {
            'initialized': function() {
                WEBSPELLCHECKER.init({
                    container: this.el
                });
            }
        }
    });
</script>```

CKEditor 4:
```CKEDITOR.on('instanceReady', function(event) {
    WEBSPELLCHECKER.init({
        container: event.editor.window.getFrame().$
    });
});```

CKEditor 5:
```ClassicEditor
    .create(document.querySelector('#editor5'))
    .then(editor => {
        WEBSPELLCHECKER.init({
            container: editor.ui._editableElements.get('main')
        });
    });``

Froala Editor example: 

Code Block
languagejs
themeEmacs
<div id="froala-editor">
    <p>These are an examples of a sentences with two mispelled words and grammar problems. Just type text with misspelling to see how it works.</p>
</div>

<!-- Include the WEBSPELLCHECKER_CONFIG variable. -->
<script>
    window.WEBSPELLCHECKER_CONFIG = {
autoSearch: true,
autoDestroy: true,
...	
    };
</script>

<script type="text/javascript" src="http(s)://your_host_name/wscservice/wscbundle/wscbundle.js"></script>

<script>
    new FroalaEditor('#froala-editor', {
        events: {
            'initialized': function() {
                WEBSPELLCHECKER.init({
                    container: this.el
                });
            }
        }
    });
</script>