[VuePress in action] Take you step by step to develop a code copy plug-in
This article will take you to understand the actual combat of VuePress and introduce how to develop a VuePress plug-in (code copy plug-in) from scratch. I hope it will be helpful to everyone!
In the process of building a VuePress blog, not all plug-ins can meet the needs, so in this article we take the implementation of a code copy plug-in as an example to teach you how to Implement a VuePress plug-in from scratch.
Local development
The first problem to be solved when developing a plug-in is how to develop it locally. We checked the "Developing plug-in" chapter of the VuePress 1.0 official document and did not find it. Solution, but in the "Local Plug-in" of the VuePress 2.0 official document, it is written:
It is recommended that you use the configuration file directly as a plug-in, because almost all Plug-in APIs can be used in configuration files, which is more convenient in most scenarios.
But if you have too many things to do in the configuration file, it is better to extract them into separate plugins and use them by setting absolute paths or via require :
module.exports = { plugins: [ path.resolve(__dirname, './path/to/your-plugin.js'), require('./another-plugin'), ], }
Then let’s get started!
Initialize the project
We create a new folder vuepress-plugin-code-copy
under the .vuepress
folder to store the plug-in Relevant code, and then enter the folder with the command line, execute npm init
, and create package.json
. At this time, the directory of the file is:
.vuepress ├─ vuepress-plugin-code-copy │ └─ package.json └─ config.js
We are in Create a new index.js
file under vuepress-plugin-code-copy
, refer to the writing method in Official Document Plug-in Example, we use the function form of the returned object, This function accepts the plug-in configuration options as the first parameter, and the ctx object containing the compile-time context as the second parameter:
module.exports = (options, ctx) => { return { // ... } }
Refer to the name in the official document Option API, and For the ready hook in the life cycle function, we write an initial test code:
module.exports = (options, ctx) => { return { name: 'vuepress-plugin-code-copy', async ready() { console.log('Hello World!'); } } }
At this time we run yarn run docs:dev
, which can be used during the running process See our plug-in name and print results in:
Plug-in design
Now we can imagine the effect of our code copying the plug-in, I think The effect to be achieved is:
There is a Copy text button in the lower right corner of the code block. After clicking, the text changes to Copied! Then the text changes to Copy again after one second, and the code in the code block is copied to the clipboard when clicked. The expected performance effect is as follows:
plugin Development
If it is in a Vue component, we can easily achieve this effect. When the root component is mounted
or updated
, use document.querySelector
Get all code blocks, insert a button element, and then bind a click event to the button element. When the click event is triggered, copy the code to the clipboard, then modify the text, and then modify the text after 1 second.
Does the VuePress plug-in have a way to control the life cycle of the root component? We check the Option API of the VuePress official documentation and find that VuePress provides a clientRootMixin method:
points to the path to the mixin file, which allows you to control the life cycle of the root component.
Look at the sample code:
// 插件的入口 const path = require('path') module.exports = { clientRootMixin: path.resolve(__dirname, 'mixin.js') }
// mixin.js export default { created () {}, mounted () {} }
Isn’t this what we need? Then let’s do it, modify the content of index.js
to:
const path = require('path'); module.exports = (options, ctx) => { return { name: 'vuepress-plugin-code-copy', clientRootMixin: path.resolve(__dirname, 'clientRootMixin.js') } }
Create a new clientRootMixin.js# under
vuepress-plugin-code-copy ##File, code writing:
export default { updated() { setTimeout(() => { document.querySelectorAll('div[class*="language-"] pre').forEach(el => { console.log('one code block') }) }, 100) } }Refresh the page in the browser, and then view the print:
接下来就要思考如何写入按钮元素了。
当然我们可以使用原生 JavaScript 一点点的创建元素,然后插入其中,但我们其实是在一个支持 Vue 语法的项目里,其实我们完全可以创建一个 Vue 组件,然后将组件的实例挂载到元素上。那用什么方法挂载呢?
我们可以在 Vue 的全局 API 里,找到 Vue.extend
API,看一下使用示例:
// 要挂载的元素 <div id="mount-point"></div>
// 创建构造器 var Profile = Vue.extend({ template: '<p>{{firstName}} {{lastName}} aka {{alias}}</p>', data: function () { return { firstName: 'Walter', lastName: 'White', alias: 'Heisenberg' } } }) // 创建 Profile 实例,并挂载到一个元素上。 new Profile().$mount('#mount-point')
结果如下:
// 结果为: <p>Walter White aka Heisenberg</p>
那接下来,我们就创建一个 Vue 组件,然后通过 Vue.extend
方法,挂载到每个代码块元素中。
在 vuepress-plugin-code-copy
下新建一个 CodeCopy.vue
文件,写入代码如下:
<template> <span class="code-copy-btn" @click="copyToClipboard">{{ buttonText }}</span> </template> <script> export default { data() { return { buttonText: 'Copy' } }, methods: { copyToClipboard(el) { this.setClipboard(this.code, this.setText); }, setClipboard(code, cb) { if (navigator.clipboard) { navigator.clipboard.writeText(code).then( cb, () => {} ) } else { let copyelement = document.createElement('textarea') document.body.appendChild(copyelement) copyelement.value = code copyelement.select() document.execCommand('Copy') copyelement.remove() cb() } }, setText() { this.buttonText = 'Copied!' setTimeout(() => { this.buttonText = 'Copy' }, 1000) } } } </script> <style scoped> .code-copy-btn { position: absolute; bottom: 10px; right: 7.5px; opacity: 0.75; cursor: pointer; font-size: 14px; } .code-copy-btn:hover { opacity: 1; } </style>
该组件实现了按钮的样式和点击时将代码写入剪切版的效果,整体代码比较简单,就不多叙述了。
我们修改一下 clientRootMixin.js
:
import CodeCopy from './CodeCopy.vue' import Vue from 'vue' export default { updated() { // 防止阻塞 setTimeout(() => { document.querySelectorAll('div[class*="language-"] pre').forEach(el => { // 防止重复写入 if (el.classList.contains('code-copy-added')) return let ComponentClass = Vue.extend(CodeCopy) let instance = new ComponentClass() instance.code = el.innerText instance.$mount() el.classList.add('code-copy-added') el.appendChild(instance.$el) }) }, 100) } }
这里注意两点,第一是我们通过 el.innerText
获取要复制的代码内容,然后写入到实例的 code
属性,在组件中,我们是通过 this.code
获取的。
第二是我们没有使用 $mount(element)
,直接传入一个要挂载的节点元素,这是因为 $mount()
的挂载会清空目标元素,但是这里我们需要添加到元素中,所以我们在执行 instance.$mount()
后,通过 instance.$el
获取了实例元素,然后再将其 appendChild
到每个代码块中。关于 $el
的使用可以参考官方文档的 el 章节 。
此时,我们的文件目录如下:
.vuepress ├─ vuepress-plugin-code-copy │ ├─ CodeCopy.vue │ ├─ clientRootMixin.js │ ├─ index.js │ └─ package.json └─ config.js
至此,其实我们就已经实现了代码复制的功能。
插件选项
有的时候,为了增加插件的可拓展性,会允许配置可选项,就比如我们不希望按钮的文字是 Copy,而是中文的「复制」,复制完后,文字变为 「已复制!」,该如何实现呢?
前面讲到,我们的 index.js
导出的函数,第一个参数就是 options 参数:
const path = require('path'); module.exports = (options, ctx) => { return { name: 'vuepress-plugin-code-copy', clientRootMixin: path.resolve(__dirname, 'clientRootMixin.js') } }
我们在 config.js
先写入需要用到的选项:
module.exports = { plugins: [ [ require('./vuepress-plugin-code-copy'), { 'copybuttonText': '复制', 'copiedButtonText': '已复制!' } ] ] }
我们 index.js
中通过 options
参数可以接收到我们在 config.js
写入的选项,但我们怎么把这些参数传入 CodeCopy.vue
文件呢?
我们再翻下 VuePress 提供的 Option API,可以发现有一个 define API,其实这个 define 属性就是定义我们插件内部使用的全局变量。我们修改下 index.js
:
const path = require('path'); module.exports = (options, ctx) => { return { name: 'vuepress-plugin-code-copy', define: { copybuttonText: options.copybuttonText || 'copy', copiedButtonText: options.copiedButtonText || "copied!" }, clientRootMixin: path.resolve(__dirname, 'clientRootMixin.js') } }
现在我们已经写入了两个全局变量,组件里怎么使用呢?答案是直接使用!
我们修改下 CodeCopy.vue
的代码:
// ... <script> export default { data() { return { buttonText: copybuttonText } }, methods: { copyToClipboard(el) { this.setClipboard(this.code, this.setText); }, setClipboard(code, cb) { if (navigator.clipboard) { navigator.clipboard.writeText(code).then( cb, () => {} ) } else { let copyelement = document.createElement('textarea') document.body.appendChild(copyelement) copyelement.value = code copyelement.select() document.execCommand('Copy') copyelement.remove() cb() } }, setText() { this.buttonText = copiedButtonText setTimeout(() => { this.buttonText = copybuttonText }, 1000) } } } </script> // ...
最终的效果如下:
代码参考
完整的代码查看:https://github.com/mqyqingfeng/Blog/tree/master/demos/VuePress/vuepress-plugin-code-copy
【相关推荐:vue.js视频教程】
The above is the detailed content of [VuePress in action] Take you step by step to develop a code copy plug-in. For more information, please follow other related articles on the PHP Chinese website!

Netflix's front-end technology stack is mainly based on React and Redux. 1.React is used to build high-performance single-page applications, and improves code reusability and maintenance through component development. 2. Redux is used for state management to ensure that state changes are predictable and traceable. 3. The toolchain includes Webpack, Babel, Jest and Enzyme to ensure code quality and performance. 4. Performance optimization is achieved through code segmentation, lazy loading and server-side rendering to improve user experience.

Vue.js is a progressive framework suitable for building highly interactive user interfaces. Its core functions include responsive systems, component development and routing management. 1) The responsive system realizes data monitoring through Object.defineProperty or Proxy, and automatically updates the interface. 2) Component development allows the interface to be split into reusable modules. 3) VueRouter supports single-page applications to improve user experience.

The main disadvantages of Vue.js include: 1. The ecosystem is relatively new, and third-party libraries and tools are not as rich as other frameworks; 2. The learning curve becomes steep in complex functions; 3. Community support and resources are not as extensive as React and Angular; 4. Performance problems may be encountered in large applications; 5. Version upgrades and compatibility challenges are greater.

Netflix uses React as its front-end framework. 1.React's component development and virtual DOM mechanism improve performance and development efficiency. 2. Use Webpack and Babel to optimize code construction and deployment. 3. Use code segmentation, server-side rendering and caching strategies for performance optimization.

Reasons for Vue.js' popularity include simplicity and easy learning, flexibility and high performance. 1) Its progressive framework design is suitable for beginners to learn step by step. 2) Component-based development improves code maintainability and team collaboration efficiency. 3) Responsive systems and virtual DOM improve rendering performance.

Vue.js is easier to use and has a smooth learning curve, which is suitable for beginners; React has a steeper learning curve, but has strong flexibility, which is suitable for experienced developers. 1.Vue.js is easy to get started with through simple data binding and progressive design. 2.React requires understanding of virtual DOM and JSX, but provides higher flexibility and performance advantages.

Vue.js is suitable for fast development and small projects, while React is more suitable for large and complex projects. 1.Vue.js is simple and easy to learn, suitable for rapid development and small projects. 2.React is powerful and suitable for large and complex projects. 3. The progressive features of Vue.js are suitable for gradually introducing functions. 4. React's componentized and virtual DOM performs well when dealing with complex UI and data-intensive applications.

Vue.js and React each have their own advantages and disadvantages. When choosing, you need to comprehensively consider team skills, project size and performance requirements. 1) Vue.js is suitable for fast development and small projects, with a low learning curve, but deep nested objects can cause performance problems. 2) React is suitable for large and complex applications, with a rich ecosystem, but frequent updates may lead to performance bottlenecks.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Dreamweaver Mac version
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools
