|
|
import { Path } from 'slate' |
|
|
import { Key } from 'slate-dom' |
|
|
import { |
|
|
Chunk, |
|
|
ChunkTree, |
|
|
ChunkLeaf, |
|
|
ChunkDescendant, |
|
|
ChunkAncestor, |
|
|
} from './types' |
|
|
|
|
|
type SavedPointer = |
|
|
| 'start' |
|
|
| { |
|
|
chunk: ChunkAncestor |
|
|
node: ChunkDescendant |
|
|
} |
|
|
|
|
|
export interface ChunkTreeHelperOptions { |
|
|
chunkSize: number |
|
|
debug?: boolean |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export class ChunkTreeHelper { |
|
|
|
|
|
|
|
|
|
|
|
private root: ChunkTree |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private chunkSize: number |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private debug: boolean |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private reachedEnd: boolean |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private pointerChunk: ChunkAncestor |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private pointerIndex: number |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private pointerIndexStack: number[] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private cachedPointerNode: ChunkDescendant | null | undefined |
|
|
|
|
|
constructor( |
|
|
chunkTree: ChunkTree, |
|
|
{ chunkSize, debug }: ChunkTreeHelperOptions |
|
|
) { |
|
|
this.root = chunkTree |
|
|
this.chunkSize = chunkSize |
|
|
|
|
|
this.debug = debug ?? false |
|
|
this.pointerChunk = chunkTree |
|
|
this.pointerIndex = -1 |
|
|
this.pointerIndexStack = [] |
|
|
this.reachedEnd = false |
|
|
this.validateState() |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public readLeaf(): ChunkLeaf | null { |
|
|
|
|
|
if (this.reachedEnd) return null |
|
|
|
|
|
|
|
|
while (true) { |
|
|
if (this.pointerIndex + 1 < this.pointerSiblings.length) { |
|
|
this.pointerIndex++ |
|
|
this.cachedPointerNode = undefined |
|
|
break |
|
|
} else if (this.pointerChunk.type === 'root') { |
|
|
this.reachedEnd = true |
|
|
return null |
|
|
} else { |
|
|
this.exitChunk() |
|
|
} |
|
|
} |
|
|
|
|
|
this.validateState() |
|
|
|
|
|
|
|
|
this.enterChunkUntilLeaf(false) |
|
|
|
|
|
return this.pointerNode as ChunkLeaf |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public returnToPreviousLeaf() { |
|
|
|
|
|
|
|
|
if (this.reachedEnd) { |
|
|
this.reachedEnd = false |
|
|
this.enterChunkUntilLeaf(true) |
|
|
return |
|
|
} |
|
|
|
|
|
|
|
|
while (true) { |
|
|
if (this.pointerIndex >= 1) { |
|
|
this.pointerIndex-- |
|
|
this.cachedPointerNode = undefined |
|
|
break |
|
|
} else if (this.pointerChunk.type === 'root') { |
|
|
this.pointerIndex = -1 |
|
|
return |
|
|
} else { |
|
|
this.exitChunk() |
|
|
} |
|
|
} |
|
|
|
|
|
this.validateState() |
|
|
|
|
|
|
|
|
this.enterChunkUntilLeaf(true) |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public insertBefore(leaves: ChunkLeaf[]) { |
|
|
this.returnToPreviousLeaf() |
|
|
this.insertAfter(leaves) |
|
|
this.readLeaf() |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public insertAfter(leaves: ChunkLeaf[]) { |
|
|
|
|
|
if (leaves.length === 0) return |
|
|
|
|
|
let beforeDepth = 0 |
|
|
let afterDepth = 0 |
|
|
|
|
|
|
|
|
|
|
|
while ( |
|
|
this.pointerChunk.type === 'chunk' && |
|
|
this.pointerIndex === this.pointerSiblings.length - 1 |
|
|
) { |
|
|
const remainingCapacity = this.chunkSize - this.pointerSiblings.length |
|
|
const toInsertCount = Math.min(remainingCapacity, leaves.length) |
|
|
|
|
|
if (toInsertCount > 0) { |
|
|
const leavesToInsert = leaves.splice(0, toInsertCount) |
|
|
this.rawInsertAfter(leavesToInsert, beforeDepth) |
|
|
} |
|
|
|
|
|
this.exitChunk() |
|
|
beforeDepth++ |
|
|
} |
|
|
|
|
|
if (leaves.length === 0) return |
|
|
|
|
|
|
|
|
|
|
|
const rawInsertPointer = this.savePointer() |
|
|
|
|
|
|
|
|
|
|
|
let finalPointer: SavedPointer | null = null |
|
|
|
|
|
|
|
|
if (this.readLeaf()) { |
|
|
|
|
|
|
|
|
while (this.pointerChunk.type === 'chunk' && this.pointerIndex === 0) { |
|
|
const remainingCapacity = this.chunkSize - this.pointerSiblings.length |
|
|
const toInsertCount = Math.min(remainingCapacity, leaves.length) |
|
|
|
|
|
if (toInsertCount > 0) { |
|
|
const leavesToInsert = leaves.splice(-toInsertCount, toInsertCount) |
|
|
|
|
|
|
|
|
this.pointerIndex = -1 |
|
|
this.cachedPointerNode = undefined |
|
|
this.rawInsertAfter(leavesToInsert, afterDepth) |
|
|
|
|
|
|
|
|
|
|
|
if (!finalPointer) { |
|
|
finalPointer = this.savePointer() |
|
|
} |
|
|
} |
|
|
|
|
|
this.exitChunk() |
|
|
afterDepth++ |
|
|
} |
|
|
} |
|
|
|
|
|
this.restorePointer(rawInsertPointer) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const minDepth = Math.max(beforeDepth, afterDepth) |
|
|
this.rawInsertAfter(leaves, minDepth) |
|
|
|
|
|
if (finalPointer) { |
|
|
this.restorePointer(finalPointer) |
|
|
} |
|
|
|
|
|
this.validateState() |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public remove() { |
|
|
this.pointerSiblings.splice(this.pointerIndex--, 1) |
|
|
this.cachedPointerNode = undefined |
|
|
|
|
|
if ( |
|
|
this.pointerSiblings.length === 0 && |
|
|
this.pointerChunk.type === 'chunk' |
|
|
) { |
|
|
this.exitChunk() |
|
|
this.remove() |
|
|
} else { |
|
|
this.invalidateChunk() |
|
|
} |
|
|
|
|
|
this.validateState() |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public invalidateChunk() { |
|
|
for (let c = this.pointerChunk; c.type === 'chunk'; c = c.parent) { |
|
|
this.root.modifiedChunks.add(c) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private get atStart() { |
|
|
return this.pointerChunk.type === 'root' && this.pointerIndex === -1 |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private get pointerSiblings(): ChunkDescendant[] { |
|
|
return this.pointerChunk.children |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private getPointerNode(): ChunkDescendant | null { |
|
|
if (this.reachedEnd || this.pointerIndex === -1) { |
|
|
return null |
|
|
} |
|
|
|
|
|
return this.pointerSiblings[this.pointerIndex] |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private get pointerNode(): ChunkDescendant | null { |
|
|
if (this.cachedPointerNode !== undefined) return this.cachedPointerNode |
|
|
const pointerNode = this.getPointerNode() |
|
|
this.cachedPointerNode = pointerNode |
|
|
return pointerNode |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private getChunkPath(chunk: ChunkAncestor): number[] | null { |
|
|
const path: number[] = [] |
|
|
|
|
|
for (let c = chunk; c.type === 'chunk'; c = c.parent) { |
|
|
const index = c.parent.children.indexOf(c) |
|
|
|
|
|
|
|
|
if (index === -1) { |
|
|
return null |
|
|
} |
|
|
|
|
|
path.unshift(index) |
|
|
} |
|
|
|
|
|
return path |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private savePointer(): SavedPointer { |
|
|
if (this.atStart) return 'start' |
|
|
|
|
|
|
|
|
if (!this.pointerNode) { |
|
|
throw new Error('Cannot save pointer when pointerNode is null') |
|
|
} |
|
|
|
|
|
return { |
|
|
chunk: this.pointerChunk, |
|
|
node: this.pointerNode, |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private restorePointer(savedPointer: SavedPointer) { |
|
|
if (savedPointer === 'start') { |
|
|
this.pointerChunk = this.root |
|
|
this.pointerIndex = -1 |
|
|
this.pointerIndexStack = [] |
|
|
this.reachedEnd = false |
|
|
this.cachedPointerNode = undefined |
|
|
return |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const { chunk, node } = savedPointer |
|
|
const index = chunk.children.indexOf(node) |
|
|
|
|
|
|
|
|
if (index === -1) { |
|
|
throw new Error( |
|
|
'Cannot restore point because saved node is no longer in saved chunk' |
|
|
) |
|
|
} |
|
|
|
|
|
const indexStack = this.getChunkPath(chunk) |
|
|
|
|
|
|
|
|
if (!indexStack) { |
|
|
throw new Error( |
|
|
'Cannot restore point because saved chunk is no longer connected to root' |
|
|
) |
|
|
} |
|
|
|
|
|
this.pointerChunk = chunk |
|
|
this.pointerIndex = index |
|
|
this.pointerIndexStack = indexStack |
|
|
this.reachedEnd = false |
|
|
this.cachedPointerNode = node |
|
|
this.validateState() |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private enterChunk(end: boolean) { |
|
|
|
|
|
if (this.pointerNode?.type !== 'chunk') { |
|
|
throw new Error('Cannot enter non-chunk') |
|
|
} |
|
|
|
|
|
this.pointerIndexStack.push(this.pointerIndex) |
|
|
this.pointerChunk = this.pointerNode |
|
|
this.pointerIndex = end ? this.pointerSiblings.length - 1 : 0 |
|
|
this.cachedPointerNode = undefined |
|
|
this.validateState() |
|
|
|
|
|
|
|
|
if (this.pointerChunk.children.length === 0) { |
|
|
throw new Error('Cannot enter empty chunk') |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private enterChunkUntilLeaf(end: boolean) { |
|
|
while (this.pointerNode?.type === 'chunk') { |
|
|
this.enterChunk(end) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private exitChunk() { |
|
|
|
|
|
if (this.pointerChunk.type === 'root') { |
|
|
throw new Error('Cannot exit root') |
|
|
} |
|
|
|
|
|
const previousPointerChunk = this.pointerChunk |
|
|
this.pointerChunk = previousPointerChunk.parent |
|
|
this.pointerIndex = this.pointerIndexStack.pop()! |
|
|
this.cachedPointerNode = undefined |
|
|
this.validateState() |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private rawInsertAfter(leaves: ChunkLeaf[], minDepth: number) { |
|
|
if (leaves.length === 0) return |
|
|
|
|
|
const groupIntoChunks = ( |
|
|
leaves: ChunkLeaf[], |
|
|
parent: ChunkAncestor, |
|
|
perChunk: number |
|
|
): ChunkDescendant[] => { |
|
|
if (perChunk === 1) return leaves |
|
|
const chunks: Chunk[] = [] |
|
|
|
|
|
for (let i = 0; i < this.chunkSize; i++) { |
|
|
const chunkNodes = leaves.slice(i * perChunk, (i + 1) * perChunk) |
|
|
if (chunkNodes.length === 0) break |
|
|
|
|
|
const chunk: Chunk = { |
|
|
type: 'chunk', |
|
|
key: new Key(), |
|
|
parent, |
|
|
children: [], |
|
|
} |
|
|
|
|
|
chunk.children = groupIntoChunks( |
|
|
chunkNodes, |
|
|
chunk, |
|
|
perChunk / this.chunkSize |
|
|
) |
|
|
chunks.push(chunk) |
|
|
} |
|
|
|
|
|
return chunks |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const newTotal = this.pointerSiblings.length + leaves.length |
|
|
let depthForTotal = 0 |
|
|
|
|
|
for (let i = this.chunkSize; i < newTotal; i *= this.chunkSize) { |
|
|
depthForTotal++ |
|
|
} |
|
|
|
|
|
|
|
|
const depth = Math.max(depthForTotal, minDepth) |
|
|
const perTopLevelChunk = Math.pow(this.chunkSize, depth) |
|
|
|
|
|
const chunks = groupIntoChunks(leaves, this.pointerChunk, perTopLevelChunk) |
|
|
this.pointerSiblings.splice(this.pointerIndex + 1, 0, ...chunks) |
|
|
this.pointerIndex += chunks.length |
|
|
this.cachedPointerNode = undefined |
|
|
this.invalidateChunk() |
|
|
this.validateState() |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private validateState() { |
|
|
if (!this.debug) return |
|
|
|
|
|
const validateDescendant = (node: ChunkDescendant) => { |
|
|
if (node.type === 'chunk') { |
|
|
const { parent, children } = node |
|
|
|
|
|
if (!parent.children.includes(node)) { |
|
|
throw new Error( |
|
|
`Debug: Chunk ${node.key.id} has an incorrect parent property` |
|
|
) |
|
|
} |
|
|
|
|
|
children.forEach(validateDescendant) |
|
|
} |
|
|
} |
|
|
|
|
|
this.root.children.forEach(validateDescendant) |
|
|
|
|
|
if ( |
|
|
this.cachedPointerNode !== undefined && |
|
|
this.cachedPointerNode !== this.getPointerNode() |
|
|
) { |
|
|
throw new Error( |
|
|
'Debug: The cached pointer is incorrect and has not been invalidated' |
|
|
) |
|
|
} |
|
|
|
|
|
const actualIndexStack = this.getChunkPath(this.pointerChunk) |
|
|
|
|
|
if (!actualIndexStack) { |
|
|
throw new Error('Debug: The pointer chunk is not connected to the root') |
|
|
} |
|
|
|
|
|
if (!Path.equals(this.pointerIndexStack, actualIndexStack)) { |
|
|
throw new Error( |
|
|
`Debug: The cached index stack [${this.pointerIndexStack.join( |
|
|
', ' |
|
|
)}] does not match the path of the pointer chunk [${actualIndexStack.join( |
|
|
', ' |
|
|
)}]` |
|
|
) |
|
|
} |
|
|
} |
|
|
} |
|
|
|