openHuiKaiFa.js 3.6 KB

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