// In order to stick with the format used in Fidus Writer 1.1-2.0,
// we do a few smaller modifications to the node before it is saved.

export class ModNodeConvert {
    constructor(editor) {
        editor.mod.nodeConvert = this
        this.editor = editor
    }

    modelToEditorNode(node) {
        let fnNodes = node.querySelectorAll('span.footnote')

          for (let i = 0; i < fnNodes.length; i++) {
              let newNode = document.createElement('span')
              newNode.setAttribute('contents',fnNodes[i].innerHTML)
              newNode.classList.add('footnote-marker')
              fnNodes[i].parentNode.replaceChild(newNode, fnNodes[i])
          }
          return node
    }

    editorToModelNode(node) {
        let fnNodes = node.querySelectorAll('.footnote-marker')

        for (let i = 0; i < fnNodes.length; i++) {
            let newNode = document.createElement('span')
            newNode.innerHTML = fnNodes[i].getAttribute('contents')
            newNode.classList.add('footnote')
            fnNodes[i].parentNode.replaceChild(newNode, fnNodes[i])
        }

        let strongNodes = node.querySelectorAll('strong')

        for (let i = 0; i < strongNodes.length; i++) {
            let newNode = document.createElement('b')
            while (strongNodes[i].firstChild) {
                newNode.appendChild(strongNodes[i].firstChild)
            }
            strongNodes[i].parentNode.replaceChild(newNode, strongNodes[i])
        }

        let emNodes = node.querySelectorAll('em')

        for (let i = 0; i < emNodes.length; i++) {
            let newNode = document.createElement('i')
            while (emNodes[i].firstChild) {
                newNode.appendChild(emNodes[i].firstChild)
            }
            emNodes[i].parentNode.replaceChild(newNode, emNodes[i])
        }

        // Remove katex contents of <span class="equation" >
        let mathNodes = node.querySelectorAll('span.equation')

        for (let i = 0; i < mathNodes.length; i++) {
            while (mathNodes[i].firstChild) {
                mathNodes[i].removeChild(mathNodes[i].firstChild)
            }
        }

        // Remove all rendered contents (euqations and images) of figures.
        let figureNodes = node.querySelectorAll('figure')

        for (let i = 0; i < figureNodes.length; i++) {
            while (figureNodes[i].firstChild) {
                figureNodes[i].removeChild(figureNodes[i].firstChild)
            }
        }

        // Remove all contenteditable attributes
        let ceNodes = node.querySelectorAll('[contenteditable]')

        for (let i = 0; i < ceNodes.length; i++) {
            ceNodes[i].removeAttribute('contenteditable')
        }

        return node
    }

}

@@ -2133,32 +2133,119 @@
    %7D%0A        %7D%0A%0A
+        // TODO: Possibly enable this, but only for saving on server, not for exports.%0A
         // Remov
@@ -2274,10 +2274,10 @@
 s (e
-u
 q
+u
 atio
@@ -2307,24 +2307,26 @@
 es.%0A        
+//
 let figureNo
@@ -2359,24 +2359,34 @@
 l('figure')%0A
+        //
 %0A        for
@@ -2374,32 +2374,34 @@
      //%0A        
+//
 for (let i = 0; 
@@ -2431,32 +2431,34 @@
  i++) %7B%0A        
+//
     while (figur
@@ -2481,32 +2481,34 @@
 hild) %7B%0A        
+//
         figureNo
@@ -2553,32 +2553,34 @@
 tChild)%0A        
+//
     %7D%0A        %7D%0A
@@ -2569,32 +2569,34 @@
 //    %7D%0A        
+//
 %7D%0A%0A        // Re

fix figure export, fixes #272 for 3.0