Refactor extension, make it easy to maintain and unique to register
This commit is contained in:
1 parent
472f5efea0
commit
b23e69cfbb
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"
|
"Other"
|
||||||
],
|
],
|
||||||
"activationEvents": [
|
"activationEvents": [
|
||||||
"onCommand:extension.htmlToRsx",
|
"onCommand:extension.htmlToDioxusRsx",
|
||||||
"onCommand:extension.htmlToComponent"
|
"onCommand:extension.htmlToDioxusComponent"
|
||||||
],
|
],
|
||||||
"main": "./out/extension",
|
"main": "./out/extension",
|
||||||
"contributes": {
|
"contributes": {
|
||||||
"commands": [
|
"commands": [
|
||||||
{
|
{
|
||||||
"command": "extension.htmlToRsx",
|
"command": "extension.htmlToDioxusRsx",
|
||||||
"title": "Convert HTML to RSX"
|
"title": "Dioxus: Convert HTML to RSX"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"command": "extension.htmlToComponent",
|
"command": "extension.htmlToDioxusComponent",
|
||||||
"title": "Convert HTML to Dioxus Component"
|
"title": "Dioxus: Convert HTML to Component"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
@@ -37,7 +37,8 @@
|
|||||||
"vscode:prepublish": "npm run compile",
|
"vscode:prepublish": "npm run compile",
|
||||||
"compile": "tsc -p ./",
|
"compile": "tsc -p ./",
|
||||||
"lint": "eslint . --ext .ts,.tsx",
|
"lint": "eslint . --ext .ts,.tsx",
|
||||||
"watch": "tsc -watch -p ./"
|
"watch": "tsc -watch -p ./",
|
||||||
|
"package": "vsce package"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/node": "^12.12.0",
|
"@types/node": "^12.12.0",
|
||||||
|
|||||||
+25
-50
@@ -1,57 +1,32 @@
|
|||||||
'use strict';
|
|
||||||
|
|
||||||
import * as vscode from 'vscode';
|
import * as vscode from 'vscode';
|
||||||
import { spawn } from "child_process";
|
import { spawn } from "child_process";
|
||||||
|
|
||||||
export function activate(context: vscode.ExtensionContext) {
|
export function activate(context: vscode.ExtensionContext) {
|
||||||
const htmlToPureRsx = vscode.commands.registerCommand('extension.htmlToRsx', function () {
|
function registerCommand(cmd: string) {
|
||||||
// Get the active text editor
|
function convert(cmd: string) {
|
||||||
const editor = vscode.window.activeTextEditor;
|
const editor = vscode.window.activeTextEditor;// Get the active text editor
|
||||||
|
if (editor) {
|
||||||
if (editor) {
|
const html = editor.document.getText(editor.selection);
|
||||||
const document = editor.document;
|
if (html.length > 0) {
|
||||||
const selection = editor.selection;
|
let params = ["translate"];
|
||||||
const word = document.getText(selection);
|
if (cmd.includes("Component")) params.push("--component");
|
||||||
|
params.push("--source");
|
||||||
const child_proc = spawn("dioxus", ["translate", "--source", word]);
|
params.push(html);
|
||||||
|
const child_proc = spawn("dioxus", params);
|
||||||
let result = '';
|
let result = '';
|
||||||
child_proc.stdout?.on('data', data => result += data);
|
child_proc.stdout?.on('data', data => result += data);
|
||||||
|
child_proc.on('close', () => {
|
||||||
child_proc.on('close', () => {
|
if (result.length > 0) editor.edit(editBuilder => editBuilder.replace(editor.selection, result));
|
||||||
editor.edit(editBuilder => {
|
});
|
||||||
if (result != '') {
|
} else {
|
||||||
editBuilder.replace(selection, result)
|
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 () {
|
registerCommand('extension.htmlToDioxusRsx');
|
||||||
// Get the active text editor
|
registerCommand('extension.htmlToDioxusComponent');
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user