Skip to content

Commit

Permalink
Merge pull request danielearwicker#45 from creately/break-hyperlinks
Browse files Browse the repository at this point in the history
Break hyperlinks at the start and end
  • Loading branch information
thani-sh authored Sep 3, 2019
2 parents 8b2cbc6 + 69d7b1e commit 666355e
Show file tree
Hide file tree
Showing 3 changed files with 41 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 ([email protected])",
"description": "Simple, flexible rich text rendering/editing on HTML Canvas",
"version": "2.12.0",
"version": "2.13.0",
"repository": {
"type": "git",
"url": "https://github.com/danielearwicker/carota.git"
Expand Down
36 changes: 36 additions & 0 deletions src/doc.js
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,42 @@ var prototype = node.derive({
return this.range( currentCaret - left + 1, currentCaret + right - 1 );
},

/**
* This function applies the previouse value to the newly entered char.
* replacing the link and link related styles
*/
breakHyperlink: function () {
var currentCaret = this.selection.start;
var newCharRange = this.range( currentCaret - 1, currentCaret );
var prevFormat = this.getPreviousFormat([ 'link' ], currentCaret - 1 );
newCharRange.setFormatting( 'link', undefined, true );
newCharRange.setFormatting( 'color', prevFormat.color, true );
newCharRange.setFormatting( 'underline', prevFormat.underline, true );
},

/**
* This function returns true if the caret is at the begining or end of a link
*/
isCaretAtHyperlinkEdge: function () {
if ( this.selection.start !== this.selection.end && this.frame.length < 1 ) {
return false
}
if ( this.selection.start === 0 && this.range( 0, 0 ).getFormatting().link ) {
return true;
}
var last = this.frame.length - 1;
if ( this.selection.start === last && this.range( last, last ).getFormatting().link ) {
return true;
}
var caret = this.selection.start;
var leftFormat = this.range( caret, caret ).getFormatting();
var rightFormat = this.range( caret, caret + 1 ).getFormatting();
if ( leftFormat.link !== rightFormat.link ) {
return true;
}
return false;
},

autoLink: function () {
var links =this.words.filter( w => isUrlValid( w.text.plainText ));
links.forEach( link => {
Expand Down
4 changes: 4 additions & 0 deletions src/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -358,13 +358,17 @@ exports.create = function(element, defaultFormatting, drawtext = true ) {
if (newText === plainClipboard) {
newText = richClipboard;
}
var isCaretAtLinkEdge = doc.isCaretAtHyperlinkEdge();
doc.insert(newText);
if ( newText === ' ' || newText === '\n' ) {
doc.autolinkOnKeyup();
}
if ( newText.length >= 'http://x.xx'.length ) {
doc.autoLink();
}
if ( isCaretAtLinkEdge ) {
doc.breakHyperlink();
}
}
});

Expand Down

0 comments on commit 666355e

Please sign in to comment.