Как исправить скрипт на JS?

308
25 февраля 2017, 06:58

Есть скрипт помогающий оформить код при редактировании https://jsfiddle.net/L5qhzdh7/

Вопроса 2:

1) Если поставить курсор в конец набранного текста и нажать "цитата" или "код" то теги появятся всё равно в самом начале поля, в первой строке. Что нужно поменять что бы теги вставлялись там где стоит курсор?

2) Функции CodeSelection() и QuoteSelection() абсолютно идентичны - только теги разные вставляются, можно ведь сократить это код... Как это правильно сделать и какое событие ловить для общей вынесенной части функции, все мои попытки привели к неработоспособности.

Code.onclick = function ShowSelection()
{
  var textComponent = document.getElementById('Editor');
  var selectedText;
  // IE version
  if (document.selection != undefined)
  {
    textComponent.focus();
    var sel = document.selection.createRange();
    selectedText = sel.text;
  }
  // Mozilla version
  else if (textComponent.selectionStart != undefined)
  {
    var startPos = textComponent.selectionStart;
    var endPos = textComponent.selectionEnd;
    selectedText = textComponent.value.substring(startPos, endPos)
  }
  var textareaValue = document.getElementById('Editor').value;
  var textareaValueNew = textareaValue.replace(selectedText,'<code>'+selectedText+'</code>');
  document.getElementById('Editor').value = textareaValueNew;
}

Img.onclick = function ImageSelection()
{
  var textComponent = document.getElementById('Editor');
  var selectedText;
  // IE version
  if (document.selection != undefined)
  {
    textComponent.focus();
    var sel = document.selection.createRange();
    selectedText = sel.text;
  }
  // Mozilla version
  else if (textComponent.selectionStart != undefined)
  {
    var startPos = textComponent.selectionStart;
    var endPos = textComponent.selectionEnd;
    selectedText = textComponent.value.substring(startPos, endPos)
  }
  if(selectedText === ''){
      var ImgURL = prompt("Введите адрес картинки", '');
      if(ImgURL !== null){
          var textareaValue = document.getElementById('Editor').value;
          var textareaValueNew = textareaValue.replace(selectedText,'<img src="'+ImgURL+'">');
          document.getElementById('Editor').value = textareaValueNew;
      }
  }
}

Quote.onclick = function ImageSelection()
{
  var textComponent = document.getElementById('Editor');
  var selectedText;
  // IE version
  if (document.selection != undefined)
  {
    textComponent.focus();
    var sel = document.selection.createRange();
    selectedText = sel.text;
  }
  // Mozilla version
  else if (textComponent.selectionStart != undefined)
  {
    var startPos = textComponent.selectionStart;
    var endPos = textComponent.selectionEnd;
    selectedText = textComponent.value.substring(startPos, endPos)
  }
  var textareaValue = document.getElementById('Editor').value;
  var textareaValueNew = textareaValue.replace(selectedText,'<q>'+selectedText+'</q>');
  document.getElementById('Editor').value = textareaValueNew;
}
READ ALSO
Плагин jQuery Waypoints не работает

Плагин jQuery Waypoints не работает

Скачала плагин jQuery WaypointsНе понимаю, как подключить

590
Js, как получить this класса в методе без bind?

Js, как получить this класса в методе без bind?

Как получить this класса, внутри метода класса, не прибегая к bind? Тк в этом методе нужен свой this

246
Копирование в буфер обмена на android js

Копирование в буфер обмена на android js

Как скопировать в буфер обмена текст (на Android) при помощи JS?

291
Angular 2 и MS SQL Server

Angular 2 и MS SQL Server

Доброго времени суток Делаю в Angular 2 первые шаги, всплыла задача чтения данных из базы под управлением MS SQL server 2008Нагуглил две библиотеки - mssql и tedious,...

426