Internal API change: keep ref to selected DOM node, not just bounds

This commit is contained in:
Rainer Simon 2020-05-12 19:50:33 +02:00
parent 1508ed1207
commit 4825f3b21d
4 changed files with 13 additions and 15 deletions

View File

@ -15,8 +15,8 @@ addPolyfills(); // For Microsoft Edge
export default class TextAnnotator extends Component { export default class TextAnnotator extends Component {
state = { state = {
selectionBounds: null,
selectedAnnotation: null, selectedAnnotation: null,
selectedDOMElement: null,
selectedRelation: null, selectedRelation: null,
applyTemplate: null, applyTemplate: null,
@ -26,8 +26,8 @@ export default class TextAnnotator extends Component {
/** Shorthand **/ /** Shorthand **/
clearState = () => { clearState = () => {
this.setState({ this.setState({
selectionBounds: null, selectedAnnotation: null,
selectedAnnotation: null selectedDOMElement: null
}); });
} }
@ -62,14 +62,14 @@ export default class TextAnnotator extends Component {
/** Selection on the text **/ /** Selection on the text **/
handleSelect = evt => { handleSelect = evt => {
const { selection, clientRect } = evt; const { selection, element } = evt;
if (selection) { if (selection) {
this.setState({ this.setState({
selectedAnnotation: null, selectedAnnotation: null,
selectionBounds: null selectedDOMElement: null
}, () => this.setState({ }, () => this.setState({
selectedAnnotation: selection, selectedAnnotation: selection,
selectionBounds: clientRect selectedDOMElement: element
})) }))
} else { } else {
this.clearState(); this.clearState();
@ -264,8 +264,8 @@ export default class TextAnnotator extends Component {
{ this.state.selectedAnnotation && { this.state.selectedAnnotation &&
<Editor <Editor
wrapperEl={this.props.wrapperEl} wrapperEl={this.props.wrapperEl}
bounds={this.state.selectionBounds}
annotation={this.state.selectedAnnotation} annotation={this.state.selectedAnnotation}
selectedElement={this.state.selectedDOMElement}
readOnly={this.props.readOnly} readOnly={this.props.readOnly}
headless={this.state.headless} headless={this.state.headless}
applyTemplate={this.state.applyTemplate} applyTemplate={this.state.applyTemplate}

View File

@ -39,8 +39,8 @@ const Editor = props => {
props.onAnnotationCreated(annotation.toAnnotation()); props.onAnnotationCreated(annotation.toAnnotation());
if (element.current) if (element.current)
setPosition(props.wrapperEl, element.current, props.bounds); setPosition(props.wrapperEl, element.current, props.selectedElement);
}, [ props.bounds ]); }, [ props.selectedElement ]);
const onAppendBody = body => setCurrentAnnotation( const onAppendBody = body => setCurrentAnnotation(
currentAnnotation.clone({ currentAnnotation.clone({

View File

@ -1,5 +1,5 @@
/** Sets the editor position and determines a proper orientation **/ /** Sets the editor position and determines a proper orientation **/
const setPosition = (wrapperEl, editorEl, annotationBounds) => { const setPosition = (wrapperEl, editorEl, selectedEl) => {
// Container element offset // Container element offset
const containerBounds = wrapperEl.getBoundingClientRect(); const containerBounds = wrapperEl.getBoundingClientRect();
const { scrollX, scrollY } = window; const { scrollX, scrollY } = window;
@ -11,7 +11,7 @@ const setPosition = (wrapperEl, editorEl, annotationBounds) => {
editorEl.style.opacity = 1; editorEl.style.opacity = 1;
// Default orientation // Default orientation
const { left, top, right, height } = annotationBounds; const { left, top, right, height } = selectedEl.getBoundingClientRect();
editorEl.style.top = `${top + height - containerBounds.top}px`; editorEl.style.top = `${top + height - containerBounds.top}px`;
editorEl.style.left = `${left + scrollX - containerBounds.left}px`; editorEl.style.left = `${left + scrollX - containerBounds.left}px`;

View File

@ -41,7 +41,7 @@ export default class SelectionHandler extends EventEmitter {
if (annotationSpan) { if (annotationSpan) {
this.emit('select', { this.emit('select', {
selection: this.highlighter.getAnnotationsAt(annotationSpan)[0], selection: this.highlighter.getAnnotationsAt(annotationSpan)[0],
clientRect: annotationSpan.getBoundingClientRect() element: annotationSpan
}); });
} else { } else {
// De-select // De-select
@ -51,8 +51,6 @@ export default class SelectionHandler extends EventEmitter {
const selectedRange = trimRange(selection.getRangeAt(0)); const selectedRange = trimRange(selection.getRangeAt(0));
const stub = rangeToSelection(selectedRange, this.el); const stub = rangeToSelection(selectedRange, this.el);
const clientRect = selectedRange.getBoundingClientRect();
const spans = this.highlighter.wrapRange(selectedRange); const spans = this.highlighter.wrapRange(selectedRange);
spans.forEach(span => span.className = 'r6o-selection'); spans.forEach(span => span.className = 'r6o-selection');
@ -60,7 +58,7 @@ export default class SelectionHandler extends EventEmitter {
this.emit('select', { this.emit('select', {
selection: stub, selection: stub,
clientRect element: selectedRange
}); });
} }
} }