Skip to content

Commit ab3fcb8

Browse files
mejo-backportbot[bot]
authored andcommitted
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 289e7d6 commit ab3fcb8

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 = 'A-Za-z\u00C0-\u00D6\u00D8-\u00F6'
@@ -53,11 +58,21 @@ function TextDirectionPlugin({ types }: { types: string[] }) {
5358
}
5459

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

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

7994
return modified ? tr : null

0 commit comments

Comments
 (0)