mirror of
https://github.com/ueberdosis/tiptap.git
synced 2025-06-07 09:25:29 +08:00
Add TaskList and TaskItem demo for React
This commit is contained in:
parent
ddd5545e9a
commit
f2e6d3d8ef
15
demos/src/Nodes/TaskItem/React/index.html
Normal file
15
demos/src/Nodes/TaskItem/React/index.html
Normal file
@ -0,0 +1,15 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
</head>
|
||||
<body>
|
||||
<div id="app"></div>
|
||||
<script type="module">
|
||||
import setup from "../../../../setup/react.ts";
|
||||
import source from "@source";
|
||||
setup("Nodes/TaskItem", source);
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
63
demos/src/Nodes/TaskItem/React/index.jsx
Normal file
63
demos/src/Nodes/TaskItem/React/index.jsx
Normal file
@ -0,0 +1,63 @@
|
||||
import React from 'react'
|
||||
import { useEditor, EditorContent } from '@tiptap/react'
|
||||
import Document from '@tiptap/extension-document'
|
||||
import Paragraph from '@tiptap/extension-paragraph'
|
||||
import Text from '@tiptap/extension-text'
|
||||
import TaskList from '@tiptap/extension-task-list'
|
||||
import TaskItem from '@tiptap/extension-task-item'
|
||||
import './styles.scss'
|
||||
|
||||
export default () => {
|
||||
const editor = useEditor({
|
||||
extensions: [
|
||||
Document,
|
||||
Paragraph,
|
||||
Text,
|
||||
TaskList,
|
||||
TaskItem.configure({
|
||||
nested: true,
|
||||
}),
|
||||
],
|
||||
content: `
|
||||
<ul data-type="taskList">
|
||||
<li data-type="taskItem" data-checked="true">A list item</li>
|
||||
<li data-type="taskItem" data-checked="false">And another one</li>
|
||||
</ul>
|
||||
`,
|
||||
})
|
||||
|
||||
if (!editor) {
|
||||
return null
|
||||
}
|
||||
|
||||
return (
|
||||
<>
|
||||
<button
|
||||
onClick={() => editor.chain().focus().toggleTaskList().run()}
|
||||
className={editor.isActive('taskList') ? 'is-active' : ''}
|
||||
>
|
||||
toggleTaskList
|
||||
</button>
|
||||
<button
|
||||
onClick={() => editor.chain().focus().splitListItem('taskItem').run()}
|
||||
disabled={!editor.can().splitListItem('taskItem')}
|
||||
>
|
||||
splitListItem
|
||||
</button>
|
||||
<button
|
||||
onClick={() => editor.chain().focus().sinkListItem('taskItem').run()}
|
||||
disabled={!editor.can().sinkListItem('taskItem')}
|
||||
>
|
||||
sinkListItem
|
||||
</button>
|
||||
<button
|
||||
onClick={() => editor.chain().focus().liftListItem('taskItem').run()}
|
||||
disabled={!editor.can().liftListItem('taskItem')}
|
||||
>
|
||||
liftListItem
|
||||
</button>
|
||||
|
||||
<EditorContent editor={editor} />
|
||||
</>
|
||||
)
|
||||
}
|
7
demos/src/Nodes/TaskItem/React/index.spec.js
Normal file
7
demos/src/Nodes/TaskItem/React/index.spec.js
Normal file
@ -0,0 +1,7 @@
|
||||
context('/src/Nodes/TaskItem/React/', () => {
|
||||
before(() => {
|
||||
cy.visit('/src/Nodes/TaskItem/React/')
|
||||
})
|
||||
|
||||
// TODO: Write tests
|
||||
})
|
22
demos/src/Nodes/TaskItem/React/styles.scss
Normal file
22
demos/src/Nodes/TaskItem/React/styles.scss
Normal file
@ -0,0 +1,22 @@
|
||||
ul[data-type="taskList"] {
|
||||
list-style: none;
|
||||
padding: 0;
|
||||
|
||||
p {
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
li {
|
||||
display: flex;
|
||||
|
||||
> label {
|
||||
flex: 0 0 auto;
|
||||
margin-right: 0.5rem;
|
||||
user-select: none;
|
||||
}
|
||||
|
||||
> div {
|
||||
flex: 1 1 auto;
|
||||
}
|
||||
}
|
||||
}
|
15
demos/src/Nodes/TaskList/React/index.html
Normal file
15
demos/src/Nodes/TaskList/React/index.html
Normal file
@ -0,0 +1,15 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
</head>
|
||||
<body>
|
||||
<div id="app"></div>
|
||||
<script type="module">
|
||||
import setup from "../../../../setup/react.ts";
|
||||
import source from "@source";
|
||||
setup("Nodes/TaskList", source);
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
63
demos/src/Nodes/TaskList/React/index.jsx
Normal file
63
demos/src/Nodes/TaskList/React/index.jsx
Normal file
@ -0,0 +1,63 @@
|
||||
import React from 'react'
|
||||
import { useEditor, EditorContent } from '@tiptap/react'
|
||||
import Document from '@tiptap/extension-document'
|
||||
import Paragraph from '@tiptap/extension-paragraph'
|
||||
import Text from '@tiptap/extension-text'
|
||||
import TaskList from '@tiptap/extension-task-list'
|
||||
import TaskItem from '@tiptap/extension-task-item'
|
||||
import './styles.scss'
|
||||
|
||||
export default () => {
|
||||
const editor = useEditor({
|
||||
extensions: [
|
||||
Document,
|
||||
Paragraph,
|
||||
Text,
|
||||
TaskList,
|
||||
TaskItem.configure({
|
||||
nested: true,
|
||||
}),
|
||||
],
|
||||
content: `
|
||||
<ul data-type="taskList">
|
||||
<li data-type="taskItem" data-checked="true">A list item</li>
|
||||
<li data-type="taskItem" data-checked="false">And another one</li>
|
||||
</ul>
|
||||
`,
|
||||
})
|
||||
|
||||
if (!editor) {
|
||||
return null
|
||||
}
|
||||
|
||||
return (
|
||||
<>
|
||||
<button
|
||||
onClick={() => editor.chain().focus().toggleTaskList().run()}
|
||||
className={editor.isActive('taskList') ? 'is-active' : ''}
|
||||
>
|
||||
toggleTaskList
|
||||
</button>
|
||||
<button
|
||||
onClick={() => editor.chain().focus().splitListItem('taskItem').run()}
|
||||
disabled={!editor.can().splitListItem('taskItem')}
|
||||
>
|
||||
splitListItem
|
||||
</button>
|
||||
<button
|
||||
onClick={() => editor.chain().focus().sinkListItem('taskItem').run()}
|
||||
disabled={!editor.can().sinkListItem('taskItem')}
|
||||
>
|
||||
sinkListItem
|
||||
</button>
|
||||
<button
|
||||
onClick={() => editor.chain().focus().liftListItem('taskItem').run()}
|
||||
disabled={!editor.can().liftListItem('taskItem')}
|
||||
>
|
||||
liftListItem
|
||||
</button>
|
||||
|
||||
<EditorContent editor={editor} />
|
||||
</>
|
||||
)
|
||||
}
|
113
demos/src/Nodes/TaskList/React/index.spec.js
Normal file
113
demos/src/Nodes/TaskList/React/index.spec.js
Normal file
@ -0,0 +1,113 @@
|
||||
context('/src/Nodes/TaskList/React/', () => {
|
||||
before(() => {
|
||||
cy.visit('/src/Nodes/TaskList/React/')
|
||||
})
|
||||
|
||||
beforeEach(() => {
|
||||
cy.get('.ProseMirror').then(([{ editor }]) => {
|
||||
editor.commands.setContent('<p>Example Text</p>')
|
||||
cy.get('.ProseMirror').type('{selectall}')
|
||||
})
|
||||
})
|
||||
|
||||
it('should parse unordered lists correctly', () => {
|
||||
cy.get('.ProseMirror').then(([{ editor }]) => {
|
||||
editor.commands.setContent(
|
||||
'<ul data-type="taskList"><li data-checked="true" data-type="taskItem"><p>Example Text</p></li></ul>',
|
||||
)
|
||||
expect(editor.getHTML()).to.eq(
|
||||
'<ul data-type="taskList"><li data-checked="true" data-type="taskItem"><label><input type="checkbox" checked="checked"><span></span></label><div><p>Example Text</p></div></li></ul>',
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
it('should parse unordered lists without paragraphs correctly', () => {
|
||||
cy.get('.ProseMirror').then(([{ editor }]) => {
|
||||
editor.commands.setContent(
|
||||
'<ul data-type="taskList"><li data-checked="false" data-type="taskItem">Example Text</li></ul>',
|
||||
)
|
||||
expect(editor.getHTML()).to.eq(
|
||||
'<ul data-type="taskList"><li data-checked="false" data-type="taskItem"><label><input type="checkbox"><span></span></label><div><p>Example Text</p></div></li></ul>',
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
it('the button should make the selected line a task list item', () => {
|
||||
cy.get('.ProseMirror ul').should('not.exist')
|
||||
|
||||
cy.get('.ProseMirror ul li').should('not.exist')
|
||||
|
||||
cy.get('button:nth-child(1)').click()
|
||||
|
||||
cy.get('.ProseMirror').find('ul[data-type="taskList"]').should('contain', 'Example Text')
|
||||
|
||||
cy.get('.ProseMirror').find('ul[data-type="taskList"] li').should('contain', 'Example Text')
|
||||
})
|
||||
|
||||
it('the button should toggle the task list', () => {
|
||||
cy.get('.ProseMirror ul').should('not.exist')
|
||||
|
||||
cy.get('button:nth-child(1)').click()
|
||||
|
||||
cy.get('.ProseMirror').find('ul[data-type="taskList"]').should('contain', 'Example Text')
|
||||
|
||||
cy.get('button:nth-child(1)').click()
|
||||
|
||||
cy.get('.ProseMirror ul').should('not.exist')
|
||||
})
|
||||
|
||||
it('should make the paragraph a task list when the keyboard shortcut is pressed', () => {
|
||||
cy.get('.ProseMirror')
|
||||
.trigger('keydown', { modKey: true, shiftKey: true, key: '9' })
|
||||
.find('ul li')
|
||||
.should('contain', 'Example Text')
|
||||
})
|
||||
|
||||
it('should leave the list with double enter', () => {
|
||||
cy.get('.ProseMirror').then(([{ editor }]) => {
|
||||
editor.commands.clearContent()
|
||||
})
|
||||
|
||||
cy.get('.ProseMirror').type('[ ] List Item 1{enter}{enter}Paragraph')
|
||||
|
||||
cy.get('.ProseMirror').find('li').its('length').should('eq', 1)
|
||||
|
||||
cy.get('.ProseMirror').find('p').should('contain', 'Paragraph')
|
||||
})
|
||||
|
||||
it('should make a task list from square brackets', () => {
|
||||
cy.get('.ProseMirror').then(([{ editor }]) => {
|
||||
editor.commands.clearContent()
|
||||
})
|
||||
|
||||
cy.get('.ProseMirror').type('[ ] List Item 1{enter}List Item 2')
|
||||
|
||||
cy.get('.ProseMirror')
|
||||
.find('li:nth-child(1)')
|
||||
.should('contain', 'List Item 1')
|
||||
.should('have.attr', 'data-checked', 'false')
|
||||
|
||||
cy.get('.ProseMirror')
|
||||
.find('li:nth-child(2)')
|
||||
.should('contain', 'List Item 2')
|
||||
.should('have.attr', 'data-checked', 'false')
|
||||
})
|
||||
|
||||
it('should make a task list from checked square brackets', () => {
|
||||
cy.get('.ProseMirror').then(([{ editor }]) => {
|
||||
editor.commands.clearContent()
|
||||
})
|
||||
|
||||
cy.get('.ProseMirror').type('[x] List Item 1{enter}List Item 2')
|
||||
|
||||
cy.get('.ProseMirror')
|
||||
.find('li:nth-child(1)')
|
||||
.should('contain', 'List Item 1')
|
||||
.should('have.attr', 'data-checked', 'true')
|
||||
|
||||
cy.get('.ProseMirror')
|
||||
.find('li:nth-child(2)')
|
||||
.should('contain', 'List Item 2')
|
||||
.should('have.attr', 'data-checked', 'false')
|
||||
})
|
||||
})
|
22
demos/src/Nodes/TaskList/React/styles.scss
Normal file
22
demos/src/Nodes/TaskList/React/styles.scss
Normal file
@ -0,0 +1,22 @@
|
||||
ul[data-type="taskList"] {
|
||||
list-style: none;
|
||||
padding: 0;
|
||||
|
||||
p {
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
li {
|
||||
display: flex;
|
||||
|
||||
> label {
|
||||
flex: 0 0 auto;
|
||||
margin-right: 0.5rem;
|
||||
user-select: none;
|
||||
}
|
||||
|
||||
> div {
|
||||
flex: 1 1 auto;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user