()
| 376 | } |
| 377 | |
| 378 | private replaceAll() { |
| 379 | const query = this.getQuery(); |
| 380 | // Allow empty replacement (delete matches) by only requiring a search term. |
| 381 | if (!query.search) return; |
| 382 | |
| 383 | const searchString = this.view.state.doc.toString(); |
| 384 | const changes: { from: number; to: number; insert: string }[] = []; |
| 385 | |
| 386 | try { |
| 387 | let regex: RegExp; |
| 388 | const flags = query.caseSensitive ? "g" : "gi"; |
| 389 | const pattern = query.regexp ? query.search : this.escapeRegex(query.search); |
| 390 | |
| 391 | if (query.wholeWord) { |
| 392 | regex = new RegExp(`\\b${pattern}\\b`, flags); |
| 393 | } else { |
| 394 | regex = new RegExp(pattern, flags); |
| 395 | } |
| 396 | |
| 397 | // 查找所有匹配 |
| 398 | let execResult: RegExpExecArray | null; |
| 399 | |
| 400 | while ((execResult = regex.exec(searchString)) !== null) { |
| 401 | changes.push({ |
| 402 | from: execResult.index, |
| 403 | to: execResult.index + execResult[0].length, |
| 404 | insert: query.replace |
| 405 | }); |
| 406 | } |
| 407 | |
| 408 | if (changes.length > 0) { |
| 409 | this.view.dispatch({ changes }); |
| 410 | } |
| 411 | } catch (error) { |
| 412 | console.error("Replace all error:", error); |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | open() { |
| 417 | this.panelEl.removeClass("is-hidden"); |
no test coverage detected