forked from Alamantus/FeatherWiki
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pell.js
185 lines (174 loc) · 4.88 KB
/
pell.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/**
* Modified from pell, the simplest and smallest WYSIWYG text editor for web, with no dependencies
* https://github.com/jaredreich/pell
*
* Several customization options have been removed to work with Feather Wiki specifically.
*
* @licence MIT
*/
// Shorten the names of these to help the minifier
const fb = 'formatBlock'
const ael = (parent, type, listener) => parent.addEventListener(type, listener)
const ac = (parent, child) => parent.appendChild(child)
const ce = tag => document.createElement(tag)
const qcs = command => document.queryCommandState(command)
export const exec = (command, value = null) => document.execCommand(command, false, value)
export const init = settings => {
const actions = [
{ // clear
icon: '☒',
title: 'Clear Formatting',
result: () => exec('removeFormat')
},
{ // bold
icon: '<b>B</b>',
title: 'Bold',
state: () => qcs('bold'),
result: () => exec('bold')
},
{ // italic
icon: '<i>I</i>',
title: 'Italic',
state: () => qcs('italic'),
result: () => exec('italic')
},
{ // underline
icon: '<u>U</u>',
title: 'Underline',
state: () => qcs('underline'),
result: () => exec('underline')
},
// { // strikethrough
// icon: '<strike>S</strike>',
// title: 'Strike-through',
// state: () => qcs('strikeThrough'),
// result: () => exec('strikeThrough')
// },
{ // heading2
icon: '<b>H</b>',
title: 'Heading',
result: () => exec(fb, '<h2>'),
},
{ // heading3
icon: '<b>H<sub>2</sub></b>',
title: 'Sub-Heading',
result: () => exec(fb, '<h3>'),
},
{ // paragraph
icon: '¶',
title: 'Paragraph',
result: () => exec(fb, '<p>')
},
{ // align left
icon: '↦',
title: 'Align Left',
state: () => qcs('justifyLeft'),
result: () => exec('justifyLeft')
},
{ // align center
icon: '↔',
title: 'Align Center',
state: () => qcs('justifyCenter'),
result: () => exec('justifyCenter')
},
{ // align right
icon: '↤',
title: 'Align Right',
state: () => qcs('justifyRight'),
result: () => exec('justifyRight')
},
{ // olist
icon: '#',
title: 'Number List',
result: () => exec('insertOrderedList')
},
{ // ulist
icon: '•',
title: 'Bullet List',
result: () => exec('insertUnorderedList')
},
{ // quote
icon: '“ ”',
title: 'Quote',
result: () => exec(fb, '<blockquote>')
},
// { // code
// icon: '</>',
// title: 'Code',
// result: () => exec(fb, '<pre>')
// },
{ // line
icon: '―',
title: 'Separator',
result: () => exec('insertHorizontalRule')
},
{ // link
icon: '🔗',
title: 'Link',
result: () => {
const url = window.prompt('Link URL:')
if (url) exec('createLink', url)
}
},
{ // externalImage
title: 'Link External Image',
icon: '🖼️',
result: () => {
const url = window.prompt('Image URL:');
if (url) exec('insertImage', url);
},
},
{ // insertImage
title: 'Insert Image from File',
icon: '📸',
result: settings.insert,
},
{ // existingImage
title: 'Add Existing Image',
icon: '📎',
result: () => document.getElementById('g').showModal(),
},
];
const classes = {
actionbar: 'pell-actionbar',
button: 'pell-button',
content: 'pell-content',
selected: 'pell-button-selected'
};
const actionbar = ce('div')
actionbar.className = classes.actionbar
actionbar.role = 'toolbar'
ac(settings.element, actionbar)
const content = settings.element.content = ce('div')
content.contentEditable = true
content.className = classes.content
content.oninput = ({ target: { firstChild } }) => {
if (firstChild && firstChild.nodeType === 3) exec(fb, `<p>`)
else if (content.innerHTML === '<br>') content.innerHTML = ''
settings.onChange(content.innerHTML)
}
content.onkeydown = event => {
if (event.key === 'Enter' && document.queryCommandValue(fb) === 'blockquote') {
setTimeout(() => exec(fb, `<p>`), 0)
}
}
ac(settings.element, content)
actions.forEach(action => {
const button = ce('button')
button.className = classes.button
button.innerHTML = action.icon
button.title = action.title
button.setAttribute('type', 'button')
button.onclick = () => action.result() && content.focus()
if (action.state) {
const handler = () => button.classList[action.state() ? 'add' : 'remove'](classes.selected)
ael(content, 'keyup', handler)
ael(content, 'mouseup', handler)
ael(button, 'click', handler)
}
ac(actionbar, button)
})
exec('defaultParagraphSeparator', 'p')
return settings.element
}
export default { exec, init }