| 1 |
const { |
| 2 |
Decoration, |
| 3 |
DecorationSet, |
| 4 |
Direction, |
| 5 |
EditorView, |
| 6 |
PluginSpec, |
| 7 |
Prec, |
| 8 |
RangeSet, |
| 9 |
RangeSetBuilder, |
| 10 |
ViewPlugin, |
| 11 |
ViewUpdate, |
| 12 |
syntaxTree |
| 13 |
} = require( 'ext.CodeMirror.v6.lib' ); |
| 14 |
const mwModeConfig = require( './codemirror.mediawiki.config.js' ); |
| 15 |
|
| 16 |
/** |
| 17 |
* @type {Decoration} |
| 18 |
* @private |
| 19 |
*/ |
| 20 |
const isolate = Decoration.mark( { |
| 21 |
class: 'cm-bidi-isolate', |
| 22 |
bidiIsolate: Direction.LTR |
| 23 |
} ); |
| 24 |
|
| 25 |
/** |
| 26 |
* @param {EditorView} view |
| 27 |
* @return {RangeSet} |
| 28 |
* @private |
| 29 |
*/ |
| 30 |
function computeIsolates( view ) { |
| 31 |
const set = new RangeSetBuilder(); |
| 32 |
|
| 33 |
if ( view.editorAttrs.dir === 'rtl' ) { |
| 34 |
for ( const { from, to } of view.visibleRanges ) { |
| 35 |
let startPos = null; |
| 36 |
syntaxTree( view.state ).iterate( { |
| 37 |
from, |
| 38 |
to, |
| 39 |
enter( node ) { |
| 40 |
// Determine if this is a bracket node (start or end of a tag). |
| 41 |
const isBracket = node.name.split( '_' ) |
| 42 |
.some( ( tag ) => [ |
| 43 |
mwModeConfig.tags.htmlTagBracket, |
| 44 |
mwModeConfig.tags.extTagBracket |
| 45 |
].includes( tag ) ); |
| 46 |
|
| 47 |
if ( startPos === null && isBracket ) { |
| 48 |
// If we find a bracket node, we keep track of the start position. |
| 49 |
startPos = node.from; |
| 50 |
} else if ( isBracket ) { |
| 51 |
// When we find the closing bracket, add the isolate. |
| 52 |
set.add( startPos, node.to, isolate ); |
| 53 |
startPos = null; |
| 54 |
} |
| 55 |
} |
| 56 |
} ); |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
return set.finish(); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* @private |
| 65 |
*/ |
| 66 |
class CodeMirrorBidiIsolation { |
| 67 |
/** |
| 68 |
* @constructor |
| 69 |
* @param {EditorView} view The editor view. |
| 70 |
*/ |
| 71 |
constructor( view ) { |
| 72 |
/** @type {DecorationSet} */ |
| 73 |
this.isolates = computeIsolates( view ); |
| 74 |
/** @type {Tree} */ |
| 75 |
this.tree = syntaxTree( view.state ); |
| 76 |
/** @type {Direction} */ |
| 77 |
this.dir = view.textDirection; |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* @param {ViewUpdate} update |
| 82 |
*/ |
| 83 |
update( update ) { |
| 84 |
if ( update.docChanged || update.viewportChanged || |
| 85 |
syntaxTree( update.state ) !== this.tree || |
| 86 |
update.view.textDirection !== this.dir |
| 87 |
) { |
| 88 |
this.isolates = computeIsolates( update.view ); |
| 89 |
this.tree = syntaxTree( update.state ); |
| 90 |
} |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* @type {PluginSpec} |
| 96 |
* @private |
| 97 |
*/ |
| 98 |
const bidiIsolationSpec = { |
| 99 |
provide: ( plugin ) => { |
| 100 |
/** |
| 101 |
* @param {EditorView} view |
| 102 |
* @return {DecorationSet} |
| 103 |
*/ |
| 104 |
const access = ( view ) => view.plugin( plugin ) ? |
| 105 |
( view.plugin( plugin ).isolates || Decoration.none ) : |
| 106 |
Decoration.none; |
| 107 |
|
| 108 |
// Use the lowest precedence to ensure that other decorations |
| 109 |
// don't break up the isolating decorations. |
| 110 |
return Prec.lowest( [ |
| 111 |
EditorView.decorations.of( access ), |
| 112 |
EditorView.bidiIsolatedRanges.of( access ) |
| 113 |
] ); |
| 114 |
} |
| 115 |
}; |
| 116 |
|
| 117 |
/** |
| 118 |
* Bidirectional isolation plugin for CodeMirror for use on RTL pages. |
| 119 |
* This ensures HTML and MediaWiki tags are always displayed left-to-right. |
| 120 |
* |
| 121 |
* Use this plugin by passing in `bidiIsolation: true` when instantiating |
| 122 |
* a [CodeMirrorModeMediaWiki]{@link CodeMirrorModeMediaWiki} object. |
| 123 |
* |
| 124 |
* @module CodeMirrorBidiIsolation |
| 125 |
* @see https://codemirror.net/examples/bidi/ |
| 126 |
*/ |
| 127 |
module.exports = ViewPlugin.fromClass( CodeMirrorBidiIsolation, bidiIsolationSpec ); |