mirror of
https://github.com/ueberdosis/tiptap.git
synced 2025-06-11 03:33:12 +08:00
Add OrderedList demo for React
This commit is contained in:
parent
73120d79bb
commit
cbc717334d
15
demos/src/Nodes/OrderedList/React/index.html
Normal file
15
demos/src/Nodes/OrderedList/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/OrderedList", source);
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
60
demos/src/Nodes/OrderedList/React/index.jsx
Normal file
60
demos/src/Nodes/OrderedList/React/index.jsx
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
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 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, OrderedList, ListItem],
|
||||||
|
content: `
|
||||||
|
<ol>
|
||||||
|
<li>A list item</li>
|
||||||
|
<li>And another one</li>
|
||||||
|
</ol>
|
||||||
|
|
||||||
|
<ol start="5">
|
||||||
|
<li>This item starts at 5</li>
|
||||||
|
<li>And another one</li>
|
||||||
|
</ol>
|
||||||
|
`,
|
||||||
|
})
|
||||||
|
|
||||||
|
if (!editor) {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<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} />
|
||||||
|
</>
|
||||||
|
)
|
||||||
|
}
|
101
demos/src/Nodes/OrderedList/React/index.spec.js
Normal file
101
demos/src/Nodes/OrderedList/React/index.spec.js
Normal file
@ -0,0 +1,101 @@
|
|||||||
|
context('/src/Nodes/OrderedList/React/', () => {
|
||||||
|
before(() => {
|
||||||
|
cy.visit('/src/Nodes/OrderedList/React/')
|
||||||
|
})
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
cy.get('.ProseMirror').then(([{ editor }]) => {
|
||||||
|
editor.commands.setContent('<p>Example Text</p>')
|
||||||
|
cy.get('.ProseMirror').type('{selectall}')
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should parse ordered lists correctly', () => {
|
||||||
|
cy.get('.ProseMirror').then(([{ editor }]) => {
|
||||||
|
editor.commands.setContent('<ol><li><p>Example Text</p></li></ol>')
|
||||||
|
expect(editor.getHTML()).to.eq('<ol><li><p>Example Text</p></li></ol>')
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should parse ordered lists without paragraphs correctly', () => {
|
||||||
|
cy.get('.ProseMirror').then(([{ editor }]) => {
|
||||||
|
editor.commands.setContent('<ol><li>Example Text</li></ol>')
|
||||||
|
expect(editor.getHTML()).to.eq('<ol><li><p>Example Text</p></li></ol>')
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
it('the button should make the selected line a ordered list item', () => {
|
||||||
|
cy.get('.ProseMirror ol').should('not.exist')
|
||||||
|
|
||||||
|
cy.get('.ProseMirror ol li').should('not.exist')
|
||||||
|
|
||||||
|
cy.get('button:nth-child(1)').click()
|
||||||
|
|
||||||
|
cy.get('.ProseMirror').find('ol').should('contain', 'Example Text')
|
||||||
|
|
||||||
|
cy.get('.ProseMirror').find('ol li').should('contain', 'Example Text')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('the button should toggle the ordered list', () => {
|
||||||
|
cy.get('.ProseMirror ol').should('not.exist')
|
||||||
|
|
||||||
|
cy.get('button:nth-child(1)').click()
|
||||||
|
|
||||||
|
cy.get('.ProseMirror').find('ol').should('contain', 'Example Text')
|
||||||
|
|
||||||
|
cy.get('button:nth-child(1)').click()
|
||||||
|
|
||||||
|
cy.get('.ProseMirror ol').should('not.exist')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should make the paragraph an ordered list keyboard shortcut is pressed', () => {
|
||||||
|
cy.get('.ProseMirror')
|
||||||
|
.trigger('keydown', { modKey: true, shiftKey: true, key: '7' })
|
||||||
|
.find('ol 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('1. 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 ordered list from a number', () => {
|
||||||
|
cy.get('.ProseMirror').then(([{ editor }]) => {
|
||||||
|
editor.commands.clearContent()
|
||||||
|
})
|
||||||
|
|
||||||
|
cy.get('.ProseMirror').type('1. List Item 1{enter}List Item 2')
|
||||||
|
|
||||||
|
cy.get('.ProseMirror').find('li:nth-child(1)').should('contain', 'List Item 1')
|
||||||
|
|
||||||
|
cy.get('.ProseMirror').find('li:nth-child(2)').should('contain', 'List Item 2')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should make a ordered list from a number other than number one', () => {
|
||||||
|
cy.get('.ProseMirror').then(([{ editor }]) => {
|
||||||
|
editor.commands.clearContent()
|
||||||
|
})
|
||||||
|
|
||||||
|
cy.get('.ProseMirror').type('2. List Item 1{enter}List Item 2')
|
||||||
|
|
||||||
|
cy.get('.ProseMirror').find('ol').should('have.attr', 'start', '2')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should remove the ordered list after pressing backspace', () => {
|
||||||
|
cy.get('.ProseMirror').then(([{ editor }]) => {
|
||||||
|
editor.commands.clearContent()
|
||||||
|
})
|
||||||
|
|
||||||
|
cy.get('.ProseMirror').type('1. {backspace}Example')
|
||||||
|
|
||||||
|
cy.get('.ProseMirror').find('p').should('contain', '1. Example')
|
||||||
|
})
|
||||||
|
})
|
11
demos/src/Nodes/OrderedList/React/styles.scss
Normal file
11
demos/src/Nodes/OrderedList/React/styles.scss
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
/* Basic editor styles */
|
||||||
|
.ProseMirror {
|
||||||
|
> * + * {
|
||||||
|
margin-top: 0.75em;
|
||||||
|
}
|
||||||
|
|
||||||
|
ul,
|
||||||
|
ol {
|
||||||
|
padding: 0 1rem;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user