Merge pull request #8 from c5soft/master
Refactor extension, make it easy to maintain and unique to register
This commit is contained in:
3 files changed
+54
-57
No files matched your search
@@ -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.
|
||||
@@ -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
@@ -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');
|
||||
}
|
||||
Reference in new issue
Block a user