|
| 1 | +import { Extension } from '@codemirror/state'; |
| 2 | +import { Facet, RangeSetBuilder } from '@codemirror/state'; |
| 3 | +import { EditorView, Decoration, ViewPlugin, DecorationSet, ViewUpdate } from '@codemirror/view'; |
| 4 | + |
| 5 | +const lineNumber = Facet.define({ |
| 6 | + combine: (values) => { |
| 7 | + return values.length && Array.isArray(values) ? values.flat() : []; |
| 8 | + }, |
| 9 | +}); |
| 10 | + |
| 11 | +const stepSize = Facet.define({ |
| 12 | + combine: (values) => { |
| 13 | + return values.length && Array.isArray(values) ? Math.min(...values) : 2; |
| 14 | + }, |
| 15 | +}); |
| 16 | + |
| 17 | +const stripe = Decoration.line({ |
| 18 | + attributes: { class: 'cm-zebra-stripe' }, |
| 19 | +}); |
| 20 | + |
| 21 | +function stripeDeco(view: EditorView) { |
| 22 | + const step = view.state.facet(stepSize); |
| 23 | + const num = view.state.facet(lineNumber); |
| 24 | + const builder = new RangeSetBuilder<Decoration>(); |
| 25 | + for (let { from, to } of view.visibleRanges) { |
| 26 | + for (let pos = from; pos <= to; ) { |
| 27 | + let line = view.state.doc.lineAt(pos); |
| 28 | + |
| 29 | + if (line.number % step === 0 && num.length === 0) { |
| 30 | + builder.add(line.from, line.from, stripe); |
| 31 | + } |
| 32 | + if (num.length > 0 && num.flat().includes(line.number)) { |
| 33 | + builder.add(line.from, line.from, stripe); |
| 34 | + } |
| 35 | + pos = line.to + 1; |
| 36 | + } |
| 37 | + } |
| 38 | + return builder.finish(); |
| 39 | +} |
| 40 | + |
| 41 | +const showStripes = ViewPlugin.fromClass( |
| 42 | + class { |
| 43 | + decorations: DecorationSet; |
| 44 | + constructor(view: EditorView) { |
| 45 | + this.decorations = stripeDeco(view); |
| 46 | + } |
| 47 | + |
| 48 | + update(update: ViewUpdate) { |
| 49 | + this.decorations = stripeDeco(update.view); |
| 50 | + // if (update.docChanged || update.viewportChanged) { |
| 51 | + // this.decorations = stripeDeco(update.view); |
| 52 | + // } |
| 53 | + } |
| 54 | + }, |
| 55 | + { |
| 56 | + decorations: (v) => v.decorations, |
| 57 | + }, |
| 58 | +); |
| 59 | + |
| 60 | +const baseTheme = (opt: Pick<Partial<ZebraStripesOptions>, 'lightColor' | 'darkColor'> = {}) => { |
| 61 | + return EditorView.baseTheme({ |
| 62 | + '&light .cm-zebra-stripe': { backgroundColor: opt.lightColor || '#eef6ff' }, |
| 63 | + '&dark .cm-zebra-stripe': { backgroundColor: opt.darkColor || '#3a404d' }, |
| 64 | + }); |
| 65 | +}; |
| 66 | + |
| 67 | +export type ZebraStripesOptions = { |
| 68 | + step?: number | null; |
| 69 | + lightColor?: string; |
| 70 | + darkColor?: string; |
| 71 | + /** |
| 72 | + * @example `[1,[2,6], 10]` |
| 73 | + */ |
| 74 | + lineNumber?: (number | number[])[] | null; |
| 75 | +}; |
| 76 | + |
| 77 | +const range = (start: number, stop: number, step: number) => |
| 78 | + Array.from({ length: (stop - start) / step + 1 }, (_, i) => start + i * step); |
| 79 | + |
| 80 | +export function zebraStripes(options: ZebraStripesOptions = {}): Extension { |
| 81 | + const zebraStripeTheme = baseTheme({ lightColor: options.lightColor, darkColor: options.darkColor }); |
| 82 | + if (options.lineNumber && Array.isArray(options.lineNumber)) { |
| 83 | + options.step = null; |
| 84 | + options.lineNumber = options.lineNumber.map((item) => { |
| 85 | + if (Array.isArray(item) && typeof item[0] === 'number' && typeof item[1] === 'number') { |
| 86 | + return range(item[0], item[1], 1); |
| 87 | + } |
| 88 | + return item; |
| 89 | + }); |
| 90 | + } else { |
| 91 | + options.lineNumber = null; |
| 92 | + } |
| 93 | + return [ |
| 94 | + options.lineNumber === null ? [] : lineNumber.of(options.lineNumber || []), |
| 95 | + options.step === null ? [] : stepSize.of(options.step || 2), |
| 96 | + showStripes, |
| 97 | + zebraStripeTheme, |
| 98 | + ]; |
| 99 | +} |
0 commit comments