// $Id: ckeditor-3.0.js,v 1.2 2009/09/26 05:37:57 sun Exp $ (function($) { Drupal.wysiwyg.editor.init.ckeditor = function(settings) { // Initialize editor configurations. CKEDITOR.on('focus', function(ev) { Drupal.wysiwyg.activeId = ev.editor.name; }); // Plugins must only be loaded once. Only the settings from the first format // will be used but they're identical anyway. var registeredPlugins = {}; for (var format in settings) { if (Drupal.settings.wysiwyg.plugins[format]) { // Load native external plugins. // Array syntax required; 'native' is a predefined token in JavaScript. for (var pluginName in Drupal.settings.wysiwyg.plugins[format]['native']) { if (!registeredPlugins[pluginName]) { var plugin = Drupal.settings.wysiwyg.plugins[format]['native'][pluginName]; CKEDITOR.plugins.addExternal(pluginName, plugin.path, plugin.fileName); registeredPlugins[pluginName] = true; } } // Load Drupal plugins. for (var pluginName in Drupal.settings.wysiwyg.plugins[format].drupal) { if (!registeredPlugins[pluginName]) { Drupal.wysiwyg.editor.instance.ckeditor.addPlugin(pluginName, Drupal.settings.wysiwyg.plugins[format].drupal[pluginName], Drupal.settings.wysiwyg.plugins.drupal[pluginName]); registeredPlugins[pluginName] = true; } } } } }; /** * Attach this editor to a target element. */ Drupal.wysiwyg.editor.attach.ckeditor = function(context, params, settings) { // Apply editor instance settings. CKEDITOR.config.customConfig = ''; settings.on = { // Event handlers. instanceReady: function(ev) { var editor = ev.editor; var dtd = CKEDITOR.dtd; var tags = CKEDITOR.tools.extend( {}, dtd.$block, dtd.$listItem, dtd.$tableContent ); if (settings.apply_source_formatting) { // Mimic FCKeditor output. for (var tag in tags) { if (tag == 'pre') { continue; } this.dataProcessor.writer.setRules(tag, { indent: true, breakBeforeOpen: true, breakAfterOpen: false, breakBeforeClose: false, breakAfterClose: true }); } } else { // No indents or linebreaks; for (var key in tags) { if (tag == 'pre') { continue; } this.dataProcessor.writer.setRules(tag, { indent: false, breakBeforeOpen: false, breakAfterOpen: false, breakBeforeClose: false, breakAfterClose: false }); } } }, pluginsLoaded: function(ev) { // Override the conversion methods to let Drupal plugins modify the data. var editor = ev.editor; if (editor.dataProcessor) { editor.dataProcessor.toHtml = CKEDITOR.tools.override(editor.dataProcessor.toHtml, function(originalToHtml) { // Convert raw data for display in WYSIWYG mode. return function(data, fixForBody) { for (var plugin in Drupal.settings.wysiwyg.plugins[params.format].drupal) { if (typeof Drupal.wysiwyg.plugins[plugin].attach == 'function') { data = Drupal.wysiwyg.plugins[plugin].attach(data, Drupal.settings.wysiwyg.plugins.drupal[plugin], editor.name); data = Drupal.wysiwyg.instances[params.field].prepareContent(data); } } return originalToHtml.call(this, data, fixForBody); }; }); editor.dataProcessor.toDataFormat = CKEDITOR.tools.override(editor.dataProcessor.toDataFormat, function(originalToDataFormat) { // Convert WYSIWYG mode content to raw data. return function(data, fixForBody) { for (var plugin in Drupal.settings.wysiwyg.plugins[params.format].drupal) { if (typeof Drupal.wysiwyg.plugins[plugin].detach == 'function') { data = Drupal.wysiwyg.plugins[plugin].detach(data, Drupal.settings.wysiwyg.plugins.drupal[plugin], editor.name); } } return originalToDataFormat.call(this, data, fixForBody); }; }); } } }; // Attach editor. CKEDITOR.replace(params.field, settings); }; /** * Detach a single or all editors. * * @todo 3.x: editor.prototype.getInstances() should always return an array * containing all instances or the passed in params.field instance, but * always return an array to simplify all detach functions. */ Drupal.wysiwyg.editor.detach.ckeditor = function(context, params) { if (typeof params != 'undefined') { var instance = CKEDITOR.instances[params.field]; if (instance) { instance.destroy(); } } else { for (var instanceName in CKEDITOR.instances) { CKEDITOR.instances[instanceName].destroy(); } } }; Drupal.wysiwyg.editor.instance.ckeditor = { addPlugin: function(pluginName, settings, pluginSettings) { CKEDITOR.plugins.add(pluginName, { // Wrap Drupal plugin in a proxy pluygin. init: function(editor) { if (settings.css) { editor.on('mode', function(ev) { if (ev.editor.mode == 'wysiwyg') { // Inject CSS files directly into the editing area head tag. $('head', $('#cke_contents_' + ev.editor.name + ' iframe').eq(0).contents()).append(''); } }); } if (typeof Drupal.wysiwyg.plugins[pluginName].invoke == 'function') { var pluginCommand = { exec: function(editor) { var data = { format: 'html', node: editor.getSelection().getSelectedElement() }; // @todo This is NOT the same as data.node. data.content = data.node ? data.node.innerHTML : ''; Drupal.wysiwyg.plugins[pluginName].invoke(data, pluginSettings, editor.name); } }; editor.addCommand(pluginName, pluginCommand); } editor.ui.addButton(pluginName, { label: settings.iconTitle, command: pluginName, icon: settings.icon }); // @todo Add button state handling. } }); }, prepareContent: function(content) { // @todo Don't know if we need this yet. return content; }, insert: function(content) { CKEDITOR.instances[this.field].insertHtml(content); } }; })(jQuery);