-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.js
72 lines (58 loc) · 1.55 KB
/
plugin.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
// Revealing module pattern
var Butcher = (function () {
/*
* Private method
*/
const wrapContext = function (contexts, parent, className) {
contexts.forEach((ctx, index) => {
if (ctx !== ' ') {
const el = document.createElement('span')
el.classList.add(className)
el.style.display = 'inline-block'
el.style.position = 'relative'
el.textContent = ctx
parent.appendChild(el)
}
// it would be better if classname is part of public api
// Looking for a way to refactor this
if ((className === 'char' && ctx === ' ') || (className === 'word')) {
parent.appendChild(document.createTextNode(' '))
}
})
}
/**
* Public method
*/
const words = function (param) {
let elements
if (typeof param === 'string') {
elements = document.querySelectorAll(param)
} else if (param instanceof Element) {
elements = param
}
elements.forEach(el => {
let words = el.textContent.split(' ')
el.classList.add('words')
el.textContent = ''
wrapContext(words, el, 'word')
})
}
const chars = function (param) {
let elements
if (typeof param === 'string') {
elements = document.querySelectorAll(param)
} else if (param instanceof Element) {
elements = param
}
elements.forEach(el => {
let words = el.textContent.split('')
el.classList.add('chars')
el.textContent = ''
wrapContext(words, el, 'char')
})
}
return {
words: words,
chars: chars
}
})()