-
Notifications
You must be signed in to change notification settings - Fork 547
Expand file tree
/
Copy pathpell.js
More file actions
219 lines (201 loc) · 6.08 KB
/
pell.js
File metadata and controls
219 lines (201 loc) · 6.08 KB
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
const defaultParagraphSeparatorString = 'defaultParagraphSeparator'
const formatBlock = 'formatBlock'
const addEventListener = (parent, type, listener) => parent.addEventListener(type, listener)
const appendChild = (parent, child) => parent.appendChild(child)
const createElement = tag => document.createElement(tag)
const queryCommandState = command => document.queryCommandState(command)
const queryCommandValue = command => document.queryCommandValue(command)
export const exec = (command, value = null) => document.execCommand(command, false, value)
const defaultActions = {
bold: {
icon: '<b>B</b>',
title: 'Bold',
state: () => queryCommandState('bold'),
result: () => exec('bold')
},
italic: {
icon: '<i>I</i>',
title: 'Italic',
state: () => queryCommandState('italic'),
result: () => exec('italic')
},
underline: {
icon: '<u>U</u>',
title: 'Underline',
state: () => queryCommandState('underline'),
result: () => exec('underline')
},
strikethrough: {
icon: '<strike>S</strike>',
title: 'Strike-through',
state: () => queryCommandState('strikeThrough'),
result: () => exec('strikeThrough')
},
heading1: {
icon: '<b>H<sub>1</sub></b>',
title: 'Heading 1',
result: () => exec(formatBlock, '<h1>')
},
heading2: {
icon: '<b>H<sub>2</sub></b>',
title: 'Heading 2',
result: () => exec(formatBlock, '<h2>')
},
paragraph: {
icon: '¶',
title: 'Paragraph',
result: () => exec(formatBlock, '<p>')
},
quote: {
icon: '“ ”',
title: 'Quote',
result: () => exec(formatBlock, '<blockquote>')
},
olist: {
icon: '#',
title: 'Ordered List',
result: () => exec('insertOrderedList')
},
ulist: {
icon: '•',
title: 'Unordered List',
result: () => exec('insertUnorderedList')
},
code: {
icon: '</>',
title: 'Code',
result: () => exec(formatBlock, '<pre>')
},
line: {
icon: '―',
title: 'Horizontal Line',
result: () => exec('insertHorizontalRule')
},
link: {
icon: '🔗',
title: 'Link',
result: () => {
const url = window.prompt('Enter the link URL')
if (url) exec('createLink', url)
}
},
image: {
icon: '📷',
title: 'Image',
result: () => {
// const url = window.prompt('Enter the image URL')
// if (url) exec('insertImage', url)
execInsertImageAction()
}
}
}
// default for not set `upload` config
const execInsertImageAction = function () {
const uploadImageInput = document.querySelector('.pell input[type="file"]')
if (!uploadImageInput) {
const url = window.prompt('Enter the image URL')
if (url) exec('insertImage', url)
} else {
uploadImageInput.click()
}
}
// just set `url`, `method` and `body` for fetch api
const uploadImage = function ({ api, data }, success, error) {
window.fetch && window.fetch(
api,
{
method: 'POST',
body: data
}
)
.then(res => res.json())
.then(
data => {
// responsive data format:
// { success: true, url: 'xxx' }
if (data.success) success(data.url)
},
err => error(err)
)
}
const initUploadImageInput = function (settings) {
const uploadAPI = settings.upload && settings.upload.api
if (uploadAPI) {
const input = createElement('input')
input.type = 'file'
input.hidden = true
addEventListener(input, 'change', e => {
const image = e.target.files[0]
const fd = new window.FormData()
fd.append('pell-upload-image', image)
uploadImage(
{
api: uploadAPI,
data: fd
},
url => exec('insertImage', url),
err => window.alert(err)
)
})
appendChild(settings.element, input)
}
}
const defaultClasses = {
actionbar: 'pell-actionbar',
button: 'pell-button',
content: 'pell-content',
selected: 'pell-button-selected'
}
export const init = settings => {
const actions = settings.actions
? (
settings.actions.map(action => {
if (typeof action === 'string') return defaultActions[action]
else if (defaultActions[action.name]) return { ...defaultActions[action.name], ...action }
return action
})
)
: Object.keys(defaultActions).map(action => defaultActions[action])
const classes = { ...defaultClasses, ...settings.classes }
const defaultParagraphSeparator = settings[defaultParagraphSeparatorString] || 'div'
const actionbar = createElement('div')
actionbar.className = classes.actionbar
appendChild(settings.element, actionbar)
const content = settings.element.content = createElement('div')
content.contentEditable = true
content.className = classes.content
content.oninput = ({ target: { firstChild } }) => {
if (firstChild && firstChild.nodeType === 3) exec(formatBlock, `<${defaultParagraphSeparator}>`)
else if (content.innerHTML === '<br>') content.innerHTML = ''
settings.onChange(content.innerHTML)
}
content.onkeydown = event => {
if (event.key === 'Tab') {
event.preventDefault()
} else if (event.key === 'Enter' && queryCommandValue(formatBlock) === 'blockquote') {
setTimeout(() => exec(formatBlock, `<${defaultParagraphSeparator}>`), 0)
}
}
appendChild(settings.element, content)
actions.forEach(action => {
const button = createElement('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)
addEventListener(content, 'keyup', handler)
addEventListener(content, 'mouseup', handler)
addEventListener(button, 'click', handler)
}
appendChild(actionbar, button)
})
if (settings.styleWithCSS) exec('styleWithCSS')
exec(defaultParagraphSeparatorString, defaultParagraphSeparator)
// init a upload image input or not
initUploadImageInput(settings)
return settings.element
}
export default { exec, init }