| 1 |
const { |
| 2 |
EditorView, |
| 3 |
SearchQuery, |
| 4 |
closeSearchPanel, |
| 5 |
findNext, |
| 6 |
findPrevious, |
| 7 |
keymap, |
| 8 |
replaceAll, |
| 9 |
replaceNext, |
| 10 |
runScopeHandlers, |
| 11 |
search, |
| 12 |
searchKeymap, |
| 13 |
selectMatches, |
| 14 |
setSearchQuery |
| 15 |
} = require( 'ext.CodeMirror.v6.lib' ); |
| 16 |
const CodeMirrorPanel = require( './codemirror.panel.js' ); |
| 17 |
|
| 18 |
/** |
| 19 |
* Custom search panel for CodeMirror using CSS-only Codex components. |
| 20 |
* |
| 21 |
* @extends CodeMirrorPanel |
| 22 |
*/ |
| 23 |
class CodeMirrorSearch extends CodeMirrorPanel { |
| 24 |
constructor() { |
| 25 |
super(); |
| 26 |
|
| 27 |
/** |
| 28 |
* @type {SearchQuery} |
| 29 |
*/ |
| 30 |
this.searchQuery = { |
| 31 |
search: '' |
| 32 |
}; |
| 33 |
/** |
| 34 |
* @type {HTMLInputElement} |
| 35 |
*/ |
| 36 |
this.searchInput = undefined; |
| 37 |
/** |
| 38 |
* @type {HTMLInputElement} |
| 39 |
*/ |
| 40 |
this.replaceInput = undefined; |
| 41 |
/** |
| 42 |
* @type {HTMLButtonElement} |
| 43 |
*/ |
| 44 |
this.matchCaseButton = undefined; |
| 45 |
/** |
| 46 |
* @type {HTMLButtonElement} |
| 47 |
*/ |
| 48 |
this.regexpButton = undefined; |
| 49 |
/** |
| 50 |
* @type {HTMLButtonElement} |
| 51 |
*/ |
| 52 |
this.wholeWordButton = undefined; |
| 53 |
/** |
| 54 |
* @type {HTMLButtonElement} |
| 55 |
*/ |
| 56 |
this.nextButton = undefined; |
| 57 |
/** |
| 58 |
* @type {HTMLButtonElement} |
| 59 |
*/ |
| 60 |
this.prevButton = undefined; |
| 61 |
/** |
| 62 |
* @type {HTMLButtonElement} |
| 63 |
*/ |
| 64 |
this.allButton = undefined; |
| 65 |
/** |
| 66 |
* @type {HTMLButtonElement} |
| 67 |
*/ |
| 68 |
this.replaceButton = undefined; |
| 69 |
/** |
| 70 |
* @type {HTMLButtonElement} |
| 71 |
*/ |
| 72 |
this.replaceAllButton = undefined; |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* @inheritDoc |
| 77 |
*/ |
| 78 |
get extension() { |
| 79 |
return [ |
| 80 |
search( { |
| 81 |
createPanel: ( view ) => { |
| 82 |
this.view = view; |
| 83 |
return this.panel; |
| 84 |
} |
| 85 |
} ), |
| 86 |
keymap.of( searchKeymap ) |
| 87 |
]; |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* @inheritDoc |
| 92 |
*/ |
| 93 |
get panel() { |
| 94 |
const container = document.createElement( 'div' ); |
| 95 |
container.className = 'cm-mw-panel cm-mw-panel--search-panel'; |
| 96 |
container.addEventListener( 'keydown', this.onKeydown.bind( this ) ); |
| 97 |
|
| 98 |
const firstRow = document.createElement( 'div' ); |
| 99 |
firstRow.className = 'cm-mw-panel--row'; |
| 100 |
container.appendChild( firstRow ); |
| 101 |
|
| 102 |
// Search input. |
| 103 |
const [ searchInputWrapper, searchInput ] = this.getTextInput( |
| 104 |
'search', |
| 105 |
this.searchQuery.search || '', |
| 106 |
'codemirror-find' |
| 107 |
); |
| 108 |
this.searchInput = searchInput; |
| 109 |
this.searchInput.setAttribute( 'main-field', 'true' ); |
| 110 |
firstRow.appendChild( searchInputWrapper ); |
| 111 |
|
| 112 |
this.appendPrevAndNextButtons( firstRow ); |
| 113 |
|
| 114 |
// "All" button. |
| 115 |
this.allButton = this.getButton( 'codemirror-all' ); |
| 116 |
this.allButton.title = mw.msg( 'codemirror-all-tooltip' ); |
| 117 |
this.allButton.addEventListener( 'click', ( e ) => { |
| 118 |
e.preventDefault(); |
| 119 |
selectMatches( this.view ); |
| 120 |
} ); |
| 121 |
firstRow.appendChild( this.allButton ); |
| 122 |
|
| 123 |
this.appendSearchOptions( firstRow ); |
| 124 |
this.appendSecondRow( container ); |
| 125 |
|
| 126 |
return { |
| 127 |
dom: container, |
| 128 |
top: true, |
| 129 |
mount: () => { |
| 130 |
this.searchInput.focus(); |
| 131 |
this.searchInput.select(); |
| 132 |
} |
| 133 |
}; |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* @param {HTMLDivElement} firstRow |
| 138 |
* @private |
| 139 |
*/ |
| 140 |
appendPrevAndNextButtons( firstRow ) { |
| 141 |
const buttonGroup = document.createElement( 'div' ); |
| 142 |
buttonGroup.className = 'cdx-button-group'; |
| 143 |
|
| 144 |
// "Previous" button. |
| 145 |
this.prevButton = this.getButton( 'codemirror-previous', 'previous', true ); |
| 146 |
buttonGroup.appendChild( this.prevButton ); |
| 147 |
this.prevButton.addEventListener( 'click', ( e ) => { |
| 148 |
e.preventDefault(); |
| 149 |
findPrevious( this.view ); |
| 150 |
} ); |
| 151 |
|
| 152 |
// "Next" button. |
| 153 |
this.nextButton = this.getButton( 'codemirror-next', 'next', true ); |
| 154 |
buttonGroup.appendChild( this.nextButton ); |
| 155 |
this.nextButton.addEventListener( 'click', ( e ) => { |
| 156 |
e.preventDefault(); |
| 157 |
findNext( this.view ); |
| 158 |
} ); |
| 159 |
|
| 160 |
firstRow.appendChild( buttonGroup ); |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* @param {HTMLDivElement} firstRow |
| 165 |
* @private |
| 166 |
*/ |
| 167 |
appendSearchOptions( firstRow ) { |
| 168 |
const buttonGroup = document.createElement( 'div' ); |
| 169 |
buttonGroup.className = 'cdx-toggle-button-group'; |
| 170 |
|
| 171 |
// "Match case" ToggleButton. |
| 172 |
this.matchCaseButton = this.getToggleButton( |
| 173 |
'case', |
| 174 |
'codemirror-match-case', |
| 175 |
'match-case', |
| 176 |
this.searchQuery.caseSensitive |
| 177 |
); |
| 178 |
buttonGroup.appendChild( this.matchCaseButton ); |
| 179 |
|
| 180 |
// "Regexp" ToggleButton. |
| 181 |
this.regexpButton = this.getToggleButton( |
| 182 |
're', |
| 183 |
'codemirror-regexp', |
| 184 |
'regexp', |
| 185 |
this.searchQuery.regexp |
| 186 |
); |
| 187 |
buttonGroup.appendChild( this.regexpButton ); |
| 188 |
|
| 189 |
// "Whole word" checkbox. |
| 190 |
this.wholeWordButton = this.getToggleButton( |
| 191 |
'word', |
| 192 |
'codemirror-by-word', |
| 193 |
'quotes', |
| 194 |
this.searchQuery.wholeWord |
| 195 |
); |
| 196 |
buttonGroup.appendChild( this.wholeWordButton ); |
| 197 |
|
| 198 |
firstRow.appendChild( buttonGroup ); |
| 199 |
} |
| 200 |
|
| 201 |
/** |
| 202 |
* @param {HTMLDivElement} container |
| 203 |
* @private |
| 204 |
*/ |
| 205 |
appendSecondRow( container ) { |
| 206 |
const shouldBeDisabled = this.view.state.readOnly; |
| 207 |
const row = document.createElement( 'div' ); |
| 208 |
row.className = 'cm-mw-panel--row'; |
| 209 |
container.appendChild( row ); |
| 210 |
|
| 211 |
// Replace input. |
| 212 |
const [ replaceInputWrapper, replaceInput ] = this.getTextInput( |
| 213 |
'replace', |
| 214 |
this.searchQuery.replace || '', |
| 215 |
'codemirror-replace-placeholder' |
| 216 |
); |
| 217 |
this.replaceInput = replaceInput; |
| 218 |
this.replaceInput.disabled = shouldBeDisabled; |
| 219 |
row.appendChild( replaceInputWrapper ); |
| 220 |
|
| 221 |
// "Replace" button. |
| 222 |
this.replaceButton = this.getButton( 'codemirror-replace' ); |
| 223 |
this.replaceButton.disabled = shouldBeDisabled; |
| 224 |
row.appendChild( this.replaceButton ); |
| 225 |
this.replaceButton.addEventListener( 'click', ( e ) => { |
| 226 |
e.preventDefault(); |
| 227 |
replaceNext( this.view ); |
| 228 |
} ); |
| 229 |
|
| 230 |
// "Replace all" button. |
| 231 |
this.replaceAllButton = this.getButton( 'codemirror-replace-all' ); |
| 232 |
this.replaceAllButton.disabled = shouldBeDisabled; |
| 233 |
row.appendChild( this.replaceAllButton ); |
| 234 |
this.replaceAllButton.addEventListener( 'click', ( e ) => { |
| 235 |
e.preventDefault(); |
| 236 |
replaceAll( this.view ); |
| 237 |
} ); |
| 238 |
|
| 239 |
// "Done" button. |
| 240 |
const doneButton = this.getButton( 'codemirror-done' ); |
| 241 |
row.appendChild( doneButton ); |
| 242 |
doneButton.addEventListener( 'click', ( e ) => { |
| 243 |
e.preventDefault(); |
| 244 |
closeSearchPanel( this.view ); |
| 245 |
this.view.focus(); |
| 246 |
} ); |
| 247 |
} |
| 248 |
|
| 249 |
/** |
| 250 |
* Respond to keydown events. |
| 251 |
* |
| 252 |
* @param {KeyboardEvent} event |
| 253 |
*/ |
| 254 |
onKeydown( event ) { |
| 255 |
if ( runScopeHandlers( this.view, event, 'search-panel' ) ) { |
| 256 |
event.preventDefault(); |
| 257 |
} else if ( event.key === 'Enter' && event.target === this.searchInput ) { |
| 258 |
event.preventDefault(); |
| 259 |
( event.shiftKey ? findPrevious : findNext )( this.view ); |
| 260 |
} else if ( event.key === 'Enter' && event.target === this.replaceInput ) { |
| 261 |
event.preventDefault(); |
| 262 |
replaceNext( this.view ); |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
/** |
| 267 |
* Create a new {@link SearchQuery} and dispatch it to the {@link EditorView}. |
| 268 |
*/ |
| 269 |
commit() { |
| 270 |
const query = new SearchQuery( { |
| 271 |
search: this.searchInput.value, |
| 272 |
caseSensitive: this.matchCaseButton.dataset.checked === 'true', |
| 273 |
regexp: this.regexpButton.dataset.checked === 'true', |
| 274 |
wholeWord: this.wholeWordButton.dataset.checked === 'true', |
| 275 |
replace: this.replaceInput.value, |
| 276 |
// Makes i.e. "\n" match the literal string "\n" instead of a newline. |
| 277 |
literal: true |
| 278 |
} ); |
| 279 |
if ( !this.searchQuery || !query.eq( this.searchQuery ) ) { |
| 280 |
this.searchQuery = query; |
| 281 |
this.view.dispatch( { |
| 282 |
effects: setSearchQuery.of( query ) |
| 283 |
} ); |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
/** |
| 288 |
* @inheritDoc |
| 289 |
*/ |
| 290 |
getTextInput( name, value = '', placeholder = '' ) { |
| 291 |
const [ container, input ] = super.getTextInput( name, value, placeholder ); |
| 292 |
input.addEventListener( 'change', this.commit.bind( this ) ); |
| 293 |
input.addEventListener( 'keyup', this.commit.bind( this ) ); |
| 294 |
return [ container, input ]; |
| 295 |
} |
| 296 |
|
| 297 |
/** |
| 298 |
* @inheritDoc |
| 299 |
*/ |
| 300 |
getToggleButton( name, label, icon, checked = false ) { |
| 301 |
const button = super.getToggleButton( name, label, icon, checked ); |
| 302 |
button.addEventListener( 'click', this.commit.bind( this ) ); |
| 303 |
return button; |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
module.exports = CodeMirrorSearch; |