| 1 |
const { EditorView, Extension, Panel } = require( 'ext.CodeMirror.v6.lib' ); |
| 2 |
|
| 3 |
/** |
| 4 |
* Abstract class for a panel that can be used with CodeMirror. |
| 5 |
* This class provides methods to create CSS-only Codex components. |
| 6 |
* |
| 7 |
* @see https://codemirror.net/docs/ref/#h_panels |
| 8 |
* @abstract |
| 9 |
*/ |
| 10 |
class CodeMirrorPanel { |
| 11 |
/** |
| 12 |
* @constructor |
| 13 |
*/ |
| 14 |
constructor() { |
| 15 |
/** |
| 16 |
* @type {EditorView} |
| 17 |
*/ |
| 18 |
this.view = undefined; |
| 19 |
} |
| 20 |
|
| 21 |
/** |
| 22 |
* Get the panel and any associated keymaps as a CodeMirror Extension. |
| 23 |
* |
| 24 |
* @abstract |
| 25 |
* @type {Extension} |
| 26 |
*/ |
| 27 |
// eslint-disable-next-line getter-return |
| 28 |
get extension() {} |
| 29 |
|
| 30 |
/** |
| 31 |
* Get the Panel object. |
| 32 |
* |
| 33 |
* @abstract |
| 34 |
* @type {Panel} |
| 35 |
*/ |
| 36 |
// eslint-disable-next-line getter-return |
| 37 |
get panel() {} |
| 38 |
|
| 39 |
/** |
| 40 |
* Get a CSS-only Codex TextInput. |
| 41 |
* |
| 42 |
* @param {string} name |
| 43 |
* @param {string} [value=''] |
| 44 |
* @param {string} placeholder |
| 45 |
* @return {Array<HTMLElement>} [HTMLDivElement, HTMLInputElement] |
| 46 |
* @internal |
| 47 |
*/ |
| 48 |
getTextInput( name, value = '', placeholder = '' ) { |
| 49 |
const wrapper = document.createElement( 'div' ); |
| 50 |
wrapper.className = 'cdx-text-input cm-mw-panel--text-input'; |
| 51 |
const input = document.createElement( 'input' ); |
| 52 |
input.className = 'cdx-text-input__input'; |
| 53 |
input.type = 'text'; |
| 54 |
input.name = name; |
| 55 |
// The following messages may be used here: |
| 56 |
// * codemirror-find |
| 57 |
// * codemirror-replace-placeholder |
| 58 |
input.placeholder = placeholder ? mw.msg( placeholder ) : ''; |
| 59 |
input.value = value; |
| 60 |
wrapper.appendChild( input ); |
| 61 |
return [ wrapper, input ]; |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Get a CSS-only Codex Button. |
| 66 |
* |
| 67 |
* @param {string} label |
| 68 |
* @param {string|null} [icon=null] |
| 69 |
* @param {boolean} [iconOnly=false] |
| 70 |
* @return {HTMLButtonElement} |
| 71 |
* @internal |
| 72 |
*/ |
| 73 |
getButton( label, icon = null, iconOnly = false ) { |
| 74 |
const button = document.createElement( 'button' ); |
| 75 |
button.className = 'cdx-button cm-mw-panel--button'; |
| 76 |
button.type = 'button'; |
| 77 |
|
| 78 |
if ( icon ) { |
| 79 |
const iconSpan = document.createElement( 'span' ); |
| 80 |
// The following CSS classes may be used here: |
| 81 |
// * cm-mw-icon--previous |
| 82 |
// * cm-mw-icon--next |
| 83 |
// * cm-mw-icon--all |
| 84 |
// * cm-mw-icon--replace |
| 85 |
// * cm-mw-icon--replace-all |
| 86 |
// * cm-mw-icon--done |
| 87 |
// * cm-mw-icon--goto-line-go |
| 88 |
iconSpan.className = 'cdx-button__icon cm-mw-icon--' + icon; |
| 89 |
|
| 90 |
if ( !iconOnly ) { |
| 91 |
iconSpan.setAttribute( 'aria-hidden', 'true' ); |
| 92 |
} |
| 93 |
|
| 94 |
button.appendChild( iconSpan ); |
| 95 |
} |
| 96 |
|
| 97 |
// The following messages may be used here: |
| 98 |
// * codemirror-next |
| 99 |
// * codemirror-previous |
| 100 |
// * codemirror-all |
| 101 |
// * codemirror-replace |
| 102 |
// * codemirror-replace-all |
| 103 |
const message = mw.msg( label ); |
| 104 |
if ( iconOnly ) { |
| 105 |
button.classList.add( 'cdx-button--icon-only' ); |
| 106 |
button.title = message; |
| 107 |
button.setAttribute( 'aria-label', message ); |
| 108 |
} else { |
| 109 |
button.append( message ); |
| 110 |
} |
| 111 |
|
| 112 |
return button; |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Get a CSS-only Codex Checkbox. |
| 117 |
* |
| 118 |
* @param {string} name |
| 119 |
* @param {string} label |
| 120 |
* @param {boolean} [checked=false] |
| 121 |
* @return {Array<HTMLElement>} [HTMLSpanElement, HTMLInputElement] |
| 122 |
* @internal |
| 123 |
*/ |
| 124 |
getCheckbox( name, label, checked = false ) { |
| 125 |
const wrapper = document.createElement( 'span' ); |
| 126 |
wrapper.className = 'cdx-checkbox cdx-checkbox--inline cm-mw-panel--checkbox'; |
| 127 |
const input = document.createElement( 'input' ); |
| 128 |
input.className = 'cdx-checkbox__input'; |
| 129 |
input.id = `cm-mw-panel--checkbox-${ name }`; |
| 130 |
input.type = 'checkbox'; |
| 131 |
input.name = name; |
| 132 |
input.checked = checked; |
| 133 |
wrapper.appendChild( input ); |
| 134 |
const emptyIcon = document.createElement( 'span' ); |
| 135 |
emptyIcon.className = 'cdx-checkbox__icon'; |
| 136 |
wrapper.appendChild( emptyIcon ); |
| 137 |
const labelWrapper = document.createElement( 'div' ); |
| 138 |
labelWrapper.className = 'cdx-checkbox__label cdx-label'; |
| 139 |
const labelElement = document.createElement( 'label' ); |
| 140 |
labelElement.className = 'cdx-label__label'; |
| 141 |
labelElement.htmlFor = input.id; |
| 142 |
const innerSpan = document.createElement( 'span' ); |
| 143 |
innerSpan.className = 'cdx-label__label__text'; |
| 144 |
// The following messages may be used here: |
| 145 |
// * codemirror-match-case |
| 146 |
// * codemirror-regexp |
| 147 |
// * codemirror-by-word |
| 148 |
innerSpan.textContent = mw.msg( label ); |
| 149 |
labelElement.appendChild( innerSpan ); |
| 150 |
labelWrapper.appendChild( labelElement ); |
| 151 |
wrapper.appendChild( labelWrapper ); |
| 152 |
return [ wrapper, input ]; |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* Get a CSS-only Codex ToggleButton. |
| 157 |
* |
| 158 |
* @param {string} name |
| 159 |
* @param {string} label |
| 160 |
* @param {string} icon |
| 161 |
* @param {boolean} [checked=false] |
| 162 |
* @return {HTMLButtonElement} |
| 163 |
* @internal |
| 164 |
*/ |
| 165 |
getToggleButton( name, label, icon, checked = false ) { |
| 166 |
const btn = document.createElement( 'button' ); |
| 167 |
// The following CSS classes may be used here: |
| 168 |
// * cdx-toggle-button--toggled-on |
| 169 |
// * cdx-toggle-button--toggled-off |
| 170 |
btn.className = 'cdx-toggle-button cdx-toggle-button--framed ' + |
| 171 |
`cdx-toggle-button--toggled-${ checked ? 'on' : 'off' } cm-mw-panel--toggle-button`; |
| 172 |
btn.dataset.checked = String( checked ); |
| 173 |
btn.setAttribute( 'aria-pressed', checked ); |
| 174 |
// The following messages may be used here: |
| 175 |
// * codemirror-match-case |
| 176 |
// * codemirror-regexp |
| 177 |
// * codemirror-by-word |
| 178 |
const message = mw.msg( label ); |
| 179 |
btn.title = message; |
| 180 |
btn.setAttribute( 'aria-label', message ); |
| 181 |
|
| 182 |
// Add the icon. |
| 183 |
const iconWrapper = document.createElement( 'span' ); |
| 184 |
// The following CSS classes may be used here: |
| 185 |
// * cm-mw-icon--match-case |
| 186 |
// * cm-mw-icon--regexp |
| 187 |
// * cm-mw-icon--quotes |
| 188 |
iconWrapper.className = 'cdx-icon cdx-icon--medium cm-mw-icon--' + icon; |
| 189 |
btn.appendChild( iconWrapper ); |
| 190 |
|
| 191 |
// Add the click handler. |
| 192 |
btn.addEventListener( 'click', ( e ) => { |
| 193 |
e.preventDefault(); |
| 194 |
const toggled = btn.dataset.checked === 'true'; |
| 195 |
btn.dataset.checked = String( !toggled ); |
| 196 |
btn.setAttribute( 'aria-pressed', String( !toggled ) ); |
| 197 |
btn.classList.toggle( 'cdx-toggle-button--toggled-on', !toggled ); |
| 198 |
btn.classList.toggle( 'cdx-toggle-button--toggled-off', toggled ); |
| 199 |
} ); |
| 200 |
|
| 201 |
return btn; |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
module.exports = CodeMirrorPanel; |