implement scroll-fixer
This commit is contained in:
parent
38558a7fad
commit
c098e8e745
@ -1,7 +1,7 @@
|
|||||||
/* global Y */
|
/* global Y */
|
||||||
|
|
||||||
window.onload = function () {
|
window.onload = function () {
|
||||||
window.domBinding = new Y.DomBinding(window.yXmlType, document.body)
|
window.domBinding = new Y.DomBinding(window.yXmlType, document.body, { scrollingElement: document.scrollingElement })
|
||||||
}
|
}
|
||||||
|
|
||||||
let y = new Y('htmleditor', {
|
let y = new Y('htmleditor', {
|
||||||
|
@ -33,6 +33,7 @@ export default class DomBinding extends Binding {
|
|||||||
this.opts = opts
|
this.opts = opts
|
||||||
opts.document = opts.document || document
|
opts.document = opts.document || document
|
||||||
opts.hooks = opts.hooks || {}
|
opts.hooks = opts.hooks || {}
|
||||||
|
this.scrollingElement = opts.scrollingElement || null
|
||||||
/**
|
/**
|
||||||
* Maps each DOM element to the type that it is associated with.
|
* Maps each DOM element to the type that it is associated with.
|
||||||
* @type {Map}
|
* @type {Map}
|
||||||
@ -105,17 +106,6 @@ export default class DomBinding extends Binding {
|
|||||||
createAssociation(this, target, type)
|
createAssociation(this, target, type)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Enables the smart scrolling functionality for a Dom Binding.
|
|
||||||
* This is useful when YXml is bound to a shared editor. When activated,
|
|
||||||
* the viewport will be changed to accommodate remote changes.
|
|
||||||
*
|
|
||||||
* @param {Element} scrollElement The node that is
|
|
||||||
*/
|
|
||||||
enableSmartScrolling (scrollElement) {
|
|
||||||
// @TODO: implement smart scrolling
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* NOTE: currently does not apply filter to existing elements!
|
* NOTE: currently does not apply filter to existing elements!
|
||||||
* @param {FilterFunction} filter The filter function to use from now on.
|
* @param {FilterFunction} filter The filter function to use from now on.
|
||||||
|
@ -125,9 +125,6 @@ export default function domObserver (mutations, _document) {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
for (let dom of diffChildren) {
|
for (let dom of diffChildren) {
|
||||||
if (dom.yOnChildrenChanged !== undefined) {
|
|
||||||
dom.yOnChildrenChanged()
|
|
||||||
}
|
|
||||||
const yxml = this.domToType.get(dom)
|
const yxml = this.domToType.get(dom)
|
||||||
applyChangesFromDom(this, dom, yxml, _document)
|
applyChangesFromDom(this, dom, yxml, _document)
|
||||||
}
|
}
|
||||||
|
@ -1,13 +1,49 @@
|
|||||||
|
/* global getSelection */
|
||||||
|
|
||||||
import YXmlText from '../../Types/YXml/YXmlText.js'
|
import YXmlText from '../../Types/YXml/YXmlText.js'
|
||||||
import YXmlHook from '../../Types/YXml/YXmlHook.js'
|
import YXmlHook from '../../Types/YXml/YXmlHook.js'
|
||||||
import { removeDomChildrenUntilElementFound } from './util.js'
|
import { removeDomChildrenUntilElementFound } from './util.js'
|
||||||
|
|
||||||
|
function findScrollReference (scrollingElement) {
|
||||||
|
if (scrollingElement !== null) {
|
||||||
|
let anchor = getSelection().anchorNode
|
||||||
|
if (anchor == null) {
|
||||||
|
let children = scrollingElement.children
|
||||||
|
for (let i = 0; i < children.length; i++) {
|
||||||
|
const elem = children[i]
|
||||||
|
const rect = elem.getBoundingClientRect()
|
||||||
|
if (rect.top >= 0) {
|
||||||
|
return { elem, top: rect.top }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (anchor.nodeType === document.TEXT_NODE) {
|
||||||
|
anchor = anchor.parentElement
|
||||||
|
}
|
||||||
|
const top = anchor.getBoundingClientRect().top
|
||||||
|
return { elem: anchor, top: top }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
function fixScroll (scrollingElement, ref) {
|
||||||
|
if (ref !== null) {
|
||||||
|
const { elem, top } = ref
|
||||||
|
const currentTop = elem.getBoundingClientRect().top
|
||||||
|
const newScroll = scrollingElement.scrollTop + currentTop - top
|
||||||
|
if (newScroll >= 0) {
|
||||||
|
scrollingElement.scrollTop = newScroll
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
export default function typeObserver (events) {
|
export default function typeObserver (events) {
|
||||||
this._mutualExclude(() => {
|
this._mutualExclude(() => {
|
||||||
|
const scrollRef = findScrollReference(this.scrollingElement)
|
||||||
events.forEach(event => {
|
events.forEach(event => {
|
||||||
const yxml = event.target
|
const yxml = event.target
|
||||||
const dom = this.typeToDom.get(yxml)
|
const dom = this.typeToDom.get(yxml)
|
||||||
@ -59,5 +95,6 @@ export default function typeObserver (events) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
fixScroll(this.scrollingElement, scrollRef)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user