diff --git a/src/editor/Editor.jsx b/src/editor/Editor.jsx index 283c955..6e0186b 100644 --- a/src/editor/Editor.jsx +++ b/src/editor/Editor.jsx @@ -50,13 +50,13 @@ const Editor = props => { if (shouldApplyTemplate && props.applyImmediately) props.onAnnotationCreated(annotation.toAnnotation()); - if (element.current) + if (element.current) { setPosition(props.wrapperEl, element.current, props.selectedElement); + return initResizeObserver(); + } }, [ props.selectedElement, bounds(props.selectedElement) ]); - // Attach resize listener on mount - useEffect(() => { - // Older iOS Safaris don't support ResizeObserver + const initResizeObserver = () => { if (window.ResizeObserver) { const resizeObserver = new ResizeObserver(() => { setPosition(props.wrapperEl, element.current, props.selectedElement); @@ -64,8 +64,8 @@ const Editor = props => { resizeObserver.observe(props.wrapperEl); return () => resizeObserver.disconnect(); - } - }, []); + } + } // Creator and created/modified timestamp metadata const creationMeta = body => { diff --git a/src/editor/setPosition.js b/src/editor/setPosition.js index 907baab..4da14bc 100644 --- a/src/editor/setPosition.js +++ b/src/editor/setPosition.js @@ -1,6 +1,6 @@ /** Sets the editor position and determines a proper orientation **/ const setPosition = (wrapperEl, editorEl, selectedEl) => { - // Container element offset + // Container element offset const containerBounds = wrapperEl.getBoundingClientRect(); const { scrollX, scrollY } = window; @@ -23,10 +23,13 @@ const setPosition = (wrapperEl, editorEl, selectedEl) => { editorEl.style.left = `${right - defaultOrientation.width + scrollX - containerBounds.left}px`; } - if (defaultOrientation.bottom > window.innerHeight - scrollY) { + console.log(defaultOrientation.bottom, window.innerHeight) + + if (defaultOrientation.bottom + scrollY > window.innerHeight) { // Flip vertically - const annotationTop = top + scrollY; // Annotation top relative to parents - const containerHeight = containerBounds.height + containerBounds.top + scrollY; + console.log('flipping'); + const annotationTop = top + scrollY; // Annotation bottom relative to parents + const containerHeight = containerBounds.bottom + scrollY; editorEl.classList.add('align-bottom'); editorEl.style.top = 'auto';