Skip to content

Commit

Permalink
wip: rename search ext => find ext
Browse files Browse the repository at this point in the history
  • Loading branch information
ocavue committed Nov 2, 2022
1 parent e5b8b52 commit dd05924
Show file tree
Hide file tree
Showing 16 changed files with 262 additions and 1,736 deletions.
1 change: 0 additions & 1 deletion packages/remirror/src/extensions.ts
Expand Up @@ -35,7 +35,6 @@ export * from '@remirror/extension-node-formatting';
export * from '@remirror/extension-paragraph';
export * from '@remirror/extension-placeholder';
export * from '@remirror/extension-positioner';
export * from '@remirror/extension-search';
export * from '@remirror/extension-shortcuts';
export * from '@remirror/extension-strike';
export * from '@remirror/extension-sub';
Expand Down
@@ -1,27 +1,27 @@
import { extensionValidityTest, renderEditor } from 'jest-remirror';

import { SearchExtension, SearchOptions } from '../';
import { FindExtension, FindOptions } from '../';

extensionValidityTest(SearchExtension);
extensionValidityTest(FindExtension);

function create(options?: SearchOptions) {
function create(options?: FindOptions) {
const {
add,
nodes: { p, doc },
view,
commands,
helpers,
} = renderEditor<SearchExtension>([new SearchExtension(options)]);
} = renderEditor<FindExtension>([new FindExtension(options)]);
const node = doc(p('Welcome'), p('friend'), p('welcome friend'));
add(node);
return { p, doc, view, commands, helpers, node, add };
}

