From ac2cb8c005db770ea9e75bd8e50ee5bbd0c25d29 Mon Sep 17 00:00:00 2001 From: "Friedrich W. H. Kossebau" Date: Thu, 25 Jul 2013 22:51:55 +0200 Subject: [PATCH] Change spec of ops.OpUpdateParagraphStyle to take real ODF properties as payload for "setProperties" --- .../editor/widgets/paragraphStylesDialog.js | 84 +++++++++- webodf/lib/odf/Formatting.js | 20 +-- webodf/lib/ops/OpUpdateParagraphStyle.js | 158 ++++-------------- webodf/tests/ops/operationtests.xml | 12 +- 4 files changed, 126 insertions(+), 148 deletions(-) diff --git a/programs/editor/widgets/paragraphStylesDialog.js b/programs/editor/widgets/paragraphStylesDialog.js index eccdd169..9e19552f 100644 --- a/programs/editor/widgets/paragraphStylesDialog.js +++ b/programs/editor/widgets/paragraphStylesDialog.js @@ -65,12 +65,90 @@ define("webodf/editor/widgets/paragraphStylesDialog", [], function () { deleteButton, cloneTooltip, cloneDropDown, - newStyleName = null; + newStyleName = null, + /** + * Mapping of the properties from edit pane properties to the attributes of style:text-properties + * @const@type{Array.} + */ + textPropertyMapping = [ + { + propertyName: 'fontSize', + attributeName: 'fo:font-size', + unit: 'pt' + }, { + propertyName: 'fontName', + attributeName: 'style:font-name' + }, { + propertyName: 'color', + attributeName: 'fo:color' + }, { + propertyName: 'backgroundColor', + attributeName: 'fo:background-color' + }, { + propertyName: 'fontWeight', + attributeName: 'fo:font-weight' + }, { + propertyName: 'fontStyle', + attributeName: 'fo:font-style' + }, { + propertyName: 'underline', + attributeName: 'style:text-underline-style' + }, { + propertyName: 'strikethrough', + attributeName: 'style:text-line-through-style' + }], + /** + * Mapping of the properties from edit pane properties to the attributes of style:paragraph-properties + * @const@type{Array.} + */ + paragraphPropertyMapping = [ + { + propertyName: 'topMargin', + attributeName: 'fo:margin-top', + unit: 'mm' + }, { + propertyName: 'bottomMargin', + attributeName: 'fo:margin-bottom', + unit: 'mm' + }, { + propertyName: 'leftMargin', + attributeName: 'fo:margin-left', + unit: 'mm' + }, { + propertyName: 'rightMargin', + attributeName: 'fo:margin-right', + unit: 'mm' + }, { + propertyName: 'textAlign', + attributeName: 'fo:text-align' + }]; + + /** + * Sets attributes of a node by the properties of the object properties, + * based on the mapping defined in propertyMapping. + * @param {!Object} properties + * @param {!Array.} propertyMapping + * @return {undefined} + */ + function mappedProperties(properties, propertyMapping) { + var i, m, value, + result = {}; + for (i = 0; i < propertyMapping.length; i += 1) { + m = propertyMapping[i]; + value = properties[m.propertyName]; + // Set a value as the attribute of a node, if that value is defined. + // If there is a unit specified, it is suffixed to the value. + if (value !== undefined) { + result[m.attributeName] = (m.unit !== undefined) ? value + m.unit : value; + } + } + return result; + } function accept() { editorSession.updateParagraphStyle(stylePicker.value(), { - paragraphProperties: alignmentPane.value(), - textProperties: fontEffectsPane.value() + "style:paragraph-properties": mappedProperties(alignmentPane.value(), paragraphPropertyMapping), + "style:text-properties": mappedProperties(fontEffectsPane.value(), textPropertyMapping) }); dialog.hide(); diff --git a/webodf/lib/odf/Formatting.js b/webodf/lib/odf/Formatting.js index 8c778378..bb1e13e0 100644 --- a/webodf/lib/odf/Formatting.js +++ b/webodf/lib/odf/Formatting.js @@ -268,18 +268,18 @@ odf.Formatting = function Formatting() { this.getStyleAttributes = getStyleAttributes; /** - * Maps attributes and elements in the info object over top of the node. Supports + * Maps attributes and elements in the properties object over top of the node. Supports * recursion and deep mapping. This is effectively the inverse of getStyleAttributes * @param {!Element} node - * @param {!Object} info + * @param {!Object} properties */ - function mapObjOntoNode(node, info) { - Object.keys(info).forEach(function(key) { + function mapObjOntoNode(node, properties) { + Object.keys(properties).forEach(function(key) { var parts = key.split(":"), prefix = parts[0], localName = parts[1], ns = odf.Namespaces.resolvePrefix(prefix), - value = info[key], + value = properties[key], element; if (typeof value === "object" && Object.keys(value).length) { @@ -511,17 +511,17 @@ odf.Formatting = function Formatting() { } /** - * Overrides the specific properties on the styleNode from the values in the supplied info Object. + * Overrides the specific properties on the styleNode from the values in the supplied properties Object. * If a newStylePrefix is supplied, this method will automatically generate a unique name for the style node * @param {!Element} styleNode - * @param {!Object} info + * @param {!Object} properties * @param {string=} newStylePrefix Prefix to put in front of new auto styles */ - this.updateStyle = function(styleNode, info, newStylePrefix) { + this.updateStyle = function(styleNode, properties, newStylePrefix) { var name, existingNames, startIndex; - mapObjOntoNode(styleNode, info); - name = styleNode.getAttributeNS(stylens, "name"); + mapObjOntoNode(styleNode, properties); if (newStylePrefix) { + name = styleNode.getAttributeNS(stylens, "name"); existingNames = getAllStyleNames(); startIndex = 0; do { diff --git a/webodf/lib/ops/OpUpdateParagraphStyle.js b/webodf/lib/ops/OpUpdateParagraphStyle.js index 34b42063..07e3a77e 100644 --- a/webodf/lib/ops/OpUpdateParagraphStyle.js +++ b/webodf/lib/ops/OpUpdateParagraphStyle.js @@ -33,7 +33,9 @@ * @source: http://gitorious.org/webodf/webodf/ */ -/*global ops*/ +/*global runtime, odf, ops*/ + +runtime.loadClass("odf.Formatting"); /** * @constructor @@ -43,128 +45,24 @@ ops.OpUpdateParagraphStyle = function OpUpdateParagraphStyle() { "use strict"; var memberid, timestamp, styleName, - /**@type{{paragraphProperties,textProperties}}*/setProperties, + /**@type{Object}*/setProperties, + formatting = new odf.Formatting(), /**@type{{paragraphPropertyNames,textPropertyNames}}*/removedProperties, - /**@const*/fons = "urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0", /**@const*/stylens = "urn:oasis:names:tc:opendocument:xmlns:style:1.0", - /**@const*/svgns = "urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0", - /** - * Mapping of the properties from info.textProperties to the attributes of style:text-properties - * @const@type{Array.} - */ - textPropertyMapping = [ - { - propertyName: 'fontSize', - attrNs: fons, - attrPrefix: 'fo', - attrLocaName: 'font-size', - unit: 'pt' - }, { - propertyName: 'fontName', - attrNs: stylens, - attrPrefix: 'style', - attrLocaName: 'font-name' - }, { - propertyName: 'color', - attrNs: fons, - attrPrefix: 'fo', - attrLocaName: 'color' - }, { - propertyName: 'backgroundColor', - attrNs: fons, - attrPrefix: 'fo', - attrLocaName: 'background-color' - }, { - propertyName: 'fontWeight', - attrNs: fons, - attrPrefix: 'fo', - attrLocaName: 'font-weight' - }, { - propertyName: 'fontStyle', - attrNs: fons, - attrPrefix: 'fo', - attrLocaName: 'font-style' - }, { - propertyName: 'underline', - attrNs: stylens, - attrPrefix: 'style', - attrLocaName: 'text-underline-style' - }, { - propertyName: 'strikethrough', - attrNs: stylens, - attrPrefix: 'style', - attrLocaName: 'text-line-through-style' - }], - /** - * Mapping of the properties from info.paragraphProperties to the attributes of style:paragraph-properties - * @const@type{Array.} - */ - paragraphPropertyMapping = [ - { - propertyName: 'topMargin', - attrNs: fons, - attrPrefix: 'fo', - attrLocaName: 'margin-top', - unit: 'mm' - }, { - propertyName: 'bottomMargin', - attrNs: fons, - attrPrefix: 'fo', - attrLocaName: 'margin-bottom', - unit: 'mm' - }, { - propertyName: 'leftMargin', - attrNs: fons, - attrPrefix: 'fo', - attrLocaName: 'margin-left', - unit: 'mm' - }, { - propertyName: 'rightMargin', - attrNs: fons, - attrPrefix: 'fo', - attrLocaName: 'margin-right', - unit: 'mm' - }, { - propertyName: 'textAlign', - attrNs: fons, - attrPrefix: 'fo', - attrLocaName: 'text-align' - }]; + /**@const*/svgns = "urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0"; /** - * Sets attributes of a node by the properties of the object properties, - * based on the mapping defined in propertyMapping. - * @param {!Node} node - * @param {!Object} properties - * @param {!Array.} propertyMapping - */ - function setPropertiesInStyleNode(node, properties, propertyMapping) { - var i, m, value; - for (i = 0; i < propertyMapping.length; i += 1) { - m = propertyMapping[i]; - value = properties[m.propertyName]; - // Set a value as the attribute of a node, if that value is defined. - // If there is a unit specified, it is suffixed to the value. - if (value !== undefined) { - node.setAttributeNS(m.attrNs, m.attrPrefix+":"+m.attrLocaName, (m.unit !== undefined) ? value + m.unit : value); - } - } - } - - /** - * Removes attributes of a node by the names listed in removedPropertyNames, - * based on the mapping defined in propertyMapping. + * Removes attributes of a node by the names listed in removedPropertyNames. * @param {!Node} node * @param {!Array.} removedPropertyNames - * @param {!Array.} propertyMapping */ - function removePropertiesFromStyleNode(node, removedPropertyNames, propertyMapping) { - var i, m; - for (i = 0; i < propertyMapping.length; i += 1) { - m = propertyMapping[i]; - if (removedPropertyNames.indexOf(m.propertyName) !== -1) { - node.removeAttributeNS(m.attrNs, m.attrLocaName); - } + function removePropertiesFromStyleNode(node, removedPropertyNames) { + var i, propertyNameParts; + + for (i = 0; i < removedPropertyNames.length; i += 1) { + propertyNameParts = removedPropertyNames[i].split(":"); + // TODO: ensure all used prefixes have a namespaces listed + node.removeAttributeNS(odf.Namespaces.resolvePrefix(propertyNameParts[0]), propertyNameParts[1]); } } @@ -177,8 +75,9 @@ ops.OpUpdateParagraphStyle = function OpUpdateParagraphStyle() { }; this.execute = function (odtDocument) { - var styleNode, paragraphPropertiesNode, textPropertiesNode, fontFaceNode; + var styleNode, paragraphPropertiesNode, textPropertiesNode, fontFaceNode, fontName; + formatting.setOdfContainer(odtDocument.getOdfCanvas().odfContainer()); styleNode = odtDocument.getParagraphStyleElement(styleName); if (styleNode) { @@ -188,39 +87,40 @@ ops.OpUpdateParagraphStyle = function OpUpdateParagraphStyle() { if (setProperties) { // ensure nodes if needed if ((paragraphPropertiesNode === undefined) - && setProperties.paragraphProperties) { + && setProperties["style:paragraph-properties"]) { paragraphPropertiesNode = odtDocument.getDOM().createElementNS(stylens, 'style:paragraph-properties'); styleNode.appendChild(paragraphPropertiesNode); } if ((textPropertiesNode === undefined) - && setProperties.textProperties) { + && setProperties["style:text-properties"]) { textPropertiesNode = odtDocument.getDOM().createElementNS(stylens, 'style:text-properties'); styleNode.appendChild(textPropertiesNode); } // set attributes in the style nodes - if (setProperties.paragraphProperties) { - setPropertiesInStyleNode(paragraphPropertiesNode, setProperties.paragraphProperties, paragraphPropertyMapping); + if (setProperties["style:paragraph-properties"]) { + formatting.updateStyle(paragraphPropertiesNode, setProperties["style:paragraph-properties"]); } - if (setProperties.textProperties) { + if (setProperties["style:text-properties"]) { // Declare the requested font if it is not already declared - if (setProperties.textProperties.fontName && - !odtDocument.getOdfCanvas().getFormatting().getFontMap().hasOwnProperty(setProperties.textProperties.fontName)) { + fontName = setProperties["style:text-properties"]["style:font-name"]; + if (fontName && + !odtDocument.getOdfCanvas().getFormatting().getFontMap().hasOwnProperty(fontName)) { fontFaceNode = odtDocument.getDOM().createElementNS(stylens, 'style:font-face'); - fontFaceNode.setAttributeNS(stylens, 'style:name', setProperties.textProperties.fontName); - fontFaceNode.setAttributeNS(svgns, 'svg:font-family', setProperties.textProperties.fontName); + fontFaceNode.setAttributeNS(stylens, 'style:name', fontName); + fontFaceNode.setAttributeNS(svgns, 'svg:font-family', fontName); odtDocument.getOdfCanvas().odfContainer().rootElement.fontFaceDecls.appendChild(fontFaceNode); } - setPropertiesInStyleNode(textPropertiesNode, setProperties.textProperties, textPropertyMapping); + formatting.updateStyle(textPropertiesNode, setProperties["style:text-properties"]); } } // remove attributes in the style nodes if (removedProperties) { if (removedProperties.paragraphPropertyNames) { - removePropertiesFromStyleNode(paragraphPropertiesNode, removedProperties.paragraphPropertyNames, paragraphPropertyMapping); + removePropertiesFromStyleNode(paragraphPropertiesNode, removedProperties.paragraphPropertyNames); if (paragraphPropertiesNode.attributes.length === 0) { styleNode.removeChild(paragraphPropertiesNode); } @@ -228,7 +128,7 @@ ops.OpUpdateParagraphStyle = function OpUpdateParagraphStyle() { if (removedProperties.textPropertyNames) { // TODO: check if fontname can be removed from font-face-declaration - removePropertiesFromStyleNode(textPropertiesNode, removedProperties.textPropertyNames, textPropertyMapping); + removePropertiesFromStyleNode(textPropertiesNode, removedProperties.textPropertyNames); if (textPropertiesNode.attributes.length === 0) { styleNode.removeChild(textPropertiesNode); } diff --git a/webodf/tests/ops/operationtests.xml b/webodf/tests/ops/operationtests.xml index 821f49a3..66b52ac2 100644 --- a/webodf/tests/ops/operationtests.xml +++ b/webodf/tests/ops/operationtests.xml @@ -564,7 +564,7 @@ - + @@ -577,7 +577,7 @@ - + @@ -591,7 +591,7 @@ - fontSize + fo:font-size @@ -601,7 +601,7 @@ - + @@ -611,7 +611,7 @@ - + @@ -621,7 +621,7 @@ - rightMargin + fo:margin-right