]>
git.ipfire.org Git - ipfire.org.git/blob - src/static/js/editor.js
beb3413e64eaa2c1bfa5ac4e9b18dd51059a9e77
3 this.parent
= $(parent
);
4 this.data
= this.parent
.data();
6 // Hide the preview here
7 this.preview
= $("#preview");
11 this.textarea
= this.parent
.find("textarea");
13 // Initialise selection
21 // Make the textarea magic
22 this.activateTextArea();
27 // Change timer for preview
30 console
.log("Editor initialised for " + this.parent
);
32 // Set focus on the textarea
33 this.textarea
.focus();
39 // Render preview when content has changed
40 this.textarea
.on("keyup change", function(e
) {
42 clearTimeout(editor
.update
);
44 var content
= editor
.textarea
.val();
46 // If the field is all empty, we will hide it
48 editor
.preview
.show();
50 editor
.preview
.hide();
52 // Go into update mode
53 editor
.preview
.addClass("updating");
55 // Render content and show it
56 editor
.update
= setTimeout(function() {
57 var c
= $("#preview-content");
59 $.post(editor
.data
.render_url
, { content
: content
},
64 editor
.preview
.removeClass("updating");
70 // Remember any selected text
71 this.textarea
.on("select keyup click", function(e
) {
72 // Ignore any keyboard shortcuts
78 start
: this.selectionStart
,
79 end
: this.selectionEnd
,
80 text
: this.value
.slice(this.selectionStart
, this.selectionEnd
),
81 length
: this.selectionEnd
- this.selectionStart
,
84 console
.debug("Something got selected:");
85 console
.debug(editor
.selection
);
88 // Bind keyboard shortcuts
89 this.textarea
.on("keyup", function(e
) {
90 // If Ctrl wasn't pressed this isn't for us
125 this.parent
.find("#bold").click(this.bold
.bind(this));
126 this.parent
.find("#italic").click(this.italic
.bind(this));
127 this.parent
.find("#code").click(this.code
.bind(this));
130 this.parent
.find("#headline").click(this.headline
.bind(this));
131 this.parent
.find("#headline-down").click(this.headline_down
.bind(this));
132 this.parent
.find("#headline-up").click(this.headline_up
.bind(this));
135 this.parent
.find("#link").click(this.link
.bind(this));
138 // Functions to modify the text
140 replaceSelection(replacement
) {
141 // Get the DOM element
142 var textarea
= this.textarea
.get(0);
144 // Write text to textarea and move the cursor to the end
145 textarea
.setRangeText(replacement
,
146 this.selection
.start
, this.selection
.end
, "end");
149 insertAtCursor(insertion
) {
150 this.replaceSelection(insertion
);
154 console
.debug("Converting into bold: " + this.selection
.text
);
155 this.replaceSelection("**" + this.selection
.text
+ "**");
159 console
.debug("Converting into italic: " + this.selection
.text
);
160 this.replaceSelection("*" + this.selection
.text
+ "*");
164 var multiline
= this.selection
.text
.indexOf("\n");
166 if (multiline
>= 0) {
167 this.replaceSelection("```\n" + this.selection
.text
+ "\n```\n\n");
169 this.replaceSelection("`" + this.selection
.text
+ "`");
175 if (this.selection
.text
.startsWith("https://") || this.selection
.text
.startsWith("http://")) {
176 this.replaceSelection("[" + this.selection
.text
+ "](" + this.selection
.text
+ ")");
177 // Handle selected text
179 this.replaceSelection("[" + this.selection
.text
+ "]()")
186 // Get all text between start and cursor position
187 var text
= this.textarea
.val().slice(0, this.selection
.start
);
189 // Split it in lines and reverse
190 var lines
= text
.split("\n");
193 for (var line
of lines
) {
196 // Return the number of # found in the nearest headline
197 var match
= line
.match("^(#+)");
199 return match
[1].length
;
203 // If nothing was found, we are on level one
207 insertHeadline(offset
) {
208 // Find level of headlines
209 var level
= Math
.max(this.findLevel() + offset
, 1);
211 console
.debug("Adding headline (" + level
+ ")");
212 var headline
= "#".repeat(level
);
214 if (this.selection
.length
== 0) {
217 // Add some space if we don't have any, yet
218 if (!this.selection
.text
.startsWith(" "))
221 headline
+= this.selection
.text
+ "\n\n";
224 this.replaceSelection(headline
);
228 return this.insertHeadline(0);
232 return this.insertHeadline(1);
236 return this.insertHeadline(-1);
240 $(document
).ready(function() {
241 // Initialise all editors
242 $(".editor").each(function(i
, e
) {