index.vue 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <template>
  2. <PageWrapper title="代码编辑器组件示例" contentFullHeight fixedHeight contentBackground>
  3. <template #extra>
  4. <a-space size="middle">
  5. <a-button @click="showData" type="primary">获取数据</a-button>
  6. <RadioGroup button-style="solid" v-model:value="modeValue" @change="handleModeChange">
  7. <RadioButton value="application/json"> json数据 </RadioButton>
  8. <RadioButton value="htmlmixed"> html代码 </RadioButton>
  9. <RadioButton value="javascript"> javascript代码 </RadioButton>
  10. </RadioGroup>
  11. </a-space>
  12. </template>
  13. <CodeEditor v-model:value="value" :mode="modeValue" />
  14. </PageWrapper>
  15. </template>
  16. <script lang="ts">
  17. import { defineComponent, ref } from 'vue';
  18. import { CodeEditor } from '/@/components/CodeEditor';
  19. import { PageWrapper } from '/@/components/Page';
  20. import { Radio, Space, Modal } from 'ant-design-vue';
  21. const jsonData =
  22. '{"name":"BeJson","url":"http://www.xxx.com","page":88,"isNonProfit":true,"address":{"street":"科技园路.","city":"江苏苏州","country":"中国"},"links":[{"name":"Google","url":"http://www.xxx.com"},{"name":"Baidu","url":"http://www.xxx.com"},{"name":"SoSo","url":"http://www.xxx.com"}]}';
  23. const jsData = `
  24. (() => {
  25. var htmlRoot = document.getElementById('htmlRoot');
  26. var theme = window.localStorage.getItem('__APP__DARK__MODE__');
  27. if (htmlRoot && theme) {
  28. htmlRoot.setAttribute('data-theme', theme);
  29. theme = htmlRoot = null;
  30. }
  31. })();
  32. `;
  33. const htmlData = `
  34. <!DOCTYPE html>
  35. <html lang="en" id="htmlRoot">
  36. <head>
  37. <meta charset="UTF-8" />
  38. <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
  39. <meta name="renderer" content="webkit" />
  40. <meta
  41. name="viewport"
  42. content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,user-scalable=0"
  43. />
  44. <title><%= title %></title>
  45. <link rel="icon" href="/favicon.ico" />
  46. </head>
  47. <body>
  48. <div id="app">
  49. </div>
  50. </body>
  51. </html>
  52. `;
  53. export default defineComponent({
  54. components: {
  55. CodeEditor,
  56. PageWrapper,
  57. RadioButton: Radio.Button,
  58. RadioGroup: Radio.Group,
  59. ASpace: Space,
  60. },
  61. setup() {
  62. const modeValue = ref('application/json');
  63. const value = ref(jsonData);
  64. function handleModeChange(e: ChangeEvent) {
  65. const mode = e.target.value;
  66. if (mode === 'application/json') {
  67. value.value = jsonData;
  68. return;
  69. }
  70. if (mode === 'htmlmixed') {
  71. value.value = htmlData;
  72. return;
  73. }
  74. if (mode === 'javascript') {
  75. value.value = jsData;
  76. return;
  77. }
  78. }
  79. function showData() {
  80. Modal.info({ title: '编辑器当前值', content: value.value });
  81. }
  82. return { value, modeValue, handleModeChange, showData };
  83. },
  84. });
  85. </script>