Skip to content

Commit

Permalink
1.5.0 minify selected text - issue #5
Browse files Browse the repository at this point in the history
  • Loading branch information
Josee9988 committed Oct 19, 2019
1 parent e048e14 commit 3001a2c
Show file tree
Hide file tree
Showing 6 changed files with 198 additions and 34 deletions.
1 change: 1 addition & 0 deletions .vscodeignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ Screenshots/command.png
Screenshots/output.png
Screenshots/rawIcon.png
Screenshots/RightClick.png
Screenshots/SelectedText.gif
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)

---

## [**1.5.0**] - 2019-10-19

### Added

* Minify **selected text**.

### Changed

* Some dependencies were updated.

## [**1.4.3**] - 2019-10-14

### Fixed
Expand Down
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

VSCode **minifier** for **CSS**, **HTML** ,**JSON**, **Typescript**(alpha), **JavaScript**(alpha), **JavaScriptReact**(alpha), **LESS**, **SASS**, **SCSS**, and **JSONC**. you will love its simplicity!

You can minify the file and replace all the content with the new minified text, **or** you can preserve the original document and get the minified text in another document!
You can minify the file and replace all the content with the new minified text, **or** you can preserve the original document and get the minified text in another document! Also you can simply minify your **selected text**.

Go to the extension *settings* and make it as you want, enable or disable: **minify on save**, **languages**, **hexadecimal shortener**, **messages**, and much more.

Expand Down Expand Up @@ -34,6 +34,7 @@ Check our **[changelog](CHANGELOG.md)**.

- **```Minify this document ⚑``` Or ```CTRL+ALT+M```**
- **```Minify this document and preserve the original ⛏``` Or ```CTRL+ALT+N```**
- **```"Minify the selected text 🎯``` Or ```CTRL+ALT+M CTRL+ALT+M```**

---

Expand All @@ -55,9 +56,13 @@ Check our **[changelog](CHANGELOG.md)**.

<img src="https://i.imgur.com/RTyX0PZ.gif" alt="Preview2nd" title="preview2nd" style="border-radius:15px; box-shadow: 6px 6px #282829; max-height: 750px; max-width:750px;margin-left: auto; margin-right:auto;display: block;margin-left: auto;margin-right:auto;width:75%;"/>

- Minify only your **selected text**!

<img src="https://i.imgur.com/Cym0J2C.gif" alt="rightclick" title="rightclick" style="border-radius:15px; box-shadow: 6px 6px #282829; max-height: 750px; max-width:750px;margin-left: auto; margin-right:auto;display: block;margin-left: auto;margin-right:auto;width:75%;"/>

- Also, minify when right-clicking on the code!

<img src="https://i.imgur.com/fXMQTc0.png" alt="rightclick" title="rightclick" style="border-radius:15px; box-shadow: 6px 6px #282829; max-height: 750px; max-width:750px;margin-left: auto; margin-right:auto;display: block;margin-left: auto;margin-right:auto;width:75%;"/>
<img src="https://i.imgur.com/fXMQTc0.png" alt="selectedText" title="selectedText" style="border-radius:15px; box-shadow: 6px 6px #282829; max-height: 750px; max-width:750px;margin-left: auto; margin-right:auto;display: block;margin-left: auto;margin-right:auto;width:75%;"/>

- Minify when right-clicking on a file of the menu without opening it!

