monocart-code-viewer

A code viewer component with code coverage visualization, based on CodeMirror 6

MIT 21 个版本
安装
npm install monocart-code-viewer
yarn add monocart-code-viewer
pnpm add monocart-code-viewer
bun add monocart-code-viewer
README

monocart-code-viewer

A code viewer component with code coverage visualization, based on CodeMirror 6.

Features

  • Syntax highlighting for JavaScript, CSS, HTML (with mixed language support)
  • Line coverage indicators (covered / partial / uncovered)
  • Inline execution count badges with hover highlighting
  • Uncovered code range highlighting
  • Custom decorations (e.g. "else path uncovered" markers)
  • Read-only editor with code folding and search
  • Cursor tracking events
  • Touch device support

Install

npm i monocart-code-viewer

Usage

import { createCodeViewer } from 'monocart-code-viewer';
const codeViewer = createCodeViewer(container, report);

Report

The report object defines the source content and optional coverage data:

const report = {
    content: 'your source content',
    coverage: {

        // Line coverage status (key is 0-based line index)
        uncoveredLines: {
            '0': 'comment',
            '1': 'blank',
            '8': 'partial',
            '9': 'uncovered'
        },

        // Uncovered character ranges within a line (key is 0-based line index)
        uncoveredPieces: {
            '8': [{
                start: 27,
                end: 34
            }]
        },

        // Custom inline decorations (key is 0-based line index)
        decorations: {
            '14': [{
                column: 4,
                value: 'E',
                attrs: {
                    title: 'else path uncovered'
                }
            }]
        },

        // Execution count badges (key is 0-based line index)
        executionCounts: {
            '20': [{
                column: 22,
                count: 10,
                value: '10',
                end: 916
            }]
        }
    }
};

API

const codeViewer = createCodeViewer(container, report);
  • codeViewer.on(eventType, handler) - Register an event listener

    codeViewer.on('cursor', (loc) => {
        // loc: { line, column, position, start?, end? }
    });
    
  • codeViewer.update(newReport) - Update content and/or coverage data

    codeViewer.update(newReport);
    
  • codeViewer.setSelection(start, end, options?) - Set text selection and scroll into view

    codeViewer.setSelection(100, 200);
    
  • codeViewer.setCursor(pos, options?) - Set cursor position and scroll into view

    codeViewer.setCursor(100);
    
  • codeViewer.viewer - Access the underlying CodeMirror EditorView instance

版本列表
1.2.0 2026-04-11
1.1.5 2025-02-26
1.1.4 2024-06-15
1.1.3 2024-03-30
1.1.2 2024-03-04
1.1.1 2024-02-23
1.1.0 2024-02-07
1.0.13 2023-12-28
1.0.12 2023-12-26
1.0.11 2023-12-22
1.0.10 2023-12-21
1.0.9 2023-12-15
1.0.8 2023-12-03
1.0.7 2023-06-25
1.0.6 2023-05-20
1.0.5 2023-05-18
1.0.4 2023-05-17
1.0.3 2023-05-12
1.0.2 2023-05-11
1.0.1 2023-05-08
1.0.0 2023-05-01