]>
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
65 this.parent
.find("#bold").click(this.bold
.bind(this));
66 this.parent
.find("#italic").click(this.italic
.bind(this));
69 this.parent
.find("#headline").click(this.headline
.bind(this));
70 this.parent
.find("#headline-down").click(this.headline_down
.bind(this));
71 this.parent
.find("#headline-up").click(this.headline_up
.bind(this));
74 // Functions to modify the text
76 replaceSelection(replacement
) {
77 // Get the DOM element
78 var textarea
= this.textarea
.get(0);
80 // Write text to textarea and move the cursor to the end
81 textarea
.setRangeText(replacement
,
82 this.selection
.start
, this.selection
.end
, "end");
85 insertAtCursor(insertion
) {
86 this.replaceSelection(insertion
);
90 console
.debug("Converting into bold: " + this.selection
.text
);
91 this.replaceSelection("**" + this.selection
.text
+ "**");
95 console
.debug("Converting into italic: " + this.selection
.text
);
96 this.replaceSelection("*" + this.selection
.text
+ "*");
102 // Get all text between start and cursor position
103 var text
= this.textarea
.val().slice(0, this.selection
.start
);
105 // Split it in lines and reverse
106 var lines
= text
.split("\n");
109 for (var line
of lines
) {
112 // Return the number of # found in the nearest headline
113 var match
= line
.match("^(#+)");
115 return match
[1].length
;
119 // If nothing was found, we are on level one
123 insertHeadline(offset
) {
124 // Find level of headlines
125 var level
= Math
.max(this.findLevel() + offset
, 1);
127 console
.debug("Adding headline (" + level
+ ")");
128 var headline
= "#".repeat(level
);
130 if (this.selection
.length
== 0) {
133 // Add some space if we don't have any, yet
134 if (!this.selection
.text
.startsWith(" "))
137 headline
+= this.selection
.text
+ "\n\n";
140 this.replaceSelection(headline
);
144 return this.insertHeadline(0);
148 return this.insertHeadline(1);
152 return this.insertHeadline(-1);
156 $(document
).ready(function() {
157 // Initialise all editors
158 $(".editor").each(function(i
, e
) {