Expand Down
Binary file added Screenshots/SelectedText.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 19 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "minifyall",
"displayName": "MinifyAll",
"description": "Minifier for JSON, CSS, HTML, LESS, SASS, SCSS, Typescript(alpha), JavaScript(alpha), JavaScriptReact(alpha) and JSONC. you will love its simplicity!",
"version": "1.4.3",
"version": "1.5.0",
"publisher": "josee9988",
"license": "SEE LICENSE IN LICENSE",
"author": {
Expand Down Expand Up @@ -59,6 +59,7 @@
"activationEvents": [
"onCommand:extension.MinifyAll",
"onCommand:extension.MinifyAll2OtherDoc",
"onCommand:extension.MinifyAllSelectedText",
"*"
],
"main": "./src/main.js",
Expand All @@ -74,6 +75,9 @@
{
"command": "extension.MinifyAll2OtherDocSelected",
"title": "Minify the selected document and preserve the original πŸ’Ž"
}, {
"command": "extension.MinifyAllSelectedText",
"title": "Minify the selected text 🎯"
}
],
"keybindings": [{
Expand All @@ -91,6 +95,14 @@
"mac": "cmd+alt+N",
"linux": "Ctrl+alt+N",
"win": "Ctrl+alt+N"
},
{
"command": "extension.MinifyAllSelectedText",
"title": "Minify the selected text 🎯",
"key": "Ctrl+alt+M Ctrl+alt+M",
"mac": "Ctrl+alt+M Ctrl+alt+M",
"linux": "Ctrl+alt+M Ctrl+alt+M",
"win": "Ctrl+alt+M Ctrl+alt+M"
}
],
"configuration": {
Expand Down Expand Up @@ -201,6 +213,10 @@
"command": "extension.MinifyAll2OtherDoc",
"when": "editorLangId == html || editorLangId == css || editorLangId == scss || editorLangId == less || editorLangId == json || editorLangId == jsonc || editorLangId == javascript || editorLangId == javascriptreact || editorLangId == typescript",
"group": "MinifyAll"
},
{
"command": "extension.MinifyAllSelectedText",
"group": "MinifyAll"
}
],
"explorer/context": [{
Expand All @@ -212,11 +228,11 @@
"devDependencies": {
"@types/glob": "^7.1.1",
"@types/mocha": "^5.2.7",
"@types/node": "^12.7.12",
"@types/node": "^12.11.1",
"@types/vscode": "^1.39.0",
"eslint": "^6.5.1",
"glob": "^7.1.4",
"mocha": "^6.2.1",
"mocha": "^6.2.2",
"typescript": "^3.6.4",
"vscode-test": "^1.2.0"
}
Expand Down
190 changes: 161 additions & 29 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,10 @@ function activate(context) {
case 'less':
case 'sass':

if ((window.activeTextEditor.document.languageId == 'css' && !disableCss)
|| (window.activeTextEditor.document.languageId == 'scss' && !disableScss)
|| (window.activeTextEditor.document.languageId == 'less' && !disableLess)
|| (window.activeTextEditor.document.languageId == 'sass' && !disableSass)) {
if ((window.activeTextEditor.document.languageId == 'css' && !disableCss) ||
(window.activeTextEditor.document.languageId == 'scss' && !disableScss) ||
(window.activeTextEditor.document.languageId == 'less' && !disableLess) ||
(window.activeTextEditor.document.languageId == 'sass' && !disableSass)) {
const cssMinifier = require('./langDefaultMinifiers/cssMinifier.js');
const cssContent = document.getText().split('\n');

Expand All @@ -156,8 +156,8 @@ function activate(context) {
case 'json':
case 'jsonc':

if ((window.activeTextEditor.document.languageId == 'json' && !disableJson)
|| (window.activeTextEditor.document.languageId == 'jsonc' && !disableJsonc)) {
if ((window.activeTextEditor.document.languageId == 'json' && !disableJson) ||
(window.activeTextEditor.document.languageId == 'jsonc' && !disableJsonc)) {
const jsonMinifier = require('./langDefaultMinifiers/jsonMinifier.js');
const jsonContent = document.getText().split('\n');

Expand Down Expand Up @@ -197,9 +197,9 @@ function activate(context) {
case 'javascriptreact':
case 'typescript':

if ((window.activeTextEditor.document.languageId == 'javascript' && !disableJavascript)
|| (window.activeTextEditor.document.languageId == 'javascriptreact' && !disableJavascriptReact)
|| (window.activeTextEditor.document.languageId == 'typescript' && !disableTypescript)) {
if ((window.activeTextEditor.document.languageId == 'javascript' && !disableJavascript) ||
(window.activeTextEditor.document.languageId == 'javascriptreact' && !disableJavascriptReact) ||
(window.activeTextEditor.document.languageId == 'typescript' && !disableTypescript)) {
const jsMinifier = require('./langDefaultMinifiers/jsMinifier.js');
const jsContent = document.getText().split('\n');

Expand Down Expand Up @@ -243,10 +243,10 @@ function activate(context) {
case 'less':
case 'sass':

if ((window.activeTextEditor.document.languageId == 'css' && !disableCss)
|| (window.activeTextEditor.document.languageId == 'scss' && !disableScss)
|| (window.activeTextEditor.document.languageId == 'less' && !disableLess)
|| (window.activeTextEditor.document.languageId == 'sass' && !disableSass)) {
if ((window.activeTextEditor.document.languageId == 'css' && !disableCss) ||
(window.activeTextEditor.document.languageId == 'scss' && !disableScss) ||
(window.activeTextEditor.document.languageId == 'less' && !disableLess) ||
(window.activeTextEditor.document.languageId == 'sass' && !disableSass)) {
const path2NewFile = getNewFilePath(path, fileName, window.activeTextEditor.document.languageId);
const cssMinifier = require('./langDefaultMinifiers/cssMinifier.js');
const cssContent = document.getText().split('\n');
Expand All @@ -270,8 +270,8 @@ function activate(context) {
case 'json':
case 'jsonc':

if ((window.activeTextEditor.document.languageId == 'json' && !disableJson)
|| (window.activeTextEditor.document.languageId == 'jsonc' && !disableJsonc)) {
if ((window.activeTextEditor.document.languageId == 'json' && !disableJson) ||
(window.activeTextEditor.document.languageId == 'jsonc' && !disableJsonc)) {
const path2NewFile = getNewFilePath(path, fileName, window.activeTextEditor.document.languageId);
const jsonMinifier = require('./langDefaultMinifiers/jsonMinifier.js');
const jsonContent = document.getText().split('\n');
Expand Down Expand Up @@ -317,9 +317,9 @@ function activate(context) {
case 'javascriptreact':
case 'typescript':

if ((window.activeTextEditor.document.languageId == 'javascript' && !disableJavascript)
|| (window.activeTextEditor.document.languageId == 'javascriptreact' && !disableJavascriptReact)
|| (window.activeTextEditor.document.languageId == 'typescript' && !disableTypescript)) {
if ((window.activeTextEditor.document.languageId == 'javascript' && !disableJavascript) ||
(window.activeTextEditor.document.languageId == 'javascriptreact' && !disableJavascriptReact) ||
(window.activeTextEditor.document.languageId == 'typescript' && !disableTypescript)) {
const path2NewFile = getNewFilePath(path, fileName, window.activeTextEditor.document.languageId);
const jsMinifier = require('./langDefaultMinifiers/jsMinifier.js');
const jsContent = document.getText().split('\n');
Expand Down Expand Up @@ -366,10 +366,10 @@ function activate(context) {
case 'less':
case 'sass':

if ((fileUri.path.split('.').pop() == 'css' && !disableCss)
|| (fileUri.path.split('.').pop() == 'scss' && !disableScss)
|| (fileUri.path.split('.').pop() == 'less' && !disableLess)
|| (fileUri.path.split('.').pop() == 'sass' && !disableSass)) {
if ((fileUri.path.split('.').pop() == 'css' && !disableCss) ||
(fileUri.path.split('.').pop() == 'scss' && !disableScss) ||
(fileUri.path.split('.').pop() == 'less' && !disableLess) ||
(fileUri.path.split('.').pop() == 'sass' && !disableSass)) {
const newName = path.basename(fileUri.path).replace('.css', '-min.css');
const path2NewFile = path.join(filePath, newName);
const cssMinifier = require('./langDefaultMinifiers/cssMinifier.js');
Expand All @@ -394,8 +394,8 @@ function activate(context) {
case 'json':
case 'jsonc':

if ((fileUri.path.split('.').pop() == 'json' && !disableJson)
|| (fileUri.path.split('.').pop() == 'jsonc' && !disableJsonc)) {
if ((fileUri.path.split('.').pop() == 'json' && !disableJson) ||
(fileUri.path.split('.').pop() == 'jsonc' && !disableJsonc)) {
const newNameJson = path.basename(fileUri.path).replace('.json', '-min.json');
const path2NewFileJson = path.join(filePath, newNameJson);
const jsonMinifier = require('./langDefaultMinifiers/jsonMinifier.js');
Expand Down Expand Up @@ -443,9 +443,9 @@ function activate(context) {
case 'javascriptreact':
case 'typescript':

if ((fileUri.path.split('.').pop() == 'javascript' && !disableJavascript)
|| (fileUri.path.split('.').pop() == 'javascriptreact' && !disableJavascriptReact)
|| (fileUri.path.split('.').pop() == 'typescript' && !disableTypescript)) {
if ((fileUri.path.split('.').pop() == 'javascript' && !disableJavascript) ||
(fileUri.path.split('.').pop() == 'javascriptreact' && !disableJavascriptReact) ||
(fileUri.path.split('.').pop() == 'typescript' && !disableTypescript)) {
const newNameJs = path.basename(fileUri.path).replace('.js', '-min.js');
const path2NewFileJs = path.join(filePath, newNameJs);
const jsMinifier = require('./langDefaultMinifiers/jsMinifier.js');
Expand Down Expand Up @@ -473,6 +473,115 @@ function activate(context) {
}
context.subscriptions.push(MinifyAll2OtherDocSelected);
});


// Command MinifyAll. It executes if its called the command "extension.MinifyAll"
const MinifyAllSelectedText = commands.registerCommand('extension.MinifyAllSelectedText', () => {
console.log("The extension 'MinifyAll' with the command: 'MinifyAllSelectedText' is currently working...");

const editor = vscode.window.activeTextEditor;
const {
selection,
} = editor;
const selectedText = editor.document.getText(selection);
startTime = new Date().getTime();
statusReady = true;
const {
document,
} = window.activeTextEditor;

switch (window.activeTextEditor.document.languageId) {
case 'css':
case 'scss':
case 'less':
case 'sass':

if ((window.activeTextEditor.document.languageId == 'css' && !disableCss) ||
(window.activeTextEditor.document.languageId == 'scss' && !disableScss) ||
(window.activeTextEditor.document.languageId == 'less' && !disableLess) ||
(window.activeTextEditor.document.languageId == 'sass' && !disableSass)) {
const cssMinifier = require('./langDefaultMinifiers/cssMinifier.js');
const cssContent = selectedText.split('\n');

const RemoveComments = removeComments(cssContent);

const hexMinifiedCss = HexMinify(RemoveComments);

const minifierCss = new cssMinifier(hexMinifiedCss);

const modifiedCssText = minifierCss.getCssMinified();

timeSpend = replaceSelectedCodeAndGetTime(editor, selection, modifiedCssText);
} else {
showMessage('We will not format this file type because it is disabled.', false);
}
break;

case 'json':
case 'jsonc':

if ((window.activeTextEditor.document.languageId == 'json' && !disableJson) ||
(window.activeTextEditor.document.languageId == 'jsonc' && !disableJsonc)) {
const jsonMinifier = require('./langDefaultMinifiers/jsonMinifier.js');
const jsonContent = selectedText.split('\n');

const contentWithHexMinified = HexMinify(jsonContent);

const RemoveComments = removeComments(contentWithHexMinified);

const minifierJson = new jsonMinifier(RemoveComments);

const modifiedJsonText = minifierJson.getJSONMinified();

timeSpend = replaceSelectedCodeAndGetTime(editor, selection, modifiedJsonText);
} else {
showMessage('We will not format this file type because it is disabled.', false);
}
break;

case 'html':

if ((window.activeTextEditor.document.languageId == 'html' && !disableHtml)) {
const htmlMinifier = require('./langDefaultMinifiers/htmlMinifier.js');
const htmlContent = selectedText.split('\n');

const minifierHtml = new htmlMinifier(htmlContent);

minifierHtml.removeMultipleLineComments();

const modifiedHtmlText = minifierHtml.getHtmlMinified();

timeSpend = replaceSelectedCodeAndGetTime(editor, selection, modifiedHtmlText);
} else {
showMessage('We will not format this file type because it is disabled.', false);
}
break;

case 'javascript':
case 'javascriptreact':
case 'typescript':

if ((window.activeTextEditor.document.languageId == 'javascript' && !disableJavascript) ||
(window.activeTextEditor.document.languageId == 'javascriptreact' && !disableJavascriptReact) ||
(window.activeTextEditor.document.languageId == 'typescript' && !disableTypescript)) {
const jsMinifier = require('./langDefaultMinifiers/jsMinifier.js');
const jsContent = selectedText.split('\n');

const RemoveComments = removeComments(jsContent);

const minifierJs = new jsMinifier(RemoveComments);

timeSpend = replaceSelectedCodeAndGetTime(editor, selection, minifierJs);
} else if (!disableMessages) {
window.showInformationMessage('We will not format this file type because it is disabled.');
}
break;
default:
showMessage('β›” We can not format this file type yet, use a valid one.', true);
break;
}
context.subscriptions.push(MinifyAllSelectedText);
});
}


Expand Down Expand Up @@ -591,8 +700,7 @@ function statusBarInfo() {
*/
function HexMinify(Content) {
let MinifierHex;
let
returnValue;
let returnValue;

if (!hexDisabled) {
MinifierHex = new HexMinifier(Content);
Expand Down Expand Up @@ -632,6 +740,30 @@ function replaceActualCodeAndGetTime(modifiedText) {
return ((new Date().getTime()) - startTime);
}

/**
* Summary gets the selected code and replaces it with the minified one.
*
* Description it receives the minified text and replaces it with the
* received text and also it gets the total time spend minifying.
*
* @access private
* @param {object} editor openned editor
* @param {object} selection selection provided.
* @param {string} modifiedText the minified text
*
* @return {Number} of the time spend.
*/
function replaceSelectedCodeAndGetTime(editor, selection, modifiedText) {
editor.edit((builder) => {
builder.replace(selection, modifiedText);
})
.then(() => {
const postion = editor.selection.end;
editor.selection = new vscode.Selection(postion, postion);
});
return ((new Date().getTime()) - startTime);
}

/**
* Summary gets the minified code and writes it in a new file.
*
Expand Down

0 comments on commit 3001a2c

Please sign in to comment.