当前位置:   article > 正文

vue3通过monaco-editor实现文本对比功能_monaco编辑器开启对比

monaco编辑器开启对比

安装插件:

monaco-editor必须安装,余下两个根据你的项目类型去下载

npm install monaco-editor@0.44.0 --save-dev
npm install monaco-editor-webpack-plugin@6.0.0 --save-dev

npm install vite-plugin-monaco-editor@1.1.0 -save-dev

 常用属性:

  1. {
  2. value: "",
  3. theme: "vs-dark", // 编辑器主题:vs, hc-black, or vs-dark,更多选择详见官网https://microsoft.github.io/monaco-editor/docs.html
  4. roundedSelection: false, // 编辑器是否有圆角
  5. scrollbar: {
  6. verticalScrollbarSize: 0,
  7. },
  8. readOnly: true, // 是否只读
  9. autoIndent: true, // 自动缩进
  10. scrollBeyondLastLine: false, // 滚动条是否可以滚到最后一条之后
  11. diffWordWrap: true,
  12. wordWrap: "on", // 文字过长是否自动换行
  13. enableSplitViewResizing: false,
  14. originalEditable: true, // 原始数据是否可编辑
  15. automaticLayout: true, // 自适应页面宽度
  16. }

引入控件,避免引入全部控件导致体积过大可以参考如下方法:

  1. import * as monaco from "monaco-editor";
  2. import editorWorker from "monaco-editor/esm/vs/editor/editor.worker?worker";
  3. import jsonWorker from "monaco-editor/esm/vs/language/json/json.worker?worker";
  4. import cssWorker from "monaco-editor/esm/vs/language/css/css.worker?worker";
  5. import htmlWorker from "monaco-editor/esm/vs/language/html/html.worker?worker";
  6. import tsWorker from "monaco-editor/esm/vs/language/typescript/ts.worker?worker";
  7. self.MonacoEnvironment = {
  8. getWorker(_, label) {
  9. if (label === "json") {
  10. return new jsonWorker();
  11. }
  12. if (label === "css" || label === "scss" || label === "less") {
  13. return new cssWorker();
  14. }
  15. if (label === "html" || label === "handlebars" || label === "razor") {
  16. return new htmlWorker();
  17. }
  18. if (label === "typescript" || label === "javascript") {
  19. return new tsWorker();
  20. }
  21. return new editorWorker();
  22. },
  23. };

整体代码:

  1. <template>
  2. <div class="app-container">
  3. <div id="container" style="height: calc(100% - 50px); width: 100%"></div>
  4. </div>
  5. </template>
  6. <script setup>
  7. import { getUploadList } from "@/api/ekms/article";
  8. import * as monaco from "monaco-editor";
  9. import editorWorker from "monaco-editor/esm/vs/editor/editor.worker?worker";
  10. // import jsonWorker from "monaco-editor/esm/vs/language/json/json.worker?worker";
  11. // import cssWorker from "monaco-editor/esm/vs/language/css/css.worker?worker";
  12. // import htmlWorker from "monaco-editor/esm/vs/language/html/html.worker?worker";
  13. // import tsWorker from "monaco-editor/esm/vs/language/typescript/ts.worker?worker";
  14. const route = useRoute();
  15. const { proxy } = getCurrentInstance();
  16. let diffEditor = null;
  17. let originalModel = null;
  18. let modifiedModel = null;
  19. const upExpect = ref(null);
  20. const compareData = ref({});
  21. const originalText = ref("");
  22. const modifiedText = ref("");
  23. onBeforeMount(() => {
  24. getInitUpload();
  25. compareData.value = JSON.parse(localStorage.getItem("compare"));
  26. });
  27. onMounted(() => {
  28. nextTick(() => {
  29. originalText.value = compareData.value.original.attachContent
  30. ? compareData.value.original.attachContent
  31. : compareData.value.original.content;
  32. modifiedText.value = compareData.value.modified.attachContent
  33. ? compareData.value.modified.attachContent
  34. : compareData.value.modified.content;
  35. defineDiff();
  36. });
  37. });
  38. self.MonacoEnvironment = {
  39. getWorker(_, label) {
  40. // if (label === "json") {
  41. // return new jsonWorker();
  42. // }
  43. // if (label === "css" || label === "scss" || label === "less") {
  44. // return new cssWorker();
  45. // }
  46. // if (label === "html" || label === "handlebars" || label === "razor") {
  47. // return new htmlWorker();
  48. // }
  49. // if (label === "typescript" || label === "javascript") {
  50. // return new tsWorker();
  51. // }
  52. return new editorWorker();
  53. },
  54. };
  55. const defineDiff = () => {
  56. originalModel && originalModel.dispose();
  57. modifiedModel && modifiedModel.dispose();
  58. diffEditor && diffEditor.dispose();
  59. originalModel = monaco.editor.createModel(originalText.value, "text/plain");
  60. modifiedModel = monaco.editor.createModel(modifiedText.value, "text/plain");
  61. diffEditor = monaco.editor.createDiffEditor(
  62. document.getElementById("container"),
  63. {
  64. // value: "",
  65. // theme: "vs-dark", // 编辑器主题:vs, hc-black, or vs-dark,更多选择详见官网https://microsoft.github.io/monaco-editor/docs.html
  66. // roundedSelection: false, // 编辑器是否有圆角
  67. // scrollbar: {
  68. // verticalScrollbarSize: 0,
  69. // },
  70. // readOnly: true, // 是否只读
  71. // autoIndent: true, // 自动缩进
  72. scrollBeyondLastLine: false, // 滚动条是否可以滚到最后一条之后
  73. diffWordWrap: true,
  74. wordWrap: "on", // 文字过长是否自动换行
  75. enableSplitViewResizing: false,
  76. originalEditable: true, // 原始数据是否可编辑
  77. automaticLayout: true, // 自适应页面宽度
  78. }
  79. );
  80. diffEditor.setModel({
  81. original: originalModel,
  82. modified: modifiedModel,
  83. });
  84. };
  85. </script>
  86. <style lang="scss" scoped></style>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/710216
推荐阅读