]>
git.ipfire.org Git - ipfire.org.git/blob - src/static/js/editor.js
7639ae2eaa09bd13f436f6b17967cf0cd9f1e453
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
) {
23 start
: this.selectionStart
,
24 end
: this.selectionEnd
,
25 text
: this.value
.slice(this.selectionStart
, this.selectionEnd
),
26 length
: this.selectionEnd
- this.selectionStart
,
29 console
.debug("Something got selected:");
30 console
.debug(editor
.selection
);
36 this.parent
.find("#bold").click(this.bold
.bind(this));
37 this.parent
.find("#italic").click(this.italic
.bind(this));
40 this.parent
.find("#headline").click(this.headline
.bind(this));
41 this.parent
.find("#headline-down").click(this.headline_down
.bind(this));
42 this.parent
.find("#headline-up").click(this.headline_up
.bind(this));
45 // Functions to modify the text
47 replaceSelection(replacement
) {
48 var text
= this.textarea
.val();
50 text
= text
.slice(0, this.selection
.start
) + replacement
+ text
.slice(this.selection
.end
);
52 // Write text to textarea
53 this.textarea
.val(text
);
56 insertAtCursor(insertion
) {
57 this.replaceSelection(insertion
);
61 console
.debug("Converting into bold: " + this.selection
.text
);
62 this.replaceSelection("**" + this.selection
.text
+ "**");
66 console
.debug("Converting into italic: " + this.selection
.text
);
67 this.replaceSelection("*" + this.selection
.text
+ "*");
73 // Get all text between start and cursor position
74 var text
= this.textarea
.val().slice(0, this.selection
.start
);
76 // Split it in lines and reverse
77 var lines
= text
.split("\n");
80 for (var line
of lines
) {
83 // Return the number of # found in the nearest headline
84 var match
= line
.match("^(#+)");
86 return match
[1].length
;
90 // If nothing was found, we are on level one
94 insertHeadline(offset
) {
95 // Find level of headlines
96 var level
= Math
.max(this.findLevel() + offset
, 1);
98 console
.debug("Adding headline (" + level
+ ")");
99 var headline
= "#".repeat(level
);
101 if (this.selection
.length
== 0) {
104 // Add some space if we don't have any, yet
105 if (!this.selection
.text
.startsWith(" "))
108 headline
+= this.selection
.text
+ "\n\n";
111 this.replaceSelection(headline
);
115 return this.insertHeadline(0);
119 return this.insertHeadline(1);
123 return this.insertHeadline(-1);
127 $(document
).ready(function() {
128 // Initialise all editors
129 $(".editor").each(function(i
, e
) {