2 Star 80 Fork 0

IanLew / px2rem

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
convert.js 1.98 KB
一键复制 编辑 原始数据 按行查看 历史
IanLew 提交于 2018-06-29 20:59 . Add rem2px and move it to edit menu
/*
* Copyright (c) 2016 liuyang - All rights reserved
*
*/
define(function(require, exports, module) {
"use strict";
var EditorManager = brackets.getModule("editor/EditorManager"),
DocumentManager = brackets.getModule("document/DocumentManager");
var UNITS_REGEX = /(\d*\.?\d+)\s?(px|rem)/igm,
DOT_REGEX = /^\d+(?:\.\d{0,3})?/,
RES_REGEX = /\d+/g;
function convert(editor, base) {
if (editor) {
var isSelection = false;
var selectedText = editor.getSelectedText();
var selection = editor.getSelection();
console.log(selectedText)
var result;
if (/px/g.test(selectedText)) {
result = parseInt(selectedText) / parseInt(base);
result = DOT_REGEX.exec(result)[0];
result = parseFloat(result).toString();
var match = result.match(RES_REGEX);
if (match[0] === "0") result = "." + match[1] + "rem";
else result = result + "rem";
} else if (/rem/g.test(selectedText)) {
result = parseFloat(selectedText) * parseInt(base);
result = Math.round(result) + 'px';
}
if (selectedText.length > 0) {
isSelection = true;
}
var doc = DocumentManager.getCurrentDocument();
doc.batchOperation(function() {
if (isSelection) {
doc.replaceRange(result, selection.start, selection.end);
} else {
doc.setText(result);
}
});
}
}
function pxrem(base) {
var match, start, end;
var editor = EditorManager.getCurrentFullEditor();
var pos = editor.getCursorPos();
var sel = editor.getSelection();
if (sel.start.line === sel.end.line) {
var unitRegEx = new RegExp(UNITS_REGEX);
var cursorLine = editor.document.getLine(pos.line);
do {
match = unitRegEx.exec(cursorLine);
if (match) {
start = match.index;
end = start + match[0].length;
}
} while (match && (pos.ch < start || pos.ch > end));
if (match) {
pos.ch = start;
editor.setSelection(pos, {
line: pos.line,
ch: end
});
convert(editor, base);
} // Missing error prompt.
}
}
exports.pxrem = pxrem;
});
NodeJS
1
https://gitee.com/IanLew/px2rem.git
git@gitee.com:IanLew/px2rem.git
IanLew
px2rem
px2rem
master

搜索帮助