|
| 1 | +import PropTypes from 'prop-types'; |
| 2 | +import React, { useState, useEffect, useRef, useCallback } from 'react'; |
| 3 | +import { |
| 4 | + SearchQuery, |
| 5 | + setSearchQuery, |
| 6 | + findNext, |
| 7 | + findPrevious, |
| 8 | + replaceNext, |
| 9 | + replaceAll, |
| 10 | + openSearchPanel, |
| 11 | + closeSearchPanel, |
| 12 | + getSearchQuery |
| 13 | +} from '@codemirror/search'; |
| 14 | +import { runScopeHandlers } from '@codemirror/view'; |
| 15 | +import MatchCaseSvg from '../../../../images/match-case.svg'; |
| 16 | +import RegexSvg from '../../../../images/regex.svg'; |
| 17 | +import ArrowSvg from '../../../../images/arrow.svg'; |
| 18 | +import CrossSvg from '../../../../images/cross.svg'; |
| 19 | +import CaretArrowSvg from '../../../../images/right-arrow.svg'; |
| 20 | + |
| 21 | +/** |
| 22 | + * Custom implementation of CodeMirror 6's built-in SearchPanel. |
| 23 | + * |
| 24 | + * TODO: |
| 25 | + * We may need to add an announce functionality for screen readers, similar to the original: |
| 26 | + * https://github.com/codemirror/search/blob/4db1811530f9e77239e5de8bd7bb155ffc0377e0/src/search.ts#L766 |
| 27 | + * |
| 28 | + * It may also be nice to have a X of X label for results, similar to vscode. |
| 29 | + */ |
| 30 | +export default function SearchPanel({ view, closePanel, isMobile }) { |
| 31 | + // Seed initial state from whatever query is already active in the editor, |
| 32 | + // falling back to an empty query. |
| 33 | + const initialQuery = view |
| 34 | + ? getSearchQuery(view.state) |
| 35 | + : new SearchQuery({ search: '' }); |
| 36 | + |
| 37 | + const [search, setSearch] = useState(initialQuery.search); |
| 38 | + const [replace, setReplace] = useState(initialQuery.replace); |
| 39 | + const [caseSensitive, setCaseSensitive] = useState( |
| 40 | + initialQuery.caseSensitive |
| 41 | + ); |
| 42 | + const [regexp, setRegexp] = useState(initialQuery.regexp); |
| 43 | + const [showReplace, setShowReplace] = useState(false); |
| 44 | + |
| 45 | + const searchFieldRef = useRef(null); |
| 46 | + const replaceFieldRef = useRef(null); |
| 47 | + |
| 48 | + // Keep a ref of the "last committed" query so we can avoid redundant dispatches, |
| 49 | + // same as the original's `this.query` comparison. |
| 50 | + const lastQueryRef = useRef(initialQuery); |
| 51 | + |
| 52 | + useEffect(() => { |
| 53 | + if (view) { |
| 54 | + openSearchPanel(view); |
| 55 | + } |
| 56 | + |
| 57 | + return () => { |
| 58 | + if (view) { |
| 59 | + closeSearchPanel(view); |
| 60 | + view.focus(); |
| 61 | + } |
| 62 | + }; |
| 63 | + }, [view]); |
| 64 | + |
| 65 | + // Push the current form state into the editor as a SearchQuery. |
| 66 | + const commit = useCallback(() => { |
| 67 | + if (!view) return; |
| 68 | + const query = new SearchQuery({ |
| 69 | + search, |
| 70 | + caseSensitive, |
| 71 | + regexp, |
| 72 | + replace |
| 73 | + }); |
| 74 | + if (!query.eq(lastQueryRef.current)) { |
| 75 | + lastQueryRef.current = query; |
| 76 | + view.dispatch({ effects: setSearchQuery.of(query) }); |
| 77 | + } |
| 78 | + }, [view, search, replace, caseSensitive, regexp]); |
| 79 | + |
| 80 | + // Re-commit whenever any field changes (mirrors onchange/onkeyup -> commit()). |
| 81 | + useEffect(() => { |
| 82 | + commit(); |
| 83 | + }, [commit]); |
| 84 | + |
| 85 | + // Focus + select the search field on mount, like the original's mount(). |
| 86 | + useEffect(() => { |
| 87 | + searchFieldRef.current?.select(); |
| 88 | + }, []); |
| 89 | + |
| 90 | + const handleKeyDown = (e) => { |
| 91 | + if (view && runScopeHandlers(view, e.nativeEvent, 'search-panel')) { |
| 92 | + e.preventDefault(); |
| 93 | + return; |
| 94 | + } |
| 95 | + if (e.keyCode === 13 && e.target === searchFieldRef.current) { |
| 96 | + e.preventDefault(); |
| 97 | + if (view) (e.shiftKey ? findPrevious : findNext)(view); |
| 98 | + } else if (e.keyCode === 13 && e.target === replaceFieldRef.current) { |
| 99 | + e.preventDefault(); |
| 100 | + if (view) replaceNext(view); |
| 101 | + } else if (e.keyCode === 27) { |
| 102 | + // Escape closes the panel |
| 103 | + e.preventDefault(); |
| 104 | + closePanel(); |
| 105 | + } |
| 106 | + }; |
| 107 | + |
| 108 | + return ( |
| 109 | + <> |
| 110 | + {/* The <div> element TODO */} |
| 111 | + {/* eslint-disable-next-line jsx-a11y/no-static-element-interactions */} |
| 112 | + <div |
| 113 | + className={`cm-search-panel ${isMobile ? 'mobile' : ''}`} |
| 114 | + onKeyDown={handleKeyDown} |
| 115 | + > |
| 116 | + <button |
| 117 | + type="button" |
| 118 | + aria-label="close" |
| 119 | + onClick={closePanel} |
| 120 | + className="cm-search-close" |
| 121 | + > |
| 122 | + <CrossSvg focusable="false" aria-hidden="true" /> |
| 123 | + </button> |
| 124 | + <button |
| 125 | + type="button" |
| 126 | + className="cm-search-toggle-replace" |
| 127 | + onClick={() => setShowReplace((s) => !s)} |
| 128 | + aria-expanded={showReplace} |
| 129 | + aria-controls="cm-search-replace-row" |
| 130 | + title={showReplace ? 'Hide replace' : 'Show replace'} |
| 131 | + > |
| 132 | + <CaretArrowSvg |
| 133 | + aria-hidden="true" |
| 134 | + style={{ |
| 135 | + transform: showReplace ? 'rotate(90deg)' : 'rotate(0deg)', |
| 136 | + transition: 'transform 0.18s ease' |
| 137 | + }} |
| 138 | + /> |
| 139 | + </button> |
| 140 | + <div className="cm-search-content"> |
| 141 | + <div className="cm-search-row cm-search-find"> |
| 142 | + <div className="cm-search-findContainer"> |
| 143 | + <input |
| 144 | + ref={searchFieldRef} |
| 145 | + type="text" |
| 146 | + name="search" |
| 147 | + className="cm-textfield" |
| 148 | + placeholder="Find" |
| 149 | + aria-label="Find" |
| 150 | + value={search} |
| 151 | + onChange={(e) => setSearch(e.target.value)} |
| 152 | + /> |
| 153 | + <div className="cm-search-findOptions"> |
| 154 | + <label htmlFor="cm-search-caseSensitive"> |
| 155 | + <input |
| 156 | + id="cm-search-caseSensitive" |
| 157 | + type="checkbox" |
| 158 | + checked={caseSensitive} |
| 159 | + onChange={(e) => setCaseSensitive(e.target.checked)} |
| 160 | + /> |
| 161 | + <MatchCaseSvg focusable="false" aria-hidden="true" /> |
| 162 | + </label> |
| 163 | + <label htmlFor="cm-search-regexp"> |
| 164 | + <input |
| 165 | + id="cm-search-regexp" |
| 166 | + type="checkbox" |
| 167 | + checked={regexp} |
| 168 | + onChange={(e) => setRegexp(e.target.checked)} |
| 169 | + /> |
| 170 | + <RegexSvg focusable="false" aria-hidden="true" /> |
| 171 | + </label> |
| 172 | + </div> |
| 173 | + </div> |
| 174 | + <button |
| 175 | + type="button" |
| 176 | + className="cm-search-button cm-search-next" |
| 177 | + onClick={() => view && findNext(view)} |
| 178 | + > |
| 179 | + <ArrowSvg aria-label="Find next search" /> |
| 180 | + </button> |
| 181 | + <button |
| 182 | + type="button" |
| 183 | + className="cm-search-button cm-search-previous" |
| 184 | + onClick={() => view && findPrevious(view)} |
| 185 | + > |
| 186 | + <ArrowSvg aria-label="Find previous search" /> |
| 187 | + </button> |
| 188 | + </div> |
| 189 | + {showReplace && ( |
| 190 | + <div |
| 191 | + id="cm-search-replace-row" |
| 192 | + className="cm-search-row cm-search-replace" |
| 193 | + > |
| 194 | + <input |
| 195 | + ref={replaceFieldRef} |
| 196 | + type="text" |
| 197 | + name="replace" |
| 198 | + className="cm-textfield" |
| 199 | + placeholder="Replace" |
| 200 | + aria-label="Replace" |
| 201 | + value={replace} |
| 202 | + onChange={(e) => setReplace(e.target.value)} |
| 203 | + /> |
| 204 | + <button |
| 205 | + className="cm-search-button" |
| 206 | + onClick={() => { |
| 207 | + console.log('replace next'); |
| 208 | + if (view) replaceNext(view); |
| 209 | + }} |
| 210 | + > |
| 211 | + replace |
| 212 | + </button> |
| 213 | + <button |
| 214 | + className="cm-search-button" |
| 215 | + onClick={() => view && replaceAll(view)} |
| 216 | + > |
| 217 | + replace all |
| 218 | + </button> |
| 219 | + </div> |
| 220 | + )} |
| 221 | + </div> |
| 222 | + </div> |
| 223 | + </> |
| 224 | + ); |
| 225 | +} |
| 226 | + |
| 227 | +SearchPanel.propTypes = { |
| 228 | + view: PropTypes.shape({ |
| 229 | + dispatch: PropTypes.func.isRequired, |
| 230 | + state: PropTypes.shape({}), |
| 231 | + focus: PropTypes.func.isRequired |
| 232 | + }), |
| 233 | + closePanel: PropTypes.func.isRequired, |
| 234 | + isMobile: PropTypes.bool |
| 235 | +}; |
| 236 | + |
| 237 | +SearchPanel.defaultProps = { |
| 238 | + view: null, |
| 239 | + isMobile: false |
| 240 | +}; |
0 commit comments