Skip to content
Merged
Changes from 1 commit
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
Next Next commit
fix(TextDirection): Only regard changed nodes in appendTransaction
No need to iterate over all nodes of the document each time.

Upstream PR: amirhhashemi/tiptap-text-direction#23

Signed-off-by: Jonas <[email protected]>
  • Loading branch information
mejo- committed Jul 29, 2025
commit a10882a78ce6d26482fad7242ee7f9ebe130560c
27 changes: 21 additions & 6 deletions src/extensions/TextDirection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@
* SPDX-License-Identifier: MIT
*/

import { Extension } from '@tiptap/core'
import { Plugin, PluginKey } from '@tiptap/pm/state'
import {
Extension,
combineTransactionSteps,
findChildrenInRange,
getChangedRanges,
} from '@tiptap/core'
import { Plugin, PluginKey, Transaction } from '@tiptap/pm/state'

const RTL = '\u0591-\u07FF\uFB1D-\uFDFD\uFE70-\uFEFC'
const LTR =
Expand Down Expand Up @@ -54,11 +59,21 @@ function TextDirectionPlugin({ types }: { types: string[] }) {
}

let modified = false
const tr = newState.tr
const { tr } = newState
const transform = combineTransactionSteps(
oldState.doc,
transactions as Transaction[],
)
const changes = getChangedRanges(transform)

tr.setMeta('addToHistory', false)

newState.doc.descendants((node, pos) => {
if (types.includes(node.type.name)) {
changes.forEach(({ newRange }) => {
const nodes = findChildrenInRange(newState.doc, newRange, (node) =>
types.includes(node.type.name),
)

nodes.forEach(({ node, pos }) => {
if (node.attrs.dir !== null && node.textContent.length > 0) {
return
}
Expand All @@ -74,7 +89,7 @@ function TextDirectionPlugin({ types }: { types: string[] }) {
tr.addStoredMark(mark)
}
modified = true
}
})
})

return modified ? tr : null
Expand Down