Track the size of the first note's delimiter, and use that (instead of hard coded value) to decide how big range we need to protect in the beginning

This commit is contained in:
Jonatan Heyman 2022-12-29 22:24:07 +01:00
parent c7282b3131
commit 1b3665b7b6

View File

@ -8,6 +8,9 @@ import { IterMode } from "@lezer/common";
import { INITIAL_DATA } from "./annotation.js"; import { INITIAL_DATA } from "./annotation.js";
// tracks the size of the first delimiter
let firstBlockDelimiterSize
function getBlocks(state) { function getBlocks(state) {
const blocks = []; const blocks = [];
syntaxTree(state).iterate({ syntaxTree(state).iterate({
@ -32,6 +35,7 @@ function getBlocks(state) {
}, },
mode: IterMode.IgnoreMounts, mode: IterMode.IgnoreMounts,
}); });
firstBlockDelimiterSize = blocks[0]?.delimiter.to
return blocks return blocks
} }
@ -42,6 +46,7 @@ const blockState = StateField.define({
update(blocks, transaction) { update(blocks, transaction) {
//console.log("blocks", blocks) //console.log("blocks", blocks)
if (transaction.docChanged) { if (transaction.docChanged) {
//console.log("updating block state", transaction)
return getBlocks(transaction.state); return getBlocks(transaction.state);
} }
//return widgets.map(transaction.changes); //return widgets.map(transaction.changes);
@ -80,6 +85,7 @@ const noteBlockWidget = () => {
block: true, block: true,
side: 0, side: 0,
}); });
//console.log("deco range:", delimiter.from === 0 ? delimiter.from : delimiter.from+1,delimiter.to-1)
widgets.push(deco.range( widgets.push(deco.range(
delimiter.from === 0 ? delimiter.from : delimiter.from+1, delimiter.from === 0 ? delimiter.from : delimiter.from+1,
delimiter.to-1, delimiter.to-1,
@ -115,7 +121,11 @@ const noteBlockWidget = () => {
function atomicRanges(view) { function atomicRanges(view) {
let builder = new RangeSetBuilder() let builder = new RangeSetBuilder()
view.state.facet(blockState).forEach(block => { view.state.facet(blockState).forEach(block => {
builder.add(block.delimiter.from, block.delimiter.to, {}) builder.add(
block.delimiter.from,
block.delimiter.to,
{},
)
}) })
return builder.finish() return builder.finish()
} }
@ -193,8 +203,8 @@ const blockLayer = () => {
const preventFirstBlockFromBeingDeleted = EditorState.changeFilter.of((tr) => { const preventFirstBlockFromBeingDeleted = EditorState.changeFilter.of((tr) => {
if (!tr.annotations.some(a => a.value === INITIAL_DATA)) { if (!tr.annotations.some(a => a.value === INITIAL_DATA) && firstBlockDelimiterSize) {
return [-1,11] return [0, firstBlockDelimiterSize]
} }
}) })
@ -202,15 +212,18 @@ const preventFirstBlockFromBeingDeleted = EditorState.changeFilter.of((tr) => {
* Transaction filter to prevent the selection from being before the first block * Transaction filter to prevent the selection from being before the first block
*/ */
const preventSelectionBeforeFirstBlock = EditorState.transactionFilter.of((tr) => { const preventSelectionBeforeFirstBlock = EditorState.transactionFilter.of((tr) => {
//console.log("transaction:", tr) if (!firstBlockDelimiterSize) {
return tr
}
tr?.selection?.ranges.forEach(range => { tr?.selection?.ranges.forEach(range => {
// change the selection to after the first block if the transaction sets the selection before the first block // change the selection to after the first block if the transaction sets the selection before the first block
const markerSize = 11 if (range && range.from < firstBlockDelimiterSize) {
if (range && range.from < markerSize) { range.from = firstBlockDelimiterSize
range.from = markerSize //console.log("changing the from selection to", markerSize)
} }
if (range && range.to < markerSize) { if (range && range.to < firstBlockDelimiterSize) {
range.to = markerSize range.to = firstBlockDelimiterSize
//console.log("changing the from selection to", markerSize)
} }
}) })
return tr return tr