@internetstiftelsen/styleguide 4.0.10-beta.0.1 → 4.0.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import className from './className';
|
|
2
|
+
import htmlTextLength from './htmlTextLength';
|
|
3
|
+
|
|
4
|
+
class CharCounter {
|
|
5
|
+
constructor(el) {
|
|
6
|
+
this.el = el;
|
|
7
|
+
this.counterEl = null;
|
|
8
|
+
this.isRichText = this.el.dataset.richText !== undefined;
|
|
9
|
+
this.min = parseInt(this.el.getAttribute('data-min') || 0, 10);
|
|
10
|
+
this.max = parseInt(this.el.getAttribute('data-max') || 0, 10);
|
|
11
|
+
|
|
12
|
+
if (!this.min && !this.max) {
|
|
13
|
+
console.warn('Either min or max must be set and greater than 0.');
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
if (this.isRichText) {
|
|
18
|
+
this.waitForEditor();
|
|
19
|
+
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
this.build();
|
|
24
|
+
this.attach();
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
waitForEditor() {
|
|
28
|
+
if (this.el.editor) {
|
|
29
|
+
this.build();
|
|
30
|
+
this.attach();
|
|
31
|
+
} else {
|
|
32
|
+
this.el.addEventListener('editor-ready', () => {
|
|
33
|
+
this.build();
|
|
34
|
+
this.attach();
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
count() {
|
|
40
|
+
if (this.isRichText) {
|
|
41
|
+
return htmlTextLength(this.el.editor.getHTML());
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return this.el.value.length;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
setCountText() {
|
|
48
|
+
const count = this.count();
|
|
49
|
+
|
|
50
|
+
if (this.min && count < this.min) {
|
|
51
|
+
this.counterEl.textContent = `${count}/${this.min}`;
|
|
52
|
+
this.counterEl.className = `color-ruby ${className('a-meta')}`;
|
|
53
|
+
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if (this.max && count > this.max) {
|
|
58
|
+
this.counterEl.textContent = `${count}/${this.max}`;
|
|
59
|
+
this.counterEl.classList.remove('color-granit');
|
|
60
|
+
this.counterEl.className = `color-ruby ${className('a-meta')}`;
|
|
61
|
+
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
this.counterEl.textContent = `${count}/${this.max || this.min}`;
|
|
66
|
+
this.counterEl.className = `color-jade ${className('a-meta')}`;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
build() {
|
|
70
|
+
const counter = document.createElement('small');
|
|
71
|
+
let wrapper;
|
|
72
|
+
|
|
73
|
+
if (this.isRichText) {
|
|
74
|
+
wrapper = this.el.editor.options.element;
|
|
75
|
+
wrapper.style.paddingRight = '3.8333333333rem';
|
|
76
|
+
} else {
|
|
77
|
+
wrapper = document.createElement('div');
|
|
78
|
+
|
|
79
|
+
wrapper.className = 'u-position-relative';
|
|
80
|
+
this.el.parentNode.insertBefore(wrapper, this.el);
|
|
81
|
+
wrapper.appendChild(this.el);
|
|
82
|
+
|
|
83
|
+
this.el.style.paddingRight = '3.8333333333rem';
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
counter.className = `color-granit ${className('a-meta')}`;
|
|
87
|
+
counter.style.cssText = 'position: absolute; top: 5px; right: 10px; z-index: 501;';
|
|
88
|
+
|
|
89
|
+
wrapper.appendChild(counter);
|
|
90
|
+
|
|
91
|
+
this.counterEl = counter;
|
|
92
|
+
this.setCountText();
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
attach() {
|
|
96
|
+
if (this.isRichText) {
|
|
97
|
+
this.el.editor.on('update', () => {
|
|
98
|
+
this.setCountText();
|
|
99
|
+
});
|
|
100
|
+
} else {
|
|
101
|
+
this.el.addEventListener('input', () => {
|
|
102
|
+
this.setCountText();
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const elements = document.querySelectorAll('[data-min], [data-max]');
|
|
109
|
+
elements.forEach((el) => {
|
|
110
|
+
el.charCounter = new CharCounter(el);
|
|
111
|
+
});
|
|
@@ -186,6 +186,14 @@ export function setupTextArea(el, onChange = () => {}) {
|
|
|
186
186
|
el.parentNode.insertBefore(editorEl, el);
|
|
187
187
|
|
|
188
188
|
createToolbar(editorEl, editor);
|
|
189
|
+
|
|
190
|
+
const event = new CustomEvent('editor-ready', {
|
|
191
|
+
detail: {
|
|
192
|
+
editor,
|
|
193
|
+
},
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
el.dispatchEvent(event);
|
|
189
197
|
}
|
|
190
198
|
|
|
191
199
|
export function init() {
|
|
@@ -51,6 +51,44 @@ module.exports = {
|
|
|
51
51
|
is_richtext: true,
|
|
52
52
|
has_help: false
|
|
53
53
|
}
|
|
54
|
+
},
|
|
55
|
+
{
|
|
56
|
+
name: 'Counter (max)',
|
|
57
|
+
status: 'wip',
|
|
58
|
+
context: {
|
|
59
|
+
is_richtext: false,
|
|
60
|
+
has_help: false,
|
|
61
|
+
max: 300,
|
|
62
|
+
},
|
|
63
|
+
},
|
|
64
|
+
{
|
|
65
|
+
name: 'Counter (min)',
|
|
66
|
+
status: 'wip',
|
|
67
|
+
context: {
|
|
68
|
+
is_richtext: false,
|
|
69
|
+
has_help: false,
|
|
70
|
+
min: 30,
|
|
71
|
+
},
|
|
72
|
+
},
|
|
73
|
+
{
|
|
74
|
+
name: 'Counter (min and max)',
|
|
75
|
+
status: 'wip',
|
|
76
|
+
context: {
|
|
77
|
+
is_richtext: false,
|
|
78
|
+
has_help: false,
|
|
79
|
+
min: 30,
|
|
80
|
+
max: 300,
|
|
81
|
+
},
|
|
82
|
+
},
|
|
83
|
+
{
|
|
84
|
+
name: 'Rich text with counter',
|
|
85
|
+
status: 'wip',
|
|
86
|
+
context: {
|
|
87
|
+
is_richtext: true,
|
|
88
|
+
has_help: true,
|
|
89
|
+
min: 30,
|
|
90
|
+
max: 300,
|
|
91
|
+
},
|
|
54
92
|
}
|
|
55
93
|
]
|
|
56
94
|
}
|
package/src/components.js
CHANGED