]>
git.ipfire.org Git - ipfire.org.git/blob - src/static/js/editor.js
3 this.parent
= $(parent
);
6 this.textarea
= this.parent
.find("textarea");
8 // Make the textarea magic
9 this.activateTextArea();
14 console
.log("Editor initialised for " + this.parent
);
20 // Remember any selected text
21 this.textarea
.on("select keyup click", function(e
) {
22 // Ignore any keyboard shortcuts
28 start
: this.selectionStart
,
29 end
: this.selectionEnd
,
30 text
: this.value
.slice(this.selectionStart
, this.selectionEnd
),
31 length
: this.selectionEnd
- this.selectionStart
,
34 console
.debug("Something got selected:");
35 console
.debug(editor
.selection
);
38 // Bind keyboard shortcuts
39 this.textarea
.on("keyup", function(e
) {
40 // If Ctrl wasn't pressed this isn't for us
75 this.parent
.find("#bold").click(this.bold
.bind(this));
76 this.parent
.find("#italic").click(this.italic
.bind(this));
77 this.parent
.find("#code").click(this.code
.bind(this));
80 this.parent
.find("#headline").click(this.headline
.bind(this));
81 this.parent
.find("#headline-down").click(this.headline_down
.bind(this));
82 this.parent
.find("#headline-up").click(this.headline_up
.bind(this));
85 this.parent
.find("#link").click(this.link
.bind(this));
88 // Functions to modify the text
90 replaceSelection(replacement
) {
91 // Get the DOM element
92 var textarea
= this.textarea
.get(0);
94 // Write text to textarea and move the cursor to the end
95 textarea
.setRangeText(replacement
,
96 this.selection
.start
, this.selection
.end
, "end");
99 insertAtCursor(insertion
) {
100 this.replaceSelection(insertion
);
104 console
.debug("Converting into bold: " + this.selection
.text
);
105 this.replaceSelection("**" + this.selection
.text
+ "**");
109 console
.debug("Converting into italic: " + this.selection
.text
);
110 this.replaceSelection("*" + this.selection
.text
+ "*");
114 var multiline
= this.selection
.text
.indexOf("\n");
116 if (multiline
>= 0) {
117 this.replaceSelection("```\n" + this.selection
.text
+ "\n```\n\n");
119 this.replaceSelection("`" + this.selection
.text
+ "`");
125 if (this.selection
.text
.startsWith("https://") || this.selection
.text
.startsWith("http://")) {
126 this.replaceSelection("[" + this.selection
.text
+ "](" + this.selection
.text
+ ")");
127 // Handle selected text
129 this.replaceSelection("[" + this.selection
.text
+ "]()")
136 // Get all text between start and cursor position
137 var text
= this.textarea
.val().slice(0, this.selection
.start
);
139 // Split it in lines and reverse
140 var lines
= text
.split("\n");
143 for (var line
of lines
) {
146 // Return the number of # found in the nearest headline
147 var match
= line
.match("^(#+)");
149 return match
[1].length
;
153 // If nothing was found, we are on level one
157 insertHeadline(offset
) {
158 // Find level of headlines
159 var level
= Math
.max(this.findLevel() + offset
, 1);
161 console
.debug("Adding headline (" + level
+ ")");
162 var headline
= "#".repeat(level
);
164 if (this.selection
.length
== 0) {
167 // Add some space if we don't have any, yet
168 if (!this.selection
.text
.startsWith(" "))
171 headline
+= this.selection
.text
+ "\n\n";
174 this.replaceSelection(headline
);
178 return this.insertHeadline(0);
182 return this.insertHeadline(1);
186 return this.insertHeadline(-1);
190 $(document
).ready(function() {
191 // Initialise all editors
192 $(".editor").each(function(i
, e
) {