合同小程序前端代码仓库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

110 lines
2.3 KiB

5 months ago
  1. // @ts-nocheck
  2. import {IconCollection} from './components/l-icon/types'
  3. // #ifndef UNI-APP-X
  4. // #ifdef VUE3
  5. import { reactive } from 'vue';
  6. function definePlugin(options: any) {
  7. return options
  8. }
  9. // #endif
  10. // #ifdef VUE2
  11. import Vue from 'vue'
  12. let reactive = Vue.observable
  13. type VueApp = any
  14. // #endif
  15. type UTSJSONObject = any//Record<string, any>
  16. // #endif
  17. let topApp : VueApp | null = null;
  18. const _iconCollection = reactive<IconCollection>({
  19. has: true,
  20. // #ifdef UNI-APP-X
  21. icons: new Map<string, any|null>()
  22. // #endif
  23. // #ifndef UNI-APP-X
  24. icons: {}
  25. // #endif
  26. })
  27. export function useIconHost(iconHost : string) {
  28. // #ifdef UNI-APP-X
  29. uni.setStorageSync('$limeIconsHost', iconHost)
  30. // #endif
  31. // #ifndef UNI-APP-X
  32. uni.$limeIconsHost = iconHost
  33. // #endif
  34. }
  35. let isInstall = false
  36. export function useIconCollection(iconCollection: UTSJSONObject|null = {}) {
  37. if(!isInstall) {
  38. console.warn('[lime-icon]: useIconCollection 请先注册,app.use(limeIcons, null, iconjson)')
  39. return
  40. }
  41. // #ifdef UNI-APP-X
  42. const map = (iconCollection as UTSJSONObject).toMap()
  43. if(map.size != 0) {
  44. uni.setStorageSync('$limeIconCollection', iconCollection)
  45. _iconCollection.icons = map
  46. }
  47. // #endif
  48. // #ifndef UNI-APP-X
  49. if(Object.keys(iconCollection).length != 0) {
  50. uni.setStorageSync('$limeIconCollection', iconCollection)
  51. _iconCollection.icons = iconCollection
  52. }
  53. // #endif
  54. }
  55. function useProvide() {
  56. if(topApp == null) return
  57. isInstall = true
  58. // #ifdef VUE3
  59. topApp!.provide('$iconCollection', _iconCollection)
  60. // #endif
  61. // #ifndef VUE3
  62. topApp.mixin({
  63. provide: {
  64. $iconCollection: _iconCollection
  65. }
  66. })
  67. // #endif
  68. }
  69. // #ifdef VUE3
  70. export const limeIcons = definePlugin({
  71. install: (app: VueApp, iconHost: string | null, iconCollection: UTSJSONObject | null):void => {
  72. topApp = app;
  73. if(iconHost != null || iconHost != '') {
  74. useIconHost(iconHost!)
  75. }
  76. if(iconCollection != null) {
  77. useProvide()
  78. useIconCollection(iconCollection)
  79. }
  80. }
  81. })
  82. // #endif
  83. // #ifdef VUE2
  84. export const limeIcons = {
  85. install: (app: any, options: any[]) => {
  86. topApp = app;
  87. let [iconHost, iconCollection] = options
  88. if(iconHost != null && typeof iconHost == 'object') {
  89. iconCollection = iconHost
  90. }
  91. if(iconHost && iconHost != '') {
  92. useIconHost(iconHost!)
  93. }
  94. if(iconCollection != null) {
  95. useProvide()
  96. useIconCollection(iconCollection)
  97. }
  98. }
  99. }
  100. // #endif