mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2024-11-21 17:26:29 -05:00
fix(frontend): improve performance by reducing array constructions
Signed-off-by: Tilman Vatteroth <git@tilmanvatteroth.de>
This commit is contained in:
parent
28f04f461c
commit
ced4cd953c
6 changed files with 26 additions and 24 deletions
|
@ -35,9 +35,11 @@ export class YTextSyncViewPlugin implements PluginValue {
|
||||||
const [changes] = event.delta.reduce(
|
const [changes] = event.delta.reduce(
|
||||||
([changes, position], delta) => {
|
([changes, position], delta) => {
|
||||||
if (delta.insert !== undefined && typeof delta.insert === 'string') {
|
if (delta.insert !== undefined && typeof delta.insert === 'string') {
|
||||||
return [[...changes, { from: position, to: position, insert: delta.insert }], position]
|
changes.push({ from: position, to: position, insert: delta.insert })
|
||||||
|
return [changes, position]
|
||||||
} else if (delta.delete !== undefined) {
|
} else if (delta.delete !== undefined) {
|
||||||
return [[...changes, { from: position, to: position + delta.delete, insert: '' }], position + delta.delete]
|
changes.push({ from: position, to: position + delta.delete, insert: '' })
|
||||||
|
return [changes, position + delta.delete]
|
||||||
} else if (delta.retain !== undefined) {
|
} else if (delta.retain !== undefined) {
|
||||||
return [changes, position + delta.retain]
|
return [changes, position + delta.retain]
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -11,6 +11,7 @@ import { linter } from '@codemirror/lint'
|
||||||
import type { Extension } from '@codemirror/state'
|
import type { Extension } from '@codemirror/state'
|
||||||
import type { EditorView } from '@codemirror/view'
|
import type { EditorView } from '@codemirror/view'
|
||||||
import { useMemo } from 'react'
|
import { useMemo } from 'react'
|
||||||
|
import { useTranslation } from 'react-i18next'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The Linter interface.
|
* The Linter interface.
|
||||||
|
@ -32,11 +33,16 @@ const createLinterExtension = () =>
|
||||||
/**
|
/**
|
||||||
* Creates a codemirror linter that runs all markdown extension linters.
|
* Creates a codemirror linter that runs all markdown extension linters.
|
||||||
* Due to a bug in codemirror that breaks the "fix" buttons when switching themes, the extension is recreated if the app switches between dark and light mode.
|
* Due to a bug in codemirror that breaks the "fix" buttons when switching themes, the extension is recreated if the app switches between dark and light mode.
|
||||||
|
* To update translations the t function is also included.
|
||||||
*
|
*
|
||||||
* @return The build codemirror linter extension
|
* @return The build codemirror linter extension
|
||||||
*/
|
*/
|
||||||
export const useLinter = (): Extension => {
|
export const useLinter = (): Extension => {
|
||||||
const darkModeActivated = useDarkModeState()
|
const darkModeActivated = useDarkModeState()
|
||||||
|
const { t } = useTranslation()
|
||||||
|
|
||||||
return useMemo(() => (darkModeActivated ? createLinterExtension() : createLinterExtension()), [darkModeActivated])
|
return useMemo(
|
||||||
|
() => (darkModeActivated && !!t ? createLinterExtension() : createLinterExtension()),
|
||||||
|
[darkModeActivated, t]
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,19 +30,15 @@ export class SingleLineRegexLinter implements Linter {
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
lint(view: EditorView): Diagnostic[] {
|
lint(view: EditorView): Diagnostic[] {
|
||||||
return view.state.doc
|
const lines = view.state.doc.toString().split('\n')
|
||||||
.toString()
|
return lines
|
||||||
.split('\n')
|
.reduce((state, line, lineIndex, lines) => {
|
||||||
.reduce(
|
state[lineIndex] = {
|
||||||
(state, line, lineIndex, lines) => [
|
line,
|
||||||
...state,
|
startIndex: lineIndex === 0 ? 0 : state[lineIndex - 1].startIndex + lines[lineIndex - 1].length + 1
|
||||||
{
|
} as LineWithStartIndex
|
||||||
line,
|
return state
|
||||||
startIndex: lineIndex === 0 ? 0 : state[lineIndex - 1].startIndex + lines[lineIndex - 1].length + 1
|
}, new Array<LineWithStartIndex>(lines.length))
|
||||||
} as LineWithStartIndex
|
|
||||||
],
|
|
||||||
[] as LineWithStartIndex[]
|
|
||||||
)
|
|
||||||
.map(({ line, startIndex }) => ({
|
.map(({ line, startIndex }) => ({
|
||||||
lineStartIndex: startIndex,
|
lineStartIndex: startIndex,
|
||||||
regexResult: this.regex.exec(line)
|
regexResult: this.regex.exec(line)
|
||||||
|
|
|
@ -42,7 +42,8 @@ const processCommentNode = (node: DataNode): void => {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const dataAttribute of [...regexResult[2].matchAll(dataAttributesSyntax)]) {
|
const matches = [...regexResult[2].matchAll(dataAttributesSyntax)]
|
||||||
|
for (const dataAttribute of matches) {
|
||||||
const attributeName = dataAttribute[1]
|
const attributeName = dataAttribute[1]
|
||||||
const attributeValue = dataAttribute[2] ?? dataAttribute[3]
|
const attributeValue = dataAttribute[2] ?? dataAttribute[3]
|
||||||
if (attributeValue) {
|
if (attributeValue) {
|
||||||
|
|
|
@ -63,10 +63,7 @@ export class LineContentToLineIdMapper {
|
||||||
LineContentToLineIdMapper.changeIsNotChangingLines(change) ||
|
LineContentToLineIdMapper.changeIsNotChangingLines(change) ||
|
||||||
LineContentToLineIdMapper.changeIsAddingLines(change)
|
LineContentToLineIdMapper.changeIsAddingLines(change)
|
||||||
)
|
)
|
||||||
.reduce(
|
.flatMap((currentChange) => this.convertChangeToLinesWithIds(currentChange))
|
||||||
(previousLineKeys, currentChange) => [...previousLineKeys, ...this.convertChangeToLinesWithIds(currentChange)],
|
|
||||||
[] as LineWithId[]
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -12,7 +12,7 @@
|
||||||
*/
|
*/
|
||||||
export const calculateLineStartIndexes = (markdownContentLines: string[]): number[] => {
|
export const calculateLineStartIndexes = (markdownContentLines: string[]): number[] => {
|
||||||
return markdownContentLines.reduce((state, line, lineIndex, lines) => {
|
return markdownContentLines.reduce((state, line, lineIndex, lines) => {
|
||||||
const lastIndex = lineIndex === 0 ? 0 : state[lineIndex - 1] + lines[lineIndex - 1].length + 1
|
state[lineIndex] = lineIndex === 0 ? 0 : state[lineIndex - 1] + lines[lineIndex - 1].length + 1
|
||||||
return [...state, lastIndex]
|
return state
|
||||||
}, [] as number[])
|
}, new Array<number>(markdownContentLines.length))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue