openHuiKaiFa.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * @Author: wjc
  3. * @Date: 2021-12-14 15:05:05
  4. * @LastEditors: wjc
  5. <<<<<<< HEAD
  6. <<<<<<< Updated upstream
  7. * @LastEditTime: 2021-12-22 10:03:46
  8. =======
  9. * @LastEditTime: 2021-12-24 18:03:19
  10. >>>>>>> Stashed changes
  11. =======
  12. * @LastEditTime: 2021-12-24 17:48:19
  13. >>>>>>> ae406df4bde6c8006525ab8feb4e3212556af665
  14. * @Description:
  15. */
  16. /* eslint-disable */
  17. const vscode = require('vscode')
  18. const fs = require('fs')
  19. const path = require('path')
  20. const open = require('open')
  21. const DB_PATH = path.join(__dirname, './plugin/dbData/db.json')
  22. function getExtensionFileAbsolutePath(context, relativePath) {
  23. return path.join(context.extensionPath, relativePath)
  24. }
  25. /**
  26. * 从某个HTML文件读取能被Webview加载的HTML内容
  27. * @param {*} context 上下文
  28. * @param {*} templatePath 相对于插件根目录的html文件相对路径
  29. */
  30. function getWebViewContent(context, templatePath) {
  31. const resourcePath = getExtensionFileAbsolutePath(context, templatePath)
  32. const dirPath = path.dirname(resourcePath)
  33. let html = fs.readFileSync(resourcePath, 'utf-8')
  34. // vscode不支持直接加载本地资源,需要替换成其专有路径格式,这里只是简单的将样式和JS的路径替换
  35. html = html.replace(/(<link.+?href="|<script.+?src="|<img.+?src=")(.+?)"/g, (m, $1, $2) => {
  36. return $1 + vscode.Uri.file(path.resolve(dirPath, $2)).with({ scheme: 'vscode-resource' }).toString() + '"'
  37. })
  38. return html
  39. }
  40. const methods = {
  41. writeFile(message, vscode, dirPath) {
  42. const { fileName, code } = message.data
  43. const filePath = path.join(dirPath, fileName)
  44. fs.writeFileSync(filePath, code)
  45. vscode.window.showInformationMessage(`文件${fileName}创建成功`)
  46. },
  47. openUrl(message, vscode, dirPath) {
  48. open(message.data.url)
  49. },
  50. setStorageItem(message, vscode, dirPath) {
  51. const { key, val } = message.data
  52. const str = fs.readFileSync(DB_PATH).toString()
  53. let json = {}
  54. if (str) {
  55. json = JSON.parse(str)
  56. }
  57. json[key] = val
  58. fs.writeFileSync(DB_PATH, JSON.stringify(json))
  59. }
  60. }
  61. module.exports = function (context) {
  62. context.subscriptions.push(vscode.commands.registerCommand('extension.openHuiKaiFa', (uri) => {
  63. if (uri) {
  64. console.log('--url--', uri)
  65. let dirPath = uri.fsPath,
  66. stat = fs.lstatSync(dirPath)
  67. if (stat.isFile()) dirPath = path.dirname(dirPath)
  68. let pclintBar = vscode.window.createStatusBarItem()
  69. pclintBar.text = `目标文件夹:${dirPath}`
  70. pclintBar.show()
  71. const panel = vscode.window.createWebviewPanel(
  72. 'huikaifa',
  73. '绘开发',
  74. vscode.ViewColumn.One,
  75. {
  76. enableScripts: true, // 启用JS,默认禁用
  77. retainContextWhenHidden: true // webview被隐藏时保持状态,避免被重置
  78. }
  79. )
  80. panel.onDidChangeViewState(e => {
  81. if (panel.visible) {
  82. pclintBar.show()
  83. } else {
  84. pclintBar.hide()
  85. }
  86. })
  87. panel.webview.html = getWebViewContent(context, 'plugin/index.html')
  88. // panel.webview.html = '<html><body>你好,我是Webview</body></html>'
  89. panel.webview.postMessage({
  90. cmd: 'setSrc',
  91. data: {
  92. src: vscode.workspace.getConfiguration().get('openHuiKaiFa.src'),
  93. db: JSON.parse(fs.readFileSync(DB_PATH).toString() || '{}')
  94. }
  95. })
  96. panel.webview.onDidReceiveMessage(message => {
  97. if (message.cmd && message.data) {
  98. const method = methods[message.cmd]
  99. if (method) method(message, vscode, dirPath)
  100. } else {
  101. vscode.window.showInformationMessage('没有与消息对应的方法')
  102. }
  103. }, undefined, context.subscriptions)
  104. panel.onDidDispose(e => {
  105. pclintBar.dispose()
  106. })
  107. } else {
  108. vscode.window.showInformationMessage('无法获取文件夹路径')
  109. }
  110. }))
  111. }