Merge pull request #2540 from CatoTH/diff-formattings-in-pdf

Colors / text-Decorations for insered / deleted text
This commit is contained in:
Norman Jäckel 2016-11-01 22:55:25 +01:00 committed by GitHub
commit 3c48df1245

View File

@ -165,6 +165,11 @@ angular.module('OpenSlidesApp.core.pdf', [])
var createInstance = function(images, fonts, pdfMake) { var createInstance = function(images, fonts, pdfMake) {
var slice = Function.prototype.call.bind([].slice), var slice = Function.prototype.call.bind([].slice),
map = Function.prototype.call.bind([].map), map = Function.prototype.call.bind([].map),
DIFF_MODE_NORMAL = 0,
DIFF_MODE_INSERT = 1,
DIFF_MODE_DELETE = 2,
/** /**
* Adds a custom font to pdfMake.vfs * Adds a custom font to pdfMake.vfs
* @function * @function
@ -226,6 +231,10 @@ angular.module('OpenSlidesApp.core.pdf', [])
"h6": ["font-size:20"], "h6": ["font-size:20"],
"a": ["color:blue", "text-decoration:underline"] "a": ["color:blue", "text-decoration:underline"]
}, },
classStyles = {
"delete": ["color:red", "text-decoration:line-through"],
"insert": ["color:green", "text-decoration:underline"]
},
/** /**
* Parses Children of the current paragraph * Parses Children of the current paragraph
* @function * @function
@ -233,13 +242,14 @@ angular.module('OpenSlidesApp.core.pdf', [])
* @param {object} element - * @param {object} element -
* @param {object} currentParagraph - * @param {object} currentParagraph -
* @param {object} styles - * @param {object} styles -
* @param {number} diff_mode
*/ */
parseChildren = function(converted, element, currentParagraph, styles) { parseChildren = function(converted, element, currentParagraph, styles, diff_mode) {
var elements = []; var elements = [];
var children = element.childNodes; var children = element.childNodes;
if (children.length !== 0) { if (children.length !== 0) {
_.forEach(children, function(child) { _.forEach(children, function(child) {
currentParagraph = ParseElement(elements, child, currentParagraph, styles); currentParagraph = ParseElement(elements, child, currentParagraph, styles, diff_mode);
}); });
} }
if (elements.length !== 0) { if (elements.length !== 0) {
@ -318,8 +328,9 @@ angular.module('OpenSlidesApp.core.pdf', [])
* @param {object} element - * @param {object} element -
* @param {object} currentParagraph - * @param {object} currentParagraph -
* @param {object} styles - * @param {object} styles -
* @param {number} diff_mode
*/ */
ParseElement = function(alreadyConverted, element, currentParagraph, styles) { ParseElement = function(alreadyConverted, element, currentParagraph, styles, diff_mode) {
styles = styles || []; styles = styles || [];
if (element.getAttribute) { if (element.getAttribute) {
var nodeStyle = element.getAttribute("style"); var nodeStyle = element.getAttribute("style");
@ -329,6 +340,22 @@ angular.module('OpenSlidesApp.core.pdf', [])
styles.push(tmp); styles.push(tmp);
}); });
} }
var nodeClass = element.getAttribute("class");
if (nodeClass) {
nodeClass.split(" ").forEach(function(nodeClass) {
if (typeof(classStyles[nodeClass]) != 'undefined') {
classStyles[nodeClass].forEach(function(style) {
styles.push(style);
});
}
if (nodeClass == 'insert') {
diff_mode = DIFF_MODE_INSERT;
}
if (nodeClass == 'delete') {
diff_mode = DIFF_MODE_DELETE;
}
});
}
} }
var nodeName = element.nodeName.toLowerCase(); var nodeName = element.nodeName.toLowerCase();
switch (nodeName) { switch (nodeName) {
@ -341,7 +368,7 @@ angular.module('OpenSlidesApp.core.pdf', [])
currentParagraph = create("text"); currentParagraph = create("text");
/* falls through */ /* falls through */
case "a": case "a":
parseChildren(alreadyConverted, element, currentParagraph, styles.concat(elementStyles[nodeName])); parseChildren(alreadyConverted, element, currentParagraph, styles.concat(elementStyles[nodeName]), diff_mode);
alreadyConverted.push(currentParagraph); alreadyConverted.push(currentParagraph);
break; break;
case "b": case "b":
@ -349,7 +376,7 @@ angular.module('OpenSlidesApp.core.pdf', [])
case "u": case "u":
case "em": case "em":
case "i": case "i":
parseChildren(alreadyConverted, element, currentParagraph, styles.concat(elementStyles[nodeName])); parseChildren(alreadyConverted, element, currentParagraph, styles.concat(elementStyles[nodeName]), diff_mode);
break; break;
case "table": case "table":
var t = create("table", { var t = create("table", {
@ -361,7 +388,7 @@ angular.module('OpenSlidesApp.core.pdf', [])
if (border) if (border)
if (parseInt(border) == 1) isBorder = true; if (parseInt(border) == 1) isBorder = true;
if (!isBorder) t.layout = 'noBorders'; if (!isBorder) t.layout = 'noBorders';
parseChildren(t.table.body, element, currentParagraph, styles); parseChildren(t.table.body, element, currentParagraph, styles, diff_mode);
var widths = element.getAttribute("widths"); var widths = element.getAttribute("widths");
if (!widths) { if (!widths) {
if (t.table.body.length !== 0) { if (t.table.body.length !== 0) {
@ -376,11 +403,11 @@ angular.module('OpenSlidesApp.core.pdf', [])
alreadyConverted.push(t); alreadyConverted.push(t);
break; break;
case "tbody": case "tbody":
parseChildren(alreadyConverted, element, currentParagraph, styles); parseChildren(alreadyConverted, element, currentParagraph, styles, diff_mode);
break; break;
case "tr": case "tr":
var row = []; var row = [];
parseChildren(row, element, currentParagraph, styles); parseChildren(row, element, currentParagraph, styles, diff_mode);
alreadyConverted.push(row); alreadyConverted.push(row);
break; break;
case "td": case "td":
@ -393,22 +420,29 @@ angular.module('OpenSlidesApp.core.pdf', [])
var cspan = element.getAttribute("colspan"); var cspan = element.getAttribute("colspan");
if (cspan) if (cspan)
st.colSpan = parseInt(cspan); st.colSpan = parseInt(cspan);
parseChildren(st.stack, element, currentParagraph, styles); parseChildren(st.stack, element, currentParagraph, styles, diff_mode);
alreadyConverted.push(st); alreadyConverted.push(st);
break; break;
case "span": case "span":
if (scope.lineNumberMode == "inline") { if (scope.lineNumberMode == "inline") {
var lineNumberInline = element.getAttribute("data-line-number"), if (diff_mode != DIFF_MODE_INSERT) {
lineNumberObjInline = { var lineNumberInline = element.getAttribute("data-line-number"),
text: lineNumberInline, lineNumberObjInline = {
color: "gray", text: lineNumberInline,
fontSize: 5 color: "gray",
}; fontSize: 5
currentParagraph.text.push(lineNumberObjInline); };
parseChildren(alreadyConverted, element, currentParagraph, styles); currentParagraph.text.push(lineNumberObjInline);
}
parseChildren(alreadyConverted, element, currentParagraph, styles, diff_mode);
} else if (scope.lineNumberMode == "outside") { } else if (scope.lineNumberMode == "outside") {
var lineNumberOutline = element.getAttribute("data-line-number"), var lineNumberOutline;
lineNumberObject = { if (diff_mode == DIFF_MODE_INSERT) {
lineNumberOutline = "";
} else {
lineNumberOutline = element.getAttribute("data-line-number");
}
var lineNumberObject = {
width: 20, width: 20,
text: lineNumberOutline, text: lineNumberOutline,
color: "gray", color: "gray",
@ -422,10 +456,10 @@ angular.module('OpenSlidesApp.core.pdf', [])
}; };
currentParagraph = create("text"); currentParagraph = create("text");
col.columns.push(currentParagraph); col.columns.push(currentParagraph);
parseChildren(col.columns[0], element, currentParagraph, styles); parseChildren(col.columns[0], element, currentParagraph, styles, diff_mode);
alreadyConverted.push(col); alreadyConverted.push(col);
} else { } else {
parseChildren(alreadyConverted, element, currentParagraph, styles); parseChildren(alreadyConverted, element, currentParagraph, styles, diff_mode);
} }
break; break;
case "br": case "br":
@ -441,7 +475,7 @@ angular.module('OpenSlidesApp.core.pdf', [])
var stackDiv = create("stack"); var stackDiv = create("stack");
stackDiv.stack.push(currentParagraph); stackDiv.stack.push(currentParagraph);
ComputeStyle(stackDiv, styles); ComputeStyle(stackDiv, styles);
parseChildren(stackDiv.stack, element, currentParagraph); parseChildren(stackDiv.stack, element, currentParagraph, [], diff_mode);
alreadyConverted.push(stackDiv); alreadyConverted.push(stackDiv);
break; break;
case "p": case "p":
@ -450,7 +484,7 @@ angular.module('OpenSlidesApp.core.pdf', [])
var stackP = create("stack"); var stackP = create("stack");
stackP.stack.push(currentParagraph); stackP.stack.push(currentParagraph);
ComputeStyle(stackP, styles); ComputeStyle(stackP, styles);
parseChildren(stackP.stack, element, currentParagraph); parseChildren(stackP.stack, element, currentParagraph, [], diff_mode);
alreadyConverted.push(stackP); alreadyConverted.push(stackP);
break; break;
case "img": case "img":
@ -484,12 +518,12 @@ angular.module('OpenSlidesApp.core.pdf', [])
break; break;
case "ul": case "ul":
var u = create("ul"); var u = create("ul");
parseChildren(u.ul, element, currentParagraph, styles); parseChildren(u.ul, element, currentParagraph, styles, diff_mode);
alreadyConverted.push(u); alreadyConverted.push(u);
break; break;
case "ol": case "ol":
var o = create("ol"); var o = create("ol");
parseChildren(o.ol, element, currentParagraph, styles); parseChildren(o.ol, element, currentParagraph, styles, diff_mode);
alreadyConverted.push(o); alreadyConverted.push(o);
break; break;
default: default:
@ -519,7 +553,7 @@ angular.module('OpenSlidesApp.core.pdf', [])
html = $(html.replace(/\t/g, "").replace(/\n/g, "")); html = $(html.replace(/\t/g, "").replace(/\n/g, ""));
var emptyParagraph = create("text"); var emptyParagraph = create("text");
slice(html).forEach(function(element) { slice(html).forEach(function(element) {
ParseElement(converted, element); ParseElement(converted, element, null, [], DIFF_MODE_NORMAL);
}); });
}, },
content = []; content = [];