-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace entire term instead of leaving replaced tail in text #163
- Loading branch information
Egor Stambakio
committed
Aug 19, 2019
1 parent
77f977b
commit 83407ad
Showing
3 changed files
with
75 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
src/client/components/PlainMarkdownInput/plugins/slate-autocomplete-plugin/utils.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
export const matchUnderCursor = ({ text, regexp, cursor }) => { | ||
const result = { | ||
match: '', | ||
start: cursor, | ||
end: cursor | ||
} | ||
|
||
for (let start = cursor; start >= 0; start--) { | ||
for (let end = cursor; end <= text.length; end++) { | ||
const chunk = text.slice(start, end); | ||
if (regexp.test(chunk) && chunk.length > result.match.length) { | ||
result.match = chunk; | ||
result.start = start; | ||
result.end = end; | ||
} | ||
} | ||
} | ||
|
||
return result; | ||
} |
42 changes: 42 additions & 0 deletions
42
src/client/components/PlainMarkdownInput/plugins/slate-autocomplete-plugin/utils.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { expect } from 'chai'; | ||
|
||
import { matchUnderCursor } from './utils'; | ||
|
||
describe('matchUnderCursor', () => { | ||
it('should find regexp match under cursor', () => { | ||
const regexp = /^\$(\w*)$/; | ||
|
||
const tests = [ | ||
// returns match under cursor | ||
{ | ||
args: { text: 'abc $def ghi $jkl mno', regexp, cursor: 5 }, | ||
want: { match: '$def', start: 4, end: 8 } | ||
}, | ||
{ | ||
args: { text: 'abc $def ghi $jkl mno', regexp, cursor: 14 }, | ||
want: { match: '$jkl', start: 13, end: 17 } | ||
}, | ||
// move cursor left to right to test all cases | ||
...('abc $def ghi'.split('').reduce((acc, ch, cursor) => { | ||
return [ | ||
...acc, | ||
{ | ||
args: { text: 'abc $def ghi', regexp, cursor }, | ||
...( | ||
// if cursor is out of match then return empty match | ||
(cursor < 4 || cursor > 8) ? | ||
{ want: { match: '', start: cursor, end: cursor } } : | ||
// if cursor is on (or on edges of) match then return match and its boundaries | ||
{ want: { match: '$def', start: 4, end: 8 } } | ||
) | ||
} | ||
] | ||
}, [])) | ||
] | ||
|
||
tests.forEach(({ args, want }) => { | ||
const got = matchUnderCursor(args) | ||
expect(got).to.deep.equal(want, `input: ${JSON.stringify(args)}`) | ||
}) | ||
}) | ||
}) |