|
| 1 | +// SPDX-FileCopyrightText: 2026 caixw |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | +import { getOwner, type JSX, runWithOwner } from 'solid-js'; |
| 6 | +import { render } from 'solid-js/web'; |
| 7 | + |
| 8 | +import { Button } from '@components/button'; |
| 9 | +import { ClipboardAPI } from '@components/clipboard'; |
| 10 | +import styles from './style.module.css'; |
| 11 | + |
| 12 | +/** |
| 13 | + * 装饰器的类型 |
| 14 | + * |
| 15 | + * @param pre - 代码高亮的对象,装饰器最终会被放在此元素上; |
| 16 | + * @remarks |
| 17 | + * 这是对代码高亮对象 pre 的各种修改,比如添加按钮,或是改变颜色等。 |
| 18 | + */ |
| 19 | +export type Decorate = (pre: HTMLPreElement) => JSX.Element; |
| 20 | + |
| 21 | +/** |
| 22 | + * 获取的有已经注册的装饰器 |
| 23 | + * |
| 24 | + * @remarks |
| 25 | + * 默认情况下,支持以下装饰器: |
| 26 | + * - toolbar 在顶部显示一个工具栏; |
| 27 | + * - copyButton 在右上角显示一个复制按钮; |
| 28 | + * - border 为代码组件显示边框; |
| 29 | + */ |
| 30 | +const decorates = new Map<string, Decorate>(); |
| 31 | + |
| 32 | +/** |
| 33 | + * 注册装饰器 |
| 34 | + * @param name - 装饰器的名称; |
| 35 | + * @param decorate - 装饰器的实例; |
| 36 | + */ |
| 37 | +export function registerDecorate(name: string, decorate: Decorate): void { |
| 38 | + if (decorates.has(name)) { |
| 39 | + throw new Error(`${name} 已经存在`); |
| 40 | + } |
| 41 | + |
| 42 | + decorates.set(name, decorate); |
| 43 | +} |
| 44 | + |
| 45 | +/** |
| 46 | + * 获取装饰器的名称列表 |
| 47 | + */ |
| 48 | +export function getDecorates(): Array<string> { |
| 49 | + return Array.from(decorates.keys()); |
| 50 | +} |
| 51 | + |
| 52 | +/** |
| 53 | + * 为 elem 及其子元素中的所有 shiki 代码块添加指定的组件 |
| 54 | + * |
| 55 | + * @remarks |
| 56 | + * 装饰器根据 {@link highlight} 的 decorate 参数而定。 |
| 57 | + */ |
| 58 | +export function withDecorate(elem: HTMLElement): void { |
| 59 | + // 当前元素匹配 |
| 60 | + if (elem.matches('[data-code]')) { |
| 61 | + mount(elem as HTMLPreElement); |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + const elems = elem.querySelectorAll('[data-code]'); |
| 66 | + for (const elem of elems) { |
| 67 | + mount(elem as HTMLPreElement); |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +/** |
| 72 | + * 根据 pre 上的 data-decorate 属性挂载相应的装饰器 |
| 73 | + */ |
| 74 | +function mount(pre: HTMLPreElement) { |
| 75 | + const names = pre.dataset.decorate; |
| 76 | + if (!names) { |
| 77 | + return; |
| 78 | + } |
| 79 | + |
| 80 | + const owner = getOwner(); |
| 81 | + |
| 82 | + for (const name of names.split(',')) { |
| 83 | + const d = decorates.get(name); |
| 84 | + if (d) { |
| 85 | + runWithOwner(owner, () => render(() => d(pre), pre)); |
| 86 | + } |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +/************************* 注册装饰器 **********************************/ |
| 91 | + |
| 92 | +registerDecorate('copy-button', pre => { |
| 93 | + let clipboardRef: ClipboardAPI.RootRef; |
| 94 | + |
| 95 | + return ( |
| 96 | + <Button.Root class={styles.copy} square kind="flat" onclick={() => clipboardRef.writeText(pre.dataset.code ?? '')}> |
| 97 | + <ClipboardAPI.Root ref={el => (clipboardRef = el)} /> |
| 98 | + </Button.Root> |
| 99 | + ); |
| 100 | +}); |
| 101 | + |
| 102 | +registerDecorate('toolbar', pre => { |
| 103 | + let clipboardRef: ClipboardAPI.RootRef; |
| 104 | + |
| 105 | + // 不需要考虑组件卸载之后如何改回原始的情况。 |
| 106 | + // 因为生成的代码就已经固定了 decorate 属性,不会动态切换。 |
| 107 | + pre.style.flexDirection = 'column-reverse'; |
| 108 | + |
| 109 | + return ( |
| 110 | + <header class={styles.toolbar}> |
| 111 | + <span>{pre.dataset.lang}</span> |
| 112 | + <div class={styles.actions}> |
| 113 | + <Button.Root |
| 114 | + class={styles.btn} |
| 115 | + square |
| 116 | + kind="flat" |
| 117 | + onclick={() => clipboardRef.writeText(pre.dataset.code ?? '')} |
| 118 | + > |
| 119 | + <ClipboardAPI.Root ref={el => (clipboardRef = el)} /> |
| 120 | + </Button.Root> |
| 121 | + </div> |
| 122 | + </header> |
| 123 | + ); |
| 124 | +}); |
| 125 | + |
| 126 | +registerDecorate('border', pre => { |
| 127 | + pre.style.border = '1px solid var(--palette-border)'; |
| 128 | + return ''; |
| 129 | +}); |
0 commit comments