1.2.1 • Published 1 month ago

editorjs-indent-tune v1.2.1

Weekly downloads
-
License
MIT
Repository
github
Last release
1 month ago

EditorJS Indent Tune

Indent feature for Editor.js.

npm.io

Instalation

Install via NPM

npm i editorjs-indent-tune

Load from CDN

Require this script on a page with Editor.js.

<script src="https://cdn.jsdelivr.net/npm/editorjs-indent-tune/dist/bundle.js"><script>

Usage

import EditorJS from '@editorjs/editorjs'
import IndentTune from 'editorjs-indent-tune'

/**
 * Editor.js configuration
 */
const editor = new EditorJS({
    /**
     * Connect tool
     */
    tools: {
        indentTune: IndentTune,
    },

    /**
     * Apply to all the blocks
     */
    tunes: ['indentTune'],

    // ...
})

Optionally, you can connect this Tune only for specified blocks:

const editor = new EditorJS({
    tools: {
        indentTune: IndentTune,
        paragraph: {
            // apply only for the 'paragraph' tool
            tunes: ['indentTune'],
        },
    },
})

You can disable this tune for a specific block by not adding it in the tunes array

const editor = new EditorJS({
    tools: {
        someOtherTool: {
            //...
            tunes: [
                /* all other tunes except those you dont want*/
            ],
        },
    },
})

Complete example:

import IndentTune, { type IndentTuneConfig } from 'editorjs-indent-tune'

const editor = new EditorJS({
    tools: {

        someOtherBlock: {
            //...
        },
        indentTune: {
            class: IndentTune,
            config: {
                customBlockIndentLimits: {
                    someOtherBlock: { max: 5 },
                },
                maxIndent: 10,
                indentSize: 30,
                multiblock: true,
                tuneName: 'indentTune',
                // If you use typescript
            } as IndentTuneConfig,
        },
    },
})

What if my editor also has a text alignment tune?

You can use the directionChangeHandler config field for that.

Here is one solution:

class MyAlignmentTuneClass /* extends maybe other class */ {
    private block
    constructor({ block }) {
        this.block = block
        // ...
    }

    private static listeners = new Set()
    public static addChangeListener(listener: (blockId: string, direction: 'ltr' | 'rtl') => void) {
        MyAlignmentTuneClass.listeners.add(l)
    }

    private onChange(alignment) {
        //...
        MyAlignmentTuneClass.listeners.forEach((l) => l(this.block.id, alignment == 'left' ? 'ltr' : 'rtl'))
        //...
    }

    public wrap(blockContent) {
        //...
        AlignmentBlockTune.listeners.forEach((l) => l(this.block.id, this.data.alignment == 'left' ? 'ltr' : 'rtl'))
        //...
    }
}

const editor = new EditorJS({
    tools: {
        alignmentTune: {
            class: MyAlignmentTuneClass,
        },
        indentTune: {
            class: IndentTune,
            config: {
                directionChangeHandler: MyAlignmentTuneClass.addChangeListener,
            },
        },
    },
})

You're free to use whatever implementation you wish.

Config Params (optional)

FieldTypeDescriptionDefault
indentSizenumberSize of one indent level (in pixels)24
maxIndentnumberThe upper indent limit of any block8
minIndentnumberThe lower indent limit of any block0
orientation'horizontal' \| 'vertical'The UI design for how you want the toolbox to be displayedhorizontal
customBlockIndentLimitsRecord<string, Partial<Record<'min' \| 'max', number>>>A set of overrides of the indent limit for each type of block{}
multiblockbooleanMarks if you can indent multiple blocks at a timefalse
tuneNamestring \| nullThis is required for multiblock to worknull
handleShortcut((e:KeyboardEvent, blockId:string) => 'unindent' \| 'indent' \| 'default' \| void) \| undefinedCustom shortcut function that allows overriding the default indenting using keyboardundefined
direction'ltr' \| 'rtl'Specify the global direction of the indentsltr
directionChangeHandlernull \| (listener: (blockId: string, direction: TextDirection) => void): voidIf provided will be used to apply visual changes (indent direction) based on the provided change value.null
1.2.1

1 month ago

1.2.0

1 month ago

1.1.1

1 month ago

1.1.0

1 month ago

1.0.11

4 months ago

1.0.10

4 months ago

1.0.8

4 months ago

1.0.7

4 months ago

1.0.6

4 months ago

1.0.5

4 months ago

1.0.4

4 months ago

1.0.3

4 months ago

1.0.2

4 months ago

1.0.1

4 months ago

1.0.0

4 months ago