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

[visual] Avoid showing braces around section headings when editing the section title (#13464)

GitOrigin-RevId: 050663e752720993ccf1ab745a8b4250d54198fc
Alf Eaton 3 лет назад
Родитель
Сommit
db26446d76

+ 18 - 7
services/web/frontend/js/features/source-editor/extensions/visual/atomic-decorations.ts

@@ -45,6 +45,7 @@ import { InlineGraphicsWidget } from './visual-widgets/inline-graphics'
 import getMeta from '../../../../utils/meta'
 import { EditableGraphicsWidget } from './visual-widgets/editable-graphics'
 import { EditableInlineGraphicsWidget } from './visual-widgets/editable-inline-graphics'
+import { CloseBrace, OpenBrace } from '../../lezer-latex/latex.terms.mjs'
 
 type Options = {
   fileTreeManager: {
@@ -449,12 +450,16 @@ export const atomicDecorations = (options: Options) => {
             'SectioningCommand'
           )
           if (ancestorNode) {
-            const shouldShowBraces = !shouldDecorate(state, ancestorNode)
             // a section (or subsection, etc) command
             const argumentNode = ancestorNode.getChild('SectioningArgument')
             if (argumentNode) {
-              const braces = argumentNode.getChildren('$Brace')
-              if (braces.length !== 2) {
+              const openBrace = argumentNode.getChild(OpenBrace)
+              const closeBrace = argumentNode.getChild(CloseBrace)
+              if (!openBrace || !closeBrace) {
+                return false
+              }
+              const sectionCtrlSeqNode = ancestorNode.getChild('$CtrlSeq')
+              if (!sectionCtrlSeqNode) {
                 return false
               }
               const titleNode = argumentNode.getChild('LongArg')
@@ -466,17 +471,23 @@ export const atomicDecorations = (options: Options) => {
                 return false
               }
 
+              const showBraces =
+                selectionIntersects(state.selection, sectionCtrlSeqNode) ||
+                selectionIntersects(state.selection, openBrace) ||
+                selectionIntersects(state.selection, closeBrace)
+
               decorations.push(
                 Decoration.replace({
-                  widget: new BraceWidget(shouldShowBraces ? '}' : ''),
-                }).range(braces[1].from, braces[1].to)
+                  widget: new BraceWidget(showBraces ? '{' : ''),
+                }).range(nodeRef.from, titleNode.from)
               )
 
               decorations.push(
                 Decoration.replace({
-                  widget: new BraceWidget(shouldShowBraces ? '{' : ''),
-                }).range(nodeRef.from, titleNode.from)
+                  widget: new BraceWidget(showBraces ? '}' : ''),
+                }).range(closeBrace.from, closeBrace.to)
               )
+
               return false
             }
           }

+ 7 - 3
services/web/test/frontend/features/source-editor/components/codemirror-editor-visual.spec.tsx

@@ -151,9 +151,13 @@ describe('<CodeMirrorEditor/> in Rich Text mode', function () {
     cy.get('@first-line').should('have.text', `\\${command}{}`)
     cy.get('@first-line').type('{rightArrow} ')
     cy.get('@first-line').should('have.text', `\\${command}{} `)
-    // Press enter before closing brace
-    cy.get('@first-line').type('{Backspace}{leftArrow}title{leftArrow}{Enter}')
-    cy.get('@first-line').should('have.text', 'title')
+    // Type a section heading
+    cy.get('@first-line').type('{Backspace}{leftArrow}title')
+    cy.get('@first-line').should('have.text', '{title}') // braces are visible as cursor is adjacent
+    cy.get('@first-line').type('{leftArrow}')
+    cy.get('@first-line').should('have.text', 'title') // braces are hidden as cursor is not adjacent
+    cy.get('@first-line').type('{Enter}')
+    cy.get('@first-line').should('have.text', 'title') // braces are hidden as cursor is on the next line
     cy.get('@first-line').find(`.ol-cm-heading.ol-cm-command-${command}`)
   })