froala:editor-reactive

v3.2.7Published 3 years ago

Froala Reactive WYSIWYG HTML Editor

Froala-Reactive provides a template-based, reactive wrapper around the Froala WYSIWYG HTML Editor, designed to play nicely with Meteor Framework client-side templates.

Note that Froala Editor requires a license for commercial use.

Breaking Change to v2

Version 2.0.0 of this package onwards uses the upgraded Froala Editor V2. You will need to update and revise all Froala Editor API usage in your code (e.g. events, additional Froala Editor method calls, options) as described in the V2 migration guide. Please contact Froala directly for help, unless you really think there is an issue in the reactive wrapper code in this package(!)

Installation

You can install Froala-Reactive using Meteor's package management system The Froala-Reactive package automatically includes the separate froala:editor package which provides the actual Froala Editor javascript library:

meteor add froala:editor-reactive

Basic Usage

Froala-Reactive provides a Template inclusion tag which wraps the underlying Froala-Editor jQuery plugin. In it's simplest form, add it to your template like this:

1<template name="myTemplate">
2  <div>
3    {{> froalaReactive getFEContext}}
4  </div>
5</template>
1Template.myTemplate.helpers({
2  getFEContext: function () {
3    var self = this;
4    self.myDoc.myHTMLField = 'Hello World !';
5    return {
6      // Set html content
7      _value: self.myDoc.myHTMLField,
8      // Preserving cursor markers
9      _keepMarkers: true,
10      // Override wrapper class 
11      _className: "froala-reactive-meteorized-override",
12
13      // Set some FE options
14      // toolbarInline: true,
15      initOnClick: false,
16      tabSpaces: false,
17
18      // FE save.before event handler function:
19      "_onsave.before": function ( editor) {
20        // Get edited HTML from Froala-Editor
21        var newHTML = editor.html.get(true /* keep_markers */);
22        // Do something to update the edited value provided by the Froala-Editor plugin, if it has changed:
23        if (!_.isEqual(newHTML, self.myDoc.myHTMLField)) {
24          console.log("onSave HTML is :"+newHTML);
25          myCollection.update({_id: self.myDoc._id}, {
26            $set: {myHTMLField: newHTML}
27          });
28        }
29        return false; // Stop Froala Editor from POSTing to the Save URL
30      },
31    }
32  },
33})

Where:

  • The "myTemplate" template has a data context that contains a 'myDoc' property, which itself contains '_id' and 'myHTMLField' properties.
  • We use a helper to build the data context object to pass to the froalalReactive template.
  • We set some Froala Editor options
  • The "_onsave.before" property provides a callback function to handle the Froala-Editor save.before event.
  • The _value argument provides the HTML string that you want to display and edit

Here, we are triggering the update of the underlying 'myDoc' document record in the 'myCollection' collection when the Froala Editor 'beforeSave' event triggers. We could easily have used the 'blur' or 'contentChanged' events instead.

The final line in the callback stops Froala Editor from generating its own AJAX call to post the updated HTML contents, because we have used the awesomeness of Meteor to do that for us instead.

Note that Froala-Reactive does not automatically update the edited _value, you have to provide your own Froala-Editor event handler(s) to do that.

However, Froala-Reactive will reactively update the displayed _value HTML immediately if you have assigned it to a data context property or template helper function which changes its value any time after the template has rendered (e.g. if the underlying collection document is updated from the server, or another action on the client).

Events

You can provide callbacks for any of the Froala-Editor events by specifying _on<event name> arguments in the {{> froalaReactive}} inclusion tag with name of template helper functions that must return a function with the expected Froala-Editor event function signature.

For example, to set up a callback for the image.beforeUpload event:

1{{> froalaReactive ...  _onimage.beforeUpload=imagePasted ...}}
1Template.myTemplate.helpers({
2  imagePasted: function () {
3    var self = this;
4    return function (editor, img) {
5      // Do something
6    };
7  }
8});

Note that the event name used in the _on<event name> argument name must be exactly the same as used in the Froala Editor on('<event name>', function ....) callback declaration.

Options

Similarly, you can pass any of the Froala-Editor options to the underlying Froala-Editor plugin object, by simply declaring them as arguments to the froalaReactive inclusion tag. Also, if any of these option argument values are set to values on your template's data context, or from return vaues from template helpers, Froala-Reactive will call the Froala Editor option setter method to change them if any of them change values once your template has been rendered.

1{{> froalaReactive ... language=getLanguage ...}}
1Template.myTemplate.helpers({
2  getlanguage: function () {
3    return Session.get('language');
4  }
5})

Note that option values cannot be changed after initialisation (e.g. inlineMode) ... please refer to the Meteor-Editor documentation.

_className wrapper div class name override

If you have multiple instances of {{froalaReactive}} in the same template, and you need to target the underlying FroalaEditor instance in each, override the wrapping div class name in each instance to be a unique value, by specifying the _className=fooClass context property.

_keepMarkers

If you preserve the current cursor position marker when saving the FroalaEditor contents (using html.get keep_markers method flag), then also set _keepMarkers=true context property. This ensures that FroalaReactive's comparison between current and new contents takes the market html into account.

Methods

You can invoke any of the Froala Editor methods directly on the editor object in your Froala Editor event callback functions. See above for an example of calling editor.html.get().

Gotchas

  1. Remember that you must provide one or more _on callbacks to handle changing the froalaEditor contents, if you want use the Meteor Framework to do so.
  2. If two or more users are actively editing the same underlying state (e.g. the same property of the same document in a collection), and you have set up a contentChanged event handler, or an autosaving Froala Editor, then the content will keep changing. Their local caret cursor will keep resetting and jumping around. To avoid this, you may want to implement some kind of locking mechanism, to only one user can initiate an edit session at a time. To do this properly requires implementing something like Operational Transform!
  3. The Froala Editor V2 API has renamed some methods with dotted notation (e.g. save.before. This means you cannot set their values directly in a blaze template (it throws an error in the blaze compiler if you try something like {{froalaReactive onsave.before=doSave}}). Instead, you will have to create a template helper function that builds the complete context JSON object ... see the example given in the Basic section above.

Acknowledgements

This package is based on the implementation of the x-editable-reactive-template package.

License

This package is released under the MIT License (see LICENSE). However, in order to use Froala WYSIWYG HTML Editor plugin you should purchase a license for it.

Froala Editor has 3 different licenses for commercial use. For details please see License Agreement.