Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions src/components/NcFormBoxCopyButton/NcFormBoxCopyButton.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,10 @@
<script setup lang="ts">
import type { Slot } from 'vue'

import { mdiCheck, mdiContentCopy } from '@mdi/js'
import { whenever } from '@vueuse/core'
import { computed } from 'vue'
import NcFormBoxButton from '../NcFormBoxButton/NcFormBoxButton.vue'
import NcIconSvgWrapper from '../NcIconSvgWrapper/NcIconSvgWrapper.vue'
import { t } from '../../l10n.ts'
import { useCopy } from './useCopy.ts'
import { useCopy } from '../../composables/useCopy.ts'

const {
label = undefined,
Expand All @@ -37,9 +34,7 @@ defineSlots<{
default?: Slot
}>()

const { isCopied, copy } = useCopy(() => value)

const icon = computed(() => isCopied.value ? mdiCheck : mdiContentCopy)
const { isCopied, copy, icon, altText } = useCopy(() => value)

whenever(isCopied, () => emit('copy'))
</script>
Expand All @@ -51,7 +46,7 @@ whenever(isCopied, () => emit('copy'))
@click="copy">
<template v-if="$slots.default || label" #default>
<span class="hidden-visually">
{{ isCopied ? t('Copied') : t('Copy to clipboard') }}
{{ altText }}
</span>
<slot>
{{ label }}
Expand Down
33 changes: 33 additions & 0 deletions src/components/NcRichText/NcRichText.vue
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ import { Fragment, h, ref, resolveComponent } from 'vue'
import { RouterLink } from 'vue-router'
import NcCheckboxRadioSwitch from '../NcCheckboxRadioSwitch/NcCheckboxRadioSwitch.vue'
import NcReferenceList from './NcReferenceList.vue'
import NcRichTextCopyButton from './NcRichTextCopyButton.vue'
import { createElementId } from '../../utils/createElementId.ts'
import { getRoute, parseUrl, remarkAutolink } from './autolink.ts'
import { remarkPlaceholder } from './remarkPlaceholder.ts'
Expand Down Expand Up @@ -559,6 +560,15 @@ export default {
&& props?.class?.includes('language')) {
importRehypeHighlightLibrary()
}

if (String(type) === 'pre' && children && String(children.type) === 'code') {
const id = this.parentId + '-code-block-' + createElementId()
return h('p', { class: 'rich-text__code-block' }, [
h(type, { ...props, id }, children),
h(NcRichTextCopyButton, { class: 'rich-text__code-block-button', contentId: id }),
])
}

if (String(type) === 'li' && Array.isArray(children)
&& children.length !== 0
&& children[0].type === 'input'
Expand Down Expand Up @@ -784,4 +794,27 @@ a:not(.rich-text--component) {
@include highlight.highlight-light-colors;
}
}

.rich-text__code-block {
position: relative;
padding-inline-end: calc(var(--clickable-area-small) + var(--default-grid-baseline));

& pre {
width: 100%;
overflow-x: auto;
}

.rich-text__code-block-button {
position: absolute;
top: var(--default-grid-baseline);
inset-inline-end: var(--default-grid-baseline);
opacity: 0;
}

&:hover .rich-text__code-block-button,
&:focus-within .rich-text__code-block-button,
& .rich-text__code-block-button:focus {
opacity: 1;
}
}
</style>
34 changes: 34 additions & 0 deletions src/components/NcRichText/NcRichTextCopyButton.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<!--
- SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
- SPDX-License-Identifier: AGPL-3.0-or-later
-->

<script setup lang="ts">
import NcButton from '../NcButton/NcButton.vue'
import NcIconSvgWrapper from '../NcIconSvgWrapper/NcIconSvgWrapper.vue'
import { useCopy } from '../../composables/useCopy.ts'

const { contentId } = defineProps<{
/** Node representing code block content */
contentId: string
}>()

const { copy, icon, altText } = useCopy(() => document.getElementById(contentId)!.textContent)
</script>

<template>
<NcButton
variant="tertiary"
size="small"
:aria-label="altText"
:title="altText"
@click="copy">
<template #icon>
<NcIconSvgWrapper :path="icon" inline />
</template>
</NcButton>
</template>

<docs>
An internal component
</docs>
2 changes: 1 addition & 1 deletion src/components/NcRichText/highlight.scss
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
}

@mixin highlight-rules {
pre:has(.hljs) {
.rich-text__code-block:has(.hljs) {
color: var(--hljs-color);
background: var(--hljs-background-color);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@

import type { MaybeRefOrGetter } from 'vue'

import { ref, toValue } from 'vue'
import { mdiCheck, mdiContentCopy } from '@mdi/js'
import { computed, ref, toValue } from 'vue'
import { t } from '../l10n.ts'

const DELAY = 2000

Expand All @@ -16,6 +18,8 @@ const DELAY = 2000
*/
export function useCopy(content: MaybeRefOrGetter<string>) {
const isCopied = ref(false)
const icon = computed(() => isCopied.value ? mdiCheck : mdiContentCopy)
const altText = computed(() => isCopied.value ? t('Copied') : t('Copy to clipboard'))

/**
* Copy the content to clipboard
Expand All @@ -42,5 +46,7 @@ export function useCopy(content: MaybeRefOrGetter<string>) {
return {
isCopied,
copy,
icon,
altText,
}
}
Loading