mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/CodeMirror
synced 2024-11-24 06:13:31 +00:00
VE: Match whitespace rendering of ve.ce.TextNode
Bug: T187694 Change-Id: Ie27e9862e0f55fcc10af91a2bdd9c5d21d49ddc4
This commit is contained in:
parent
29a3801a4b
commit
452d6d0a80
|
@ -30,6 +30,39 @@ ve.ui.CodeMirrorAction.static.name = 'codeMirror';
|
||||||
*/
|
*/
|
||||||
ve.ui.CodeMirrorAction.static.methods = [ 'toggle' ];
|
ve.ui.CodeMirrorAction.static.methods = [ 'toggle' ];
|
||||||
|
|
||||||
|
/* Static methods */
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Make whitespace replacements to match ve.ce.TextNode functionality
|
||||||
|
*
|
||||||
|
* @param {string} text Text
|
||||||
|
* @return {string} Replaced text
|
||||||
|
*/
|
||||||
|
ve.ui.CodeMirrorAction.static.fixWhitespace = ( function () {
|
||||||
|
var ws, symbol, pattern,
|
||||||
|
whitespaceHtmlCharacters = ve.ce.TextNode.static.whitespaceHtmlCharacters,
|
||||||
|
replacements = [];
|
||||||
|
|
||||||
|
// Convert whitespaceHtmlCharacters object into regex/symbol
|
||||||
|
// pairs and cache result locally.
|
||||||
|
for ( ws in whitespaceHtmlCharacters ) {
|
||||||
|
// We actally render newlines
|
||||||
|
if ( ws !== '\n' ) {
|
||||||
|
symbol = whitespaceHtmlCharacters[ ws ];
|
||||||
|
pattern = new RegExp( ws, 'g' );
|
||||||
|
replacements.push( [ pattern, symbol ] );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return function fixWhitespace( text ) {
|
||||||
|
replacements.forEach( function ( replacement ) {
|
||||||
|
text = text.replace( replacement[ 0 ], replacement[ 1 ] );
|
||||||
|
} );
|
||||||
|
return text;
|
||||||
|
};
|
||||||
|
|
||||||
|
}() );
|
||||||
|
|
||||||
/* Methods */
|
/* Methods */
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -45,11 +78,10 @@ ve.ui.CodeMirrorAction.prototype.toggle = function ( enable ) {
|
||||||
|
|
||||||
if ( !surface.mirror && enable !== false ) {
|
if ( !surface.mirror && enable !== false ) {
|
||||||
surface.mirror = CodeMirror( surfaceView.$element[ 0 ], {
|
surface.mirror = CodeMirror( surfaceView.$element[ 0 ], {
|
||||||
value: surface.getDom(),
|
value: this.constructor.static.fixWhitespace( surface.getDom() ),
|
||||||
mwConfig: mw.config.get( 'extCodeMirrorConfig' ),
|
mwConfig: mw.config.get( 'extCodeMirrorConfig' ),
|
||||||
readOnly: 'nocursor',
|
readOnly: 'nocursor',
|
||||||
lineWrapping: true,
|
lineWrapping: true,
|
||||||
tabSize: 1,
|
|
||||||
scrollbarStyle: 'null',
|
scrollbarStyle: 'null',
|
||||||
specialChars: /^$/,
|
specialChars: /^$/,
|
||||||
viewportMargin: 5,
|
viewportMargin: 5,
|
||||||
|
@ -130,7 +162,8 @@ ve.ui.CodeMirrorAction.prototype.onDocumentPrecommit = function ( tx ) {
|
||||||
replacements = [],
|
replacements = [],
|
||||||
linearData = this.surface.getModel().getDocument().data,
|
linearData = this.surface.getModel().getDocument().data,
|
||||||
store = linearData.getStore(),
|
store = linearData.getStore(),
|
||||||
mirror = this.surface.mirror;
|
mirror = this.surface.mirror,
|
||||||
|
fixWhitespace = this.constructor.static.fixWhitespace;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Convert a VE offset to a 2D CodeMirror position
|
* Convert a VE offset to a 2D CodeMirror position
|
||||||
|
@ -152,7 +185,7 @@ ve.ui.CodeMirrorAction.prototype.onDocumentPrecommit = function ( tx ) {
|
||||||
start: convertOffset( offset ),
|
start: convertOffset( offset ),
|
||||||
// Don't bother recalculating end offset if not a removal, replaceRange works with just one arg
|
// Don't bother recalculating end offset if not a removal, replaceRange works with just one arg
|
||||||
end: op.remove.length ? convertOffset( offset + op.remove.length ) : undefined,
|
end: op.remove.length ? convertOffset( offset + op.remove.length ) : undefined,
|
||||||
data: new ve.dm.ElementLinearData( store, op.insert ).getSourceText()
|
data: fixWhitespace( new ve.dm.ElementLinearData( store, op.insert ).getSourceText() )
|
||||||
} );
|
} );
|
||||||
offset += op.remove.length;
|
offset += op.remove.length;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue