| 1 |
const { |
| 2 |
EditorState, |
| 3 |
Extension, |
| 4 |
KeyBinding, |
| 5 |
StateField, |
| 6 |
SyntaxNode, |
| 7 |
Tree, |
| 8 |
Tooltip, |
| 9 |
codeFolding, |
| 10 |
ensureSyntaxTree, |
| 11 |
foldEffect, |
| 12 |
foldedRanges, |
| 13 |
keymap, |
| 14 |
showTooltip, |
| 15 |
syntaxTree, |
| 16 |
unfoldAll, |
| 17 |
unfoldEffect |
| 18 |
} = require( 'ext.CodeMirror.v6.lib' ); |
| 19 |
const modeConfig = require( './codemirror.mediawiki.config.js' ); |
| 20 |
|
| 21 |
/** |
| 22 |
* Check if a SyntaxNode is a template bracket (`{{` or `}}`) |
| 23 |
* |
| 24 |
* @param {SyntaxNode} node The SyntaxNode to check |
| 25 |
* @return {boolean} |
| 26 |
* @private |
| 27 |
*/ |
| 28 |
const isBracket = ( node ) => node.name.split( '_' ).includes( modeConfig.tags.templateBracket ), |
| 29 |
/** |
| 30 |
* Check if a SyntaxNode is a template delimiter (`|`) |
| 31 |
* |
| 32 |
* @param {SyntaxNode} node The SyntaxNode to check |
| 33 |
* @return {boolean} |
| 34 |
* @private |
| 35 |
*/ |
| 36 |
isDelimiter = ( node ) => node.name.split( '_' ).includes( modeConfig.tags.templateDelimiter ), |
| 37 |
/** |
| 38 |
* Check if a SyntaxNode is part of a template, except for the brackets |
| 39 |
* |
| 40 |
* @param {SyntaxNode} node The SyntaxNode to check |
| 41 |
* @return {boolean} |
| 42 |
* @private |
| 43 |
*/ |
| 44 |
isTemplate = ( node ) => /-template[a-z\d-]+ground/u.test( node.name ) && !isBracket( node ), |
| 45 |
/** |
| 46 |
* Update the stack of opening (+) or closing (-) brackets |
| 47 |
* |
| 48 |
* @param {EditorState} state EditorState instance |
| 49 |
* @param {SyntaxNode} node The SyntaxNode of the bracket |
| 50 |
* @return {number} |
| 51 |
* @private |
| 52 |
*/ |
| 53 |
stackUpdate = ( state, node ) => state.sliceDoc( node.from, node.from + 1 ) === '{' ? 1 : -1; |
| 54 |
|
| 55 |
/** |
| 56 |
* If the node is a template, find the range of the template parameters |
| 57 |
* |
| 58 |
* @param {EditorState} state EditorState instance |
| 59 |
* @param {number|SyntaxNode} posOrNode Position or node |
| 60 |
* @param {Tree|null} [tree] Syntax tree |
| 61 |
* @return {{from: number, to: number}|null} |
| 62 |
* @private |
| 63 |
*/ |
| 64 |
const foldable = ( state, posOrNode, tree ) => { |
| 65 |
if ( typeof posOrNode === 'number' ) { |
| 66 |
tree = ensureSyntaxTree( state, posOrNode ); |
| 67 |
} |
| 68 |
if ( !tree ) { |
| 69 |
return null; |
| 70 |
} |
| 71 |
/** @type {SyntaxNode} */ |
| 72 |
let node; |
| 73 |
if ( typeof posOrNode === 'number' ) { |
| 74 |
// Find the initial template node on both sides of the position |
| 75 |
node = tree.resolve( posOrNode, -1 ); |
| 76 |
if ( !isTemplate( node ) ) { |
| 77 |
node = tree.resolve( posOrNode, 1 ); |
| 78 |
} |
| 79 |
} else { |
| 80 |
node = posOrNode; |
| 81 |
} |
| 82 |
if ( !isTemplate( node ) ) { |
| 83 |
// Not a template |
| 84 |
return null; |
| 85 |
} |
| 86 |
let { prevSibling, nextSibling } = node, |
| 87 |
/** The stack of opening (+) or closing (-) brackets */ |
| 88 |
stack = 1, |
| 89 |
/** The first delimiter */ |
| 90 |
delimiter = isDelimiter( node ) ? node : null; |
| 91 |
while ( nextSibling ) { |
| 92 |
if ( isBracket( nextSibling ) ) { |
| 93 |
stack += stackUpdate( state, nextSibling ); |
| 94 |
if ( stack === 0 ) { |
| 95 |
// The closing bracket of the current template |
| 96 |
break; |
| 97 |
} |
| 98 |
} else if ( !delimiter && stack === 1 && isDelimiter( nextSibling ) ) { |
| 99 |
// The first delimiter of the current template so far |
| 100 |
delimiter = nextSibling; |
| 101 |
} |
| 102 |
( { nextSibling } = nextSibling ); |
| 103 |
} |
| 104 |
if ( !nextSibling ) { |
| 105 |
// The closing bracket of the current template is missing |
| 106 |
return null; |
| 107 |
} |
| 108 |
stack = -1; |
| 109 |
while ( prevSibling ) { |
| 110 |
if ( isBracket( prevSibling ) ) { |
| 111 |
stack += stackUpdate( state, prevSibling ); |
| 112 |
if ( stack === 0 ) { |
| 113 |
// The opening bracket of the current template |
| 114 |
break; |
| 115 |
} |
| 116 |
} else if ( stack === -1 && isDelimiter( prevSibling ) ) { |
| 117 |
// The first delimiter of the current template so far |
| 118 |
delimiter = prevSibling; |
| 119 |
} |
| 120 |
( { prevSibling } = prevSibling ); |
| 121 |
} |
| 122 |
/** The end of the first delimiter */ |
| 123 |
const from = delimiter && delimiter.to, |
| 124 |
/** The start of the closing bracket */ |
| 125 |
to = nextSibling.from; |
| 126 |
if ( from && from < to ) { |
| 127 |
return { from, to }; |
| 128 |
} |
| 129 |
return null; |
| 130 |
}; |
| 131 |
|
| 132 |
/** |
| 133 |
* Create a tooltip for folding a template |
| 134 |
* |
| 135 |
* @param {EditorState} state EditorState instance |
| 136 |
* @return {Tooltip|null} |
| 137 |
* @private |
| 138 |
*/ |
| 139 |
const create = ( state ) => { |
| 140 |
const { selection: { main: { head } } } = state, |
| 141 |
range = foldable( state, head ); |
| 142 |
if ( range ) { |
| 143 |
const { from, to } = range; |
| 144 |
let folded = false; |
| 145 |
// Check if the range is already folded |
| 146 |
foldedRanges( state ).between( from, to, ( i, j ) => { |
| 147 |
if ( i === from && j === to ) { |
| 148 |
folded = true; |
| 149 |
} |
| 150 |
} ); |
| 151 |
return folded ? |
| 152 |
null : |
| 153 |
{ |
| 154 |
pos: head, |
| 155 |
above: true, |
| 156 |
create( view ) { |
| 157 |
const dom = document.createElement( 'div' ); |
| 158 |
dom.className = 'cm-tooltip-fold'; |
| 159 |
dom.textContent = '\uff0d'; |
| 160 |
dom.title = mw.msg( 'codemirror-fold-template' ); |
| 161 |
dom.onclick = () => { |
| 162 |
view.dispatch( { |
| 163 |
effects: foldEffect.of( { from, to } ), |
| 164 |
selection: { anchor: to } |
| 165 |
} ); |
| 166 |
dom.remove(); |
| 167 |
}; |
| 168 |
return { dom }; |
| 169 |
} |
| 170 |
}; |
| 171 |
} |
| 172 |
return null; |
| 173 |
}; |
| 174 |
|
| 175 |
/** |
| 176 |
* @type {KeyBinding[]} |
| 177 |
* @private |
| 178 |
*/ |
| 179 |
const foldKeymap = [ |
| 180 |
{ |
| 181 |
// Fold the template at the selection/cursor |
| 182 |
key: 'Ctrl-Shift-[', |
| 183 |
mac: 'Cmd-Alt-[', |
| 184 |
run( view ) { |
| 185 |
const { state } = view, |
| 186 |
tree = ensureSyntaxTree( state, view.viewport.to ); |
| 187 |
if ( !tree ) { |
| 188 |
return false; |
| 189 |
} |
| 190 |
const effects = [], |
| 191 |
{ selection: { ranges } } = state; |
| 192 |
/** The rightmost position of all selections, to be updated with folding */ |
| 193 |
let anchor = Math.max( ...ranges.map( ( { to } ) => to ) ); |
| 194 |
for ( const { from, to } of ranges ) { |
| 195 |
let node; |
| 196 |
if ( from === to ) { |
| 197 |
// No selection, try both sides of the cursor position |
| 198 |
node = tree.resolve( from, -1 ); |
| 199 |
} |
| 200 |
if ( !node || !isTemplate( node ) ) { |
| 201 |
node = tree.resolve( from, 1 ); |
| 202 |
} |
| 203 |
while ( node && node.from <= to ) { |
| 204 |
const range = foldable( state, node, tree ); |
| 205 |
if ( range ) { |
| 206 |
effects.push( foldEffect.of( range ) ); |
| 207 |
node = tree.resolve( range.to, 1 ); |
| 208 |
// Update the anchor with the end of the last folded range |
| 209 |
anchor = Math.max( anchor, range.to ); |
| 210 |
continue; |
| 211 |
} |
| 212 |
node = node.nextSibling; |
| 213 |
} |
| 214 |
} |
| 215 |
if ( effects.length > 0 ) { |
| 216 |
const dom = view.dom.querySelector( '.cm-tooltip-fold' ); |
| 217 |
if ( dom ) { |
| 218 |
dom.remove(); |
| 219 |
} |
| 220 |
// Fold the template(s) and update the cursor position |
| 221 |
view.dispatch( { effects, selection: { anchor } } ); |
| 222 |
return true; |
| 223 |
} |
| 224 |
return false; |
| 225 |
} |
| 226 |
}, |
| 227 |
{ |
| 228 |
// Unfold the template at the selection/cursor |
| 229 |
key: 'Ctrl-Shift-]', |
| 230 |
mac: 'Cmd-Alt-]', |
| 231 |
run( view ) { |
| 232 |
const { state } = view, |
| 233 |
{ selection } = state, |
| 234 |
effects = [], |
| 235 |
folded = foldedRanges( state ); |
| 236 |
for ( const { from, to } of selection.ranges ) { |
| 237 |
// Unfold any folded range at the selection |
| 238 |
folded.between( from, to, ( i, j ) => { |
| 239 |
effects.push( unfoldEffect.of( { from: i, to: j } ) ); |
| 240 |
} ); |
| 241 |
} |
| 242 |
if ( effects.length > 0 ) { |
| 243 |
// Unfold the template(s) and redraw the selections |
| 244 |
view.dispatch( { effects, selection } ); |
| 245 |
return true; |
| 246 |
} |
| 247 |
return false; |
| 248 |
} |
| 249 |
}, |
| 250 |
{ |
| 251 |
// Fold all templates in the document |
| 252 |
key: 'Ctrl-Alt-[', |
| 253 |
run( view ) { |
| 254 |
const { state } = view, |
| 255 |
tree = syntaxTree( state ), |
| 256 |
effects = []; |
| 257 |
/** The rightmost position of all selections, to be updated with folding */ |
| 258 |
let anchor = Math.max( ...state.selection.ranges.map( ( { to } ) => to ) ), |
| 259 |
node = tree.topNode.firstChild; |
| 260 |
while ( node ) { |
| 261 |
const range = foldable( state, node, tree ); |
| 262 |
if ( range ) { |
| 263 |
effects.push( foldEffect.of( range ) ); |
| 264 |
const { from, to } = range; |
| 265 |
node = tree.resolve( to, 1 ); |
| 266 |
if ( from <= anchor && to > anchor ) { |
| 267 |
// Update the anchor with the end of the last folded range |
| 268 |
anchor = to; |
| 269 |
} |
| 270 |
continue; |
| 271 |
} |
| 272 |
node = node.nextSibling; |
| 273 |
} |
| 274 |
if ( effects.length > 0 ) { |
| 275 |
const dom = view.dom.querySelector( '.cm-tooltip-fold' ); |
| 276 |
if ( dom ) { |
| 277 |
dom.remove(); |
| 278 |
} |
| 279 |
// Fold the template(s) and update the cursor position |
| 280 |
view.dispatch( { effects, selection: { anchor } } ); |
| 281 |
return true; |
| 282 |
} |
| 283 |
return false; |
| 284 |
} |
| 285 |
}, |
| 286 |
{ key: 'Ctrl-Alt-]', run: unfoldAll } |
| 287 |
]; |
| 288 |
|
| 289 |
/** |
| 290 |
* CodeMirror extension providing |
| 291 |
* [template folding](https://www.mediawiki.org/wiki/Help:Extension:CodeMirror#Template_folding) |
| 292 |
* for the MediaWiki mode. This automatically applied when using {@link CodeMirrorModeMediaWiki}. |
| 293 |
* |
| 294 |
* @module CodeMirrorTemplateFolding |
| 295 |
* @type {Extension} |
| 296 |
*/ |
| 297 |
const templateFoldingExtension = [ |
| 298 |
codeFolding( { |
| 299 |
placeholderDOM( view ) { |
| 300 |
const element = document.createElement( 'span' ); |
| 301 |
element.textContent = '…'; |
| 302 |
element.setAttribute( 'aria-label', mw.msg( 'codemirror-folded-code' ) ); |
| 303 |
element.title = mw.msg( 'codemirror-unfold' ); |
| 304 |
element.className = 'cm-foldPlaceholder'; |
| 305 |
element.onclick = ( { target } ) => { |
| 306 |
const pos = view.posAtDOM( target ), |
| 307 |
{ state } = view, |
| 308 |
{ selection } = state; |
| 309 |
foldedRanges( state ).between( pos, pos, ( from, to ) => { |
| 310 |
if ( from === pos ) { |
| 311 |
// Unfold the template and redraw the selections |
| 312 |
view.dispatch( { effects: unfoldEffect.of( { from, to } ), selection } ); |
| 313 |
} |
| 314 |
} ); |
| 315 |
}; |
| 316 |
return element; |
| 317 |
} |
| 318 |
} ), |
| 319 |
/** @see https://codemirror.net/examples/tooltip/ */ |
| 320 |
StateField.define( { |
| 321 |
create, |
| 322 |
update( tooltip, { state, docChanged, selection } ) { |
| 323 |
return docChanged || selection ? create( state ) : tooltip; |
| 324 |
}, |
| 325 |
provide( f ) { |
| 326 |
return showTooltip.from( f ); |
| 327 |
} |
| 328 |
} ), |
| 329 |
keymap.of( foldKeymap ) |
| 330 |
]; |
| 331 |
|
| 332 |
module.exports = templateFoldingExtension; |