Skip to content

Commit 933797a

Browse files
committed
feat(monaco): added first draft for monaco editor integration
1 parent aace8de commit 933797a

File tree

11 files changed

+757
-30
lines changed

11 files changed

+757
-30
lines changed

.vscode/launch.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": "Launch via NPM",
9+
"request": "launch",
10+
"runtimeArgs": [
11+
"run",
12+
"dev"
13+
],
14+
"runtimeExecutable": "npm",
15+
"skipFiles": [
16+
"<node_internals>/**"
17+
],
18+
"type": "node"
19+
}
20+
]
21+
}

app.html

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,26 @@
11
<!DOCTYPE html>
22
<html lang="en" {{ HTML_ATTRS }} class="hidden">
3+
<head {{ HEAD_ATTRS }}>
4+
<script>
5+
window.dataLayer = window.dataLayer || []
6+
function gtag() {
7+
dataLayer.push(arguments)
8+
}
9+
gtag('js', new Date())
10+
gtag('config', 'G-HBV7K2PRX6')
11+
</script>
12+
<link
13+
rel="stylesheet"
14+
data-name="vs/editor/editor.main"
15+
href="https://unpkg.com/monaco-editor/min/vs/editor/editor.main.css"
16+
/>
17+
<script>
18+
var require = { paths: { vs: 'https://unpkg.com/monaco-editor/min/vs' } }
19+
</script>
20+
{{ HEAD }}
21+
</head>
322

4-
<head {{ HEAD_ATTRS }}>
5-
<script>
6-
window.dataLayer = window.dataLayer || [];
7-
function gtag() { dataLayer.push(arguments); }
8-
gtag('js', new Date());
9-
gtag('config', 'G-HBV7K2PRX6');
10-
</script>
11-
{{ HEAD }}
12-
</head>
13-
14-
<body {{ BODY_ATTRS }}>
15-
{{ APP }}
16-
</body>
17-
23+
<body {{ BODY_ATTRS }}>
24+
{{ APP }}
25+
</body>
1826
</html>

components/v2/diffActionBar.vue

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
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

Comments
 (0)