Add Subscript demo for React

This commit is contained in:
svenadlung 2021-11-16 17:46:23 +01:00
parent 61603c8323
commit 6b31c9c378
3 changed files with 100 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/Subscript", source);
</script>
</body>
</html>

View File

@ -0,0 +1,46 @@
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 Subscript from '@tiptap/extension-subscript'
export default () => {
const editor = useEditor({
extensions: [Document, Paragraph, Text, Subscript],
content: `
<p>This is regular text.</p>
<p><sub>This is subscript.</sub></p>
<p><span style="vertical-align: sub">And this.</span></p>
`,
})
if (!editor) {
return null
}
return (
<>
<button
onClick={() => editor.chain().focus().toggleSubscript().run()}
className={editor.isActive('subscript') ? 'is-active' : ''}
>
toggleSubscript
</button>
<button
onClick={() => editor.chain().focus().setSubscript().run()}
disabled={editor.isActive('subscript')}
>
setSubscript
</button>
<button
onClick={() => editor.chain().focus().unsetSubscript().run()}
disabled={!editor.isActive('subscript')}
>
unsetSubscript
</button>
<EditorContent editor={editor} />
</>
)
}

View File

@ -0,0 +1,39 @@
context('/src/Marks/Subscript/React/', () => {
before(() => {
cy.visit('/src/Marks/Subscript/React/')
})
beforeEach(() => {
cy.get('.ProseMirror').then(([{ editor }]) => {
editor.commands.setContent('<p>Example Text</p>')
cy.get('.ProseMirror').type('{selectall}')
})
})
it('should transform inline style to sub tags', () => {
cy.get('.ProseMirror').then(([{ editor }]) => {
editor.commands.setContent('<p><span style="vertical-align: sub">Example Text</span></p>')
expect(editor.getHTML()).to.eq('<p><sub>Example Text</sub></p>')
})
})
it('sould omit inline style with a different vertical align', () => {
cy.get('.ProseMirror').then(([{ editor }]) => {
editor.commands.setContent('<p><b style="vertical-align: middle">Example Text</b></p>')
expect(editor.getHTML()).to.eq('<p>Example Text</p>')
})
})
it('the button should make the selected text bold', () => {
cy.get('button:first').click()
cy.get('.ProseMirror').find('sub').should('contain', 'Example Text')
})
it('the button should toggle the selected text bold', () => {
cy.get('button:first').click()
cy.get('.ProseMirror').type('{selectall}')
cy.get('button:first').click()
cy.get('.ProseMirror sub').should('not.exist')
})
})