Set note background layers' width to 100% in CSS instead of measuring the editor width in a requestMeasure() call

This commit is contained in:
Jonatan Heyman 2022-12-30 12:52:32 +01:00
parent 1f4c63cff1
commit 9aff278c80
2 changed files with 41 additions and 55 deletions

View File

@ -1,6 +1,6 @@
import { ViewPlugin, EditorView, Decoration, WidgetType, lineNumbers } from "@codemirror/view" import { ViewPlugin, EditorView, Decoration, WidgetType, lineNumbers } from "@codemirror/view"
import { layer, RectangleMarker } from "@codemirror/view" import { layer, RectangleMarker } from "@codemirror/view"
import { EditorState, RangeSetBuilder, StateField } from "@codemirror/state"; import { EditorState, RangeSetBuilder, StateField, Facet , StateEffect} from "@codemirror/state";
import { RangeSet } from "@codemirror/rangeset"; import { RangeSet } from "@codemirror/rangeset";
import { syntaxTree } from "@codemirror/language" import { syntaxTree } from "@codemirror/language"
import { Note, Document, NoteDelimiter } from "./lang-heynote/parser.terms.js" import { Note, Document, NoteDelimiter } from "./lang-heynote/parser.terms.js"
@ -148,63 +148,47 @@ const atomicNoteBlock = ViewPlugin.fromClass(
} }
) )
const blockLayer = () => {
let editorWidth = 0; const blockLayer = layer({
const measureEditorWidth = EditorView.updateListener.of((update) => { above: false,
if (update.geometryChanged) {
update.view.requestMeasure({ markers(view) {
read(a) { const markers = []
const gutterWidth = update.view.contentDOM.previousSibling.clientWidth let idx = 0
editorWidth = update.view.contentDOM.clientWidth + gutterWidth //console.log("visible ranges:", view.visibleRanges[0].from, view.visibleRanges[0].to, view.visibleRanges.length)
} function rangesOverlaps(range1, range2) {
}) return range1.from <= range2.to && range2.from <= range1.to
} }
}) const blocks = view.state.facet(blockState)
blocks.forEach(block => {
const layerExtension = layer({ // make sure the block is visible
above: false, if (!view.visibleRanges.some(range => rangesOverlaps(block.content, range))) {
idx++;
markers(view) { return
const markers = []
let idx = 0
//console.log("visible ranges:", view.visibleRanges[0].from, view.visibleRanges[0].to, view.visibleRanges.length)
function rangesOverlaps(range1, range2) {
return range1.from <= range2.to && range2.from <= range1.to
} }
const blocks = view.state.facet(blockState) const fromCoordsTop = view.coordsAtPos(Math.max(block.content.from, view.visibleRanges[0].from)).top
blocks.forEach(block => { let toCoordsBottom = view.coordsAtPos(Math.min(block.content.to, view.visibleRanges[view.visibleRanges.length - 1].to)).bottom
// make sure the block is visible if (idx === blocks.length - 1) {
if (!view.visibleRanges.some(range => rangesOverlaps(block.content, range))) { let extraHeight = view.viewState.editorHeight - view.defaultLineHeight - view.documentPadding.top - 0.5
idx++; toCoordsBottom += extraHeight
return }
} markers.push(new RectangleMarker(
const fromCoordsTop = view.coordsAtPos(Math.max(block.content.from, view.visibleRanges[0].from)).top idx++ % 2 == 0 ? "block-even" : "block-odd",
let toCoordsBottom = view.coordsAtPos(Math.min(block.content.to, view.visibleRanges[view.visibleRanges.length - 1].to)).bottom 0,
if (idx === blocks.length - 1) { fromCoordsTop - (view.documentTop - view.documentPadding.top) - 1,
let extraHeight = view.viewState.editorHeight - view.defaultLineHeight - view.documentPadding.top - 0.5 null, // width is set to 100% in CSS
toCoordsBottom += extraHeight (toCoordsBottom - fromCoordsTop) + 2,
} ))
markers.push(new RectangleMarker( })
idx++ % 2 == 0 ? "block-even" : "block-odd", return markers
0, },
fromCoordsTop - (view.documentTop - view.documentPadding.top) - 1,
editorWidth,
(toCoordsBottom - fromCoordsTop) + 2,
))
})
return markers
}, update(update, dom) {
return update.docChanged || update.viewportChanged
},
update(update, dom) { class: "blocks-layer"
return update.docChanged || update.viewportChanged })
},
class: "blocks-layer"
})
return [measureEditorWidth, layerExtension]
}
const preventFirstBlockFromBeingDeleted = EditorState.changeFilter.of((tr) => { const preventFirstBlockFromBeingDeleted = EditorState.changeFilter.of((tr) => {
@ -240,7 +224,7 @@ export const noteBlockExtension = () => {
blockState, blockState,
noteBlockWidget(), noteBlockWidget(),
atomicNoteBlock, atomicNoteBlock,
blockLayer(), blockLayer,
preventFirstBlockFromBeingDeleted, preventFirstBlockFromBeingDeleted,
preventSelectionBeforeFirstBlock, preventSelectionBeforeFirstBlock,
lineNumbers({ lineNumbers({

View File

@ -22,10 +22,12 @@ body {
.blocks-layer .block-even { .blocks-layer .block-even {
width: 100%;
/*background: #3f3b4b;*/ /*background: #3f3b4b;*/
background: #232537; background: #232537;
} }
.blocks-layer .block-odd { .blocks-layer .block-odd {
width: 100%;
/*background: rgb(57, 71, 77);*/ /*background: rgb(57, 71, 77);*/
background: #222f38; background: #222f38;
} }