diff --git a/demo/absolute-container.html b/demo/absolute-container.html new file mode 100644 index 000000000..175c432f7 --- /dev/null +++ b/demo/absolute-container.html @@ -0,0 +1,61 @@ + + + + + medium editor | demo + + + + + + + + + Fork me on GitHub +
+ Theme: + +
+
+

Medium Editor

+
+

My father’s family name being Pirrip, and my Christian name Philip, my infant tongue could make of both names nothing longer or more explicit than Pip. So, I called myself Pip, and came to be called Pip.

+

I give Pirrip as my father’s family name, on the authority of his tombstone and my sister,—Mrs. Joe Gargery, who married the blacksmith. As I never saw my father or my mother, and never saw any likeness of either of them (for their days were long before the days of photographs), my first fancies regarding what they were like were unreasonably derived from their tombstones. The shape of the letters on my father’s, gave me an odd idea that he was a square, stout, dark man, with curly black hair. From the character and turn of the inscription, “Also Georgiana Wife of the Above,” I drew a childish conclusion that my mother was freckled and sickly. To five little stone lozenges, each about a foot and a half long, which were arranged in a neat row beside their grave, and were sacred to the memory of five little brothers of mine,—who gave up trying to get a living, exceedingly early in that universal struggle,—I am indebted for a belief I religiously entertained that they had all been born on their backs with their hands in their trousers-pockets, and had never taken them out in this state of existence.

+

Ours was the marsh country, down by the river, within, as the river wound, twenty miles of the sea. My first most vivid and broad impression of the identity of things seems to me to have been gained on a memorable raw afternoon towards evening. At such a time I found out for certain that this bleak place overgrown with nettles was the churchyard; and that Philip Pirrip, late of this parish, and also Georgiana wife of the above, were dead and buried; and that Alexander, Bartholomew, Abraham, Tobias, and Roger, infant children of the aforesaid, were also dead and buried; and that the dark flat wilderness beyond the churchyard, intersected with dikes and mounds and gates, with scattered cattle feeding on it, was the marshes; and that the low leaden line beyond was the river; and that the distant savage lair from which the wind was rushing was the sea; and that the small bundle of shivers growing afraid of it all and beginning to cry, was Pip.

+
+
+

Source

