Skip to content

Commit 855d311

Browse files
committed
refactor: Updated read time to a class based script
1 parent 003f35c commit 855d311

3 files changed

Lines changed: 38 additions & 40 deletions

File tree

Dist/Functional/CopyToClipboard.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ const initializeCopyToClipboard = () => {
3838
const triggers = document.querySelectorAll('[wt-copycb-element="container"]');
3939
triggers.forEach(trigger => {
4040
let instance = new CopyToClipboard(trigger);
41-
window.trickeries.push({'copyToClipboard': instance});
41+
window.trickeries.push({'CopyToClipboard': instance});
4242
});
4343
}
4444

Dist/Functional/FormCheck.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ const initializeFormCheck = () => {
117117
const forms = document.querySelectorAll('[wt-formcheck-element="form"]');
118118
forms.forEach(form => {
119119
let instance = new FormCheck(form);
120-
window.trickeries.push({'formCheck': instance});
120+
window.trickeries.push({'FormCheck': instance});
121121
});
122122
}
123123

Dist/Functional/ReadTime.js

Lines changed: 36 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,46 @@
11
'use strict'
22

3-
const SetReadTime = () => {
4-
const timeDisplays = document.querySelectorAll('[wt-readtime-element="display"]');
5-
const container = document.querySelector('[wt-readtime-element="article"]');
6-
7-
let valid = true;
8-
let err = []
9-
10-
if(!timeDisplays || timeDisplays.length === 0) {
11-
err.push(`%c display element is missing`);
12-
valid = false;
13-
}
14-
if(!container){
15-
err.push(`%c article element is missing`);
16-
valid = false;
3+
class ReadTime {
4+
constructor(_container) {
5+
this.articleContainer = _container;
6+
this.timeDisplays = document.querySelectorAll('[wt-readtime-element="display"]');
7+
this.text = this.articleContainer ? this.articleContainer.innerText : "";
8+
this.words = this.text.trim().split(/\s+/).length;
9+
this.wpm = this.articleContainer.getAttribute("wt-readtime-words") || 225;
10+
this.suffix = this.articleContainer.getAttribute('wt-readtime-suffix') || "";
11+
this.smallsuffix = this.articleContainer.getAttribute('wt-readtime-smallsuffix') || "";
12+
this.rawTime = this.words / this.wpm;
13+
this.init();
1714
}
1815

19-
if (!valid) {
20-
console.log("%cThanks for using Webflow Trickery","color: blue; font-size: 18px; padding: 8px; font-weight:500");
21-
console.log("%cIt seems like you're missing an element that is required for the integration.","color: red; font-size: 12px; padding: 8px; font-weight:500");
22-
for(let e of err){
23-
console.log(e,"color: red; font-size: 12px; padding: 8px; font-weight:500")
24-
}
25-
return;
16+
init = () => {
17+
if(this.timeDisplays && this.timeDisplays.length > 0) this.SetReadTime();
2618
}
2719

28-
const text = container.innerText;
29-
const words = text.trim().split(/\s+/).length;
30-
const wpm = container.getAttribute("wt-readtime-words") || 225;
31-
const suffix = container.getAttribute('wt-readtime-suffix');
32-
const smallsuffix = container.getAttribute('wt-readtime-smallsuffix');
33-
const rawTime = words / wpm;
34-
35-
for(let display of timeDisplays) {
36-
if(rawTime < 1 ) {
37-
display.innerText = smallsuffix ? smallsuffix : "less than a minute.";
38-
}
39-
else if(rawTime == 1 ) {
40-
display.innerText = "a minute.";
41-
}
42-
else {
43-
display.innerText = suffix ? `${Math.ceil(rawTime)} ${suffix}` : `${Math.ceil(rawTime)} minutes.`;
20+
SetReadTime = () => {
21+
for(let _display of this.timeDisplays) {
22+
if(this.rawTime < 1 )
23+
_display.innerText = this.smallsuffix ? this.smallsuffix : "less than a minute.";
24+
else if(this.rawTime == 1 )
25+
_display.innerText = "a minute.";
26+
else
27+
_display.innerText = this.suffix ? `${Math.ceil(this.rawTime)} ${this.suffix}` : `${Math.ceil(this.rawTime)} minutes.`;
4428
}
4529
}
4630
}
4731

48-
window.addEventListener('DOMContentLoaded', SetReadTime());
32+
const InitializeReadTime = () => {
33+
window.trickeries = window.trickeries || [];
34+
let articleContainers = document.querySelectorAll('[wt-readtime-element="article"]');
35+
if(!articleContainers || articleContainers.length === 0) return;
36+
articleContainers.forEach(article => {
37+
let instance = new ReadTime(article);
38+
window.trickeries.push({'ReadTime': instance});
39+
});
40+
}
41+
42+
if (/complete|interactive|loaded/.test(document.readyState)) {
43+
InitializeReadTime();
44+
} else {
45+
window.addEventListener('DOMContentLoaded',InitializeReadTime )
46+
}

0 commit comments

Comments
 (0)