This commit is contained in:
Felix Roos 2022-08-14 11:27:13 +02:00
parent f34d0a29e8
commit 4e09cc14b6
3 changed files with 23 additions and 26 deletions

File diff suppressed because one or more lines are too long

View File

@ -89,8 +89,7 @@ const highlightField = StateField.define({
try { try {
for (let e of tr.effects) { for (let e of tr.effects) {
if (e.is(setHighlights)) { if (e.is(setHighlights)) {
highlights = Decoration.set( const marks = e.value.map(
e.value.flatMap(
(hap) => (hap.context.locations || []).map(({ start, end }) => { (hap) => (hap.context.locations || []).map(({ start, end }) => {
const color = hap.context.color || "#FFCA28"; const color = hap.context.color || "#FFCA28";
let from = tr.newDoc.line(start.line).from + start.column; let from = tr.newDoc.line(start.line).from + start.column;
@ -102,14 +101,13 @@ const highlightField = StateField.define({
const mark = Decoration.mark({ attributes: { style: `outline: 1.5px solid ${color};` } }); const mark = Decoration.mark({ attributes: { style: `outline: 1.5px solid ${color};` } });
return mark.range(from, to); return mark.range(from, to);
}) })
).filter(Boolean), ).flat().filter(Boolean) || [];
true highlights = Decoration.set(marks, true);
);
} }
} }
return highlights; return highlights;
} catch (err) { } catch (err) {
return highlights; return Decoration.set([]);
} }
}, },
provide: (f) => EditorView.decorations.from(f) provide: (f) => EditorView.decorations.from(f)
@ -423,7 +421,6 @@ function useHighlighting({ view, pattern, active }) {
highlights = highlights.filter((hap) => hap.whole.end > audioTime); // keep only highlights that are still active highlights = highlights.filter((hap) => hap.whole.end > audioTime); // keep only highlights that are still active
const haps = pattern.queryArc(...span).filter((hap) => hap.hasOnset()); const haps = pattern.queryArc(...span).filter((hap) => hap.hasOnset());
highlights = highlights.concat(haps); // add potential new onsets highlights = highlights.concat(haps); // add potential new onsets
console.log('update...', view.state.doc.length);
view.dispatch({ effects: setHighlights.of(highlights) }); // highlight all still active + new active haps view.dispatch({ effects: setHighlights.of(highlights) }); // highlight all still active + new active haps
} catch (err) { } catch (err) {
// console.log('error in updateHighlights', err); // console.log('error in updateHighlights', err);

View File

@ -49,9 +49,9 @@ const highlightField = StateField.define({
try { try {
for (let e of tr.effects) { for (let e of tr.effects) {
if (e.is(setHighlights)) { if (e.is(setHighlights)) {
highlights = Decoration.set( const marks =
e.value e.value
.flatMap((hap) => .map((hap) =>
(hap.context.locations || []).map(({ start, end }) => { (hap.context.locations || []).map(({ start, end }) => {
const color = hap.context.color || '#FFCA28'; const color = hap.context.color || '#FFCA28';
let from = tr.newDoc.line(start.line).from + start.column; let from = tr.newDoc.line(start.line).from + start.column;
@ -65,15 +65,15 @@ const highlightField = StateField.define({
return mark.range(from, to); return mark.range(from, to);
}), }),
) )
.filter(Boolean), .flat()
true, .filter(Boolean) || [];
); highlights = Decoration.set(marks, true);
} }
} }
return highlights; return highlights;
} catch (err) { } catch (err) {
// console.warn('highlighting error', err); // console.warn('highlighting error', err);
return highlights; return Decoration.set([]);
} }
}, },
provide: (f) => EditorView.decorations.from(f), provide: (f) => EditorView.decorations.from(f),