Skip to content

Commit a10882a

Browse files
committed
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]>
1 parent 35a9c53 commit a10882a

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

src/extensions/TextDirection.ts

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,13 @@
33
* SPDX-License-Identifier: MIT
44
*/
55

6-
import { Extension } from '@tiptap/core'
7-
import { Plugin, PluginKey } from '@tiptap/pm/state'
6+
import {
7+
Extension,
8+
combineTransactionSteps,
9+
findChildrenInRange,
10+
getChangedRanges,
11+
} from '@tiptap/core'
12+
import { Plugin, PluginKey, Transaction } from '@tiptap/pm/state'
813

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

5661
let modified = false
57-
const tr = newState.tr
62+
const { tr } = newState
63+
const transform = combineTransactionSteps(
64+
oldState.doc,
65+
transactions as Transaction[],
66+
)
67+
const changes = getChangedRanges(transform)
68+
5869
tr.setMeta('addToHistory', false)
5970

60-
newState.doc.descendants((node, pos) => {
61-
if (types.includes(node.type.name)) {
71+
changes.forEach(({ newRange }) => {
72+
const nodes = findChildrenInRange(newState.doc, newRange, (node) =>
73+
types.includes(node.type.name),
74+
)
75+
76+
nodes.forEach(({ node, pos }) => {
6277
if (node.attrs.dir !== null && node.textContent.length > 0) {
6378
return
6479
}
@@ -74,7 +89,7 @@ function TextDirectionPlugin({ types }: { types: string[] }) {
7489
tr.addStoredMark(mark)
7590
}
7691
modified = true
77-
}
92+
})
7893
})
7994

8095
return modified ? tr : null

0 commit comments

Comments
 (0)