-
-
Notifications
You must be signed in to change notification settings - Fork 360
Expand file tree
/
Copy pathViewer.vue
More file actions
231 lines (203 loc) · 6.22 KB
/
Viewer.vue
File metadata and controls
231 lines (203 loc) · 6.22 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
<script setup lang="ts">
const props = defineProps<{
html: string
lines: number
selectedLines: { start: number; end: number } | null
wordWrap?: boolean
}>()
const emit = defineEmits<{
lineClick: [lineNum: number, event: MouseEvent]
}>()
const codeRef = useTemplateRef('codeRef')
const lineNumbersRef = useTemplateRef('lineNumbersRef')
// Generate line numbers array
const lineNumbers = computed(() => {
return Array.from({ length: props.lines }, (_, i) => i + 1)
})
// Used for CSS calculation of line number column width
const lineDigits = computed(() => {
return String(props.lines).length
})
// 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)
}
// Synchronize line number heights with code line heights (needed for word wrap)
function syncLineHeights() {
if (!props.wordWrap || !codeRef.value || !lineNumbersRef.value) {
// Reset heights if word wrap is disabled
if (lineNumbersRef.value) {
const nums = lineNumbersRef.value.querySelectorAll<HTMLElement>('.line-number')
nums.forEach(num => (num.style.height = ''))
}
return
}
const lines = codeRef.value.querySelectorAll<HTMLElement>('code > .line')
const nums = lineNumbersRef.value.querySelectorAll<HTMLElement>('.line-number')
lines.forEach((line, index) => {
const num = nums[index]
if (num) {
// Use getBoundingClientRect for more precision if needed, but offsetHeight is usually enough
const height = line.offsetHeight
num.style.height = `${height}px`
}
})
}
// 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()
syncLineHeights()
})
},
{ immediate: true },
)
// Also watch wordWrap specifically
watch(
() => props.wordWrap,
() => {
nextTick(syncLineHeights)
},
)
// Sync on resize
if (import.meta.client) {
useEventListener(window, 'resize', syncLineHeights)
}
// Use Nuxt's `navigateTo` for the rendered import links
function handleImportLinkNavigate() {
if (!codeRef.value) return
const anchors = codeRef.value.querySelectorAll<HTMLAnchorElement>('a.import-link')
anchors.forEach(anchor => {
// NOTE: We do not need to remove previous listeners because we re-create the entire HTML content on each html update
anchor.addEventListener('click', event => {
if (event.metaKey || event.altKey || event.ctrlKey || event.shiftKey) return
const href = anchor.getAttribute('href')
if (href) {
event.preventDefault()
navigateTo(href)
}
})
})
}
watch(
() => props.html,
() => {
nextTick(handleImportLinkNavigate)
},
{ immediate: true },
)
</script>
<template>
<div class="code-viewer flex min-h-full max-w-full" :class="{ 'is-wrapped': wordWrap }">
<!-- Line numbers column -->
<div
ref="lineNumbersRef"
class="line-numbers shrink-0 bg-bg-subtle border-ie border-solid border-border text-end select-none relative"
:style="{ '--line-digits': lineDigits }"
aria-hidden="true"
>
<!-- This needs to be a native <a> element, because `LinkBase` (or specifically `NuxtLink`) does not seem to work when trying to prevent default behavior (jumping to the anchor) -->
<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 min-w-full w-fit" v-html="html" />
<!-- eslint-enable vue/no-v-html -->
</div>
</div>
</template>
<style scoped>
.code-viewer {
font-size: 14px;
}
.line-numbers {
/* 1ch per digit + 1.5rem (px-3 * 2) padding */
min-width: calc(var(--line-digits) * 1ch + 1.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;
}
.is-wrapped .code-content :deep(.line) {
white-space: pre-wrap;
word-break: break-all;
max-height: none;
}
/* Highlighted lines in code content - extend full width with negative margin */
.code-content :deep(.line.highlighted) {
@apply bg-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>