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

[cm6] Optimise getUpdatedProjection

GitOrigin-RevId: af321d3828185b245f557ab0e046851192c97296
This commit is contained in:
ilkin-overleaf 2023-07-07 13:14:18 +03:00 committed by Copybot
parent 225de683c7
commit 12bcdad850
4 changed files with 21 additions and 12 deletions

View file

@ -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,

View file

@ -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 = (

View file

@ -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[]

View file

@ -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,
}
}