Skip to content

Commit 948c00e

Browse files
authored
Merge branch 'master' into fix/remove-babel-polyfill
2 parents 9c51c8e + f356ee9 commit 948c00e

File tree

5 files changed

+66
-4
lines changed

5 files changed

+66
-4
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,16 @@
66

77
![alt text](https://raw.githubusercontent.com/webscopeio/react-markdown-editor/master/static/demo.gif)
88

9+
## Warning
10+
This is a work in progress. It's not well tested yet, please do not use this in production yet.
11+
12+
## Demo
13+
https://webscope-react-markdown-editor.firebaseapp.com/
14+
915
## Install
1016

1117
```bash
12-
npm install --save @webscopeio/react-markdown-editor
18+
yarn add @webscopeio/react-markdown-editor
1319
```
1420

1521
## Usage

_config.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
theme: jekyll-theme-minimal

docs/_config.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
theme: jekyll-theme-minimal

src/helpers.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// @flow
22

3-
const isEmptyAtPosition = (word: string, position: number) => word[position] === ' ' || word[position] === '\n'
3+
const isEmptyAtPosition = (word: string, position: number) => word[position] === ' '
44

55
export const getWordStartAndEndLocation = (word: string, position: number) => {
66
if (isEmptyAtPosition(word, position) && isEmptyAtPosition(word, position - 1)) {
@@ -19,7 +19,7 @@ export const getWordStartAndEndLocation = (word: string, position: number) => {
1919
]
2020
}
2121

22-
export const insertSymbol = (word: string, position: number, [prefix, suffix]: Array<string>) => {
22+
const insertSymbolInWord = (word: string, position: number, [prefix, suffix]: Array<string>) => {
2323
const [start, end] = getWordStartAndEndLocation(word, position)
2424
const startWord = word.slice(0, start)
2525
const actualWord = word.slice(start, end)
@@ -33,3 +33,22 @@ export const insertSymbol = (word: string, position: number, [prefix, suffix]: A
3333
return [`${startWord}${prefix}${actualWord}${suffix}${endWord}`, true]
3434
}
3535

36+
// reduce helper
37+
function sum(total, num) {
38+
return total + num
39+
}
40+
41+
export const insertSymbol = (text: string, position: number, [prefix, suffix]: Array<string>) => {
42+
const lines = text.split('\n')
43+
const lineLengths = lines.map(line => line.length)
44+
45+
const currentLine = text.substring(0, position).split('\n').length - 1
46+
47+
const relativeLinePosition: number = position - lineLengths.slice(0, currentLine).reduce(sum, 0)
48+
const [newLine, replaced] =
49+
insertSymbolInWord(lines[currentLine], relativeLinePosition, [prefix, suffix])
50+
51+
lines[currentLine] = newLine
52+
return [lines.join('\n'), replaced]
53+
}
54+

src/helpers.test.js

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ describe('Markdown replace functions', () => {
3737
expect(insertSymbol(text, 14, ['_', '_'])).toEqual(['Hello _Webscope_', true])
3838
})
3939

40-
it('multiline comment', () => {
40+
it('multiline comment - cursor is in an empty line', () => {
4141
const text =
4242
`This is a multiline
4343
comment
@@ -50,8 +50,43 @@ comment
5050
WOW`, true])
5151
})
5252

53+
it('multiline comment - cursor is at the word', () => {
54+
const text =
55+
`This is a multiline
56+
comment
57+
58+
WOW`
59+
60+
expect(insertSymbol(text, 30, ['_', '_'])).toEqual([`This is a multiline
61+
comment
62+
63+
_WOW_`, true])
64+
})
65+
5366
it('word is already wrapped, insertSymbol is invoked with this symbol, symbol is removed', () => {
5467
const text = '**Hello** world'
5568
expect(insertSymbol(text, 1, ['**', '**'])).toEqual(['Hello world', false])
5669
})
70+
71+
it('empty string is wrapped with a **** symbol, insertSymbol is invoked with the symbol, **** disappear', () => {
72+
const text = `**A**
73+
74+
****
75+
76+
AHOJ **B**`
77+
expect(insertSymbol(text, 9, ['**', '**'])).toEqual([`**A**
78+
79+
80+
81+
AHOJ **B**`, false])
82+
})
83+
84+
it('there are multiple lines, word in a middle of those lines should be wrapped if cursor is at that word', () => {
85+
const text = `first
86+
hello world
87+
last`
88+
expect(insertSymbol(text, 9, ['**', '**'])).toEqual([`first
89+
**hello** world
90+
last`, true])
91+
})
5792
})

0 commit comments

Comments
 (0)