应用商店

    svelte 包含一些额外的组件,从里面我们导入一些动画和辅助方法(数组推入,删除、补间动画等等)。

    1. import { ipcRenderer as ipc, shell } from 'electron'
    2. import marked from 'marked'
    3. import { fade } from 'svelte-transitions'
    4. import { push, splice } from 'svelte-extras'

    添加 helper

    destroy 的时候一定要移除监听,要不然会造成内存泄露。

    1. export default {
    2. data() {
    3. return {
    4. local_plugins: [], // 本地插件
    5. all_plugins: [], // 所有插件
    6. infoIndex: -1, // 展开详情
    7. workingFilename: [] // 操作中文件
    8. }
    9. },
    10. components: {
    11. Back: '../components/Back.svelte'
    12. },
    13. helpers: {
    14. marked, // markdown 处理
    15. inWorking // 是否处理中
    16. },
    17. transitions: {
    18. fade
    19. },
    20. computed: {
    21. plugins: ({ all_plugins, local_plugins }) =>
    22. addDownloaed(all_plugins, local_plugins)
    23. },
    24. methods: {
    25. push,
    26. splice,
    27. showInfo(index) {
    28. // 显示某一个插件介绍
    29. const oldIndex = this.get().infoIndex
    30. if (oldIndex == index) {
    31. return this.set({
    32. infoIndex: -1
    33. })
    34. }
    35. this.set({
    36. infoIndex: index
    37. })
    38. },
    39. refresh() {
    40. // 刷新
    41. ipc.send('reload_local_plugins')
    42. ipc.send('reload_all_plugins')
    43. },
    44. download(filename, url) {
    45. // 下载插件
    46. if (inWorking(this.get().workingFilename, filename)) {
    47. return
    48. }
    49. this.push('workingFilename', filename)
    50. filename,
    51. url
    52. })
    53. },
    54. del(filename) {
    55. // 删除插件
    56. if (inWorking(this.get().workingFilename, filename)) {
    57. return
    58. }
    59. this.push('workingFilename', filename)
    60. this.store.setMsg('info', `删除插件中`)
    61. ipc.send('delete_plugin', {
    62. filename
    63. })
    64. },
    65. all_plugins(event, all_plugins) {
    66. this.set({
    67. all_plugins
    68. })
    69. },
    70. local_plugins(event, local_plugins) {
    71. this.set({
    72. local_plugins
    73. })
    74. },
    75. download_plugin_success(event, filename) {
    76. // 下载成功
    77. const filenames = this.get().workingFilename || []
    78. let index = filenames.indexOf(filename)
    79. if (index >= 0) {
    80. this.splice('workingFilename', index, 1)
    81. }
    82. if (this.get().workingFilename == 0) this.store.success('下载完成')
    83. },
    84. delete_plugin_success(event, filename) {
    85. // 删除成功
    86. const filenames = this.get().workingFilename || []
    87. let index = filenames.indexOf(filename)
    88. if (index >= 0) {
    89. this.splice('workingFilename', index, 1)
    90. }
    91. if (this.get().workingFilename == 0) this.store.success('删除完成')
    92. }
    93. },
    94. ondestroy() {
    95. // 清理监听
    96. ipc.removeListener('all_plugins', this.all_plugins.bind(this))
    97. ipc.removeListener('local_plugins', this.local_plugins.bind(this))
    98. ipc.removeListener(
    99. 'download_plugin_success',
    100. this.download_plugin_success.bind(this)
    101. )
    102. ipc.removeListener(
    103. 'delete_plugin_success',
    104. this.delete_plugin_success.bind(this)
    105. )
    106. },
    107. oncreate() {
    108. ipc.send('get_all_plugins')
    109. ipc.send('get_local_plugins')
    110. ipc.on('all_plugins', this.all_plugins.bind(this))
    111. ipc.on('local_plugins', this.local_plugins.bind(this))
    112. ipc.on('download_plugin_success', this.download_plugin_success.bind(this))
    113. ipc.on('delete_plugin_success', this.delete_plugin_success.bind(this))
    114. this.refs.list.addEventListener(
    115. // 处理点击事件,外部浏览器打开
    116. 'click',
    117. e => {
    118. if (e.target.tagName.toLowerCase() == 'a') {
    119. e.preventDefault()
    120. shell.openExternal(e.target.href)
    121. }
    122. },
    123. true
    124. )
    125. }
    126. }

    样式

    1. <div class="wrap">
    2. <Link className="download-page-btn" to="Download">下载页面</Link>
    3. <Link className="music-page-btn" to="Music">播放器</Link>
    4. <Link className="store-page-btn" to="CrawlStore">爬虫商店</Link>
    5. </div>
    6. <script>
    7. export default {
    8. components: {
    9. Link: '../components/Link.svelte'
    10. },
    11. };
    12. </script>
    13. <style>
    14. .wrap {
    15. display: flex;
    16. height: 100vh;
    17. flex-wrap: wrap;
    18. background: #333;
    19. }
    20. .wrap :global(a) {
    21. display: inline-flex;
    22. width: 50%;
    23. text-decoration: none;
    24. justify-content: center;
    25. align-items: center;
    26. color: #fff;
    27. font-size: 1rem;
    28. letter-spacing: 5px;
    29. transition: all .2s;
    30. }
    31. .wrap :global(a):hover {
    32. background: #fb584c
    33. </style>

    修改窗口创建参数

    创建一个无边框的应用。