spellRicher

简单富文本编辑

بۇ قوليازمىنى بىۋاسىتە قاچىلاشقا بولمايدۇ. بۇ باشقا قوليازمىلارنىڭ ئىشلىتىشى ئۈچۈن تەمىنلەنگەن ئامبار بولۇپ، ئىشلىتىش ئۈچۈن مېتا كۆرسەتمىسىگە قىستۇرىدىغان كود: // @require https://update.greasyfork.org/scripts/534392/1580077/spellRicher.js

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

//altered from https://github.com/ninja33/ODH/blob/master/src/fg/js/spell.js
;

function spell() {
    let exec = (command, value = null) => document.execCommand(command, false, value)
    let ensureHTTP = url => /^https?:\//.test(url) ? url : `https://${url}`
    let $ = (tag, props, children = [], elm = document.createElement(tag)) =>
        children.map(child => child && elm.appendChild(child)) && Object.assign(elm, props)

    let colorPicker = _ => $('input', {type: 'color'})
    let select = options => $('select', {}, options.map(o => $('option', {textContent: o})))

    let buttons = {};
    let queryState = _ => {
        for (let cmd in buttons)
            buttons[cmd].classList.toggle('selected', document.queryCommandState(cmd))
    }

    let actions = [
        [
            ['bold'],
            ['italic'],
            ['underline']
        ],
        [
            ['paragraph', '<p>'],
            ['quote', '<blockquote>'],
            ['code', '<pre>']
        ].map(([title, format]) => [title, _ => exec('formatBlock', format)]),
        [
            ['insertOrderedList'],
            ['insertUnorderedList'],
            ['insertHorizontalRule'],
        ],
        [
            ['removeFormat'],
            ['unlink']
        ],
        [
            ['createLink', 'link', ensureHTTP],
            ['insertImage', 'image', img => img]
        ].map(([cmd, type, t]) => [type, url => (url = prompt(`Enter the ${type} URL`)) && exec(cmd, t(url))]),
        [
            ['undo'],
            ['redo']
        ]
    ]

    return $('div', {className: 'spell'}, [
        $('div', {className: 'spell-bar'}, actions.map(
            bar => $('div', {className: 'spell-zone'}, bar.map(
                ([cmd, onclick = _ => exec(cmd), control]) => buttons[cmd] = $('button', {
                    className: 'spell-icon',
                    title: cmd.replace(/([^a-z])/g, ' $1').toLowerCase(),
                    onclick
                }, [$('i', {className: 'icon-' + cmd.toLowerCase()}), control])
            ))
        )),
        $('div', {
            className: 'spell-content',
            contentEditable: true,
            onkeydown: event => event.which !== 9,
            onkeyup: queryState,
            onmouseup: queryState
        })
    ])
}