├── .browserslistrc ├── .editorconfig ├── .eslintrc.json ├── .github └── workflows │ └── node.js.yml ├── .gitignore ├── .husky └── commit-msg ├── .prettierrc ├── CHANGELOG.md ├── LICENSE ├── README.md ├── angular.json ├── commitlint.config.js ├── package-lock.json ├── package.json ├── projects └── ngx-diff │ ├── .eslintrc.json │ ├── LICENSE │ ├── README.md │ ├── karma.conf.js │ ├── ng-package.json │ ├── package.json │ ├── src │ ├── lib │ │ ├── common │ │ │ ├── diff-calculation.interface.ts │ │ │ ├── line-diff-type.ts │ │ │ └── line-select-event.ts │ │ ├── components │ │ │ ├── inline-diff │ │ │ │ ├── inline-diff.component.html │ │ │ │ ├── inline-diff.component.scss │ │ │ │ ├── inline-diff.component.spec.ts │ │ │ │ └── inline-diff.component.ts │ │ │ ├── side-by-side-diff │ │ │ │ ├── side-by-side-diff.component.html │ │ │ │ ├── side-by-side-diff.component.scss │ │ │ │ ├── side-by-side-diff.component.spec.ts │ │ │ │ └── side-by-side-diff.component.ts │ │ │ └── unified-diff │ │ │ │ ├── unified-diff.component.html │ │ │ │ ├── unified-diff.component.scss │ │ │ │ ├── unified-diff.component.spec.ts │ │ │ │ └── unified-diff.component.ts │ │ ├── pipes │ │ │ └── line-number │ │ │ │ ├── line-number.pipe.spec.ts │ │ │ │ └── line-number.pipe.ts │ │ └── services │ │ │ └── diff-match-patch │ │ │ ├── diff-match-patch.service.spec.ts │ │ │ └── diff-match-patch.service.ts │ ├── public-api.ts │ └── test.ts │ ├── styles │ └── default-theme.css │ ├── tsconfig.lib.json │ ├── tsconfig.lib.prod.json │ └── tsconfig.spec.json ├── src ├── app │ ├── app-routing.module.ts │ ├── app.component.html │ ├── app.component.scss │ ├── app.component.spec.ts │ └── app.component.ts ├── assets │ └── .gitkeep ├── environments │ ├── environment.prod.ts │ └── environment.ts ├── favicon.ico ├── index.html ├── main.ts ├── polyfills.ts ├── styles.scss └── test.ts ├── tsconfig.app.json ├── tsconfig.json └── tsconfig.spec.json /.browserslistrc: -------------------------------------------------------------------------------- 1 | # This file is used by the build system to adjust CSS and JS output to support the specified browsers below. 2 | # For additional information regarding the format and rule options, please see: 3 | # https://github.com/browserslist/browserslist#queries 4 | 5 | # You can see what browsers were selected by your queries by running: 6 | # npx browserslist 7 | 8 | > 0.5% 9 | last 2 versions 10 | Firefox ESR 11 | not dead 12 | not IE 9-11 # For IE 9-11 support, remove 'not'. -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # Editor configuration, see https://editorconfig.org 2 | root = true 3 | 4 | [*] 5 | charset = utf-8 6 | indent_style = space 7 | indent_size = 2 8 | insert_final_newline = true 9 | trim_trailing_whitespace = true 10 | 11 | [*.md] 12 | max_line_length = off 13 | trim_trailing_whitespace = false 14 | -------------------------------------------------------------------------------- /.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "root": true, 3 | "ignorePatterns": ["projects/**/*"], 4 | "overrides": [ 5 | { 6 | "files": ["*.ts"], 7 | "parserOptions": { 8 | "project": ["tsconfig.json"], 9 | "createDefaultProgram": true 10 | }, 11 | "extends": [ 12 | "eslint:recommended", 13 | "plugin:@typescript-eslint/recommended", 14 | "plugin:@angular-eslint/recommended", 15 | "plugin:@angular-eslint/template/process-inline-templates" 16 | ], 17 | "rules": { 18 | "@angular-eslint/component-selector": [ 19 | "error", 20 | { 21 | "type": "element", 22 | "prefix": "app", 23 | "style": "kebab-case" 24 | } 25 | ], 26 | "@angular-eslint/directive-selector": [ 27 | "error", 28 | { 29 | "type": "attribute", 30 | "prefix": "app", 31 | "style": "camelCase" 32 | } 33 | ], 34 | "@typescript-eslint/explicit-member-accessibility": [ 35 | "off", 36 | { 37 | "accessibility": "explicit" 38 | } 39 | ], 40 | "arrow-parens": ["off", "always"], 41 | "import/order": "off" 42 | } 43 | }, 44 | { 45 | "files": ["*.html"], 46 | "extends": ["plugin:@angular-eslint/template/recommended"], 47 | "rules": {} 48 | } 49 | ] 50 | } 51 | -------------------------------------------------------------------------------- /.github/workflows/node.js.yml: -------------------------------------------------------------------------------- 1 | name: Node.js CI 2 | 3 | on: 4 | push: 5 | branches: [main] 6 | pull_request: 7 | branches: [main] 8 | 9 | jobs: 10 | build: 11 | runs-on: ubuntu-latest 12 | 13 | strategy: 14 | matrix: 15 | node-version: [20.x, 22.x, 24.x] 16 | 17 | steps: 18 | - uses: actions/checkout@v3 19 | - name: Use Node.js ${{ matrix.node-version }} 20 | uses: actions/setup-node@v3 21 | with: 22 | node-version: ${{ matrix.node-version }} 23 | - run: npm ci 24 | - run: npm run lint 25 | - run: npm run build --if-present 26 | - run: npm test 27 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # See http://help.github.com/ignore-files/ for more about ignoring files. 2 | 3 | # compiled output 4 | /dist 5 | /tmp 6 | /out-tsc 7 | # Only exists if Bazel was run 8 | /bazel-out 9 | 10 | # dependencies 11 | /node_modules 12 | 13 | # profiling files 14 | chrome-profiler-events*.json 15 | speed-measure-plugin*.json 16 | 17 | # IDEs and editors 18 | /.idea 19 | .project 20 | .classpath 21 | .c9/ 22 | *.launch 23 | .settings/ 24 | *.sublime-workspace 25 | 26 | # IDE - VSCode 27 | .vscode/* 28 | !.vscode/settings.json 29 | !.vscode/tasks.json 30 | !.vscode/launch.json 31 | !.vscode/extensions.json 32 | .history/* 33 | 34 | # misc 35 | /.angular/cache 36 | /.nx/cache 37 | /.sass-cache 38 | /connect.lock 39 | /coverage 40 | /libpeerconnection.log 41 | npm-debug.log 42 | yarn-error.log 43 | testem.log 44 | /typings 45 | 46 | # System Files 47 | .DS_Store 48 | Thumbs.db 49 | 50 | .npmrc 51 | -------------------------------------------------------------------------------- /.husky/commit-msg: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | . "$(dirname "$0")/_/husky.sh" 3 | 4 | npx --no -- commitlint --edit "" 5 | -------------------------------------------------------------------------------- /.prettierrc: -------------------------------------------------------------------------------- 1 | { 2 | "singleQuote": true, 3 | "printWidth": 100, 4 | "importOrder": [ 5 | "^@angular/(.*)$", 6 | "^@(.*)/(.*)$", 7 | "^[./]" 8 | ], 9 | "importOrderSeparation": true, 10 | "importOrderSortSpecifiers": true, 11 | "trailingComma": "all", 12 | "importOrderParserPlugins": ["typescript", "decorators-legacy"] 13 | } 14 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines. 4 | 5 | ## [11.0.0](https://github.com/rars/ngx-diff/compare/v10.0.0...v11.0.0) (2025-06-06) 6 | 7 | 8 | ### Features 9 | 10 | * update to Angular 20 ([efcc59f](https://github.com/rars/ngx-diff/commit/efcc59fd0a4c17656e0cee3edec39cca621f841e)) 11 | 12 | ## [10.0.0](https://github.com/rars/ngx-diff/compare/v9.1.0...v10.0.0) (2024-12-06) 13 | 14 | 15 | ### Features 16 | 17 | * **ngx-diff:** update to Angular 19 ([cc846ba](https://github.com/rars/ngx-diff/commit/cc846babd17925b900987e0b5c1cef6e6c52e35c)) 18 | 19 | ## [9.1.0](https://github.com/rars/ngx-diff/compare/v9.0.0...v9.1.0) (2024-10-09) 20 | 21 | 22 | ### Features 23 | 24 | * **ngx-diff:** improve tracking on line diff DOM elements ([ffe25bd](https://github.com/rars/ngx-diff/commit/ffe25bd73079db06695265ef03b2ee2200de5e5e)) 25 | 26 | ## [9.0.0](https://github.com/rars/ngx-diff/compare/v8.0.4...v9.0.0) (2024-06-06) 27 | 28 | 29 | ### Features 30 | 31 | * **ngx-diff:** update to Angular 18 ([97b053a](https://github.com/rars/ngx-diff/commit/97b053ab221312b2ad9f7344ce95375eaf53c870)) 32 | 33 | ### [8.0.4](https://github.com/rars/ngx-diff/compare/v8.0.3...v8.0.4) (2024-03-27) 34 | 35 | 36 | ### Bug Fixes 37 | 38 | * **ngx-diff:** make background color fill full width in horizontal overflow ([02cdc16](https://github.com/rars/ngx-diff/commit/02cdc16a7c47dce4c88dc5ad872db25ae8a3fd1b)) 39 | 40 | ### [8.0.3](https://github.com/rars/ngx-diff/compare/v8.0.2...v8.0.3) (2024-03-27) 41 | 42 | 43 | ### Bug Fixes 44 | 45 | * **ngx-diff:** avoid directory export for style CSS ([1f44b34](https://github.com/rars/ngx-diff/commit/1f44b34670e08b788692f369a5fe3514eb94d5cb)) 46 | 47 | ### [8.0.2](https://github.com/rars/ngx-diff/compare/v8.0.1...v8.0.2) (2024-03-27) 48 | 49 | 50 | ### Bug Fixes 51 | 52 | * **ngx-diff:** add styles to exports of package.json ([911c864](https://github.com/rars/ngx-diff/commit/911c8642cfc81b4d305aab7ef536c43a6bbc1ce4)), closes [#78](https://github.com/rars/ngx-diff/issues/78) 53 | 54 | ### [8.0.1](https://github.com/rars/ngx-diff/compare/v8.0.0...v8.0.1) (2024-03-15) 55 | 56 | 57 | ### Bug Fixes 58 | 59 | * **ngx-diff:** add UnifiedDiffComponent to exported types ([842d520](https://github.com/rars/ngx-diff/commit/842d520f333763080a2e5a00d66879ec54f7a7f1)) 60 | 61 | ## [8.0.0](https://github.com/rars/ngx-diff/compare/v7.0.0...v8.0.0) (2024-03-15) 62 | 63 | 64 | ### Features 65 | 66 | * **ngx-diff:** add `ngx-unified-diff` component to replace `inline-diff`; deprecate `inline-diff` ([94b6bf4](https://github.com/rars/ngx-diff/commit/94b6bf483b9c2c6df2b3cca8899a7b0c1f0c39a4)) 67 | * **ngx-diff:** add named light and dark themes ([f870bd8](https://github.com/rars/ngx-diff/commit/f870bd8076a47ff9639ff3b61aeec159d1d17c11)) 68 | * **ngx-diff:** add side-by-side diff component ([6826e7e](https://github.com/rars/ngx-diff/commit/6826e7e44586e130b1fe9bf0d0cb327c61beeb9a)) 69 | * **ngx-diff:** add title bar to inline-diff ([63183b3](https://github.com/rars/ngx-diff/commit/63183b37d169390b69600b59ad29d72898c97a85)) 70 | 71 | ## [7.0.0](https://github.com/rars/ngx-diff/compare/v6.0.1...v7.0.0) (2024-03-10) 72 | 73 | ### Features 74 | 75 | - **ngx-diff:** allow truncated placeholder lines to be expanded to reveal hidden content ([481066c](https://github.com/rars/ngx-diff/commit/481066c3828cf32b9900db3994fd3db9e6887302)) 76 | 77 | ## [6.0.1](https://github.com/rars/ngx-diff/compare/v6.0.0...v6.0.1) (2024-01-18) 78 | 79 | ### Bug Fixes 80 | 81 | - **ngx-diff:** include README.md and LICENSE in npm dist 82 | 83 | ## [6.0.0](https://github.com/rars/ngx-diff/compare/v5.0.0...v6.0.0) (2023-11-08) 84 | 85 | ### Features 86 | 87 | - **ngx-diff:** update to Angular 17 ([0080aad](https://github.com/rars/ngx-diff/commit/0080aad4c391443ed368c07f508e51c7bc740576)) 88 | 89 | ## [5.0.0](https://github.com/rars/ngx-diff/compare/v4.0.0...v5.0.0) (2023-05-07) 90 | 91 | ### Features 92 | 93 | - **ngx-diff:** convert components/pipes to standalone, remove `NgxDiffModule` ([712a34b](https://github.com/rars/ngx-diff/commit/712a34bc02dc33b2ec02a163409417c4334d020a)) 94 | - **ngx-diff:** update to Angular 16 ([6e79821](https://github.com/rars/ngx-diff/commit/6e79821b6a78f7cd750ae29d4c63a895cb97b19d)) 95 | 96 | ## [4.0.0](https://github.com/rars/ngx-diff/compare/v3.0.0...v4.0.0) (2023-05-07) 97 | 98 | ### Features 99 | 100 | - **ngx-diff:** update to Angular 15 ([16de002](https://github.com/rars/ngx-diff/commit/16de0025724e6888ddd06308e6a8cabecf685210)) 101 | 102 | ## [3.0.0](https://github.com/rars/ngx-diff/compare/v2.0.0...v3.0.0) (2023-05-07) 103 | 104 | ### Features 105 | 106 | - **ngx-diff:** add CSS variables for customising appearance of diff ([5b4b818](https://github.com/rars/ngx-diff/commit/5b4b81803aae4a7b210babfd5478158022122238)) 107 | - **ngx-jwt:** add selectedLineChange output event and allow lines to be selected ([002e8db](https://github.com/rars/ngx-diff/commit/002e8dbb0db765edc8d578c6c507f0420d84b9a1)) 108 | - **ngx-jwt:** mark DiffMatchPatchService as providedIn root ([dd13fab](https://github.com/rars/ngx-diff/commit/dd13fabeedb8546bdc0f0c1bacc33cbaca06d682)) 109 | 110 | ## [2.0.0](https://github.com/rars/ngx-diff/compare/v1.0.0...v2.0.0) (2023-05-06) 111 | 112 | ### Features 113 | 114 | - **ngx-diff:** update to Angular 14 ([e95fbaa](https://github.com/rars/ngx-diff/commit/e95fbaaf5b52ad40a3e519ec0d8f5a11ac5a60c8)) 115 | 116 | ## [1.0.0](https://github.com/rars/ngx-diff/compare/v0.4.0...v1.0.0) (2022-03-21) 117 | 118 | ### Features 119 | 120 | - update to Angular 13 ([f64d646](https://github.com/rars/ngx-diff/commit/f64d646b8f18124c092ab67d00dce08068e090ce)) 121 | 122 | 123 | 124 | ## [0.1.1](https://github.com/rars/ngx-diff/compare/v0.1.0...v0.1.1) (2018-01-25) 125 | 126 | ### Features 127 | 128 | - **module:** export public api in main module ([8a2878f](https://github.com/rars/ngx-diff/commit/8a2878f)) 129 | 130 | 131 | 132 | ## 0.1.0 (2018-01-01) 133 | 134 | ### Features 135 | 136 | - **inline-diff:** add inline-diff component ([94a3979](https://github.com/rars/ngx-diff/commit/94a3979)) 137 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018, 2020-2024 Richard Russell 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ngx-diff 2 | 3 | [![Node.js CI](https://github.com/rars/ngx-diff/actions/workflows/node.js.yml/badge.svg)](https://github.com/rars/ngx-diff/actions/workflows/node.js.yml) 4 | 5 | Angular component library for displaying diffs of text. [Demo](https://rars.github.io/ngx-diff/). 6 | 7 | ## Quickstart 8 | 9 | 1. Install `ngx-diff` modules from npm: 10 | ```bash 11 | npm install ngx-diff diff-match-patch-ts --save 12 | ``` 13 | 2. Either: 14 | 15 | 2.1. If you are using this component in an NgModule-based setting, add `UnifiedDiffComponent` or `SideBySideDiffComponent` to your module's `imports`: 16 | 17 | ```typescript 18 | import { UnifiedDiffComponent } from 'ngx-diff'; 19 | 20 | import { NgModule } from '@angular/core'; 21 | import { BrowserModule } from '@angular/platform-browser'; 22 | 23 | import { AppComponent } from './app.component'; 24 | 25 | @NgModule({ 26 | declarations: [AppComponent], 27 | imports: [BrowserModule, UnifiedDiffComponent], 28 | providers: [], 29 | bootstrap: [AppComponent], 30 | }) 31 | export class AppModule {} 32 | ``` 33 | 34 | 2.2. Or if you are using this component in a standalone component setting, add `UnifiedDiffComponent` or `SideBySideDiffComponent` to your component's `imports`: 35 | 36 | ```typescript 37 | import { SideBySideDiffComponent } from 'ngx-diff'; 38 | 39 | import { Component } from '@angular/core'; 40 | 41 | @Component({ 42 | selector: 'app-root', 43 | templateUrl: './app.component.html', 44 | styleUrls: ['./app.component.scss'], 45 | standalone: true, 46 | imports: [SideBySideDiffComponent], 47 | }) 48 | export class AppComponent { 49 | // ... 50 | } 51 | ``` 52 | 53 | 3. Use the `ngx-unified-diff` component by setting the `before` and `after` attributes: 54 | 55 | ```HTML 56 | 57 | ``` 58 | 59 | or use the `ngx-side-by-side-diff` component by setting the `before` and `after` attributes: 60 | 61 | ```HTML 62 | 63 | ``` 64 | 65 | ### Upgrading from v7.0.0 66 | 67 | In v8.0.0, `inline-diff` component has been deprecated and users should switch to the `ngx-unified-diff` component that has been added and provides equivalent functionality. `inline-diff` will be removed in the next release. 68 | 69 | ## Theming 70 | 71 | For version 3+, you can customise the appearance of the diff through various CSS variable settings. If you are not using the latest version, refer to the `README.md` file in earlier releases. 72 | 73 | In version 8.0.0, a light and dark theme was introduced. This should be imported to your application `styles.scss` file or equivalent. 74 | 75 | ```scss 76 | @use 'ngx-diff/styles/default-theme'; 77 | ``` 78 | 79 | You can then use the provided `ngx-diff-light-theme` or `ngx-diff-dark-theme` classes. 80 | 81 | ### Custom theme 82 | 83 | To create your own theme, override the relevant CSS variables; for example, in your `styles.scss` file, define: 84 | 85 | ```SCSS 86 | .my-custom-ngx-diff-theme { 87 | --ngx-diff-border-color: #dfdfdf; 88 | --ngx-diff-font-size: 0.9rem; 89 | --ngx-diff-font-family: Consolas, Courier, monospace; 90 | --ngx-diff-font-color: #000; 91 | --ngx-diff-line-number-font-color: #aaaaaa; 92 | --ngx-diff-line-number-hover-font-color: #484848; 93 | 94 | --ngx-diff-selected-border-width: 0; 95 | --ngx-diff-selected-border-color: #000; 96 | --ngx-diff-selected-line-background-color: #d6f1ff; 97 | 98 | --ngx-diff-line-number-width: 2rem; 99 | --ngx-diff-border-width: 1px; 100 | --ngx-diff-line-left-padding: 1rem; 101 | --ngx-diff-bottom-spacer-height: 1rem; 102 | --ngx-diff-title-bar-padding: 0.5rem; 103 | --ngx-diff-title-font-weight: 600; 104 | 105 | --ngx-diff-insert-color: #d6ffd6; 106 | --ngx-diff-delete-color: #ffd6d6; 107 | --ngx-diff-equal-color: #ffffff; 108 | --ngx-diff-mix-color: #000; 109 | --ngx-diff-light-mix-percentage: 4%; 110 | --ngx-diff-heavy-mix-percentage: 10%; 111 | } 112 | ``` 113 | 114 | Then use this class in your desired component in your HTML template: 115 | 116 | ```HTML 117 | 125 | ``` 126 | 127 | It is recommended to use these settings rather than attempt to override styles based upon DOM structure or class names that are internal details that may change. 128 | 129 | ## Version history 130 | 131 | | Angular Version | ngx-diff Version | 132 | | --------------- | ---------------- | 133 | | 9 | 0.2.0 | 134 | | 10 | 0.3.0 | 135 | | 11 | 0.4.0 | 136 | | 13 | 1.0.0 | 137 | | 14 | 2.0.0 | 138 | | 14 | 3.0.0 | 139 | | 15 | 4.0.0 | 140 | | 16 | 5.0.0 | 141 | | 17 | 6.0.0+ | 142 | | 18 | 9.0.0+ | 143 | | 19 | 10.0.0+ | 144 | | 20 | 11.0.0+ | 145 | 146 | ## Contributions welcome! 147 | 148 | If you have a feature or improvement you would like to see included, please raise an issue or a PR and I will review. 149 | 150 | ## License 151 | 152 | See the [LICENSE](LICENSE) file for license rights and limitations (MIT). 153 | -------------------------------------------------------------------------------- /angular.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "./node_modules/@angular/cli/lib/config/schema.json", 3 | "version": 1, 4 | "newProjectRoot": "projects", 5 | "projects": { 6 | "ngx-diff-demo": { 7 | "projectType": "application", 8 | "root": "", 9 | "sourceRoot": "src", 10 | "prefix": "app", 11 | "architect": { 12 | "build": { 13 | "builder": "@angular/build:application", 14 | "options": { 15 | "outputPath": { 16 | "base": "dist/ngx-diff-demo" 17 | }, 18 | "index": "src/index.html", 19 | "polyfills": [ 20 | "src/polyfills.ts" 21 | ], 22 | "tsConfig": "tsconfig.app.json", 23 | "assets": [ 24 | "src/favicon.ico", 25 | "src/assets" 26 | ], 27 | "styles": [ 28 | "src/styles.scss" 29 | ], 30 | "scripts": [], 31 | "extractLicenses": false, 32 | "sourceMap": true, 33 | "optimization": false, 34 | "namedChunks": true, 35 | "browser": "src/main.ts" 36 | }, 37 | "configurations": { 38 | "production": { 39 | "fileReplacements": [ 40 | { 41 | "replace": "src/environments/environment.ts", 42 | "with": "src/environments/environment.prod.ts" 43 | } 44 | ], 45 | "optimization": true, 46 | "outputHashing": "all", 47 | "sourceMap": false, 48 | "namedChunks": false, 49 | "extractLicenses": true, 50 | "budgets": [ 51 | { 52 | "type": "initial", 53 | "maximumWarning": "2mb", 54 | "maximumError": "5mb" 55 | }, 56 | { 57 | "type": "anyComponentStyle", 58 | "maximumWarning": "6kb", 59 | "maximumError": "10kb" 60 | } 61 | ] 62 | } 63 | }, 64 | "defaultConfiguration": "" 65 | }, 66 | "serve": { 67 | "builder": "@angular/build:dev-server", 68 | "options": { 69 | "buildTarget": "ngx-diff-demo:build" 70 | }, 71 | "configurations": { 72 | "production": { 73 | "buildTarget": "ngx-diff-demo:build:production" 74 | } 75 | } 76 | }, 77 | "extract-i18n": { 78 | "builder": "@angular/build:extract-i18n", 79 | "options": { 80 | "buildTarget": "ngx-diff-demo:build" 81 | } 82 | }, 83 | "test": { 84 | "builder": "@angular/build:karma", 85 | "options": { 86 | "main": "src/test.ts", 87 | "polyfills": "src/polyfills.ts", 88 | "tsConfig": "tsconfig.spec.json", 89 | "karmaConfig": "karma.conf.js", 90 | "assets": [ 91 | "src/favicon.ico", 92 | "src/assets" 93 | ], 94 | "styles": [ 95 | "src/styles.scss" 96 | ], 97 | "scripts": [] 98 | } 99 | } 100 | } 101 | }, 102 | "ngx-diff": { 103 | "projectType": "library", 104 | "root": "projects/ngx-diff", 105 | "sourceRoot": "projects/ngx-diff/src", 106 | "prefix": "ngx", 107 | "architect": { 108 | "build": { 109 | "builder": "@angular/build:ng-packagr", 110 | "options": { 111 | "tsConfig": "projects/ngx-diff/tsconfig.lib.json", 112 | "project": "projects/ngx-diff/ng-package.json" 113 | }, 114 | "configurations": { 115 | "production": { 116 | "tsConfig": "projects/ngx-diff/tsconfig.lib.prod.json" 117 | } 118 | } 119 | }, 120 | "test": { 121 | "builder": "@angular/build:karma", 122 | "options": { 123 | "main": "projects/ngx-diff/src/test.ts", 124 | "tsConfig": "projects/ngx-diff/tsconfig.spec.json", 125 | "karmaConfig": "projects/ngx-diff/karma.conf.js" 126 | } 127 | }, 128 | "lint": { 129 | "builder": "@angular-eslint/builder:lint", 130 | "options": { 131 | "lintFilePatterns": [ 132 | "projects/ngx-diff/**/*.ts", 133 | "projects/ngx-diff/**/*.html" 134 | ] 135 | } 136 | } 137 | } 138 | } 139 | }, 140 | "schematics": { 141 | "@schematics/angular:component": { 142 | "style": "scss", 143 | "type": "component" 144 | }, 145 | "@angular-eslint/schematics:application": { 146 | "setParserOptionsProject": true 147 | }, 148 | "@angular-eslint/schematics:library": { 149 | "setParserOptionsProject": true 150 | }, 151 | "@schematics/angular:directive": { 152 | "type": "directive" 153 | }, 154 | "@schematics/angular:service": { 155 | "type": "service" 156 | }, 157 | "@schematics/angular:guard": { 158 | "typeSeparator": "." 159 | }, 160 | "@schematics/angular:interceptor": { 161 | "typeSeparator": "." 162 | }, 163 | "@schematics/angular:module": { 164 | "typeSeparator": "." 165 | }, 166 | "@schematics/angular:pipe": { 167 | "typeSeparator": "." 168 | }, 169 | "@schematics/angular:resolver": { 170 | "typeSeparator": "." 171 | } 172 | }, 173 | "cli": { 174 | "analytics": "3c2711c5-4843-4cca-a328-5806ff26cc55" 175 | } 176 | } 177 | -------------------------------------------------------------------------------- /commitlint.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { extends: ['@commitlint/config-conventional'] }; 2 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ngx-diff", 3 | "version": "11.0.0", 4 | "type": "module", 5 | "scripts": { 6 | "ng": "ng", 7 | "start": "ng serve", 8 | "build": "ng build ngx-diff --configuration production", 9 | "test": "ng test ngx-diff --no-watch", 10 | "lint": "ng lint", 11 | "e2e": "ng e2e", 12 | "release": "standard-version" 13 | }, 14 | "private": true, 15 | "dependencies": { 16 | "@angular/animations": "^20.0.1", 17 | "@angular/common": "^20.0.1", 18 | "@angular/compiler": "^20.0.1", 19 | "@angular/core": "^20.0.1", 20 | "@angular/forms": "^20.0.1", 21 | "@angular/platform-browser": "^20.0.1", 22 | "@angular/platform-browser-dynamic": "^20.0.1", 23 | "@angular/router": "^20.0.1", 24 | "@commitlint/cli": "^19.8.1", 25 | "@commitlint/config-conventional": "^19.8.1", 26 | "diff-match-patch-ts": "^0.6.0", 27 | "rxjs": "~7.8.2", 28 | "standard-version": "^9.3.2", 29 | "tslib": "^2.3.1", 30 | "zone.js": "~0.15.1" 31 | }, 32 | "devDependencies": { 33 | "@angular-devkit/core": "^20.0.1", 34 | "@angular-eslint/builder": "^20.0.0", 35 | "@angular-eslint/eslint-plugin": "^20.0.0", 36 | "@angular-eslint/eslint-plugin-template": "^20.0.0", 37 | "@angular-eslint/schematics": "20.0.0", 38 | "@angular-eslint/template-parser": "^20.0.0", 39 | "@angular/build": "^20.0.1", 40 | "@angular/cli": "^20.0.1", 41 | "@angular/compiler-cli": "^20.0.1", 42 | "@angular/language-service": "^20.0.1", 43 | "@trivago/prettier-plugin-sort-imports": "^5.2.2", 44 | "@types/jasmine": "^5.1.8", 45 | "@types/jasminewd2": "~2.0.3", 46 | "@typescript-eslint/eslint-plugin": "^8.33.1", 47 | "@typescript-eslint/parser": "^8.33.1", 48 | "eslint": "^9.28.0", 49 | "eslint-plugin-import": "2.31.0", 50 | "eslint-plugin-jsdoc": "50.6.0", 51 | "eslint-plugin-prefer-arrow": "1.2.3", 52 | "husky": "^9.0.11", 53 | "jasmine-core": "~5.1.1", 54 | "jasmine-spec-reporter": "^7.0.0", 55 | "karma": "~6.4.2", 56 | "karma-chrome-launcher": "^3.1.1", 57 | "karma-coverage-istanbul-reporter": "~3.0.2", 58 | "karma-firefox-launcher": "^2.1.2", 59 | "karma-jasmine": "~5.1.0", 60 | "karma-jasmine-html-reporter": "^2.0.0", 61 | "ng-packagr": "^20.0.0", 62 | "prettier": "^3.5.3", 63 | "typescript": "^5.8.3" 64 | }, 65 | "standard-version": { 66 | "skip": { 67 | "commit": true, 68 | "tag": true, 69 | "bump": true 70 | } 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /projects/ngx-diff/.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "../../.eslintrc.json", 3 | "ignorePatterns": ["!**/*"], 4 | "overrides": [ 5 | { 6 | "files": ["*.ts"], 7 | "parserOptions": { 8 | "project": ["projects/ngx-diff/tsconfig.lib.json", "projects/ngx-diff/tsconfig.spec.json"], 9 | "createDefaultProgram": true 10 | }, 11 | "rules": { 12 | "@angular-eslint/component-selector": [ 13 | "error", 14 | { 15 | "type": "element", 16 | "prefix": "ngx", 17 | "style": "kebab-case" 18 | } 19 | ], 20 | "@angular-eslint/directive-selector": [ 21 | "error", 22 | { 23 | "type": "attribute", 24 | "prefix": "ngx", 25 | "style": "camelCase" 26 | } 27 | ], 28 | "@typescript-eslint/explicit-member-accessibility": [ 29 | "off", 30 | { 31 | "accessibility": "explicit" 32 | } 33 | ], 34 | "arrow-parens": ["off", "always"], 35 | "import/order": "off" 36 | } 37 | }, 38 | { 39 | "files": ["*.html"], 40 | "rules": {} 41 | } 42 | ] 43 | } 44 | -------------------------------------------------------------------------------- /projects/ngx-diff/LICENSE: -------------------------------------------------------------------------------- 1 | ../../LICENSE -------------------------------------------------------------------------------- /projects/ngx-diff/README.md: -------------------------------------------------------------------------------- 1 | ../../README.md -------------------------------------------------------------------------------- /projects/ngx-diff/karma.conf.js: -------------------------------------------------------------------------------- 1 | // Karma configuration file, see link for more information 2 | // https://karma-runner.github.io/1.0/config/configuration-file.html 3 | 4 | module.exports = function (config) { 5 | config.set({ 6 | basePath: '', 7 | frameworks: ['jasmine', '@angular-devkit/build-angular'], 8 | plugins: [ 9 | require('karma-jasmine'), 10 | require('karma-chrome-launcher'), 11 | require('karma-jasmine-html-reporter'), 12 | require('karma-coverage-istanbul-reporter'), 13 | 14 | ], 15 | client: { 16 | clearContext: false // leave Jasmine Spec Runner output visible in browser 17 | }, 18 | coverageIstanbulReporter: { 19 | dir: require('path').join(__dirname, '../../coverage/ngx-diff'), 20 | reports: ['html', 'lcovonly', 'text-summary'], 21 | fixWebpackSourcePaths: true 22 | }, 23 | reporters: ['progress', 'kjhtml'], 24 | port: 9876, 25 | colors: true, 26 | logLevel: config.LOG_INFO, 27 | autoWatch: true, 28 | browsers: ['ChromeHeadless'], 29 | singleRun: false, 30 | restartOnFileChange: true 31 | }); 32 | }; 33 | -------------------------------------------------------------------------------- /projects/ngx-diff/ng-package.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "../../node_modules/ng-packagr/ng-package.schema.json", 3 | "dest": "../../dist/ngx-diff", 4 | "lib": { 5 | "entryFile": "src/public-api.ts" 6 | }, 7 | "assets": [ 8 | "styles/*" 9 | ] 10 | } 11 | -------------------------------------------------------------------------------- /projects/ngx-diff/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ngx-diff", 3 | "version": "11.0.0", 4 | "peerDependencies": { 5 | "@angular/common": ">=20.0.0", 6 | "@angular/core": ">=20.0.0", 7 | "diff-match-patch-ts": ">=0.6.0" 8 | }, 9 | "dependencies": { 10 | "tslib": "^2.0.0" 11 | }, 12 | "repository": { 13 | "type": "git", 14 | "url": "https://github.com/rars/ngx-diff" 15 | }, 16 | "keywords": [ 17 | "Angular", 18 | "ng", 19 | "diff" 20 | ], 21 | "author": "Richard Russell", 22 | "license": "MIT", 23 | "bugs": { 24 | "url": "https://github.com/rars/ngx-diff/issues" 25 | }, 26 | "homepage": "https://github.com/rars/ngx-diff#readme", 27 | "exports": { 28 | "./styles/*": { 29 | "style": "./styles/*.css" 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /projects/ngx-diff/src/lib/common/diff-calculation.interface.ts: -------------------------------------------------------------------------------- 1 | import { LineDiffType } from './line-diff-type'; 2 | 3 | /* Holds the state of the calculation of the diff result we intend to display. 4 | * > lines contains the data that will be displayed on screen. 5 | * > lineInOldText keeps track of the document line number in the [oldText] input. 6 | * > lineInNewText keeps track of the document line number in the [newText] input. 7 | */ 8 | export interface IDiffCalculation { 9 | lines: Array<{ 10 | id: string; 11 | type: LineDiffType; 12 | lineNumberInOldText: number | null; 13 | lineNumberInNewText: number | null; 14 | line: string; 15 | args?: { 16 | skippedLines?: string[]; 17 | lineInOldText?: number | null; 18 | lineInNewText?: number | null; 19 | }; 20 | }>; 21 | lineInOldText: number; 22 | lineInNewText: number; 23 | } 24 | -------------------------------------------------------------------------------- /projects/ngx-diff/src/lib/common/line-diff-type.ts: -------------------------------------------------------------------------------- 1 | export const enum LineDiffType { 2 | Equal = 1, 3 | Insert = 2, 4 | Delete = 3, 5 | Placeholder = 4, 6 | } 7 | -------------------------------------------------------------------------------- /projects/ngx-diff/src/lib/common/line-select-event.ts: -------------------------------------------------------------------------------- 1 | import { LineDiffType } from './line-diff-type'; 2 | 3 | export type LineSelectEvent = { 4 | index: number; 5 | type: LineDiffType; 6 | lineNumberInOldText: number | null; 7 | lineNumberInNewText: number | null; 8 | line: string; 9 | }; 10 | -------------------------------------------------------------------------------- /projects/ngx-diff/src/lib/components/inline-diff/inline-diff.component.html: -------------------------------------------------------------------------------- 1 | 8 | -------------------------------------------------------------------------------- /projects/ngx-diff/src/lib/components/inline-diff/inline-diff.component.scss: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rars/ngx-diff/6309d395a9839558b4d91b235f29b1904325134a/projects/ngx-diff/src/lib/components/inline-diff/inline-diff.component.scss -------------------------------------------------------------------------------- /projects/ngx-diff/src/lib/components/inline-diff/inline-diff.component.spec.ts: -------------------------------------------------------------------------------- 1 | import { Diff, DiffOp } from 'diff-match-patch-ts'; 2 | 3 | import { ComponentFixture, TestBed } from '@angular/core/testing'; 4 | 5 | import { LineNumberPipe } from '../../pipes/line-number/line-number.pipe'; 6 | import { DiffMatchPatchService } from '../../services/diff-match-patch/diff-match-patch.service'; 7 | import { InlineDiffComponent } from './inline-diff.component'; 8 | import { UnifiedDiffComponent } from '../unified-diff/unified-diff.component'; 9 | 10 | class DiffMatchPatchServiceMock { 11 | // eslint-disable-next-line @typescript-eslint/naming-convention, @typescript-eslint/no-unused-vars, no-underscore-dangle, id-blacklist, id-match 12 | public computeLineDiff(_oldText: string, _newText: string): Diff[] { 13 | return [ 14 | [DiffOp.Equal, 'Diff One A\r\nDiff One B\r\n'], 15 | [DiffOp.Insert, 'Diff Two A\r\nDiff Two B\r\n'], 16 | [DiffOp.Delete, 'Diff Three A\r\nDiff Three B'], 17 | [DiffOp.Equal, 'Diff Four A\r\nDiff Four B\r\n'], 18 | ]; 19 | } 20 | } 21 | 22 | describe('InlineDiffComponent', () => { 23 | let component: InlineDiffComponent; 24 | let fixture: ComponentFixture; 25 | 26 | beforeEach(async () => { 27 | await TestBed.configureTestingModule({ 28 | imports: [InlineDiffComponent, UnifiedDiffComponent, LineNumberPipe], 29 | providers: [{ provide: DiffMatchPatchService, useClass: DiffMatchPatchServiceMock }], 30 | }).compileComponents(); 31 | 32 | fixture = TestBed.createComponent(InlineDiffComponent); 33 | component = fixture.componentInstance; 34 | fixture.detectChanges(); 35 | }); 36 | 37 | it('should create', () => { 38 | expect(component).toBeTruthy(); 39 | }); 40 | }); 41 | -------------------------------------------------------------------------------- /projects/ngx-diff/src/lib/components/inline-diff/inline-diff.component.ts: -------------------------------------------------------------------------------- 1 | import { Component, EventEmitter, Input, Output } from '@angular/core'; 2 | 3 | import { LineSelectEvent } from '../../common/line-select-event'; 4 | import { UnifiedDiffComponent } from '../unified-diff/unified-diff.component'; 5 | 6 | /** 7 | * This is now just a wrapper around ngx-unified-diff. 8 | * @deprecated use ngx-unified-diff instead. 9 | */ 10 | @Component({ 11 | // eslint-disable-next-line @angular-eslint/component-selector 12 | selector: 'inline-diff', 13 | templateUrl: './inline-diff.component.html', 14 | styleUrls: ['./inline-diff.component.scss'], 15 | imports: [UnifiedDiffComponent] 16 | }) 17 | export class InlineDiffComponent { 18 | /** 19 | * Optional title to be displayed at the top of the diff. 20 | */ 21 | @Input({ required: false }) 22 | public title?: string; 23 | @Input({ required: true }) 24 | public oldText: string | number | boolean | undefined; 25 | @Input({ required: true }) 26 | public newText: string | number | boolean | undefined; 27 | /** 28 | * The number of lines of context to provide either side of a DiffOp.Insert or DiffOp.Delete diff. 29 | * Context is taken from a DiffOp.Equal section. 30 | */ 31 | @Input({ required: false }) 32 | public lineContextSize?: number; 33 | 34 | @Output() 35 | public selectedLineChange = new EventEmitter(); 36 | 37 | protected onSelectedLineChange(event: LineSelectEvent): void { 38 | this.selectedLineChange.emit(event); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /projects/ngx-diff/src/lib/components/side-by-side-diff/side-by-side-diff.component.html: -------------------------------------------------------------------------------- 1 |
2 | @if (title) { 3 | {{ title }}  5 | } 6 | +++ {{ diffSummary.numLinesAdded }}  8 | --- {{ diffSummary.numLinesRemoved }} 9 |
10 | @if (isContentEqual) { 11 |
12 |
There are no changes to display.
13 |
14 | } 15 | @if (!isContentEqual) { 16 |
17 | 18 |
19 | @for (lineDiff of beforeLines; track lineDiff.id; let idx = $index) { 20 |
27 |
{{ lineDiff.lineNumber | lineNumber }}
28 |
29 | } 30 |
31 |
32 |
33 |
34 | @for (lineDiff of beforeLines; track lineDiff.id; let idx = $index) { 35 |
41 |
{{ lineDiff.line }}
42 |
43 | } 44 |
45 |
46 |
47 | 48 |
49 | @for (lineDiff of afterLines; track lineDiff.id; let idx = $index) { 50 |
57 |
{{ lineDiff.lineNumber | lineNumber }}
58 |
59 | } 60 |
61 |
62 |
63 |
64 | @for (lineDiff of afterLines; track lineDiff.id; let idx = $index) { 65 |
71 |
{{ lineDiff.line }}
72 |
73 | } 74 |
75 |
76 |
77 |
78 | } 79 | -------------------------------------------------------------------------------- /projects/ngx-diff/src/lib/components/side-by-side-diff/side-by-side-diff.component.scss: -------------------------------------------------------------------------------- 1 | div.sbs-diff-title-bar { 2 | background-color: var(--ngx-diff-margin-background-color); 3 | color: var(--ngx-diff-font-color); 4 | font-family: var(--ngx-diff-font-family); 5 | font-size: var(--ngx-diff-font-size); 6 | font-weight: var(--ngx-diff-title-font-weight); 7 | padding: var(--ngx-diff-title-bar-padding); 8 | border-top: var(--ngx-diff-border-width) solid var(--ngx-diff-border-color); 9 | border-left: var(--ngx-diff-border-width) solid var(--ngx-diff-border-color); 10 | border-right: var(--ngx-diff-border-width) solid var(--ngx-diff-border-color); 11 | } 12 | 13 | div.sbs-diff-no-changes-text { 14 | font-family: var(--ngx-diff-font-family); 15 | font-size: var(--ngx-diff-font-size); 16 | font-weight: var(--ngx-diff-title-font-weight); 17 | padding: var(--ngx-diff-title-bar-padding); 18 | background-color: var(--ngx-diff-equal-background-color); 19 | color: var(--ngx-diff-font-color); 20 | } 21 | 22 | .sbs-diff-summary-lines-added { 23 | color: var(--ngx-diff-insert-color-darkest); 24 | } 25 | 26 | .sbs-diff-summary-lines-removed { 27 | color: var(--ngx-diff-delete-color-darkest); 28 | } 29 | 30 | div.sbs-diff { 31 | display: flex; 32 | flex-direction: row; 33 | border: var(--ngx-diff-border-width) solid var(--ngx-diff-border-color); 34 | font-family: var(--ngx-diff-font-family); 35 | 36 | div.sbs-diff-margin:last-of-type { 37 | border-left: var(--ngx-diff-border-width) solid var(--ngx-diff-border-color); 38 | } 39 | } 40 | 41 | div.sbs-diff-content { 42 | position: relative; 43 | top: 0px; 44 | left: 0px; 45 | flex-grow: 1; 46 | overflow-x: auto; 47 | overflow-y: hidden; 48 | } 49 | 50 | div.sbs-diff-content-wrapper { 51 | position: absolute; 52 | top: 0px; 53 | left: 0px; 54 | display: flex; 55 | flex-direction: column; 56 | align-items: stretch; 57 | min-width: 100%; 58 | } 59 | 60 | div.sbs-diff-old { 61 | width: var(--ngx-diff-line-number-width); 62 | text-align: center; 63 | font-size: var(--ngx-diff-font-size); 64 | } 65 | 66 | div.sbs-diff-new { 67 | width: var(--ngx-diff-line-number-width); 68 | text-align: center; 69 | border-right: var(--ngx-diff-border-width) solid var(--border-color); 70 | font-size: var(--ngx-diff-font-size); 71 | } 72 | 73 | div.sbs-diff-text { 74 | white-space: pre; 75 | padding-left: var(--ngx-diff-line-left-padding); 76 | font-size: var(--ngx-diff-font-size); 77 | color: var(--ngx-diff-font-color); 78 | } 79 | 80 | .sbs-diff-equal { 81 | background-color: var(--ngx-diff-margin-background-color); 82 | 83 | &.line-content { 84 | background-color: var(--ngx-diff-equal-background-color); 85 | } 86 | } 87 | 88 | .sbs-diff-delete { 89 | background-color: var(--ngx-diff-delete-color-darker); 90 | 91 | &.line-content { 92 | background-color: var(--ngx-diff-deleted-background-color); 93 | } 94 | } 95 | 96 | .sbs-diff-insert { 97 | background-color: var(--ngx-diff-insert-color-darker); 98 | 99 | &.line-content { 100 | background-color: var(--ngx-diff-inserted-background-color); 101 | } 102 | } 103 | 104 | .sbs-diff-delete > div { 105 | display: inline-block; 106 | } 107 | 108 | .sbs-diff-insert > div { 109 | display: inline-block; 110 | } 111 | 112 | .sbs-diff-equal > div { 113 | display: inline-block; 114 | } 115 | 116 | .dmp-margin-bottom-spacer { 117 | height: var(--ngx-diff-bottom-spacer-height); 118 | background-color: var(--ngx-diff-margin-background-color); 119 | border-right: var(--ngx-diff-border-width) solid var(--border-color); 120 | 121 | &.line-content { 122 | background-color: var(--ngx-diff-equal-background-color); 123 | } 124 | } 125 | 126 | .line-selector { 127 | color: var(--ngx-diff-line-number-font-color); 128 | 129 | .sbs-diff-before, 130 | .sbs-diff-after { 131 | width: var(--ngx-diff-line-number-width); 132 | text-align: center; 133 | } 134 | 135 | &:hover { 136 | cursor: pointer; 137 | color: var(--ngx-diff-line-number-hover-font-color); 138 | } 139 | 140 | &.selected { 141 | border-top: var(--ngx-diff-selected-border-width) solid var(--ngx-diff-selected-border-color); 142 | border-left: var(--ngx-diff-selected-border-width) solid var(--ngx-diff-selected-border-color); 143 | border-bottom: var(--ngx-diff-selected-border-width) solid var(--ngx-diff-selected-border-color); 144 | background-color: var(--ngx-diff-selected-line-background-color); 145 | } 146 | } 147 | 148 | .line-content.selected { 149 | border-top: var(--ngx-diff-selected-border-width) solid var(--ngx-diff-selected-border-color); 150 | border-right: var(--ngx-diff-selected-border-width) solid var(--ngx-diff-selected-border-color); 151 | border-bottom: var(--ngx-diff-selected-border-width) solid var(--ngx-diff-selected-border-color); 152 | background-color: var(--ngx-diff-selected-line-background-color); 153 | } 154 | -------------------------------------------------------------------------------- /projects/ngx-diff/src/lib/components/side-by-side-diff/side-by-side-diff.component.spec.ts: -------------------------------------------------------------------------------- 1 | import { ComponentFixture, TestBed } from '@angular/core/testing'; 2 | 3 | import { SideBySideDiffComponent } from './side-by-side-diff.component'; 4 | 5 | describe('SideBySideDiffComponent', () => { 6 | let component: SideBySideDiffComponent; 7 | let fixture: ComponentFixture; 8 | 9 | beforeEach(async () => { 10 | await TestBed.configureTestingModule({ 11 | imports: [SideBySideDiffComponent] 12 | }) 13 | .compileComponents(); 14 | 15 | fixture = TestBed.createComponent(SideBySideDiffComponent); 16 | component = fixture.componentInstance; 17 | fixture.detectChanges(); 18 | }); 19 | 20 | it('should create', () => { 21 | expect(component).toBeTruthy(); 22 | }); 23 | }); 24 | -------------------------------------------------------------------------------- /projects/ngx-diff/src/lib/components/side-by-side-diff/side-by-side-diff.component.ts: -------------------------------------------------------------------------------- 1 | import { Component, EventEmitter, Input, OnChanges, OnInit, Output, inject } from '@angular/core'; 2 | import { Diff, DiffOp } from 'diff-match-patch-ts'; 3 | import { DiffMatchPatchService } from '../../services/diff-match-patch/diff-match-patch.service'; 4 | 5 | import { LineNumberPipe } from '../../pipes/line-number/line-number.pipe'; 6 | import { LineDiffType } from '../../common/line-diff-type'; 7 | import { NgClass } from '@angular/common'; 8 | import { LineSelectEvent } from '../../common/line-select-event'; 9 | 10 | interface IDiffCalculation { 11 | beforeLineNumber: number; 12 | afterLineNumber: number; 13 | } 14 | 15 | interface ILine { 16 | id: string; 17 | type: LineDiffType; 18 | lineNumber: number | null; 19 | line: string | null; 20 | cssClass: string; 21 | args?: { 22 | skippedLines: string[]; 23 | beforeLineNumber: number; 24 | afterLineNumber: number; 25 | }; 26 | } 27 | 28 | @Component({ 29 | selector: 'ngx-side-by-side-diff', 30 | imports: [NgClass, LineNumberPipe], 31 | templateUrl: './side-by-side-diff.component.html', 32 | styleUrl: './side-by-side-diff.component.scss' 33 | }) 34 | export class SideBySideDiffComponent implements OnInit, OnChanges { 35 | private readonly dmp = inject(DiffMatchPatchService); 36 | 37 | /** 38 | * Optional title to be displayed at the top of the diff. 39 | */ 40 | @Input({ required: false }) 41 | public title?: string; 42 | 43 | @Input({ required: true }) 44 | public before?: string; 45 | 46 | @Input({ required: true }) 47 | public after?: string; 48 | 49 | /** 50 | * The number of lines of context to provide either side of a DiffOp.Insert or DiffOp.Delete diff. 51 | * Context is taken from a DiffOp.Equal section. 52 | */ 53 | @Input({ required: false }) 54 | public lineContextSize?: number; 55 | 56 | @Output() 57 | public selectedLineChange = new EventEmitter(); 58 | 59 | public isContentEqual = false; 60 | public diffSummary = { 61 | numLinesAdded: 0, 62 | numLinesRemoved: 0, 63 | }; 64 | 65 | public beforeLines: ILine[] = []; 66 | public afterLines: ILine[] = []; 67 | public selectedLineIndex?: number; 68 | 69 | public ngOnInit(): void { 70 | this.update(); 71 | } 72 | 73 | public ngOnChanges(): void { 74 | this.update(); 75 | } 76 | 77 | public selectLine(index: number): void { 78 | this.selectedLineIndex = index; 79 | 80 | const selectedBeforeLine = this.beforeLines[index]; 81 | const selectedAfterLine = this.afterLines[index]; 82 | 83 | const type = selectedAfterLine.type; 84 | 85 | const line = 86 | (type === LineDiffType.Delete ? selectedBeforeLine.line : selectedAfterLine.line) ?? ''; 87 | 88 | let lineNumberInOldText: number | null = null; 89 | let lineNumberInNewText: number | null = null; 90 | 91 | switch (type) { 92 | case LineDiffType.Insert: { 93 | lineNumberInNewText = selectedAfterLine.lineNumber; 94 | break; 95 | } 96 | case LineDiffType.Delete: { 97 | lineNumberInOldText = selectedBeforeLine.lineNumber; 98 | break; 99 | } 100 | case LineDiffType.Equal: { 101 | lineNumberInOldText = selectedBeforeLine.lineNumber; 102 | lineNumberInNewText = selectedAfterLine.lineNumber; 103 | break; 104 | } 105 | } 106 | 107 | if (type === LineDiffType.Placeholder) { 108 | this.expandPlaceholder(index, selectedBeforeLine); 109 | this.selectedLineIndex = undefined; 110 | } 111 | 112 | this.selectedLineChange.emit({ 113 | index, 114 | type, 115 | lineNumberInOldText, 116 | lineNumberInNewText, 117 | line, 118 | }); 119 | } 120 | 121 | private expandPlaceholder(index: number, placeholder: ILine): void { 122 | const replacementLines = this.getPlaceholderReplacementLines(placeholder); 123 | 124 | this.beforeLines.splice(index, 1, ...replacementLines.beforeLineDiffs); 125 | this.afterLines.splice(index, 1, ...replacementLines.afterLineDiffs); 126 | } 127 | 128 | private getPlaceholderReplacementLines(placeholder: ILine): { 129 | beforeLineDiffs: ILine[]; 130 | afterLineDiffs: ILine[]; 131 | } { 132 | const { skippedLines, beforeLineNumber, afterLineNumber } = placeholder.args!; 133 | 134 | if (this.lineContextSize && skippedLines.length > 2 * this.lineContextSize) { 135 | const prefix = skippedLines.slice(0, this.lineContextSize); 136 | const remainingSkippedLines = skippedLines.slice( 137 | this.lineContextSize, 138 | skippedLines.length - this.lineContextSize, 139 | ); 140 | const suffix = skippedLines.slice( 141 | skippedLines.length - this.lineContextSize, 142 | skippedLines.length, 143 | ); 144 | 145 | const prefixLines = this.createLineDiffs(prefix, beforeLineNumber, afterLineNumber); 146 | 147 | const newPlaceholder: ILine = { 148 | id: `skip-${beforeLineNumber}-${afterLineNumber}-${remainingSkippedLines.length}`, 149 | type: LineDiffType.Placeholder, 150 | lineNumber: null, 151 | line: `... ${remainingSkippedLines.length} hidden lines ...`, 152 | args: { 153 | skippedLines: remainingSkippedLines, 154 | beforeLineNumber: beforeLineNumber + prefix.length, 155 | afterLineNumber: afterLineNumber + prefix.length, 156 | }, 157 | cssClass: this.getCssClass(LineDiffType.Placeholder), 158 | }; 159 | 160 | const numberOfPrefixAndSkippedLines = prefix.length + remainingSkippedLines.length; 161 | 162 | const suffixLines = this.createLineDiffs( 163 | suffix, 164 | beforeLineNumber + numberOfPrefixAndSkippedLines, 165 | afterLineNumber + numberOfPrefixAndSkippedLines, 166 | ); 167 | 168 | return { 169 | beforeLineDiffs: [ 170 | ...prefixLines.beforeLineDiffs, 171 | newPlaceholder, 172 | ...suffixLines.beforeLineDiffs, 173 | ], 174 | afterLineDiffs: [ 175 | ...prefixLines.afterLineDiffs, 176 | newPlaceholder, 177 | ...suffixLines.afterLineDiffs, 178 | ], 179 | }; 180 | } 181 | 182 | return this.createLineDiffs(skippedLines, beforeLineNumber, afterLineNumber); 183 | } 184 | 185 | private createLineDiffs( 186 | lines: string[], 187 | startLineInOldText: number, 188 | startLineInNewText: number, 189 | ): { beforeLineDiffs: ILine[]; afterLineDiffs: ILine[] } { 190 | let beforeLineNumber = startLineInOldText; 191 | let afterLineNumber = startLineInNewText; 192 | 193 | const cssClass = this.getCssClass(LineDiffType.Equal); 194 | 195 | const beforeLineDiffs: ILine[] = []; 196 | const afterLineDiffs: ILine[] = []; 197 | 198 | for (const line of lines) { 199 | const toInsert = { 200 | type: LineDiffType.Equal, 201 | line, 202 | cssClass, 203 | }; 204 | 205 | beforeLineDiffs.push({ 206 | id: `eql-${beforeLineNumber}`, 207 | ...toInsert, 208 | lineNumber: beforeLineNumber, 209 | }); 210 | beforeLineNumber++; 211 | 212 | afterLineDiffs.push({ 213 | id: `eql-${afterLineNumber}`, 214 | ...toInsert, 215 | lineNumber: afterLineNumber, 216 | }); 217 | afterLineNumber++; 218 | } 219 | 220 | return { beforeLineDiffs, afterLineDiffs }; 221 | } 222 | 223 | private update(): void { 224 | const beforeText = this.before ?? ''; 225 | const afterText = this.after ?? ''; 226 | this.calculateLineDiffs(this.dmp.computeLineDiff(beforeText, afterText)); 227 | } 228 | 229 | private calculateLineDiffs(diffs: Diff[]): void { 230 | this.beforeLines = []; 231 | this.afterLines = []; 232 | 233 | const diffCalculation = { 234 | beforeLineNumber: 1, 235 | afterLineNumber: 1, 236 | }; 237 | 238 | this.isContentEqual = diffs.length === 1 && diffs[0][0] === DiffOp.Equal; 239 | 240 | if (this.isContentEqual) { 241 | this.beforeLines = []; 242 | this.afterLines = []; 243 | this.diffSummary = { 244 | numLinesAdded: 0, 245 | numLinesRemoved: 0, 246 | }; 247 | return; 248 | } 249 | 250 | for (let i = 0; i < diffs.length; i++) { 251 | const diff = diffs[i]; 252 | const diffLines: string[] = diff[1].split(/\r?\n/); 253 | 254 | // If the original line had a \r\n at the end then remove the 255 | // empty string after it. 256 | if (diffLines[diffLines.length - 1].length === 0) { 257 | diffLines.pop(); 258 | } 259 | 260 | switch (diff[0]) { 261 | case DiffOp.Equal: { 262 | const isFirstDiff = i === 0; 263 | const isLastDiff = i === diffs.length - 1; 264 | this.outputEqualDiff(diffLines, diffCalculation, isFirstDiff, isLastDiff); 265 | break; 266 | } 267 | case DiffOp.Delete: { 268 | this.outputDeleteDiff(diffLines, diffCalculation); 269 | break; 270 | } 271 | case DiffOp.Insert: { 272 | this.outputInsertDiff(diffLines, diffCalculation); 273 | break; 274 | } 275 | } 276 | } 277 | 278 | this.diffSummary = { 279 | numLinesAdded: this.afterLines.filter((x) => x.type === LineDiffType.Insert).length, 280 | numLinesRemoved: this.beforeLines.filter((x) => x.type === LineDiffType.Delete).length, 281 | }; 282 | } 283 | 284 | /* If the number of diffLines is greater than lineContextSize then we may need to adjust the diff 285 | * that is output. 286 | * > If the first diff of a document is DiffOp.Equal then the leading lines can be dropped 287 | * leaving the last 'lineContextSize' lines for context. 288 | * > If the last diff of a document is DiffOp.Equal then the trailing lines can be dropped 289 | * leaving the first 'lineContextSize' lines for context. 290 | * > If the diff is a DiffOp.Equal occurs in the middle then the diffs either side of it must be 291 | * DiffOp.Insert or DiffOp.Delete. If it has more than 2 * 'lineContextSize' lines of content 292 | * then the middle lines are dropped leaving the first 'lineContextSize' and last 'lineContextSize' 293 | * lines for context. A special line is inserted with '...' indicating that content is skipped. 294 | * 295 | * A document cannot consist of a single Diff with DiffOp.Equal and reach this function because 296 | * in this case the calculateLineDiff method returns early. 297 | */ 298 | private outputEqualDiff( 299 | diffLines: string[], 300 | diffCalculation: IDiffCalculation, 301 | isFirstDiff: boolean, 302 | isLastDiff: boolean, 303 | ): void { 304 | if (this.lineContextSize && diffLines.length > this.lineContextSize) { 305 | if (isFirstDiff) { 306 | // Take the last 'lineContextSize' lines from the first diff 307 | const lineIncrement = diffLines.length - this.lineContextSize; 308 | diffCalculation.beforeLineNumber += lineIncrement; 309 | diffCalculation.afterLineNumber += lineIncrement; 310 | diffLines = diffLines.slice(diffLines.length - this.lineContextSize, diffLines.length); 311 | } else if (isLastDiff) { 312 | // Take only the first 'lineContextSize' lines from the final diff 313 | diffLines = diffLines.slice(0, this.lineContextSize); 314 | } else if (diffLines.length > 2 * this.lineContextSize) { 315 | // Take the first 'lineContextSize' lines from this diff to provide context for the last diff 316 | this.outputEqualDiffLines(diffLines.slice(0, this.lineContextSize), diffCalculation); 317 | 318 | const skippedLines = diffLines.slice( 319 | this.lineContextSize, 320 | diffLines.length - this.lineContextSize, 321 | ); 322 | 323 | // Output a special line indicating that some content is equal and has been skipped 324 | const skippedLine = { 325 | id: `skip-${diffCalculation.beforeLineNumber}-${diffCalculation.afterLineNumber}-${skippedLines.length}`, 326 | type: LineDiffType.Placeholder, 327 | lineNumber: null, 328 | line: `... ${skippedLines.length} hidden lines ...`, 329 | cssClass: this.getCssClass(LineDiffType.Placeholder), 330 | args: { 331 | skippedLines, 332 | beforeLineNumber: diffCalculation.beforeLineNumber, 333 | afterLineNumber: diffCalculation.afterLineNumber, 334 | }, 335 | }; 336 | 337 | this.beforeLines.push(skippedLine); 338 | this.afterLines.push(skippedLine); 339 | 340 | const numberOfSkippedLines = diffLines.length - 2 * this.lineContextSize; 341 | diffCalculation.beforeLineNumber += numberOfSkippedLines; 342 | diffCalculation.afterLineNumber += numberOfSkippedLines; 343 | 344 | // Take the last 'lineContextSize' lines from this diff to provide context for the next diff 345 | this.outputEqualDiffLines( 346 | diffLines.slice(diffLines.length - this.lineContextSize), 347 | diffCalculation, 348 | ); 349 | // This if branch has already output the diff lines so we return early to avoid outputting the lines 350 | // at the end of the method. 351 | return; 352 | } 353 | } 354 | this.outputEqualDiffLines(diffLines, diffCalculation); 355 | } 356 | 357 | private outputEqualDiffLines(diffLines: string[], diffCalculation: IDiffCalculation): void { 358 | for (const line of diffLines) { 359 | this.beforeLines.push({ 360 | id: `eql-${diffCalculation.beforeLineNumber}`, 361 | type: LineDiffType.Equal, 362 | lineNumber: diffCalculation.beforeLineNumber, 363 | line, 364 | cssClass: this.getCssClass(LineDiffType.Equal), 365 | }); 366 | 367 | this.afterLines.push({ 368 | id: `eql-${diffCalculation.afterLineNumber}`, 369 | type: LineDiffType.Equal, 370 | lineNumber: diffCalculation.afterLineNumber, 371 | line, 372 | cssClass: this.getCssClass(LineDiffType.Equal), 373 | }); 374 | 375 | diffCalculation.beforeLineNumber++; 376 | diffCalculation.afterLineNumber++; 377 | } 378 | } 379 | 380 | private outputDeleteDiff(diffLines: string[], diffCalculation: IDiffCalculation): void { 381 | for (const line of diffLines) { 382 | this.beforeLines.push({ 383 | id: `del-${diffCalculation.beforeLineNumber}`, 384 | type: LineDiffType.Delete, 385 | lineNumber: diffCalculation.beforeLineNumber, 386 | line, 387 | cssClass: this.getCssClass(LineDiffType.Delete), 388 | }); 389 | 390 | this.afterLines.push({ 391 | id: `del-${diffCalculation.beforeLineNumber}`, 392 | type: LineDiffType.Delete, 393 | lineNumber: null, 394 | line: null, 395 | cssClass: this.getCssClass(LineDiffType.Delete), 396 | }); 397 | 398 | diffCalculation.beforeLineNumber++; 399 | } 400 | } 401 | 402 | private outputInsertDiff(diffLines: string[], diffCalculation: IDiffCalculation): void { 403 | for (const line of diffLines) { 404 | this.beforeLines.push({ 405 | id: `ins-${diffCalculation.afterLineNumber}`, 406 | type: LineDiffType.Insert, 407 | lineNumber: null, 408 | line: null, 409 | cssClass: this.getCssClass(LineDiffType.Insert), 410 | }); 411 | 412 | this.afterLines.push({ 413 | id: `ins-${diffCalculation.afterLineNumber}`, 414 | type: LineDiffType.Insert, 415 | lineNumber: diffCalculation.afterLineNumber, 416 | line, 417 | cssClass: this.getCssClass(LineDiffType.Insert), 418 | }); 419 | 420 | diffCalculation.afterLineNumber++; 421 | } 422 | } 423 | 424 | private getCssClass(type: LineDiffType): string { 425 | switch (type) { 426 | case LineDiffType.Placeholder: 427 | case LineDiffType.Equal: 428 | return 'sbs-diff-equal'; 429 | case LineDiffType.Insert: 430 | return 'sbs-diff-insert'; 431 | case LineDiffType.Delete: 432 | return 'sbs-diff-delete'; 433 | default: 434 | return 'unknown'; 435 | } 436 | } 437 | } 438 | -------------------------------------------------------------------------------- /projects/ngx-diff/src/lib/components/unified-diff/unified-diff.component.html: -------------------------------------------------------------------------------- 1 |
2 | @if (title) { 3 | {{ title }}  5 | } 6 | +++ {{ diffSummary.numLinesAdded }}  8 | --- {{ diffSummary.numLinesRemoved }} 9 |
10 | @if (isContentEqual) { 11 |
12 |
There are no changes to display.
13 |
14 | } 15 | @if (!isContentEqual) { 16 |
17 |
18 | @for (lineDiff of calculatedDiff; track lineDiff.id; let idx = $index) { 19 |
26 |
{{ lineDiff.lineNumberInOldText | lineNumber }}
27 |
{{ lineDiff.lineNumberInNewText | lineNumber }}
28 |
29 | } 30 |
31 |
32 |
33 |
34 | @for (lineDiff of calculatedDiff; track lineDiff.id) { 35 |
41 |
{{ lineDiff.line }}
42 |
43 | } 44 |
45 |
46 |
47 |
48 | } 49 | -------------------------------------------------------------------------------- /projects/ngx-diff/src/lib/components/unified-diff/unified-diff.component.scss: -------------------------------------------------------------------------------- 1 | div.ufd-diff-title-bar { 2 | background-color: var(--ngx-diff-margin-background-color); 3 | color: var(--ngx-diff-font-color); 4 | font-family: var(--ngx-diff-font-family); 5 | font-size: var(--ngx-diff-font-size); 6 | font-weight: var(--ngx-diff-title-font-weight); 7 | padding: var(--ngx-diff-title-bar-padding); 8 | border-top: var(--ngx-diff-border-width) solid var(--ngx-diff-border-color); 9 | border-left: var(--ngx-diff-border-width) solid var(--ngx-diff-border-color); 10 | border-right: var(--ngx-diff-border-width) solid var(--ngx-diff-border-color); 11 | } 12 | 13 | div.ufd-diff-no-changes-text { 14 | font-family: var(--ngx-diff-font-family); 15 | font-size: var(--ngx-diff-font-size); 16 | font-weight: var(--ngx-diff-title-font-weight); 17 | padding: var(--ngx-diff-title-bar-padding); 18 | background-color: var(--ngx-diff-equal-background-color); 19 | color: var(--ngx-diff-font-color); 20 | flex-grow: 1; 21 | } 22 | 23 | .ufd-diff-summary-lines-added { 24 | color: var(--ngx-diff-insert-color-darkest); 25 | } 26 | 27 | .ufd-diff-summary-lines-removed { 28 | color: var(--ngx-diff-delete-color-darkest); 29 | } 30 | 31 | div.ufd-diff { 32 | display: flex; 33 | flex-direction: row; 34 | border: var(--ngx-diff-border-width) solid var(--ngx-diff-border-color); 35 | font-family: var(--ngx-diff-font-family); 36 | } 37 | 38 | div.ufd-diff-content { 39 | position: relative; 40 | top: 0px; 41 | left: 0px; 42 | flex-grow: 1; 43 | overflow-x: auto; 44 | overflow-y: hidden; 45 | } 46 | 47 | div.ufd-diff-content-wrapper { 48 | position: absolute; 49 | top: 0px; 50 | left: 0px; 51 | display: flex; 52 | flex-direction: column; 53 | align-items: stretch; 54 | min-width: 100%; 55 | } 56 | 57 | div.ufd-diff-old { 58 | width: var(--ngx-diff-line-number-width); 59 | text-align: center; 60 | font-size: var(--ngx-diff-font-size); 61 | } 62 | 63 | div.ufd-diff-new { 64 | width: var(--ngx-diff-line-number-width); 65 | text-align: center; 66 | border-right: var(--ngx-diff-border-width) solid var(--border-color); 67 | font-size: var(--ngx-diff-font-size); 68 | } 69 | 70 | div.ufd-diff-text { 71 | white-space: pre; 72 | padding-left: var(--ngx-diff-line-left-padding); 73 | font-size: var(--ngx-diff-font-size); 74 | color: var(--ngx-diff-font-color); 75 | } 76 | 77 | .ufd-diff-equal { 78 | background-color: var(--ngx-diff-margin-background-color); 79 | 80 | &.line-content { 81 | background-color: var(--ngx-diff-equal-background-color); 82 | } 83 | } 84 | 85 | .ufd-diff-delete { 86 | background-color: var(--ngx-diff-delete-color-darker); 87 | 88 | &.line-content { 89 | background-color: var(--ngx-diff-deleted-background-color); 90 | } 91 | } 92 | 93 | .ufd-diff-insert { 94 | background-color: var(--ngx-diff-insert-color-darker); 95 | 96 | &.line-content { 97 | background-color: var(--ngx-diff-inserted-background-color); 98 | } 99 | } 100 | 101 | .ufd-diff-delete > div { 102 | display: inline-block; 103 | } 104 | 105 | .ufd-diff-insert > div { 106 | display: inline-block; 107 | } 108 | 109 | .ufd-diff-equal > div { 110 | display: inline-block; 111 | } 112 | 113 | .dmp-margin-bottom-spacer { 114 | height: var(--ngx-diff-bottom-spacer-height); 115 | background-color: var(--ngx-diff-margin-background-color); 116 | border-right: var(--ngx-diff-border-width) solid var(--border-color); 117 | 118 | &.line-content { 119 | background-color: var(--ngx-diff-equal-background-color); 120 | } 121 | } 122 | 123 | .line-selector { 124 | color: var(--ngx-diff-line-number-font-color); 125 | 126 | &:hover { 127 | cursor: pointer; 128 | color: var(--ngx-diff-line-number-hover-font-color); 129 | } 130 | 131 | &.selected { 132 | border-top: var(--ngx-diff-selected-border-width) solid var(--ngx-diff-selected-border-color); 133 | border-left: var(--ngx-diff-selected-border-width) solid var(--ngx-diff-selected-border-color); 134 | border-bottom: var(--ngx-diff-selected-border-width) solid var(--ngx-diff-selected-border-color); 135 | background-color: var(--ngx-diff-selected-line-background-color); 136 | } 137 | } 138 | 139 | .line-content.selected { 140 | border-top: var(--ngx-diff-selected-border-width) solid var(--ngx-diff-selected-border-color); 141 | border-right: var(--ngx-diff-selected-border-width) solid var(--ngx-diff-selected-border-color); 142 | border-bottom: var(--ngx-diff-selected-border-width) solid var(--ngx-diff-selected-border-color); 143 | background-color: var(--ngx-diff-selected-line-background-color); 144 | } 145 | -------------------------------------------------------------------------------- /projects/ngx-diff/src/lib/components/unified-diff/unified-diff.component.spec.ts: -------------------------------------------------------------------------------- 1 | import { ComponentFixture, TestBed } from '@angular/core/testing'; 2 | 3 | import { UnifiedDiffComponent } from './unified-diff.component'; 4 | 5 | import { Diff, DiffOp } from 'diff-match-patch-ts'; 6 | 7 | import { LineDiffType } from '../../common/line-diff-type'; 8 | import { LineNumberPipe } from '../../pipes/line-number/line-number.pipe'; 9 | import { DiffMatchPatchService } from '../../services/diff-match-patch/diff-match-patch.service'; 10 | 11 | class DiffMatchPatchServiceMock { 12 | // eslint-disable-next-line @typescript-eslint/naming-convention, @typescript-eslint/no-unused-vars, no-underscore-dangle, id-blacklist, id-match 13 | public computeLineDiff(_oldText: string, _newText: string): Diff[] { 14 | return [ 15 | [DiffOp.Equal, 'Diff One A\r\nDiff One B\r\n'], 16 | [DiffOp.Insert, 'Diff Two A\r\nDiff Two B\r\n'], 17 | [DiffOp.Delete, 'Diff Three A\r\nDiff Three B'], 18 | [DiffOp.Equal, 'Diff Four A\r\nDiff Four B\r\n'], 19 | ]; 20 | } 21 | } 22 | 23 | describe('UnifiedDiffComponent', () => { 24 | let component: UnifiedDiffComponent; 25 | let fixture: ComponentFixture; 26 | 27 | beforeEach(async () => { 28 | await TestBed.configureTestingModule({ 29 | imports: [UnifiedDiffComponent, LineNumberPipe], 30 | providers: [{ provide: DiffMatchPatchService, useClass: DiffMatchPatchServiceMock }], 31 | }).compileComponents(); 32 | 33 | fixture = TestBed.createComponent(UnifiedDiffComponent); 34 | component = fixture.componentInstance; 35 | fixture.detectChanges(); 36 | }); 37 | 38 | it('should create', () => { 39 | expect(component).toBeTruthy(); 40 | }); 41 | 42 | it('should have 8 line diffs', () => { 43 | expect(component.calculatedDiff.length).toBe(8); 44 | }); 45 | 46 | it('should have correct line numbers', () => { 47 | const leftLineNumbers = component.calculatedDiff.map((x) => x.lineNumberInOldText); 48 | expect(leftLineNumbers).toEqual([1, 2, null, null, 3, 4, 5, 6]); 49 | 50 | const rightLineNumbers = component.calculatedDiff.map((x) => x.lineNumberInNewText); 51 | expect(rightLineNumbers).toEqual([1, 2, 3, 4, null, null, 5, 6]); 52 | }); 53 | 54 | it('should have correct class annotations', () => { 55 | const classes = component.calculatedDiff.map((x) => x.type); 56 | expect(classes).toEqual([ 57 | LineDiffType.Equal, 58 | LineDiffType.Equal, 59 | LineDiffType.Insert, 60 | LineDiffType.Insert, 61 | LineDiffType.Delete, 62 | LineDiffType.Delete, 63 | LineDiffType.Equal, 64 | LineDiffType.Equal, 65 | ]); 66 | }); 67 | 68 | it('should have correct line contents', () => { 69 | const contents = component.calculatedDiff.map((x) => x.line); 70 | expect(contents).toEqual([ 71 | 'Diff One A', 72 | 'Diff One B', 73 | 'Diff Two A', 74 | 'Diff Two B', 75 | 'Diff Three A', 76 | 'Diff Three B', 77 | 'Diff Four A', 78 | 'Diff Four B', 79 | ]); 80 | }); 81 | }); 82 | -------------------------------------------------------------------------------- /projects/ngx-diff/src/lib/components/unified-diff/unified-diff.component.ts: -------------------------------------------------------------------------------- 1 | import { Diff, DiffOp } from 'diff-match-patch-ts'; 2 | 3 | import { Component, EventEmitter, Input, OnChanges, OnInit, Output, inject } from '@angular/core'; 4 | 5 | import { IDiffCalculation } from '../../common/diff-calculation.interface'; 6 | import { LineDiffType } from '../../common/line-diff-type'; 7 | import { LineSelectEvent } from '../../common/line-select-event'; 8 | import { DiffMatchPatchService } from '../../services/diff-match-patch/diff-match-patch.service'; 9 | import { LineNumberPipe } from '../../pipes/line-number/line-number.pipe'; 10 | import { NgClass } from '@angular/common'; 11 | 12 | type LineDiff = { 13 | id: string; 14 | type: LineDiffType; 15 | lineNumberInOldText: number | null; 16 | lineNumberInNewText: number | null; 17 | line: string; 18 | args?: { skippedLines?: string[]; lineInOldText?: number | null; lineInNewText?: number | null }; 19 | cssClass: string; 20 | }; 21 | 22 | @Component({ 23 | selector: 'ngx-unified-diff', 24 | imports: [NgClass, LineNumberPipe], 25 | templateUrl: './unified-diff.component.html', 26 | styleUrl: './unified-diff.component.scss' 27 | }) 28 | export class UnifiedDiffComponent implements OnInit, OnChanges { 29 | private readonly dmp = inject(DiffMatchPatchService); 30 | 31 | /** 32 | * Optional title to be displayed at the top of the diff. 33 | */ 34 | @Input({ required: false }) 35 | public title?: string; 36 | @Input({ required: true }) 37 | public before: string | number | boolean | undefined; 38 | @Input({ required: true }) 39 | public after: string | number | boolean | undefined; 40 | /** 41 | * The number of lines of context to provide either side of a DiffOp.Insert or DiffOp.Delete diff. 42 | * Context is taken from a DiffOp.Equal section. 43 | */ 44 | @Input({ required: false }) 45 | public lineContextSize?: number; 46 | 47 | @Output() 48 | public selectedLineChange = new EventEmitter(); 49 | 50 | public diffSummary = { 51 | numLinesAdded: 0, 52 | numLinesRemoved: 0, 53 | }; 54 | public calculatedDiff: LineDiff[] = []; 55 | public selectedLine?: LineDiff; 56 | public isContentEqual: boolean = false; 57 | 58 | public ngOnInit(): void { 59 | this.updateHtml(); 60 | } 61 | 62 | public ngOnChanges(): void { 63 | this.updateHtml(); 64 | } 65 | 66 | public selectLine(index: number, lineDiff: LineDiff): void { 67 | this.selectedLine = lineDiff; 68 | const { type, lineNumberInOldText, lineNumberInNewText, line } = lineDiff; 69 | 70 | if (type === LineDiffType.Placeholder) { 71 | this.expandPlaceholder(index, lineDiff); 72 | this.selectedLine = undefined; 73 | } 74 | 75 | this.selectedLineChange.emit({ 76 | index, 77 | type, 78 | lineNumberInOldText, 79 | lineNumberInNewText, 80 | line, 81 | }); 82 | } 83 | 84 | private expandPlaceholder(index: number, placeholder: LineDiff): void { 85 | const replacementLines = this.getPlaceholderReplacementLines(placeholder); 86 | this.calculatedDiff.splice(index, 1, ...replacementLines); 87 | } 88 | 89 | private getPlaceholderReplacementLines(placeholder: LineDiff): LineDiff[] { 90 | const skippedLines = placeholder.args?.skippedLines ?? []; 91 | const lineInOldText = placeholder.args?.lineInOldText ?? 0; 92 | const lineInNewText = placeholder.args?.lineInNewText ?? 0; 93 | 94 | if (this.lineContextSize && skippedLines.length > 2 * this.lineContextSize) { 95 | const prefix = skippedLines.slice(0, this.lineContextSize); 96 | const remainingSkippedLines = skippedLines.slice( 97 | this.lineContextSize, 98 | skippedLines.length - this.lineContextSize, 99 | ); 100 | const suffix = skippedLines.slice( 101 | skippedLines.length - this.lineContextSize, 102 | skippedLines.length, 103 | ); 104 | 105 | const prefixLines = this.createLineDiffs(prefix, lineInOldText, lineInNewText); 106 | 107 | const newPlaceholder: LineDiff = { 108 | id: `skip-${lineInOldText + prefix.length}-${lineInNewText + prefix.length}-${remainingSkippedLines.length}`, 109 | type: LineDiffType.Placeholder, 110 | lineNumberInOldText: null, 111 | lineNumberInNewText: null, 112 | line: `... ${remainingSkippedLines.length} hidden lines ...`, 113 | args: { 114 | skippedLines: remainingSkippedLines, 115 | lineInOldText: lineInOldText + prefix.length, 116 | lineInNewText: lineInNewText + prefix.length, 117 | }, 118 | cssClass: this.getCssClass(LineDiffType.Placeholder), 119 | }; 120 | 121 | const numberOfPrefixAndSkippedLines = prefix.length + remainingSkippedLines.length; 122 | 123 | const suffixLines = this.createLineDiffs( 124 | suffix, 125 | lineInOldText + numberOfPrefixAndSkippedLines, 126 | lineInNewText + numberOfPrefixAndSkippedLines, 127 | ); 128 | 129 | return [...prefixLines, newPlaceholder, ...suffixLines]; 130 | } 131 | 132 | return this.createLineDiffs(skippedLines, lineInOldText, lineInNewText); 133 | } 134 | 135 | private createLineDiffs( 136 | lines: string[], 137 | startLineInOldText: number, 138 | startLineInNewText: number, 139 | ): LineDiff[] { 140 | let lineNumberInOldText = startLineInOldText; 141 | let lineNumberInNewText = startLineInNewText; 142 | 143 | const cssClass = this.getCssClass(LineDiffType.Equal); 144 | const linesToInsert: LineDiff[] = []; 145 | 146 | for (const line of lines) { 147 | linesToInsert.push({ 148 | id: `eql-${lineNumberInOldText}-${lineNumberInNewText}`, 149 | type: LineDiffType.Equal, 150 | lineNumberInOldText, 151 | lineNumberInNewText, 152 | line: line, 153 | cssClass, 154 | }); 155 | lineNumberInOldText++; 156 | lineNumberInNewText++; 157 | } 158 | 159 | return linesToInsert; 160 | } 161 | 162 | private updateHtml(): void { 163 | if (typeof this.before === 'number' || typeof this.before === 'boolean') { 164 | this.before = this.before.toString(); 165 | } 166 | if (typeof this.after === 'number' || typeof this.after === 'boolean') { 167 | this.after = this.after.toString(); 168 | } 169 | this.calculateLineDiff(this.dmp.computeLineDiff(this.before ?? '', this.after ?? '')); 170 | } 171 | 172 | private calculateLineDiff(diffs: Diff[]): void { 173 | const diffCalculation: IDiffCalculation = { 174 | lineInNewText: 1, 175 | lineInOldText: 1, 176 | lines: [], 177 | }; 178 | 179 | this.isContentEqual = diffs.length === 1 && diffs[0][0] === DiffOp.Equal; 180 | if (this.isContentEqual) { 181 | this.calculatedDiff = []; 182 | this.diffSummary = { 183 | numLinesAdded: 0, 184 | numLinesRemoved: 0, 185 | }; 186 | return; 187 | } 188 | 189 | for (let i = 0; i < diffs.length; i++) { 190 | const diff = diffs[i]; 191 | const diffLines: string[] = diff[1].split(/\r?\n/); 192 | 193 | // If the original line had a \r\n at the end then remove the 194 | // empty string after it. 195 | if (diffLines[diffLines.length - 1].length === 0) { 196 | diffLines.pop(); 197 | } 198 | 199 | switch (diff[0]) { 200 | case DiffOp.Equal: { 201 | const isFirstDiff = i === 0; 202 | const isLastDiff = i === diffs.length - 1; 203 | this.outputEqualDiff(diffLines, diffCalculation, isFirstDiff, isLastDiff); 204 | break; 205 | } 206 | case DiffOp.Delete: { 207 | this.outputDeleteDiff(diffLines, diffCalculation); 208 | break; 209 | } 210 | case DiffOp.Insert: { 211 | this.outputInsertDiff(diffLines, diffCalculation); 212 | break; 213 | } 214 | } 215 | } 216 | 217 | this.calculatedDiff = diffCalculation.lines.map( 218 | ({ id, type, lineNumberInOldText, lineNumberInNewText, line, args }) => { 219 | return { 220 | id, 221 | type, 222 | lineNumberInOldText, 223 | lineNumberInNewText, 224 | line, 225 | args, 226 | cssClass: this.getCssClass(type), 227 | }; 228 | }, 229 | ); 230 | 231 | this.diffSummary = { 232 | numLinesAdded: this.calculatedDiff.filter((x) => x.type === LineDiffType.Insert).length, 233 | numLinesRemoved: this.calculatedDiff.filter((x) => x.type === LineDiffType.Delete).length, 234 | }; 235 | } 236 | 237 | /* If the number of diffLines is greater than lineContextSize then we may need to adjust the diff 238 | * that is output. 239 | * > If the first diff of a document is DiffOp.Equal then the leading lines can be dropped 240 | * leaving the last 'lineContextSize' lines for context. 241 | * > If the last diff of a document is DiffOp.Equal then the trailing lines can be dropped 242 | * leaving the first 'lineContextSize' lines for context. 243 | * > If the diff is a DiffOp.Equal occurs in the middle then the diffs either side of it must be 244 | * DiffOp.Insert or DiffOp.Delete. If it has more than 2 * 'lineContextSize' lines of content 245 | * then the middle lines are dropped leaving the first 'lineContextSize' and last 'lineContextSize' 246 | * lines for context. A special line is inserted with '...' indicating that content is skipped. 247 | * 248 | * A document cannot consist of a single Diff with DiffOp.Equal and reach this function because 249 | * in this case the calculateLineDiff method returns early. 250 | */ 251 | private outputEqualDiff( 252 | diffLines: string[], 253 | diffCalculation: IDiffCalculation, 254 | isFirstDiff: boolean, 255 | isLastDiff: boolean, 256 | ): void { 257 | if (this.lineContextSize && diffLines.length > this.lineContextSize) { 258 | if (isFirstDiff) { 259 | // Take the last 'lineContextSize' lines from the first diff 260 | const lineIncrement = diffLines.length - this.lineContextSize; 261 | diffCalculation.lineInOldText += lineIncrement; 262 | diffCalculation.lineInNewText += lineIncrement; 263 | diffLines = diffLines.slice(diffLines.length - this.lineContextSize, diffLines.length); 264 | } else if (isLastDiff) { 265 | // Take only the first 'lineContextSize' lines from the final diff 266 | diffLines = diffLines.slice(0, this.lineContextSize); 267 | } else if (diffLines.length > 2 * this.lineContextSize) { 268 | // Take the first 'lineContextSize' lines from this diff to provide context for the last diff 269 | this.outputEqualDiffLines(diffLines.slice(0, this.lineContextSize), diffCalculation); 270 | 271 | const skippedLines = diffLines.slice( 272 | this.lineContextSize, 273 | diffLines.length - this.lineContextSize, 274 | ); 275 | 276 | // Output a special line indicating that some content is equal and has been skipped 277 | diffCalculation.lines.push({ 278 | id: `skip-${diffCalculation.lineInOldText}-${diffCalculation.lineInNewText}-${skippedLines.length}`, 279 | type: LineDiffType.Placeholder, 280 | lineNumberInOldText: null, 281 | lineNumberInNewText: null, 282 | line: `... ${skippedLines.length} hidden lines ...`, 283 | args: { 284 | skippedLines, 285 | lineInOldText: diffCalculation.lineInOldText, 286 | lineInNewText: diffCalculation.lineInNewText, 287 | }, 288 | }); 289 | const numberOfSkippedLines = diffLines.length - 2 * this.lineContextSize; 290 | diffCalculation.lineInOldText += numberOfSkippedLines; 291 | diffCalculation.lineInNewText += numberOfSkippedLines; 292 | 293 | // Take the last 'lineContextSize' lines from this diff to provide context for the next diff 294 | this.outputEqualDiffLines( 295 | diffLines.slice(diffLines.length - this.lineContextSize), 296 | diffCalculation, 297 | ); 298 | // This if branch has already output the diff lines so we return early to avoid outputting the lines 299 | // at the end of the method. 300 | return; 301 | } 302 | } 303 | this.outputEqualDiffLines(diffLines, diffCalculation); 304 | } 305 | 306 | private outputEqualDiffLines(diffLines: string[], diffCalculation: IDiffCalculation): void { 307 | for (const line of diffLines) { 308 | diffCalculation.lines.push({ 309 | id: `eql-${diffCalculation.lineInOldText}-${diffCalculation.lineInNewText}`, 310 | type: LineDiffType.Equal, 311 | lineNumberInOldText: diffCalculation.lineInOldText, 312 | lineNumberInNewText: diffCalculation.lineInNewText, 313 | line, 314 | }); 315 | diffCalculation.lineInOldText++; 316 | diffCalculation.lineInNewText++; 317 | } 318 | } 319 | 320 | private outputDeleteDiff(diffLines: string[], diffCalculation: IDiffCalculation): void { 321 | for (const line of diffLines) { 322 | diffCalculation.lines.push({ 323 | id: `del-${diffCalculation.lineInOldText}`, 324 | type: LineDiffType.Delete, 325 | lineNumberInOldText: diffCalculation.lineInOldText, 326 | lineNumberInNewText: null, 327 | line, 328 | }); 329 | diffCalculation.lineInOldText++; 330 | } 331 | } 332 | 333 | private outputInsertDiff(diffLines: string[], diffCalculation: IDiffCalculation): void { 334 | for (const line of diffLines) { 335 | diffCalculation.lines.push({ 336 | id: `ins-${diffCalculation.lineInNewText}`, 337 | type: LineDiffType.Insert, 338 | lineNumberInOldText: null, 339 | lineNumberInNewText: diffCalculation.lineInNewText, 340 | line, 341 | }); 342 | diffCalculation.lineInNewText++; 343 | } 344 | } 345 | 346 | private getCssClass(type: LineDiffType): string { 347 | switch (type) { 348 | case LineDiffType.Placeholder: 349 | case LineDiffType.Equal: 350 | return 'ufd-diff-equal'; 351 | case LineDiffType.Insert: 352 | return 'ufd-diff-insert'; 353 | case LineDiffType.Delete: 354 | return 'ufd-diff-delete'; 355 | default: 356 | return 'unknown'; 357 | } 358 | } 359 | } 360 | -------------------------------------------------------------------------------- /projects/ngx-diff/src/lib/pipes/line-number/line-number.pipe.spec.ts: -------------------------------------------------------------------------------- 1 | import { LineNumberPipe } from './line-number.pipe'; 2 | 3 | describe('LineNumberPipe', () => { 4 | it('create an instance', () => { 5 | const pipe = new LineNumberPipe(); 6 | expect(pipe).toBeTruthy(); 7 | }); 8 | }); 9 | -------------------------------------------------------------------------------- /projects/ngx-diff/src/lib/pipes/line-number/line-number.pipe.ts: -------------------------------------------------------------------------------- 1 | import { Pipe, PipeTransform } from '@angular/core'; 2 | 3 | @Pipe({ 4 | name: 'lineNumber', 5 | standalone: true, 6 | }) 7 | export class LineNumberPipe implements PipeTransform { 8 | public transform(value: number | null): string { 9 | return value === null ? '-' : `${value}`; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /projects/ngx-diff/src/lib/services/diff-match-patch/diff-match-patch.service.spec.ts: -------------------------------------------------------------------------------- 1 | import { TestBed } from '@angular/core/testing'; 2 | 3 | import { DiffMatchPatchService } from './diff-match-patch.service'; 4 | 5 | describe('DiffMatchPatchService', () => { 6 | let service: DiffMatchPatchService; 7 | 8 | beforeEach(() => { 9 | TestBed.configureTestingModule({}); 10 | service = TestBed.inject(DiffMatchPatchService); 11 | }); 12 | 13 | it('should be created', () => { 14 | expect(service).toBeTruthy(); 15 | }); 16 | }); 17 | -------------------------------------------------------------------------------- /projects/ngx-diff/src/lib/services/diff-match-patch/diff-match-patch.service.ts: -------------------------------------------------------------------------------- 1 | import { type Diff, DiffMatchPatch } from 'diff-match-patch-ts'; 2 | 3 | import { Injectable } from '@angular/core'; 4 | 5 | @Injectable({ 6 | providedIn: 'root', 7 | }) 8 | export class DiffMatchPatchService { 9 | private readonly dmp = new DiffMatchPatch(); 10 | 11 | public computeLineDiff(text1: string, text2: string): Diff[] { 12 | return this.dmp.diff_lineMode(text1, text2); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /projects/ngx-diff/src/public-api.ts: -------------------------------------------------------------------------------- 1 | /* 2 | * Public API Surface of ngx-diff 3 | */ 4 | 5 | export * from './lib/services/diff-match-patch/diff-match-patch.service'; 6 | export * from './lib/components/inline-diff/inline-diff.component'; 7 | export * from './lib/components/side-by-side-diff/side-by-side-diff.component'; 8 | export * from './lib/components/unified-diff/unified-diff.component'; 9 | export * from './lib/common/line-select-event'; 10 | export * from './lib/common/line-diff-type'; 11 | -------------------------------------------------------------------------------- /projects/ngx-diff/src/test.ts: -------------------------------------------------------------------------------- 1 | // This file is required by karma.conf.js and loads recursively all the .spec and framework files 2 | 3 | import 'zone.js'; 4 | import 'zone.js/testing'; 5 | import { getTestBed } from '@angular/core/testing'; 6 | import { 7 | BrowserDynamicTestingModule, 8 | platformBrowserDynamicTesting, 9 | } from '@angular/platform-browser-dynamic/testing'; 10 | 11 | // First, initialize the Angular testing environment. 12 | getTestBed().initTestEnvironment(BrowserDynamicTestingModule, platformBrowserDynamicTesting(), { 13 | teardown: { destroyAfterEach: false }, 14 | }); 15 | -------------------------------------------------------------------------------- /projects/ngx-diff/styles/default-theme.css: -------------------------------------------------------------------------------- 1 | .ngx-diff-light-theme, 2 | .ngx-diff-dark-theme { 3 | --ngx-diff-border-color: #dfdfdf; 4 | --ngx-diff-font-size: 0.9rem; 5 | --ngx-diff-font-family: Consolas, Courier, monospace; 6 | --ngx-diff-font-color: #000; 7 | --ngx-diff-line-number-font-color: #aaaaaa; 8 | --ngx-diff-line-number-hover-font-color: #484848; 9 | 10 | --ngx-diff-selected-border-width: 0; 11 | --ngx-diff-selected-border-color: #000; 12 | --ngx-diff-selected-line-background-color: #d6f1ff; 13 | 14 | --ngx-diff-line-number-width: 2rem; 15 | --ngx-diff-border-width: 1px; 16 | --ngx-diff-line-left-padding: 1rem; 17 | --ngx-diff-bottom-spacer-height: 1rem; 18 | --ngx-diff-title-bar-padding: 0.5rem; 19 | --ngx-diff-title-font-weight: 600; 20 | 21 | --ngx-diff-insert-color: #d6ffd6; 22 | --ngx-diff-delete-color: #ffd6d6; 23 | --ngx-diff-equal-color: #ffffff; 24 | --ngx-diff-mix-color: #000; 25 | --ngx-diff-light-mix-percentage: 4%; 26 | --ngx-diff-heavy-mix-percentage: 10%; 27 | 28 | --ngx-diff-inserted-background-color: var(--ngx-diff-insert-color); 29 | --ngx-diff-deleted-background-color: var(--ngx-diff-delete-color); 30 | --ngx-diff-equal-background-color: var(--ngx-diff-equal-color); 31 | --ngx-diff-margin-background-color: color-mix( 32 | in srgb, 33 | var(--ngx-diff-equal-color), 34 | var(--ngx-diff-mix-color) var(--ngx-diff-light-mix-percentage) 35 | ); 36 | 37 | --ngx-diff-insert-color-darker: color-mix( 38 | in srgb, 39 | var(--ngx-diff-insert-color), 40 | var(--ngx-diff-mix-color) var(--ngx-diff-light-mix-percentage) 41 | ); 42 | --ngx-diff-insert-color-darkest: color-mix( 43 | in srgb, 44 | var(--ngx-diff-insert-color), 45 | var(--ngx-diff-mix-color) var(--ngx-diff-heavy-mix-percentage) 46 | ); 47 | 48 | --ngx-diff-delete-color-darker: color-mix( 49 | in srgb, 50 | var(--ngx-diff-delete-color), 51 | var(--ngx-diff-mix-color) var(--ngx-diff-light-mix-percentage) 52 | ); 53 | --ngx-diff-delete-color-darkest: color-mix( 54 | in srgb, 55 | var(--ngx-diff-delete-color), 56 | var(--ngx-diff-mix-color) var(--ngx-diff-heavy-mix-percentage) 57 | ); 58 | } 59 | 60 | .ngx-diff-dark-theme { 61 | --ngx-diff-border-color: #474747; 62 | 63 | --ngx-diff-font-color: #ffffff; 64 | --ngx-diff-line-number-font-color: #636363; 65 | --ngx-diff-line-number-hover-font-color: #ffffff; 66 | 67 | --ngx-diff-selected-line-background-color: #354a54; 68 | 69 | --ngx-diff-insert-color: #355435; 70 | --ngx-diff-delete-color: #543535; 71 | --ngx-diff-equal-color: #292929; 72 | --ngx-diff-mix-color: #fff; 73 | --ngx-diff-light-mix-percentage: 4%; 74 | --ngx-diff-heavy-mix-percentage: 10%; 75 | } 76 | -------------------------------------------------------------------------------- /projects/ngx-diff/tsconfig.lib.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "../../tsconfig.json", 3 | "compilerOptions": { 4 | "outDir": "../../out-tsc/lib", 5 | "declarationMap": true, 6 | "declaration": true, 7 | "inlineSources": true, 8 | "types": [], 9 | "lib": [ 10 | "dom", 11 | "es2018" 12 | ] 13 | }, 14 | "angularCompilerOptions": { 15 | "skipTemplateCodegen": true, 16 | "strictMetadataEmit": true, 17 | "enableResourceInlining": true 18 | }, 19 | "exclude": [ 20 | "src/test.ts", 21 | "**/*.spec.ts" 22 | ] 23 | } 24 | -------------------------------------------------------------------------------- /projects/ngx-diff/tsconfig.lib.prod.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./tsconfig.lib.json", 3 | "compilerOptions": { 4 | "declarationMap": false 5 | }, 6 | "angularCompilerOptions": { 7 | "compilationMode": "partial" 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /projects/ngx-diff/tsconfig.spec.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "../../tsconfig.json", 3 | "compilerOptions": { 4 | "outDir": "../../out-tsc/spec", 5 | "types": [ 6 | "jasmine", 7 | "node" 8 | ] 9 | }, 10 | "files": [ 11 | "src/test.ts" 12 | ], 13 | "include": [ 14 | "**/*.spec.ts", 15 | "**/*.d.ts" 16 | ] 17 | } 18 | -------------------------------------------------------------------------------- /src/app/app-routing.module.ts: -------------------------------------------------------------------------------- 1 | import { NgModule } from '@angular/core'; 2 | import { Routes, RouterModule } from '@angular/router'; 3 | 4 | 5 | const routes: Routes = []; 6 | 7 | @NgModule({ 8 | imports: [RouterModule.forRoot(routes, {})], 9 | exports: [RouterModule] 10 | }) 11 | export class AppRoutingModule { } 12 | -------------------------------------------------------------------------------- /src/app/app.component.html: -------------------------------------------------------------------------------- 1 |
2 | 10 |
11 | 20 |
21 | 28 |
29 | 37 |
38 | 46 |
47 | 56 |
57 | -------------------------------------------------------------------------------- /src/app/app.component.scss: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rars/ngx-diff/6309d395a9839558b4d91b235f29b1904325134a/src/app/app.component.scss -------------------------------------------------------------------------------- /src/app/app.component.spec.ts: -------------------------------------------------------------------------------- 1 | import { TestBed, waitForAsync } from '@angular/core/testing'; 2 | import { RouterTestingModule } from '@angular/router/testing'; 3 | import { AppComponent } from './app.component'; 4 | 5 | describe('AppComponent', () => { 6 | beforeEach(waitForAsync(() => { 7 | TestBed.configureTestingModule({ 8 | imports: [ 9 | RouterTestingModule 10 | ], 11 | declarations: [AppComponent], 12 | }).compileComponents(); 13 | })); 14 | 15 | it('should create the app', () => { 16 | const fixture = TestBed.createComponent(AppComponent); 17 | const app = fixture.componentInstance; 18 | expect(app).toBeTruthy(); 19 | }); 20 | 21 | it(`should have as title 'ngx-diff'`, () => { 22 | const fixture = TestBed.createComponent(AppComponent); 23 | const app = fixture.componentInstance; 24 | expect(app.title).toEqual('ngx-diff'); 25 | }); 26 | 27 | it('should render title', () => { 28 | const fixture = TestBed.createComponent(AppComponent); 29 | fixture.detectChanges(); 30 | const compiled = fixture.nativeElement; 31 | expect(compiled.querySelector('.content span').textContent).toContain('ngx-diff app is running!'); 32 | }); 33 | }); 34 | -------------------------------------------------------------------------------- /src/app/app.component.ts: -------------------------------------------------------------------------------- 1 | import { InlineDiffComponent, SideBySideDiffComponent } from 'ngx-diff'; 2 | 3 | import { Component } from '@angular/core'; 4 | 5 | @Component({ 6 | selector: 'app-root', 7 | templateUrl: './app.component.html', 8 | styleUrls: ['./app.component.scss'], 9 | imports: [InlineDiffComponent, SideBySideDiffComponent] 10 | }) 11 | export class AppComponent { 12 | public oldText = `common text 13 | common text 14 | common text 15 | common text 16 | common text 17 | common text 18 | common text 19 | common text 20 | common text 21 | common text 22 | common text 23 | common text 24 | common text 25 | common text 26 | common text 27 | common text 28 | apples 29 | oranges 30 | common text 31 | common text 32 | common text 33 | common text 34 | common text 35 | common text 36 | common text 37 | common text 38 | common text 39 | common text 40 | common text 41 | common text 42 | common text 43 | common text 44 | common text 45 | common text 46 | common text 47 | common text 48 | common text 49 | common text 50 | common text 51 | common text 52 | common text 53 | common text 54 | common text 55 | common text 56 | common text 57 | common text 58 | kiwis 59 | carrots 60 | `; 61 | public newText = `common text 62 | common text 63 | common text 64 | common text 65 | common text 66 | common text 67 | common text 68 | common text 69 | common text 70 | common text 71 | common text 72 | common text 73 | common text 74 | common text 75 | common text 76 | apples 77 | pears 78 | common text 79 | common text 80 | common text 81 | common text 82 | common text 83 | common text 84 | common text 85 | common text 86 | common text 87 | common text 88 | common text 89 | common text 90 | common text 91 | common text 92 | common text 93 | common text 94 | common text 95 | common text 96 | common text 97 | common text 98 | common text 99 | common text 100 | common text 101 | common text 102 | common text 103 | common text 104 | common text 105 | common text 106 | kiwis 107 | grapefruit 108 | carrots 109 | `; 110 | 111 | public selectedLineChange(event: unknown): void { 112 | console.log(event); 113 | } 114 | } 115 | -------------------------------------------------------------------------------- /src/assets/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rars/ngx-diff/6309d395a9839558b4d91b235f29b1904325134a/src/assets/.gitkeep -------------------------------------------------------------------------------- /src/environments/environment.prod.ts: -------------------------------------------------------------------------------- 1 | export const environment = { 2 | production: true 3 | }; 4 | -------------------------------------------------------------------------------- /src/environments/environment.ts: -------------------------------------------------------------------------------- 1 | // This file can be replaced during build by using the `fileReplacements` array. 2 | // `ng build --prod` replaces `environment.ts` with `environment.prod.ts`. 3 | // The list of file replacements can be found in `angular.json`. 4 | 5 | export const environment = { 6 | production: false 7 | }; 8 | 9 | /* 10 | * For easier debugging in development mode, you can import the following file 11 | * to ignore zone related error stack frames such as `zone.run`, `zoneDelegate.invokeTask`. 12 | * 13 | * This import should be commented out in production mode because it will have a negative impact 14 | * on performance if an error is thrown. 15 | */ 16 | // import 'zone.js/plugins/zone-error'; // Included with Angular CLI. 17 | -------------------------------------------------------------------------------- /src/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rars/ngx-diff/6309d395a9839558b4d91b235f29b1904325134a/src/favicon.ico -------------------------------------------------------------------------------- /src/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | ngx-diff 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /src/main.ts: -------------------------------------------------------------------------------- 1 | import { enableProdMode, importProvidersFrom } from '@angular/core'; 2 | import { BrowserModule, bootstrapApplication } from '@angular/platform-browser'; 3 | 4 | import { AppRoutingModule } from './app/app-routing.module'; 5 | import { AppComponent } from './app/app.component'; 6 | import { environment } from './environments/environment'; 7 | 8 | if (environment.production) { 9 | enableProdMode(); 10 | } 11 | 12 | bootstrapApplication(AppComponent, { 13 | providers: [importProvidersFrom(BrowserModule, AppRoutingModule)], 14 | }).catch((err) => console.error(err)); 15 | -------------------------------------------------------------------------------- /src/polyfills.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * This file includes polyfills needed by Angular and is loaded before the app. 3 | * You can add your own extra polyfills to this file. 4 | * 5 | * This file is divided into 2 sections: 6 | * 1. Browser polyfills. These are applied before loading ZoneJS and are sorted by browsers. 7 | * 2. Application imports. Files imported after ZoneJS that should be loaded before your main 8 | * file. 9 | * 10 | * The current setup is for so-called "evergreen" browsers; the last versions of browsers that 11 | * automatically update themselves. This includes Safari >= 10, Chrome >= 55 (including Opera), 12 | * Edge >= 13 on the desktop, and iOS 10 and Chrome on mobile. 13 | * 14 | * Learn more in https://angular.io/guide/browser-support 15 | */ 16 | 17 | /*************************************************************************************************** 18 | * BROWSER POLYFILLS 19 | */ 20 | 21 | /** 22 | * By default, zone.js will patch all possible macroTask and DomEvents 23 | * user can disable parts of macroTask/DomEvents patch by setting following flags 24 | * because those flags need to be set before `zone.js` being loaded, and webpack 25 | * will put import in the top of bundle, so user need to create a separate file 26 | * in this directory (for example: zone-flags.ts), and put the following flags 27 | * into that file, and then add the following code before importing zone.js. 28 | * import './zone-flags.ts'; 29 | * 30 | * The flags allowed in zone-flags.ts are listed here. 31 | * 32 | * The following flags will work for all browsers. 33 | * 34 | * (window as any).__Zone_disable_requestAnimationFrame = true; // disable patch requestAnimationFrame 35 | * (window as any).__Zone_disable_on_property = true; // disable patch onProperty such as onclick 36 | * (window as any).__zone_symbol__UNPATCHED_EVENTS = ['scroll', 'mousemove']; // disable patch specified eventNames 37 | * 38 | * in IE/Edge developer tools, the addEventListener will also be wrapped by zone.js 39 | * with the following flag, it will bypass `zone.js` patch for IE/Edge 40 | * 41 | * (window as any).__Zone_enable_cross_context_check = true; 42 | * 43 | */ 44 | 45 | /*************************************************************************************************** 46 | * Zone JS is required by default for Angular itself. 47 | */ 48 | import 'zone.js'; // Included with Angular CLI. 49 | 50 | 51 | /*************************************************************************************************** 52 | * APPLICATION IMPORTS 53 | */ 54 | -------------------------------------------------------------------------------- /src/styles.scss: -------------------------------------------------------------------------------- 1 | /* You can add global styles to this file, and also import other style files */ 2 | 3 | @use '../projects/ngx-diff/styles/default-theme'; 4 | 5 | :root .my-inline-diff-theme { 6 | --ngx-diff-border-color: yellow; 7 | --ngx-diff-selected-border-color: blue; 8 | --ngx-diff-font-family: Consolas; 9 | --ngx-diff-line-number-hover-font-color: white; 10 | --ngx-diff-line-number-width: 2rem; 11 | --ngx-diff-font-size: 1rem; 12 | --ngx-diff-line-left-padding: 1rem; 13 | --ngx-diff-bottom-spacer-height: 1rem; 14 | 15 | --ngx-diff-insert-color-darker: color-mix(in srgb, var(--ngx-diff-insert-color), #000 15%); 16 | --ngx-diff-insert-color-darkest: color-mix(in srgb, var(--ngx-diff-insert-color), #000 30%); 17 | --ngx-diff-delete-color-darker: color-mix(in srgb, var(--ngx-diff-delete-color), #000 15%); 18 | --ngx-diff-delete-color-darkest: color-mix(in srgb, var(--ngx-diff-delete-color), #000 30%); 19 | } 20 | -------------------------------------------------------------------------------- /src/test.ts: -------------------------------------------------------------------------------- 1 | // This file is required by karma.conf.js and loads recursively all the .spec and framework files 2 | 3 | import 'zone.js/testing'; 4 | import { getTestBed } from '@angular/core/testing'; 5 | import { 6 | BrowserDynamicTestingModule, 7 | platformBrowserDynamicTesting 8 | } from '@angular/platform-browser-dynamic/testing'; 9 | 10 | // First, initialize the Angular testing environment. 11 | getTestBed().initTestEnvironment( 12 | BrowserDynamicTestingModule, 13 | platformBrowserDynamicTesting(), { 14 | teardown: { destroyAfterEach: false } 15 | } 16 | ); 17 | -------------------------------------------------------------------------------- /tsconfig.app.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./tsconfig.json", 3 | "compilerOptions": { 4 | "outDir": "./out-tsc/app", 5 | "types": [] 6 | }, 7 | "files": [ 8 | "src/main.ts", 9 | "src/polyfills.ts" 10 | ], 11 | "include": [ 12 | "src/**/*.d.ts" 13 | ] 14 | } 15 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compileOnSave": false, 3 | "compilerOptions": { 4 | "baseUrl": "./", 5 | "outDir": "./dist/out-tsc", 6 | "sourceMap": true, 7 | "esModuleInterop": true, 8 | "declaration": false, 9 | "experimentalDecorators": true, 10 | "module": "es2020", 11 | "moduleResolution": "bundler", 12 | "noImplicitAny": true, 13 | "noImplicitReturns": true, 14 | "noImplicitThis": true, 15 | "noUnusedLocals": true, 16 | "noUnusedParameters": true, 17 | "strict": true, 18 | "importHelpers": true, 19 | "target": "ES2022", 20 | "typeRoots": [ 21 | "node_modules/@types" 22 | ], 23 | "lib": [ 24 | "es2018", 25 | "dom" 26 | ], 27 | "paths": { 28 | "ngx-diff": [ 29 | "dist/ngx-diff/ngx-diff", 30 | "dist/ngx-diff" 31 | ] 32 | }, 33 | "useDefineForClassFields": false 34 | }, 35 | "angularCompilerOptions": { 36 | "fullTemplateTypeCheck": true, 37 | "strictInjectionParameters": true 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /tsconfig.spec.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./tsconfig.json", 3 | "compilerOptions": { 4 | "outDir": "./out-tsc/spec", 5 | "types": [ 6 | "jasmine", 7 | "node" 8 | ] 9 | }, 10 | "files": [ 11 | "src/test.ts", 12 | "src/polyfills.ts" 13 | ], 14 | "include": [ 15 | "src/**/*.spec.ts", 16 | "src/**/*.d.ts" 17 | ] 18 | } 19 | --------------------------------------------------------------------------------