alm
Version:
The best IDE for TypeScript
65 lines (64 loc) • 2.56 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
var ui = require("../../ui");
var monacoUtils = require("../monacoUtils");
var CommonEditorRegistry = monaco.CommonEditorRegistry;
var EditorAction = monaco.EditorAction;
var EditorContextKeys = monaco.EditorContextKeys;
/* Test:
<!-- Hello world -->
<div class="awesome" style="border: 1px solid red">
<label for="name">Enter your name: </label>
<input type="text" id="name" />
</div>
<p>Enter your HTML here</p>
*/
var HtmlToTsxAction = /** @class */ (function (_super) {
__extends(HtmlToTsxAction, _super);
function HtmlToTsxAction() {
return _super.call(this, {
id: 'editor.action.htmlToTsx',
label: 'HTML to TSX',
alias: 'HTML to TSX',
precondition: EditorContextKeys.Writable,
}) || this;
}
HtmlToTsxAction.prototype.run = function (accessor, editor) {
var filePath = editor.filePath;
var selection = editor.getSelection();
if (!selection.isEmpty()) {
var indentSize = editor.getModel()._editorOptions ? editor.getModel()._editorOptions.tabSize : 2;
var oldText = editor.getModel().getValueInRange(selection);
var newText = convert(oldText, indentSize);
monacoUtils.replaceSelection({ editor: editor, newText: newText });
}
else {
ui.notifyWarningNormalDisappear('Please select the HTML you want converted to TSX and try again 🌹');
}
};
return HtmlToTsxAction;
}(EditorAction));
CommonEditorRegistry.registerEditorAction(new HtmlToTsxAction());
/**
* Take a look at :
* https://github.com/reactjs/react-magic
* https://www.npmjs.com/package/htmltojsx
*/
var htmlToJsx_1 = require("../../htmlToJsx/htmlToJsx");
function convert(content, indentSize) {
var indent = Array(indentSize + 1).join(' ');
var converter = new htmlToJsx_1.HTMLtoJSX({ indent: indent, createClass: false });
var output = converter.convert(content);
return output;
}
exports.convert = convert;