+ },
+
+ addTheme: function()
+ {
+ var selection = document.getSelection();
+ var n = selection.rangeCount;
+
+ if(n == 0)
+ window.alert("Nie zaznaczono żadnego obszaru");
+
+ // for now allow only 1 range
+ if(n > 1)
+ window.alert("Zaznacz jeden obszar");
+
+ // from this point, we will assume that the ranges are disjoint
+ for(var i=0; i < n; i++) {
+ var range = selection.getRangeAt(i);
+ console.log(i, range.startContainer, range.endContainer);
+ var date = Date.now();
+ var random = Math.floor(4000000000*Math.random());
+ var id = (''+date) + '-' + (''+random);
+
+ var ipoint = document.createRange();
+
+
+
+ // Firefox alters the later node when inserting, so
+ // insert from end
+ ipoint.setStart(range.endContainer, range.endOffset);
+ elem = $('<span class="theme-end" x-theme-class="'+id+'" id="e'+id+'"></span>')[0];
+ ipoint.insertNode(elem);
+
+ // insert theme-ref
+ ipoint.setStart(range.startContainer, range.startOffset);
+ var elem = $('<span class="theme-ref" x-theme-class="'+id+'" id="m'+id+'">Nowy motyw</span>')[0];
+ ipoint.insertNode(elem);
+ ipoint.setStartBefore(elem);
+
+ // insert theme-begin
+ elem = $('<span class="theme-begin" x-theme-class="'+id+'" id="b'+id+'"></span>')[0];
+ ipoint.insertNode(elem);
+ }
+
+ selection.removeAllRanges();
+ },
+
+ selectTheme: function(themeId)
+ {
+ var selection = document.getSelection();
+
+ // remove current selection
+ selection.removeAllRanges();
+
+ var range = document.createRange();
+ var s = $('#m'+themeId)[0];
+ var e = $('#e'+themeId)[0];
+ console.log('Selecting range:', themeId, range, s, e);
+
+ if(s && e) {
+ range.setStartAfter(s);
+ range.setEndBefore(e);
+ selection.addRange(range);
+ }