Skip to content

Commit

Permalink
build: v0.3.5, support sync scroll when outputFormat is side-by-side
Browse files Browse the repository at this point in the history
  • Loading branch information
Shimada666 committed Nov 13, 2021
1 parent b2f9091 commit e8e19f4
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .npmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
registry=http://registry.npmjs.org
registry=https://registry.npmjs.org
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "v-code-diff",
"description": "A diff plugin of vue, support vue2 and vue3",
"version": "0.3.4",
"version": "0.3.5",
"scripts": {
"dev": "vite",
"build": "npx rimraf dist && rollup --config rollup.config.js",
Expand Down
1 change: 1 addition & 0 deletions src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
</a-form-item>
</a-form>
<code-diff
:highlight="true"
:old-string="oldString"
:new-string="newString"
:context="formState.context"
Expand Down
20 changes: 20 additions & 0 deletions src/lib/v-code-diff/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,23 @@ export async function highlightElements (element: Element, props, ctx: SetupCont
await Promise.all(promises)
ctx.emit('after-render')
}

export function syncScroll (selector) {
let active: HTMLElement = document.createElement('div')
document.querySelectorAll(selector).forEach(function (element) {
element.addEventListener('mouseenter', function (e) {
active = e.target
})

element.addEventListener('scroll', function (e) {
if (e.target !== active) return

document.querySelectorAll(selector).forEach(function (target) {
if (active === target) return

target.scrollTop = active.scrollTop
target.scrollLeft = active.scrollLeft
})
})
})
}
14 changes: 12 additions & 2 deletions src/lib/v-code-diff/v-code-diff.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { defineComponent, isVue3, PropType, h, ref, watch } from 'vue-demi'
import { createHtml, highlightElements, useDebounceFn } from '@/lib/v-code-diff/util'
import { defineComponent, isVue3, PropType, h, ref, watch, onUpdated, onMounted } from 'vue-demi'
import { createHtml, highlightElements, syncScroll, useDebounceFn } from '@/lib/v-code-diff/util'
import './styles'

export default defineComponent({
Expand Down Expand Up @@ -63,6 +63,16 @@ export default defineComponent({
}
}, 200)
watch(props, cb, { deep: true, immediate: true })
onMounted(() => {
if (props.outputFormat === 'side-by-side') {
syncScroll('.d2h-file-side-diff')
}
})
onUpdated(() => {
if (props.outputFormat === 'side-by-side') {
syncScroll('.d2h-file-side-diff')
}
})
return {
html
}
Expand Down

0 comments on commit e8e19f4

Please sign in to comment.