@netang/quasar 0.2.21 → 0.2.22

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (284) hide show
  1. package/.editorconfig +12 -12
  2. package/_docs/docs/components/field-table.md +58 -58
  3. package/_docs/docs/components/field-tree.md +21 -21
  4. package/_docs/docs/components/table.md +24 -24
  5. package/_docs/docs/utils/table.md +196 -196
  6. package/components/column-title/index.vue +38 -38
  7. package/components/data/index.vue +20 -20
  8. package/components/dialog/img-viewer/index.vue +697 -697
  9. package/components/dragger/index.vue +203 -203
  10. package/components/drawer/index.vue +303 -303
  11. package/components/editor-code/index.vue +325 -325
  12. package/components/empty/index.vue +82 -82
  13. package/components/field-date/index.vue +850 -850
  14. package/components/field-date/methods.js +100 -100
  15. package/components/field-table/index.vue +1482 -1482
  16. package/components/field-text/index.vue +165 -165
  17. package/components/field-tree/index.vue +754 -754
  18. package/components/img/index.vue +279 -279
  19. package/components/list-menu/index.vue +149 -149
  20. package/components/list-menu-item/index.vue +79 -79
  21. package/components/mixed-table/index.vue +532 -532
  22. package/components/mixed-table-splitter/index.vue +377 -377
  23. package/components/power-page/index.vue +96 -96
  24. package/components/price/index.vue +188 -188
  25. package/components/private/components/index.js +11 -11
  26. package/components/private/components/move-to-tree/index.vue +154 -154
  27. package/components/private/edit-power-data/index.vue +846 -846
  28. package/components/private/table-visible-columns-button/index.vue +114 -114
  29. package/components/render/index.vue +123 -123
  30. package/components/search/index.vue +231 -231
  31. package/components/search-item/index.vue +210 -210
  32. package/components/select/index.vue +177 -177
  33. package/components/splitter/index.vue +422 -422
  34. package/components/table/index.vue +513 -513
  35. package/components/table-column-fixed/index.vue +110 -110
  36. package/components/table-pagination/index.vue +192 -192
  37. package/components/table-summary/index.vue +107 -107
  38. package/components/thumbnail/index.vue +72 -72
  39. package/components/toolbar/index.vue +146 -146
  40. package/components/tree/index.vue +1728 -1728
  41. package/components/uploader/index.vue +196 -195
  42. package/components/uploader-query/index.vue +945 -945
  43. package/components/value-format/index.vue +274 -274
  44. package/configs/area3.js +1 -1
  45. package/docs/404.html +33 -33
  46. package/docs/assets/404.html-60b35caa.js +1 -1
  47. package/docs/assets/404.html-d1e63d77.js +1 -1
  48. package/docs/assets/alert.html-b2a2a72f.js +5 -5
  49. package/docs/assets/alert.html-ba46d137.js +1 -1
  50. package/docs/assets/app-9f30aa4b.js +6 -6
  51. package/docs/assets/area.html-01b9b58d.js +42 -42
  52. package/docs/assets/area.html-9a4fce6a.js +1 -1
  53. package/docs/assets/arr.html-145d27e7.js +1 -1
  54. package/docs/assets/arr.html-674e65ab.js +11 -11
  55. package/docs/assets/auth.html-579fa830.js +1 -1
  56. package/docs/assets/auth.html-8544ed95.js +8 -8
  57. package/docs/assets/bus.html-c71254aa.js +1 -1
  58. package/docs/assets/bus.html-dc7d3d19.js +6 -6
  59. package/docs/assets/column-title.html-c735cb5a.js +3 -3
  60. package/docs/assets/column-title.html-e9316762.js +1 -1
  61. package/docs/assets/confirm.html-ddfdc27f.js +10 -10
  62. package/docs/assets/confirm.html-ef3e2bef.js +1 -1
  63. package/docs/assets/copy.html-d20345b6.js +1 -1
  64. package/docs/assets/copy.html-ef8c8571.js +13 -13
  65. package/docs/assets/data.html-6432175d.js +30 -30
  66. package/docs/assets/data.html-a3b05d5b.js +1 -1
  67. package/docs/assets/dialog.html-1f698e5a.js +1 -1
  68. package/docs/assets/dialog.html-62902b83.js +68 -68
  69. package/docs/assets/dialog.html-baea77c9.js +1 -1
  70. package/docs/assets/dialog.html-bb082fc4.js +1 -1
  71. package/docs/assets/dict.html-1311da3d.js +23 -23
  72. package/docs/assets/dict.html-b96fbf0c.js +1 -1
  73. package/docs/assets/dictOptions.html-7c4f40a5.js +1 -1
  74. package/docs/assets/dictOptions.html-fb99d175.js +5 -5
  75. package/docs/assets/dragger.html-668d3efa.js +1 -1
  76. package/docs/assets/dragger.html-749d585a.js +1 -1
  77. package/docs/assets/editor-code.html-6ab26ea9.js +1 -1
  78. package/docs/assets/editor-code.html-d196205d.js +1 -1
  79. package/docs/assets/empty.html-1c139131.js +1 -1
  80. package/docs/assets/empty.html-1e9c441d.js +1 -1
  81. package/docs/assets/field-date.html-069fdb13.js +1 -1
  82. package/docs/assets/field-date.html-ad204aa9.js +1 -1
  83. package/docs/assets/field-table.html-ce480f03.js +1 -1
  84. package/docs/assets/field-table.html-d9236160.js +1 -1
  85. package/docs/assets/field-text.html-7277c62f.js +1 -1
  86. package/docs/assets/field-text.html-ccb4cecf.js +1 -1
  87. package/docs/assets/field-tree.html-519bfb45.js +1 -1
  88. package/docs/assets/field-tree.html-fdc748d6.js +1 -1
  89. package/docs/assets/form.html-2b562c37.js +2 -2
  90. package/docs/assets/form.html-75104cd5.js +1 -1
  91. package/docs/assets/framework-204010b2.js +5 -5
  92. package/docs/assets/getData.html-990e3787.js +1 -1
  93. package/docs/assets/getData.html-bb72025f.js +34 -34
  94. package/docs/assets/getFile.html-42368004.js +1 -1
  95. package/docs/assets/getFile.html-99abd054.js +3 -3
  96. package/docs/assets/getImage.html-3429c5a1.js +1 -1
  97. package/docs/assets/getImage.html-4d886d83.js +3 -3
  98. package/docs/assets/getTime.html-7435f922.js +1 -1
  99. package/docs/assets/getTime.html-b37f49eb.js +20 -20
  100. package/docs/assets/img.html-7d1da657.js +1 -1
  101. package/docs/assets/img.html-fbea1105.js +1 -1
  102. package/docs/assets/index.html-1695dd7c.js +1 -1
  103. package/docs/assets/index.html-65a4aa67.js +1 -1
  104. package/docs/assets/index.html-7b98d5bd.js +1 -1
  105. package/docs/assets/index.html-c01f2648.js +1 -1
  106. package/docs/assets/input-number.html-0b250d2a.js +1 -1
  107. package/docs/assets/input-number.html-a8eb0378.js +1 -1
  108. package/docs/assets/list-menu-item.html-7f1b4611.js +1 -1
  109. package/docs/assets/list-menu-item.html-84ed5ab8.js +1 -1
  110. package/docs/assets/list-menu.html-28b4163f.js +1 -1
  111. package/docs/assets/list-menu.html-cb6ba95b.js +1 -1
  112. package/docs/assets/loading.html-dae9e39d.js +6 -6
  113. package/docs/assets/loading.html-dc74c9e6.js +1 -1
  114. package/docs/assets/notify.html-e6c4c514.js +1 -1
  115. package/docs/assets/notify.html-f2c4d914.js +8 -8
  116. package/docs/assets/power-page.html-32e02f82.js +1 -1
  117. package/docs/assets/power-page.html-485e77da.js +1 -1
  118. package/docs/assets/power.html-d258cc19.js +93 -93
  119. package/docs/assets/power.html-e490bd32.js +1 -1
  120. package/docs/assets/previewImage.html-6a6b4245.js +1 -1
  121. package/docs/assets/previewImage.html-c5b7e945.js +2 -2
  122. package/docs/assets/price.html-1882c548.js +19 -19
  123. package/docs/assets/price.html-94d3f5be.js +1 -1
  124. package/docs/assets/price.html-d213df0f.js +1 -1
  125. package/docs/assets/price.html-deaf880f.js +1 -1
  126. package/docs/assets/render.html-8efcbdd4.js +1 -1
  127. package/docs/assets/render.html-df228e38.js +1 -1
  128. package/docs/assets/rule.html-2cd57fc2.js +13 -13
  129. package/docs/assets/rule.html-61662001.js +1 -1
  130. package/docs/assets/ruleValid.html-04fe2552.js +1 -1
  131. package/docs/assets/ruleValid.html-e0a776af.js +14 -14
  132. package/docs/assets/search-0782d0d1.svg +1 -1
  133. package/docs/assets/search-item.html-3f75394c.js +1 -1
  134. package/docs/assets/search-item.html-4e942ecd.js +1 -1
  135. package/docs/assets/search.html-2807043e.js +1 -1
  136. package/docs/assets/search.html-c24f8806.js +1 -1
  137. package/docs/assets/select.html-00d0607c.js +1 -1
  138. package/docs/assets/select.html-de7731f5.js +1 -1
  139. package/docs/assets/splitter.html-56f51a70.js +1 -1
  140. package/docs/assets/splitter.html-f5c836d7.js +1 -1
  141. package/docs/assets/style-161e43ab.css +1 -1
  142. package/docs/assets/symbols.html-a6aea4bf.js +1 -1
  143. package/docs/assets/symbols.html-b1f65bad.js +21 -21
  144. package/docs/assets/table-column-fixed.html-3a69e7b2.js +1 -1
  145. package/docs/assets/table-column-fixed.html-e763c38b.js +1 -1
  146. package/docs/assets/table-pagination.html-236934d3.js +1 -1
  147. package/docs/assets/table-pagination.html-c37ee2ac.js +1 -1
  148. package/docs/assets/table-splitter.html-07eab15c.js +1 -1
  149. package/docs/assets/table-splitter.html-7670ee65.js +1 -1
  150. package/docs/assets/table-summary.html-04db434f.js +1 -1
  151. package/docs/assets/table-summary.html-943c65a0.js +1 -1
  152. package/docs/assets/table.html-36253ad7.js +1 -1
  153. package/docs/assets/table.html-7f9c5d1b.js +38 -38
  154. package/docs/assets/table.html-93d53dc8.js +1 -1
  155. package/docs/assets/table.html-ac99b9cb.js +1 -1
  156. package/docs/assets/thumbnail.html-bab1976b.js +1 -1
  157. package/docs/assets/thumbnail.html-eb64e5e8.js +1 -1
  158. package/docs/assets/timestamp.html-4e54f79b.js +13 -13
  159. package/docs/assets/timestamp.html-d0e1b88a.js +1 -1
  160. package/docs/assets/toast.html-58ecbe21.js +1 -1
  161. package/docs/assets/toast.html-c9b9d36b.js +6 -6
  162. package/docs/assets/toolbar.html-83d9f97c.js +1 -1
  163. package/docs/assets/toolbar.html-ff7b8c92.js +1 -1
  164. package/docs/assets/tree.html-d07cbe79.js +23 -23
  165. package/docs/assets/tree.html-ea04193e.js +1 -1
  166. package/docs/assets/uploader-query.html-05590718.js +1 -1
  167. package/docs/assets/uploader-query.html-3175bac5.js +1 -1
  168. package/docs/assets/uploader.html-36da4394.js +2 -2
  169. package/docs/assets/uploader.html-6b5f3079.js +1 -1
  170. package/docs/assets/uploader.html-b9340b57.js +1 -1
  171. package/docs/assets/uploader.html-bc1c22e3.js +1 -1
  172. package/docs/assets/value-format.html-8ae3d47d.js +1 -1
  173. package/docs/assets/value-format.html-afa99b3d.js +1 -1
  174. package/docs/components/column-title.html +35 -35
  175. package/docs/components/data.html +62 -62
  176. package/docs/components/dialog.html +33 -33
  177. package/docs/components/dragger.html +33 -33
  178. package/docs/components/editor-code.html +33 -33
  179. package/docs/components/empty.html +33 -33
  180. package/docs/components/field-date.html +33 -33
  181. package/docs/components/field-table.html +33 -33
  182. package/docs/components/field-text.html +33 -33
  183. package/docs/components/field-tree.html +33 -33
  184. package/docs/components/img.html +33 -33
  185. package/docs/components/input-number.html +33 -33
  186. package/docs/components/list-menu-item.html +33 -33
  187. package/docs/components/list-menu.html +33 -33
  188. package/docs/components/power-page.html +33 -33
  189. package/docs/components/price.html +33 -33
  190. package/docs/components/render.html +33 -33
  191. package/docs/components/search-item.html +33 -33
  192. package/docs/components/search.html +33 -33
  193. package/docs/components/select.html +33 -33
  194. package/docs/components/splitter.html +33 -33
  195. package/docs/components/table-column-fixed.html +33 -33
  196. package/docs/components/table-pagination.html +33 -33
  197. package/docs/components/table-splitter.html +33 -33
  198. package/docs/components/table-summary.html +33 -33
  199. package/docs/components/table.html +33 -33
  200. package/docs/components/thumbnail.html +33 -33
  201. package/docs/components/toolbar.html +33 -33
  202. package/docs/components/uploader-query.html +33 -33
  203. package/docs/components/uploader.html +33 -33
  204. package/docs/components/value-format.html +33 -33
  205. package/docs/css/index.css +3 -3
  206. package/docs/index.html +33 -33
  207. package/docs/utils/alert.html +37 -37
  208. package/docs/utils/area.html +74 -74
  209. package/docs/utils/arr.html +43 -43
  210. package/docs/utils/auth.html +40 -40
  211. package/docs/utils/bus.html +38 -38
  212. package/docs/utils/confirm.html +42 -42
  213. package/docs/utils/copy.html +45 -45
  214. package/docs/utils/dialog.html +100 -100
  215. package/docs/utils/dict.html +55 -55
  216. package/docs/utils/dictOptions.html +37 -37
  217. package/docs/utils/form.html +34 -34
  218. package/docs/utils/getData.html +66 -66
  219. package/docs/utils/getFile.html +35 -35
  220. package/docs/utils/getImage.html +35 -35
  221. package/docs/utils/getTime.html +52 -52
  222. package/docs/utils/index.html +33 -33
  223. package/docs/utils/loading.html +38 -38
  224. package/docs/utils/notify.html +40 -40
  225. package/docs/utils/power.html +125 -125
  226. package/docs/utils/previewImage.html +34 -34
  227. package/docs/utils/price.html +51 -51
  228. package/docs/utils/rule.html +45 -45
  229. package/docs/utils/ruleValid.html +46 -46
  230. package/docs/utils/symbols.html +53 -53
  231. package/docs/utils/table.html +70 -70
  232. package/docs/utils/timestamp.html +45 -45
  233. package/docs/utils/toast.html +38 -38
  234. package/docs/utils/tree.html +55 -55
  235. package/docs/utils/uploader.html +34 -34
  236. package/package.json +25 -25
  237. package/sass/common.scss +184 -184
  238. package/sass/index.scss +12 -12
  239. package/sass/line.scss +39 -39
  240. package/sass/quasar/btn.scss +46 -46
  241. package/sass/quasar/common.scss +3 -3
  242. package/sass/quasar/drawer.scss +6 -6
  243. package/sass/quasar/field.scss +259 -259
  244. package/sass/quasar/loading.scss +6 -6
  245. package/sass/quasar/table.scss +168 -168
  246. package/sass/quasar/toolbar.scss +22 -22
  247. package/sass/variables.scss +140 -140
  248. package/store/index.js +29 -29
  249. package/utils/$auth.js +127 -127
  250. package/utils/$form.js +72 -72
  251. package/utils/$power.js +1486 -1486
  252. package/utils/$render.js +75 -75
  253. package/utils/$rule.js +13 -13
  254. package/utils/$ruleValid.js +10 -10
  255. package/utils/$search.js +416 -416
  256. package/utils/$table.js +1275 -1275
  257. package/utils/alert.js +12 -12
  258. package/utils/area.js +400 -400
  259. package/utils/arr.js +51 -51
  260. package/utils/bus.js +6 -6
  261. package/utils/config.js +64 -64
  262. package/utils/confirm.js +11 -11
  263. package/utils/copy.js +30 -30
  264. package/utils/dialog.js +36 -36
  265. package/utils/dict.js +21 -21
  266. package/utils/dictOptions.js +28 -28
  267. package/utils/getData.js +88 -88
  268. package/utils/getFile.js +67 -67
  269. package/utils/getImage.js +276 -276
  270. package/utils/getTime.js +113 -113
  271. package/utils/index.js +67 -67
  272. package/utils/loading.js +15 -15
  273. package/utils/notify.js +13 -13
  274. package/utils/play.js +40 -40
  275. package/utils/previewImage.js +14 -14
  276. package/utils/price.js +18 -18
  277. package/utils/symbols.js +18 -18
  278. package/utils/timestamp.js +18 -18
  279. package/utils/toast.js +13 -13
  280. package/utils/uploader.js +2114 -2114
  281. package/utils/useAuth.js +30 -30
  282. package/utils/useFileUrl.js +26 -26
  283. package/utils/useRouter.js +47 -47
  284. package/utils/useSearch.js +499 -499
