Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Integrate CKEditor in Meteor

I'm trying to use CKEditor in a meteor application: My attemps:

  1. Put CKEditor folder with all the files (js, css, lang, plugins and skins) in the public folder, include the reference to the javascript file (ckeditor.js) in the header and use the appropiate class in textarea elements. Failed because the editor only works if the textarea is in the body (in any template the textarea control remains unmodified).

  2. Put the javascript files (ckeditor.js, config.js, styles.js) in client/lib/compatibility folder and the remaining files in the public folder. This time the application cant locate the files (skins, plugins, ...) because is looking for localhost:3000/client/lib/compatibility/ckeditor/ ...

Has anybody make this integration works before?

like image 779
Oscar Saraza Avatar asked Nov 15 '13 20:11

Oscar Saraza


2 Answers

I got this working and wanted to post a solution for future visitors. First, you need to put everything from the CKEDITOR build download in the public folder. CKEDITOR comes with all sorts of stuff and references everything based on relative directories.

Your public folder should have a directory named ckeditor it should contain contain the following files and folders:

 adapters
 lang
 plugins
 skins
 ckeditor.js
 config.js
 contents.css
 styles.js

In your primary layout file reference CKEDITOR like so:

 <head>
   <script type="text/javascript" src="/ckeditor/ckeditor.js"></script>
   <script type="text/javascript" src="/ckeditor/adapters/jquery.js"></script>
 </head>

In your template:

 <template name="yourTemplate">
   <textarea id="content" name="content"></textarea>
 </template>

Finally, in the rendered function of your template:

 Template.yourTemplate.rendered = function() {
   $('#content').ckeditor();
 };

Normally, you would say this.$('#content').ckeditor() but that doesn't work because CKEDITOR is in your public folder. As a result, you need to the global reference to the #content element.

like image 192
Brandon Avatar answered Nov 14 '22 07:11

Brandon


Put only the CKEditor files that you would've included in <head> inside a folder in client/lib, i.e. client/lib/ckeditor. That's all you need to do to get them served to the client: there's no need to reference anything in any <head> or anything like that. All .js and .css files that Meteor finds inside client are automatically concatenated and served to the client. This applies to any client-side library, not just CKEditor.

The next thing you need to do is cause CKEditor to be initialized on the pages that use it. Say you have a template called edit with a textarea with an ID of editor. And say you're also loading the CKEditor jQuery Adapter. Inside a JavaScript file within client, put:

Template.edit.rendered = function() {
  $('#editor').ckeditor();
}

The key here is that the initialization happens after the textarea editor exists and is ready, because this code is executed after the edit template is fully rendered. It will be reexecuted anytime edit is rerendered. Any other client-side library is included and initialized similarly.

EDIT Image files referenced via .css are a pain in Meteor. The "proper" way to deal with them is to put them all under the folder public, in this case for example public/ckeditor. Then edit the CKEditor .css files so that all references to image URLs point to your new folder at the root, i.e. /ckeditor/image1.png etc. (leave out "public").

like image 24
Geoffrey Booth Avatar answered Nov 14 '22 07:11

Geoffrey Booth