vue中使用prismjs实现代码预览及一键复制

vue中使用prismjs实现代码预览

  • npm install prismjs
    在这里插入图片描述
<template>
   <pre class="codeTree q-ma-none">
        <q-btn
          v-if="codeContent"
          id="copyCode"
          dense
          flat
          icon="copy_all"
          size="md"
          color="primary"
          :data-clipboard-text="nativeCode"
          class="copyIcon"
          @click="copyCode">
          	<q-tooltip>全部复制</q-tooltip>
        </q-btn>
        <code class="line-numbers relative-position" :class="classCodeType"  v-html="codeContent"></code>
        </pre>
</template>

<script>
import Prism from "prismjs";
import ClipboardJS from "clipboard";
import "prismjs/themes/prism.css";
import builderService from "./builder-service";
export default {
  name: "CodeView",
  data() {
    return {
      codeId: null,
      nativeCode: null,
      codeContent: null,
      codeType: "",
      classCodeType: "",
    };
  },
  watch: {
    codeContent(newValue, oldValue) {
      this.$nextTick(() => {
        Prism.highlightAll();
      });
    },
  },
  // 防止页签未关闭再进来id丢失
  beforeDestroy() {
    this.$q.sessionStorage.set("codeViewId", this.codeId);
  },
  methods: {
    copyCode() {
      const clipboard = new ClipboardJS("#copyCode");
      clipboard.on("success", (e) => {
        this.$q.notify({
          caption: "代码已复制到剪切板,可粘贴",
          message: "操作成功",
          type: "positive",
        });
        clipboard.destroy();
      });
      clipboard.on("error", (e) => {
        this.$q.notify({
          message: "代码复制失败",
          type: "negative",
        });
        clipboard.destroy();
      });
    },
    // 获取代码
    getCodeContent(id, codeFileName) {
      builderService.codeViews(id, codeFileName).then((res) => {
        this.visible = false;
        // 由于Prismjs不支持vue,按照js风格处理处理
        const languageType = Prism.languages.js;
        this.codeContent = Prism.highlight(
          res.data,
          languageType,
          this.codeType
        );
        this.nativeCode = res.data;
      });
    },
  },
};
</script>

<style lang="scss">
@import "@/styles/quasar.variables.scss";
.code-view {
  padding: 15px;
  overflow: auto;
  .codeTree {
    height: 100%;
    border-radius: 4px;
    display: flex;
    flex-wrap: nowrap;
    flex-direction: column;
    padding: 10px;
    overflow: auto;
    border: 1px solid $separator-color;
  }
  pre[class*="language-"] {
    padding: 0;
    margin: 0;
  }
  .copyIcon {
    position: absolute;
    top: 0;
    right: 15px;
    z-index: 999;
  }
}
</style>
Vue 3 可以通过自定义指令 (Directive) 和 Marked 库来实现代码一键复制的功能。 下面是实现的基本步骤: 1. 安装 Marked 库 ```bash npm install marked ``` 2. 创建一个自定义指令 在 Vue 3 ,自定义指令可以通过 `app.directive` 方法来创建。下面是一个创建 `copy-code` 指令的示例代码: ```javascript import marked from 'marked'; const copyCodeDirective = { mounted(el, binding) { const code = el.querySelector('code'); const button = document.createElement('button'); button.innerText = 'Copy'; button.addEventListener('click', () => { navigator.clipboard.writeText(code.textContent.trim()); button.innerText = 'Copied!'; setTimeout(() => { button.innerText = 'Copy'; }, 2000); }); el.appendChild(button); }, updated(el) { const code = el.querySelector('code'); const html = marked(code.textContent); code.innerHTML = html; } }; export default copyCodeDirective; ``` 这个自定义指令会在绑定的元素上添加一个 "Copy" 按钮,并且当用户点击按钮时,会将元素内部的代码文本复制到剪贴板。 3. 在组件使用自定义指令 ```html <template> <pre v-copy-code><code>{{ code }}</code></pre> </template> <script> import copyCodeDirective from './copyCodeDirective.js'; export default { directives: { copyCode: copyCodeDirective }, data() { return { code: 'console.log("Hello, world!");' } } }; </script> ``` 在组件,我们可以将 `v-copy-code` 指令绑定到 `<pre>` 标签上,然后将代码文本插入到 `<code>` 标签。 当我们在浏览器运行这个组件时,就会看到代码旁边出现一个 "Copy" 按钮。当我们点击该按钮时,代码文本就会被复制到剪贴板。 希望这个示例能够帮助您实现您的功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值