npm.io
0.0.3 • Published 1 year ago

editorjs-caret-selection

Licence
MIT
Version
0.0.3
Deps
0
Size
19 kB
Vulns
0
Weekly
0
Stars
6

EditorJs Caret Selection

TIP: In order to show approximatly correct caret position, please specify font family.

Effortlessly track and simulate caret movements across any device. In real-time collaboration tools like Notion or Google Docs, you've likely noticed a caret indicating when another user is editing or viewing the same document.

I developed this feature because, as of this writing, the official EditorJs has not yet implemented it. Feel free to use, customize, and configure it to suit your needs. Cheers!

Demo

Table Of Contents

  • Installation
  • Usage
  • Realtime Caret Approach
  • Tips
  • Bugs
  • License
  • Support

Installation

npm i editorjs-caret-selection
import CaretSelection from 'editorjs-caret-selection'
import CaretSelection from 'https://github.com/w99910/editorjs-caret-selection/blob/master/dist/editorjs-caret-selection.js'
const CaretSelection = require('https://github.com/w99910/editorjs-caret-selection/blob/master/dist/editorjs-caret-selection.cjs')

Usage

First initialize the class with your editorjs object, unique id, display name.

let editor = new EditorJS({
    ...
});

let caretSelection = new CaretSelection(editor, 'my-unique-id', 'Thomas');

You can pass HTMLElement to display parameter.

let display = document.createElement('span');
display.style.background = 'red';
display.style.padding = '0px 4px';
display.style.fontSize = '10px'
display.textContent = 'Thomas';

let caretSelection = new CaretSelection(editor, 'my-unique-id', display);

Once it is initialised, the selectionChange event is listened. Register your custom function when that event is triggered.

caretSelection.onChange(function(caret){
    console.log(caret);
})

caret object contains the following attributes.

type Caret = {
    id: string | number,
    display: string | HTMLElement,
    blockIndex: number,
    startOffset: number,
    endOffset: number,
    caretStyle?: object,
    selectionBoxStyle?: object,
}

In order to show caret, use showSelection method by passing caret object.

caretSelection.showSelection(caret);

You can customize caret style and selection box style by modifying caret object.

caret.caretStyle.background = 'red'
caret.selectionBoxStyle.background = 'red'

caretSelection.showSelection(caret);

Use hideSelection method to hide specific caret by passing caret's unique-id.

caretSelection.hideSelection('my-unique-id');
let caret = caretSelection.getCurretSelection();

Tips

In order to calculate approximately correct caret position, please specify font family instead of letting browser determine the font family.

Realtime Caret Approach

I will write an article about this.

Bugs and Features

Please kindly submit your issue or request your feature here.

License

MIT

Support

I am in need of financial assistance, and I would deeply appreciate any support you can offer, even the smallest amount.

Keywords