v-code-diff 1.14.1 → 1.16.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "v-code-diff",
3
3
  "type": "module",
4
- "version": "1.14.1",
4
+ "version": "1.16.0",
5
5
  "packageManager": "pnpm@11.2.2",
6
6
  "description": "A code diff viewer for Vue 2.6, Vue 2.7, and Vue 3",
7
7
  "license": "MIT",
@@ -72,30 +72,26 @@
72
72
  "dependencies": {
73
73
  "diff": "^5.2.2",
74
74
  "diff-match-patch": "^1.0.5",
75
- "highlight.js": "^11.10.0",
76
- "vue-demi": "^0.14.6"
75
+ "highlight.js": "^11.12.0",
76
+ "vue-demi": "^0.14.10"
77
77
  },
78
78
  "devDependencies": {
79
- "@antfu/eslint-config": "^2.4.5",
80
- "@types/diff": "^5.2.2",
79
+ "@antfu/eslint-config": "^2.27.3",
80
+ "@types/diff": "^5.2.3",
81
81
  "@types/diff-match-patch": "^1.0.36",
82
- "@types/node": "^18.11.18",
83
- "@unocss/eslint-plugin": "^0.58.0",
84
- "@vueuse/core": "^10.7.0",
82
+ "@types/node": "^22.19.15",
85
83
  "bumpp": "^8.2.1",
86
- "eslint": "^8.55.0",
84
+ "eslint": "^8.57.1",
87
85
  "npm-run-all": "^4.1.5",
88
86
  "rimraf": "^3.0.2",
89
- "sass": "^1.69.5",
90
- "tsx": "^4.19.4",
91
- "typescript": "~4.7.4",
92
- "vite": "^5.4.2",
87
+ "sass": "^1.103.1",
88
+ "tsx": "^4.23.12",
89
+ "typescript": "~5.9.3",
90
+ "vite": "^6.4.3",
93
91
  "vite-plugin-css-injected-by-js": "^2.4.0",
94
- "vitest": "^2.1.9",
95
- "vue": "^3.4.38",
96
- "vue-i18n": "^9.14.0",
97
- "vue-tsc": "^0.40.13",
98
- "vue2": "npm:vue@2",
99
- "vue3": "npm:vue@3"
92
+ "vitest": "^4.1.11",
93
+ "vue": "^3.5.41",
94
+ "vue2": "npm:vue@^2.7.16",
95
+ "vue3": "npm:vue@^3.5.41"
100
96
  }
101
97
  }
package/src/CodeDiff.vue CHANGED
@@ -1,5 +1,6 @@
1
1
  <script setup lang="ts">
2
2
  import { computed, ref, shallowRef, watch } from 'vue-demi'
3
+ import type { DiffChangeClickEvent } from './types'
3
4
  import { createSplitDiff, createUnifiedDiff } from './utils'
4
5
  import UnifiedViewer from './unified/UnifiedViewer.vue'
5
6
  import SplitViewer from './split/SplitViewer.vue'
