1.12.0 • Published 16 days ago

v-code-diff v1.12.0

Weekly downloads
-
License
MIT
Repository
-
Last release
16 days ago

v-code-diff

NPM version License: MIT Downloads

A code diff display plugin, available for Vue2 / Vue3.

Old Version:

0.x version, latest version 0.3.12 (traditional version, improved based on vue-code-diff, is no longer maintained. We will try to align the functionality of 0.x version in 1.x version and minimize migration cost as much as possible). This project references the following projects, and I would like to express my gratitude to the original authors!

Contents

Install

install v-code-diff

# npm
npm i v-code-diff

# yarn
yarn add v-code-diff

# pnpm
pnpm add v-code-diff

Vue2.6 developers need install composition-api

pnpm add @vue/composition-api

Getting Started

Vue3

Register locally

Recommend using local registration for better tree-shaking support.

<script setup>
import { CodeDiff } from 'v-code-diff'
</script>

Register globally

import { createApp } from 'vue'
import CodeDiff from 'v-code-diff'

app
  .use(CodeDiff)
  .mount('#app')

then

<template>
  <code-diff
    old-string="12345"
    new-string="3456"
    output-format="side-by-side"
  />
</template>

Vue2

Register locally

Recommend using local registration for better tree-shaking support.

<script>
import { CodeDiff } from 'v-code-diff'
export default {
  components: {
    CodeDiff
  }
}
</script>

Vue.use(CodeDiff)

## Demo

