]>
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 // Initialise selection
16 // Make the textarea magic
17 this.activateTextArea();
22 console
.log("Editor initialised for " + this.parent
);
24 // Set focus on the textarea
25 this.textarea
.focus();
31 // Remember any selected text
32 this.textarea
.on("select keyup click", function(e
) {
33 // Ignore any keyboard shortcuts
39 start
: this.selectionStart
,
40 end
: this.selectionEnd
,
41 text
: this.value
.slice(this.selectionStart
, this.selectionEnd
),
42 length
: this.selectionEnd
- this.selectionStart
,
45 console
.debug("Something got selected:");
46 console
.debug(editor
.selection
);
49 // Bind keyboard shortcuts
50 this.textarea
.on("keyup", function(e
) {
51 // If Ctrl wasn't pressed this isn't for us
86 this.parent
.find("#bold").click(this.bold
.bind(this));
87 this.parent
.find("#italic").click(this.italic
.bind(this));
88 this.parent
.find("#code").click(this.code
.bind(this));
91 this.parent
.find("#headline").click(this.headline
.bind(this));
92 this.parent
.find("#headline-down").click(this.headline_down
.bind(this));
93 this.parent
.find("#headline-up").click(this.headline_up
.bind(this));
96 this.parent
.find("#link").click(this.link
.bind(this));
99 // Functions to modify the text
101 replaceSelection(replacement
) {
102 // Get the DOM element
103 var textarea
= this.textarea
.get(0);
105 // Write text to textarea and move the cursor to the end
106 textarea
.setRangeText(replacement
,
107 this.selection
.start
, this.selection
.end
, "end");
110 insertAtCursor(insertion
) {
111 this.replaceSelection(insertion
);
115 console
.debug("Converting into bold: " + this.selection
.text
);
116 this.replaceSelection("**" + this.selection
.text
+ "**");
120 console
.debug("Converting into italic: " + this.selection
.text
);
121 this.replaceSelection("*" + this.selection
.text
+ "*");
125 var multiline
= this.selection
.text
.indexOf("\n");
127 if (multiline
>= 0) {
128 this.replaceSelection("```\n" + this.selection
.text
+ "\n```\n\n");
130 this.replaceSelection("`" + this.selection
.text
+ "`");
136 if (this.selection
.text
.startsWith("https://") || this.selection
.text
.startsWith("http://")) {
137 this.replaceSelection("[" + this.selection
.text
+ "](" + this.selection
.text
+ ")");
138 // Handle selected text
140 this.replaceSelection("[" + this.selection
.text
+ "]()")
147 // Get all text between start and cursor position
148 var text
= this.textarea
.val().slice(0, this.selection
.start
);
150 // Split it in lines and reverse
151 var lines
= text
.split("\n");
154 for (var line
of lines
) {
157 // Return the number of # found in the nearest headline
158 var match
= line
.match("^(#+)");
160 return match
[1].length
;
164 // If nothing was found, we are on level one
168 insertHeadline(offset
) {
169 // Find level of headlines
170 var level
= Math
.max(this.findLevel() + offset
, 1);
172 console
.debug("Adding headline (" + level
+ ")");
173 var headline
= "#".repeat(level
);
175 if (this.selection
.length
== 0) {
178 // Add some space if we don't have any, yet
179 if (!this.selection
.text
.startsWith(" "))
182 headline
+= this.selection
.text
+ "\n\n";
185 this.replaceSelection(headline
);
189 return this.insertHeadline(0);
193 return this.insertHeadline(1);
197 return this.insertHeadline(-1);
201 $(document
).ready(function() {
202 // Initialise all editors
203 $(".editor").each(function(i
, e
) {