Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
drupal
Manage
Activity
Members
Labels
Plan
Wiki
Custom issue tracker
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
project
drupal
Merge requests
!4596
Resolve
#3209192
"Starterkit theme add"
Code
Review changes
Check out branch
Download
Patches
Plain diff
Open
Resolve
#3209192
"Starterkit theme add"
issue/drupal-3209192:3209192-starterkit-theme-add
into
11.x
Overview
19
Commits
13
Pipelines
6
Changes
3
Open
Matthieu Scarset
requested to merge
issue/drupal-3209192:3209192-starterkit-theme-add
into
11.x
1 year ago
Overview
13
Commits
13
Pipelines
6
Changes
3
Expand
Closes
#3209192
0
0
Merge request reports
Compare
11.x
version 16
aa5a3a5a
1 year ago
version 15
29bbbcdc
1 year ago
version 14
998f86d0
1 year ago
version 13
dae48d4d
1 year ago
version 12
be14941c
1 year ago
version 11
e882a6c9
1 year ago
version 10
edf80c30
1 year ago
version 9
2c6293c1
1 year ago
version 8
8b8255e4
1 year ago
version 7
f390b912
1 year ago
version 6
538bad2e
1 year ago
version 5
06f143dd
1 year ago
version 4
eb14c587
1 year ago
version 3
f1f8e3a3
1 year ago
version 2
c132a122
1 year ago
version 1
250bc06a
1 year ago
11.x (HEAD)
and
latest version
latest version
19a66eea
13 commits,
1 year ago
version 16
aa5a3a5a
12 commits,
1 year ago
version 15
29bbbcdc
11 commits,
1 year ago
version 14
998f86d0
25 commits,
1 year ago
version 13
dae48d4d
11 commits,
1 year ago
version 12
be14941c
10 commits,
1 year ago
version 11
e882a6c9
10 commits,
1 year ago
version 10
edf80c30
9 commits,
1 year ago
version 9
2c6293c1
9 commits,
1 year ago
version 8
8b8255e4
8 commits,
1 year ago
version 7
f390b912
7 commits,
1 year ago
version 6
538bad2e
6 commits,
1 year ago
version 5
06f143dd
5 commits,
1 year ago
version 4
eb14c587
4 commits,
1 year ago
version 3
f1f8e3a3
3 commits,
1 year ago
version 2
c132a122
2 commits,
1 year ago
version 1
250bc06a
1 commit,
1 year ago
3 files
+
107
−
0
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
3
Search (e.g. *.vue) (Ctrl+P)
core/themes/starterkit_theme/js/reading-time.js
0 → 100644
+
87
−
0
Options
/**
* @file
* Defines a custom behavior.
*/
((
Drupal
,
drupalSettings
,
once
)
=>
{
// Constant variables for consistency.
const
onceName
=
'
readingTime
'
;
// Use any valid selector to target DOM elements.
// Example: '.my-class' or 'article > h1'.
// @see https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector#parameters
const
elementSelector
=
'
main[role="main"]
'
;
// Custom value coming from the backend.
// @see starterkit_theme_attachments() in starterkit_theme.theme.
const
wordsPerMinute
=
drupalSettings
.
starterkit_theme
.
wordsPerMinute
||
300
;
// Instantiate Drupal message command.
// Note that it is possible because we required the `core/drupal.message` library.
// @see core/themes/starterkit_theme/starterkit_theme.libraries.yml
// @see https://www.drupal.org/docs/drupal-apis/javascript-api/messages-api
const
messages
=
new
Drupal
.
Message
();
/**
* Calculate how many minutes it will take to read the piece of content.
*
* This helper method is encapsulated in script.
*
* @param {HTMLElement} element
* A given DOM element.
*
* @param {number} wpm
* A given "words per minute" number.
*
* @return {number}
* The reading time, in minutes.
*/
function
readingTime
(
element
,
wpm
)
{
const
text
=
element
.
innerText
;
const
words
=
text
.
trim
().
split
(
/
\s
+/
).
length
;
return
Math
.
ceil
(
words
/
wpm
);
}
/**
* Log approximate reading time of main content in console.
*
* This registers the Drupal behaviors which is triggered on every page load and
* when data is loaded by AJAX.
*
* @type {Drupal~behavior}
*
* @prop {Drupal~behaviorAttach} attach
* Attaches the behavior to the rendering context, if possible.
*
* @see starterkit_theme.libraries.yml
* Where dependencies to core/drupalSettings and core/once are defined.
* @see https://www.drupal.org/docs/drupal-apis/javascript-api/javascript-api-overview
* An introduction to the art of using JavaScript in Drupal.
* @see https://www.drupal.org/node/3158256
* The change notice with examples of how to use once().
* @see https://www.npmjs.com/package/@drupal/once
* The NPM package with the full documentation about once().
*/
Drupal
.
behaviors
.
readingTime
=
{
attach
(
context
)
{
// Process the current content to calculate the reading time.
// We use `once()` from core to avoid processing the content multiple time.
once
(
onceName
,
elementSelector
,
context
).
forEach
((
element
)
=>
{
const
time
=
readingTime
(
element
,
wordsPerMinute
);
messages
.
add
(
Drupal
.
t
(
'
This page will take you @minutes to read
'
,
{
'
@minutes
'
:
Drupal
.
formatPlural
(
time
,
'
1·minute
'
,
'
@count·minutes
'
),
}),
);
});
},
detach
(
context
,
trigger
)
{
if
(
trigger
===
'
unload
'
)
{
// Remove processing mark so that content can be processed again.
// The text might have change after an Ajax call so reading time
// will need to be recalculated.
once
.
remove
(
onceName
,
elementSelector
,
context
);
}
},
};
})(
Drupal
,
drupalSettings
,
once
);
Loading