forked from npmx-dev/npmx.dev
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeViewer.vue
More file actions
148 lines (130 loc) · 3.71 KB
/
CodeViewer.vue
File metadata and controls
148 lines (130 loc) · 3.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<script setup lang="ts">
const props = defineProps<{
html: string
lines: number
selectedLines: { start: number; end: number } | null
}>()
const emit = defineEmits<{
lineClick: [lineNum: number, event: MouseEvent]
}>()
const codeRef = useTemplateRef('codeRef')
// Generate line numbers array
const lineNumbers = computed(() => {
return Array.from({ length: props.lines }, (_, i) => i + 1)
})
// Check if a line is selected
function isLineSelected(lineNum: number): boolean {
if (!props.selectedLines) return false
return lineNum >= props.selectedLines.start && lineNum <= props.selectedLines.end
}
// Handle line number click
function onLineClick(lineNum: number, event: MouseEvent) {
emit('lineClick', lineNum, event)
}
// Apply highlighting to code lines when selection changes
function updateLineHighlighting() {
if (!codeRef.value) return
// Lines are inside pre > code > .line
const lines = codeRef.value.querySelectorAll('code > .line')
lines.forEach((line, index) => {
const lineNum = index + 1
if (isLineSelected(lineNum)) {
line.classList.add('highlighted')
} else {
line.classList.remove('highlighted')
}
})
}
// Watch for changes to selection and HTML content
// Use deep watch and nextTick to ensure DOM is updated
watch(
() => [props.selectedLines, props.html] as const,
() => {
nextTick(updateLineHighlighting)
},
{ immediate: true },
)
</script>
<template>
<div class="code-viewer flex min-h-full">
<!-- Line numbers column -->
<div
class="line-numbers shrink-0 bg-bg-subtle border-ie border-border text-end select-none"
aria-hidden="true"
>
<a
v-for="lineNum in lineNumbers"
:id="`L${lineNum}`"
:key="lineNum"
:href="`#L${lineNum}`"
tabindex="-1"
class="line-number block px-3 py-0 font-mono text-sm leading-6 cursor-pointer transition-colors no-underline"
:class="[
isLineSelected(lineNum)
? 'bg-yellow-500/20 text-fg'
: 'text-fg-subtle hover:text-fg-muted',
]"
@click.prevent="onLineClick(lineNum, $event)"
>
{{ lineNum }}
</a>
</div>
<!-- Code content -->
<div class="code-content flex-1 overflow-x-auto min-w-0">
<!-- eslint-disable vue/no-v-html -- HTML is generated server-side by Shiki -->
<div ref="codeRef" class="code-lines w-fit" v-html="html" />
<!-- eslint-enable vue/no-v-html -->
</div>
</div>
</template>
<style scoped>
.code-viewer {
font-size: 14px;
}
.line-numbers {
min-width: 3.5rem;
}
.code-content :deep(pre) {
margin: 0;
padding: 0;
background: transparent !important;
overflow: visible;
}
.code-content :deep(code) {
display: block;
padding: 0 1rem;
background: transparent !important;
}
.code-content :deep(.line) {
display: block;
/* Ensure consistent height matching line numbers */
line-height: 24px;
min-height: 24px;
max-height: 24px;
white-space: pre;
overflow: hidden;
transition: background-color 0.1s;
}
/* Highlighted lines in code content - extend full width with negative margin */
.code-content :deep(.line.highlighted) {
background: rgb(234 179 8 / 0.2); /* yellow-500/20 */
margin: 0 -1rem;
padding: 0 1rem;
}
/* Clickable import links */
.code-content :deep(.import-link) {
color: inherit;
text-decoration: underline;
text-decoration-style: dotted;
text-decoration-color: rgba(158, 203, 255, 0.5); /* syntax.str with transparency */
text-underline-offset: 2px;
transition:
text-decoration-color 0.15s,
text-decoration-style 0.15s;
cursor: pointer;
}
.code-content :deep(.import-link:hover) {
text-decoration-style: solid;
text-decoration-color: #9ecbff; /* syntax.str - light blue */
}
</style>