1
0

Attempt to fix line wrapper bugs

This commit is contained in:
Zef Hemel 2022-08-02 12:44:41 +02:00
parent 36004a7977
commit 9f7b59905a
2 changed files with 34 additions and 38 deletions

View File

@ -18,43 +18,45 @@ interface WrapElement {
function wrapLines(view: EditorView, wrapElements: WrapElement[]) { function wrapLines(view: EditorView, wrapElements: WrapElement[]) {
let widgets: Range<Decoration>[] = []; let widgets: Range<Decoration>[] = [];
let elementStack: string[] = []; let elementStack: string[] = [];
for (let { from, to } of view.visibleRanges) { const doc = view.state.doc;
const doc = view.state.doc; // Disabling the visible ranges for now, because it may be a bit buggy.
syntaxTree(view.state).iterate({ // RISK: this may actually become slow for large documents.
from, // for (let { from, to } of view.visibleRanges) {
to, syntaxTree(view.state).iterate({
enter: ({ type, from, to }) => { // from,
for (let wrapElement of wrapElements) { // to,
if (type.name == wrapElement.selector) { enter: ({ type, from, to }) => {
for (let wrapElement of wrapElements) {
if (type.name == wrapElement.selector) {
if (wrapElement.nesting) {
elementStack.push(type.name);
}
const bodyText = doc.sliceString(from, to);
let idx = from;
for (let line of bodyText.split("\n")) {
let cls = wrapElement.class;
if (wrapElement.nesting) { if (wrapElement.nesting) {
elementStack.push(type.name); cls = `${cls} ${cls}-${elementStack.length}`;
}
const bodyText = doc.sliceString(from, to);
let idx = from;
for (let line of bodyText.split("\n")) {
let cls = wrapElement.class;
if (wrapElement.nesting) {
cls = `${cls} ${cls}-${elementStack.length}`;
}
widgets.push(
Decoration.line({
class: cls,
}).range(doc.lineAt(idx).from)
);
idx += line.length + 1;
} }
widgets.push(
Decoration.line({
class: cls,
}).range(doc.lineAt(idx).from)
);
idx += line.length + 1;
} }
} }
}, }
leave({ type }) { },
for (let wrapElement of wrapElements) { leave({ type }) {
if (type.name == wrapElement.selector && wrapElement.nesting) { for (let wrapElement of wrapElements) {
elementStack.pop(); if (type.name == wrapElement.selector && wrapElement.nesting) {
} elementStack.pop();
} }
}, }
}); },
} });
// }
// Widgets have to be sorted by `from` in ascending order // Widgets have to be sorted by `from` in ascending order
widgets = widgets.sort((a, b) => { widgets = widgets.sort((a, b) => {
return a.from < b.from ? -1 : 1; return a.from < b.from ? -1 : 1;

View File

@ -1,12 +1,7 @@
@import "constants.scss"; @import "constants.scss";
//div.rhs-open #editor .cm-editor .cm-content {
// max-width: 550px;
//}
.cm-editor { .cm-editor {
font-size: var(--ident); font-size: var(--ident);
//overflow-y: hidden;
padding: 0 20px; padding: 0 20px;
.cm-content { .cm-content {
@ -256,7 +251,6 @@
color: #989797; color: #989797;
background-color: rgba(210, 210, 210, 0.2); background-color: rgba(210, 210, 210, 0.2);
border-radius: 5px; border-radius: 5px;
// font-style: italic;
font-size: 75%; font-size: 75%;
line-height: 75%; line-height: 75%;
} }