Skip to content

Commit

Permalink
fix: wrong list type change (close #208) (#209)
Browse files Browse the repository at this point in the history
* fix: wrong markdown list type change

* fix: wrong wysiwyg list type change

* refactor: apply code review (ref #209)
  • Loading branch information
kyuwoo-choi authored and seonim-ryu committed Jan 2, 2020
1 parent 686eeff commit d399bc7
Show file tree
Hide file tree
Showing 10 changed files with 134 additions and 22 deletions.
4 changes: 4 additions & 0 deletions apps/core/src/js/markdownCommands/listRegex.js
@@ -0,0 +1,4 @@
export const FIND_MD_OL_RX = /^[ \t]*[\d]+\. .*/;
export const FIND_MD_UL_RX = /^[ \t]*[-*] .*/;
export const FIND_MD_TASK_RX = /^[ \t]*([*-] |[\d]+\. )(\[[ xX]] ).*/;
export const FIND_MD_UL_TASK_RX = /^[ \t]*[*-] (\[[ xX]] ).*/;
11 changes: 7 additions & 4 deletions apps/core/src/js/markdownCommands/ol.js
Expand Up @@ -4,10 +4,13 @@
*/

import CommandManager from '../commandManager';
import {
FIND_MD_OL_RX,
FIND_MD_UL_RX,
FIND_MD_TASK_RX
} from './listRegex';

const FIND_MD_OL_RX = /^[ \t]*[\d]+\. .*/;
const FIND_MD_UL_RX = /^[ \t]*[-*] .*/;
const FIND_MD_TASK_RX = /^[ \t]*[-*]( \[[ xX]])? .*/;
const MD_LIST_OR_TASK_SYNTAX_RX = /([-*]|[\d]+\.)( \[[ xX]])? /;

/**
* OL
Expand Down Expand Up @@ -44,7 +47,7 @@ const OL = CommandManager.command('markdown', /** @lends OL */{

if (listManager.isListOrParagraph(line)) {
if (isUlOrTask(line)) {
listManager.replaceLineText(doc, i, /[*-] /, `${ordinalNumber}. `);
listManager.replaceLineText(doc, i, MD_LIST_OR_TASK_SYNTAX_RX, `${ordinalNumber}. `);
} else if (!line.match(FIND_MD_OL_RX)) {
doc.replaceRange(`${ordinalNumber}. `, currentLineStart);
}
Expand Down
17 changes: 10 additions & 7 deletions apps/core/src/js/markdownCommands/task.js
Expand Up @@ -3,11 +3,14 @@
* @author NHN Ent. FE Development Lab <dl_javascript@nhnent.com>
*/
import CommandManager from '../commandManager';
import {
FIND_MD_OL_RX,
FIND_MD_UL_RX,
FIND_MD_TASK_RX
} from './listRegex';

const FIND_MD_OL_RX = /^[ \t]*[\d]+\. .*/;
const FIND_MD_UL_RX = /^[ \t]*[-*] .*/;
const FIND_MD_TASK_RX = /^[ \t]*[-*]( \[[ xX]])? .*/;
const FIND_TASK_SYNTAX_RX = /([*-] |[\d]+\. )(\[[ xX]] )/;
const MD_UL_OR_OL_SYNTAX_RX = /([*-] |[\d]+\. )/;
const MD_TASK_SYNTAX_RX = /([*-] |[\d]+\. )(\[[ xX]] )/;

/**
* Task
Expand Down Expand Up @@ -40,13 +43,13 @@ const Task = CommandManager.command('markdown', /** @lends Task */{

line = doc.getLine(i);

const hasTaskSyntax = !!line.match(FIND_TASK_SYNTAX_RX);
const hasTaskSyntax = !!line.match(MD_TASK_SYNTAX_RX);

if (listManager.isListOrParagraph(line)) {
if (isOlOrUl(line) && hasTaskSyntax) {
listManager.replaceLineText(doc, i, FIND_TASK_SYNTAX_RX, '$1');
listManager.replaceLineText(doc, i, MD_TASK_SYNTAX_RX, '$1');
} else if (isOlOrUl(line) && !hasTaskSyntax) {
listManager.replaceLineText(doc, i, /([*-] |[\d]+\. )/, '$1[ ] ');
listManager.replaceLineText(doc, i, MD_UL_OR_OL_SYNTAX_RX, '$1[ ] ');
} else if (!line.match(FIND_MD_TASK_RX)) {
doc.replaceRange('* [ ] ', currentLineStart);
}
Expand Down
26 changes: 21 additions & 5 deletions apps/core/src/js/markdownCommands/ul.js
Expand Up @@ -3,10 +3,15 @@
* @author NHN Ent. FE Development Lab <dl_javascript@nhnent.com>
*/
import CommandManager from '../commandManager';
import {
FIND_MD_OL_RX,
FIND_MD_UL_RX,
FIND_MD_TASK_RX,
FIND_MD_UL_TASK_RX
} from './listRegex';

const FIND_MD_OL_RX = /^[ \t]*[\d]+\. .*/;
const FIND_MD_UL_RX = /^[ \t]*[-*] .*/;
const FIND_MD_TASK_RX = /^[ \t]*[-*]( \[[ xX]])? .*/;
const MD_UL_TASK_SYNTAX_RX = /([-*])( \[[ xX]]) /;
const MD_UL_OR_UL_TASK_SYNTAX_RX = /[\d]+\.( \[[ xX]])? /;

/**
* UL
Expand Down Expand Up @@ -41,8 +46,10 @@ const UL = CommandManager.command('markdown', /** @lends UL */{
line = doc.getLine(i);

if (listManager.isListOrParagraph(line)) {
if (isOlOrTask(line)) {
listManager.replaceLineText(doc, i, /[\d]+\. /, '* ');
if (isUlTask(line)) {
listManager.replaceLineText(doc, i, MD_UL_TASK_SYNTAX_RX, '$1 ');
} else if (isOlOrTask(line)) {
listManager.replaceLineText(doc, i, MD_UL_OR_UL_TASK_SYNTAX_RX, '* ');
} else if (!line.match(FIND_MD_UL_RX)) {
doc.replaceRange('* ', currentLineStart);
}
Expand All @@ -58,6 +65,15 @@ const UL = CommandManager.command('markdown', /** @lends UL */{
}
});

/**
* Return whether the given line is UL TASK
* @param {string} line Line text
* @returns {boolean}
*/
function isUlTask(line) {
return !!(line && line.match(FIND_MD_UL_TASK_RX));
}

/**
* Return whether passed line is OL or TASK or neither
* @param {string} line Line text
Expand Down
2 changes: 2 additions & 0 deletions apps/core/src/js/wysiwygCommands/ol.js
Expand Up @@ -58,6 +58,7 @@ const OL = CommandManager.command('wysiwyg', /** @lends OL */{
_changeFormatToOrderedListIfNeed(wwe, target) {
const sq = wwe.getEditor();
const range = sq.getSelection();
const taskManager = wwe.componentManager.getManager('task');
let newLI = range.startContainer;

if (!sq.hasFormat('TABLE') && !sq.hasFormat('PRE')) {
Expand All @@ -67,6 +68,7 @@ const OL = CommandManager.command('wysiwyg', /** @lends OL */{

if (sq.hasFormat('LI')) {
wwe.saveSelection(range);
taskManager.unformatTask(range.startContainer);
sq.replaceParent(range.startContainer, 'ul', 'ol');
wwe.restoreSavedSelection();
} else {
Expand Down
3 changes: 2 additions & 1 deletion apps/core/src/js/wysiwygCommands/ul.js
Expand Up @@ -57,6 +57,7 @@ const UL = CommandManager.command('wysiwyg', /** @lends UL */{
_changeFormatToUnorderedListIfNeed(wwe, target) {
const sq = wwe.getEditor();
const range = sq.getSelection();
const taskManager = wwe.componentManager.getManager('task');
let newLI = range.startContainer;

if (!sq.hasFormat('TABLE') && !sq.hasFormat('PRE')) {
Expand All @@ -66,6 +67,7 @@ const UL = CommandManager.command('wysiwyg', /** @lends UL */{

if (sq.hasFormat('LI')) {
wwe.saveSelection(range);
taskManager.unformatTask(range.startContainer);
sq.replaceParent(range.startContainer, 'ol', 'ul');
wwe.restoreSavedSelection();
} else {
Expand All @@ -81,4 +83,3 @@ const UL = CommandManager.command('wysiwyg', /** @lends UL */{
});

export default UL;

37 changes: 37 additions & 0 deletions apps/core/test/unit/markdownCommands/ol.spec.js
Expand Up @@ -245,5 +245,42 @@ describe('OL', () => {
expect(doc.getLine(5)).toEqual('mytext4');
expect(doc.getLine(6)).toEqual('# myheading');
});

it('should remove task bracket of a line at the caret position', () => {
cm.setValue('* [ ] a task');

doc.setCursor(1, 0);
OL.exec(mde);

expect(doc.getLine(0)).toEqual('1. a task');
});

it('should remove task bracket of a OL task line at the caret position', () => {
cm.setValue('1. [ ] a task');

doc.setCursor(1, 0);
OL.exec(mde);

expect(doc.getLine(0)).toEqual('1. a task');
});

it('should remove task bracket of selected lines', () => {
cm.setValue([
'1. [ ] a task',
'* [ ] another task'
].join('\n'));

doc.setSelection({
line: 0,
ch: 0
}, {
line: 1,
ch: 0
});
OL.exec(mde);

expect(doc.getLine(0)).toEqual('1. a task');
expect(doc.getLine(1)).toEqual('2. another task');
});
});
});
46 changes: 46 additions & 0 deletions apps/core/test/unit/markdownCommands/ul.spec.js
Expand Up @@ -256,5 +256,51 @@ describe('UL', () => {
expect(doc.getLine(5)).toEqual('mytext4');
expect(doc.getLine(6)).toEqual('# myheading');
});

it('should remove task bracket of a line at the caret position', () => {
cm.setValue('* [ ] a task');

doc.setCursor(1, 0);
UL.exec(mde);

expect(doc.getLine(0)).toEqual('* a task');
});

it('should preserve the original list mark', () => {
cm.setValue('- [ ] a task');

doc.setCursor(1, 0);
UL.exec(mde);

expect(doc.getLine(0)).toEqual('- a task');
});

it('should remove task bracket and change to ul of a line at the caret position', () => {
cm.setValue('1. [ ] a task');

doc.setCursor(1, 0);
UL.exec(mde);

expect(doc.getLine(0)).toEqual('* a task');
});

it('should remove task bracket of selected lines', () => {
cm.setValue([
'* [ ] a task',
'1. [ ] another task'
].join('\n'));

doc.setSelection({
line: 0,
ch: 0
}, {
line: 1,
ch: 0
});
UL.exec(mde);

expect(doc.getLine(0)).toEqual('* a task');
expect(doc.getLine(1)).toEqual('* another task');
});
});
});
6 changes: 3 additions & 3 deletions apps/core/test/unit/wysiwygCommands/ol.spec.js
Expand Up @@ -59,7 +59,7 @@ describe('OL', () => {
OL.exec(wwe);

expect(wwe.get$Body().find('ul').length).toEqual(0);
expect(wwe.get$Body().find('ol li.task-list-item[data-te-task]').length).toEqual(1);
expect(wwe.get$Body().find('ol li.task-list-item[data-te-task]').length).toEqual(0);
expect(wwe.get$Body().find('ol').length).toEqual(1);
expect(wwe.get$Body().find('li').length).toEqual(1);
expect(wwe.get$Body().find('li').text()).toEqual('test');
Expand Down Expand Up @@ -187,7 +187,7 @@ describe('OL', () => {
OL.exec(wwe);

expect(wwe.get$Body().find('ol').length).toEqual(1);
expect(wwe.get$Body().find('li.task-list-item').length).toEqual(1);
expect(wwe.get$Body().find('li.task-list-item').length).toEqual(0);
expect(wwe.get$Body().find('li').length).toEqual(1);
});

Expand All @@ -206,7 +206,7 @@ describe('OL', () => {
OL.exec(wwe);

expect(wwe.get$Body().find('ol').length).toEqual(1);
expect(wwe.get$Body().find('li.task-list-item').length).toEqual(2);
expect(wwe.get$Body().find('li.task-list-item').length).toEqual(0);
expect(wwe.get$Body().find('li').length).toEqual(2);
});

Expand Down
4 changes: 2 additions & 2 deletions apps/core/test/unit/wysiwygCommands/ul.spec.js
Expand Up @@ -169,7 +169,7 @@ describe('UL', () => {

expect(wwe.get$Body().find('ol').length).toEqual(0);
expect(wwe.get$Body().find('ul').length).toEqual(1);
expect(wwe.get$Body().find('li.task-list-item').length).toEqual(1);
expect(wwe.get$Body().find('li.task-list-item').length).toEqual(0);
expect(wwe.get$Body().find('li').length).toEqual(1);
});

Expand All @@ -188,7 +188,7 @@ describe('UL', () => {
UL.exec(wwe);

expect(wwe.get$Body().find('ul').length).toEqual(1);
expect(wwe.get$Body().find('li.task-list-item').length).toEqual(2);
expect(wwe.get$Body().find('li.task-list-item').length).toEqual(0);
expect(wwe.get$Body().find('li').length).toEqual(2);
});

Expand Down

0 comments on commit d399bc7

Please sign in to comment.