Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
"@tiptap/vue-3": "^3.30.0",
"@vueuse/shared": "^14.4.0",
"debounce": "^3.0.0",
"diff": "^8.0.3",
"escape-html": "^1.0.3",
"highlight.js": "^11.11.1",
"katex": "^0.18.1",
Expand Down
242 changes: 242 additions & 0 deletions src/comparison/comparisonDocumentIndex.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,242 @@
/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

import type { Node } from '@tiptap/pm/model'
import type { ComparisonRange } from './markdownComparisonTypes.ts'

export interface LocatedComparisonNode {
node: Node
path: readonly number[]
index: number
from: number
to: number
parent: LocatedComparisonNode | null
children: readonly LocatedComparisonNode[]
}

export interface ComparisonDocumentIndex {
children: readonly LocatedComparisonNode[]
nodes: readonly LocatedComparisonNode[]
nodeAtPath: (path: readonly number[]) => LocatedComparisonNode
}

export interface ComparisonNodeSearchAudit {
examinedNodes: number
}

interface MutableLocatedComparisonNode extends Omit<LocatedComparisonNode, 'children'> {
children: MutableLocatedComparisonNode[]
}

/**
* Build one reusable position and path index for an immutable document.
*
* @param doc Immutable ProseMirror document
* @param audit Optional operation counter for structural regression tests
*/
export function createComparisonDocumentIndex(
doc: Node,
audit?: ComparisonNodeSearchAudit,
): ComparisonDocumentIndex {
const nodes: MutableLocatedComparisonNode[] = []
const byPath = new Map<string, MutableLocatedComparisonNode>()
const locateChildren = (
parentNode: Node,
parentLocation: MutableLocatedComparisonNode | null,
parentPath: readonly number[],
contentFrom: number,
) => {
const children: MutableLocatedComparisonNode[] = []
parentNode.forEach((node, offset, index) => {
if (audit) {
audit.examinedNodes++
}
const from = contentFrom + offset
const path = [...parentPath, index]
const location: MutableLocatedComparisonNode = {
node,
path,
index,
from,
to: from + node.nodeSize,
parent: parentLocation,
children: [],
}
children.push(location)
nodes.push(location)
byPath.set(pathKey(path), location)
if (!node.isLeaf) {
location.children = locateChildren(node, location, path, from + 1)
}
})
return children
}
const children = locateChildren(doc, null, [], 0)
return {
children,
nodes,
nodeAtPath(path) {
const location = byPath.get(pathKey(path))
if (!location) {
throw new Error(`Comparison document path does not exist: ${path.join('.')}`)
}
return location
},
}
}

/**
* Find touched nodes without scanning unrelated siblings.
*
* @param range Absolute document range
* @param roots Smallest known roots for the range
* @param audit Optional operation counter for structural regression tests
*/
export function findComparisonNodes(
range: ComparisonRange,
roots: readonly LocatedComparisonNode[],
audit?: ComparisonNodeSearchAudit,
) {
const found = new Map<string, LocatedComparisonNode>()
const add = (location: LocatedComparisonNode) => found.set(pathKey(location.path), location)
const visit = (location: LocatedComparisonNode) => {
if (audit) {
audit.examinedNodes++
}
if (!touches(location, range)) {
return
}
add(location)
visitChildren(location.children, range, visit, audit)
}
for (const root of minimalRoots(roots)) {
for (let ancestor = root.parent; ancestor; ancestor = ancestor.parent) {
add(ancestor)
}
visit(root)
}
return [...found.values()].toSorted((a, b) => a.from - b.from || a.path.length - b.path.length)
}

/**
* Read text from the known roots instead of resolving the range from the document root.
*
* @param range Absolute document range
* @param roots Known range roots
*/
export function comparisonRangeText(range: ComparisonRange, roots: readonly LocatedComparisonNode[]) {
if (range.from === range.to) {
return ''
}
return minimalRoots(roots)
.filter((root) => touches(root, range))
.map((root) => textFromRoot(root, range))
.join('\n')
}

/**
* Visit child nodes that can intersect the requested range.
*
* @param children Candidate child nodes
* @param range Absolute document range
* @param visit Callback for each matching node
* @param audit Optional operation counter for structural regression tests
*/
function visitChildren(
children: readonly LocatedComparisonNode[],
range: ComparisonRange,
visit: (location: LocatedComparisonNode) => void,
audit?: ComparisonNodeSearchAudit,
) {
let lower = 0
let upper = children.length
while (lower < upper) {
const middle = (lower + upper) >>> 1
if (audit) {
audit.examinedNodes++
}
const beforeRange = range.from === range.to
? children[middle]!.to < range.from
: children[middle]!.to <= range.from
if (beforeRange) {
lower = middle + 1
} else {
upper = middle
}
}
for (let index = lower; index < children.length; index++) {
const child = children[index]!
if (audit) {
audit.examinedNodes++
}
if (range.from === range.to ? child.from > range.from : child.from >= range.to) {
break
}
visit(child)
}
}

