0
Fork 0
mirror of https://github.com/fastmail/Squire.git synced 2024-12-22 15:23:29 -05:00

Add getCursorPosition method, remove scrollRangeIntoView

Scrolling depends so much on the integration. Better to allow the cursor
position to be fetched, and then the integration can do the scrolling when it
wants (for example on each "input" event).
This commit is contained in:
Neil Jenkins 2016-03-24 13:45:36 +11:00
parent 3be9a7dea8
commit 072a2d9fce
4 changed files with 33 additions and 26 deletions

View file

@ -183,6 +183,11 @@ Returns the path through the DOM tree from the `<body>` element to the current c
Returns an object containing the active font family, size, colour and background colour for the the current cursor position, if any are set. The property names are respectively `family`, `size`, `color` and `backgroundColor`. It looks at style attributes to detect this, so will not detect `<FONT>` tags or non-inline styles. If a selection across multiple elements has been made, it will return an empty object.
### getCursorPosition
Returns a bounding client rect (top/left/right/bottom properties relative to
the viewport) for the current selection/cursor.
### getSelection
Returns a [W3C Range object](https://developer.mozilla.org/en-US/docs/Web/API/Range) representing the current selection/cursor position.

View file

@ -2390,8 +2390,7 @@ proto.getDocument = function () {
// document node, since these events are fired in a custom manner by the
// editor code.
var customEvents = {
pathChange: 1, select: 1, input: 1,
undoStateChange: 1, scrollPointIntoView: 1
pathChange: 1, select: 1, input: 1, undoStateChange: 1
};
proto.fireEvent = function ( type, event ) {
@ -2504,26 +2503,29 @@ proto._createRange =
return domRange;
};
proto.scrollRangeIntoView = function ( range ) {
proto.getCursorPosition = function ( range ) {
if ( !range && !( range = this.getSelection() ) ) {
return null;
}
// Get the bounding rect
var rect = range.getBoundingClientRect();
var node, parent;
if ( rect && !rect.top ) {
this._ignoreChange = true;
node = this._doc.createElement( 'SPAN' );
range = range.cloneRange();
node.textContent = ZWS;
insertNodeInRange( range, node );
rect = node.getBoundingClientRect();
parent = node.parentNode;
parent.removeChild( node );
parent.normalize();
}
// And fire event for integrations to use
if ( rect ) {
this.fireEvent( 'scrollPointIntoView', {
x: rect.left,
y: rect.top
mergeInlines( parent, {
startContainer: range.startContainer,
endContainer: range.endContainer,
startOffset: range.startOffset,
endOffset: range.endOffset
});
}
return rect;
};
proto._moveCursorTo = function ( toStart ) {
@ -2557,7 +2559,6 @@ proto.setSelection = function ( range ) {
if ( sel ) {
sel.removeAllRanges();
sel.addRange( range );
this.scrollRangeIntoView( range );
}
}
return this;

File diff suppressed because one or more lines are too long

View file

@ -186,8 +186,7 @@ proto.getDocument = function () {
// document node, since these events are fired in a custom manner by the
// editor code.
var customEvents = {
pathChange: 1, select: 1, input: 1,
undoStateChange: 1, scrollPointIntoView: 1
pathChange: 1, select: 1, input: 1, undoStateChange: 1
};
proto.fireEvent = function ( type, event ) {
@ -300,26 +299,29 @@ proto._createRange =
return domRange;
};
proto.scrollRangeIntoView = function ( range ) {
proto.getCursorPosition = function ( range ) {
if ( !range && !( range = this.getSelection() ) ) {
return null;
}
// Get the bounding rect
var rect = range.getBoundingClientRect();
var node, parent;
if ( rect && !rect.top ) {
this._ignoreChange = true;
node = this._doc.createElement( 'SPAN' );
range = range.cloneRange();
node.textContent = ZWS;
insertNodeInRange( range, node );
rect = node.getBoundingClientRect();
parent = node.parentNode;
parent.removeChild( node );
parent.normalize();
}
// And fire event for integrations to use
if ( rect ) {
this.fireEvent( 'scrollPointIntoView', {
x: rect.left,
y: rect.top
mergeInlines( parent, {
startContainer: range.startContainer,
endContainer: range.endContainer,
startOffset: range.startOffset,
endOffset: range.endOffset
});
}
return rect;
};
proto._moveCursorTo = function ( toStart ) {
@ -353,7 +355,6 @@ proto.setSelection = function ( range ) {
if ( sel ) {
sel.removeAllRanges();
sel.addRange( range );
this.scrollRangeIntoView( range );
}
}
return this;