From b23e69cfbbf0af2d96ab897bf45e3944f863be59 Mon Sep 17 00:00:00 2001 From: c5soft Date: Thu, 27 Jan 2022 15:54:49 +0800 Subject: [PATCH] Refactor extension, make it easy to maintain and unique to register --- extension/LICENSE.txt | 21 +++++++++++ extension/package.json | 15 ++++---- extension/src/extension.ts | 75 +++++++++++++------------------------- 3 files changed, 54 insertions(+), 57 deletions(-) create mode 100644 extension/LICENSE.txt diff --git a/extension/LICENSE.txt b/extension/LICENSE.txt new file mode 100644 index 000000000..6fd3d45f6 --- /dev/null +++ b/extension/LICENSE.txt @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2021 DioxusLabs + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file diff --git a/extension/package.json b/extension/package.json index 3b72cfa8c..0a3d50b67 100644 --- a/extension/package.json +++ b/extension/package.json @@ -17,19 +17,19 @@ "Other" ], "activationEvents": [ - "onCommand:extension.htmlToRsx", - "onCommand:extension.htmlToComponent" + "onCommand:extension.htmlToDioxusRsx", + "onCommand:extension.htmlToDioxusComponent" ], "main": "./out/extension", "contributes": { "commands": [ { - "command": "extension.htmlToRsx", - "title": "Convert HTML to RSX" + "command": "extension.htmlToDioxusRsx", + "title": "Dioxus: Convert HTML to RSX" }, { - "command": "extension.htmlToComponent", - "title": "Convert HTML to Dioxus Component" + "command": "extension.htmlToDioxusComponent", + "title": "Dioxus: Convert HTML to Component" } ] }, @@ -37,7 +37,8 @@ "vscode:prepublish": "npm run compile", "compile": "tsc -p ./", "lint": "eslint . --ext .ts,.tsx", - "watch": "tsc -watch -p ./" + "watch": "tsc -watch -p ./", + "package": "vsce package" }, "devDependencies": { "@types/node": "^12.12.0", diff --git a/extension/src/extension.ts b/extension/src/extension.ts index c9ab06662..5a41c7f5f 100644 --- a/extension/src/extension.ts +++ b/extension/src/extension.ts @@ -1,57 +1,32 @@ -'use strict'; - import * as vscode from 'vscode'; import { spawn } from "child_process"; export function activate(context: vscode.ExtensionContext) { - const htmlToPureRsx = vscode.commands.registerCommand('extension.htmlToRsx', function () { - // Get the active text editor - const editor = vscode.window.activeTextEditor; - - if (editor) { - const document = editor.document; - const selection = editor.selection; - const word = document.getText(selection); - - const child_proc = spawn("dioxus", ["translate", "--source", word]); - - let result = ''; - child_proc.stdout?.on('data', data => result += data); - - child_proc.on('close', () => { - editor.edit(editBuilder => { - if (result != '') { - editBuilder.replace(selection, result) - } - }) - }); + function registerCommand(cmd: string) { + function convert(cmd: string) { + const editor = vscode.window.activeTextEditor;// Get the active text editor + if (editor) { + const html = editor.document.getText(editor.selection); + if (html.length > 0) { + let params = ["translate"]; + if (cmd.includes("Component")) params.push("--component"); + params.push("--source"); + params.push(html); + const child_proc = spawn("dioxus", params); + let result = ''; + child_proc.stdout?.on('data', data => result += data); + child_proc.on('close', () => { + if (result.length > 0) editor.edit(editBuilder => editBuilder.replace(editor.selection, result)); + }); + } else { + vscode.window.showWarningMessage("Please select HTML fragment before invoking this command!"); + } + } } - }); + const handle = vscode.commands.registerCommand(cmd, () => convert(cmd)); + context.subscriptions.push(handle); + } - const htmlToComponent = vscode.commands.registerCommand('extension.htmlToComponent', function () { - // Get the active text editor - const editor = vscode.window.activeTextEditor; - - if (editor) { - const document = editor.document; - const selection = editor.selection; - const word = document.getText(selection); - - const child_proc = spawn("dioxus", ["translate", "--component", "--source", word]); - - let result = ''; - child_proc.stdout?.on('data', data => result += data); - - child_proc.on('close', () => { - editor.edit(editBuilder => { - if (result != '') { - editBuilder.replace(selection, result) - } - }) - }); - } - }); - - context.subscriptions.push(htmlToPureRsx); - context.subscriptions.push(htmlToComponent); + registerCommand('extension.htmlToDioxusRsx'); + registerCommand('extension.htmlToDioxusComponent'); }