Browse Source

Refactor extension, make it easy to maintain and unique to register

c5soft 3 years ago
parent
commit
b23e69cfbb
3 changed files with 54 additions and 57 deletions
  1. 21 0
      extension/LICENSE.txt
  2. 8 7
      extension/package.json
  3. 25 50
      extension/src/extension.ts

+ 21 - 0
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.

+ 8 - 7
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",

+ 25 - 50
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)
-					}
-				})
-			});
-		}
-	});
-
-	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)
-					}
-				})
-			});
+	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);
+	}
 
-	context.subscriptions.push(htmlToPureRsx);
-	context.subscriptions.push(htmlToComponent);
+	registerCommand('extension.htmlToDioxusRsx');
+	registerCommand('extension.htmlToDioxusComponent');
 }