index.vue 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <template>
  2. <PageWrapper title="代码编辑器组件示例" contentFullHeight fixedHeight contentBackground>
  3. <template #extra>
  4. <RadioGroup button-style="solid" v-model:value="modeValue" @change="handleModeChange">
  5. <RadioButton value="application/json"> json数据 </RadioButton>
  6. <RadioButton value="htmlmixed"> html代码 </RadioButton>
  7. <RadioButton value="javascript"> javascript代码 </RadioButton>
  8. </RadioGroup>
  9. </template>
  10. <CodeEditor v-model:value="value" :mode="modeValue" />
  11. </PageWrapper>
  12. </template>
  13. <script lang="ts">
  14. import { defineComponent, ref } from 'vue';
  15. import { CodeEditor } from '/@/components/CodeEditor';
  16. import { PageWrapper } from '/@/components/Page';
  17. import { Radio } from 'ant-design-vue';
  18. const jsonData =
  19. '{"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"}]}';
  20. const jsData = `
  21. (() => {
  22. var htmlRoot = document.getElementById('htmlRoot');
  23. var theme = window.localStorage.getItem('__APP__DARK__MODE__');
  24. if (htmlRoot && theme) {
  25. htmlRoot.setAttribute('data-theme', theme);
  26. theme = htmlRoot = null;
  27. }
  28. })();
  29. `;
  30. const htmlData = `
  31. <!DOCTYPE html>
  32. <html lang="en" id="htmlRoot">
  33. <head>
  34. <meta charset="UTF-8" />
  35. <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
  36. <meta name="renderer" content="webkit" />
  37. <meta
  38. name="viewport"
  39. content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,user-scalable=0"
  40. />
  41. <title><%= title %></title>
  42. <link rel="icon" href="/favicon.ico" />
  43. </head>
  44. <body>
  45. <div id="app">
  46. </div>
  47. </body>
  48. </html>
  49. `;
  50. export default defineComponent({
  51. components: { CodeEditor, PageWrapper, RadioButton: Radio.Button, RadioGroup: Radio.Group },
  52. setup() {
  53. const modeValue = ref('application/json');
  54. const value = ref(jsonData);
  55. function handleModeChange(e: ChangeEvent) {
  56. const mode = e.target.value;
  57. if (mode === 'application/json') {
  58. value.value = jsonData;
  59. return;
  60. }
  61. if (mode === 'htmlmixed') {
  62. value.value = htmlData;
  63. return;
  64. }
  65. if (mode === 'javascript') {
  66. value.value = jsData;
  67. return;
  68. }
  69. }
  70. return { value, modeValue, handleModeChange };
  71. },
  72. });
  73. </script>