Handle the fact that the syntax tree is being parsed in a separate thread, which can lead to broken UI on slow environments

This commit is contained in:
Jonatan Heyman 2023-01-13 15:27:26 +01:00
parent 8867937567
commit f20c09ce19

View File

@ -13,8 +13,9 @@ let firstBlockDelimiterSize
function getBlocks(state) { function getBlocks(state) {
const blocks = []; const blocks = [];
const tree = ensureSyntaxTree(state, state.doc.length)
ensureSyntaxTree(state, state.doc.length).iterate({ if (tree) {
tree.iterate({
enter: (type) => { enter: (type) => {
if (type.type.id == Document || type.type.id == Note) { if (type.type.id == Document || type.type.id == Note) {
return true return true
@ -44,6 +45,7 @@ function getBlocks(state) {
mode: IterMode.IgnoreMounts, mode: IterMode.IgnoreMounts,
}); });
firstBlockDelimiterSize = blocks[0]?.delimiter.to firstBlockDelimiterSize = blocks[0]?.delimiter.to
}
return blocks return blocks
} }
@ -52,8 +54,9 @@ export const blockState = StateField.define({
return getBlocks(state); return getBlocks(state);
}, },
update(blocks, transaction) { update(blocks, transaction) {
//console.log("blocks", blocks) // if blocks are empty it likely means we didn't get a parsed syntax tree, and then we want to update
if (transaction.docChanged) { // the blocks on all updates (and not just document changes)
if (transaction.docChanged || blocks.length === 0) {
//console.log("updating block state", transaction) //console.log("updating block state", transaction)
return getBlocks(transaction.state); return getBlocks(transaction.state);
} }
@ -115,7 +118,9 @@ const noteBlockWidget = () => {
return decorate(state); return decorate(state);
}, },
update(widgets, transaction) { update(widgets, transaction) {
if (transaction.docChanged) { // if widgets are empty it likely means we didn't get a parsed syntax tree, and then we want to update
// the decorations on all updates (and not just document changes)
if (transaction.docChanged || widgets.isEmpty) {
return decorate(transaction.state); return decorate(transaction.state);
} }