diff --git a/editors/js/markitup_markdown.js b/editors/js/markitup_markdown.js index e69de29..68cabff 100644 --- a/editors/js/markitup_markdown.js +++ b/editors/js/markitup_markdown.js @@ -0,0 +1,95 @@ +(function($) { + +/** + * Attach this editor to a target element. + */ +Drupal.wysiwyg.editor.attach.markitup_markdown = function(context, params, settings) { + + settings.markupSet.h1 = { + name: 'Heading 1', + key: '1', + className: 'markitup-h1', + placeHolder: 'Your title here...', + closeWith: function(markItUp) { return miu.markdownTitle(markItUp, '=') } + }; + settings.markupSet.h2 = { + name: 'Heading 2', + key: '2', + className: 'markitup-h2', + placeHolder: 'Your title here...', + closeWith: function(markItUp) { return miu.markdownTitle(markItUp, '-') } + }; + settings.markupSet.numeric = { + name: 'Numeric List', + className: 'markitup-numeric', + placeHolder: 'Your title here...', + openWith: function(markItUp) { return markItUp.line+'. '; } + }; + /* + mySettings = { + previewParserPath: '', + nameSpace: settings.nameSpace, + root: settings.root, + onShiftEnter: {keepDefault:false, openWith:'\n\n'}, + markupSet: [ + {name:'Heading 1', key:'1', className:'markitup-h1', placeHolder:'Your title here...', closeWith:function(markItUp) { return miu.markdownTitle(markItUp, '=') } }, + {name:'Heading 2', key:'2', className:'markitup-h2', placeHolder:'Your title here...', closeWith:function(markItUp) { return miu.markdownTitle(markItUp, '-') } }, + {name:'Heading 3', key:'3', className:'markitup-h3', openWith:'### ', placeHolder:'Your title here...' }, + {name:'Heading 4', key:'4', className:'markitup-h4', openWith:'#### ', placeHolder:'Your title here...' }, + {name:'Heading 5', key:'5', className:'markitup-h5', openWith:'##### ', placeHolder:'Your title here...' }, + {name:'Heading 6', key:'6', className:'markitup-h6', openWith:'###### ', placeHolder:'Your title here...' }, + {separator:'---------------' }, + {name:'Bold', key:'B', className:'markitup-bold', openWith:'**', closeWith:'**'}, + {name:'Italic', key:'I', className:'markitup-italic', openWith:'_', closeWith:'_'}, + {separator:'---------------' }, + {name:'Bulleted List', className:'markitup-bullet', openWith:'- ' }, + {name:'Numeric List', className:'markitup-numeric', openWith:function(markItUp) { + return markItUp.line+'. '; + }}, + {separator:'---------------' }, + {name:'Link', key:'L', className:'markitup-link', openWith:'[', closeWith:']([![Url:!:http://]!] "[![Title]!]")', placeHolder:'Your text to link here...' }, + {separator:'---------------'}, + {name:'Quotes', className:'markitup-quote', openWith:'> '}, + {name:'Code Block / Code', className:'markitup-code', openWith:'(!(\t|!|`)!)', closeWith:'(!(`)!)'}, + {separator:'---------------'}, + ] + } + $('#' + params.field, context).markItUp(mySettings); + */ + $('#' + params.field, context).markItUp(settings); + + // Adjust CSS for editor buttons. + $.each(settings.markupSet, function (button) { + $('.' + settings.nameSpace + ' .' + this.className + ' a') + .css({ backgroundImage: 'url(' + settings.root + 'sets/default/images/' + button + '.png' + ')' }) + .parents('li').css({ backgroundImage: 'none' }); + }); +}; + +/** + * Detach a single or all editors. + * + * See Drupal.wysiwyg.editor.detach.none() for a full desciption of this hook. + */ +Drupal.wysiwyg.editor.detach.markitup_markdown = function(context, params) { + if (typeof params != 'undefined') { + $('#' + params.field, context).markItUpRemove(); + } + else { + $('.markItUpEditor', context).markItUpRemove(); + } +}; + +//mIu nameSpace to avoid conflict. +miu = { + markdownTitle: function(markItUp, char) { + heading = ''; + n = $.trim(markItUp.selection||markItUp.placeHolder).length; + for(i = 0; i < n; i++) { + heading += char; + } + return '\n'+heading; + } +} + +})(jQuery); diff --git a/editors/markitup_markdown.inc b/editors/markitup_markdown.inc index e69de29..360fced 100644 --- a/editors/markitup_markdown.inc +++ b/editors/markitup_markdown.inc @@ -0,0 +1,179 @@ + 'markItUp (markdown)', + 'vendor url' => 'http://markitup.jaysalvat.com', + 'download url' => 'http://markitup.jaysalvat.com/downloads', + 'library path' => wysiwyg_get_path('markitup'), + 'libraries' => array( + '' => array( + 'title' => 'Source', + 'files' => array('markitup/jquery.markitup.js'), + ), + 'pack' => array( + 'title' => 'Packed', + 'files' => array('markitup/jquery.markitup.pack.js'), + ), + ), + 'version callback' => 'wysiwyg_markitup_markdown_version', + 'themes callback' => 'wysiwyg_markitup_markdown_themes', + 'settings callback' => 'wysiwyg_markitup_markdown_settings', + 'plugin callback' => 'wysiwyg_markitup_markdown_plugins', + 'versions' => array( + '1.1.5' => array( + 'js files' => array('markitup_markdown.js'), + ), + ), + 'css files' => array('markitup_markdown-1.css'), + ); + return $editor; +} + +/** + * Detect editor version. + * + * @param $editor + * An array containing editor properties as returned from hook_editor(). + * + * @return + * The installed editor version. + */ +function wysiwyg_markitup_markdown_version($editor) { + // Changelog was in markitup/markitup/readme.txt <= 1.1.5. + $changelog = $editor['library path'] . '/markitup/readme.txt'; + if (!file_exists($changelog)) { + // Changelog was moved up to markitup/CHANGELOG.md after 1.1.5. + $changelog = $editor['library path'] . '/CHANGELOG.md'; + if (!file_exists($changelog)) { + return; + } + } + $changelog = fopen($changelog, 'r'); + $line = fgets($changelog); + if (preg_match('@([0-9\.]+)@', $line, $version)) { + fclose($changelog); + return $version[1]; + } + fclose($changelog); +} + +/** + * Return runtime editor settings for a given wysiwyg profile. + * + * @param $editor + * A processed hook_editor() array of editor properties. + * @param $config + * An array containing wysiwyg editor profile settings. + * @param $theme + * The name of a theme/GUI/skin to use. + * + * @return + * A settings array to be populated in + * Drupal.settings.wysiwyg.configs.{editor} + */ +function wysiwyg_markitup_markdown_settings($editor, $config, $theme) { + // Whoever is guilty for adding this horrible CSS-file-without-filepath + // override "feature" to Drupal core... stand in the corner! + drupal_add_css($editor['library path'] . '/markitup/skins/' . $theme . '/style.css', array( + // Specify an alternate basename; otherwise, style.css would override a + // commonly used style.css file of the theme. + 'basename' => 'markitup.' . $theme . '.style.css', + 'group' => CSS_THEME, + )); + + $settings = array( + 'root' => base_path() . $editor['library path'] . '/markitup/', + 'nameSpace' => $theme, + 'markupSet' => array(), + ); + + // Add configured buttons or all available. + $default_buttons = array( + 'h1' => array('name' => t('First Level Heading'), 'placeHolder' => 'Your title here...', 'className' => 'markitup-h1', 'key' => '1', 'closeWith' => 'function(markItUp) { return miu.markdownTitle(markItUp, "=") }'), + 'h2' => array('name' => t('Second Level Heading'), 'placeHolder' => 'Your title here...', 'className' => 'markitup-h2', 'key' => '2', 'closeWith' => 'function(markItUp) { return miu.markdownTitle(markItUp, "-") }'), + 'h3' => array('name' => t('Heading 3'), 'placeHolder' => 'Your title here...', 'className' => 'markitup-h3', 'key' => '3', 'openWith' => '### '), + 'h4' => array('name' => t('Heading 4'), 'placeHolder' => 'Your title here...', 'className' => 'markitup-h4', 'key' => '4', 'openWith' => '#### '), + 'h5' => array('name' => t('Heading 5'), 'placeHolder' => 'Your title here...', 'className' => 'markitup-h5', 'key' => '5', 'openWith' => '##### '), + 'h6' => array('name' => t('Heading 6'), 'placeHolder' => 'Your title here...', 'className' => 'markitup-h6', 'key' => '6', 'openWith' => '###### '), + 'sep1' => array('separator' => '---------------'), + 'bold' => array('name' => t('Bold'), 'className' => 'markitup-bold', 'key' => 'B', 'openWith' => '**', 'closeWith' => '**'), + 'italic' => array('name' => t('Italic'), 'className' => 'markitup-italic', 'key' => 'I', 'openWith' => '_', 'closeWith' => '_'), + 'sep2' => array('separator' => '---------------'), + 'link' => array('name' => t('Link'), 'className' => 'markitup-link', 'key' => 'L', 'openWith' => '[', 'closeWith' => ']([![Url:!:http://]!] "[![Title]!]")', 'placeHolder' => 'Your text to link here...'), + 'bullet' => array('name' => t('Bulleted List'), 'className' => 'markitup-bullet', 'openWith' => '- '), + 'numeric' => array('name' => t('Numeric List'), 'className' => 'markitup-numeric', 'openWith' => 'function(1 + 1)'), + 'sep3' => array('separator' => '---------------'), + 'quote' => array('name' => t('Quotes'), 'className' => 'markitup-quote', 'openWith' => '> '), + 'code' => array('name' => t('Code Block / Code'), 'className' => 'markitup-code', 'openWith' => '(!( |!|`)!)', 'closeWith' => '(!(`)!)'), + // @todo + // 'cleanup' => array('name' => t('Clean-up'), 'className' => 'markitup-cleanup', 'replaceWith' => 'function(markitup) { return markitup.selection.replace(/<(.*?)>/g, "") }'), + 'preview' => array('name' => t('Preview'), 'className' => 'markitup-preview', 'call' => 'preview'), + ); + + if (!empty($config['buttons'])) { + foreach ($config['buttons'] as $plugin) { + foreach ($plugin as $button => $enabled) { + if (isset($default_buttons[$button])) { + $settings['markupSet'][$button] = $default_buttons[$button]; + } + } + } + } + else { + $settings['markupSet'] = $default_buttons; + } + + return $settings; +} + +/** + * Determine available editor themes or check/reset a given one. + * + * @param $editor + * A processed hook_editor() array of editor properties. + * @param $profile + * A wysiwyg editor profile. + * + * @return + * An array of theme names. The first returned name should be the default + * theme name. + */ +function wysiwyg_markitup_markdown_themes($editor, $profile) { + return array('simple', 'markitup'); +} + +/** + * Return internal plugins for this editor; semi-implementation of hook_wysiwyg_plugin(). + */ +function wysiwyg_markitup_markdown_plugins($editor) { + return array( + 'default' => array( + 'buttons' => array( + 'h1' => t('First Level Heading'), + 'h2' => t('Second Level Heading'), + 'h3' => t('Heading 3'), + 'h4' => t('Heading 4'), + 'h5' => t('Heading 5'), + 'h6' => t('Heading 6'), + 'bold' => t('Bold'), 'italic' => t('Italic'), + 'link' => t('Link'), + // 'cleanup' => t('Clean-up'), + 'preview' => t('Preview'), + ), + 'internal' => TRUE, + ), + ); +} +