Skip to content

Commit f3c7e6d

Browse files
committed
[IMP] html_editor: automatic checklist detection
Purpose: This commit implement the automatic checklist detection feature. User can now easily generate checklist by typing '[]' followed by a space. task-4002195 closes odoo#174999 Signed-off-by: David Monjoie (dmo) <dmo@odoo.com>
1 parent 4fef6ae commit f3c7e6d

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

addons/html_editor/static/src/main/list/list_plugin.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,9 @@ export class ListPlugin extends Plugin {
144144
const stringToConvert = blockEl.textContent.substring(0, selection.anchorOffset);
145145
const shouldCreateNumberList = /^(?:[1aA])[.)]\s$/.test(stringToConvert);
146146
const shouldCreateBulletList = /^[-*]\s$/.test(stringToConvert);
147+
const shouldCreateCheckList = /^\[\]\s$/.test(stringToConvert);
147148
if (
148-
(shouldCreateNumberList || shouldCreateBulletList) &&
149+
(shouldCreateNumberList || shouldCreateBulletList || shouldCreateCheckList) &&
149150
!closestElement(selection.anchorNode, "li")
150151
) {
151152
this.shared.setSelection({
@@ -173,6 +174,8 @@ export class ListPlugin extends Plugin {
173174
}
174175
} else if (shouldCreateBulletList) {
175176
this.toggleList("UL");
177+
} else if (shouldCreateCheckList) {
178+
this.toggleList("CL");
176179
}
177180
this.dispatch("ADD_STEP");
178181
}

addons/html_editor/static/tests/list/automatic_list_detection.test.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,34 @@ test("should convert a bullet list into a numbered list", async () => {
143143
expect(getContent(el)).toBe(`<ol><li placeholder="List" class="o-we-hint">[]<br></li></ol>`);
144144
});
145145

146+
test("typing '[] ' should create checklist and restore the original text when undo", async () => {
147+
const { el, editor } = await setupEditor("<p>[]</p>");
148+
insertText(editor, "[] ");
149+
expect(getContent(el)).toBe(
150+
`<ul class="o_checklist"><li placeholder="List" class="o-we-hint">[]<br></li></ul>`
151+
);
152+
153+
editor.dispatch("HISTORY_UNDO");
154+
expect(getContent(el)).toBe(`<p>\[\] []</p>`);
155+
});
156+
157+
test("Typing '[] ' at the start of existing text should create a checklist and restore the original text when undo", async () => {
158+
const { el, editor } = await setupEditor("<p>[]abc</p>");
159+
insertText(editor, "[] ");
160+
expect(getContent(el)).toBe(`<ul class="o_checklist"><li>[]abc</li></ul>`);
161+
162+
editor.dispatch("HISTORY_UNDO");
163+
expect(getContent(el)).toBe(`<p>\[\] []abc</p>`);
164+
});
165+
166+
test("should convert a checklist into a numbered list", async () => {
167+
const { el, editor } = await setupEditor("<p>[]</p>");
168+
insertText(editor, "[] ");
169+
insertText(editor, "/numberedlist");
170+
press("Enter");
171+
expect(getContent(el)).toBe(`<ol><li placeholder="List" class="o-we-hint">[]<br></li></ol>`);
172+
});
173+
146174
test("List should not be created when typing '1. ' at the end the text", async () => {
147175
const { el, editor } = await setupEditor("<p>abc[]</p>");
148176
insertText(editor, "1. ");

0 commit comments

Comments
 (0)