]> git.mxchange.org Git - friendica.git/blob - view/theme/frio/js/jot.js
attachment preview: frontend work (works with frio)
[friendica.git] / view / theme / frio / js / jot.js
1 var linkPreview;
2
3 $(document).ready(function() {
4         linkPreview = $('#profile-jot-text').linkPreview();
5 });
6
7
8 /**
9  * Insert a link into friendica jot.
10  * 
11  * @returns {void}
12  */
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';
24                 }
25
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"));
30         }
31 }
32
33 /**
34  * Get in a textarea the previous word before the cursor.
35  * 
36  * @param {object} text Textarea elemet.
37  * @param {integer} caretPos Cursor position.
38  * 
39  * @returns {string} Previous word.
40  */
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);
48         }
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
53         }
54         else {
55                 return preText;
56         }
57 }
58
59 /**
60  * Get in a textarea the previous word before the cursor.
61  * 
62  * @param {string} id The ID of a textarea element.
63  * @returns {sting|null} Previous word or null if no word is available.
64  */
65 function getPrevWord(id) {
66         var text = document.getElementById(id);
67         var caretPos = getCaretPosition(text);
68         var word = returnWord(text.value, caretPos);
69         if (word != null) {
70                 return word
71         }
72
73 }
74
75 /**
76  * Get the cursor posiotion in an text element.
77  * 
78  * @param {object} ctrl Textarea elemet.
79  * @returns {integer} Position of the cursor.
80  */
81 function getCaretPosition(ctrl) {
82         var CaretPos = 0;   // IE Support
83         if (document.selection) {
84                 ctrl.focus();
85                 var Sel = document.selection.createRange();
86                 Sel.moveStart('character', -ctrl.value.length);
87                 CaretPos = Sel.text.length;
88         }
89         // Firefox support
90         else if (ctrl.selectionStart || ctrl.selectionStart == '0') {
91                 CaretPos = ctrl.selectionStart;
92         }
93         return (CaretPos);
94 }