Skip to content

Commit

Permalink
refactor: split updateViewData to allow updating per view
Browse files Browse the repository at this point in the history
  • Loading branch information
iisakkirotko authored and maartenbreddels committed Dec 23, 2024
1 parent d724298 commit cf2180a
Showing 1 changed file with 19 additions and 16 deletions.
35 changes: 19 additions & 16 deletions js/lib/ViewListener.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ class ViewListenerModel extends base.DOMWidgetModel {
this.set('view_data', {}) // clear data
const selector = this.get('css_selector');
// initial fill
this._updateViewData();
this._updateAllViewData();

// listen to element for resize events
views.forEach((view) => {
const resizeObserver = new ResizeObserver(entries => {
this._updateViewData();
this._updateViewData(view);
});
let el = view.el;
el = selector ? el.querySelector(selector) : el;
Expand All @@ -70,25 +70,28 @@ class ViewListenerModel extends base.DOMWidgetModel {
}
})
}
async _updateViewData() {
const views = await this._getViews();
async _updateViewData(view) {
const selector = this.get('css_selector');
let el = view.el;
el = selector ? el.querySelector(selector) : el;
if(el) {
const {x, y, width, height} = el.getBoundingClientRect();
this.send({
event: 'set_view_data',
id: view.cid,
data: { x, y, width, height, resized_at, focused_at },
})
} else {
console.error('could not find element with css selector', selector);
}
}
async _updateAllViewData() {
const views = await this._getViews();
const currentViews = new Set();
views.forEach((view) => {
currentViews.add(view.cid);
this.knownViews.add(view.cid);
let el = view.el;
el = selector ? el.querySelector(selector) : el;
if(el) {
const {x, y, width, height} = el.getBoundingClientRect();
this.send({
event: 'set_view_data',
id: view.cid,
data: { x, y, width, height},
})
} else {
console.error('could not find element with css selector', selector);
}
this._updateViewData(view, true, true);
});
const removeViews = [...this.knownViews].filter((cid) => !currentViews.has(cid));
removeViews.forEach((cid) => {
Expand Down

0 comments on commit cf2180a

Please sign in to comment.