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

Merge pull request #13744 from overleaf/mj-big-projects

[cm6] Optimise getUpdatedProjection

GitOrigin-RevId: af321d3828185b245f557ab0e046851192c97296
ilkin-overleaf 3 лет назад
Родитель
Сommit
12bcdad850

+ 4 - 4
services/web/frontend/js/features/source-editor/utils/tree-operations/commands.ts

@@ -7,9 +7,9 @@ import { NodeIntersectsChangeFn, ProjectionItem } from './projection'
  * A projection of a command in the document
  */
 export class Command extends ProjectionItem {
-  title = ''
-  optionalArgCount = 0
-  requiredArgCount = 0
+  readonly title: string = ''
+  readonly optionalArgCount: number = 0
+  readonly requiredArgCount: number = 0
 }
 
 /**
@@ -62,7 +62,7 @@ export const enterNode = (
       argCountNumber--
     }
 
-    const thisCommand = {
+    const thisCommand: Readonly<Command> = {
       line: state.doc.lineAt(node.from).number,
       title: commandNameText,
       from: node.from,

+ 1 - 1
services/web/frontend/js/features/source-editor/utils/tree-operations/environments.ts

@@ -8,7 +8,7 @@ import { FigureData } from '../../extensions/figure-modal'
 const HUNDRED_MS = 100
 
 export class EnvironmentName extends ProjectionItem {
-  title = ''
+  readonly title: string = ''
 }
 
 export const enterNode = (

+ 2 - 2
services/web/frontend/js/features/source-editor/utils/tree-operations/outline.ts

@@ -15,8 +15,8 @@ export type Outline = {
  * A projection of a part of the file outline, typically a (sub)section heading
  */
 export class FlatOutlineItem extends ProjectionItem {
-  level = 0
-  title = ''
+  readonly level: number = 0
+  readonly title: string = ''
 }
 
 export type FlatOutline = FlatOutlineItem[]

+ 14 - 5
services/web/frontend/js/features/source-editor/utils/tree-operations/projection.ts

@@ -9,9 +9,9 @@ const FIVE_HUNDRED_MS = 500
  * A single item in the projection
  */
 export abstract class ProjectionItem {
-  from = 0
-  to = 0
-  line = 0
+  readonly from: number = 0
+  readonly to: number = 0
+  readonly line: number = 0
 }
 
 /* eslint-disable no-unused-vars */
@@ -47,11 +47,20 @@ export function updatePosition<T extends ProjectionItem>(
   }
   const { from, to } = item
   const newFrom = transaction.changes.mapPos(from)
+  const newTo = transaction.changes.mapPos(to)
+  const lineNumber = transaction.state.doc.lineAt(newFrom).number
+
+  if (newFrom === from && newTo === to && lineNumber === item.line) {
+    // Optimisation - if the item hasn't moved, don't create a new object
+    // If items are not immutable this can introduce problems
+    return item
+  }
+
   return {
     ...item,
     from: newFrom,
-    to: transaction.changes.mapPos(to),
-    line: transaction.state.doc.lineAt(newFrom).number,
+    to: newTo,
+    line: lineNumber,
   }
 }