Skip to content
Permalink
Browse files

fix: wrong list type change (close #208) (#209)

* fix: wrong markdown list type change

* fix: wrong wysiwyg list type change

* refactor: apply code review (ref #209)
  • Loading branch information...
kyuwoo-choi committed Jun 15, 2018
1 parent 4f9224f commit b277539412fcb428a9820eea1a053d20c9a8077c
@@ -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]] ).*/;
@@ -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
@@ -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);
}
@@ -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
@@ -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);
}
@@ -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
@@ -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);
}
@@ -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
@@ -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')) {
@@ -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 {
@@ -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')) {
@@ -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 {
@@ -81,4 +83,3 @@ const UL = CommandManager.command('wysiwyg', /** @lends UL */{
});

export default UL;

@@ -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');
});
});
});
@@ -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');
});
});
});
@@ -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');
@@ -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);
});

@@ -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);
});

@@ -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);
});

@@ -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);
});

0 comments on commit b277539

Please sign in to comment.
You can’t perform that action at this time.