Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding a class for a specific line #433

Closed
StarkovSergey opened this issue Dec 15, 2022 · 2 comments
Closed

Adding a class for a specific line #433

StarkovSergey opened this issue Dec 15, 2022 · 2 comments

Comments

@StarkovSergey
Copy link

I want dynamically add classes for specific lines and gutters

@StarkovSergey StarkovSergey changed the title How can I add a class for a specific line? Adding a class for a specific line Dec 15, 2022
@jaywcjlove
Copy link
Member

@StarkovSergey https://codemirror.net/examples/zebra/ might help.

https://uiwjs.github.io/react-codemirror/#/extensions/zebra-stripes

import { Extension } from '@codemirror/state';
import { Facet, RangeSetBuilder } from '@codemirror/state';
import { EditorView, Decoration, ViewPlugin, DecorationSet, ViewUpdate } from '@codemirror/view';
const lineNumber = Facet.define({
combine: (values) => {
return values.length && Array.isArray(values) ? values.flat() : [];
},
});
const stepSize = Facet.define({
combine: (values) => {
return values.length && Array.isArray(values) ? Math.min(...values) : 2;
},
});
const stripe = Decoration.line({
attributes: { class: 'cm-zebra-stripe' },
});
function stripeDeco(view: EditorView) {
const step = view.state.facet(stepSize);
const num = view.state.facet(lineNumber);
const builder = new RangeSetBuilder<Decoration>();
for (let { from, to } of view.visibleRanges) {
for (let pos = from; pos <= to; ) {
let line = view.state.doc.lineAt(pos);
if (line.number % step === 0 && num.length === 0) {
builder.add(line.from, line.from, stripe);
}
if (num.length > 0 && num.flat().includes(line.number)) {
builder.add(line.from, line.from, stripe);
}
pos = line.to + 1;
}
}
return builder.finish();
}
const showStripes = ViewPlugin.fromClass(
class {
decorations: DecorationSet;
constructor(view: EditorView) {
this.decorations = stripeDeco(view);
}
update(update: ViewUpdate) {
this.decorations = stripeDeco(update.view);
// if (update.docChanged || update.viewportChanged) {
// this.decorations = stripeDeco(update.view);
// }
}
},
{
decorations: (v) => v.decorations,
},
);
const baseTheme = (opt: Pick<Partial<ZebraStripesOptions>, 'lightColor' | 'darkColor'> = {}) => {
return EditorView.baseTheme({
'&light .cm-zebra-stripe': { backgroundColor: opt.lightColor || '#eef6ff' },
'&dark .cm-zebra-stripe': { backgroundColor: opt.darkColor || '#3a404d' },
});
};
export type ZebraStripesOptions = {
step?: number | null;
lightColor?: string;
darkColor?: string;
/**
* @example `[1,[2,6], 10]`
*/
lineNumber?: (number | number[])[] | null;
};
const range = (start: number, stop: number, step: number) =>
Array.from({ length: (stop - start) / step + 1 }, (_, i) => start + i * step);
export function zebraStripes(options: ZebraStripesOptions = {}): Extension {
const zebraStripeTheme = baseTheme({ lightColor: options.lightColor, darkColor: options.darkColor });
if (options.lineNumber && Array.isArray(options.lineNumber)) {
options.step = null;
options.lineNumber = options.lineNumber.map((item) => {
if (Array.isArray(item) && typeof item[0] === 'number' && typeof item[1] === 'number') {
return range(item[0], item[1], 1);
}
return item;
});
} else {
options.lineNumber = null;
}
return [
options.lineNumber === null ? [] : lineNumber.of(options.lineNumber || []),
options.step === null ? [] : stepSize.of(options.step || 2),
showStripes,
zebraStripeTheme,
];
}

@StarkovSergey
Copy link
Author

It's great! Thank you!

jaywcjlove added a commit that referenced this issue Dec 15, 2022
github-actions bot pushed a commit that referenced this issue Dec 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants