diff --git a/VisualEditor.php b/VisualEditor.php index 40aa3dac67..7f8a3abe42 100644 --- a/VisualEditor.php +++ b/VisualEditor.php @@ -726,6 +726,7 @@ $wgResourceModules += array( 'modules/ve-mw/dm/nodes/ve.dm.MWEntityNode.js', 'modules/ve-mw/dm/nodes/ve.dm.MWExtensionNode.js', + 'modules/ve-mw/dm/nodes/ve.dm.MWTableNode.js', 'modules/ve-mw/dm/annotations/ve.dm.MWNowikiAnnotation.js', @@ -734,6 +735,7 @@ $wgResourceModules += array( // ce 'modules/ve-mw/ce/nodes/ve.ce.MWEntityNode.js', 'modules/ve-mw/ce/nodes/ve.ce.MWExtensionNode.js', + 'modules/ve-mw/ce/nodes/ve.ce.MWTableNode.js', 'modules/ve-mw/ce/annotations/ve.ce.MWNowikiAnnotation.js', diff --git a/modules/ve-mw/ce/nodes/ve.ce.MWTableNode.js b/modules/ve-mw/ce/nodes/ve.ce.MWTableNode.js new file mode 100644 index 0000000000..f76656b5dc --- /dev/null +++ b/modules/ve-mw/ce/nodes/ve.ce.MWTableNode.js @@ -0,0 +1,48 @@ +/*! + * VisualEditor ContentEditable MWTableNode class. + * + * @copyright 2011-2014 VisualEditor Team and others; see AUTHORS.txt + * @license The MIT License (MIT); see LICENSE.txt + */ + +/** + * ContentEditable MW table node. + * + * @class + * @extends ve.ce.TableNode + * + * @constructor + * @param {ve.dm.MWTableNode} model Model to observe + * @param {Object} [config] Configuration options + */ +ve.ce.MWTableNode = function VeCeMWTableNode() { + // Parent constructor + ve.ce.MWTableNode.super.apply( this, arguments ); + + this.model.connect( this, { attributeChange: 'onAttributeChange' } ); +}; + +/* Inheritance */ + +OO.inheritClass( ve.ce.MWTableNode, ve.ce.TableNode ); + +/* Static Properties */ + +ve.ce.MWTableNode.static.name = 'mwTable'; + +/* Methods */ + +ve.ce.MWTableNode.prototype.onAttributeChange = function ( key, from, to ) { + switch ( key ) { + case 'wikitable': + this.$element.toggleClass( 'wikitable', !!to ); + break; + case 'sortable': + this.$element.toggleClass( 'sortable', !!to ); + break; + } +}; + +/* Registration */ + +ve.ce.nodeFactory.register( ve.ce.MWTableNode ); diff --git a/modules/ve-mw/dm/nodes/ve.dm.MWTableNode.js b/modules/ve-mw/dm/nodes/ve.dm.MWTableNode.js new file mode 100644 index 0000000000..b1ca067a06 --- /dev/null +++ b/modules/ve-mw/dm/nodes/ve.dm.MWTableNode.js @@ -0,0 +1,71 @@ +/*! + * VisualEditor DataModel MWTable class. + * + * @copyright 2011-2014 VisualEditor Team and others; see AUTHORS.txt + * @license The MIT License (MIT); see LICENSE.txt + */ + +/** + * DataModel MediaWiki table node. + * + * @class + * @extends ve.dm.TableNode + * @mixins ve.dm.ClassAttributeNode + * + * @constructor + * @param {Object} [element] Reference to element in linear model + * @param {ve.dm.Node[]} [children] + */ +ve.dm.MWTableNode = function VeDmMWTableNode() { + // Parent constructor + ve.dm.MWTableNode.super.apply( this, arguments ); + + // Mixin constructors + ve.dm.ClassAttributeNode.call( this ); +}; + +/* Inheritance */ + +OO.inheritClass( ve.dm.MWTableNode, ve.dm.TableNode ); + +OO.mixinClass( ve.dm.MWTableNode, ve.dm.ClassAttributeNode ); + +/* Static Properties */ + +ve.dm.MWTableNode.static.name = 'mwTable'; + +ve.dm.MWTableNode.static.classAttributes = { + wikitable: { wikitable: true }, + sortable: { sortable: true } +}; + +// HACK: users of parentNodeTypes should be fixed to check for inherited classes. +ve.dm.TableSectionNode.static.parentNodeTypes.push( 'mwTable' ); + +ve.dm.MWTableNode.static.toDataElement = function ( domElements ) { + var attributes = {}, + dataElement = { type: this.name }, + classAttr = domElements[0].getAttribute( 'class' ); + + this.setClassAttributes( attributes, classAttr ); + + if ( !ve.isEmptyObject( attributes ) ) { + dataElement.attributes = attributes; + } + return dataElement; +}; + +ve.dm.MWTableNode.static.toDomElements = function ( dataElement, doc ) { + var element = doc.createElement( 'table' ), + classAttr = this.getClassAttrFromAttributes( dataElement.attributes ); + + if ( classAttr ) { + element.className = classAttr; + } + + return [ element ]; +}; + +/* Registration */ + +ve.dm.modelRegistry.register( ve.dm.MWTableNode ); diff --git a/modules/ve-mw/ui/ve.ui.MWCommandRegistry.js b/modules/ve-mw/ui/ve.ui.MWCommandRegistry.js index 2fc4e2f7dc..f9ca8b5be9 100644 --- a/modules/ve-mw/ui/ve.ui.MWCommandRegistry.js +++ b/modules/ve-mw/ui/ve.ui.MWCommandRegistry.js @@ -67,3 +67,12 @@ ve.ui.commandRegistry.register( ve.ui.commandRegistry.register( new ve.ui.Command( 'preformatted', 'format', 'convert', 'mwPreformatted' ) ); +ve.ui.commandRegistry.register( + new ve.ui.Command( 'insertTable', 'table', 'create', { + header: true, + rows: 3, + cols: 4, + type: 'mwTable', + attributes: { wikitable: true } + } ) +);