@@ -1,325 +1,325 @@
1
- <template>
2
- <!-- :style="currentStyle" -->
3
- <div
4
- ref="targetRef"
5
- :style="currentStyle"
6
- ></div>
7
- </template>
8
-
9
- <script>
10
- import { ref, watch, computed, onMounted } from 'vue'
11
-
12
- import script from '@netang/utils/script'
13
- import $n_isNumeric from '@netang/utils/isNumeric'
14
- import $n_isValidValue from '@netang/utils/isValidValue'
15
- import $n_sleep from '@netang/utils/sleep'
16
- import $n_px from '@netang/utils/px'
17
-
18
- import $n_toast from '../../utils/toast'
19
-
20
- export default {
21
-
22
- /**
23
- * 标识
24
- */
25
- name: 'NEditorCode',
26
-
27
- /**
28
- * 声明属性
29
- */
30
- props: {
31
- // 值 v-model
32
- modelValue: {
33
- type: [ String, Number ],
34
- default: '',
35
- },
36
- // 原始值
37
- originalValue: [ String, Number ],
38
- // 宽度
39
- width: {
40
- type: [ String, Number ],
41
- default: '100%',
42
- },
43
- // 高度
44
- height: [ String, Number ],
45
- // 脚本语言
46
- language: String,
47
- // 是否只读
48
- readonly: Boolean,
49
- // 是否显示代码视图
50
- minimap: Boolean,
51
- // tab 长度
52
- tabSize: {
53
- type: Number,
54
- default: 4,
55
- },
56
- // 主题
57
- theme: {
58
- type: String,
59
- default: 'vs',
60
- },
61
- // 是否开启差异代码
62
- diff: Boolean,
63
- },
64
-
65
- /**
66
- * 声明事件
67
- */
68
- emits: [
69
- 'update:modelValue',
70
- ],
71
-
72
- /**
73
- * 组合式
74
- */
75
- setup(props, { emit }) {
76
-
77
- // ==========【计算属性】=========================================================================================
78
-
79
- /**
80
- * 当前样式
81
- */
82
- const currentStyle = computed(function () {
83
- return {
84
- width: $n_isNumeric(props.width) ? $n_px(props.width) : props.width,
85
- height: $n_isNumeric(props.height) ? $n_px(props.height) : props.height,
86
- }
87
- })
88
-
89
- // ==========【数据】=============================================================================================
90
-
91
- // 根节点
92
- const targetRef = ref(null)
93
-
94
- // 编辑器实例
95
- let $editor = null
96
-
97
- // 停止值观察
98
- let stopValueWatcher = false
99
-
100
- // 创建睡眠实例
101
- const sleep = $n_sleep()
102
-
103
- // ==========【监听数据】=========================================================================================
104
-
105
- /**
106
- * 监听声明值
107
- */
108
- if (! props.diff) {
109
- watch(() => props.modelValue, function (val) {
110
-
111
- // 如果停止值观察
112
- if (stopValueWatcher) {
113
-
114
- // 则无任何操作
115
- return
116
- }
117
-
118
- // 取消停止值观察
119
- stopValueWatcher = false
120
-
121
- // 设置值
122
- setValue(val)
123
- })
124
- }
125
-
126
- // ==========【方法】============================================================================================
127
-
128
- function load() {
129
-
130
- // 如果已加载
131
- if (window.monaco) {
132
- // 创建编辑器
133
- create()
134
- return
135
- }
136
-
137
- // 版本
138
- const version = '0.34.1'
139
-
140
- // 加载 script
141
- script([
142
- [
143
- `https://cdn.staticfile.org/monaco-editor/${version}/min/vs/loader.js`,
144
- `https://fastly.jsdelivr.net/npm/monaco-editor@${version}/min/vs/loader.js`,
145
- `https://unpkg.com/monaco-editor@${version}/min/vs/loader.js`,
146
- ]
147
- ])
148
- .then(function ([ url ]) {
149
-
150
- // 按需加载
151
- const _require = window.require
152
-
153
- // 配置
154
- _require.config({
155
- // 路径
156
- paths: {
157
- vs: url.replace('/loader.js', ''),
158
- },
159
- // 语言
160
- 'vs/nls' : {
161
- availableLanguages: {
162
- '*': 'zh-cn',
163
- }
164
- }
165
- })
166
-
167
- // 加载
168
- _require(['vs/editor/editor.main'], function () {
169
- // 创建编辑器
170
- create()
171
- })
172
- })
173
- }
174
-
175
- /**
176
- * 创建编辑器
177
- */
178
- function create() {
179
-
180
- // 创建编辑器
181
- $editor = monaco.editor[props.diff ? 'createDiffEditor' : 'create'](targetRef.value, {
182
- // 主题
183
- theme: props.theme,
184
- // 脚本语言
185
- language: props.language,
186
- // 自动布局
187
- automaticLayout: true,
188
- // 自动换行
189
- wordWrap: 'on',
190
- // tab 长度
191
- tabSize: props.tabSize,
192
- // 代码略缩图
193
- minimap: {
194
- enabled: props.minimap,
195
- },
196
- // 右键
197
- contextmenu: true,
198
- // 是否只读
199
- readOnly: props.readonly,
200
- })
201
-
202
- // 监听失去焦点事件
203
- if (! props.diff) {
204
- $editor.onDidBlurEditorText(function () {
205
-
206
- // 获取编辑器内容
207
- const value = getValue()
208
- if (value === void 0) {
209
- return
210
- }
211
-
212
- // 停止值观察
213
- stopValueWatcher = true
214
-
215
- // 触发更新值
216
- emit('update:modelValue', value)
217
- })
218
- }
219
-
220
- // 等编辑器全部加载完成后, 设置内容值
221
- sleep(300)
222
- .then(function () {
223
- // 设置值
224
- setValue(props.modelValue)
225
- })
226
- }
227
-
228
- /**
229
- * 设置值
230
- */
231
- function setValue(value) {
232
- if ($editor) {
233
-
234
- // 如果是比较差异代码
235
- if (props.diff) {
236
- $editor.setModel({
237
- original: monaco.editor.createModel(formatValue(props.originalValue), props.language),
238
- modified: monaco.editor.createModel(formatValue(value), props.language),
239
- })
240
-
241
- // 否则是编辑代码
242
- } else {
243
- // 编辑器设置内容
244
- $editor.setValue(formatValue(value))
245
- }
246
-
247
- // 代码格式化
248
- // $editor.getAction('editor.action.formatDocument').run()
249
- }
250
- }
251
-
252
- /**
253
- * 获取值
254
- */
255
- function getValue() {
256
-
257
- if ($editor) {
258
-
259
- // 获取编辑器的值
260
- let value = $editor.getValue()
261
-
262
- // 如果语言为 json
263
- if (
264
- props.language === 'json'
265
- && $n_isValidValue(value)
266
- ) {
267
- try {
268
- value = JSON.stringify(JSON.parse(value))
269
-
270
- } catch (e) {
271
- // 轻提示
272
- $n_toast({
273
- message: 'JSON 语法错误'
274
- })
275
- return
276
- }
277
- }
278
-
279
- return value
280
- }
281
-
282
- return ''
283
- }
284
-
285
- /**
286
- * 格式化值
287
- */
288
- function formatValue(value) {
289
- try {
290
- if (value) {
291
- if (
292
- props.language === 'json'
293
- && $n_isValidValue(value)
294
- ) {
295
- return JSON.stringify(JSON.parse(value), null, 2)
296
- }
297
- return value
298
- }
299
- } catch (e) {}
300
-
301
- return ''
302
- }
303
-
304
- // ==========【生命周期】=========================================================================================
305
-
306
- /**
307
- * 实例被挂载后调用
308
- */
309
- onMounted(function() {
310
-
311
- // 加载
312
- load()
313
- })
314
-
315
- // ==========【返回】=========================================================================================
316
-
317
- return {
318
- // 根节点
319
- targetRef,
320
- // 当前样式
321
- currentStyle,
322
- }
323
- }
324
- }
325
- </script>
1
+ <template>
2
+ <!-- :style="currentStyle" -->
3
+ <div
4
+ ref="targetRef"
5
+ :style="currentStyle"
6
+ ></div>
7
+ </template>
8
+
9
+ <script>
10
+ import { ref, watch, computed, onMounted } from 'vue'
11
+
12
+ import script from '@netang/utils/script'
13
+ import $n_isNumeric from '@netang/utils/isNumeric'
14
+ import $n_isValidValue from '@netang/utils/isValidValue'
15
+ import $n_sleep from '@netang/utils/sleep'
16
+ import $n_px from '@netang/utils/px'
17
+
18
+ import $n_toast from '../../utils/toast'
19
+
20
+ export default {
21
+
22
+ /**
23
+ * 标识
24
+ */
25
+ name: 'NEditorCode',
26
+
27
+ /**
28
+ * 声明属性
29
+ */
30
+ props: {
31
+ // 值 v-model
32
+ modelValue: {
33
+ type: [ String, Number ],
34
+ default: '',
35
+ },
36
+ // 原始值
37
+ originalValue: [ String, Number ],
38
+ // 宽度
39
+ width: {
40
+ type: [ String, Number ],
41
+ default: '100%',
42
+ },
43
+ // 高度
44
+ height: [ String, Number ],
45
+ // 脚本语言
46
+ language: String,
47
+ // 是否只读
48
+ readonly: Boolean,
49
+ // 是否显示代码视图
50
+ minimap: Boolean,
51
+ // tab 长度
52
+ tabSize: {
53
+ type: Number,
54
+ default: 4,
55
+ },
56
+ // 主题
57
+ theme: {
58
+ type: String,
59
+ default: 'vs',
60
+ },
61
+ // 是否开启差异代码
62
+ diff: Boolean,
63
+ },
64
+
65
+ /**
66
+ * 声明事件
67
+ */
68
+ emits: [
69
+ 'update:modelValue',
70
+ ],
71
+
72
+ /**
73
+ * 组合式
74
+ */
75
+ setup(props, { emit }) {
76
+
77
+ // ==========【计算属性】=========================================================================================
78
+
79
+ /**
80
+ * 当前样式
81
+ */
82
+ const currentStyle = computed(function () {
83
+ return {
84
+ width: $n_isNumeric(props.width) ? $n_px(props.width) : props.width,
85
+ height: $n_isNumeric(props.height) ? $n_px(props.height) : props.height,
86
+ }
87
+ })
88
+
89
+ // ==========【数据】=============================================================================================
90
+
91
+ // 根节点
92
+ const targetRef = ref(null)
93
+
94
+ // 编辑器实例
95
+ let $editor = null
96
+
97
+ // 停止值观察
98
+ let stopValueWatcher = false
99
+
100
+ // 创建睡眠实例
101
+ const sleep = $n_sleep()
102
+
103
+ // ==========【监听数据】=========================================================================================
104
+
105
+ /**
106
+ * 监听声明值
107
+ */
108
+ if (! props.diff) {
109
+ watch(() => props.modelValue, function (val) {
110
+
111
+ // 如果停止值观察
112
+ if (stopValueWatcher) {
113
+
114
+ // 则无任何操作
115
+ return
116
+ }
117
+
118
+ // 取消停止值观察
119
+ stopValueWatcher = false
120
+
121
+ // 设置值
122
+ setValue(val)
123
+ })
124
+ }
125
+
126
+ // ==========【方法】============================================================================================
127
+
128
+ function load() {
129
+
130
+ // 如果已加载
131
+ if (window.monaco) {
132
+ // 创建编辑器
133
+ create()
134
+ return
135
+ }
136
+
137
+ // 版本
138
+ const version = '0.34.1'
139
+
140
+ // 加载 script
141
+ script([
142
+ [
143
+ `https://cdn.staticfile.org/monaco-editor/${version}/min/vs/loader.js`,
144
+ `https://fastly.jsdelivr.net/npm/monaco-editor@${version}/min/vs/loader.js`,
145
+ `https://unpkg.com/monaco-editor@${version}/min/vs/loader.js`,
146
+ ]
147
+ ])
148
+ .then(function ([ url ]) {
149
+
150
+ // 按需加载
151
+ const _require = window.require
152
+
153
+ // 配置
154
+ _require.config({
155
+ // 路径
156
+ paths: {
157
+ vs: url.replace('/loader.js', ''),
158
+ },
159
+ // 语言
160
+ 'vs/nls' : {
161
+ availableLanguages: {
162
+ '*': 'zh-cn',
163
+ }
164
+ }
165
+ })
166
+
167
+ // 加载
168
+ _require(['vs/editor/editor.main'], function () {
169
+ // 创建编辑器
170
+ create()
171
+ })
172
+ })
173
+ }
174
+
175
+ /**
176
+ * 创建编辑器
177
+ */
178
+ function create() {
179
+
180
+ // 创建编辑器
181
+ $editor = monaco.editor[props.diff ? 'createDiffEditor' : 'create'](targetRef.value, {
182
+ // 主题
183
+ theme: props.theme,
184
+ // 脚本语言
185
+ language: props.language,
186
+ // 自动布局
187
+ automaticLayout: true,
188
+ // 自动换行
189
+ wordWrap: 'on',
190
+ // tab 长度
191
+ tabSize: props.tabSize,
192
+ // 代码略缩图
193
+ minimap: {
194
+ enabled: props.minimap,
195
+ },
196
+ // 右键
197
+ contextmenu: true,
198
+ // 是否只读
199
+ readOnly: props.readonly,
200
+ })
201
+
202
+ // 监听失去焦点事件
203
+ if (! props.diff) {
204
+ $editor.onDidBlurEditorText(function () {
205
+
206
+ // 获取编辑器内容
207
+ const value = getValue()
208
+ if (value === void 0) {
209
+ return
210
+ }
211
+
212
+ // 停止值观察
213
+ stopValueWatcher = true
214
+
215
+ // 触发更新值
216
+ emit('update:modelValue', value)
217
+ })
218
+ }
219
+
220
+ // 等编辑器全部加载完成后, 设置内容值
221
+ sleep(300)
222
+ .then(function () {
223
+ // 设置值
224
+ setValue(props.modelValue)
225
+ })
226
+ }
227
+
228
+ /**
229
+ * 设置值
230
+ */
231
+ function setValue(value) {
232
+ if ($editor) {
233
+
234
+ // 如果是比较差异代码
235
+ if (props.diff) {
236
+ $editor.setModel({
237
+ original: monaco.editor.createModel(formatValue(props.originalValue), props.language),
238
+ modified: monaco.editor.createModel(formatValue(value), props.language),
239
+ })
240
+
241
+ // 否则是编辑代码
242
+ } else {
243
+ // 编辑器设置内容
244
+ $editor.setValue(formatValue(value))
245
+ }
246
+
247
+ // 代码格式化
248
+ // $editor.getAction('editor.action.formatDocument').run()
249
+ }
250
+ }
251
+
252
+ /**
253
+ * 获取值
254
+ */
255
+ function getValue() {
256
+
257
+ if ($editor) {
258
+
259
+ // 获取编辑器的值
260
+ let value = $editor.getValue()
261
+
262
+ // 如果语言为 json
263
+ if (
264
+ props.language === 'json'
265
+ && $n_isValidValue(value)
266
+ ) {
267
+ try {
268
+ value = JSON.stringify(JSON.parse(value))
269
+
270
+ } catch (e) {
271
+ // 轻提示
272
+ $n_toast({
273
+ message: 'JSON 语法错误'
274
+ })
275
+ return
276
+ }
277
+ }
278
+
279
+ return value
280
+ }
281
+
282
+ return ''
283
+ }
284
+
285
+ /**
286
+ * 格式化值
287
+ */
288
+ function formatValue(value) {
289
+ try {
290
+ if (value) {
291
+ if (
292
+ props.language === 'json'
293
+ && $n_isValidValue(value)
294
+ ) {
295
+ return JSON.stringify(JSON.parse(value), null, 2)
296
+ }
297
+ return value
298
+ }
299
+ } catch (e) {}
300
+
301
+ return ''
302
+ }
303
+
304
+ // ==========【生命周期】=========================================================================================
305
+
306
+ /**
307
+ * 实例被挂载后调用
308
+ */
309
+ onMounted(function() {
310
+
311
+ // 加载
312
+ load()
313
+ })
314
+
315
+ // ==========【返回】=========================================================================================
316
+
317
+ return {
318
+ // 根节点
319
+ targetRef,
320
+ // 当前样式
321
+ currentStyle,
322
+ }
323
+ }
324
+ }
325
+ </script>