Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/chatty-items-build.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@tiptap/core': patch
---

fix(core):multiple-instances-style
42 changes: 30 additions & 12 deletions packages/core/src/Editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,13 @@ export class Editor extends EventEmitter<EditorEvents> {
this.createView(el)
this.emit('mount', { editor: this })

if (this.css && !document.head.contains(this.css)) {
document.head.appendChild(this.css)
if (this.css) {
if (document.head.contains(this.css)) {
const refCount = this.css.getAttribute('ref-count') || '1'
this.css.setAttribute('ref-count', `${Number(refCount) + 1}`)
} else {
document.head.appendChild(this.css)
}
}

window.setTimeout(() => {
Expand Down Expand Up @@ -197,21 +202,34 @@ export class Editor extends EventEmitter<EditorEvents> {
// Safely remove CSS element with fallback for test environments
// Only remove CSS if no other editors exist in the document after unmount
if (this.css && !document.querySelectorAll(`.${this.className}`).length) {
try {
if (typeof this.css.remove === 'function') {
this.css.remove()
} else if (this.css.parentNode) {
this.css.parentNode.removeChild(this.css)
}
} catch (error) {
// Silently handle any unexpected DOM removal errors in test environments
console.warn('Failed to remove CSS element:', error)
const refCount = this.css.getAttribute('ref-count') || '1'
if (Number(refCount) <= 1) {
this.removeCss()
} else {
this.css.setAttribute('ref-count', `${Number(refCount) - 1}`)
}
}
this.css = null
this.emit('unmount', { editor: this })
}

private removeCss() {
if (!this.css) {
return
}
try {
if (typeof this.css.remove === 'function') {
this.css.remove()
} else if (this.css.parentNode) {
this.css.parentNode.removeChild(this.css)
}
} catch (error) {
// Silently handle any unexpected DOM removal errors in test environments
console.warn('Failed to remove CSS element:', error)
}

this.css = null
}

/**
* Returns the editor storage.
*/
Expand Down
Loading