From 02b2e2ad8971ef25462e480a522e3d78a39eebc0 Mon Sep 17 00:00:00 2001 From: ilkin-overleaf <100852799+ilkin-overleaf@users.noreply.github.com> Date: Fri, 7 Jul 2023 13:14:18 +0300 Subject: [PATCH] Merge pull request #13744 from overleaf/mj-big-projects [cm6] Optimise getUpdatedProjection GitOrigin-RevId: af321d3828185b245f557ab0e046851192c97296 --- .../utils/tree-operations/commands.ts | 8 ++++---- .../utils/tree-operations/environments.ts | 2 +- .../utils/tree-operations/outline.ts | 4 ++-- .../utils/tree-operations/projection.ts | 19 ++++++++++++++----- 4 files changed, 21 insertions(+), 12 deletions(-) diff --git a/services/web/frontend/js/features/source-editor/utils/tree-operations/commands.ts b/services/web/frontend/js/features/source-editor/utils/tree-operations/commands.ts index 80c5e71a06..97123735d1 100644 --- a/services/web/frontend/js/features/source-editor/utils/tree-operations/commands.ts +++ b/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 = { line: state.doc.lineAt(node.from).number, title: commandNameText, from: node.from, diff --git a/services/web/frontend/js/features/source-editor/utils/tree-operations/environments.ts b/services/web/frontend/js/features/source-editor/utils/tree-operations/environments.ts index fe754ff66c..d9287542a4 100644 --- a/services/web/frontend/js/features/source-editor/utils/tree-operations/environments.ts +++ b/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 = ( diff --git a/services/web/frontend/js/features/source-editor/utils/tree-operations/outline.ts b/services/web/frontend/js/features/source-editor/utils/tree-operations/outline.ts index e353df50ce..d43f1d2bd6 100644 --- a/services/web/frontend/js/features/source-editor/utils/tree-operations/outline.ts +++ b/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[] diff --git a/services/web/frontend/js/features/source-editor/utils/tree-operations/projection.ts b/services/web/frontend/js/features/source-editor/utils/tree-operations/projection.ts index 1614ee8df1..55c191784f 100644 --- a/services/web/frontend/js/features/source-editor/utils/tree-operations/projection.ts +++ b/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( } 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, } }