|        | npm | cdn                                                                            |
| ------ | --- | ------------------------------------------------------------------------------ |
| vue2   |     | [vue2-cdn](https://stackblitz.com/edit/v-code-diff-vue2-cdn?file=index.html)   |
| vue2.7 |     | [vue27-cdn](https://stackblitz.com/edit/v-code-diff-vue27-cdn?file=index.html) |
| vue3   |     | [vue3-cdn](https://stackblitz.com/edit/v-code-diff-vue3-cdn?file=index.html)   |

## Props

| Prop                | Description                                                                                                                                                             | Type      | Optional Values           | Default Value |
|---------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------|---------------------------|---------------|
| language            | Code language, such as typescript, defaults to plain text. [View all supported languages](https://github.com/highlightjs/highlight.js/blob/main/SUPPORTED_LANGUAGES.md) | string    | -                         | plaintext     |
| oldString           | Old string                                                                                                                                                              | string    | -                         | -             |
| newString           | New string                                                                                                                                                              | string    | -                         | -             |
| context             | The number of lines to separate different parts so that they are not hidden                                                                                             | number    | -                         | 10            |
| outputFormat        | Display mode                                                                                                                                                            | string    | line-by-line,side-by-side | line-by-line  |
| diffStyle           | Difference style, word-level differences or letter-level differences                                                                                                    | string    | word, char                | word          |
|forceInlineComparison| Force inline comparison (word or char level)                                                                                                                            | boolean   | -                         | false         |
| trim                | Remove blank characters at the beginning and end of the string                                                                                                          | boolean   | -                         | false         |
| noDiffLineFeed      | Don't diff Windows line feed (CRLF) and Linux line feed (LF)                                                                                                            | boolean   | -                         | false         |
| maxHeight           | Maximum height of component, for example: 300px                                                                                                                         | string    | -                         | undefined     |
| filename            | Filename                                                                                                                                                                | string    | -                         | undefined     |
| newFilename         | New filename                                                                                                                                                            | string    | -                         | undefined     |
| hideHeader          | Hide header bar                                                                                                                                                         | boolean   | -                         | false         |
| hideStat            | Hide statistical part in the header bar                                                                                                                                 | boolean   | -                         | false         |
| theme               | Add dark mode                                                                                                                                                           | ThemeType | light , dark              | light         |
| ignoreMatchingLines | Give a pattern to ignore matching lines eg: '(time\|token)'                                                                                                             | string    | -                         | undefined     |

## Events

| Name | Description                 | Type                                                                            |
| ---- | --------------------------- | ------------------------------------------------------------------------------- |
| diff | triggers when diff finished | (result: {stat: { isChanged: boolean, addNum: number, delNum: number}}) => void |

## Slot

| Name | Description                                                 |
| ---- | ----------------------------------------------------------- |
| stat | Custom statistical content, The scope parameter is { stat } |

## Extend languages

In order to reduce the size of the packaged file, the system only supports the following commonly used languages by
default.

- plaintext
- xml/html
- javascript
- json
- yaml
- python
- java
- bash
- sql

If the language you need is not included, you can manually import the relevant language highlighting module.

```bash
pnpm add highlight.js

Register locally

Recommend using local registration for better tree-shaking support.

<script>
import { CodeDiff, hljs } from 'v-code-diff'
import c from 'highlight.js/lib/languages/c'
// Extend C language
hljs.registerLanguage('c', c)
export default {
  components: {
    CodeDiff,
  }
}
</script>

Register globally

import CodeDiff from 'v-code-diff'
// Extend C language
import c from 'highlight.js/lib/languages/c'

CodeDiff.hljs.registerLanguage('c', c)

Migrate from 0.x version

The v-code-diff 1.x version has features such as reduced packaging size and improved performance compared to the 0.x version. And we will try to align the functions with the 0.x version as much as possible to reduce your migration cost.

Key points:

  • In the 1.x version, language recognition and highlighting will no longer be automatically performed, you need to manually specify the language type, such as language="python", if not specified, it will default to plaintext and will not be highlighted.
  • In the 1.x version, due to the fact that rendering and highlighting are performed at the same time, the component events have been removed.
  • In the 1.x version, the following component properties (Prop) have been changed:
    • highlight - removed
    • drawFileList - removed
    • fileName - rename to "filename"
    • newFilename - new
    • theme - new

Below is a detailed comparison of the two versions, you can refer to it to complete the migration.

The difference of event.

The component events are no longer provided in the 1.x version as rendering and highlighting are carried out simultaneously.

Event NameChange Status
before-renderNo longer provided
after-renderNo longer provided

The difference of prop.

PropDescriptionChange Status
highlightControl code highlightingRemoved in version 1.x
languageCode languageNone
oldStringOld stringNone
newStringNew stringNone
contextThe number of lines to separate different parts so that they are not hiddenNone
output-formatDisplay modeNone
diffStyleDifference style, word-level differences or letter-level differencesNone
drawFileListDisplay file comparison listRemoved in version 1.x
renderNothingWhenEmptyDo not render when there is no comparisonRemoved in version 1.x
fileNameFile nameTo be determined, not under development
newFilenameNew in version 1. To be determined, not under development
isShowNoChangeDisplay source code when there is no comparisonRemoved as it became the default in version 1.x
trimRemove blank characters at the beginning and end of the stringNone
noDiffLineFeedDon't diff Windows line feed (CRLF) and Linux line feed (LF)None
themeAdd dark modeNew in version 1

Star History

Star History Chart

1.12.0

16 days ago

1.11.0

2 months ago

1.10.0

3 months ago

1.9.0

4 months ago

1.7.2

7 months ago

1.8.0

6 months ago

1.7.1

9 months ago

1.6.2

10 months ago

1.7.0

10 months ago

1.6.1

10 months ago

1.6.0

10 months ago

1.5.1

11 months ago

1.5.0

12 months ago

1.2.0

1 year ago

1.1.0

1 year ago

1.0.0

1 year ago

1.3.2

1 year ago

1.4.0

1 year ago

1.3.1

1 year ago

1.3.0

1 year ago

1.0.0-alpha.1

1 year ago

0.3.12

2 years ago

0.3.11

2 years ago

0.3.9

2 years ago

0.3.10

2 years ago

0.3.8

2 years ago

0.3.6

3 years ago

0.3.5

3 years ago

0.3.7

2 years ago

0.3.4

3 years ago

0.3.3

3 years ago

0.3.2

3 years ago

0.3.1

3 years ago

0.3.0

3 years ago

0.2.1

3 years ago

0.2.0

3 years ago

0.1.0

3 years ago

0.0.4

3 years ago

0.0.3

3 years ago

0.0.2

3 years ago

0.0.1

3 years ago