Skip to content

feat(plugin-copy-code): add inlineSelector option #416

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 22 commits into from
Mar 28, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 11 additions & 1 deletion docs/plugins/features/copy-code.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,23 @@ export default {

### ignoreSelector

- Type: `string[]`
- Type: `string[] | string`
- Details:

Elements selector in code blocks, used to ignore related elements when copying.

For example, `['.token.comment']` will ignore nodes with the class name `.token.comment` in code blocks (which in `prismjs` refers to ignoring comments).

### inlineSelector

- Type: `string[] | string | boolean`
- Default: `false`

Whether to copy inline code content when double click.

- `boolean`: Whether to copy inline code content when double click.
- `string | string[]`: The selector of inline code.

### transform <Badge type="tip" text="Composables API Only" />

- Type: `(preElement: HTMLPreElement) => void`
Expand Down
11 changes: 11 additions & 0 deletions docs/zh/plugins/features/copy-code.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,17 @@ export default {

例如: `['.token.comment']` 将忽略代码块中类名为 `.token.comment` 的节点 (这会在 `prismjs` 中忽略注释)。

### inlineSelector

- 类型:`string[] | string | boolean`
- 默认值:`false`
- 详情:

是否在双击时复制行内代码内容。

- `boolean`: 是否在双击时复制行内代码内容。
- `string[] | string`: 选择器,表示需要复制的行内代码内容。

### transform <Badge type="tip" text="仅限组合式 API" />

- 类型:`(preElement: HTMLPreElement) => void`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,13 @@ import '../styles/vars.css'

export interface UseCopyCodeOptions {
locales: CopyCodePluginLocaleConfig
selector: string[]
selector: string
ignoreSelector?: string
inlineSelector?: string
/** @default 2000 */
duration: number
/** @default false */
showInMobile?: boolean
/** @default [] */
ignoreSelector?: string[]

/**
* Transform pre element before copy
*
Expand All @@ -47,11 +46,12 @@ export interface UseCopyCodeOptions {
const SHELL_RE = /language-(shellscript|shell|bash|sh|zsh)/

export const useCopyCode = ({
selector,
ignoreSelector,
inlineSelector,
duration = 2000,
locales,
selector,
showInMobile,
ignoreSelector = [],
transform,
}: UseCopyCodeOptions): void => {
if (__VUEPRESS_SSR__) return
Expand Down Expand Up @@ -83,9 +83,7 @@ export const useCopyCode = ({
document.body.classList.toggle('no-copy-code', !enabled.value)
if (!enabled.value) return

document
.querySelectorAll<HTMLElement>(selector.join(','))
.forEach(insertCopyButton)
document.querySelectorAll<HTMLElement>(selector).forEach(insertCopyButton)
}

watchImmediate(enabled, appendCopyButton, {
Expand All @@ -106,8 +104,8 @@ export const useCopyCode = ({
): Promise<void> => {
const clone = codeContent.cloneNode(true) as HTMLPreElement

if (ignoreSelector.length) {
clone.querySelectorAll(ignoreSelector.join(',')).forEach((node) => {
if (ignoreSelector) {
clone.querySelectorAll(ignoreSelector).forEach((node) => {
node.remove()
})
}
Expand Down Expand Up @@ -148,4 +146,12 @@ export const useCopyCode = ({
void copyContent(codeContainer, preBlock, el as HTMLButtonElement)
}
})

if (inlineSelector)
useEventListener('dblclick', (event) => {
const el = event.target as HTMLElement

if (enabled.value && el.matches(inlineSelector))
void copy(el.textContent || '')
})
}
21 changes: 14 additions & 7 deletions plugins/features/plugin-copy-code/src/node/copyCodePlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,26 @@ export const copyCodePlugin =
name: PLUGIN_NAME,

define: () => ({
__CC_DURATION__: options.duration ?? 2000,
__CC_IGNORE_SELECTOR__: options.ignoreSelector ?? [],
__CC_SELECTOR__: isArray(options.selector)
? options.selector.join(',')
: (options.selector ?? '[vp-content] div[class*="language-"] pre'),
__CC_IGNORE_SELECTOR__: Array.isArray(options.ignoreSelector)
? options.ignoreSelector.join(',')
: (options.ignoreSelector ?? ''),
__CC_INLINE_SELECTOR__: Array.isArray(options.inline)
? options.inline.join(',')
: isString(options.inline)
? options.inline
: options.inline
? '[vp-content] :not(pre) > code'
: '',
__CC_LOCALES__: getFullLocaleConfig({
app,
name: PLUGIN_NAME,
default: copyCodeLocaleInfo,
config: options.locales,
}),
__CC_SELECTOR__: isArray(options.selector)
? options.selector
: isString(options.selector)
? [options.selector]
: ['[vp-content] div[class*="language-"] pre'],
__CC_DURATION__: options.duration ?? 2000,
__CC_SHOW_IN_MOBILE__: options.showInMobile ?? false,
}),

Expand Down
17 changes: 16 additions & 1 deletion plugins/features/plugin-copy-code/src/node/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,27 @@ export interface CopyCodePluginOptions {
*
* @default []
*/
ignoreSelector?: string[]
ignoreSelector?: string[] | string

/**
* Locale config
*
* 国际化配置
*/
locales?: LocaleConfig<CopyCodePluginLocaleData>

/**
* Whether to copy inline code content when double click.
*
* - boolean: Whether to copy inline code content when double click.
* - string | string[]: The selector of inline code.
*
* 是否在双击时复制内联代码内容
*
* - boolean: 是否在双击时复制内联代码内容
* - string | string[]: 内联代码的选择器
*
* @default '[vp-content] :not(pre) > code'
*/
inline?: string[] | boolean | string
}
Loading