Remove timeout parameter for getBlocksFromSyntaxTree() since we now expect the syntax tree to already be available.

This commit is contained in:
Jonatan Heyman 2024-07-07 16:04:54 +02:00
parent 29d4eb26cc
commit c3892163af

View File

@ -24,14 +24,11 @@ let firstBlockDelimiterSize
/** /**
* Return a list of blocks in the document from the syntax tree. * Return a list of blocks in the document from the syntax tree.
* syntaxTreeAvailable() should have been called before this function to ensure the syntax tree is available. * syntaxTreeAvailable() should have been called before this function to ensure the syntax tree is available.
* @param {*} state
* @param {*} timeout
* @returns
*/ */
function getBlocksFromSyntaxTree(state, timeout=50) { function getBlocksFromSyntaxTree(state) {
//const timer = startTimer() //const timer = startTimer()
const blocks = []; const blocks = [];
const tree = syntaxTree(state, state.doc.length, timeout) const tree = syntaxTree(state, state.doc.length)
if (tree) { if (tree) {
tree.iterate({ tree.iterate({
enter: (type) => { enter: (type) => {
@ -73,15 +70,9 @@ function getBlocksFromSyntaxTree(state, timeout=50) {
} }
/** /**
* Get the blocks for the document state. * Parse blocks from document's string contents using String.indexOf()
* If the syntax tree is available, we'll extract the blocks from that. Otherwise
* the blocks are parsed from the string contents of the document, which is much faster
* than waiting for the tree parsing to finnish.
*/ */
function getBlocks(state) { function getBlocksFromString(state) {
if (syntaxTreeAvailable(state, state.doc.length)) {
return getBlocksFromSyntaxTree(state)
}
//const timer = startTimer() //const timer = startTimer()
const blocks = [] const blocks = []
const doc = state.doc const doc = state.doc
@ -137,13 +128,27 @@ function getBlocks(state) {
blocks.push(block); blocks.push(block);
pos = blockEnd; pos = blockEnd;
} }
//console.log("getBlocks (string parsing) took", timer(), "ms") //console.log("getBlocksFromString() took", timer(), "ms")
return blocks; return blocks;
} }
/**
* Get the blocks from the document state.
* If the syntax tree is available, we'll extract the blocks from that. Otherwise
* the blocks are parsed from the string contents of the document, which is much faster
* than waiting for the tree parsing to finnish.
*/
function getBlocks(state) {
if (syntaxTreeAvailable(state, state.doc.length)) {
return getBlocksFromSyntaxTree(state)
} else {
return getBlocksFromString(state)
}
}
export const blockState = StateField.define({ export const blockState = StateField.define({
create(state) { create(state) {
return getBlocks(state, 1000); return getBlocks(state);
}, },
update(blocks, transaction) { update(blocks, transaction) {
// if blocks are empty it likely means we didn't get a parsed syntax tree, and then we want to update // if blocks are empty it likely means we didn't get a parsed syntax tree, and then we want to update