-
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 old term with new one instead of cutting on cursor (#164)
- Loading branch information
1 parent
77f977b
commit cf9b2d9
Showing
4 changed files
with
80 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,9 @@ | ||
|
||
[Release 2.3.10-alpha](https://github.com/OpusCapita/react-markdown/releases/tag/v2.3.10-alpha) Mon Aug 19 2019 10:15:17 GMT+0300 (MSK) | ||
======================================================= | ||
|
||
- Replace entire term instead of leaving replaced tail in text [#163](https://github.com/OpusCapita/react-markdown/issues/163) (Egor Stambakio [email protected], 2019-08-19 10:00:58 +0300) | ||
|
||
[Release 2.3.9](https://github.com/OpusCapita/react-markdown/releases/tag/v2.3.9) Mon Oct 29 2018 15:57:36 GMT+0300 (MSK) | ||
======================================================= | ||
|
||
|
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)}`) | ||
}) | ||
}) | ||
}) |