made the addCodeFences function also work without selections (#428)

* made the addCodeFences function also work without selections
changed tests accordingly

* add codeFence change in CHANGELOG.md
This commit is contained in:
Philip Molares 2020-08-19 21:26:22 +02:00 committed by GitHub
parent 5a56784cd0
commit 13b7854c65
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 91 additions and 17 deletions

View file

@ -786,8 +786,9 @@ describe('test addHeaderLevel', () => {
describe('test addCodeFences', () => {
const { cursor, firstLine, multiline, multilineOffset } = buildRanges()
it('just cursor', done => {
it('just cursor empty line', done => {
Mock.extend(editor).with({
getSelection: () => '',
listSelections: () => (
Mock.of<Range[]>([{
anchor: cursor.from,
@ -796,14 +797,40 @@ describe('test addCodeFences', () => {
to: () => cursor.to,
empty: () => true
}])
)
),
getLine: (): string => '',
replaceRange: (replacement: string | string[]) => {
expect(replacement).toEqual('```\n\n```')
done()
}
})
strikeThroughSelection(editor)
done()
addCodeFences(editor)
})
it('just cursor nonempty line', done => {
Mock.extend(editor).with({
getSelection: () => '',
listSelections: () => (
Mock.of<Range[]>([{
anchor: cursor.from,
head: cursor.to,
from: () => cursor.from,
to: () => cursor.to,
empty: () => true
}])
),
getLine: (): string => '1st line',
replaceRange: (replacement: string | string[]) => {
expect(replacement).toEqual('```\n1st line\n```')
done()
}
})
addCodeFences(editor)
})
it('1st line', done => {
Mock.extend(editor).with({
getSelection: () => testContent,
listSelections: () => (
Mock.of<Range[]>([{
anchor: firstLine.from,
@ -825,6 +852,7 @@ describe('test addCodeFences', () => {
it('multiple lines', done => {
Mock.extend(editor).with({
getSelection: () => testContent,
listSelections: () => (
Mock.of<Range[]>([{
anchor: multiline.from,
@ -846,6 +874,7 @@ describe('test addCodeFences', () => {
it('multiple lines with offset', done => {
Mock.extend(editor).with({
getSelection: () => testContent,
listSelections: () => (
Mock.of<Range[]>([{
anchor: multilineOffset.from,

View file

@ -11,7 +11,7 @@ export const superscriptSelection = (editor: Editor): void => wrapTextWith(edito
export const markSelection = (editor: Editor): void => wrapTextWith(editor, '==')
export const addHeaderLevel = (editor: Editor): void => changeLines(editor, line => line.startsWith('#') ? `#${line}` : `# ${line}`)
export const addCodeFences = (editor: Editor): void => wrapTextWith(editor, '```\n', '\n```')
export const addCodeFences = (editor: Editor): void => wrapTextWithOrJustPut(editor, '```\n', '\n```')
export const addQuotes = (editor: Editor): void => insertOnStartOfLines(editor, '> ')
export const addList = (editor: Editor): void => createList(editor, () => '- ')
@ -48,6 +48,20 @@ export const wrapTextWith = (editor: Editor, symbol: string, endSymbol?: string)
editor.setSelections(ranges)
}
const wrapTextWithOrJustPut = (editor: Editor, symbol: string, endSymbol?: string): void => {
if (!editor.getSelection()) {
const cursor = editor.getCursor()
const lineNumber = cursor.line
const line = editor.getLine(lineNumber)
const replacement = /\s*\\n/.exec(line) ? `${symbol}${endSymbol ?? ''}` : `${symbol}${line}${endSymbol ?? ''}`
editor.replaceRange(replacement,
{ line: cursor.line, ch: 0 },
{ line: cursor.line, ch: line.length },
'+input')
}
wrapTextWith(editor, symbol, endSymbol ?? symbol)
}
export const insertOnStartOfLines = (editor: Editor, symbol: string): void => {
const cursor = editor.getCursor()
const ranges = editor.listSelections()