Skip to content

Commit bb9d90c

Browse files
committed
Add delayed anchor tracking; Include debug messages in COI build
1 parent 02e9295 commit bb9d90c

File tree

4 files changed

+31
-9
lines changed

4 files changed

+31
-9
lines changed

example.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@
5656
}
5757
})
5858

59+
import time;
60+
time.sleep(2)
61+
5962
# 5. Force anchor
6063
st.subheader("Example 5", help="Programatically select an anchor")
6164
force_settings = None

streamlit_scroll_navigation/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ def inject_crossorigin_interface():
3030
# Load the script from dev_url
3131
response = requests.get(f"{dev_url}/CrossOriginInterface.js")
3232
content = response.text
33-
pass
3433

3534
# Run COI content in parent
3635
# This works because streamlit.components.v1.html() creates an iframe from same domain as the parent scope

streamlit_scroll_navigation/frontend/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"streamlit-component-lib": "^2.0.0"
1111
},
1212
"scripts": {
13-
"terser-build": "terser ./public/CrossOriginInterface.js --compress 'pure_funcs=[\"console.debug\"]' --mangle -o ./build/CrossOriginInterface.min.js",
13+
"terser-build": "terser ./public/CrossOriginInterface.js --compress --mangle -o ./build/CrossOriginInterface.min.js",
1414
"start": "react-scripts start",
1515
"build": "react-scripts build",
1616
"test": "react-scripts test",

streamlit_scroll_navigation/frontend/public/CrossOriginInterface.js

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,13 @@ class CrossOriginInterface {
2020
this.disable_scroll = false;
2121
this.updateId = 0
2222
this.enroute = false;
23+
this.untrackedanchors = [];
2324
window.addEventListener("message", this.handleMessage.bind(this));
25+
26+
//Try to track untracked anchors every 200ms
27+
setInterval(() => {
28+
this.poll_untrackedanchors();
29+
}, 200);
2430
}
2531

2632
register(component, autoUpdateAnchor, emphasisStyle) {
@@ -187,17 +193,22 @@ class CrossOriginInterface {
187193
for (const anchorId of anchor_ids) {
188194
if (this.trackedAnchors.has(anchorId)) {
189195
console.debug('Anchor is already being tracked', anchorId);
190-
return;
196+
continue;
191197
}
192198

193199
const anchor = document.getElementById(anchorId);
194-
195200
if (!anchor) {
196-
console.error('Anchor does not exist', anchorId);
197-
return
201+
console.warn('Anchor does not exist in document: ', anchorId, ". Queueing for later.");
202+
this.untrackedanchors.push(anchorId);
203+
continue
198204
}
199205
this.trackedAnchors.add(anchorId);
200206

207+
//If no active anchor, set this anchor as active
208+
if (this.activeAnchorId === null) {
209+
this.activeAnchorId = anchorId;
210+
}
211+
201212
//Insert anchor into sortedAnchors based on its position in the document
202213
let inserted = false;
203214
for (let i = 0; i < this.sortedAnchors.length; i++) {
@@ -211,13 +222,21 @@ class CrossOriginInterface {
211222
if (!inserted) {
212223
this.sortedAnchors.push(anchorId);
213224
}
214-
this.sortedAnchors.push(anchorId);
215225

216226
this.observer.observe(anchor);
217227
console.debug('Started tracking anchor', anchorId);
218228
}
219229
}
220-
230+
poll_untrackedanchors() {
231+
//If there are untracked anchors, try to track them
232+
if (this.untrackedanchors.length > 0) {
233+
const untrackedanchors = this.untrackedanchors;
234+
this.untrackedanchors = [];
235+
236+
this.trackAnchors(untrackedanchors);
237+
console.log("ASDFASDF")
238+
}
239+
}
221240
//Handle messages from the component
222241
handleMessage(event) {
223242
const { COI_method, key} = event.data;
@@ -226,12 +245,13 @@ class CrossOriginInterface {
226245
if (!COI_method || !key) {
227246
return;
228247
}
248+
229249
//Check if message is intended for this COI instance
230250
if (key !== this.key) {
231251
return;
232252
}
233253
console.debug("COI with key", key, "received message", event.data);
234-
254+
235255
//If component is not registered, only allow registration method
236256
if (this.component === null) {
237257
if (COI_method === 'register') {

0 commit comments

Comments
 (0)