Add HardBreak demo for React

This commit is contained in:
Sven Adlung 2021-11-16 14:50:12 +01:00
parent b0f447236b
commit 5cc5965ec7
3 changed files with 99 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/HardBreak", source);
</script>
</body>
</html>

View File

@ -0,0 +1,35 @@
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 HardBreak from '@tiptap/extension-hard-break'
export default () => {
const editor = useEditor({
extensions: [Document, Paragraph, Text, HardBreak],
content: `
<p>
This<br>
is<br>
a<br>
single<br>
paragraph<br>
with<br>
line<br>
breaks.
</p>
`,
})
if (!editor) {
return null
}
return (
<>
<button onClick={() => editor.chain().focus().setHardBreak().run()}>setHardBreak</button>
<EditorContent editor={editor} />
</>
)
}

View File

@ -0,0 +1,49 @@
context('/src/Nodes/HardBreak/React/', () => {
before(() => {
cy.visit('/src/Nodes/HardBreak/React/')
})
beforeEach(() => {
cy.get('.ProseMirror').then(([{ editor }]) => {
editor.commands.setContent('<p>Example Text</p>')
})
})
it('should parse hard breaks correctly', () => {
cy.get('.ProseMirror').then(([{ editor }]) => {
editor.commands.setContent('<p>Example<br>Text</p>')
expect(editor.getHTML()).to.eq('<p>Example<br>Text</p>')
})
})
it('should parse hard breaks with self-closing tag correctly', () => {
cy.get('.ProseMirror').then(([{ editor }]) => {
editor.commands.setContent('<p>Example<br />Text</p>')
expect(editor.getHTML()).to.eq('<p>Example<br>Text</p>')
})
})
it('the button should add a line break', () => {
cy.get('.ProseMirror br').should('not.exist')
cy.get('button:first').click()
cy.get('.ProseMirror br').should('exist')
})
it('the default keyboard shortcut should add a line break', () => {
cy.get('.ProseMirror br').should('not.exist')
cy.get('.ProseMirror').trigger('keydown', { shiftKey: true, key: 'Enter' })
cy.get('.ProseMirror br').should('exist')
})
it('the alternative keyboard shortcut should add a line break', () => {
cy.get('.ProseMirror br').should('not.exist')
cy.get('.ProseMirror').trigger('keydown', { modKey: true, key: 'Enter' })
cy.get('.ProseMirror br').should('exist')
})
})