diff --git includes/insert.inc includes/insert.inc index 52f101e..df6aa5c 100644 --- includes/insert.inc +++ includes/insert.inc @@ -13,6 +13,7 @@ function insert_insert_styles() { $insert_styles = array(); $insert_styles['auto'] = array('label' => t('Automatic'), 'weight' => -20); $insert_styles['link'] = array('label' => t('Link to file'), 'weight' => -11); + $insert_styles['link_selection'] = array('label' => t('Selection as link'), 'weight' => -11); $insert_styles['image'] = array('label' => t('Original image'), 'weight' => -10); return $insert_styles; } @@ -34,7 +35,12 @@ function insert_insert_content($item, $style, $widget) { return theme('insert_image', $item, $widget); } else { - return theme('insert_link', $item, $widget); + if ($style_name == 'link_selection') { + return theme('insert_link_selection', $item, $widget); + } + else { + return theme('insert_link', $item, $widget); + } } } @@ -57,3 +63,11 @@ function template_preprocess_insert_link(&$vars) { $vars['class'] = !empty($vars['widget']['insert_class']) ? $vars['widget']['insert_class'] : ''; $vars['name'] = $vars['item']['filename']; } + +/** + * Preprocess variables for the insert-link-selection.tpl.php file. + */ +function template_preprocess_insert_link_selection(&$vars) { + $vars['url'] = insert_create_url($vars['item']['filepath']); + $vars['class'] = !empty($vars['widget']['insert_class']) ? $vars['widget']['insert_class'] : ''; +} diff --git insert.js insert.js index 6f1c911..914af54 100644 --- insert.js +++ insert.js @@ -64,7 +64,7 @@ Drupal.behaviors.insert = function(context) { } // Insert the text. - Drupal.insert.insertIntoActiveEditor(content); + Drupal.insert.insertIntoActiveEditor(content, style); } }; @@ -76,20 +76,24 @@ Drupal.insert = { * * @param content */ - insertIntoActiveEditor: function(content) { + insertIntoActiveEditor: function(content, style) { // Always work in normal text areas that currently have focus. if (insertTextarea && insertTextarea.insertHasFocus) { - Drupal.insert.insertAtCursor(insertTextarea, content); + Drupal.insert.insertAtCursor(insertTextarea, content, style); } // Direct tinyMCE support. else if (typeof(tinyMCE) != 'undefined' && tinyMCE.activeEditor) { Drupal.insert.activateTabPane(document.getElementById(tinyMCE.activeEditor.editorId)); + // Style insert_linkselection uses the current selection as link text, so we need to replace it + if (style == 'link_selection') { + content = Drupal.insert._replaceSelection (tinyMCE.activeEditor.selection.getContent(), content); + } tinyMCE.activeEditor.execCommand('mceInsertContent', false, content); } // WYSIWYG support, should work in all editors if available. else if (Drupal.wysiwyg && Drupal.wysiwyg.activeId) { Drupal.insert.activateTabPane(document.getElementById(Drupal.wysiwyg.activeId)); - Drupal.wysiwyg.instances[Drupal.wysiwyg.activeId].insert(content) + Drupal.wysiwyg.instances[Drupal.wysiwyg.activeId].insert(content); } // FCKeditor module support. else if (typeof(FCKeditorAPI) != 'undefined' && typeof(fckActiveId) != 'undefined') { @@ -150,7 +154,7 @@ Drupal.insert = { * @param content * The string to be inserted. */ - insertAtCursor: function(editor, content) { + insertAtCursor: function(editor, content, style) { // Record the current scroll position. var scroll = editor.scrollTop; @@ -158,6 +162,9 @@ Drupal.insert = { if (document.selection) { editor.focus(); sel = document.selection.createRange(); + if (style == 'link_selection') { + content = Drupal.insert._replaceSelection (sel, content); + } sel.text = content; } @@ -165,6 +172,9 @@ Drupal.insert = { else if (editor.selectionStart || editor.selectionStart == '0') { var startPos = editor.selectionStart; var endPos = editor.selectionEnd; + if (style == 'link_selection') { + content = Drupal.insert._replaceSelection (editor.value.substring(startPos, endPos), content); + } editor.value = editor.value.substring(0, startPos) + content + editor.value.substring(endPos, editor.value.length); } @@ -175,6 +185,23 @@ Drupal.insert = { // Ensure the textarea does not unexpectedly scroll. editor.scrollTop = scroll; + }, + + /** + * Use current selection as link text. + * + * @param selection + * The current selection from the editor. + * @param content + * The string to be inserted. + */ + _replaceSelection: function(selection, content) { + if (selection.length) { + return content.replace('$name', selection); + } + else{ + return content.replace('$name', Drupal.t('Link')); + } } }; diff --git insert.module insert.module index 9410521..2c18b47 100644 --- insert.module +++ insert.module @@ -66,6 +66,11 @@ function insert_theme() { 'template' => 'templates/insert-link', 'file' => 'includes/insert.inc', ), + 'insert_link_selection' => array( + 'arguments' => array('item' => NULL, 'widget' => NULL), + 'template' => 'templates/insert-link-selection', + 'file' => 'includes/insert.inc', + ), // Theme functions in includes/imagecache.inc. 'imagecache_insert_image' => array( diff --git templates/insert-link-selection.tpl.php templates/insert-link-selection.tpl.php new file mode 100644 index 0000000..7d05efc --- /dev/null +++ templates/insert-link-selection.tpl.php @@ -0,0 +1,22 @@ + + title="__description__"> \ No newline at end of file