]> git.immae.eu Git - perso/Immae/Config/Nix.git/blob - pkgs/webapps/etherpad-lite/modules/ep_immae_buttons/static/js/main.js
Fix some etherpad-lite modules and packaging
[perso/Immae/Config/Nix.git] / pkgs / webapps / etherpad-lite / modules / ep_immae_buttons / static / js / main.js
1 exports.postAceInit = function(hook, context){
2 $(document).ready(function () {
3 $('.clearFormatting').click(function(){
4 context.ace.callWithAce(function(ace){
5
6 var rep = ace.ace_getRep(); // get the current user selection
7 var isSelection = (rep.selStart[0] !== rep.selEnd[0] || rep.selStart[1] !== rep.selEnd[1]);
8 if(!isSelection) return false; // No point proceeding if no selection..
9
10 var attrs = rep.apool.attribToNum; // get the attributes on this document
11 $.each(attrs, function(k, v){ // for each attribute
12 var attr = k.split(",")[0]; // get the name of the attribute
13 if(attr !== "author"){ // if its not an author attribute
14 ace.ace_setAttributeOnSelection(attr, false); // set the attribute to false
15 }
16 });
17 },'clearFormatting' , true);
18 });
19
20 $('.findAndReplace').click(function(){
21 var from = prompt("Search for...");
22 var to = prompt("Replace with...");
23 var HTMLLines = $('iframe[name="ace_outer"]').contents().find('iframe').contents().find("#innerdocbody").children("div");
24 $(HTMLLines).each(function(){ // For each line
25 findAndReplace(from, to, this);
26 });
27 });
28
29 });
30 }
31
32 function findAndReplace(searchText, replacement, searchNode) {
33 if (!searchText || typeof replacement === 'undefined') {
34 // Throw error here if you want...
35 return;
36 }
37 var regex = typeof searchText === 'string' ?
38 new RegExp(searchText, 'gi') : searchText,
39 childNodes = (searchNode || document.body).childNodes,
40 cnLength = childNodes.length,
41 excludes = ["html","head","style","title","meta","script","object","iframe","link"];
42
43 while (cnLength--) {
44 var currentNode = childNodes[cnLength];
45 if (currentNode.nodeType === 1){
46 if(excludes.indexOf(currentNode.nodeName.toLowerCase() === -1)){
47 arguments.callee(searchText, replacement, currentNode);
48 }
49 }
50 if (currentNode.nodeType !== 3 || !regex.test(currentNode.data) ) {
51 continue;
52 }
53 var parent = currentNode.parentNode,
54 frag = (function(){
55 var html = currentNode.data.replace(regex, replacement),
56 wrap = document.createElement('div'),
57 frag = document.createDocumentFragment();
58 wrap.innerHTML = html;
59 while (wrap.firstChild) {
60 frag.appendChild(wrap.firstChild);
61 }
62 return frag;
63 })();
64 parent.insertBefore(frag, currentNode);
65 parent.removeChild(currentNode);
66 }
67 }