@molecule/app-diff-viewer-angular

Feature · diff-viewer · App (browser) · v1.1.1 · Apache-2.0

Side-by-side or unified text/code diff display

npm install @molecule/app-diff-viewer-angular

npm · Source on GitHub

How it works

@molecule/app-diff-viewer-angular is a ready-made diff-viewer feature for the app (browser) side. It composes the core interfaces it needs, so it works with whichever providers your app has bonded.

import { Component } from '@angular/core'
import { DiffViewer } from '@molecule/app-diff-viewer-angular'

@Component({
  selector: 'app-diff',
  standalone: true,
  imports: [DiffViewer],
  template: `
    <div
      mol-diff-viewer
      [before]="before"
      [after]="after"
      filename="src/config.ts"
      mode="unified"
      [showLineNumbers]="true"
    ></div>
  `,
})
export class DiffPage {
  before = 'const x = 1'
  after = 'const x = 2'
}

Works with: @molecule/app-angular, @molecule/app-ui, @molecule/app-ui-angular

Reference

Angular text/code diff viewer.

Exports <DiffViewer> (unified or split mode) — the Angular counterpart of @molecule/app-diff-viewer-react, with the same props (React names), the same ClassMap tokens, the same DOM and the same inline styles for the same input. Pure JS line diff (LCS, no external library) with add/remove row highlighting, optional line numbers, and an optional filename header. The host IS the outer <div> (div[mol-diff-viewer]).

Auto-generated, AI-first package reference for the molecule.dev ecosystem. It is written to be read by coding agents as much as by people, and is generated from this package's source — edit src/index.ts JSDoc, not this file.

Quick Start

import { Component } from '@angular/core'
import { DiffViewer } from '@molecule/app-diff-viewer-angular'

@Component({
  selector: 'app-diff',
  standalone: true,
  imports: [DiffViewer],
  template: `
    <div
      mol-diff-viewer
      [before]="before"
      [after]="after"
      filename="src/config.ts"
      mode="unified"
      [showLineNumbers]="true"
    ></div>
  `,
})
export class DiffPage {
  before = 'const x = 1'
  after = 'const x = 2'
}

Type

feature

Installation

npm install @molecule/app-diff-viewer-angular @angular/common @angular/core @angular/platform-browser @molecule/app-angular @molecule/app-ui @molecule/app-ui-angular

API

Interfaces

DiffViewerProps

Props accepted by the DiffViewer component.

interface DiffViewerProps {
  /** Original text. */
  before: string
  /** New text. */
  after: string
  /** Display mode. Defaults to `'unified'`. */
  mode?: 'unified' | 'split'
  /** Show line numbers. Defaults to true. */
  showLineNumbers?: boolean
  /** Optional filename / context label. */
  filename?: string
  /** Extra classes. */
  className?: string
}

Classes

DiffViewer

DiffViewer component.

Text diff viewer — unified or split layout with line-by-line add/remove highlighting, optional line numbers, and an optional filename header. The host IS the outer <div> (div[mol-diff-viewer]), so its ClassMap classes match React's root exactly.

This is the Angular counterpart of @molecule/app-diff-viewer-react: the same props (React names), the same ClassMap tokens, the same DOM and the same inline styles for the same input.

@Component({
  selector: 'div[mol-diff-viewer]',
  standalone: true,
  imports: [NgTemplateOutlet],
  inputs: ['before', 'after', 'mode', 'showLineNumbers', 'filename', 'className'],
  host: {
    '[class]': 'classes()',
  },
  // prettier-ignore
  template: `<ng-template #rowTpl let-r><div [style]="rowStyle(r.type)">@if (lineNumbers()) {<span style="width: 36px; text-align: right; opacity: 0.5; padding-right: 6px">{{ r.n }}</span>}<span style="width: 16px">{{ sign(r.type) }}</span><span style="white-space: pre">{{ r.text }}</span></div></ng-template>@if (filename) {<header [class]="headerClasses()">{{ filename }}</header>}@if (unified()) {<div>@for (r of rows(); track $index) {<div><ng-container [ngTemplateOutlet]="rowTpl" [ngTemplateOutletContext]="{ $implicit: r }"></ng-container></div>}</div>} @else {<div [class]="gridClasses()"><div>@for (r of leftRows(); track $index) {<ng-container [ngTemplateOutlet]="rowTpl" [ngTemplateOutletContext]="{ $implicit: r }"></ng-container>}</div><div>@for (r of rightRows(); track $index) {<ng-container [ngTemplateOutlet]="rowTpl" [ngTemplateOutletContext]="{ $implicit: r }"></ng-container>}</div></div>}`,
})
export class DiffViewer {
  /** Original text. */
  before!: DiffViewerProps['before']
  /** New text. */
  after!: DiffViewerProps['after']
  /** Display mode (React's default `'unified'` applies while undefined). */
  mode?: DiffViewerProps['mode']
  /** Show line numbers (React's default `true` applies while undefined). */
  showLineNumbers?: DiffViewerProps['showLineNumbers']
  /** Optional filename / context label. */
  filename?: DiffViewerProps['filename']
  /** Extra classes. */
  className?: DiffViewerProps['className']

