From 6c10766a40347137db3e33fb92df69e387b52149 Mon Sep 17 00:00:00 2001 From: Yifei Date: Wed, 27 Nov 2024 18:28:11 +0800 Subject: [PATCH] style: fix lint --- .../decorateSearchHighlight/search/empty.spec.ts | 2 +- .../decorateSearchHighlight/search/text.spec.ts | 10 +++++----- packages/find-replace/src/lib/decorateFindReplace.ts | 11 ++++++----- 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/packages/find-replace/src/lib/__tests__/decorateSearchHighlight/search/empty.spec.ts b/packages/find-replace/src/lib/__tests__/decorateSearchHighlight/search/empty.spec.ts index affcaab872..6c2a2b9c2d 100644 --- a/packages/find-replace/src/lib/__tests__/decorateSearchHighlight/search/empty.spec.ts +++ b/packages/find-replace/src/lib/__tests__/decorateSearchHighlight/search/empty.spec.ts @@ -16,7 +16,7 @@ it('should be', () => { expect( decorateFindReplace({ ...getEditorPlugin(editor, FindReplacePlugin), - entry: [{ type: 'p', children: [{ text: '' }] }, [0]], + entry: [{ children: [{ text: '' }], type: 'p' }, [0]], }) ).toEqual(output); }); diff --git a/packages/find-replace/src/lib/__tests__/decorateSearchHighlight/search/text.spec.ts b/packages/find-replace/src/lib/__tests__/decorateSearchHighlight/search/text.spec.ts index 3eaef23505..98dc2525f0 100644 --- a/packages/find-replace/src/lib/__tests__/decorateSearchHighlight/search/text.spec.ts +++ b/packages/find-replace/src/lib/__tests__/decorateSearchHighlight/search/text.spec.ts @@ -15,7 +15,7 @@ it('should decorate matching text', () => { expect( plugin.decorate?.({ ...getEditorPlugin(editor, plugin), - entry: [{ type: 'p', children: [{ text: 'test' }] }, [0]], + entry: [{ children: [{ text: 'test' }], type: 'p' }, [0]], }) ).toEqual([ { @@ -45,7 +45,7 @@ it('should decorate matching text case-insensitively', () => { expect( plugin.decorate?.({ ...getEditorPlugin(editor, plugin), - entry: [{ type: 'p', children: [{ text: 'test' }] }, [0]], + entry: [{ children: [{ text: 'test' }], type: 'p' }, [0]], }) ).toEqual([ { @@ -76,7 +76,7 @@ it('should decorate matching consecutive text nodes', () => { plugin.decorate?.({ ...getEditorPlugin(editor, plugin), entry: [ - { type: 'p', children: [{ text: 'tes' }, { text: 't', bold: true }] }, + { children: [{ text: 'tes' }, { bold: true, text: 't' }], type: 'p' }, [0], ], }) @@ -122,12 +122,12 @@ it('should decorate matching multiple occurrences', () => { ...getEditorPlugin(editor, plugin), entry: [ { - type: 'p', children: [ { text: 'tes' }, - { text: 'ts and tests and t', bold: true }, + { bold: true, text: 'ts and tests and t' }, { text: 'ests' }, ], + type: 'p', }, [0], ], diff --git a/packages/find-replace/src/lib/decorateFindReplace.ts b/packages/find-replace/src/lib/decorateFindReplace.ts index e780cd3237..2b7a640a2b 100644 --- a/packages/find-replace/src/lib/decorateFindReplace.ts +++ b/packages/find-replace/src/lib/decorateFindReplace.ts @@ -22,12 +22,13 @@ export const decorateFindReplace: Decorate = ({ let start = 0; const matches: number[] = []; + while ((start = str.indexOf(searchLower, start)) !== -1) { matches.push(start); start += searchLower.length; } - if (!matches.length) { + if (matches.length === 0) { return []; } @@ -47,6 +48,7 @@ export const decorateFindReplace: Decorate = ({ // If the match ends before the start of the current text, move to the next match if (matchEnd <= textStart) { matchIndex++; + continue; } @@ -66,18 +68,17 @@ export const decorateFindReplace: Decorate = ({ ranges.push({ anchor: { - path: textNodePath, offset: anchorOffset, + path: textNodePath, }, focus: { - path: textNodePath, offset: focusOffset, + path: textNodePath, }, - search: search.substring(searchOverlapStart, searchOverlapEnd), + search: search.slice(searchOverlapStart, searchOverlapEnd), [type]: true, }); } - // If the match ends within the current text, move to the next match if (matchEnd <= textEnd) { matchIndex++;