+ + + + diff --git a/spec/anchor-preview.spec.js b/spec/anchor-preview.spec.js index fff98ba45..2249e0f01 100644 --- a/spec/anchor-preview.spec.js +++ b/spec/anchor-preview.spec.js @@ -407,6 +407,40 @@ describe('Anchor Preview TestCase', function () { expect(document.querySelector('.medium-editor-anchor-preview')).toBeNull(); }); + + it('should correctly set preview position even if elementsContainer is absolute', function () { + var container = document.createElement('div'), + editor, anchorPreview; + + container.style.position = 'absolute'; + container.style.left = '100px'; + container.style.top = '100px'; + document.body.appendChild(container); + + editor = this.newMediumEditor('.editor', { + elementsContainer: container, + anchorPreview: { + showWhenToolbarIsVisible: true + }, + toolbar: { + static: true + } + }); + anchorPreview = editor.getExtensionByName('anchor-preview').getPreviewElement(); + + selectElementContentsAndFire(editor.elements[0].firstChild); + + // show preview + fireEvent(document.getElementById('test-link'), 'mouseover'); + + // preview shows only after delay + jasmine.clock().tick(1); + expect(anchorPreview.classList.contains('medium-editor-anchor-preview-active')).toBe(true); + expect(parseInt(anchorPreview.style.left, 10)).toBeLessThan(100); + expect(parseInt(anchorPreview.style.top, 10)).toBeLessThan(100); + + document.body.removeChild(container); + }); }); }); diff --git a/spec/toolbar.spec.js b/spec/toolbar.spec.js index 3dc7b65b9..50fcc7a2e 100644 --- a/spec/toolbar.spec.js +++ b/spec/toolbar.spec.js @@ -362,6 +362,30 @@ describe('MediumEditor.extensions.toolbar TestCase', function () { expect(toolbar.setToolbarButtonStates).toHaveBeenCalled(); expect(toolbar.showAndUpdateToolbar).toHaveBeenCalled(); }); + + it('should correctly update position even if elementsContainer is absolute', function () { + var container = document.createElement('div'), + editor, toolbar; + + container.style.position = 'absolute'; + container.style.left = '100px'; + container.style.top = '100px'; + document.body.appendChild(container); + + this.el.innerHTML = 'lorem'; + + editor = this.newMediumEditor('.editor', { + elementsContainer: container + }); + toolbar = editor.getExtensionByName('toolbar').getToolbarElement(); + + selectElementContentsAndFire(this.el); + expect(toolbar.classList.contains('medium-editor-toolbar-active')).toBe(true); + expect(parseInt(toolbar.style.left, 10)).toBeLessThan(100); + expect(parseInt(toolbar.style.top, 10)).toBeLessThan(100); + + document.body.removeChild(container); + }); }); describe('Static Toolbars', function () { diff --git a/src/js/extensions/anchor-preview.js b/src/js/extensions/anchor-preview.js index 79a358ab6..d55a8d8cf 100644 --- a/src/js/extensions/anchor-preview.js +++ b/src/js/extensions/anchor-preview.js @@ -103,13 +103,15 @@ positionPreview: function (activeAnchor) { activeAnchor = activeAnchor || this.activeAnchor; - var buttonHeight = this.anchorPreview.offsetHeight, + var containerWidth = this.window.innerWidth, + buttonHeight = this.anchorPreview.offsetHeight, boundary = activeAnchor.getBoundingClientRect(), - middleBoundary = (boundary.left + boundary.right) / 2, diffLeft = this.diffLeft, diffTop = this.diffTop, - halfOffsetWidth, - defaultLeft; + elementsContainer = this.getEditorOption('elementsContainer'), + elementsContainerAbsolute = ['absolute', 'fixed'].indexOf(window.getComputedStyle(elementsContainer).getPropertyValue('position')) > -1, + relativeBoundary = {}, + halfOffsetWidth, defaultLeft, middleBoundary, elementsContainerBoundary, top; halfOffsetWidth = this.anchorPreview.offsetWidth / 2; var toolbarExtension = this.base.getExtensionByName('toolbar'); @@ -119,12 +121,35 @@ } defaultLeft = diffLeft - halfOffsetWidth; - this.anchorPreview.style.top = Math.round(buttonHeight + boundary.bottom - diffTop + this.window.pageYOffset - this.anchorPreview.offsetHeight) + 'px'; + // If container element is absolute / fixed, recalculate boundaries to be relative to the container + if (elementsContainerAbsolute) { + elementsContainerBoundary = elementsContainer.getBoundingClientRect(); + ['top', 'left'].forEach(function (key) { + relativeBoundary[key] = boundary[key] - elementsContainerBoundary[key]; + }); + + relativeBoundary.width = boundary.width; + relativeBoundary.height = boundary.height; + boundary = relativeBoundary; + + containerWidth = elementsContainerBoundary.width; + + // Adjust top position according to container scroll position + top = elementsContainer.scrollTop; + } else { + // Adjust top position according to window scroll position + top = this.window.pageYOffset; + } + + middleBoundary = boundary.left + boundary.width / 2; + top += buttonHeight + boundary.top + boundary.height - diffTop - this.anchorPreview.offsetHeight; + + this.anchorPreview.style.top = Math.round(top) + 'px'; this.anchorPreview.style.right = 'initial'; if (middleBoundary < halfOffsetWidth) { this.anchorPreview.style.left = defaultLeft + halfOffsetWidth + 'px'; this.anchorPreview.style.right = 'initial'; - } else if ((this.window.innerWidth - middleBoundary) < halfOffsetWidth) { + } else if ((containerWidth - middleBoundary) < halfOffsetWidth) { this.anchorPreview.style.left = 'auto'; this.anchorPreview.style.right = 0; } else { diff --git a/src/js/extensions/toolbar.js b/src/js/extensions/toolbar.js index 16ce86d53..ab94bab43 100644 --- a/src/js/extensions/toolbar.js +++ b/src/js/extensions/toolbar.js @@ -616,35 +616,66 @@ } } - var windowWidth = this.window.innerWidth, - middleBoundary = (boundary.left + boundary.right) / 2, + var containerWidth = this.window.innerWidth, toolbarElement = this.getToolbarElement(), toolbarHeight = toolbarElement.offsetHeight, toolbarWidth = toolbarElement.offsetWidth, halfOffsetWidth = toolbarWidth / 2, buttonHeight = 50, - defaultLeft = this.diffLeft - halfOffsetWidth; + defaultLeft = this.diffLeft - halfOffsetWidth, + elementsContainer = this.getEditorOption('elementsContainer'), + elementsContainerAbsolute = ['absolute', 'fixed'].indexOf(window.getComputedStyle(elementsContainer).getPropertyValue('position')) > -1, + positions = {}, + relativeBoundary = {}, + middleBoundary, elementsContainerBoundary; + + // If container element is absolute / fixed, recalculate boundaries to be relative to the container + if (elementsContainerAbsolute) { + elementsContainerBoundary = elementsContainer.getBoundingClientRect(); + ['top', 'left'].forEach(function (key) { + relativeBoundary[key] = boundary[key] - elementsContainerBoundary[key]; + }); + + relativeBoundary.width = boundary.width; + relativeBoundary.height = boundary.height; + boundary = relativeBoundary; + + containerWidth = elementsContainerBoundary.width; + + // Adjust top position according to container scroll position + positions.top = elementsContainer.scrollTop; + } else { + // Adjust top position according to window scroll position + positions.top = this.window.pageYOffset; + } + + middleBoundary = boundary.left + boundary.width / 2; + positions.top += boundary.top - toolbarHeight; if (boundary.top < buttonHeight) { toolbarElement.classList.add('medium-toolbar-arrow-over'); toolbarElement.classList.remove('medium-toolbar-arrow-under'); - toolbarElement.style.top = buttonHeight + boundary.bottom - this.diffTop + this.window.pageYOffset - toolbarHeight + 'px'; + positions.top += buttonHeight + boundary.height - this.diffTop; } else { toolbarElement.classList.add('medium-toolbar-arrow-under'); toolbarElement.classList.remove('medium-toolbar-arrow-over'); - toolbarElement.style.top = boundary.top + this.diffTop + this.window.pageYOffset - toolbarHeight + 'px'; + positions.top += this.diffTop; } if (middleBoundary < halfOffsetWidth) { - toolbarElement.style.left = defaultLeft + halfOffsetWidth + 'px'; - toolbarElement.style.right = 'initial'; - } else if ((windowWidth - middleBoundary) < halfOffsetWidth) { - toolbarElement.style.left = 'auto'; - toolbarElement.style.right = 0; + positions.left = defaultLeft + halfOffsetWidth; + positions.right = 'initial'; + } else if ((containerWidth - middleBoundary) < halfOffsetWidth) { + positions.left = 'auto'; + positions.right = 0; } else { - toolbarElement.style.left = defaultLeft + middleBoundary + 'px'; - toolbarElement.style.right = 'initial'; + positions.left = defaultLeft + middleBoundary; + positions.right = 'initial'; } + + ['top', 'left', 'right'].forEach(function (key) { + toolbarElement.style[key] = positions[key] + (isNaN(positions[key]) ? '' : 'px'); + }); } });