mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2025-05-21 18:55:19 -04:00
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:
parent
5a56784cd0
commit
13b7854c65
4 changed files with 91 additions and 17 deletions
|
@ -36,6 +36,7 @@
|
||||||
- Asciinema videos may now be embedded by pasting the URL of one video into a single line
|
- Asciinema videos may now be embedded by pasting the URL of one video into a single line
|
||||||
- The Toolbar includes an EmojiPicker
|
- The Toolbar includes an EmojiPicker
|
||||||
- Added shortcodes for [fork-awesome icons](https://forkaweso.me/Fork-Awesome/icons/) (e.g. `:fa-picture-o:`)
|
- Added shortcodes for [fork-awesome icons](https://forkaweso.me/Fork-Awesome/icons/) (e.g. `:fa-picture-o:`)
|
||||||
|
- The code button now adds code fences even if the user selected nothing beforehand
|
||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
|
|
||||||
|
|
|
@ -85,18 +85,48 @@ describe('Toolbar', () => {
|
||||||
.should('have.text', `## ${testText}`)
|
.should('have.text', `## ${testText}`)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('code', () => {
|
describe('code', () => {
|
||||||
cy.get('.CodeMirror textarea')
|
it('nothing selected empty line', () => {
|
||||||
.type(`${testText}`)
|
cy.get('.CodeMirror textarea')
|
||||||
.type('{ctrl}a')
|
.type(`${testText}`)
|
||||||
cy.get('.fa-code')
|
.type('{ctrl}a')
|
||||||
.click()
|
.type('{backspace}')
|
||||||
cy.get('.CodeMirror-code > div:nth-of-type(1) > .CodeMirror-line > span > span')
|
cy.get('.fa-code')
|
||||||
.should('have.text', '```')
|
.click()
|
||||||
cy.get('.CodeMirror-code > div:nth-of-type(2) > .CodeMirror-line > span span')
|
cy.get('.CodeMirror-code > div:nth-of-type(1) > .CodeMirror-line > span > span')
|
||||||
.should('have.text', testText)
|
.should('have.text', '```')
|
||||||
cy.get('.CodeMirror-code > div.CodeMirror-activeline > .CodeMirror-line > span span')
|
cy.get('.CodeMirror-code > div.CodeMirror-activeline > .CodeMirror-line > span span')
|
||||||
.should('have.text', '```')
|
.should('have.text', '```')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('nothing selected non line', () => {
|
||||||
|
cy.get('.CodeMirror textarea')
|
||||||
|
.type(`${testText}`)
|
||||||
|
.type('{ctrl}a')
|
||||||
|
.type('{leftArrow}')
|
||||||
|
cy.get('.fa-code')
|
||||||
|
.click()
|
||||||
|
cy.get('.CodeMirror-code > div:nth-of-type(1) > .CodeMirror-line > span > span')
|
||||||
|
.should('have.text', '```')
|
||||||
|
cy.get('.CodeMirror-code > div:nth-of-type(2) > .CodeMirror-line > span span')
|
||||||
|
.should('have.text', testText)
|
||||||
|
cy.get('.CodeMirror-code > div.CodeMirror-activeline > .CodeMirror-line > span span')
|
||||||
|
.should('have.text', '```')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('line selected', () => {
|
||||||
|
cy.get('.CodeMirror textarea')
|
||||||
|
.type(`${testText}`)
|
||||||
|
.type('{ctrl}a')
|
||||||
|
cy.get('.fa-code')
|
||||||
|
.click()
|
||||||
|
cy.get('.CodeMirror-code > div:nth-of-type(1) > .CodeMirror-line > span > span')
|
||||||
|
.should('have.text', '```')
|
||||||
|
cy.get('.CodeMirror-code > div:nth-of-type(2) > .CodeMirror-line > span span')
|
||||||
|
.should('have.text', testText)
|
||||||
|
cy.get('.CodeMirror-code > div.CodeMirror-activeline > .CodeMirror-line > span span')
|
||||||
|
.should('have.text', '```')
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
it('quote', () => {
|
it('quote', () => {
|
||||||
|
|
|
@ -786,8 +786,9 @@ describe('test addHeaderLevel', () => {
|
||||||
describe('test addCodeFences', () => {
|
describe('test addCodeFences', () => {
|
||||||
const { cursor, firstLine, multiline, multilineOffset } = buildRanges()
|
const { cursor, firstLine, multiline, multilineOffset } = buildRanges()
|
||||||
|
|
||||||
it('just cursor', done => {
|
it('just cursor empty line', done => {
|
||||||
Mock.extend(editor).with({
|
Mock.extend(editor).with({
|
||||||
|
getSelection: () => '',
|
||||||
listSelections: () => (
|
listSelections: () => (
|
||||||
Mock.of<Range[]>([{
|
Mock.of<Range[]>([{
|
||||||
anchor: cursor.from,
|
anchor: cursor.from,
|
||||||
|
@ -796,14 +797,40 @@ describe('test addCodeFences', () => {
|
||||||
to: () => cursor.to,
|
to: () => cursor.to,
|
||||||
empty: () => true
|
empty: () => true
|
||||||
}])
|
}])
|
||||||
)
|
),
|
||||||
|
getLine: (): string => '',
|
||||||
|
replaceRange: (replacement: string | string[]) => {
|
||||||
|
expect(replacement).toEqual('```\n\n```')
|
||||||
|
done()
|
||||||
|
}
|
||||||
})
|
})
|
||||||
strikeThroughSelection(editor)
|
addCodeFences(editor)
|
||||||
done()
|
})
|
||||||
|
|
||||||
|
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 => {
|
it('1st line', done => {
|
||||||
Mock.extend(editor).with({
|
Mock.extend(editor).with({
|
||||||
|
getSelection: () => testContent,
|
||||||
listSelections: () => (
|
listSelections: () => (
|
||||||
Mock.of<Range[]>([{
|
Mock.of<Range[]>([{
|
||||||
anchor: firstLine.from,
|
anchor: firstLine.from,
|
||||||
|
@ -825,6 +852,7 @@ describe('test addCodeFences', () => {
|
||||||
|
|
||||||
it('multiple lines', done => {
|
it('multiple lines', done => {
|
||||||
Mock.extend(editor).with({
|
Mock.extend(editor).with({
|
||||||
|
getSelection: () => testContent,
|
||||||
listSelections: () => (
|
listSelections: () => (
|
||||||
Mock.of<Range[]>([{
|
Mock.of<Range[]>([{
|
||||||
anchor: multiline.from,
|
anchor: multiline.from,
|
||||||
|
@ -846,6 +874,7 @@ describe('test addCodeFences', () => {
|
||||||
|
|
||||||
it('multiple lines with offset', done => {
|
it('multiple lines with offset', done => {
|
||||||
Mock.extend(editor).with({
|
Mock.extend(editor).with({
|
||||||
|
getSelection: () => testContent,
|
||||||
listSelections: () => (
|
listSelections: () => (
|
||||||
Mock.of<Range[]>([{
|
Mock.of<Range[]>([{
|
||||||
anchor: multilineOffset.from,
|
anchor: multilineOffset.from,
|
||||||
|
|
|
@ -11,7 +11,7 @@ export const superscriptSelection = (editor: Editor): void => wrapTextWith(edito
|
||||||
export const markSelection = (editor: Editor): void => wrapTextWith(editor, '==')
|
export const markSelection = (editor: Editor): void => wrapTextWith(editor, '==')
|
||||||
|
|
||||||
export const addHeaderLevel = (editor: Editor): void => changeLines(editor, line => line.startsWith('#') ? `#${line}` : `# ${line}`)
|
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 addQuotes = (editor: Editor): void => insertOnStartOfLines(editor, '> ')
|
||||||
|
|
||||||
export const addList = (editor: Editor): void => createList(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)
|
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 => {
|
export const insertOnStartOfLines = (editor: Editor, symbol: string): void => {
|
||||||
const cursor = editor.getCursor()
|
const cursor = editor.getCursor()
|
||||||
const ranges = editor.listSelections()
|
const ranges = editor.listSelections()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue