Add Link demo for React

This commit is contained in:
svenadlung 2021-11-16 17:56:24 +01:00
parent c6071d5ab6
commit 0ad3c48435
4 changed files with 198 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/Link", source);
</script>
</body>
</html>

View File

@ -0,0 +1,71 @@
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 Link from '@tiptap/extension-link'
import Code from '@tiptap/extension-code'
import './styles.scss'
export default () => {
const editor = useEditor({
extensions: [
Document,
Paragraph,
Text,
Link.configure({
openOnClick: false,
}),
Code,
],
content: `
<p>
Wow, this editor has support for links to the whole <a href="https://en.wikipedia.org/wiki/World_Wide_Web">world wide web</a>. We tested a lot of URLs and I think you can add *every URL* you want. Isnt that cool? Lets try <a href="https://statamic.com/">another one!</a> Yep, seems to work.
</p>
<p>
By default every link will get a <code>rel="noopener noreferrer nofollow"</code> attribute. Its configurable though.
</p>
`,
})
const setLink = useCallback(() => {
const previousUrl = editor.getAttributes('link').href
const url = window.prompt('URL', previousUrl)
// cancelled
if (url === null) {
return
}
// empty
if (url === '') {
editor.chain().focus().extendMarkRange('link').unsetLink()
.run()
return
}
// update link
editor.chain().focus().extendMarkRange('link').setLink({ href: url })
.run()
}, [editor])
if (!editor) {
return null
}
return (
<>
<button onClick={setLink} className={editor.isActive('link') ? 'is-active' : ''}>
setLink
</button>
<button
onClick={() => editor.chain().focus().unsetLink().run()}
disabled={!editor.isActive('link')}
>
unsetLink
</button>
<EditorContent editor={editor} />
</>
)
}

View File

@ -0,0 +1,93 @@
context('/src/Marks/Link/React/', () => {
before(() => {
cy.visit('/src/Marks/Link/React/')
})
beforeEach(() => {
cy.get('.ProseMirror').then(([{ editor }]) => {
editor.commands.setContent('<p>Example Text</p>')
cy.get('.ProseMirror').type('{selectall}')
})
})
it('should parse a tags correctly', () => {
cy.get('.ProseMirror').then(([{ editor }]) => {
editor.commands.setContent('<p><a href="#">Example Text</a></p>')
expect(editor.getHTML()).to.eq(
'<p><a target="_blank" rel="noopener noreferrer nofollow" href="#">Example Text</a></p>',
)
})
})
it('should parse a tags with target attribute correctly', () => {
cy.get('.ProseMirror').then(([{ editor }]) => {
editor.commands.setContent('<p><a href="#" target="_self">Example Text</a></p>')
expect(editor.getHTML()).to.eq(
'<p><a target="_self" rel="noopener noreferrer nofollow" href="#">Example Text</a></p>',
)
})
})
it('should parse a tags with rel attribute correctly', () => {
cy.get('.ProseMirror').then(([{ editor }]) => {
editor.commands.setContent('<p><a href="#" rel="follow">Example Text</a></p>')
expect(editor.getHTML()).to.eq(
'<p><a target="_blank" rel="noopener noreferrer nofollow" href="#">Example Text</a></p>',
)
})
})
it('the button should add a link to the selected text', () => {
cy.window().then(win => {
cy.stub(win, 'prompt').returns('https://tiptap.dev')
cy.get('button:first').click()
cy.window().its('prompt').should('be.called')
cy.get('.ProseMirror')
.find('a')
.should('contain', 'Example Text')
.should('have.attr', 'href', 'https://tiptap.dev')
})
})
it('detects a pasted URL within a text', () => {
cy.get('.ProseMirror')
.paste({
pastePayload: 'some text https://example.com around an url',
pasteType: 'text/plain',
})
.find('a')
.should('contain', 'https://example.com')
.should('have.attr', 'href', 'https://example.com')
})
it('detects a pasted URL', () => {
cy.get('.ProseMirror')
.paste({ pastePayload: 'https://example.com', pasteType: 'text/plain' })
.find('a')
.should('contain', 'Example Text')
.should('have.attr', 'href', 'https://example.com')
})
it('correctly detects multiple pasted URLs', () => {
cy.get('.ProseMirror').paste({
pastePayload:
'https://example1.com, https://example2.com/foobar, (http://example3.com/foobar)',
pasteType: 'text/plain',
})
cy.get('.ProseMirror')
.find('a[href="https://example1.com"]')
.should('contain', 'https://example1.com')
cy.get('.ProseMirror')
.find('a[href="https://example2.com/foobar"]')
.should('contain', 'https://example2.com/foobar')
cy.get('.ProseMirror')
.find('a[href="http://example3.com/foobar"]')
.should('contain', 'http://example3.com/foobar')
})
})

View File

@ -0,0 +1,19 @@
/* Basic editor styles */
.ProseMirror {
> * + * {
margin-top: 0.75em;
}
a {
color: #68cef8;
}
code {
background-color: rgba(#616161, 0.1);
border-radius: 0.25em;
box-decoration-break: clone;
color: #616161;
font-size: 0.9rem;
padding: 0.25em;
}
}