js/main.js
// This is where the magic happens.
This is where the magic happens.
This is where the magic happens .
function parseFile(file) {
    if (tests.filereader === true && acceptedTypes[file.type] === true) {
      var reader = new FileReader();
      reader.onload = function(event) {
        holder.innerHTML = event.target.result;
        var svgNode = $(holder).find("svg"),
        result = event.target.result,
        items = [],
        svgTag = result.match('<svg[^>]*>'),
        svgHead = svgTag[0],
        svgFoot = "</svg>";
        while (svgNode.children().length < 2) {
          svgNode = svgNode.children();
        }
        let styleLookup = {};
        for (var i = 0, length = svgNode.children().length; i < length; i++) {
          var node = svgNode.children().eq(i)[0],
            id = svgNode.children().eq(i).removeAttr("display").attr("id") || i,
            nodeText = serializer.serializeToString(node),
            nodeName = node.nodeName;
          if (nodeName === 'style') {
            let styles = node.innerHTML;
            let styleClasses = styles.split('}');
            styleClasses.forEach(function (styleClass) {
              let matches = /\.(.*?)({.*)/.exec(styleClass.trim());
              if (matches) {
                let className = matches[1];
                let styleRules = matches[2] + '}';
                styleLookup[className] = styleRules;
              }
            });
          } else {
            let classes = [];
            let classList = getAllMatches(node.innerHTML, /class="([^"]+)"/g);
            classList.forEach(function (styleListItem) {
              classes = classes.concat(styleListItem.split(' '));
            });
            var uniqueClasses = onlyUnique(classes);
            var nodeStyles = '';
            if (uniqueClasses.length) {
              nodeStyles = '<style type="text/css">';
              uniqueClasses.forEach(function (uniqueClass) {
                nodeStyles += '.' + uniqueClass + styleLookup[uniqueClass];
              });
              nodeStyles += '</style>';
            }
            nodeText = svgHead + nodeStyles + nodeText + svgFoot;
            zip.file(id + ".svg", nodeText);
            if ($('#enable-preview').is(':checked')) {
              items[i] = "<div>" + nodeText + "</div>";
              $("#split-svgs").append(items[i]);
            }
          }
        }
        const len = Object.keys(zip.files)
        let newZip = []
        let index = -1
        len.forEach((k, i) => {
          if (i % 100 == 0) {
            newZip.push(new JSZip())
            index++
          }
          newZip[index].file(k, zip.files[k].asText())
        })
        newZip.forEach((z, i) => {
          console.log(i)
          location.href = `data:application/zip;base64,${z.generate()}`
        })
      };
      reader.readAsText(file);
    } else {
      holder.innerHTML += '<p>Uploaded ' + file.name + ' ' + (file.size ? (file.size / 1024 | 0) + 'K' : '');
    }
  }
function parseFile ( file ) { if ( tests . filereader === true && acceptedTypes [ file . type ] === true ) { var reader = new FileReader ( ) ; reader . onload = function ( event ) { holder . innerHTML = event . target . result ; var svgNode = $ ( holder ) . find ( "svg" ) , result = event . target . result , items = [ ] , svgTag = result . match ( '<svg[^>]*>' ) , svgHead = svgTag [ 0 ] , svgFoot = "</svg>" ; while ( svgNode . children ( ) . length < 2 ) { svgNode = svgNode . children ( ) ; } let styleLookup = { } ; for ( var i = 0 , length = svgNode . children ( ) . length ; i < length ; i ++ ) { var node = svgNode . children ( ) . eq ( i ) [ 0 ] , id = svgNode . children ( ) . eq ( i ) . removeAttr ( "display" ) . attr ( "id" ) || i , nodeText = serializer . serializeToString ( node ) , nodeName = node . nodeName ; if ( nodeName === 'style' ) { let styles = node . innerHTML ; let styleClasses = styles . split ( '}' ) ; styleClasses . forEach ( function ( styleClass ) { let matches = / \.(.*?)({.*) / . exec ( styleClass . trim ( ) ) ; if ( matches ) { let className = matches [ 1 ] ; let styleRules = matches [ 2 ] + '}' ; styleLookup [ className ] = styleRules ; } } ) ; } else { let classes = [ ] ; let classList = getAllMatches ( node . innerHTML , / class="([^"]+)" / g ) ; classList . forEach ( function ( styleListItem ) { classes = classes . concat ( styleListItem . split ( ' ' ) ) ; } ) ; var uniqueClasses = onlyUnique ( classes ) ; var nodeStyles = '' ; if ( uniqueClasses . length ) { nodeStyles = '<style type="text/css">' ; uniqueClasses . forEach ( function ( uniqueClass ) { nodeStyles += '.' + uniqueClass + styleLookup [ uniqueClass ] ; } ) ; nodeStyles += '</style>' ; } nodeText = svgHead + nodeStyles + nodeText + svgFoot ; zip . file ( id + ".svg" , nodeText ) ; if ( $ ( '#enable-preview' ) . is ( ':checked' ) ) { items [ i ] = "<div>" + nodeText + "</div>" ; $ ( "#split-svgs" ) . append ( items [ i ] ) ; } } } const len = Object . keys ( zip . files ) let newZip = [ ] let index = - 1 len . forEach ( ( k , i ) => { if ( i % 100 == 0 ) { newZip . push ( new JSZip ( ) ) index ++ } newZip [ index ] . file ( k , zip . files [ k ] . asText ( ) ) } ) newZip . forEach ( ( z , i ) => { console . log ( i ) location . href = ` ${ z . generate ( ) } ` } ) } ; reader . readAsText ( file ) ; } else { holder . innerHTML += '<p>Uploaded ' + file . name + ' ' + ( file . size ? ( file . size / 1024 | 0 ) + 'K' : '' ) ; } }
This is where the magic happens.
This is where the magic happens .
// Store styles in dictionary // Add file previews below if enable-preview is checked. // Handle more than 100 files.