  /** The inputs the cached diff was computed from. */
  private cachedBefore?: string
  /** The inputs the cached diff was computed from. */
  private cachedAfter?: string
  /** The cached diff rows, recomputed only when the inputs change. */
  private cachedLines?: DiffLine[]

  /**
   * The current class map.
   * @returns The UIClassMap.
   */
  cm(): UIClassMap {
    return getClassMap()
  }

  /**
   * The diff rows for `before`/`after`, memoized like React's `useMemo`.
   * @returns The diff rows, in order.
   */
  lines(): DiffLine[] {
    if (
      this.cachedLines === undefined ||
      this.cachedBefore !== this.before ||
      this.cachedAfter !== this.after
    ) {
      this.cachedBefore = this.before
      this.cachedAfter = this.after
      this.cachedLines = diffLines(this.before, this.after)
    }
    return this.cachedLines
  }

  /**
   * Whether the unified layout renders (React's `mode === 'unified'` with the
   * `'unified'` default parameter).
   * @returns True for the unified layout.
   */
  unified(): boolean {
    return this.mode === undefined ? true : this.mode === 'unified'
  }

  /**
   * Whether line numbers render (React's `showLineNumbers = true` default
   * parameter).
   * @returns True when line numbers show.
   */
  lineNumbers(): boolean {
    return this.showLineNumbers === undefined ? true : this.showLineNumbers
  }

  /**
   * The unified layout's rows, numbered from 1.
   * @returns The rows.
   */
  rows(): DiffRow[] {
    return this.lines().map((l, i) => ({ type: l.type, text: l.text, n: i + 1 }))
  }

  /**
   * The split layout's left column: every row that is not an addition.
   * @returns The left rows, numbered from 1.
   */
  leftRows(): DiffRow[] {
    return this.lines()
      .filter((l) => l.type !== 'add')
      .map((l, i) => ({
        type: l.type === 'remove' ? 'remove' : 'context',
        text: l.text,
        n: i + 1,
      }))
  }

  /**
   * The split layout's right column: every row that is not a removal.
   * @returns The right rows, numbered from 1.
   */
  rightRows(): DiffRow[] {
    return this.lines()
      .filter((l) => l.type !== 'remove')
      .map((l, i) => ({
        type: l.type === 'add' ? 'add' : 'context',
        text: l.text,
        n: i + 1,
      }))
  }

  /**
   * A row's background color.
   * @param type - The row kind.
   * @returns The CSS background value.
   */
  color(type: DiffLine['type']): string {
    return COLOR[type]
  }

  /**
   * A row's leading sign glyph.
   * @param type - The row kind.
   * @returns The sign.
   */
  sign(type: DiffLine['type']): string {
    return SIGN[type]
  }

  /**
   * A row's inline style (React's style object).
   * @param type - The row kind.
   * @returns The style declarations.
   */
  rowStyle(type: DiffLine['type']): Record<string, string> {
    return {
      background: COLOR[type],
      display: 'flex',
      'font-family': 'monospace',
      'font-size': '12px',
    }
  }

  /**
   * The host's class list (React's outer `<div>`).
   * @returns The class string.
   */
  classes(): string {
    const cm = this.cm()
    return cm.cn(cm.stack(0), this.className)
  }

  /**
   * The filename header's class list.
   * @returns The class string.
   */
  headerClasses(): string {
    const cm = this.cm()
    return cm.cn(cm.sp('px', 3), cm.sp('py', 1), cm.textSize('xs'), cm.fontWeight('semibold'))
  }

  /**
   * The split layout's grid class list.
   * @returns The class string.
   */
  gridClasses(): string {
    return this.cm().grid({ cols: 2, gap: 'xs' })
  }
}

Injection Notes

Requirements

Peer dependencies:

  • @angular/common ^22.0.0
  • @angular/core ^22.0.0
  • @angular/platform-browser ^22.0.0
  • @molecule/app-angular ^3.5.0 || ^4.0.0
  • @molecule/app-ui ^1.0.1
  • @molecule/app-ui-angular ^1.1.0

Runtime Dependencies

  • @angular/common

  • @angular/core

  • @angular/platform-browser

  • @molecule/app-angular

  • @molecule/app-ui

  • @molecule/app-ui-angular

  • The diff is an O(n*m) dynamic program — fine for typical UI diffs; for very large inputs (10k+ lines) chunk or virtualize upstream.

  • Highlight colors are fixed translucent green/red (rgba) rather than theme tokens; they read on light and dark surfaces but are not themeable via ClassMap.

  • No user-facing text of its own (only +/- glyphs), so no locale bond is needed. Styling resolves through getClassMap() — requires a wired ClassMap bond (standard molecule app setup).