# slate-instant-replace

> A Slate plugin that gives you full power on the last word your user typed.

Latest version **0.1.17** (published 2022-03-27) · MIT license · 0 weekly downloads

## Install

```sh
npm install slate-instant-replace
pnpm add slate-instant-replace
yarn add slate-instant-replace
bun add slate-instant-replace
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.17 |
| Published | 2022-03-27 |
| First published | 2018-06-14 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 9.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 47 |
| Author | Enzo Ferey |
| Maintainers | enzoferey |
| Keywords | slate, autocomplete, replace, editor, last, word |

## Links

- npm: https://www.npmjs.com/package/slate-instant-replace
- Repository: https://github.com/enzoferey/slate-instant-replace
- Homepage: https://github.com/enzoferey/slate-instant-replace#readme
- Issues: https://github.com/enzoferey/slate-instant-replace/issues
- npm.io page: https://npm.io/package/slate-instant-replace

## Alternatives

- [ext-list](https://npm.io/package/ext-list.md) — 6.3M weekly downloads
- [@lexical/selection](https://npm.io/package/@lexical/selection.md) — 3.8M weekly downloads
- [@lexical/text](https://npm.io/package/@lexical/text.md) — 3.6M weekly downloads
- [@lexical/clipboard](https://npm.io/package/@lexical/clipboard.md) — 3.0M weekly downloads
- [@tiptap/extension-mention](https://npm.io/package/@tiptap/extension-mention.md) — 3.0M weekly downloads

## Recent versions

- 0.1.17 (latest) — 2022-03-27
- 0.1.16 — 2020-08-29
- 0.1.15 — 2019-12-26
- 0.1.14 — 2019-12-05
- 0.1.13 — 2019-11-05
- 0.1.12 — 2019-11-04
- 0.1.11 — 2019-07-12
- 0.1.10 — 2019-03-31
- 0.1.9 — 2018-11-22
- 0.1.8 — 2018-08-03
- 0.1.7 — 2018-07-13
- 0.1.6 — 2018-07-03
- 0.1.5 — 2018-06-14
- 0.1.4 — 2018-06-14
- 0.1.3 — 2018-06-14
- … 3 more at https://npm.io/package/slate-instant-replace/versions

## README

# slate-instant-replace

![Test CI](https://github.com/enzoferey/slate-instant-replace/actions/workflows/test.yml/badge.svg)

A Slate plugin that gives you full power on the last word your user typed.

⚠️ This package is compatible with `<= slate@0.47`, please refer to [this issue](https://github.com/enzoferey/slate-instant-replace/issues/14) if you are working on `>= slate@0.50` ⚠️

#### Why ?

I was looking at some plugin to automatically replace some text at the very same moment that you press the key that completes the word you want to replace. There is already a SlateJS [auto replace plugin](https://github.com/ianstormtaylor/slate-plugins/tree/master/packages/slate-auto-replace), however you need to specify a key to trigger the replacement and this key cannot be a part of the replaced text.

#### How ?

This plugin applies your transformations every time you write a new letter and has no opinion on the changes you make to the SlateJS's `editor` instance. Your transformation functions will receive as arguments the editor instance and the last word write and the rest is up to you !

## Install

```sh
npm install --save slate-instant-replace
yarn add slate-instant-replace
```

## Usage

```jsx
import InstantReplace from "slate-instant-replace";
import { Editor } from "slate-react";

const YourFunction = (editor, lastWord) => {
  ...
}

// Add the plugin to your set of plugins...
const plugins = [InstantReplace(YourFunction)];

// And later pass it into the Slate editor...
<Editor
  ...
  plugins={plugins}
/>
```

#### Multiple transforms

You can also apply a set of multiple replacement functions, in that case the argument needs to be an array of functions like (in which case the function will be applied in the same order you write them):

```jsx
const plugins = [InstantReplace([YourFunction1, YourFunction2, YourFunction3])];
```

## Options

| Option          | Type                           | Description                                                                                                                                                                         |
| --------------- | ------------------------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **`transform`** | `Function` `ArrayOf(Function)` | The transforms to apply to the `change` object each time a letter is pressed. If an array is passed, the functions will be applied from the first element of the array to the last. |

## Commands and queries

Additionally this plugins exposes the follow [commands and queries](https://docs.slatejs.org/v/v0.47/guides/commands-and-queries) if you want to reuse them for your other plugins:

| Name                    | Type      | Description                                                                               |
| ----------------------- | --------- | ----------------------------------------------------------------------------------------- |
| getSelection            | `query`   | Returns the current selection.                                                            |
| getCurrentWordOffset    | `query`   | Returns the offset of the current word in the node.                                       |
| getLastWord             | `query`   | Returns the last word using as a reference the current anchor position.                   |
| getPreviousNode         | `query`   | Returns the previous node to the one currently focused.                                   |
| isFirstCharOfNode       | `query`   | Returns true if the anchor is at position 0 of the current node.                          |
| focusPreviousInlineNode | `command` | Focuses the previous [`Inline`](https://docs.slatejs.org/v/v0.47/slate-core/inline) node. |

Commands and queries get automatically attached to the `editor` instance. This means you can use them like this:

```js
editor.getSelection();
editor.focusPreviousInlineNode();
```

## Examples

- Emoji auto replacement ([check source code](https://github.com/enzoferey/slate-instant-replace/blob/master/examples/emojis-example/src/Slate.js))

![Gif Emoji demo](./examples/emojis-example/demo.gif "Gif Emoji demo")

- URL inline-node auto insert ([check source code](https://github.com/enzoferey/slate-instant-replace/blob/master/examples/urls-example/src/Slate.js))

![Gif URL demo](./examples/urls-example/demo.gif "Gif URL demo")

- Multiples transforms (hello => hi + emoji auto replacement) ([check source code](https://github.com/enzoferey/slate-instant-replace/blob/master/examples/multiple-transforms-example/src/Slate.js))

![Gif multiple transforms demo](./examples/multiple-transforms-example/demo.gif "Gif multiple transforms demo")

---
_Source: https://npm.io/package/slate-instant-replace · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
