aboutsummaryrefslogtreecommitdiff
path: root/flakes/etherpad-lite/modules/ep_immae_buttons/static/js/main.js
blob: 07f7b9c932085ee541652a06500d13f267760e9b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
exports.postAceInit = function(hook, context){
  $(document).ready(function () {
    $('.clearFormatting').click(function(){
      context.ace.callWithAce(function(ace){

        var rep = ace.ace_getRep(); // get the current user selection
        var isSelection = (rep.selStart[0] !== rep.selEnd[0] || rep.selStart[1] !== rep.selEnd[1]);
        if(!isSelection) return false; // No point proceeding if no selection..

        var attrs = rep.apool.attribToNum; // get the attributes on this document
        $.each(attrs, function(k, v){ // for each attribute
          var attr = k.split(",")[0]; // get the name of the attribute
          if(attr !== "author"){ // if its not an author attribute
            ace.ace_setAttributeOnSelection(attr, false); // set the attribute to false
          }
        });
      },'clearFormatting' , true);
    });

    $('.findAndReplace').click(function(){
      var from = prompt("Search for...");
      var to = prompt("Replace with...");
      var HTMLLines = $('iframe[name="ace_outer"]').contents().find('iframe').contents().find("#innerdocbody").children("div");
      $(HTMLLines).each(function(){ // For each line
        findAndReplace(from, to, this);
      });
    });

  });
}

function findAndReplace(searchText, replacement, searchNode) {
  if (!searchText || typeof replacement === 'undefined') {
    // Throw error here if you want...
    return;
  }
  var regex = typeof searchText === 'string' ?
    new RegExp(searchText, 'gi') : searchText,
    childNodes = (searchNode || document.body).childNodes,
    cnLength = childNodes.length,
    excludes = ["html","head","style","title","meta","script","object","iframe","link"];

  while (cnLength--) {
    var currentNode = childNodes[cnLength];
    if (currentNode.nodeType === 1){
      if(excludes.indexOf(currentNode.nodeName.toLowerCase() === -1)){
        arguments.callee(searchText, replacement, currentNode);
      }
    }
    if (currentNode.nodeType !== 3 || !regex.test(currentNode.data) ) {
      continue;
    }
    var parent = currentNode.parentNode,
    frag = (function(){
      var html = currentNode.data.replace(regex, replacement),
      wrap = document.createElement('div'),
      frag = document.createDocumentFragment();
      wrap.innerHTML = html;
      while (wrap.firstChild) {
        frag.appendChild(wrap.firstChild);
      }
      return frag;
    })();
    parent.insertBefore(frag, currentNode);
    parent.removeChild(currentNode);
  }
}