Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing reoccurring issue #3331 and improving related PR #3533 #3862

Merged
merged 2 commits into from
Mar 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 25 additions & 0 deletions demos/src/Examples/CustomParagraph/React/Paragraph.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Paragraph as BaseParagraph } from '@tiptap/extension-paragraph'
import {
NodeViewContent,
NodeViewWrapper,
ReactNodeViewRenderer,
} from '@tiptap/react'

const ParagraphComponent = ({ node }) => {
return (
<NodeViewWrapper style={{ position: 'relative' }}>
<span contentEditable={false} className="label" style={{
position: 'absolute', right: '100%', fontSize: '10px', color: '#999',
}}>
{node.textContent.length}
</span>
<NodeViewContent as="p" />
</NodeViewWrapper>
)
}

export const Paragraph = BaseParagraph.extend({
addNodeView() {
return ReactNodeViewRenderer(ParagraphComponent)
},
})
Empty file.
27 changes: 27 additions & 0 deletions demos/src/Examples/CustomParagraph/React/index.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import './styles.scss'

import { EditorContent, useEditor } from '@tiptap/react'
import StarterKit from '@tiptap/starter-kit'
import React from 'react'

import { Paragraph } from './Paragraph.jsx'

export default () => {
const editor = useEditor({
extensions: [
StarterKit.configure({
paragraph: false,
}),
Paragraph,
],
content: `
<p>
Each line shows the number of characters in the paragraph.
</p>
`,
})

return (
<EditorContent editor={editor} />
)
}
23 changes: 23 additions & 0 deletions demos/src/Examples/CustomParagraph/React/index.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
context('/src/Examples/CustomParagraph/React/', () => {
beforeEach(() => {
cy.visit('/src/Examples/CustomParagraph/React/')
})

it('should have a working tiptap instance', () => {
cy.get('.ProseMirror').then(([{ editor }]) => {
// eslint-disable-next-line
expect(editor).to.not.be.null
})
})

it('should have a paragraph and text length', () => {
cy.get('.ProseMirror p').should('exist').should('have.text', 'Each line shows the number of characters in the paragraph.')
cy.get('.ProseMirror .label').should('exist').should('have.text', '58')
})

it('should have new paragraph', () => {
cy.get('.ProseMirror').type('{selectall}{moveToEnd}{enter}')
cy.get('.ProseMirror p').eq(1).should('exist').should('have.text', '')
cy.get('.ProseMirror .label').eq(1).should('exist').should('have.text', '0')
})
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to test the behavior addressed in this PR, but I could not reproduce in Cypress environment.
But I think this example is still helpful to find this problem easily.

})
24 changes: 24 additions & 0 deletions demos/src/Examples/CustomParagraph/React/styles.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/* Basic editor styles */
.ProseMirror {
> * + * {
margin-top: 0.75em;
}
}

/* Placeholder (at the top) */
/*.ProseMirror p.is-editor-empty:first-child::before {
content: attr(data-placeholder);
float: left;
color: #ced4da;
pointer-events: none;
height: 0;
}*/

/* Placeholder (on every new line) */
.ProseMirror .is-empty::before {
content: attr(data-placeholder);
float: left;
color: #ced4da;
pointer-events: none;
height: 0;
}
32 changes: 32 additions & 0 deletions demos/src/Examples/CustomParagraph/Vue/Component.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<template>
<node-view-wrapper class="vue-component">
<span contenteditable="false" class="label">{{ node.textContent.length }}</span>
<node-view-content as="p" />
</node-view-wrapper>
</template>

<script>
import { NodeViewContent, nodeViewProps, NodeViewWrapper } from '@tiptap/vue-3'

export default {
components: {
NodeViewWrapper,
NodeViewContent,
},

props: nodeViewProps,
}
</script>

<style lang="scss">
.vue-component {
position: relative;
}

.label {
position: absolute;
right: 100%;
font-size: 10px;
color: #999;
}
</style>
10 changes: 10 additions & 0 deletions demos/src/Examples/CustomParagraph/Vue/Extension.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Paragraph as BaseParagraph } from '@tiptap/extension-paragraph'
import { VueNodeViewRenderer } from '@tiptap/vue-3'

import Component from './Component.vue'

export default BaseParagraph.extend({
addNodeView() {
return VueNodeViewRenderer(Component)
},
})
Empty file.
23 changes: 23 additions & 0 deletions demos/src/Examples/CustomParagraph/Vue/index.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
context('/src/Examples/CustomParagraph/React/', () => {
beforeEach(() => {
cy.visit('/src/Examples/CustomParagraph/React/')
})

it('should have a working tiptap instance', () => {
cy.get('.ProseMirror').then(([{ editor }]) => {
// eslint-disable-next-line
expect(editor).to.not.be.null
})
})

it('should have a paragraph and text length', () => {
cy.get('.ProseMirror p').should('exist').should('have.text', 'Each line shows the number of characters in the paragraph.')
cy.get('.ProseMirror .label').should('exist').should('have.text', '58')
})

it('should have new paragraph', () => {
cy.get('.ProseMirror').type('{selectall}{moveToEnd}{enter}')
cy.get('.ProseMirror p').eq(1).should('exist').should('have.text', '')
cy.get('.ProseMirror .label').eq(1).should('exist').should('have.text', '0')
})
})
55 changes: 55 additions & 0 deletions demos/src/Examples/CustomParagraph/Vue/index.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<template>
<editor-content :editor="editor" />
</template>

<script>
import StarterKit from '@tiptap/starter-kit'
import { Editor, EditorContent } from '@tiptap/vue-3'

import Paragraph from './Extension.js'

export default {
components: {
EditorContent,
},

data() {
return {
editor: null,
}
},

mounted() {
this.editor = new Editor({
extensions: [
StarterKit.configure({
paragraph: false,
}),
Paragraph,
],
content: `
<p>
Each line shows the number of characters in the paragraph.
</p>
<p>
Hello, custom paragraph!
</p>
`,
})
},

beforeUnmount() {
this.editor.destroy()
},
}
</script>

<style lang="scss">
/* Basic editor styles */
.ProseMirror {
> * + * {
margin-top: 0.75em;
}
}
</style>
n
4 changes: 1 addition & 3 deletions packages/react/src/EditorContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,7 @@ export class PureEditorContent extends React.Component<EditorContentProps, Edito
// lifecycle methods, and React doesn't allow calling flushSync from inside
// a lifecycle method.
if (this.initialized) {
queueMicrotask(() => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this still prevent the initial issue we had before all those changes regarding the cursor position / rendering?

Our Netlify build didn't work for this so I can't test it right now. I'll check it out on my machine in the meantime to test your changes.

flushSync(fn)
})
flushSync(fn)
} else {
fn()
}
Expand Down