mirror of
https://github.com/ueberdosis/tiptap.git
synced 2025-06-07 09:25:29 +08:00
Add ExportHTML demo for React
This commit is contained in:
parent
9956c9c51b
commit
7749eaa18a
15
demos/src/GuideContent/ExportHTML/React/index.html
Normal file
15
demos/src/GuideContent/ExportHTML/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("GuideContent/ExportHTML", source);
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
87
demos/src/GuideContent/ExportHTML/React/index.jsx
Normal file
87
demos/src/GuideContent/ExportHTML/React/index.jsx
Normal file
@ -0,0 +1,87 @@
|
||||
import React, { useState, useEffect, useCallback } from 'react'
|
||||
import { useEditor, EditorContent } from '@tiptap/react'
|
||||
import StarterKit from '@tiptap/starter-kit'
|
||||
import './styles.scss'
|
||||
|
||||
export default () => {
|
||||
const [html, setHtml] = useState(null)
|
||||
const editor = useEditor({
|
||||
content: `
|
||||
<p>
|
||||
Wow, this editor instance exports its content as HTML.
|
||||
</p>
|
||||
`,
|
||||
extensions: [StarterKit],
|
||||
})
|
||||
|
||||
useEffect(() => {
|
||||
if (!editor) {
|
||||
return null
|
||||
}
|
||||
|
||||
// Get the initial content …
|
||||
setHtml(editor.getHTML())
|
||||
|
||||
// … and get the content after every change.
|
||||
editor.on('update', () => {
|
||||
setHtml(editor.getHTML())
|
||||
})
|
||||
}, [editor])
|
||||
|
||||
const setContent = useCallback(() => {
|
||||
// You can pass a HTML document to the editor.
|
||||
editor.commands.setContent(
|
||||
`
|
||||
<p>
|
||||
It’s 19871. You can’t turn on a radio, or go to a mall without hearing Olivia Newton-John’s hit song, Physical.
|
||||
</p>
|
||||
`,
|
||||
true,
|
||||
)
|
||||
|
||||
// It’s likely that you’d like to focus the Editor after most commands.
|
||||
editor.commands.focus()
|
||||
}, [editor])
|
||||
|
||||
const clearContent = useCallback(() => {
|
||||
editor.chain().clearContent(true).focus().run()
|
||||
}, [editor])
|
||||
|
||||
if (!editor) {
|
||||
return null
|
||||
}
|
||||
|
||||
return (
|
||||
<>
|
||||
<div className="actions">
|
||||
<button className="button" onClick={setContent}>
|
||||
Set Content
|
||||
</button>
|
||||
<button className="button" onClick={clearContent}>
|
||||
Clear Content
|
||||
</button>
|
||||
<button
|
||||
onClick={() => editor.chain().focus().toggleBold().run()}
|
||||
className={editor.isActive('bold') ? 'is-active' : ''}
|
||||
>
|
||||
Bold
|
||||
</button>
|
||||
<button
|
||||
onClick={() => editor.chain().focus().toggleItalic().run()}
|
||||
className={editor.isActive('italic') ? 'is-active' : ''}
|
||||
>
|
||||
Italic
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<EditorContent editor={editor} />
|
||||
|
||||
<div className="export">
|
||||
<h3>HTML</h3>
|
||||
<pre>
|
||||
<code>{html}</code>
|
||||
</pre>
|
||||
</div>
|
||||
</>
|
||||
)
|
||||
}
|
19
demos/src/GuideContent/ExportHTML/React/index.spec.js
Normal file
19
demos/src/GuideContent/ExportHTML/React/index.spec.js
Normal file
@ -0,0 +1,19 @@
|
||||
context('/src/GuideContent/ExportHTML/React/', () => {
|
||||
before(() => {
|
||||
cy.visit('/src/GuideContent/ExportHTML/React/')
|
||||
})
|
||||
|
||||
beforeEach(() => {
|
||||
cy.get('.ProseMirror').then(([{ editor }]) => {
|
||||
editor.commands.setContent('<p>Example Text</p>')
|
||||
})
|
||||
})
|
||||
|
||||
it('should return html', () => {
|
||||
cy.get('.ProseMirror').then(([{ editor }]) => {
|
||||
const html = editor.getHTML()
|
||||
|
||||
expect(html).to.equal('<p>Example Text</p>')
|
||||
})
|
||||
})
|
||||
})
|
77
demos/src/GuideContent/ExportHTML/React/styles.scss
Normal file
77
demos/src/GuideContent/ExportHTML/React/styles.scss
Normal file
@ -0,0 +1,77 @@
|
||||
/* Style the export */
|
||||
.export {
|
||||
padding: 1rem 0 0;
|
||||
|
||||
h3 {
|
||||
margin: 1rem 0 0.5rem;
|
||||
}
|
||||
|
||||
pre {
|
||||
border-radius: 5px;
|
||||
color: #333;
|
||||
}
|
||||
|
||||
code {
|
||||
background-color: #e9ecef;
|
||||
color: #495057;
|
||||
display: block;
|
||||
font-size: 0.8rem;
|
||||
padding: 0.75rem 1rem;
|
||||
white-space: pre-wrap;
|
||||
}
|
||||
}
|
||||
|
||||
/* Basic editor styles */
|
||||
.ProseMirror {
|
||||
> * + * {
|
||||
margin-top: 0.75em;
|
||||
}
|
||||
|
||||
ul,
|
||||
ol {
|
||||
padding: 0 1rem;
|
||||
}
|
||||
|
||||
h1,
|
||||
h2,
|
||||
h3,
|
||||
h4,
|
||||
h5,
|
||||
h6 {
|
||||
line-height: 1.1;
|
||||
}
|
||||
|
||||
code {
|
||||
background-color: rgba(#616161, 0.1);
|
||||
color: #616161;
|
||||
}
|
||||
|
||||
pre {
|
||||
background: #0d0d0d;
|
||||
border-radius: 0.5rem;
|
||||
color: #fff;
|
||||
font-family: "JetBrainsMono", monospace;
|
||||
padding: 0.75rem 1rem;
|
||||
|
||||
code {
|
||||
background: none;
|
||||
color: inherit;
|
||||
font-size: 0.8rem;
|
||||
padding: 0;
|
||||
}
|
||||
}
|
||||
|
||||
img {
|
||||
height: auto;
|
||||
max-width: 100%;
|
||||
}
|
||||
|
||||
hr {
|
||||
margin: 1rem 0;
|
||||
}
|
||||
|
||||
blockquote {
|
||||
border-left: 2px solid rgba(#0d0d0d, 0.1);
|
||||
padding-left: 1rem;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user