From 132e0b83c4be042f7559ba5049b3167b18a838b3 Mon Sep 17 00:00:00 2001 From: rabuzarus Date: Fri, 1 Feb 2019 23:48:14 +0100 Subject: [PATCH] attachment preview: mv some js functions from frio to the linkPreview to make linkPreview available for other themes --- view/js/linkPreview.js | 63 +++++++++++++++++++++++++++++++++++++++ view/theme/frio/js/jot.js | 63 --------------------------------------- 2 files changed, 63 insertions(+), 63 deletions(-) diff --git a/view/js/linkPreview.js b/view/js/linkPreview.js index ab5091c7e7..cbb5e48da3 100644 --- a/view/js/linkPreview.js +++ b/view/js/linkPreview.js @@ -441,4 +441,67 @@ defaultDescription: "Enter a description", defaultTitle: "Enter a title" }; + + /** + * Get in a textarea the previous word before the cursor. + * + * @param {object} text Textarea elemet. + * @param {integer} caretPos Cursor position. + * + * @returns {string} Previous word. + */ + function returnWord(text, caretPos) { + var index = text.indexOf(caretPos); + var preText = text.substring(0, caretPos); + // If the last charachter is a space remove the one space + // We need this in friendica for the url preview. + if (preText.slice(-1) == " ") { + preText = preText.substring(0, preText.length -1); + } + + if (preText.indexOf(" ") > 0) { + var words = preText.split(" "); + return words[words.length - 1]; //return last word + } + else { + return preText; + } + } + + /** + * Get in a textarea the previous word before the cursor. + * + * @param {string} id The ID of a textarea element. + * @returns {sting|null} Previous word or null if no word is available. + */ + function getPrevWord(id) { + var text = document.getElementById(id); + var caretPos = getCaretPosition(text); + var word = returnWord(text.value, caretPos); + if (word != null) { + return word + } + + } + + /** + * Get the cursor posiotion in an text element. + * + * @param {object} ctrl Textarea elemet. + * @returns {integer} Position of the cursor. + */ + function getCaretPosition(ctrl) { + var CaretPos = 0; // IE Support + if (document.selection) { + ctrl.focus(); + var Sel = document.selection.createRange(); + Sel.moveStart('character', -ctrl.value.length); + CaretPos = Sel.text.length; + } + // Firefox support + else if (ctrl.selectionStart || ctrl.selectionStart == '0') { + CaretPos = ctrl.selectionStart; + } + return (CaretPos); + } })(jQuery); diff --git a/view/theme/frio/js/jot.js b/view/theme/frio/js/jot.js index f63ed53569..25db238241 100644 --- a/view/theme/frio/js/jot.js +++ b/view/theme/frio/js/jot.js @@ -29,66 +29,3 @@ function jotGetLink() { autosize.update($("#profile-jot-text")); } } - -/** - * Get in a textarea the previous word before the cursor. - * - * @param {object} text Textarea elemet. - * @param {integer} caretPos Cursor position. - * - * @returns {string} Previous word. - */ -function returnWord(text, caretPos) { - var index = text.indexOf(caretPos); - var preText = text.substring(0, caretPos); - // If the last charachter is a space remove the one space - // We need this in friendica for the url preview. - if (preText.slice(-1) == " ") { - preText = preText.substring(0, preText.length -1); - } -// preText = preText.replace(/^\s+|\s+$/g, ""); - if (preText.indexOf(" ") > 0) { - var words = preText.split(" "); - return words[words.length - 1]; //return last word - } - else { - return preText; - } -} - -/** - * Get in a textarea the previous word before the cursor. - * - * @param {string} id The ID of a textarea element. - * @returns {sting|null} Previous word or null if no word is available. - */ -function getPrevWord(id) { - var text = document.getElementById(id); - var caretPos = getCaretPosition(text); - var word = returnWord(text.value, caretPos); - if (word != null) { - return word - } - -} - -/** - * Get the cursor posiotion in an text element. - * - * @param {object} ctrl Textarea elemet. - * @returns {integer} Position of the cursor. - */ -function getCaretPosition(ctrl) { - var CaretPos = 0; // IE Support - if (document.selection) { - ctrl.focus(); - var Sel = document.selection.createRange(); - Sel.moveStart('character', -ctrl.value.length); - CaretPos = Sel.text.length; - } - // Firefox support - else if (ctrl.selectionStart || ctrl.selectionStart == '0') { - CaretPos = ctrl.selectionStart; - } - return (CaretPos); -} -- 2.39.5