Add Italic demo for React

This commit is contained in:
svenadlung 2021-11-16 17:19:27 +01:00
parent 2bb4edd533
commit 3eca6a1987
3 changed files with 126 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("Marks/Italic", source);
</script>
</body>
</html>

View File

@ -0,0 +1,47 @@
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 Italic from '@tiptap/extension-italic'
export default () => {
const editor = useEditor({
extensions: [Document, Paragraph, Text, Italic],
content: `
<p>This isnt italic.</p>
<p><em>This is italic.</em></p>
<p><i>And this.</i></p>
<p style="font-style: italic">This as well.</p>
`,
})
if (!editor) {
return null
}
return (
<>
<button
onClick={() => editor.chain().focus().toggleItalic().run()}
className={editor.isActive('italic') ? 'is-active' : ''}
>
toggleItalic
</button>
<button
onClick={() => editor.chain().focus().setItalic().run()}
disabled={editor.isActive('italic')}
>
setItalic
</button>
<button
onClick={() => editor.chain().focus().unsetItalic().run()}
disabled={!editor.isActive('italic')}
>
unsetItalic
</button>
<EditorContent editor={editor} />
</>
)
}

View File

@ -0,0 +1,64 @@
context('/src/Marks/Italic/React/', () => {
before(() => {
cy.visit('/src/Marks/Italic/React/')
})
beforeEach(() => {
cy.get('.ProseMirror').then(([{ editor }]) => {
editor.commands.setContent('<p>Example Text</p>')
cy.get('.ProseMirror').type('{selectall}')
})
})
it('i tags should be transformed to em tags', () => {
cy.get('.ProseMirror').then(([{ editor }]) => {
editor.commands.setContent('<p><i>Example Text</i></p>')
expect(editor.getHTML()).to.eq('<p><em>Example Text</em></p>')
})
})
it('i tags with normal font style should be omitted', () => {
cy.get('.ProseMirror').then(([{ editor }]) => {
editor.commands.setContent('<p><i style="font-style: normal">Example Text</i></p>')
expect(editor.getHTML()).to.eq('<p>Example Text</p>')
})
})
it('generic tags with italic style should be transformed to strong tags', () => {
cy.get('.ProseMirror').then(([{ editor }]) => {
editor.commands.setContent('<p><span style="font-style: italic">Example Text</span></p>')
expect(editor.getHTML()).to.eq('<p><em>Example Text</em></p>')
})
})
it('the button should make the selected text italic', () => {
cy.get('button:first').click()
cy.get('.ProseMirror').find('em').should('contain', 'Example Text')
})
it('the button should toggle the selected text italic', () => {
cy.get('button:first').click()
cy.get('.ProseMirror').type('{selectall}')
cy.get('button:first').click()
cy.get('.ProseMirror em').should('not.exist')
})
it('the keyboard shortcut should make the selected text italic', () => {
cy.get('.ProseMirror')
.trigger('keydown', { modKey: true, key: 'i' })
.find('em')
.should('contain', 'Example Text')
})
it('the keyboard shortcut should toggle the selected text italic', () => {
cy.get('.ProseMirror')
.trigger('keydown', { modKey: true, key: 'i' })
.trigger('keydown', { modKey: true, key: 'i' })
.find('em')
.should('not.exist')
})
})