Просмотр исходного кода

[cm6] fix toggling ranges next to command (#14183)

GitOrigin-RevId: 4959419a09415b202dc7ce2271e9691d2c2387d4
Domagoj Kriskovic 3 лет назад
Родитель
Сommit
f37ae7a5f8

+ 9 - 1
services/web/frontend/js/features/source-editor/commands/ranges.ts

@@ -467,7 +467,15 @@ export function toggleRanges(
               range.to === view.state.doc.length ? -1 : 1
             )
 
-        if (ancestorAtStartOfRange !== ancestorAtEndOfRange) {
+        const tree = ensureSyntaxTree(view.state, 1000)
+        const nodeAtFrom = tree?.resolveInner(range.from, 1)
+        const nodeAtTo = tree?.resolveInner(range.to, -1)
+        const isSingleNodeSelected = nodeAtFrom === nodeAtTo
+
+        if (
+          !isSingleNodeSelected &&
+          ancestorAtStartOfRange !== ancestorAtEndOfRange
+        ) {
           // But handle the exception of case 8
           const ancestorAtStartIsWrappingCommand =
             ancestorAtStartOfRange &&

+ 24 - 0
services/web/test/frontend/features/source-editor/commands/ranges.test.ts

@@ -196,4 +196,28 @@ describe('toggleRanges', function () {
       })
     })
   })
+
+  it('still formats text next to a command', function () {
+    const cm = new CodemirrorTestSession(['<item>\\foo'])
+    cm.applyCommand(BOLD_COMMAND)
+    expect(cm).line(1).to.equal('\\textbf{item}\\foo')
+  })
+
+  it('still formats part of a text next to command', function () {
+    const cm = new CodemirrorTestSession(['hello <world>\\foo'])
+    cm.applyCommand(BOLD_COMMAND)
+    expect(cm).line(1).to.equal('hello \\textbf{world}\\foo')
+  })
+
+  it('still formats command without arguments', function () {
+    const cm = new CodemirrorTestSession(['\\item<\\foo>'])
+    cm.applyCommand(BOLD_COMMAND)
+    expect(cm).line(1).to.equal('\\item\\textbf{<\\foo>}')
+  })
+
+  it('skips formatting if in the middle of two commands', function () {
+    const cm = new CodemirrorTestSession(['\\f<oo\\b>ar'])
+    cm.applyCommand(BOLD_COMMAND)
+    expect(cm).line(1).to.equal('\\foo\\bar')
+  })
 })