Skip to content
This repository was archived by the owner on Apr 18, 2024. It is now read-only.

Commit 5bd5637

Browse files
Travis1282Travis Clarkhlomzik
authored
fix: DEV-1516 Double click applies selected (#479)
* if double click save the label from the first click and apply on the second otherwise clear * add timestamp comparison to differentiate between a double click and a two staggered clicks * Use selectedValues() for label during double click * ?. just in case; comment about var * Don't use empty doubleClickSelection * Also check coords for fast clicking everywhere this should not be treated as double click Co-authored-by: Travis Clark <“[email protected]”> Co-authored-by: hlomzik <[email protected]>
1 parent 4a5f8d9 commit 5bd5637

File tree

2 files changed

+30
-7
lines changed

2 files changed

+30
-7
lines changed

src/tags/object/RichText/model.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,13 +296,15 @@ const Model = types
296296
return region;
297297
},
298298

299-
addRegion(range) {
299+
addRegion(range, doubleClickLabel) {
300300
const states = self.getAvailableStates();
301301

302302
if (states.length === 0) return;
303303

304304
const control = states[0];
305-
const labels = { [control.valueType]: control.selectedValues() };
305+
const values = doubleClickLabel?.value ?? control.selectedValues();
306+
const labels = { [control.valueType]: values };
307+
306308
const area = self.annotation.createResult(range, labels, control, self);
307309
const rootEl = self.rootNodeRef.current;
308310
const root = rootEl?.contentDocument?.body ?? rootEl;

src/tags/object/RichText/view.js

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ import { LoadingOutlined } from "@ant-design/icons";
1111
import { Block, cn, Elem } from "../../../utils/bem";
1212
import { observe } from "mobx";
1313

14+
const DBLCLICK_TIMEOUT = 450; // ms
15+
const DBLCLICK_RANGE = 5; // px
16+
1417
class RichTextPieceView extends Component {
1518
_regionSpanSelector = ".htx-highlight";
1619

@@ -26,6 +29,9 @@ class RichTextPieceView extends Component {
2629
this.loadingRef = React.createRef();
2730

2831
this.rootRef = props.item.rootNodeRef;
32+
33+
// store value of first selected label during double click to apply it later
34+
this.doubleClickSelection = undefined;
2935
}
3036

3137
_selectRegions = (additionalMode) => {
@@ -63,8 +69,8 @@ class RichTextPieceView extends Component {
6369

6470
if (!states || states.length === 0 || ev.ctrlKey || ev.metaKey) return this._selectRegions(ev.ctrlKey || ev.metaKey);
6571
if (item.selectionenabled === false) return;
66-
6772
const label = states[0]?.selectedLabels?.[0];
73+
const value = states[0]?.selectedValues?.();
6874

6975
Utils.Selection.captureSelection(({ selectionText, range }) => {
7076
if (!range || range.collapsed || !root.contains(range.startContainer) || !root.contains(range.endContainer)) {
@@ -77,17 +83,33 @@ class RichTextPieceView extends Component {
7783

7884
if (!normedRange) return;
7985

86+
if (this.doubleClickSelection && (
87+
Date.now() - this.doubleClickSelection.time > DBLCLICK_TIMEOUT
88+
|| Math.abs(ev.pageX - this.doubleClickSelection.x) > DBLCLICK_RANGE
89+
|| Math.abs(ev.pageY - this.doubleClickSelection.y) > DBLCLICK_RANGE
90+
)) {
91+
this.doubleClickSelection = undefined;
92+
}
93+
8094
normedRange._range = range;
8195
normedRange.text = selectionText;
8296
normedRange.isText = item.type === "text";
8397
normedRange.dynamic = this.props.store.autoAnnotation;
84-
85-
item.addRegion(normedRange);
98+
item.addRegion(normedRange, this.doubleClickSelection);
8699
}, {
87100
window: rootEl?.contentWindow ?? window,
88101
granularity: label?.granularity ?? item.granularity,
89-
beforeCleanup: () => (this._selectionMode = true),
102+
beforeCleanup: () => {
103+
this.doubleClickSelection = undefined;
104+
this._selectionMode = true;
105+
},
90106
});
107+
this.doubleClickSelection = {
108+
time: Date.now(),
109+
value: value?.length ? value : undefined,
110+
x: ev.pageX,
111+
y: ev.pageY,
112+
};
91113
};
92114

93115
/**
@@ -98,7 +120,6 @@ class RichTextPieceView extends Component {
98120
this._selectionMode = false;
99121
return;
100122
}
101-
102123
if (!this.props.item.clickablelinks && matchesSelector(event.target, "a")) {
103124
event.preventDefault();
104125
return;

0 commit comments

Comments
 (0)