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
914const RTL = '\u0591-\u07FF\uFB1D-\uFDFD\uFE70-\uFEFC'
1015const 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