@hortonstudio/main 1.9.1 → 1.9.3

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.
@@ -0,0 +1,134 @@
1
+ export function init() {
2
+ function setupSiteSettings() {
3
+ const wrapper = document.querySelector('[data-site-settings-element="wrapper"]');
4
+
5
+ if (!wrapper) {
6
+ return;
7
+ }
8
+
9
+ // Find all lists inside the wrapper
10
+ const lists = wrapper.querySelectorAll('[data-site-settings-element="list"]');
11
+
12
+ if (lists.length === 0) {
13
+ return;
14
+ }
15
+
16
+ // Collect all site settings data
17
+ const siteSettings = {};
18
+
19
+ lists.forEach(list => {
20
+ // Find all descendants with data-site-settings attribute
21
+ const settingElements = list.querySelectorAll('[data-site-settings]');
22
+
23
+ settingElements.forEach(element => {
24
+ // Clone the element to avoid modifying the original DOM
25
+ const clonedElement = element.cloneNode(true);
26
+
27
+ // Find and remove elements with data-site-settings-hide that match their classes
28
+ const hideElements = clonedElement.querySelectorAll('[data-site-settings-hide]');
29
+
30
+ hideElements.forEach(hideElement => {
31
+ const hideValue = hideElement.getAttribute('data-site-settings-hide');
32
+
33
+ if (hideValue) {
34
+ // Split by spaces to get all class names to check
35
+ const classesToHide = hideValue.split(' ').filter(c => c.trim());
36
+
37
+ // Check if element has any of the classes
38
+ const hasMatchingClass = classesToHide.some(className =>
39
+ hideElement.classList.contains(className)
40
+ );
41
+
42
+ // Remove element if it has a matching class
43
+ if (hasMatchingClass) {
44
+ hideElement.remove();
45
+ }
46
+ }
47
+ });
48
+
49
+ const settingName = element.getAttribute('data-site-settings');
50
+ const settingValue = clonedElement.textContent.trim();
51
+
52
+ // Only store if both name and value exist (ignore empty elements like images)
53
+ if (settingName && settingValue) {
54
+ siteSettings[settingName] = settingValue;
55
+ }
56
+ });
57
+ });
58
+
59
+ // If no site settings found, exit early
60
+ if (Object.keys(siteSettings).length === 0) {
61
+ return;
62
+ }
63
+
64
+ // Replace text content efficiently using TreeWalker
65
+ const walker = document.createTreeWalker(
66
+ document.body,
67
+ NodeFilter.SHOW_TEXT,
68
+ {
69
+ acceptNode: (node) => {
70
+ // Check if any site setting names exist in the text content
71
+ const text = node.textContent;
72
+ for (const name in siteSettings) {
73
+ if (text.includes(`{{${name}}}`)) {
74
+ return NodeFilter.FILTER_ACCEPT;
75
+ }
76
+ }
77
+ return NodeFilter.FILTER_SKIP;
78
+ }
79
+ }
80
+ );
81
+
82
+ const textNodes = [];
83
+ let node;
84
+ while (node = walker.nextNode()) {
85
+ textNodes.push(node);
86
+ }
87
+
88
+ // Replace text in collected nodes
89
+ textNodes.forEach(textNode => {
90
+ let newText = textNode.textContent;
91
+ let hasChanges = false;
92
+
93
+ for (const name in siteSettings) {
94
+ const placeholder = `{{${name}}}`;
95
+ if (newText.includes(placeholder)) {
96
+ newText = newText.replace(new RegExp(placeholder, 'g'), siteSettings[name]);
97
+ hasChanges = true;
98
+ }
99
+ }
100
+
101
+ if (hasChanges) {
102
+ textNode.textContent = newText;
103
+ }
104
+ });
105
+
106
+ // Replace link hrefs
107
+ const links = document.querySelectorAll('a[href]');
108
+ links.forEach(link => {
109
+ let href = link.getAttribute('href');
110
+ let hasChanges = false;
111
+
112
+ for (const name in siteSettings) {
113
+ const placeholder = `{{${name}}}`;
114
+ if (href.includes(placeholder)) {
115
+ href = href.replace(new RegExp(placeholder, 'g'), siteSettings[name]);
116
+ hasChanges = true;
117
+ }
118
+ }
119
+
120
+ if (hasChanges) {
121
+ link.setAttribute('href', href);
122
+ }
123
+ });
124
+ }
125
+
126
+ setupSiteSettings();
127
+
128
+ return {
129
+ result: "site-settings initialized",
130
+ destroy: () => {
131
+ // No cleanup needed - this module only modifies DOM during init
132
+ }
133
+ };
134
+ }
package/index.js CHANGED
@@ -32,6 +32,7 @@ const initializeHsMain = async () => {
32
32
  accessibility: () => import("./autoInit/accessibility.js"),
33
33
  counter: () => import("./autoInit/counter.js"),
34
34
  form: () => import("./autoInit/form.js"),
35
+ "site-settings": () => import("./autoInit/site-settings.js"),
35
36
  };
36
37
 
37
38
  let scripts = [
@@ -71,6 +72,7 @@ const initializeHsMain = async () => {
71
72
  accessibility: true,
72
73
  counter: true,
73
74
  form: true,
75
+ "site-settings": true,
74
76
  // Only include transition if no-transition attribute is NOT present
75
77
  ...(hasNoTransition ? {} : { transition: true }),
76
78
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hortonstudio/main",
3
- "version": "1.9.1",
3
+ "version": "1.9.3",
4
4
  "description": "Animation and utility library for client websites",
5
5
  "main": "index.js",
6
6
  "type": "module",