Add Image demo for React
This commit is contained in:
15
demos/src/Nodes/Image/React/index.html
Normal file
15
demos/src/Nodes/Image/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("Nodes/Image", source);
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
38
demos/src/Nodes/Image/React/index.jsx
Normal file
38
demos/src/Nodes/Image/React/index.jsx
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
import React, { useCallback } 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 Image from '@tiptap/extension-image'
|
||||||
|
import Dropcursor from '@tiptap/extension-dropcursor'
|
||||||
|
import './styles.scss'
|
||||||
|
|
||||||
|
export default () => {
|
||||||
|
const editor = useEditor({
|
||||||
|
extensions: [Document, Paragraph, Text, Image, Dropcursor],
|
||||||
|
content: `
|
||||||
|
<p>This is a basic example of implementing images. Drag to re-order.</p>
|
||||||
|
<img src="https://source.unsplash.com/8xznAGy4HcY/800x400" />
|
||||||
|
<img src="https://source.unsplash.com/K9QHL52rE2k/800x400" />
|
||||||
|
`,
|
||||||
|
})
|
||||||
|
|
||||||
|
const addImage = useCallback(() => {
|
||||||
|
const url = window.prompt('URL')
|
||||||
|
|
||||||
|
if (url) {
|
||||||
|
editor.chain().focus().setImage({ src: url }).run()
|
||||||
|
}
|
||||||
|
}, [editor])
|
||||||
|
|
||||||
|
if (!editor) {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div v-if="editor">
|
||||||
|
<button onClick={addImage}>setImage</button>
|
||||||
|
<EditorContent editor={editor} />
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
24
demos/src/Nodes/Image/React/index.spec.js
Normal file
24
demos/src/Nodes/Image/React/index.spec.js
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
context('/src/Nodes/Image/React/', () => {
|
||||||
|
before(() => {
|
||||||
|
cy.visit('/src/Nodes/Image/React/')
|
||||||
|
})
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
cy.get('.ProseMirror').then(([{ editor }]) => {
|
||||||
|
editor.commands.setContent('<p>Example Text</p>')
|
||||||
|
cy.get('.ProseMirror').type('{selectall}')
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should add an img tag with the correct URL', () => {
|
||||||
|
cy.window().then(win => {
|
||||||
|
cy.stub(win, 'prompt').returns('foobar.png')
|
||||||
|
|
||||||
|
cy.get('button:first').click()
|
||||||
|
|
||||||
|
cy.window().its('prompt').should('be.called')
|
||||||
|
|
||||||
|
cy.get('.ProseMirror').find('img').should('have.attr', 'src', 'foobar.png')
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
15
demos/src/Nodes/Image/React/styles.scss
Normal file
15
demos/src/Nodes/Image/React/styles.scss
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
/* Basic editor styles */
|
||||||
|
.ProseMirror {
|
||||||
|
> * + * {
|
||||||
|
margin-top: 0.75em;
|
||||||
|
}
|
||||||
|
|
||||||
|
img {
|
||||||
|
height: auto;
|
||||||
|
max-width: 100%;
|
||||||
|
|
||||||
|
&.ProseMirror-selectednode {
|
||||||
|
outline: 3px solid #68cef8;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user