|
| 1 | +<template> |
| 2 | + <section |
| 3 | + class="flex items-center justify-between px-4 py-2 mb-4 sticky top-[70px] dark:bg-gray-700 bg-gray-300 dark:bg-opacity-50 bg-opacity-50 backdrop-blur-sm rounded-md shadow-lg border border-gray-500 w-full z-10" |
| 4 | + > |
| 5 | + <div class="flex gap-4"> |
| 6 | + <ToggleInSync /> |
| 7 | + <NextDiff :click-handler="goToNextDiff" /> |
| 8 | + <PrevDiff :click-handler="goToPreviousDiff" /> |
| 9 | + </div> |
| 10 | + <CopyLink :click-handler="copyUrlToClipboard" :copied="copied"></CopyLink> |
| 11 | + </section> |
| 12 | +</template> |
| 13 | + |
| 14 | +<script lang="ts"> |
| 15 | +import Vue from 'vue' |
| 16 | +import PrevDiff from '../buttons/prevDiff.vue' |
| 17 | +import ToggleInSync from '../buttons/toggleInSync.vue' |
| 18 | +import NextDiff from '../buttons/nextDiff.vue' |
| 19 | +import CopyLink from '../buttons/copyLink.vue' |
| 20 | +import { putToClipboard } from '~/helpers/utils' |
| 21 | +import { DiffActionBarData } from '~/helpers/types' |
| 22 | +export default Vue.extend({ |
| 23 | + components: { |
| 24 | + PrevDiff, |
| 25 | + NextDiff, |
| 26 | + ToggleInSync, |
| 27 | + CopyLink, |
| 28 | + }, |
| 29 | + data(): DiffActionBarData { |
| 30 | + return { |
| 31 | + copied: false, |
| 32 | + comparator: null, |
| 33 | + comparer: null, |
| 34 | + treeWalker: null, |
| 35 | + } |
| 36 | + }, |
| 37 | + mounted() { |
| 38 | + const lhsDiffNode = document.getElementById('lhsDiff') |
| 39 | + const rhsDiffNode = document.getElementById('rhsDiff') |
| 40 | + if (lhsDiffNode && rhsDiffNode) { |
| 41 | + const isLHSBigger = |
| 42 | + lhsDiffNode.children.length > rhsDiffNode.children.length |
| 43 | + let comparator, comparer |
| 44 | + if (isLHSBigger) { |
| 45 | + comparer = lhsDiffNode |
| 46 | + comparator = rhsDiffNode |
| 47 | + } else { |
| 48 | + comparer = rhsDiffNode |
| 49 | + comparator = lhsDiffNode |
| 50 | + } |
| 51 | + this.comparator = comparator |
| 52 | + this.treeWalker = document.createTreeWalker( |
| 53 | + comparer, |
| 54 | + NodeFilter.SHOW_ELEMENT, |
| 55 | + { |
| 56 | + acceptNode: (node: Node) => { |
| 57 | + if ( |
| 58 | + (node as HTMLDivElement).classList.contains('bg-red-200') || |
| 59 | + (node as HTMLDivElement).classList.contains('bg-green-200') |
| 60 | + ) { |
| 61 | + return NodeFilter.FILTER_ACCEPT |
| 62 | + } |
| 63 | + return NodeFilter.FILTER_REJECT |
| 64 | + }, |
| 65 | + } |
| 66 | + ) |
| 67 | + document.addEventListener('keydown', this.handleCtrlC) |
| 68 | + } |
| 69 | + }, |
| 70 | + beforeDestroy() { |
| 71 | + document.removeEventListener('keydown', this.handleCtrlC) |
| 72 | + }, |
| 73 | + methods: { |
| 74 | + handleCtrlC(event: KeyboardEvent) { |
| 75 | + const { metaKey, ctrlKey, key } = event |
| 76 | + if ( |
| 77 | + (metaKey || ctrlKey) && |
| 78 | + key === 'c' && |
| 79 | + !window?.getSelection()?.toString() |
| 80 | + ) { |
| 81 | + const button: HTMLButtonElement = document.getElementById( |
| 82 | + 'copyLinkButton' |
| 83 | + ) as HTMLButtonElement |
| 84 | + button.click() |
| 85 | + } |
| 86 | + }, |
| 87 | + copyUrlToClipboard() { |
| 88 | + putToClipboard( |
| 89 | + window.location.href, |
| 90 | + 'Link copied to your clipboard', |
| 91 | + this.$store |
| 92 | + ) |
| 93 | + this.copied = true |
| 94 | + setTimeout(() => { |
| 95 | + this.copied = false |
| 96 | + }, 5000) |
| 97 | + }, |
| 98 | + goToNextDiff() { |
| 99 | + const currentNode = this.treeWalker?.currentNode |
| 100 | + const nextNode = this.treeWalker?.nextNode() |
| 101 | + if (nextNode) { |
| 102 | + const currentNodeIndex = Array.prototype.indexOf.call( |
| 103 | + currentNode?.parentElement?.children, |
| 104 | + currentNode |
| 105 | + ) |
| 106 | + const nextNodeIndex = Array.prototype.indexOf.call( |
| 107 | + nextNode.parentElement?.children, |
| 108 | + nextNode |
| 109 | + ) |
| 110 | + const comparatorCurrentNode = |
| 111 | + this.comparator?.children[currentNodeIndex] |
| 112 | + const comparatorNextNode = this.comparator?.children[nextNodeIndex] |
| 113 | + this.toggleDiffHunkAndScrollIntoView( |
| 114 | + [ |
| 115 | + currentNode as HTMLDivElement, |
| 116 | + comparatorCurrentNode as HTMLDivElement, |
| 117 | + ], |
| 118 | + [nextNode as HTMLDivElement, comparatorNextNode as HTMLDivElement] |
| 119 | + ) |
| 120 | + } |
| 121 | + }, |
| 122 | + goToPreviousDiff() { |
| 123 | + const currentNode = this.treeWalker?.currentNode |
| 124 | + const prevNode = this.treeWalker?.previousNode() |
| 125 | + if (prevNode) { |
| 126 | + const currentNodeIndex = Array.prototype.indexOf.call( |
| 127 | + currentNode?.parentElement?.children, |
| 128 | + currentNode |
| 129 | + ) |
| 130 | + const prevNodeIndex = Array.prototype.indexOf.call( |
| 131 | + prevNode.parentElement?.children, |
| 132 | + prevNode |
| 133 | + ) |
| 134 | + const comparatorCurrentNode = |
| 135 | + this.comparator?.children[currentNodeIndex] |
| 136 | + const comparatorPrevNode = this.comparator?.children[prevNodeIndex] |
| 137 | + this.toggleDiffHunkAndScrollIntoView( |
| 138 | + [ |
| 139 | + currentNode as HTMLDivElement, |
| 140 | + comparatorCurrentNode as HTMLDivElement, |
| 141 | + ], |
| 142 | + [prevNode as HTMLDivElement, comparatorPrevNode as HTMLDivElement] |
| 143 | + ) |
| 144 | + } |
| 145 | + }, |
| 146 | + toggleDiffHunkAndScrollIntoView( |
| 147 | + unselectedNodes: Array<HTMLDivElement | undefined> = [], |
| 148 | + selectedNodes: Array<HTMLDivElement | undefined> = [] |
| 149 | + ) { |
| 150 | + unselectedNodes.forEach((element) => { |
| 151 | + element?.querySelector('p')?.classList.remove('selected') |
| 152 | + }) |
| 153 | + selectedNodes.forEach((element) => { |
| 154 | + element?.querySelector('p')?.classList.add('selected') |
| 155 | + element?.scrollIntoView() |
| 156 | + }) |
| 157 | + }, |
| 158 | + }, |
| 159 | +}) |
| 160 | +</script> |
| 161 | +<style lang="scss"> |
| 162 | +.copy-uri-button:hover svg { |
| 163 | + @apply rotate-12; |
| 164 | +} |
| 165 | +</style> |
0 commit comments