diff --git a/assets/js/theme.js b/assets/js/theme.js
index 6a388924e..ed328407e 100644
--- a/assets/js/theme.js
+++ b/assets/js/theme.js
@@ -663,18 +663,42 @@ class FixIt {
const cursorSpeed = typeitConfig.cursorSpeed || 1000;
const cursorChar = typeitConfig.cursorChar || '|';
const loop = typeitConfig.loop ?? false;
- Object.values(typeitConfig.data).forEach((group) => {
+ // divide them into different groups according to the data-group attribute value of the element
+ // results in an object like {group1: [ele1, ele2], group2: [ele3, ele4]}
+ const typeitElements = document.querySelectorAll('.typeit')
+ const groupMap = Array.from(typeitElements).reduce((acc, ele) => {
+ const group = ele.dataset.group || ele.id || Math.random().toString(36).substring(2);
+ acc[group] = acc[group] || [];
+ acc[group].push(ele);
+ return acc;
+ }, {});
+ const stagingElement = document.createElement('div')
+ stagingElement.style.display = 'none';
+ document.body.appendChild(stagingElement);
+
+ Object.values(groupMap).forEach((group) => {
const typeone = (i) => {
- const id = group[i];
- const shortcodeLoop = document.querySelector(`#${id}`).parentElement.dataset.loop;
- const instance = new TypeIt(`#${id}`, {
- strings: this.data[id],
+ const typeitElement = group[i];
+ const singleLoop = typeitElement.dataset.loop;
+ // get the content of the typed element
+ stagingElement.innerHTML = '';
+ stagingElement.appendChild(typeitElement.querySelector('template').content);
+ // for shortcodes usage
+ let targetEle = typeitElement.firstElementChild
+ // for system elements usage
+ if (typeitElement.firstElementChild.tagName === 'TEMPLATE') {
+ typeitElement.innerHTML = '';
+ targetEle = typeitElement
+ }
+ // create a new instance of TypeIt for each element
+ const instance = new TypeIt(targetEle, {
+ strings: stagingElement.innerHTML,
speed: speed,
lifeLike: true,
cursorSpeed: cursorSpeed,
cursorChar: cursorChar,
waitUntilVisible: true,
- loop: shortcodeLoop ? JSON.parse(shortcodeLoop) : loop,
+ loop: singleLoop ? JSON.parse(singleLoop) : loop,
afterComplete: () => {
if (i === group.length - 1) {
if (typeitConfig.duration >= 0) {
@@ -691,6 +715,7 @@ class FixIt {
};
typeone(0);
});
+ document.body.removeChild(stagingElement);
}
}
diff --git a/layouts/_default/baseof.html b/layouts/_default/baseof.html
index a7dd0415e..9ca820146 100644
--- a/layouts/_default/baseof.html
+++ b/layouts/_default/baseof.html
@@ -28,7 +28,7 @@
{{- if $profile.typeit -}} - {{- $id := dict "Content" . "Scratch" $.Scratch "Id" "typeit-profile-subtitle" | partial "function/id.html" -}} {{ . }} - - {{- dict $id (slice $id) | dict "typeitMap" | merge ($.Scratch.Get "this") | $.Scratch.Set "this" -}} + {{- $.Store.Set "hasTyped" true -}} + {{ . }} {{- else -}} {{- . -}} {{- end -}} diff --git a/layouts/partials/init/index.html b/layouts/partials/init/index.html index 1d6498db3..e8c3b3da7 100644 --- a/layouts/partials/init/index.html +++ b/layouts/partials/init/index.html @@ -1,5 +1,5 @@ {{- .Scratch.Set "version" "v0.3.3-RC" -}} -{{- .Scratch.Set "this" (dict "version" (.Scratch.Get "version") | dict "config") -}} +{{- .Scratch.Set "this" dict -}} {{- partial "init/detection-env.html" . -}} {{- partial "init/detection-version.html" . -}} diff --git a/layouts/shortcodes/typeit.html b/layouts/shortcodes/typeit.html index 685a34d35..6f1d9416a 100644 --- a/layouts/shortcodes/typeit.html +++ b/layouts/shortcodes/typeit.html @@ -1,3 +1,4 @@ +{{- /* trim the newline */ -}} {{- $content := trim (partial "function/dos2unix.html" .Inner) "\n" -}} {{- $classList := slice -}} {{- with .Get "class" -}} @@ -15,25 +16,26 @@ {{- /* parsing code links */ -}} {{- $content = replaceRE `(]*>)([^<>]*)\[([^<>]+)\]\(([^<>]+)\)([^<>]*)()` "$1$2$6$3$1$5$6" $content -}} {{- end -}} - {{- /* split multiline string */ -}} - {{- $content = split $content "\n" -}} {{- $classList = $classList | append "highlight" -}} {{- else -}} {{- $content = $content | .Page.RenderString -}} {{- end -}} -{{- /* trim the newline */ -}} -{{- $id := dict "Content" $content "Scratch" .Page.Scratch | partial "function/id.html" -}} -{{- $key := .Get "group" | string | default $id -}} -{{- $typeitMap := (.Page.Scratch.Get "this").typeitMap | default dict -}} -{{- $group := index $typeitMap $key -}} -{{- $group = $group | default slice | append $id -}} -{{- dict $key $group | merge $typeitMap | .Page.Scratch.SetInMap "this" "typeitMap" -}} +{{- $wrapperAttrs := `class="typeit"` -}} +{{- with (.Get "group" | string) -}} + {{- $wrapperAttrs = printf `%v data-group="%v"` $wrapperAttrs . -}} +{{- end -}} +{{- with $loop -}} + {{- $wrapperAttrs = printf `%v data-loop="true"` $wrapperAttrs -}} +{{- end -}} -{{- $attrs := printf `id="%v"` $id -}} +{{- $innerAttrs := "" -}} {{- with $classList -}} - {{- $attrs = delimit $classList " " | printf `%v class="%v"` $attrs -}} + {{- $innerAttrs = printf `class="%v"` (delimit $classList " ") -}} {{- end -}} -