3 $(document).ready(function() {
4 linkPreview = $('#profile-jot-text').linkPreview();
9 * Insert a link into friendica jot.
13 function jotGetLink() {
14 var currentText = $("#profile-jot-text").val();
15 var noAttachment = '';
16 reply = prompt(aStr.linkurl);
17 if(reply && reply.length) {
18 // There should be only one attachment per post.
19 // So we need to remove the old one.
20 $('#jot-attachment-preview').empty();
21 $('#profile-rotator').show();
22 if (currentText.includes("[attachment") && currentText.includes("[/attachment]")) {
23 noAttachment = '&noAttachment=1';
26 // We use the linkPreview library to have a preview
27 // of the attachments.
28 linkPreview.crawlText(reply + noAttachment);
29 autosize.update($("#profile-jot-text"));
34 * Get in a textarea the previous word before the cursor.
36 * @param {object} text Textarea elemet.
37 * @param {integer} caretPos Cursor position.
39 * @returns {string} Previous word.
41 function returnWord(text, caretPos) {
42 var index = text.indexOf(caretPos);
43 var preText = text.substring(0, caretPos);
44 // If the last charachter is a space remove the one space
45 // We need this in friendica for the url preview.
46 if (preText.slice(-1) == " ") {
47 preText = preText.substring(0, preText.length -1);
49 // preText = preText.replace(/^\s+|\s+$/g, "");
50 if (preText.indexOf(" ") > 0) {
51 var words = preText.split(" ");
52 return words[words.length - 1]; //return last word
60 * Get in a textarea the previous word before the cursor.
62 * @param {string} id The ID of a textarea element.
63 * @returns {sting|null} Previous word or null if no word is available.
65 function getPrevWord(id) {
66 var text = document.getElementById(id);
67 var caretPos = getCaretPosition(text);
68 var word = returnWord(text.value, caretPos);
76 * Get the cursor posiotion in an text element.
78 * @param {object} ctrl Textarea elemet.
79 * @returns {integer} Position of the cursor.
81 function getCaretPosition(ctrl) {
82 var CaretPos = 0; // IE Support
83 if (document.selection) {
85 var Sel = document.selection.createRange();
86 Sel.moveStart('character', -ctrl.value.length);
87 CaretPos = Sel.text.length;
90 else if (ctrl.selectionStart || ctrl.selectionStart == '0') {
91 CaretPos = ctrl.selectionStart;