summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFilipa Lacerda <filipa@gitlab.com>2017-11-10 17:20:29 +0000
committerFilipa Lacerda <filipa@gitlab.com>2017-11-10 17:20:29 +0000
commit868e024486e084a068013de47fb931b2259ab1a4 (patch)
treef7434a1af989d75c9ac13400dd1108f8150717d7
parent2e065c2bd1d3fbe794a8e3b10a216736909b6c18 (diff)
downloadgitlab-ce-text-utils.tar.gz
Fix broken specstext-utils
-rw-r--r--app/assets/javascripts/abuse_reports.js4
-rw-r--r--app/assets/javascripts/gl_form.js6
-rw-r--r--app/assets/javascripts/lib/utils/text_markdown.js165
-rw-r--r--app/assets/javascripts/lib/utils/text_utility.js10
-rw-r--r--app/assets/javascripts/main.js1
-rw-r--r--spec/javascripts/lib/utils/text_markdown_spec.js12
6 files changed, 104 insertions, 94 deletions
diff --git a/app/assets/javascripts/abuse_reports.js b/app/assets/javascripts/abuse_reports.js
index 3de192d56eb..d2d3a257c0d 100644
--- a/app/assets/javascripts/abuse_reports.js
+++ b/app/assets/javascripts/abuse_reports.js
@@ -1,3 +1,5 @@
+import { truncate } from './lib/utils/text_utility';
+
const MAX_MESSAGE_LENGTH = 500;
const MESSAGE_CELL_SELECTOR = '.abuse-reports .message';
@@ -15,7 +17,7 @@ export default class AbuseReports {
if (reportMessage.length > MAX_MESSAGE_LENGTH) {
$messageCellElement.data('original-message', reportMessage);
$messageCellElement.data('message-truncated', 'true');
- $messageCellElement.text(window.gl.text.truncate(reportMessage, MAX_MESSAGE_LENGTH));
+ $messageCellElement.text(truncate(reportMessage, MAX_MESSAGE_LENGTH));
}
}
diff --git a/app/assets/javascripts/gl_form.js b/app/assets/javascripts/gl_form.js
index 349180f886b..d0f9e6af0f8 100644
--- a/app/assets/javascripts/gl_form.js
+++ b/app/assets/javascripts/gl_form.js
@@ -2,7 +2,7 @@
import GfmAutoComplete from './gfm_auto_complete';
import dropzoneInput from './dropzone_input';
-import { initTextMarkdown, removeMarkdownListeners } from './lib/utils/text_markdown';
+import textUtils from './lib/utils/text_markdown';
export default class GLForm {
constructor(form, enableGFM = false) {
@@ -47,7 +47,7 @@ export default class GLForm {
}
// form and textarea event listeners
this.addEventListeners();
- initTextMarkdown(this.form);
+ textUtils.init(this.form);
// hide discard button
this.form.find('.js-note-discard').hide();
this.form.show();
@@ -86,7 +86,7 @@ export default class GLForm {
clearEventListeners() {
this.textarea.off('focus');
this.textarea.off('blur');
- removeMarkdownListeners(this.form);
+ textUtils.removeListeners(this.form);
}
addEventListeners() {
diff --git a/app/assets/javascripts/lib/utils/text_markdown.js b/app/assets/javascripts/lib/utils/text_markdown.js
index 842b42632b6..2dc9cf0cc29 100644
--- a/app/assets/javascripts/lib/utils/text_markdown.js
+++ b/app/assets/javascripts/lib/utils/text_markdown.js
@@ -1,65 +1,42 @@
+/* eslint-disable import/prefer-default-export, func-names, space-before-function-paren, wrap-iife, no-var, no-param-reassign, no-cond-assign, quotes, one-var, one-var-declaration-per-line, operator-assignment, no-else-return, prefer-template, prefer-arrow-callback, no-empty, max-len, consistent-return, no-unused-vars, no-return-assign, max-len, vars-on-top */
-/* eslint-disable no-var, no-param-reassign, no-cond-assign, one-var, one-var-declaration-per-line, operator-assignment, no-else-return, prefer-template, prefer-arrow-callback, no-empty, max-len, consistent-return, no-return-assign, max-len, vars-on-top */
-export function replaceRange(s, start, end, substitute) {
- return s.substring(0, start) + substitute + s.substring(end);
-}
+const textUtils = {};
-export function selectedText(text, textarea) {
+textUtils.selectedText = function(text, textarea) {
return text.substring(textarea.selectionStart, textarea.selectionEnd);
-}
+};
-function getLineBefore(text, textarea) {
- const split = text.substring(0, textarea.selectionStart).trim().split('\n');
+textUtils.lineBefore = function(text, textarea) {
+ var split;
+ split = text.substring(0, textarea.selectionStart).trim().split('\n');
return split[split.length - 1];
-}
+};
-function getLineAfter(text, textarea) {
+textUtils.lineAfter = function(text, textarea) {
return text.substring(textarea.selectionEnd).trim().split('\n')[0];
-}
-
-function blockTagText(text, textArea, blockTag, selected) {
- const lineBefore = getLineBefore(text, textArea);
- const lineAfter = getLineAfter(text, textArea);
+};
+textUtils.blockTagText = function(text, textArea, blockTag, selected) {
+ var lineAfter, lineBefore;
+ lineBefore = this.lineBefore(text, textArea);
+ lineAfter = this.lineAfter(text, textArea);
if (lineBefore === blockTag && lineAfter === blockTag) {
// To remove the block tag we have to select the line before & after
if (blockTag != null) {
- // eslint-disable-next-line no-param-reassign
textArea.selectionStart = textArea.selectionStart - (blockTag.length + 1);
- // eslint-disable-next-line no-param-reassign
textArea.selectionEnd = textArea.selectionEnd + (blockTag.length + 1);
}
return selected;
+ } else {
+ return blockTag + "\n" + selected + "\n" + blockTag;
}
- return blockTag + '\n' + selected + '\n' + blockTag;
-}
-
-function moveCursor(textArea, tag, wrapped, removedLastNewLine) {
- let 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);
- }
-}
+};
-export function insertTextinTextarea(textArea, text, tag, blockTag, selected, wrap) {
- let removedLastNewLine = false;
- let removedFirstNewLine = false;
- let currentLineEmpty = false;
+textUtils.insertText = function(textArea, text, tag, blockTag, selected, wrap) {
+ var insertText, inserted, selectedSplit, startChar, removedLastNewLine, removedFirstNewLine, currentLineEmpty, lastNewLine;
+ removedLastNewLine = false;
+ removedFirstNewLine = false;
+ currentLineEmpty = false;
// Remove the first newline
if (selected.indexOf('\n') === 0) {
@@ -73,10 +50,10 @@ export function insertTextinTextarea(textArea, text, tag, blockTag, selected, wr
selected = selected.replace(/\n$/, '');
}
- const selectedSplit = selected.split('\n');
+ selectedSplit = selected.split('\n');
if (!wrap) {
- const lastNewLine = textArea.value.substr(0, textArea.selectionStart).lastIndexOf('\n');
+ 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))) {
@@ -84,23 +61,22 @@ export function insertTextinTextarea(textArea, text, tag, blockTag, selected, wr
}
}
- const startChar = !wrap && !currentLineEmpty && textArea.selectionStart > 0 ? '\n' : '';
- let insertText;
+ startChar = !wrap && !currentLineEmpty && textArea.selectionStart > 0 ? '\n' : '';
if (selectedSplit.length > 1 && (!wrap || (blockTag != null && blockTag !== ''))) {
if (blockTag != null && blockTag !== '') {
- insertText = blockTagText(text, textArea, blockTag, selected);
+ insertText = this.blockTagText(text, textArea, blockTag, selected);
} else {
- insertText = selectedSplit.map((val) => {
+ insertText = selectedSplit.map(function(val) {
if (val.indexOf(tag) === 0) {
- return '' + (val.replace(tag, ''));
+ return "" + (val.replace(tag, ''));
} else {
- return '' + tag + val;
+ return "" + tag + val;
}
}).join('\n');
}
} else {
- insertText = '' + startChar + tag + selected + (wrap ? tag : ' ');
+ insertText = "" + startChar + tag + selected + (wrap ? tag : ' ');
}
if (removedFirstNewLine) {
@@ -110,45 +86,68 @@ export function insertTextinTextarea(textArea, text, tag, blockTag, selected, wr
if (removedLastNewLine) {
insertText += '\n';
}
- let inserted;
if (document.queryCommandSupported('insertText')) {
inserted = document.execCommand('insertText', false, insertText);
}
if (!inserted) {
try {
- document.execCommand('ms-beginUndoUnit');
+ document.execCommand("ms-beginUndoUnit");
} catch (error) {}
textArea.value = this.replaceRange(text, textArea.selectionStart, textArea.selectionEnd, insertText);
try {
- document.execCommand('ms-endUndoUnit');
+ document.execCommand("ms-endUndoUnit");
} catch (error) {}
}
- return moveCursor(textArea, tag, wrap, removedLastNewLine);
-}
-
-function updateText(textArea, tag, blockTag, wrap) {
- const $textArea = $(textArea);
- const gettextArea = $textArea.get(0);
- const text = $textArea.val();
- const selected = this.selectedText(text, gettextArea);
- $textArea.focus();
- return insertTextinTextarea(gettextArea, text, tag, blockTag, selected, wrap);
-}
+ return this.moveCursor(textArea, tag, wrap, removedLastNewLine);
+};
+
+textUtils.moveCursor = function(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;
+ }
-export function removeMarkdownListeners(form) {
+ if (removedLastNewLine) {
+ pos -= 1;
+ }
+
+ return textArea.setSelectionRange(pos, pos);
+ }
+};
+
+textUtils.updateText = function(textArea, tag, blockTag, wrap) {
+ var $textArea, selected, text;
+ $textArea = $(textArea);
+ textArea = $textArea.get(0);
+ text = $textArea.val();
+ selected = this.selectedText(text, textArea);
+ $textArea.focus();
+ return this.insertText(textArea, text, tag, blockTag, selected, wrap);
+};
+
+textUtils.init = function(form) {
+ var self;
+ self = this;
+ return $('.js-md', form).off('click').on('click', function() {
+ var $this;
+ $this = $(this);
+ return self.updateText($this.closest('.md-area').find('textarea'), $this.data('md-tag'), $this.data('md-block'), !$this.data('md-prepend'));
+ });
+};
+
+textUtils.removeListeners = function(form) {
return $('.js-md', form).off('click');
-}
-
-export function initTextMarkdown(form) {
- return $('.js-md', form)
- .off('click')
- .on('click', function onClickMd() {
- const $this = $(this);
- return updateText(
- $this.closest('.md-area').find('textarea'),
- $this.data('md-tag'), $this.data('md-block'),
- !$this.data('md-prepend'),
- );
- });
-}
+};
+
+textUtils.replaceRange = function(s, start, end, substitute) {
+ return s.substring(0, start) + substitute + s.substring(end);
+};
+
+export default textUtils;
diff --git a/app/assets/javascripts/lib/utils/text_utility.js b/app/assets/javascripts/lib/utils/text_utility.js
index b1ba54d8719..28ab9dddc4c 100644
--- a/app/assets/javascripts/lib/utils/text_utility.js
+++ b/app/assets/javascripts/lib/utils/text_utility.js
@@ -45,3 +45,13 @@ export const dasherize = str => str.replace(/[_\s]+/g, '-');
* @returns {String}
*/
export const slugify = str => str.trim().toLowerCase();
+
+/**
+ * Truncates given text
+ *
+ * @param {String} string
+ * @param {Number} maxLength
+ * @returns {String}
+ */
+export const truncate = (string, maxLength) => `${string.substr(0, (maxLength - 3))}...`;
+
diff --git a/app/assets/javascripts/main.js b/app/assets/javascripts/main.js
index 31c5cfc5e55..127fddcf8d3 100644
--- a/app/assets/javascripts/main.js
+++ b/app/assets/javascripts/main.js
@@ -30,7 +30,6 @@ import './commit/image_file';
import { handleLocationHash } from './lib/utils/common_utils';
import './lib/utils/datetime_utility';
import './lib/utils/pretty_time';
-import './lib/utils/text_utility';
import './lib/utils/url_utility';
// behaviors
diff --git a/spec/javascripts/lib/utils/text_markdown_spec.js b/spec/javascripts/lib/utils/text_markdown_spec.js
index 58f27ff42b7..a95a7e2a5be 100644
--- a/spec/javascripts/lib/utils/text_markdown_spec.js
+++ b/spec/javascripts/lib/utils/text_markdown_spec.js
@@ -1,6 +1,6 @@
-import { insertTextinTextarea } from '~/lib/utils/text_markdown';
+import textUtils from '~/lib/utils/text_markdown';
-describe('insertTextinTextarea', () => {
+describe('init markdown', () => {
let textArea;
beforeAll(() => {
@@ -21,7 +21,7 @@ describe('insertTextinTextarea', () => {
textArea.selectionStart = 0;
textArea.selectionEnd = 0;
- insertTextinTextarea(textArea, textArea.value, '*', null, '', false);
+ textUtils.insertText(textArea, textArea.value, '*', null, '', false);
expect(textArea.value).toEqual(`${initialValue}* `);
});
@@ -32,7 +32,7 @@ describe('insertTextinTextarea', () => {
textArea.value = initialValue;
textArea.setSelectionRange(initialValue.length, initialValue.length);
- insertTextinTextarea(textArea, textArea.value, '*', null, '', false);
+ textUtils.insertText(textArea, textArea.value, '*', null, '', false);
expect(textArea.value).toEqual(`${initialValue}\n* `);
});
@@ -43,7 +43,7 @@ describe('insertTextinTextarea', () => {
textArea.value = initialValue;
textArea.setSelectionRange(initialValue.length, initialValue.length);
- insertTextinTextarea(textArea, textArea.value, '*', null, '', false);
+ textUtils.insertText(textArea, textArea.value, '*', null, '', false);
expect(textArea.value).toEqual(`${initialValue}* `);
});
@@ -54,7 +54,7 @@ describe('insertTextinTextarea', () => {
textArea.value = initialValue;
textArea.setSelectionRange(initialValue.length, initialValue.length);
- insertTextinTextarea(textArea, textArea.value, '*', null, '', false);
+ textUtils.insertText(textArea, textArea.value, '*', null, '', false);
expect(textArea.value).toEqual(`${initialValue}* `);
});