forked from jaredreich/pell
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathspell.js
88 lines (84 loc) · 2.41 KB
/
spell.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
function spell() {
let exec = (command, value=null) => document.execCommand(command, false, value)
let ensureHTTP = url => /^https?:\//.test(url) ? url : `http://${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'],
['strikeThrough'],
['subscript'],
['superscript'],
],
[
['justifyLeft'],
['justifyCenter'],
['justifyRight'],
['justifyFull'],
['indent'],
['outdent']
],
[
['fontName', select(['serif','sans-serif','monospace','cursive','fantasy'])],
['fontSize', select([...Array(33)].map((_,i)=>8+i*2))],
['forecolor', colorPicker()],
['hilitecolor', colorPicker()]
].map(([cmd, input]) => [cmd, 0, Object.assign(input, { onchange: _=> exec(cmd, input.value) })]),
[
...[1, 2, 3, 4].map(n => ['heading' + n, `<h${n}>`]),
['paragraph', '<p>'],
['quote', '<blockquote>'],
['code', '<pre>']
].map(([title, format]) => [title, _=> exec('formatBlock', format)]),
[
['insertOrderedList'],
['insertUnorderedList'],
['insertHorizontalRule'],
],
[
['removeFormat'],
['unlink']
],
[
['createLink', 'link', ensureHTTP],
['insertImage','image', ensureHTTP],
['insertHTML', 'video', url => `<video controls src="${ensureHTTP(url)}">`]
].map(([cmd, type, t]) => [type, url => (url=prompt(`Enter the ${type} URL`)) && exec(cmd, t(url))]),
[
['copy'],
['cut'],
['paste']
],
[
['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
})
])
}