forked from tpryan/Brackets-Reflow-Cleaner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
59 lines (44 loc) · 2.34 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*jslint vars: true, plusplus: true, devel: true, nomen: true, regexp: true, indent: 4, maxerr: 50 */
/*global define, $, brackets, ReflowHTMLExtractor, ReflowCSSExtractor */
/** Simple extension that converts Reflow generated HTML and CSS into a decent starting point for handcode. */
define(function (require, exports, module) {
"use strict";
var CommandManager = brackets.getModule("command/CommandManager"),
EditorManager = brackets.getModule("editor/EditorManager"),
Menus = brackets.getModule("command/Menus");
require("jscssp");
require("reflowHTMLExtractor");
require("reflowCSSExtractor");
// Function to run when the menu item is clicked
function handleReflowClean() {
var editor = EditorManager.getFocusedEditor();
if (editor) {
var editorContent = editor.document.getText();
var newEditorContent = "";
var extension = editor.document.file.name.split('.').pop();
if (extension === "css") {
var reflowCSSExtractor = new ReflowCSSExtractor(editorContent);
newEditorContent = reflowCSSExtractor.createReport();
editor.document.setText(newEditorContent);
} else if (extension === "html") {
var reflowHTMLExtractor = new ReflowHTMLExtractor(editorContent, $);
reflowHTMLExtractor.processHTML();
var doc = reflowHTMLExtractor.htmldoc;
newEditorContent = "<!DOCTYPE html>" + '\n' +
"<html>" + '\n' +
doc.documentElement.innerHTML + '\n' + "</html>";
editor.document.setText(newEditorContent);
}
}
}
// First, register a command - a UI-less object associating an id to a handler
var MY_COMMAND_ID = "reflowcleaner.writehello"; // package-style naming to avoid collisions
CommandManager.register("Extract from Reflow Content", MY_COMMAND_ID, handleReflowClean);
// Then create a menu item bound to the command
// The label of the menu item is the name we gave the command (see above)
var menu = Menus.getMenu(Menus.AppMenuBar.EDIT_MENU);
if (typeof menu !== "undefined") {
menu.addMenuItem(MY_COMMAND_ID);
exports.handleReflowClean = handleReflowClean;
}
});