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