mishig HF staff commited on
Commit
8f8f7dd
·
1 Parent(s): 8d4959a

add src files

Browse files
Files changed (2) hide show
  1. src/index.js +2 -0
  2. src/syncHFSpacesURLHash.js +90 -0
src/index.js CHANGED
@@ -1,9 +1,11 @@
1
  // import { plotClusters } from './clusters'
2
  import { init_memory_plot } from './memory'
3
  import { loadFragments } from './fragmentLoader'
 
4
 
5
  document.addEventListener("DOMContentLoaded", () => {
6
  console.log("DOMContentLoaded");
7
  loadFragments();
8
  init_memory_plot();
 
9
  }, { once: true });
 
1
  // import { plotClusters } from './clusters'
2
  import { init_memory_plot } from './memory'
3
  import { loadFragments } from './fragmentLoader'
4
+ import { syncHFSpacesURLHash } from './syncHFSpacesURLHash'
5
 
6
  document.addEventListener("DOMContentLoaded", () => {
7
  console.log("DOMContentLoaded");
8
  loadFragments();
9
  init_memory_plot();
10
+ syncHFSpacesURLHash();
11
  }, { once: true });
src/syncHFSpacesURLHash.js ADDED
@@ -0,0 +1,90 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ const queryArg = "section";
2
+
3
+ function syncHFSpacesURLHash(){
4
+ // Check for section parameter in URL
5
+ const urlParams = new URLSearchParams(window.location.search);
6
+ const sectionId = urlParams.get(queryArg);
7
+
8
+ if (sectionId) {
9
+ // Find the element with the specified ID
10
+ const targetElement = document.getElementById(sectionId);
11
+
12
+ // scroll if the element exists
13
+ if (targetElement) {
14
+ targetElement.scrollIntoView();
15
+ history.replaceState(null, null, `#${sectionId}`);
16
+ }
17
+ }
18
+
19
+ updateHashBasedOnHashChange();
20
+
21
+ // Variables to manage throttling
22
+ let isScrolling = false;
23
+ let lastKnownScrollPosition = 0;
24
+
25
+ // Add the scroll event listener here
26
+ window.addEventListener('scroll', function() {
27
+ lastKnownScrollPosition = window.scrollY;
28
+
29
+ if (!isScrolling) {
30
+ window.requestAnimationFrame(function() {
31
+ updateHashBasedOnScroll(lastKnownScrollPosition);
32
+ isScrolling = false;
33
+ });
34
+ }
35
+
36
+ isScrolling = true;
37
+ });
38
+
39
+ // Initial hash update on page load
40
+ updateHashBasedOnScroll(window.scrollY);
41
+ }
42
+
43
+ // Function to update the URL hash based on scroll position
44
+ function updateHashBasedOnScroll(scrollPosition) {
45
+ // Get only heading elements with IDs that we want to track
46
+ const elementsWithIds = Array.from(document.querySelectorAll('h1[id], h2[id], h3[id], h4[id], h5[id], h6[id]'));
47
+
48
+ // Skip updating if there are no elements with IDs
49
+ if (elementsWithIds.length === 0) return;
50
+
51
+ // Find the element closest to the top of the viewport
52
+ let closestElement = null;
53
+ let closestDistance = Infinity;
54
+ const viewportMiddle = scrollPosition + window.innerHeight / 2;
55
+
56
+ // Iterate through all elements with IDs to find the closest one
57
+ elementsWithIds.forEach(element => {
58
+ const elementTop = element.getBoundingClientRect().top + scrollPosition;
59
+ const distance = Math.abs(elementTop - viewportMiddle);
60
+
61
+ if (distance < closestDistance) {
62
+ closestDistance = distance;
63
+ closestElement = element;
64
+ }
65
+ });
66
+
67
+ // Update the URL hash if we found a closest element
68
+ if (closestElement && closestElement.id) {
69
+ // Only update if the hash is different to avoid unnecessary history entries
70
+ if (window.location.hash !== `#${closestElement.id}`) {
71
+ // Update the URL without adding a new history entry
72
+ history.replaceState(null, null, `#${closestElement.id}`);
73
+ postMessageToHFSpaces(closestElement.id);
74
+ }
75
+ }
76
+ }
77
+
78
+ function updateHashBasedOnHashChange() {
79
+ window.addEventListener('hashchange', () => {
80
+ const elementId = window.location.hash.slice(1);
81
+ postMessageToHFSpaces(elementId);
82
+ });
83
+ }
84
+
85
+ function postMessageToHFSpaces(elementId){
86
+ const parentOrigin = "https://huggingface.co";
87
+ window.parent.postMessage({ queryString: `${queryArg}=${elementId}` }, parentOrigin);
88
+ }
89
+
90
+ export { syncHFSpacesURLHash };