# editorjs-block-shortcut

> Shortcuts for blocks like in Notion

Latest version **1.0.8** (published 2023-11-21) · MIT license · 0 weekly downloads

## Install

```sh
npm install editorjs-block-shortcut
pnpm add editorjs-block-shortcut
yarn add editorjs-block-shortcut
bun add editorjs-block-shortcut
```

## Health

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

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.8 |
| Published | 2023-11-21 |
| First published | 2023-10-28 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 11.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Kalimat Dev Group |
| Maintainers | imangazalievm |
| Keywords | editorjs, editor.js, shortcut, plugin |

## Links

- npm: https://www.npmjs.com/package/editorjs-block-shortcut
- Repository: https://github.com/kalimat-dev/editorjs-block-shortcuts
- Homepage: https://github.com/kalimat-dev/editorjs-block-shortcuts#readme
- Issues: https://github.com/kalimat-dev/editorjs-block-shortcuts/issues
- npm.io page: https://npm.io/package/editorjs-block-shortcut

## 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

- 1.0.8 (latest) — 2023-11-21
- 1.0.7 — 2023-11-10
- 1.0.5 — 2023-11-10
- 1.0.4 — 2023-11-10
- 1.0.3 — 2023-11-09
- 1.0.2 — 2023-11-09
- 1.0.1 — 2023-11-08
- 1.0.0 — 2023-10-28

## README

<p align="center">
    <picture>
      <source media="(prefers-color-scheme: dark)"  srcset="./assets/logo_night.png">
      <source media="(prefers-color-scheme: light)" srcset="./assets/logo_day.png">
      <img alt="Block Shortcuts Logo" src="./assets/logo_day.png">
    </picture>    
</p>

<p align="center">
  <a href="https://www.npmjs.com/package/editorjs-block-shortcut">
    <img src="https://flat.badgen.net/npm/v/editorjs-block-shortcut?icon=npm" alt="npm"/>
  </a>
</p>

Block shortcut feature for Editor.js (like in Notion)

## Installation

Insall the package

```shell
npm install editorjs-block-shortcut
```

## Usage

Minimal configuration:

```typescript
import { BlockShortcuts } from 'editorjs-block-shortcut'
import Header from '@editorjs/header'

const blockConverters = [
    {
        shortcut: ['#'],
        converter: (shortcutText) => ({
            type: 'header',
            data: {
                text: '',
                level: 1,
            },
            config: null,
        }),
    }
]

const editor = new EditorJS({
    tools: {
        header: Header,
    },
    onReady: () => {
        new BlockShortcuts({ editor })
    },
});
```

If a user input '#' and press "Space" in the beginning some block it will be converted to the header block.

### Multiple shortcuts

You can specify multiple shortcuts:

```typescript
import { BlockShortcuts } from 'editorjs-block-shortcut'
import Header from '@editorjs/header'

const blockConverters = [
    {
        shortcuts: ['-', '*'],
        converter: (shortcutText) => ({
            type: 'list',
            data: {
                style: 'unordered',
                items: [],
            },
            config: null,
        }),
    }
]
```

### Regex shortcuts

The plugin also allows you to validate shortcuts using regular expressions. This can be useful, for example, in blocks
with different variations. Let's look at an example with headers:

```typescript
import { BlockShortcuts } from 'editorjs-block-shortcut'
import Header from '@editorjs/header'

const blockConverters = [
    {
        shortcut: [/[#]{1,6}}/],
        converter: (shortcutText) => ({
            type: 'header',
            data: {
                text: '',
                level: shortcutText.length,
            },
            config: null,
        })
    }
]
```

Now, when you enter # or ##, the block will be converted to block H1 or H2, respectively.

### Retrieving data from an old block

Sometimes it is necessary not only to convert one block into another, but also to preserve the data from the original
block. Below is an example of how to do this:

```typescript
import { SavedData } from '@editorjs/editorjs/types/data-formats'
import { BlockShortcuts } from 'editorjs-block-shortcut'
import Header from '@editorjs/header'

const blockConverters = [
    {
        shortcut: [/[#]{1,6}}/],
        converter: (
            shortcutText: string,
            oldBlockType: string,
            oldBlockData: SavedData | void,
        ) => ({
            type: 'header',
            data: {
                text: (oldBlockData as SavedData).data.text,
                level: shortcutText.length,
            },
            config: null,
        })
    }
]
```

### Enabling converting only for specific blocks

You can specify that the converter only works for certain blocks using the xxx option:

```typescript
import { BlockShortcuts } from 'editorjs-block-shortcut'
import Header from '@editorjs/header'

const blockConverters = [
    {
        shortcut: [/[#]{1,6}}/],
        enabledFor: ['paragraph'],
        converter: (shortcutText) => ({
            type: 'header',
            data: {
                text: '',
                level: shortcutText.length,
            },
            config: null,
        })
    }
]
```

By default, converters apply to all block types.

### Comparing blocks

Notion allows us to convert one heading level to another, but with the default configuration of the converter this will
not be possible. By default, the library compares the old and new block by type, and if the types are the same, then the
conversion will not occur.

To correct this behavior, you can specify your own block comparison function, which, in addition to the type, also
compares the header level:

```typescript
import { SavedData } from '@editorjs/editorjs/types/data-formats'
import { BlockShortcuts } from 'editorjs-block-shortcut'
import Header from '@editorjs/header'

const blockConverters = [
    {
        shortcut: [/[#]{1,6}}/],
        enabledFor: ['paragraph', 'header'],
        blockTypeComparator: (
            newBlockType: string,
            oldBlockType: string,
            oldBlockData: BlockToolData,
            newBlockData: BlockToolData,
        ) => {
            return newBlockType != oldBlockType || oldBlockData.level != newBlockData.level
        },
        converter: (
            shortcutText: string,
            oldBlockType: string,
            oldBlockData: SavedData | void,
        ) => ({
            type: 'header',
            data: {
                text: (oldBlockData as SavedData).data.text,
                level: shortcutText.length,
            },
            config: null,
        })
    }
]
```

## Config Params

Converter config supports the options (* - required option):

| Field               | Type     | Description        |
|---------------------|----------| ------------------ |
| shortcuts *         | <code>(string &#124; RegExp)[]</code> | |
| converter *         | `BlockConverter` | |
| enabledFor          | `string[]` | |
| blockTypeComparator | `BlockTypeComparator` | |

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