debian-mirror-gitlab/app/assets/javascripts/lib/utils/text_markdown.js

137 lines
4.3 KiB
JavaScript
Raw Normal View History

2018-11-08 19:23:39 +05:30
/* eslint-disable func-names, no-var, no-param-reassign, quotes, one-var, one-var-declaration-per-line, operator-assignment, no-else-return, prefer-template, prefer-arrow-callback, max-len, consistent-return, no-unused-vars, max-len */
2018-05-09 12:01:36 +05:30
import $ from 'jquery';
import { insertText } from '~/lib/utils/common_utils';
2018-03-17 18:26:18 +05:30
2018-05-09 12:01:36 +05:30
function selectedText(text, textarea) {
2018-03-17 18:26:18 +05:30
return text.substring(textarea.selectionStart, textarea.selectionEnd);
2018-05-09 12:01:36 +05:30
}
2018-03-17 18:26:18 +05:30
2018-05-09 12:01:36 +05:30
function lineBefore(text, textarea) {
2018-03-17 18:26:18 +05:30
var split;
split = text.substring(0, textarea.selectionStart).trim().split('\n');
return split[split.length - 1];
2018-05-09 12:01:36 +05:30
}
2018-03-17 18:26:18 +05:30
2018-05-09 12:01:36 +05:30
function lineAfter(text, textarea) {
2018-03-17 18:26:18 +05:30
return text.substring(textarea.selectionEnd).trim().split('\n')[0];
2018-05-09 12:01:36 +05:30
}
2018-03-17 18:26:18 +05:30
2018-05-09 12:01:36 +05:30
function blockTagText(text, textArea, blockTag, selected) {
const before = lineBefore(text, textArea);
const after = lineAfter(text, textArea);
if (before === blockTag && after === blockTag) {
2018-03-17 18:26:18 +05:30
// To remove the block tag we have to select the line before & after
if (blockTag != null) {
textArea.selectionStart = textArea.selectionStart - (blockTag.length + 1);
textArea.selectionEnd = textArea.selectionEnd + (blockTag.length + 1);
}
return selected;
} else {
return blockTag + "\n" + selected + "\n" + blockTag;
}
2018-05-09 12:01:36 +05:30
}
function moveCursor(textArea, tag, wrapped, removedLastNewLine) {
var pos;
if (!textArea.setSelectionRange) {
return;
}
if (textArea.selectionStart === textArea.selectionEnd) {
if (wrapped) {
pos = textArea.selectionStart - tag.length;
} else {
pos = textArea.selectionStart;
}
if (removedLastNewLine) {
pos -= 1;
}
return textArea.setSelectionRange(pos, pos);
}
}
2018-03-17 18:26:18 +05:30
2018-05-09 12:01:36 +05:30
export function insertMarkdownText(textArea, text, tag, blockTag, selected, wrap) {
var textToInsert, inserted, selectedSplit, startChar, removedLastNewLine, removedFirstNewLine, currentLineEmpty, lastNewLine;
2018-03-17 18:26:18 +05:30
removedLastNewLine = false;
removedFirstNewLine = false;
currentLineEmpty = false;
// Remove the first newline
if (selected.indexOf('\n') === 0) {
removedFirstNewLine = true;
selected = selected.replace(/\n+/, '');
}
// Remove the last newline
if (textArea.selectionEnd - textArea.selectionStart > selected.replace(/\n$/, '').length) {
removedLastNewLine = true;
selected = selected.replace(/\n$/, '');
}
selectedSplit = selected.split('\n');
if (!wrap) {
lastNewLine = textArea.value.substr(0, textArea.selectionStart).lastIndexOf('\n');
// Check whether the current line is empty or consists only of spaces(=handle as empty)
if (/^\s*$/.test(textArea.value.substring(lastNewLine, textArea.selectionStart))) {
currentLineEmpty = true;
}
}
startChar = !wrap && !currentLineEmpty && textArea.selectionStart > 0 ? '\n' : '';
if (selectedSplit.length > 1 && (!wrap || (blockTag != null && blockTag !== ''))) {
if (blockTag != null && blockTag !== '') {
2018-05-09 12:01:36 +05:30
textToInsert = blockTagText(text, textArea, blockTag, selected);
2018-03-17 18:26:18 +05:30
} else {
2018-05-09 12:01:36 +05:30
textToInsert = selectedSplit.map(function(val) {
2018-03-17 18:26:18 +05:30
if (val.indexOf(tag) === 0) {
return "" + (val.replace(tag, ''));
} else {
return "" + tag + val;
}
}).join('\n');
}
} else {
2018-05-09 12:01:36 +05:30
textToInsert = "" + startChar + tag + selected + (wrap ? tag : ' ');
2018-03-17 18:26:18 +05:30
}
if (removedFirstNewLine) {
2018-05-09 12:01:36 +05:30
textToInsert = '\n' + textToInsert;
2018-03-17 18:26:18 +05:30
}
if (removedLastNewLine) {
2018-05-09 12:01:36 +05:30
textToInsert += '\n';
2018-03-17 18:26:18 +05:30
}
2018-05-09 12:01:36 +05:30
insertText(textArea, textToInsert);
return moveCursor(textArea, tag, wrap, removedLastNewLine);
}
2018-03-17 18:26:18 +05:30
2018-05-09 12:01:36 +05:30
function updateText(textArea, tag, blockTag, wrap) {
2018-03-17 18:26:18 +05:30
var $textArea, selected, text;
$textArea = $(textArea);
textArea = $textArea.get(0);
text = $textArea.val();
2018-05-09 12:01:36 +05:30
selected = selectedText(text, textArea);
2018-03-17 18:26:18 +05:30
$textArea.focus();
2018-05-09 12:01:36 +05:30
return insertMarkdownText(textArea, text, tag, blockTag, selected, wrap);
}
2018-03-17 18:26:18 +05:30
2018-05-09 12:01:36 +05:30
function replaceRange(s, start, end, substitute) {
return s.substring(0, start) + substitute + s.substring(end);
}
export function addMarkdownListeners(form) {
2018-03-17 18:26:18 +05:30
return $('.js-md', form).off('click').on('click', function() {
2018-05-09 12:01:36 +05:30
const $this = $(this);
return updateText($this.closest('.md-area').find('textarea'), $this.data('mdTag'), $this.data('mdBlock'), !$this.data('mdPrepend'));
2018-03-17 18:26:18 +05:30
});
2018-05-09 12:01:36 +05:30
}
2018-03-17 18:26:18 +05:30
2018-05-09 12:01:36 +05:30
export function removeMarkdownListeners(form) {
2018-03-17 18:26:18 +05:30
return $('.js-md', form).off('click');
2018-05-09 12:01:36 +05:30
}