Add Bold demo for React
This commit is contained in:
15
demos/src/Marks/Bold/React/index.html
Normal file
15
demos/src/Marks/Bold/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("Marks/Bold", source);
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
50
demos/src/Marks/Bold/React/index.jsx
Normal file
50
demos/src/Marks/Bold/React/index.jsx
Normal file
@@ -0,0 +1,50 @@
|
||||
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 Bold from '@tiptap/extension-bold'
|
||||
|
||||
export default () => {
|
||||
const editor = useEditor({
|
||||
extensions: [Document, Paragraph, Text, Bold],
|
||||
content: `
|
||||
<p>This isn’t bold.</p>
|
||||
<p><strong>This is bold.</strong></p>
|
||||
<p><b>And this.</b></p>
|
||||
<p style="font-weight: bold">This as well.</p>
|
||||
<p style="font-weight: bolder">Oh, and this!</p>
|
||||
<p style="font-weight: 500">Cool, isn’t it!?</p>
|
||||
<p style="font-weight: 999">Up to font weight 999!!!</p>
|
||||
`,
|
||||
})
|
||||
|
||||
if (!editor) {
|
||||
return null
|
||||
}
|
||||
|
||||
return (
|
||||
<>
|
||||
<button
|
||||
onClick={() => editor.chain().focus().toggleBold().run()}
|
||||
className={editor.isActive('bold') ? 'is-active' : ''}
|
||||
>
|
||||
toggleBold
|
||||
</button>
|
||||
<button
|
||||
onClick={() => editor.chain().focus().setBold().run()}
|
||||
disabled={editor.isActive('bold')}
|
||||
>
|
||||
setBold
|
||||
</button>
|
||||
<button
|
||||
onClick={() => editor.chain().focus().unsetBold().run()}
|
||||
disabled={!editor.isActive('bold')}
|
||||
>
|
||||
unsetBold
|
||||
</button>
|
||||
|
||||
<EditorContent editor={editor} />
|
||||
</>
|
||||
)
|
||||
}
|
||||
81
demos/src/Marks/Bold/React/index.spec.js
Normal file
81
demos/src/Marks/Bold/React/index.spec.js
Normal file
@@ -0,0 +1,81 @@
|
||||
context('/src/Marks/Bold/React/', () => {
|
||||
before(() => {
|
||||
cy.visit('/src/Marks/Bold/React/')
|
||||
})
|
||||
|
||||
beforeEach(() => {
|
||||
cy.get('.ProseMirror').then(([{ editor }]) => {
|
||||
editor.commands.setContent('<p>Example Text</p>')
|
||||
cy.get('.ProseMirror').type('{selectall}')
|
||||
})
|
||||
})
|
||||
|
||||
it('should transform b tags to strong tags', () => {
|
||||
cy.get('.ProseMirror').then(([{ editor }]) => {
|
||||
editor.commands.setContent('<p><b>Example Text</b></p>')
|
||||
expect(editor.getHTML()).to.eq('<p><strong>Example Text</strong></p>')
|
||||
})
|
||||
})
|
||||
|
||||
it('sould omit b tags with normal font weight inline style', () => {
|
||||
cy.get('.ProseMirror').then(([{ editor }]) => {
|
||||
editor.commands.setContent('<p><b style="font-weight: normal">Example Text</b></p>')
|
||||
expect(editor.getHTML()).to.eq('<p>Example Text</p>')
|
||||
})
|
||||
})
|
||||
|
||||
it('should transform any tag with bold inline style to strong tags', () => {
|
||||
cy.get('.ProseMirror').then(([{ editor }]) => {
|
||||
editor.commands.setContent('<p><span style="font-weight: bold">Example Text</span></p>')
|
||||
expect(editor.getHTML()).to.eq('<p><strong>Example Text</strong></p>')
|
||||
|
||||
editor.commands.setContent('<p><span style="font-weight: bolder">Example Text</span></p>')
|
||||
expect(editor.getHTML()).to.eq('<p><strong>Example Text</strong></p>')
|
||||
|
||||
editor.commands.setContent('<p><span style="font-weight: 500">Example Text</span></p>')
|
||||
expect(editor.getHTML()).to.eq('<p><strong>Example Text</strong></p>')
|
||||
|
||||
editor.commands.setContent('<p><span style="font-weight: 900">Example Text</span></p>')
|
||||
expect(editor.getHTML()).to.eq('<p><strong>Example Text</strong></p>')
|
||||
})
|
||||
})
|
||||
|
||||
it('the button should make the selected text bold', () => {
|
||||
cy.get('button:first').click()
|
||||
|
||||
cy.get('.ProseMirror').find('strong').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 strong').should('not.exist')
|
||||
})
|
||||
|
||||
it('should make the selected text bold when the keyboard shortcut is pressed', () => {
|
||||
cy.get('.ProseMirror')
|
||||
.trigger('keydown', { modKey: true, key: 'b' })
|
||||
.find('strong')
|
||||
.should('contain', 'Example Text')
|
||||
})
|
||||
|
||||
it('should toggle the selected text bold when the keyboard shortcut is pressed', () => {
|
||||
cy.get('.ProseMirror')
|
||||
.trigger('keydown', { modKey: true, key: 'b' })
|
||||
.find('strong')
|
||||
.should('contain', 'Example Text')
|
||||
|
||||
cy.get('.ProseMirror').trigger('keydown', { modKey: true, key: 'b' })
|
||||
|
||||
cy.get('.ProseMirror strong').should('not.exist')
|
||||
})
|
||||
|
||||
it('should make a bold text from the default markdown shortcut', () => {
|
||||
cy.get('.ProseMirror').type('**Bold**').find('strong').should('contain', 'Bold')
|
||||
})
|
||||
|
||||
it('should make a bold text from the alternative markdown shortcut', () => {
|
||||
cy.get('.ProseMirror').type('__Bold__').find('strong').should('contain', 'Bold')
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user