Add link button to text editor

This commit is contained in:
Rafael de Camargo 2019-07-24 01:04:55 -03:00 committed by Frédéric Guillot
parent 7283bfaef6
commit 2bf0f99b51
3 changed files with 27 additions and 1 deletions

View File

@ -218,6 +218,7 @@ class FormHelper extends Base
'tabindex' => isset($attributes['tabindex']) ? $attributes['tabindex'] : '-1', 'tabindex' => isset($attributes['tabindex']) ? $attributes['tabindex'] : '-1',
'labelPreview' => t('Preview'), 'labelPreview' => t('Preview'),
'labelWrite' => t('Write'), 'labelWrite' => t('Write'),
'labelTitle' => t('Title'),
'placeholder' => t('Write your text in Markdown'), 'placeholder' => t('Write your text in Markdown'),
'autofocus' => isset($attributes['autofocus']) && $attributes['autofocus'], 'autofocus' => isset($attributes['autofocus']) && $attributes['autofocus'],
'suggestOptions' => array( 'suggestOptions' => array(

File diff suppressed because one or more lines are too long

View File

@ -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-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-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-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-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-list-ul fa-fw"></i>', click: function() { insertPrependTag('* '); }},
{href: '#', html: '<i class="fa fa-code fa-fw"></i>', click: function() { insertBlockTag('```'); }} {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); 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) { function insertText(replacedText) {
textarea.focus(); textarea.focus();