網站首頁 編程語言 正文
react 代碼塊輸出,代碼高亮顯示,帶行號,能復制
以modal組件為例
import React, { useState, useEffect } from 'react';
import { Modal, Button, message } from 'antd';
import Highlight from 'react-highlight';
import "highlight.js/styles/mono-blue.css";
import { CopyToClipboard } from 'react-copy-to-clipboard';
import "./infoModal.less";
import hljs from 'highlight.js';
?
export default function infoModal(props: any) {
? const { visible, handleCancel, handleOk, modalType } = props;
? const [title, setIitle] = useState('');
? const [num, setNum] = useState(0);
? const [data, setData] = useState({
? ? "a": title,
? ? "a2": "1",
? ? "a3": "1",
? ? "a4": "1",
? ? "a5": "1",
? ? "a6": "1",
? ? "a7": "1",
? ? "a8": "1",
? ? "a9": "1",
? });
?
?
?
? useEffect(() => {
? ? if (modalType === "in") { setIitle("入參信息") } else { setIitle("出參信息") };
? }, [modalType]);
?
? //顯示行號
? useEffect(() => {
? ? if (visible) {
? ? ? hljs.initHighlightingOnLoad();
? ? ? document.querySelectorAll('pre code').forEach((block) => {
? ? ? ? block.innerHTML = "<ul id='ulcode'><li>" + block.innerHTML.replace(/\n/g, "\n</li><li>") + " </li></ul>";
? ? ? });
? ? }
? }, [visible])
?
? const handleCopy = () => {
? ? message.success('復制成功!')
? };
?
? return <div>
? ? <Modal title={title} visible={visible} onCancel={handleCancel} onOk={handleOk}
? ? ? footer={[
? ? ? ? <CopyToClipboard text={JSON.stringify(data, null, 2)} onCopy={handleCopy}>
? ? ? ? ? <Button>復制</Button>
? ? ? ? </CopyToClipboard>,
? ? ? ? <Button key="submit" type="primary" onClick={handleCancel}>確定</Button>
? ? ? ]}
? ? >
? ? ? <Highlight className="javascript mycode" >
? ? ? ? {JSON.stringify(data, null, 2)}
? ? ? </Highlight>
? ? </Modal>
? </div>;
}
infoModal.less樣式
.hljs ul {?? ??? ??? ?
? list-style: decimal;?? ??? ??? ?
? margin: 0 0 0 40px!important;?? ??? ??? ?
? padding: 0?? ??? ??? ?
? }?? ??? ??? ?
? .hljs li {?? ??? ??? ?
? list-style: decimal-leading-zero;?? ??? ??? ?
? border-left: 1px solid #333!important;?? ??? ??? ?
? padding: 2px 5px!important;?? ??? ??? ?
? margin: 0!important;?? ??? ??? ?
? line-height: 14px;?? ??? ??? ?
? width: 100%;?? ??? ??? ?
? box-sizing: border-box
? }
? .hljs li:nth-of-type(even) {
? background-color: rgba(255,255,255,.015);
? color: inherit
? }
?
?
? .javascript{
? ? max-height:400px;
? }
效果圖:
- 代碼塊顯示:react-highlight插件
- 代碼塊復制:react-copy-to-clipboard插件
- 代碼塊顯示行號:highlight.js插件+js代碼(上文中已注釋)+less樣式(上文中已添加)
react 代碼塊插件
代碼塊插件
vscode擴展搜索 ES7 React/Redux/GraphQL/React-Native snippetsd或者React-Native/React/Redux snippets for es6/es7 并安裝(如果安裝后者 直接cccs可以一鍵生成模板塊 安裝前者 步驟如下)
原文鏈接:https://blog.csdn.net/qq_32584661/article/details/122705283
相關推薦
- 2022-11-15 C++構造析構賦值運算函數應用詳解_C 語言
- 2022-07-08 python?動態規劃問題解析(背包問題和最長公共子串)_python
- 2022-03-17 Docker容器之間的通信的方法實現_docker
- 2022-10-20 Swift協議Protocol介紹_Swift
- 2022-04-27 Python學習筆記之字典,元組,布爾類型和讀寫文件_python
- 2022-07-19 Eureka注冊中心之搭建eureka-server
- 2022-09-19 Tomca啟動閃退問題解決(八大類)_Tomcat
- 2022-10-15 C++算法實現leetcode?1252奇數值單元格數目_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支