describe('helpers and commands', () => {
it('search', () => {
it('find', () => {
const { view, helpers } = create();

let result = helpers.search({ searchTerm: 'welcome', caseSensitive: true });
let result = helpers.find({ text: 'welcome', caseSensitive: true });
expect(result.activeIndex).toBeUndefined();
expect(result.ranges).toHaveLength(1);
expect(view.dom.innerHTML).toMatchInlineSnapshot(`
Expand All @@ -39,7 +39,7 @@ describe('helpers and commands', () => {
</p>
`);

result = helpers.search({ searchTerm: 'friend', activeIndex: -1 });
result = helpers.find({ text: 'friend', activeIndex: -1 });
expect(result.activeIndex).toBe(1);
expect(result.ranges).toHaveLength(2);
expect(view.dom.innerHTML).toMatchInlineSnapshot(`
Expand All @@ -64,30 +64,30 @@ describe('helpers and commands', () => {
const { add, doc, p, helpers } = create();

// C++ is an invalid regexp
const searchTerm = 'C++';
expect(() => new RegExp(searchTerm)).toThrow();
const text = 'C++';
expect(() => new RegExp(text)).toThrow();
add(doc(p('C++'), p('++C C++')));
expect(helpers.search({ searchTerm }).ranges).toHaveLength(2);
expect(helpers.find({ text }).ranges).toHaveLength(2);
});

it('can handle a regexp-like search term', () => {
it('can handle a regexp-like find term', () => {
const { add, doc, p, helpers } = create();

const searchTerm = '.*';
expect(() => new RegExp(searchTerm)).not.toThrow();
const text = '.*';
expect(() => new RegExp(text)).not.toThrow();
add(doc(p('Hello world'), p('')));

// This search term should not be treat as a regexp and it should not match anything
expect(helpers.search({ searchTerm }).ranges).toHaveLength(0);
// This find term should not be treat as a regexp and it should not match anything
expect(helpers.find({ text }).ranges).toHaveLength(0);

add(doc(p('Hello world'), p('.*')));
expect(helpers.search({ searchTerm }).ranges).toHaveLength(1);
expect(helpers.find({ text }).ranges).toHaveLength(1);
});

it('findAndReplace', () => {
const { view, commands } = create();
commands.findAndReplace({
searchTerm: 'friend',
text: 'friend',
caseSensitive: true,
replacement: 'FRIEND',
});
Expand All @@ -110,7 +110,7 @@ describe('helpers and commands', () => {
it('findAndReplaceAll', () => {
const { view, commands } = create();
commands.findAndReplaceAll({
searchTerm: 'friend',
text: 'friend',
caseSensitive: false,
replacement: 'FRIEND',
});
Expand All @@ -129,9 +129,9 @@ describe('helpers and commands', () => {

it('clearResult', () => {
const { view, commands } = create();
commands.startSearch({ searchTerm: 'friend' });
commands.startFind({ text: 'friend' });
expect(view.dom.innerHTML).toContain('background-color');
commands.stopSearch();
commands.stopFind();
expect(view.dom.innerHTML).not.toContain('background-color');
});
});
@@ -1,4 +1,4 @@
import { rotateIndex } from '../src/search-utils';
import { rotateIndex } from '../src/find-utils';

test('rotateIndex', () => {
expect(rotateIndex(0, 10)).toBe(0);
Expand Down
@@ -1,16 +1,16 @@
{
"name": "@remirror/extension-search",
"version": "2.0.7",
"description": "Find all of your favourite things in the remirror editor.",
"name": "@remirror/extension-find",
"version": "0.0.0",
"description": "Find and replace text in the remirror editor.",
"keywords": [
"remirror",
"extension"
],
"homepage": "https://github.com/remirror/remirror/tree/HEAD/packages/remirror__extension-search",
"homepage": "https://github.com/remirror/remirror/tree/HEAD/packages/remirror__extension-find",
"repository": {
"type": "git",
"url": "https://github.com/remirror/remirror.git",
"directory": "packages/remirror__extension-search"
"directory": "packages/remirror__extension-find"
},
"license": "MIT",
"contributors": [
Expand All @@ -20,14 +20,14 @@
"type": "module",
"exports": {
".": {
"import": "./dist/remirror-extension-search.js",
"require": "./dist/remirror-extension-search.cjs",
"import": "./dist/remirror-extension-find.js",
"require": "./dist/remirror-extension-find.cjs",
"types": "./dist-types/index.d.ts"
},
"./package.json": "./package.json"
},
"main": "./dist/remirror-extension-search.cjs",
"module": "./dist/remirror-extension-search.js",
"main": "./dist/remirror-extension-find.cjs",
"module": "./dist/remirror-extension-find.js",
"types": "./dist-types/index.d.ts",
"files": [
"dist",
Expand Down
26 changes: 26 additions & 0 deletions packages/remirror__extension-find/readme.md
@@ -0,0 +1,26 @@
# @remirror/extension-find

> Find all of your favourite things in the remirror editor.
[![Version][version]][npm] [![Weekly Downloads][downloads-badge]][npm] [![Bundled size][size-badge]][size] [![Typed Codebase][typescript]](#) [![MIT License][license]](#)

[version]: https://flat.badgen.net/npm/v/@remirror/extension-find
[npm]: https://npmjs.com/package/@remirror/extension-find
[license]: https://flat.badgen.net/badge/license/MIT/purple
[size]: https://bundlephobia.com/result?p=@remirror/extension-find
[size-badge]: https://flat.badgen.net/bundlephobia/minzip/@remirror/extension-find
[typescript]: https://flat.badgen.net/badge/icon/TypeScript?icon=typescript&label
[downloads-badge]: https://badgen.net/npm/dw/@remirror/extension-find/red?icon=npm

## Installation

```bash
# yarn
yarn add @remirror/extension-find

# pnpm
pnpm add @remirror/extension-find

# npm
npm install @remirror/extension-find
```
Expand Up @@ -16,16 +16,16 @@ import {
} from '@remirror/core';
import { Decoration, DecorationAttrs, DecorationSet } from '@remirror/pm/view';

import { FindAndReplaceAllProps, FindAndReplaceProps, FindProps, FindResult } from './search-types';
import { FindAndReplaceAllProps, FindAndReplaceProps, FindProps, FindResult } from './find-types';
import { rotateIndex } from './search-utils';

export interface SearchOptions {
export interface FindOptions {
/**
* The inline decoraton to apply to all search results.
*
* @defaultValue '{style: "background-color: yellow;"}'
*/
searchDecoration?: DecorationAttrs;
decoration?: DecorationAttrs;

/**
* The inline decoraton to apply to the active search result (if any).
Expand All @@ -39,26 +39,26 @@ export interface SearchOptions {
*
* @defaultValue false
*/
alwaysSearch?: boolean;
alwaysFind?: boolean;
}

/**
* This extension add search functionality to your editor.
*/
@extension<SearchOptions>({
@extension<FindOptions>({
defaultOptions: {
searchDecoration: { style: 'background-color: yellow;' },
decoration: { style: 'background-color: yellow;' },
activeDecoration: { style: 'background-color: orange;' },
alwaysSearch: false,
alwaysFind: false,
},
})
export class SearchExtension extends PlainExtension<SearchOptions> {
export class FindExtension extends PlainExtension<FindOptions> {
get name() {
return 'search' as const;
return 'find' as const;
}

private _updating = false;
private _searchTerm = '';
private _text = '';
private _caseSensitive = true;
private _ranges: FromToProps[] = [];
private _activeIndex?: number = undefined;
Expand All @@ -67,12 +67,12 @@ export class SearchExtension extends PlainExtension<SearchOptions> {
* Find and highlight the search result in the editor.
*/
@command()
find({ searchTerm, activeIndex, caseSensitive }: FindProps): CommandFunction {
if (!searchTerm) {
find({ text, activeIndex, caseSensitive }: FindProps): CommandFunction {
if (!text) {
return this.stopFind();
}

this._searchTerm = escapeStringRegex(searchTerm);
this._text = escapeStringRegex(text);
this._activeIndex = activeIndex;
this._caseSensitive = caseSensitive ?? false;

Expand All @@ -87,7 +87,7 @@ export class SearchExtension extends PlainExtension<SearchOptions> {
@command()
stopFind(): CommandFunction {
return ({ tr, dispatch }) => {
this._searchTerm = '';
this._text = '';
this._activeIndex = undefined;
return this.updateView(tr, dispatch);
};
Expand All @@ -98,7 +98,7 @@ export class SearchExtension extends PlainExtension<SearchOptions> {
*/
@command()
findAndReplace({ replacement, index }: FindAndReplaceProps): CommandFunction {
// TODO: use searchTerm
// TODO: use text
return (props) => {
const { tr, dispatch } = props;
index = rotateIndex(isNumber(index) ? index : this._activeIndex ?? 0, this._ranges.length);
Expand All @@ -122,10 +122,10 @@ export class SearchExtension extends PlainExtension<SearchOptions> {
*/
@command()
findAndReplaceAll({ replacement }: FindAndReplaceAllProps): CommandFunction {
// TODO: use searchTerm
// TODO: use text
return (props) => {
const { tr } = props;
const ranges = this.gatherSearchResults(tr.doc);
const ranges = this.gatherFindResults(tr.doc);

for (let i = ranges.length - 1; i >= 0; i--) {
const { from, to } = ranges[i];
Expand Down Expand Up @@ -159,9 +159,9 @@ export class SearchExtension extends PlainExtension<SearchOptions> {
return DecorationSet.empty;
},
apply: (tr, old) => {
if (this._updating || (tr.docChanged && this.options.alwaysSearch)) {
if (this._updating || (tr.docChanged && this.options.alwaysFind)) {
const doc = tr.doc;
this._ranges = this.gatherSearchResults(doc);
this._ranges = this.gatherFindResults(doc);
this.normalizeActiveIndex();
this.scrollToActiveResult();
return this.createDecorationSet(doc);
Expand All @@ -183,12 +183,12 @@ export class SearchExtension extends PlainExtension<SearchOptions> {
};
}

private gatherSearchResults(doc: ProsemirrorNode): FromToProps[] {
if (!this._searchTerm) {
private gatherFindResults(doc: ProsemirrorNode): FromToProps[] {
if (!this._text) {
return [];
}

const re = new RegExp(this._searchTerm, this._caseSensitive ? 'gu' : 'gui');
const re = new RegExp(this._text, this._caseSensitive ? 'gu' : 'gui');
const ranges: FromToProps[] = [];

doc.descendants((node, pos) => {
Expand Down Expand Up @@ -221,7 +221,7 @@ export class SearchExtension extends PlainExtension<SearchOptions> {
return Decoration.inline(
deco.from,
deco.to,
index === this._activeIndex ? this.options.activeDecoration : this.options.searchDecoration,
index === this._activeIndex ? this.options.activeDecoration : this.options.decoration,
);
});
return DecorationSet.create(doc, decorations);
Expand Down Expand Up @@ -269,7 +269,7 @@ export class SearchExtension extends PlainExtension<SearchOptions> {
declare global {
namespace Remirror {
interface AllExtensions {
search: SearchExtension;
find: FindExtension;
}
}
}
Expand Up @@ -17,7 +17,7 @@ export interface FindProps {
/**
* The text to search for.
*/
searchTerm: string;
text: string;

/**
* @defaultValue false (search is case insensitive)
Expand All @@ -35,7 +35,7 @@ export interface FindAndReplaceProps {
/**
* The text to search for.
*/
searchTerm: string;
text: string;

/**
* @defaultValue false (search is case insensitive)
Expand All @@ -58,7 +58,7 @@ export interface FindAndReplaceAllProps {
/**
* The text to search for.
*/
searchTerm: string;
text: string;

/**
* @defaultValue false (search is case insensitive)
Expand Down
8 changes: 8 additions & 0 deletions packages/remirror__extension-find/src/index.ts
@@ -0,0 +1,8 @@
export type { FindOptions } from '././find-extension';
export { FindExtension } from '././find-extension';
export type {
FindAndReplaceAllProps,
FindAndReplaceProps,
FindProps,
FindResult,
} from './find-types';

0 comments on commit dd05924

Please sign in to comment.