Skip to content

Commit

Permalink
Merge pull request danielearwicker#27 from creately/add-caret-methods
Browse files Browse the repository at this point in the history
Added public methods related to caret
  • Loading branch information
thani-sh committed Jun 20, 2019
2 parents 1c646cf + 23145b2 commit ed445f0
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@creately/carota",
"author": "Daniel Earwicker (dan@earwicker.com)",
"description": "Simple, flexible rich text rendering/editing on HTML Canvas",
"version": "2.5.0",
"version": "2.6.0",
"repository": {
"type": "git",
"url": "https://github.com/danielearwicker/carota.git"
Expand Down
46 changes: 46 additions & 0 deletions src/doc.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,52 @@ var prototype = node.derive({
documentRange: function() {
return this.range(0, this.frame.length - 1);
},

isMultiLine: function() {
return this.frame.lines.length > 1;
},
isCaretAtEnd: function() {
return this.documentRange().end === this.selection.end;
},
isCaretAtStart: function() {
return this.documentRange().start === this.selection.end;
},
isCaretAtLastLine: function() {
var endCaret = this.getCaretCoords( this.documentRange().end );
var currentCaret = this.getCurrentCaret();
return endCaret.t === currentCaret.t;
},
isCaretAtFirstLine: function() {
var startCaret = this.getCaretCoords( this.documentRange().start );
var currentCaret = this.getCurrentCaret();
return startCaret.t === currentCaret.t;
},
getCurrentCaret: function() {
return this.getCaretCoords( this.selection.end );
},

getSelectionBounds: function() {
if ( this.selection.end === this.selection.start ) {
return;
}
var startCaret = this.getCaretCoords( this.selection.start );
var endCaret = this.getCaretCoords( this.selection.end );
if ( startCaret.t === endCaret.t ) {
return {
x: startCaret.l,
y: startCaret.t,
width: endCaret.l - startCaret.l,
}
} else {
return {
x: 0,
y: startCaret.t,
width: this.frame._bounds.w,
}
}
// TODO - Find height
},

selectedRange: function() {
return this.range(this.selection.start, this.selection.end);
},
Expand Down

0 comments on commit ed445f0

Please sign in to comment.