visbug-lib
Version:
<p align="center"> <img src="./assets/visbug.png" width="300" height="300" alt="visbug"> <br> <a href="https://www.npmjs.org/package/visbug"><img src="https://img.shields.io/npm/v/visbug.svg?style=flat" alt="npm latest version number"></a> <a href
38 lines (30 loc) • 943 B
JavaScript
import $ from 'blingblingjs'
import hotkeys from 'hotkeys-js'
import { showHideNodeLabel } from '../utilities/'
const removeEditability = ({target}) => {
target.removeAttribute('contenteditable')
target.removeAttribute('spellcheck')
target.removeEventListener('blur', removeEditability)
target.removeEventListener('keydown', stopBubbling)
hotkeys.unbind('escape,esc')
}
const stopBubbling = e => e.key != 'Escape' && e.stopPropagation()
const cleanup = (e, handler) => {
$('[spellcheck="true"]').forEach(target => removeEditability({target}))
window.getSelection().empty()
}
export function EditText(elements) {
if (!elements.length) return
elements.map(el => {
let $el = $(el)
$el.attr({
contenteditable: true,
spellcheck: true,
})
el.focus()
showHideNodeLabel(el, true)
$el.on('keydown', stopBubbling)
$el.on('blur', removeEditability)
})
hotkeys('escape,esc', cleanup)
}