Add ListItem demo for React

This commit is contained in:
Sven Adlung 2021-11-16 15:08:58 +01:00
parent 60957c2677
commit 73120d79bb
4 changed files with 141 additions and 0 deletions

View 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/ListItem", source);
</script>
</body>
</html>

View File

@ -0,0 +1,77 @@
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 BulletList from '@tiptap/extension-bullet-list'
import OrderedList from '@tiptap/extension-ordered-list'
import ListItem from '@tiptap/extension-list-item'
import './styles.scss'
export default () => {
const editor = useEditor({
extensions: [Document, Paragraph, Text, BulletList, OrderedList, ListItem],
content: `
<p>
I like lists. Lets add one:
</p>
<ul>
<li>This is a bullet list.</li>
<li>And it has three list items.</li>
<li>Here is the third one.</li>
</ul>
<p>
Do you want to see one more? I bet! Here is another one:
</p>
<ol>
<li>Thats a different list, actually its an ordered list.</li>
<li>It also has three list items.</li>
<li>And all of them are numbered.</li>
</ol>
<p>
Lists would be nothing without list items.
</p>
`,
})
if (!editor) {
return null
}
return (
<>
<button
onClick={() => editor.chain().focus().toggleBulletList().run()}
className={editor.isActive('bulletList') ? 'is-active' : ''}
>
toggleBulletList
</button>
<button
onClick={() => editor.chain().focus().toggleOrderedList().run()}
className={editor.isActive('orderedList') ? 'is-active' : ''}
>
toggleOrderedList
</button>
<button
onClick={() => editor.chain().focus().splitListItem('listItem').run()}
disabled={!editor.can().splitListItem('listItem')}
>
splitListItem
</button>
<button
onClick={() => editor.chain().focus().sinkListItem('listItem').run()}
disabled={!editor.can().sinkListItem('listItem')}
>
sinkListItem
</button>
<button
onClick={() => editor.chain().focus().liftListItem('listItem').run()}
disabled={!editor.can().liftListItem('listItem')}
>
liftListItem
</button>
<EditorContent editor={editor} />
</>
)
}

View File

@ -0,0 +1,38 @@
context('/src/Nodes/ListItem/React/', () => {
before(() => {
cy.visit('/src/Nodes/ListItem/React/')
})
beforeEach(() => {
cy.get('.ProseMirror').then(([{ editor }]) => {
editor.commands.setContent('<ul><li>Example Text</li></ul>')
})
})
it('should add a new list item on Enter', () => {
cy.get('.ProseMirror').type('{enter}2nd Item')
cy.get('.ProseMirror').find('li:nth-child(1)').should('contain', 'Example Text')
cy.get('.ProseMirror').find('li:nth-child(2)').should('contain', '2nd Item')
})
it('should sink the list item on Tab', () => {
cy.get('.ProseMirror').type('{enter}').trigger('keydown', { key: 'Tab' })
cy.get('.ProseMirror').type('2nd Level')
cy.get('.ProseMirror').find('li:nth-child(1) li').should('contain', '2nd Level')
})
it('should lift the list item on Shift+Tab', () => {
cy.get('.ProseMirror')
.type('{enter}')
.trigger('keydown', { key: 'Tab' })
.trigger('keydown', { shiftKey: true, key: 'Tab' })
cy.get('.ProseMirror').type('1st Level')
cy.get('.ProseMirror').find('li:nth-child(2)').should('contain', '1st Level')
})
})

View File

@ -0,0 +1,11 @@
/* Basic editor styles */
.ProseMirror {
> * + * {
margin-top: 0.75em;
}
ul,
ol {
padding: 0 1rem;
}
}