Skip to content

Commit

Permalink
Fix/trigger on new word (#58)
Browse files Browse the repository at this point in the history
* fix: do not trigger on different word

* chore: update version
  • Loading branch information
visualjerk committed Jul 14, 2021
1 parent 58d9e0d commit e142e90
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 18 deletions.
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "quill-magic-url",
"version": "4.1.1",
"version": "4.1.2",
"description": "Checks for URLs during typing and pasting and automatically converts them to links.",
"main": "dist/index.js",
"babel": {
Expand Down
33 changes: 16 additions & 17 deletions src/index.js
Expand Up @@ -121,28 +121,27 @@ export default class MagicUrl {
return
}

const urlMatch = text.match(this.options.urlRegularExpression)
const mailMatch = text.match(this.options.mailRegularExpression)
if (urlMatch) {
const match = urlMatch.pop()
const matchIndex = text.lastIndexOf(match)
this.textToUrl(leafIndex + matchIndex, match.trim())
} else if (mailMatch) {
const match = mailMatch.pop()
const matchIndex = text.lastIndexOf(match)
this.textToMail(leafIndex + matchIndex, match.trim())
const urlMatches = text.match(this.options.urlRegularExpression)
const mailMatches = text.match(this.options.mailRegularExpression)
if (urlMatches) {
this.handleMatches(leafIndex, text, urlMatches, this.urlNormalizer)
} else if (mailMatches) {
this.handleMatches(leafIndex, text, mailMatches, this.mailNormalizer)
}
}
textToUrl(index, url) {
const ops = new Delta()
.retain(index)
.retain(url.length, { link: this.urlNormalizer(url) })
this.quill.updateContents(ops)
handleMatches(leafIndex, text, matches, normalizer) {
const match = matches.pop()
const matchIndex = text.lastIndexOf(match)
const after = text.split(match).pop()
if (after.match(/\S/)) {
return
}
this.updateText(leafIndex + matchIndex, match.trim(), normalizer)
}
textToMail(index, mail) {
updateText(index, string, normalizer) {
const ops = new Delta()
.retain(index)
.retain(mail.length, { link: this.mailNormalizer(mail) })
.retain(string.length, { link: normalizer(string) })
this.quill.updateContents(ops)
}
normalize(url) {
Expand Down
5 changes: 5 additions & 0 deletions tests/e2e/index.spec.js
Expand Up @@ -97,6 +97,11 @@ describe('quill-magic-url', () => {
shouldContain('<p>http://test.de </p>')
})

it('on new word in same line', () => {
type(' new ')
shouldContain('<p>http://test.de new </p>')
})

it('on blank space and enter', () => {
type('{enter}')
shouldContain('<p>http://test.de </p><p><br></p>')
Expand Down

0 comments on commit e142e90

Please sign in to comment.