/**
* Check whether a located node intersects a document range.
*
* @param location Located document node
* @param range Absolute document range
*/
function touches(location: LocatedComparisonNode, range: ComparisonRange) {
return range.from === range.to
? range.from >= location.from && range.from <= location.to
: range.from < location.to && range.to > location.from
}

/**
* Remove roots whose ancestor is already present.
*
* @param roots Known range roots
*/
function minimalRoots(roots: readonly LocatedComparisonNode[]) {
const rootPaths = new Set(roots.map(({ path }) => pathKey(path)))
return roots.filter((root) => {
for (let ancestor = root.parent; ancestor; ancestor = ancestor.parent) {
if (rootPaths.has(pathKey(ancestor.path))) {
return false
}
}
return true
})
}

/**
* Read the requested text from one known root.
*
* @param root Known range root
* @param range Absolute document range
*/
function textFromRoot(root: LocatedComparisonNode, range: ComparisonRange) {
if (root.node.isText) {
return root.node.text?.slice(
Math.max(0, range.from - root.from),
Math.min(root.node.nodeSize, range.to - root.from),
) ?? ''
}
if (root.node.isLeaf) {
return '\ufffc'
}
const contentFrom = root.from + 1
return root.node.textBetween(
Math.max(0, range.from - contentFrom),
Math.min(root.node.content.size, range.to - contentFrom),
'\n',
'\ufffc',
)
}

/**
* Encode a document path for map lookup.
*
* @param path Document child-index path
*/
function pathKey(path: readonly number[]) {
return path.join('.')
}
45 changes: 45 additions & 0 deletions src/comparison/comparisonDocumentLocation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

/**
* Center one comparison target inside its own document scroller.
*
* @param pane Side-specific document pane
* @param scroller Side-specific document scroller
* @param id Opaque comparison descriptor ID
* @param behavior Motion preference
*/
export function locateComparisonTarget(
pane: HTMLElement | null,
scroller: HTMLElement | null,
id: string,
behavior: ScrollBehavior,
) {
if (!pane || !scroller || !pane.contains(scroller)) {
return false
}
if (pane.hidden || pane.style.display === 'none') {
return false
}
const target = [...pane.querySelectorAll<HTMLElement>('[data-comparison-change]')]
.find((element) => element.dataset.comparisonChange === id)
if (!target) {
return false
}

const scrollerRect = scroller.getBoundingClientRect()
const targetRect = target.getBoundingClientRect()
const centeredTop = scroller.scrollTop
+ targetRect.top
- scrollerRect.top
- (scroller.clientHeight - targetRect.height) / 2
const maximumTop = Math.max(0, scroller.scrollHeight - scroller.clientHeight)
scroller.scrollTo({
behavior,
left: scroller.scrollLeft,
top: Math.min(Math.max(0, centeredTop), maximumTop),
})
return true
}
59 changes: 59 additions & 0 deletions src/comparison/comparisonEditorLifecycle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

import type { EditorState, Plugin } from '@tiptap/pm/state'
import type { Editor } from '@tiptap/vue-3'

const INITIALIZATION_ERROR = 'Comparison editor plugin initialization failed'
const stateOnlyEditors = new WeakSet<Editor>()

/**
* Register one comparison editor created without a view.
*
* @param editor State-only comparison editor
*/
export function registerStateOnlyComparisonEditor(editor: Editor) {
if (editor.options.element !== null || editor.contentComponent !== null || editor.appContext !== null) {
throw new Error(INITIALIZATION_ERROR)
}
stateOnlyEditors.add(editor)
}

/**
* Register one comparison plugin against the live mounted state.
*
* @param editor Mounted comparison editor
* @param plugin Comparison plugin
*/
export function registerMountedPlugin(editor: Editor, plugin: Plugin): EditorState {
const previousPlugins = new Set(editor.view.state.plugins)
let nextState: EditorState
try {
nextState = editor.registerPlugin(plugin, (newPlugin) => [
...editor.view.state.plugins.filter((item) => item.spec.key !== newPlugin.spec.key),
newPlugin,
])
} catch (error) {
throw new Error(INITIALIZATION_ERROR, { cause: error })
}
if ([...previousPlugins].some((item) => !nextState.plugins.includes(item))
|| nextState.plugins.filter((item) => item === plugin).length !== 1
|| nextState !== editor.view.state
|| editor.state !== nextState) {
throw new Error(INITIALIZATION_ERROR)
}
return nextState
}

/**
* Consume the one-shot state-only registration before the visible mount.
*
* @param editor State-only comparison editor
*/
export function consumeStateOnlyComparisonEditor(editor: Editor) {
if (!stateOnlyEditors.delete(editor)) {
throw new Error(INITIALIZATION_ERROR)
}
}
Loading
Loading