From 21ad8e08147370b02d7318af92fd6721fa1343fa Mon Sep 17 00:00:00 2001 From: Jonatan Heyman Date: Wed, 27 Dec 2023 12:52:48 +0100 Subject: [PATCH] Return the already parsed blocks in the blockState facet from Editor.getBlocks() instead of parsing the syntax tree again. Make the block extension's getBlocks() method private. --- src/editor/block/block.js | 4 +--- src/editor/editor.js | 4 ++-- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/src/editor/block/block.js b/src/editor/block/block.js index c212d5da..8e3cc07f 100644 --- a/src/editor/block/block.js +++ b/src/editor/block/block.js @@ -13,7 +13,7 @@ import { emptyBlockSelected } from "./select-all.js"; // tracks the size of the first delimiter let firstBlockDelimiterSize -export function getBlocks(state, timeout=50) { +function getBlocks(state, timeout=50) { const blocks = []; const tree = ensureSyntaxTree(state, state.doc.length, timeout) if (tree) { @@ -63,10 +63,8 @@ export const blockState = StateField.define({ // 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); } - //return widgets.map(transaction.changes); return blocks }, }) diff --git a/src/editor/editor.js b/src/editor/editor.js index 9a4a319f..0c287127 100644 --- a/src/editor/editor.js +++ b/src/editor/editor.js @@ -8,7 +8,7 @@ import { heynoteDark } from "./theme/dark.js" import { heynoteBase } from "./theme/base.js" import { customSetup } from "./setup.js" import { heynoteLang } from "./lang-heynote/heynote.js" -import { noteBlockExtension, blockLineNumbers, getBlocks } from "./block/block.js" +import { noteBlockExtension, blockLineNumbers, blockState } from "./block/block.js" import { heynoteEvent, SET_CONTENT } from "./annotation.js"; import { changeCurrentBlockLanguage, triggerCurrenciesLoaded } from "./block/commands.js" import { formatBlockContent } from "./block/format-code.js" @@ -128,7 +128,7 @@ export class HeynoteEditor { } getBlocks() { - return getBlocks(this.view.state) + return this.view.state.facet(blockState) } focus() {