Change spec of ops.OpUpdateParagraphStyle to take real ODF properties as payload for "setProperties"

This commit is contained in:
Friedrich W. H. Kossebau
2013-07-29 16:10:52 +02:00
parent a0dbe10cbb
commit ac2cb8c005
4 changed files with 126 additions and 148 deletions
@@ -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.<!{propertyName:string,attributeName:string,unit:string}>}
*/
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.<!{propertyName:string,attributeName:string,unit:string}>}
*/
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.<!{propertyName:string,attributeName:string,unit:string}>} 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();
+10 -10
View File
@@ -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 {
+29 -129
View File
@@ -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.<!{propertyName:string,attrNs:string,attrPrefix:string,attrLocaName:string,unit:string}>}
*/
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.<!{propertyName:string,attrNs:string,attrPrefix:string,attrLocaName:string,unit:string}>}
*/
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.<!{propertyName:string,attrNs:string,attrPrefix:string,attrLocaName:string,unit:string}>} 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.<!string>} removedPropertyNames
* @param {!Array.<!{propertyName:string,attrNs:string,attrPrefix:string,attrLocaName:string,unit:string}>} 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);
}
+6 -6
View File
@@ -564,7 +564,7 @@
<office:text><text:p/></office:text>
</before>
<ops>
<op optype="UpdateParagraphStyle" memberid="Alice" styleName="S"><setProperties><textProperties fontSize="8"/></setProperties></op>
<op optype="UpdateParagraphStyle" memberid="Alice" styleName="S"><setProperties><style:text-properties fo:font-size="8pt"/></setProperties></op>
</ops>
<after>
<office:styles><style:style style:name="S" style:family="paragraph" style:class="text"><style:text-properties fo:font-size="8pt"></style:text-properties></style:style></office:styles>
@@ -577,7 +577,7 @@
<office:text><text:p/></office:text>
</before>
<ops>
<op optype="UpdateParagraphStyle" memberid="Alice" styleName="S"><setProperties><textProperties fontSize="8"/></setProperties></op>
<op optype="UpdateParagraphStyle" memberid="Alice" styleName="S"><setProperties><style:text-properties fo:font-size="8pt"/></setProperties></op>
</ops>
<after>
<office:styles><style:style style:name="S" style:family="paragraph" style:class="text"><style:text-properties fo:font-size="8pt"/></style:style></office:styles>
@@ -591,7 +591,7 @@
<office:text><text:p/></office:text>
</before>
<ops>
<op optype="UpdateParagraphStyle" memberid="Alice" styleName="S"><removedProperties><textPropertyNames>fontSize</textPropertyNames></removedProperties></op>
<op optype="UpdateParagraphStyle" memberid="Alice" styleName="S"><removedProperties><textPropertyNames>fo:font-size</textPropertyNames></removedProperties></op>
</ops>
<after>
<office:styles><style:style style:name="S" style:family="paragraph" style:class="text"></style:style></office:styles>
@@ -601,7 +601,7 @@
<test name="UpdateParagraphStyleU_nothing_rightMargin4">
<before><office:styles><style:style style:name="S" style:family="paragraph" style:class="text"></style:style></office:styles><office:text><text:p/></office:text></before>
<ops>
<op optype="UpdateParagraphStyle" memberid="Alice" styleName="S"><setProperties><paragraphProperties rightMargin="4"/></setProperties></op>
<op optype="UpdateParagraphStyle" memberid="Alice" styleName="S"><setProperties><style:paragraph-properties fo:margin-right="4mm"/></setProperties></op>
</ops>
<after>
<office:styles><style:style style:name="S" style:family="paragraph" style:class="text"><style:paragraph-properties fo:margin-right="4mm"/></style:style></office:styles>
@@ -611,7 +611,7 @@
<test name="UpdateParagraphStyleU_rightMargin2_rightMargin4">
<before><office:styles><style:style style:name="S" style:family="paragraph" style:class="text"><style:paragraph-properties fo:margin-right="2mm"/></style:style></office:styles><office:text><text:p/></office:text></before>
<ops>
<op optype="UpdateParagraphStyle" memberid="Alice" styleName="S"><setProperties><paragraphProperties rightMargin="4"/></setProperties></op>
<op optype="UpdateParagraphStyle" memberid="Alice" styleName="S"><setProperties><style:paragraph-properties fo:margin-right="4mm"/></setProperties></op>
</ops>
<after>
<office:styles><style:style style:name="S" style:family="paragraph" style:class="text"><style:paragraph-properties fo:margin-right="4mm"/></style:style></office:styles>
@@ -621,7 +621,7 @@
<test name="UpdateParagraphStyleU_rightMargin2_nothing">
<before><office:styles><style:style style:name="S" style:family="paragraph" style:class="text"><style:paragraph-properties fo:margin-right="2mm"/></style:style></office:styles><office:text><text:p/></office:text></before>
<ops>
<op optype="UpdateParagraphStyle" memberid="Alice" styleName="S"><removedProperties><paragraphPropertyNames>rightMargin</paragraphPropertyNames></removedProperties></op>
<op optype="UpdateParagraphStyle" memberid="Alice" styleName="S"><removedProperties><paragraphPropertyNames>fo:margin-right</paragraphPropertyNames></removedProperties></op>
</ops>
<after>
<office:styles><style:style style:name="S" style:family="paragraph" style:class="text"></style:style></office:styles>