Skip to content

Commit

Permalink
Merge pull request #501 from anyproto/bugfix/JS-1305-selection-on-scroll
Browse files Browse the repository at this point in the history
Bugfix/JS-1305: Selection on scroll
  • Loading branch information
ra3orblade authored Jan 19, 2024
2 parents e7a7bc9 + 5ea1b04 commit dc9f3e0
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 24 deletions.
46 changes: 29 additions & 17 deletions src/ts/component/selection/provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const SelectionProvider = observer(class SelectionProvider extends React.Compone
range: any = null;
nodes: any[] = [];
top = 0;
startTop = 0;
containerOffset = null;
frame = 0;

Expand Down Expand Up @@ -59,7 +60,7 @@ const SelectionProvider = observer(class SelectionProvider extends React.Compone
this._isMounted = true;
this.unbind();

UtilCommon.getScrollContainer(isPopup).on('scroll.selection', (e: any) => { this.onScroll(e); });
UtilCommon.getScrollContainer(isPopup).on('scroll.selection', e => this.onScroll(e));
};

componentWillUnmount () {
Expand Down Expand Up @@ -121,7 +122,6 @@ const SelectionProvider = observer(class SelectionProvider extends React.Compone

const { focused } = focus.state;
const win = $(window);
const nodes = this.getPageContainer().find('.selectable');
const container = UtilCommon.getScrollContainer(isPopup);
const selectionRect = $('#selection-rect');

Expand All @@ -131,7 +131,7 @@ const SelectionProvider = observer(class SelectionProvider extends React.Compone
this.y = e.pageY;
this.moved = false;
this.focused = focused;
this.top = container.scrollTop();
this.top = this.startTop = container.scrollTop();
this.cache.clear();
this.idsOnStart = new Map(this.ids);
this.setIsSelecting(true);
Expand All @@ -144,18 +144,7 @@ const SelectionProvider = observer(class SelectionProvider extends React.Compone
this.y -= this.containerOffset.top - this.top;
};

nodes.each((i: number, item: any) => {
item = $(item);

const node = {
id: item.attr('data-id'),
type: item.attr('data-type'),
obj: item,
};

this.nodes.push(node);
this.cacheRect(node);
});
this.initNodes();

if (e.shiftKey) {
const target = $(e.target).closest('.selectable');
Expand All @@ -174,6 +163,23 @@ const SelectionProvider = observer(class SelectionProvider extends React.Compone
win.on(`mousemove.selection`, (e: any) => { this.onMouseMove(e); });
win.on(`blur.selection mouseup.selection`, (e: any) => { this.onMouseUp(e); });
};

initNodes () {
const nodes = this.getPageContainer().find('.selectable');

nodes.each((i: number, item: any) => {
item = $(item);

const node = {
id: item.attr('data-id'),
type: item.attr('data-type'),
obj: item,
};

this.nodes.push(node);
this.cacheRect(node);
});
};

onMouseMove (e: any) {
if (!this._isMounted) {
Expand All @@ -185,13 +191,13 @@ const SelectionProvider = observer(class SelectionProvider extends React.Compone
return;
};

const isPopup = keyboard.isPopup();
const rect = this.getRect(this.x, this.y, e.pageX, e.pageY);

if ((rect.width < THRESHOLD) && (rect.height < THRESHOLD)) {
return;
};

const isPopup = keyboard.isPopup();
this.top = UtilCommon.getScrollContainer(isPopup).scrollTop();
this.checkNodes(e);
this.drawRect(e.pageX, e.pageY);
Expand All @@ -211,12 +217,18 @@ const SelectionProvider = observer(class SelectionProvider extends React.Compone
const x = keyboard.mouse.page.x;
const y = keyboard.mouse.page.y + Math.abs(top - this.top) * d;
const rect = this.getRect(this.x, this.y, x, y);
const { wh } = UtilCommon.getWindowDimensions();

if ((rect.width < THRESHOLD) && (rect.height < THRESHOLD)) {
return;
};

this.nodes.forEach(it => this.cacheRect(it));
if (Math.abs(top - this.startTop) >= wh / 2) {
this.initNodes();
this.startTop = top;
} else {
this.nodes.forEach(it => this.cacheRect(it));
};

this.checkNodes({ ...e, pageX: x, pageY: y });
this.drawRect(x, y);
Expand Down
14 changes: 7 additions & 7 deletions src/ts/store/detail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,16 +155,16 @@ class DetailStore {
return { id, _empty_: true };
};

const keys = new Set(withKeys ? [ ...withKeys, ...(!forceKeys ? Constant.defaultRelationKeys : []) ] : []);
const object = { id };

if (withKeys) {
let keys = [ 'id', ...withKeys ];
if (!forceKeys) {
keys = keys.concat(Constant.defaultRelationKeys);
};
list = list.filter(it => keys.includes(it.relationKey));
list = list.filter(it => keys.has(it.relationKey));
};

const object = { id };
list.forEach(it => object[it.relationKey] = it.value);
for (let i = 0; i < list.length; i++) {
object[list[i].relationKey] = list[i].value;
};

return this.mapper(object);
};
Expand Down

0 comments on commit dc9f3e0

Please sign in to comment.