Add link button to text editor
This commit is contained in:
parent
7283bfaef6
commit
2bf0f99b51
|
|
@ -218,6 +218,7 @@ class FormHelper extends Base
|
|||
'tabindex' => isset($attributes['tabindex']) ? $attributes['tabindex'] : '-1',
|
||||
'labelPreview' => t('Preview'),
|
||||
'labelWrite' => t('Write'),
|
||||
'labelTitle' => t('Title'),
|
||||
'placeholder' => t('Write your text in Markdown'),
|
||||
'autofocus' => isset($attributes['autofocus']) && $attributes['autofocus'],
|
||||
'suggestOptions' => array(
|
||||
|
|
|
|||
File diff suppressed because one or more lines are too long
|
|
@ -44,6 +44,7 @@ KB.component('text-editor', function (containerElement, options) {
|
|||
{href: '#', html: '<i class="fa fa-bold fa-fw"></i>', click: function() { insertEnclosedTag('**'); }},
|
||||
{href: '#', html: '<i class="fa fa-italic fa-fw"></i>', click: function() { insertEnclosedTag('_'); }},
|
||||
{href: '#', html: '<i class="fa fa-strikethrough fa-fw"></i>', click: function() { insertEnclosedTag('~~'); }},
|
||||
{href: '#', html: '<i class="fa fa-link fa-fw"></i>', click: function() { insertLinkTag(); }},
|
||||
{href: '#', html: '<i class="fa fa-quote-right fa-fw"></i>', click: function() { insertPrependTag('> '); }},
|
||||
{href: '#', html: '<i class="fa fa-list-ul fa-fw"></i>', click: function() { insertPrependTag('* '); }},
|
||||
{href: '#', html: '<i class="fa fa-code fa-fw"></i>', click: function() { insertBlockTag('```'); }}
|
||||
|
|
@ -130,6 +131,30 @@ KB.component('text-editor', function (containerElement, options) {
|
|||
setCursorBeforeClosingTag(tag, 1);
|
||||
}
|
||||
|
||||
function insertLinkTag() {
|
||||
var selectedText = getSelectedText();
|
||||
var linkLabel = options.labelTitle;
|
||||
var linkUrl = 'http://...';
|
||||
var selectionStartOffset = 0;
|
||||
var selectionEndOffset = 0;
|
||||
|
||||
if (selectedText.startsWith('http')) {
|
||||
linkUrl = selectedText;
|
||||
selectionStartOffset = -1 * (linkUrl.length + 3 + linkLabel.length);
|
||||
selectionEndOffset = selectionStartOffset + linkLabel.length;
|
||||
} else if (selectedText.length > 0) {
|
||||
linkLabel = selectedText;
|
||||
selectionStartOffset = -1 * (linkUrl.length + 1);
|
||||
selectionEndOffset = selectionStartOffset + linkUrl.length;
|
||||
}
|
||||
insertText('[' + linkLabel + '](' + linkUrl + ')');
|
||||
|
||||
var selectionPosition = KB.utils.getSelectionPosition(textarea);
|
||||
var currentSelectionStart = selectionPosition.selectionStart;
|
||||
|
||||
textarea.setSelectionRange(currentSelectionStart + selectionStartOffset, currentSelectionStart + selectionEndOffset);
|
||||
}
|
||||
|
||||
function insertText(replacedText) {
|
||||
textarea.focus();
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue