openHuiKaiFa.js 3.7 KB

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