@@ -23,6 +24,7 @@ interface Props {
23
24
  newFilename?: string
24
25
  hideHeader?: boolean
25
26
  hideStat?: boolean
27
+ hideNavigation?: boolean
26
28
  theme?: 'light' | 'dark'
27
29
  // Give a pattern to ignore matching lines eg: '(time|token)'
28
30
  ignoreMatchingLines?: string
@@ -41,12 +43,14 @@ const props = withDefaults(defineProps<Props>(), {
41
43
  newFilename: undefined,
42
44
  hideHeader: false,
43
45
  hideStat: false,
46
+ hideNavigation: false,
44
47
  theme: 'light',
45
48
  ignoreMatchingLines: undefined,
46
49
  })
47
50
 
48
51
  const emits = defineEmits<{
49
52
  (e: 'diff', diffResult: DiffResult): void
53
+ (e: 'change-click', payload: DiffChangeClickEvent): void
50
54
  }>()
51
55
 
52
56
  interface DiffResult {
@@ -132,7 +136,7 @@ watch(() => props, () => {
132
136
  <div class="info-left">{{ filename }}</div>
133
137
  <div class="info-left">{{ newFilename }}</div>
134
138
  </span>
135
- <span class="diff-commandbar">
139
+ <span v-if="!hideNavigation" class="diff-commandbar">
136
140
  <button class="command-item-button" title="Next Change" @click="goToNextDiff">
137
141
  <DownArrowIcon />
138
142
  </button>
@@ -140,6 +144,7 @@ watch(() => props, () => {
140
144
  <UpArrowIcon />
141
145
  </button>
142
146
  </span>
147
+ <slot name="header-actions" />
143
148
  <span v-if="!hideStat" class="diff-stat">
144
149
  <slot name="stat" :stat="diffChange.stat">
145
150
  <span class="diff-stat-added">+{{ diffChange.stat.additionsNum }} additions</span>
@@ -152,7 +157,7 @@ watch(() => props, () => {
152
157
  <span class="info-left">{{ filename }}</span>
153
158
  <span class="info-right">
154
159
  <span style="margin-left: 20px;">{{ newFilename }}</span>
155
- <span class="diff-commandbar">
160
+ <span v-if="!hideNavigation" class="diff-commandbar">
156
161
  <button class="command-item-button" title="Next Change" @click="goToNextDiff">
157
162
  <DownArrowIcon />
158
163
  </button>
@@ -160,6 +165,7 @@ watch(() => props, () => {
160
165
  <UpArrowIcon />
161
166
  </button>
162
167
  </span>
168
+ <slot name="header-actions" />
163
169
  <span v-if="!hideStat" class="diff-stat">
164
170
  <slot name="stat" :stat="diffChange.stat">
165
171
  <span class="diff-stat-added">+{{ diffChange.stat.additionsNum }} additions</span>
@@ -169,8 +175,8 @@ watch(() => props, () => {
169
175
  </span>
170
176
  </div>
171
177
  </div>
172
- <UnifiedViewer v-if="isUnifiedViewer" :diff-change="diffChange" :language="language" />
173
- <SplitViewer v-else :diff-change="diffChange" :language="language" />
178
+ <UnifiedViewer v-if="isUnifiedViewer" :diff-change="diffChange" :language="language" @change-click="emits('change-click', $event)" />
179
+ <SplitViewer v-else :diff-change="diffChange" :language="language" @change-click="emits('change-click', $event)" />
174
180
  </div>
175
181
  </template>
176
182
 
@@ -1,12 +1,15 @@
1
1
  <script setup lang="ts">
2
2
  import { DiffType } from '../types'
3
- import type { SplitLineChange } from '../types'
3
+ import type { DiffChangeClickEvent, DiffLine, SplitLineChange } from '../types'
4
4
 
5
5
  defineProps<{
6
6
  splitLine: SplitLineChange
7
7
  }>()
8
8
 
9
- const emit = defineEmits(['expand'])
9
+ const emit = defineEmits<{
10
+ (e: 'expand', line: SplitLineChange): void
11
+ (e: 'change-click', payload: DiffChangeClickEvent): void
12
+ }>()
10
13
 
11
14
  function getCodeMarker(type: DiffType) {
12
15
  if (type === DiffType.DELETE)
@@ -27,6 +30,19 @@ function onSplitLineMousedown(event: MouseEvent, side: 'left' | 'right') {
27
30
  for (const el of leftElements)
28
31
  el.classList.toggle('no-select', side === 'right')
29
32
  }
33
+
34
+ function onChangeClick(event: MouseEvent, line: DiffLine, side: 'old' | 'new') {
35
+ if (line.type !== DiffType.ADD && line.type !== DiffType.DELETE)
36
+ return
37
+
38
+ // Vue 2 DOM templates require kebab-case event names.
39
+ emit('change-click', {
40
+ side,
41
+ type: line.type,
42
+ lineNumber: line.num!,
43
+ event,
44
+ })
45
+ }
30
46
  </script>
31
47
 
32
48
  <template>
@@ -71,6 +87,7 @@ function onSplitLineMousedown(event: MouseEvent, side: 'left' | 'right') {
71
87
  'split-side-right': index === 1,
72
88
  }"
73
89
  @mousedown="onSplitLineMousedown($event, index === 0 ? 'left' : 'right')"
90
+ @click="onChangeClick($event, line, index === 0 ? 'old' : 'new')"
74
91
  >
75
92
  <span
76
93
  class="blob-code-inner blob-code-marker" :data-code-marker="getCodeMarker(line.type)"
@@ -1,6 +1,6 @@
1
1
  <script setup lang="ts">
2
2
  import { computed, ref, shallowRef, watch } from 'vue-demi'
3
- import type { SplitLineChange, SplitViewerChange } from '../types'
3
+ import type { DiffChangeClickEvent, SplitLineChange, SplitViewerChange } from '../types'
4
4
  import { RENDER_BATCH_SIZE, highlightSplitLine } from '../utils'
5
5
  import SplitLine from './SplitLine.vue'
6
6
 
@@ -9,6 +9,10 @@ const props = defineProps<{
9
9
  language: string
10
10
  }>()
11
11
 
12
+ const emit = defineEmits<{
13
+ (e: 'change-click', payload: DiffChangeClickEvent): void
14
+ }>()
15
+
12
16
  const renderLimit = ref(RENDER_BATCH_SIZE)
13
17
  const visibleChanges = shallowRef(props.diffChange.changes.filter(line => !line.hide || line.hideIndex !== undefined))
14
18
  const renderedChanges = computed(() => visibleChanges.value.slice(0, renderLimit.value))
@@ -50,7 +54,13 @@ function loadMore() {
50
54
  <col>
51
55
  </colgroup>
52
56
  <tbody>
53
- <SplitLine v-for="(item, index) in renderedChanges" :key="index" :split-line="item" @expand="expandHandler" />
57
+ <SplitLine
58
+ v-for="(item, index) in renderedChanges"
59
+ :key="index"
60
+ :split-line="item"
61
+ @expand="expandHandler"
62
+ @change-click="emit('change-click', $event)"
63
+ />
54
64
  <tr v-if="remainingLines">
55
65
  <td class="blob-code blob-code-hunk load-more" colspan="4">
56
66
  <button class="load-more-button" type="button" @click="loadMore">
package/src/types.ts CHANGED
@@ -21,6 +21,13 @@ export interface DiffLine {
21
21
  num?: number
22
22
  }
23
23
 
24
+ export interface DiffChangeClickEvent {
25
+ side: 'old' | 'new'
26
+ type: DiffType.ADD | DiffType.DELETE
27
+ lineNumber: number
28
+ event: MouseEvent
29
+ }
30
+
24
31
  export interface SplitLineChange {
25
32
  fold?: boolean
26
33
  highlighted?: boolean
@@ -1,11 +1,27 @@
1
1
  <script lang="ts" setup>
2
- import type { UnifiedLineChange } from '../types'
2
+ import type { DiffChangeClickEvent, UnifiedLineChange } from '../types'
3
3
  import { DiffType } from '../types'
4
4
 
5
- defineProps<{
5
+ const props = defineProps<{
6
6
  line: UnifiedLineChange
7
7
  }>()
8
- const emit = defineEmits(['expand'])
8
+ const emit = defineEmits<{
9
+ (e: 'expand', line: UnifiedLineChange): void
10
+ (e: 'change-click', payload: DiffChangeClickEvent): void
11
+ }>()
12
+
13
+ function onChangeClick(event: MouseEvent) {
14
+ if (props.line.type !== DiffType.ADD && props.line.type !== DiffType.DELETE)
15
+ return
16
+
17
+ // Vue 2 DOM templates require kebab-case event names.
18
+ emit('change-click', {
19
+ side: props.line.type === DiffType.DELETE ? 'old' : 'new',
20
+ type: props.line.type,
21
+ lineNumber: props.line.type === DiffType.DELETE ? props.line.delNum! : props.line.addNum!,
22
+ event,
23
+ })
24
+ }
9
25
 
10
26
  function getCodeMarker(type: DiffType) {
11
27
  if (type === DiffType.DELETE)
@@ -25,7 +41,7 @@ function getCodeMarker(type: DiffType) {
25
41
 
26
42
  </td>
27
43
  </tr>
28
- <tr v-else-if="!line.hide" :data-diff-change="line.type !== DiffType.EQUAL ? '' : undefined">
44
+ <tr v-else-if="!line.hide" :data-diff-change="line.type !== DiffType.EQUAL ? '' : undefined" @click="onChangeClick">
29
45
  <td
30
46
  class="blob-num"
31
47
  :class="{
@@ -1,6 +1,6 @@
1
1
  <script setup lang="ts">
2
2
  import { computed, ref, shallowRef, watch } from 'vue-demi'
3
- import type { UnifiedLineChange, UnifiedViewerChange } from '../types'
3
+ import type { DiffChangeClickEvent, UnifiedLineChange, UnifiedViewerChange } from '../types'
4
4
  import { RENDER_BATCH_SIZE, highlightUnifiedLine } from '../utils'
5
5
  import UnifiedLine from './UnifiedLine.vue'
6
6
 
@@ -9,6 +9,10 @@ const props = defineProps<{
9
9
  language: string
10
10
  }>()
11
11
 
12
+ const emit = defineEmits<{
13
+ (e: 'change-click', payload: DiffChangeClickEvent): void
14
+ }>()
15
+
12
16
  const renderLimit = ref(RENDER_BATCH_SIZE)
13
17
  const visibleChanges = shallowRef(props.diffChange.changes.filter(line => !line.hide || line.hideIndex !== undefined))
14
18
  const renderedChanges = computed(() => visibleChanges.value.slice(0, renderLimit.value))
@@ -44,7 +48,13 @@ function loadMore() {
44
48
  <template>
45
49
  <table class="diff-table">
46
50
  <tbody>
47
- <UnifiedLine v-for="(item, index) in renderedChanges" :key="index" :line="item" @expand="expandHandler" />
51
+ <UnifiedLine
52
+ v-for="(item, index) in renderedChanges"
53
+ :key="index"
54
+ :line="item"
55
+ @expand="expandHandler"
56
+ @change-click="emit('change-click', $event)"
57
+ />
48
58
  <tr v-if="remainingLines">
49
59
  <td class="blob-code blob-code-hunk load-more" colspan="3">
50
60
  <button class="load-more-button" type="button" @click="loadMore">
package/types/index.d.ts CHANGED
@@ -16,6 +16,7 @@ export interface CodeDiffProps {
16
16
  newFilename?: string
17
17
  hideHeader?: boolean
18
18
  hideStat?: boolean
19
+ hideNavigation?: boolean
19
20
  theme?: 'light' | 'dark'
20
21
  ignoreMatchingLines?: string
21
22
  }
@@ -40,16 +41,25 @@ export interface DiffResult {
40
41
  }
41
42
  }
42
43
 
44
+ export interface DiffChangeClickEvent {
45
+ side: 'old' | 'new'
46
+ type: 'added' | 'removed'
47
+ lineNumber: number
48
+ event: MouseEvent
49
+ }
50
+
43
51
  export interface CodeDiffSlots {
44
- stat: (props: { stat: DiffStat }) => VNode[]
52
+ 'header-actions': () => VNode[]
53
+ 'stat': (props: { stat: DiffStat }) => VNode[]
45
54
  }
46
55
 
47
56
  interface CodeDiffEmits {
48
- diff: (diffResult: DiffResult) => void
57
+ 'diff': (diffResult: DiffResult) => void
58
+ 'change-click': (payload: DiffChangeClickEvent) => void
49
59
  }
50
60
 
51
61
  type Empty = Record<string, never>
52
- type CodeDiffComponent = DefineComponent<CodeDiffProps, Empty, Empty, Empty, Empty, Empty, Empty, CodeDiffEmits, 'diff'>
62
+ type CodeDiffComponent = DefineComponent<CodeDiffProps, Empty, Empty, Empty, Empty, Empty, Empty, CodeDiffEmits, 'diff' | 'change-click'>
53
63
 
54
64
  export declare const CodeDiff: CodeDiffComponent & {
55
65
  new (): InstanceType<CodeDiffComponent> & { $slots: CodeDiffSlots }