diff options
author | Jacob Schatz <jschatz1@gmail.com> | 2017-04-14 07:41:44 +0300 |
---|---|---|
committer | Jacob Schatz <jschatz1@gmail.com> | 2017-04-14 07:41:44 +0300 |
commit | 8687843895fa5ff91fe8c9acf3c023e3a816a838 (patch) | |
tree | c4a2a7e0715fccb521d1b509d2303be4b83b5222 | |
parent | f2949437caeac0d24f8747e5969e6e9c53442d97 (diff) |
Make hyperlinks work right.
-rw-r--r-- | app/assets/javascripts/blob/docx/docx.js | 31 | ||||
-rw-r--r-- | app/assets/javascripts/blob/docx/index.js | 4 |
2 files changed, 32 insertions, 3 deletions
diff --git a/app/assets/javascripts/blob/docx/docx.js b/app/assets/javascripts/blob/docx/docx.js index cb1550aded2..6cf8c39a666 100644 --- a/app/assets/javascripts/blob/docx/docx.js +++ b/app/assets/javascripts/blob/docx/docx.js @@ -8,10 +8,10 @@ export default class Docx { this.$currentLists = []; this.commonColors = {'black': '#000','blue': '#0000FF','cyan':'#00ffff','green':'#008000','magenta':'#ff00ff','red':'#FF0000','yellow':'#ffff00','white':'#FFF','darkBlue':'#00008b','darkCyan':'#008b8b','darkGreen':'#006400','darkMagenta':'#8b008b','darkRed':'#8b0000','darkYellow':'#E5E500','darkGray':'#a9a9a9','lightGray':'#d3d3d3'}; this.styles = {}; + this.relationships = {}; } setStyles(styles) { - console.log('set styles') const $xml = $($.parseXML(styles)); const $styles = $xml.find('style'); const attrs = ['b', 'color', 'sz', 'i', 'u']; @@ -28,6 +28,21 @@ export default class Docx { }); } + setRelationships(relationships) { + const $xml = $($.parseXML(relationships)); + const $relationships = $xml.find('Relationship'); + $relationships.each((i, r) => { + const $r = $(r); + const id = $r.attr('Id'); + const targetMode = $r.attr('TargetMode'); + this.relationships[id] = {}; + this.relationships[id].target = $r.attr('Target'); + if(targetMode) { + this.relationships[id].targetMode = targetMode; + } + }); + } + setHexOrCommonColor(colorString) { if(this.commonColors.hasOwnProperty(colorString)){ return this.commonColors[colorString]; @@ -83,6 +98,7 @@ export default class Docx { $span = this.getUnderline($span, $r, $p); $span = this.getColor($span, $r, $p); $span = this.getHighlight($span, $r, $p); + $span = this.getHyperLink($span, $r, $p); return $span; } @@ -164,6 +180,17 @@ export default class Docx { return $el; } + getHyperLink($span, $r, $p) { + const $hyperlink = $r.parent('w\\:hyperlink'); + var $a; + if($hyperlink.length) { + $a = $span.wrap(`<a href='${this.relationships[$hyperlink.attr('r:id')].target}'></a>`).parent().get(0).outerHTML; + console.log($a) + return $($a); + } + return $span; + } + getSavedStyle($paragraph, $p) { const $savedStyle = $paragraph.find('pStyle'); var s = ''; @@ -236,7 +263,6 @@ export default class Docx { // was already in a list and will continue to be in a list } else { const newListLevel = parseInt($listInfo.find('ilvl').attr('w:val')); - console.log(newListLevel, this.currentListLevel) if(newListLevel > this.currentListLevel) { // if we just made a sublist this.$currentLists.push(this.getListByType(listType)); @@ -251,7 +277,6 @@ export default class Docx { } else if(newListLevel < this.currentListLevel) { // if we just exited a sublist this.currentListLevel = newListLevel - console.log('less than', newListLevel, this.currentListLevel); this.$currentLists.pop(); } diff --git a/app/assets/javascripts/blob/docx/index.js b/app/assets/javascripts/blob/docx/index.js index fd30b7a34ce..82968899f91 100644 --- a/app/assets/javascripts/blob/docx/index.js +++ b/app/assets/javascripts/blob/docx/index.js @@ -26,6 +26,10 @@ export default class DocxRenderer { }) .then((content) => { this.docx.setStyles(content); + return this.asyncResult.files['word/_rels/document.xml.rels'].async('string'); + }) + .then((content) => { + this.docx.setRelationships(content); this.el.appendChild(this.docx.parseDoc()); }) .catch(this.error.bind(this)); |