-
-
Notifications
You must be signed in to change notification settings - Fork 5.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Desktop: Resolves #4813 : skip empty lines while converting selection to list #4832
Merged
Merged
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2e68c00
unexpected insert- list behaviour fixed
adarsh-sgh 80004ad
tests added
adarsh-sgh bc969bb
only skip last empty line
adarsh-sgh 00d5b1f
refactored
adarsh-sgh 3055603
a console logging removed
adarsh-sgh f50d9d0
Update useCursorUtils.test.ts
laurent22 5a358c3
Update useCursorUtils.test.ts
laurent22 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
21 changes: 21 additions & 0 deletions
21
packages/app-desktop/gui/NoteEditor/NoteBody/CodeMirror/utils/useCursorUtils.test.ts
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,21 @@ | ||
import { modifyListLines } from './useCursorUtils'; | ||
|
||
describe('Check list item modification', () => { | ||
const num = 0; | ||
let lineInitial = `- item1 | ||
- item2 | ||
- item3`; | ||
let lineFinal = `item1 | ||
item2 | ||
item3`; | ||
test('should remove "- " from beggining of each line of input string', () => { | ||
expect(JSON.stringify(modifyListLines(lineInitial.split('\n'), num, '- '))).toBe(JSON.stringify(lineFinal.split('\n'))); | ||
}); | ||
test('should add "- " at the beggining of each line of the input string', () => { | ||
// swap input and expected output | ||
const temp = lineInitial; | ||
lineInitial = lineFinal; | ||
lineFinal = temp; | ||
expect(JSON.stringify(modifyListLines(lineInitial.split('\n'), num, '- '))).toBe(JSON.stringify(lineFinal.split('\n'))); | ||
}); | ||
}); |
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,6 +1,24 @@ | ||
import markdownUtils from '@joplin/lib/markdownUtils'; | ||
import Setting from '@joplin/lib/models/Setting'; | ||
|
||
export function modifyListLines(lines: string[],num: number,string1: string) { | ||
for (let j = 0; j < lines.length; j++) { | ||
const line = lines[j]; | ||
if (!line && j == lines.length - 1) continue; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. strict equality |
||
// Only add the list token if it's not already there | ||
// if it is, remove it | ||
if (!line.startsWith(string1)) { | ||
if (num) { | ||
lines[j] = `${num.toString()}. ${line}`; | ||
num++; | ||
} else { | ||
lines[j] = string1 + line; | ||
} | ||
} else { | ||
lines[j] = line.substr(string1.length, line.length - string1.length); | ||
} | ||
} | ||
return lines; | ||
} | ||
// Helper functions that use the cursor | ||
export default function useCursorUtils(CodeMirror: any) { | ||
|
||
|
@@ -81,28 +99,12 @@ export default function useCursorUtils(CodeMirror: any) { | |
for (let i = 0; i < selectedStrings.length; i++) { | ||
const selected = selectedStrings[i]; | ||
|
||
let num = markdownUtils.olLineNumber(string1); | ||
const num = markdownUtils.olLineNumber(string1); | ||
|
||
const lines = selected.split(/\r?\n/); | ||
// Save the newline character to restore it later | ||
const newLines = selected.match(/\r?\n/); | ||
|
||
for (let j = 0; j < lines.length; j++) { | ||
const line = lines[j]; | ||
// Only add the list token if it's not already there | ||
// if it is, remove it | ||
if (!line.startsWith(string1)) { | ||
if (num) { | ||
lines[j] = `${num.toString()}. ${line}`; | ||
num++; | ||
} else { | ||
lines[j] = string1 + line; | ||
} | ||
} else { | ||
lines[j] = line.substr(string1.length, line.length - string1.length); | ||
} | ||
} | ||
|
||
modifyListLines(lines,num,string1); | ||
const newLine = newLines !== null ? newLines[0] : '\n'; | ||
selectedStrings[i] = lines.join(newLine); | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please could you give a descriptive name to "string1"?