BoxMask.vue 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. <template>
  2. <transition name="fade">
  3. <div class="markdown-preview-wrapper" v-show="visible" @click.self="closeMarkdownPreview">
  4. <!-- 顶部信息栏 & 工具栏 -->
  5. <div class="tip-wrapper" v-if="visible">
  6. <div class="name" :title="getFileNameComplete(fileInfo)">
  7. {{ getFileNameComplete(fileInfo) }}
  8. <span class="un-save" v-show="isModify">(未保存)</span>
  9. </div>
  10. <div class="editor-preveiw">在线预览{{ editable ? ' & 编辑' : '' }}</div>
  11. <div class="tool-wrapper">
  12. <a
  13. class="item download-link"
  14. target="_blank"
  15. :href="getDownloadFilePath(fileInfo)"
  16. :download="getFileNameComplete(fileInfo)"
  17. >
  18. <download-outlined title="下载" />
  19. </a>
  20. <a-tooltip placement="bottom">
  21. <template #title>
  22. <div>
  23. 1. 点击文档以外的区域可退出查看<br />
  24. 2. 按 Esc 键可退出查看
  25. </div>
  26. </template>
  27. <div class="item text-wrapper">
  28. <span class="text">操作提示</span>
  29. <bulb-outlined />
  30. </div>
  31. </a-tooltip>
  32. <close-outlined class="item" title="关闭预览" @click="closeMarkdownPreview" />
  33. </div>
  34. </div>
  35. <!-- mavon-editor 组件,配置项说明文档 https://www.npmjs.com/package/mavon-editor -->
  36. <a-spin :spinning="markdownLoading">
  37. <mavonEditor
  38. ref="mavonEditorRef"
  39. v-model="markdownText"
  40. :toolbars="toolbars"
  41. :editable="editable"
  42. :toolbarsFlag="toolbarsFlag"
  43. :externalLink="externalLink"
  44. :subfield="screenWidth > 768 ? true : false"
  45. defaultOpen="preview"
  46. @save="handleModifyFileContent"
  47. ></mavonEditor>
  48. </a-spin>
  49. </div>
  50. </transition>
  51. </template>
  52. <script setup>
  53. import { ref, computed, watch, nextTick, getCurrentInstance } from 'vue'
  54. import { mavonEditor } from 'mavon-editor'
  55. import 'mavon-editor/dist/css/index.css'
  56. // 代码高亮样式表
  57. import '@/assets/mavonEditor/css/tomorrow-night.css'
  58. import '@/assets/mavonEditor/css/github-markdown.css'
  59. import { useMyResourceStore } from '@/store/myResource.js'
  60. import { getFilePreview, modifyFileContent } from '@/api/myResource/file.js'
  61. import { DownloadOutlined, BulbOutlined, CloseOutlined } from '@ant-design/icons-vue'
  62. import tool from '@/utils/tool'
  63. // 获取全局变量
  64. const { proxy } = getCurrentInstance()
  65. // 获取store
  66. const myResourceStore = useMyResourceStore()
  67. // 定义props
  68. const props = defineProps({
  69. fileInfo: Object,
  70. editable: Boolean,
  71. callback: Function
  72. })
  73. // 从proxy中获取$file方法
  74. const getFileNameComplete = (fileInfo) => {
  75. return proxy.$file.getFileNameComplete(fileInfo)
  76. }
  77. const getDownloadFilePath = (fileInfo) => {
  78. return proxy.$file.getDownloadFilePath(fileInfo)
  79. }
  80. // 响应式数据
  81. const visible = ref(false)
  82. const originalMarkdownText = ref('')
  83. const markdownText = ref('')
  84. const markdownLoading = ref(false)
  85. // 暴露visible属性给父组件
  86. defineExpose({ visible })
  87. // 工具栏配置
  88. const toolbars = {
  89. bold: true, // 粗体
  90. italic: true, // 斜体
  91. header: true, // 标题
  92. underline: true, // 下划线
  93. strikethrough: true, // 中划线
  94. mark: true, // 标记
  95. superscript: true, // 上角标
  96. subscript: true, // 下角标
  97. quote: true, // 引用
  98. ol: true, // 有序列表
  99. ul: true, // 无序列表
  100. link: true, // 链接
  101. imagelink: true, // 图片链接
  102. code: true, // code
  103. table: true, // 表格
  104. fullscreen: true, // 全屏编辑
  105. readmodel: true, // 沉浸式阅读
  106. htmlcode: true, // 展示html源码
  107. help: true, // 帮助
  108. /* 1.3.5 */
  109. undo: true, // 上一步
  110. redo: true, // 下一步
  111. trash: true, // 清空
  112. save: true, // 保存(触发 events 中的 save 事件)
  113. /* 1.4.2 */
  114. navigation: true, // 导航目录
  115. /* 2.1.8 */
  116. aligncenter: true, // 居中
  117. alignleft: true, // 左对齐
  118. alignright: true, // 右对齐
  119. /* 2.2.1 */
  120. subfield: true, // 单双栏模式
  121. preview: true // 预览
  122. }
  123. // 计算属性
  124. // 屏幕宽度
  125. const screenWidth = computed(() => {
  126. return myResourceStore.screenWidth
  127. })
  128. // 是否修改
  129. const isModify = computed(() => {
  130. return originalMarkdownText.value !== markdownText.value
  131. })
  132. // 外链 cdn 改为本地引入
  133. const externalLink = computed(() => {
  134. let context = process.env.NODE_ENV === 'production' ? '/' : '/'
  135. return {
  136. markdown_css: function () {
  137. // 这是你的markdown css文件路径
  138. return `${context}mavonEditor/css/github-markdown.css`
  139. },
  140. hljs_js: function () {
  141. // 这是你的hljs文件路径
  142. return `${context}mavonEditor/js/highlight.min.js`
  143. },
  144. hljs_css: function () {
  145. // 这是你的代码高亮配色文件路径
  146. return `${context}mavonEditor/css/tomorrow-night.css`
  147. },
  148. hljs_lang: function () {
  149. // 这是你的代码高亮语言解析路径
  150. return `${context}mavonEditor/js/lang.hljs.js`
  151. },
  152. katex_css: function () {
  153. // 这是你的katex配色方案路径路径
  154. return `${context}mavonEditor/css/katex.min.css`
  155. },
  156. katex_js: function () {
  157. // 这是你的katex.js路径
  158. return `${context}mavonEditor/js/katex.min.js`
  159. }
  160. }
  161. })
  162. // 工具栏是否展示
  163. const toolbarsFlag = computed(() => {
  164. return props.editable
  165. })
  166. // 监听 markdown 查看组件 显隐状态变化
  167. watch(visible, (val) => {
  168. if (val) {
  169. getMarkdownText()
  170. // 添加键盘 Esc 事件
  171. nextTick(() => {
  172. document.addEventListener('keyup', handleKeyup)
  173. })
  174. } else {
  175. document.removeEventListener('keyup', handleKeyup)
  176. }
  177. })
  178. // 键盘事件处理函数
  179. const handleKeyup = (e) => {
  180. if (e.key === 'Escape') {
  181. closeMarkdownPreview()
  182. }
  183. }
  184. /**
  185. * 获取 markdown 文本内容
  186. */
  187. const getMarkdownText = () => {
  188. markdownLoading.value = true
  189. getFilePreview({
  190. userFileId: props.fileInfo.userFileId,
  191. isMin: false,
  192. shareBatchNum: props.fileInfo.shareBatchNum,
  193. extractionCode: props.fileInfo.extractionCode,
  194. token: tool.data.get('TOKEN')
  195. }).then((res) => {
  196. markdownLoading.value = false
  197. originalMarkdownText.value = res
  198. markdownText.value = res
  199. })
  200. }
  201. /**
  202. * 修改 markdown 文本内容
  203. */
  204. const handleModifyFileContent = () => {
  205. markdownLoading.value = true
  206. modifyFileContent({
  207. userFileId: props.fileInfo.userFileId,
  208. fileContent: markdownText.value
  209. })
  210. .then((res) => {
  211. markdownLoading.value = false
  212. if (res.success) {
  213. proxy.$message.success('已保存')
  214. getMarkdownText()
  215. } else {
  216. proxy.$message.error(res.message)
  217. }
  218. })
  219. .catch((err) => {
  220. markdownLoading.value = false
  221. proxy.$message.error(err.message)
  222. })
  223. }
  224. /**
  225. * 关闭 markdown 预览
  226. */
  227. const closeMarkdownPreview = () => {
  228. visible.value = false
  229. props.callback('cancel')
  230. }
  231. </script>
  232. <style lang="less" scoped>
  233. @import '@/style/myResource/varibles.less';
  234. .markdown-preview-wrapper {
  235. position: fixed;
  236. top: 0;
  237. right: 0;
  238. bottom: 0;
  239. left: 0;
  240. overflow: auto;
  241. width: 100%;
  242. height: 100vh;
  243. z-index: 2;
  244. text-align: center;
  245. display: flex;
  246. align-items: center;
  247. animation: imgPreviewAnimation 0.3s;
  248. -webkit-animation: imgPreviewAnimation 0.3s; /* Safari and Chrome */
  249. animation-iteration-count: 0.3;
  250. -webkit-animation-iteration-count: 0.3;
  251. animation-fill-mode: forwards;
  252. -webkit-animation-fill-mode: forwards; /* Safari 和 Chrome */
  253. @keyframes imgPreviewAnimation {
  254. 0% {
  255. background: transparent;
  256. }
  257. 100% {
  258. background: rgba(0, 0, 0, 0.8);
  259. }
  260. }
  261. @keyframes imgPreviewAnimation {
  262. 0% {
  263. background: transparent;
  264. }
  265. 100% {
  266. background: rgba(0, 0, 0, 0.8);
  267. }
  268. }
  269. .tip-wrapper {
  270. position: fixed;
  271. top: 0;
  272. left: 0;
  273. z-index: 2;
  274. background: rgba(0, 0, 0, 0.5);
  275. padding: 0 48px;
  276. width: 100%;
  277. height: 48px;
  278. line-height: 48px;
  279. color: #fff;
  280. font-size: 16px;
  281. display: flex;
  282. justify-content: space-between;
  283. .name {
  284. flex: 1;
  285. padding-right: 16px;
  286. text-align: left;
  287. overflow: hidden;
  288. text-overflow: ellipsis;
  289. white-space: nowrap;
  290. .un-save {
  291. color: @Warning;
  292. font-size: 14px;
  293. }
  294. }
  295. .tool-wrapper {
  296. flex: 1;
  297. display: flex;
  298. justify-content: flex-end;
  299. .item {
  300. margin-left: 16px;
  301. height: 48px;
  302. line-height: 48px;
  303. cursor: pointer;
  304. &:hover {
  305. opacity: 0.7;
  306. }
  307. }
  308. .save-img {
  309. margin: 16px 0;
  310. width: 16px;
  311. height: 16px;
  312. }
  313. .download-link {
  314. color: inherit;
  315. font-size: 18px;
  316. }
  317. .text-wrapper {
  318. .text {
  319. margin-right: 8px;
  320. }
  321. }
  322. }
  323. }
  324. :deep(.v-note-wrapper) {
  325. box-shadow: none !important;
  326. border: 1px solid @border-base;
  327. .v-note-op {
  328. border-bottom-color: @border-base;
  329. .op-image {
  330. .dropdown-item:nth-of-type(2) {
  331. display: none;
  332. }
  333. }
  334. }
  335. .v-note-navigation-wrapper {
  336. .v-note-navigation-content {
  337. h1,
  338. h2,
  339. h3,
  340. h4,
  341. h5,
  342. h6 {
  343. color: @primary-text;
  344. &:hover {
  345. color: @primary-text;
  346. }
  347. }
  348. }
  349. }
  350. }
  351. :deep(.v-note-wrapper:not(.fullscreen)) {
  352. margin: 56px auto 0 auto;
  353. width: 90vw;
  354. height: calc(100vh - 80px);
  355. }
  356. }
  357. </style>