1+ /*
2+ * Copyright (c) 2012 Yasin Kuyu - twitter.com/yasinkuyu. All rights reserved.
3+ *
4+ * Permission is hereby granted, free of charge, to any person obtaining a
5+ * copy of this software and associated documentation files (the "Software"),
6+ * to deal in the Software without restriction, including without limitation
7+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8+ * and/or sell copies of the Software, and to permit persons to whom the
9+ * Software is furnished to do so, subject to the following conditions:
10+ *
11+ * The above copyright notice and this permission notice shall be included in
12+ * all copies or substantial portions of the Software.
13+ *
14+ */
15+
16+ /*jslint vars: true, plusplus: true, devel: true, nomen: true, indent: 4, maxerr: 50 */
17+ /*global define, $, brackets, btoa, atob */
18+
19+ define ( function ( require , exports , module ) {
20+ 'use strict' ;
21+
22+ // Brackets modules
23+ var EditorManager = brackets . getModule ( "editor/EditorManager" ) ,
24+ CommandManager = brackets . getModule ( "command/CommandManager" ) ,
25+ Menus = brackets . getModule ( "command/Menus" ) ,
26+ Localize = require ( "strings" ) ;
27+
28+ var STRING_REMOVE_EMPTY_LINES = "string_remove_empty_lines" ,
29+ STRING_REMOVE_BREAK_LINES = "string_remove_break_lines" ,
30+ STRING_REMOVE_LEADING_NEW_LINES = "string_remove_leading_newlines" ,
31+ STRING_UPPERCASE = "string_uppercase" ,
32+ STRING_LOWERCASE = "string_lowercase" ,
33+ STRING_TITLECASE = "string_titlecase" ,
34+ STRING_HTML_ENCODE = "string_html_encode" ,
35+ STRING_HTML_DECODE = "string_html_decode" ,
36+ STRING_ENCODE_URI = "string_encode_uri_component" ,
37+ STRING_DECODE_URI = "string_decode_uri_component" ;
38+
39+ /* Extension const */
40+ var commandId = "insya-tools" ,
41+ menuId = "insya-menu" ,
42+ moduleName = "insya-module-string" ;
43+
44+ // Selections
45+ function getSelectedText ( ) {
46+ var editor = EditorManager . getFocusedEditor ( ) ;
47+ if ( editor ) {
48+ var selection = editor . getSelection ( ) ;
49+ var selectText = editor . getSelectedText ( ) ;
50+ return selectText ;
51+ }
52+
53+ }
54+
55+ function setSelectedText ( text ) {
56+
57+ var editor = EditorManager . getFocusedEditor ( ) ;
58+ var selection = editor . getSelection ( ) ;
59+
60+ //EditorManager.getFocusedEditor()._codeMirror.replaceSelection(text);
61+
62+ editor . document . replaceRange ( text , selection . start , selection . end ) ;
63+ }
64+
65+ // Line Operations
66+ function removeBreaklines ( ) {
67+
68+ var result = getSelectedText ( ) . replace ( / \n / g, '' ) ;
69+ setSelectedText ( result ) ;
70+
71+ }
72+
73+ function removeEmptyLines ( ) {
74+
75+ var result = getSelectedText ( ) . replace ( / ^ ( \r \n ) | ( \n ) / , '' ) ;
76+ setSelectedText ( result ) ;
77+
78+ }
79+
80+ function removeLeadingNewLines ( ) {
81+
82+ var result = getSelectedText ( ) . replace ( / ^ \n + / , "" ) ;
83+ setSelectedText ( result ) ;
84+
85+ }
86+
87+ function toUppper ( ) {
88+ setSelectedText ( getSelectedText ( ) . toUpperCase ( ) ) ;
89+ }
90+
91+ function toLower ( ) {
92+ setSelectedText ( getSelectedText ( ) . toLowerCase ( ) ) ;
93+ }
94+
95+ function toTitle ( ) {
96+ setSelectedText ( getSelectedText ( ) . toLowerCase ( ) ) ;
97+ }
98+
99+ function urlEncode ( ) {
100+ setSelectedText ( encodeURIComponent ( getSelectedText ( ) ) ) ;
101+ }
102+
103+ function urlDecode ( ) {
104+ setSelectedText ( decodeURIComponent ( getSelectedText ( ) ) ) ;
105+ }
106+
107+ function htmlEncode ( ) {
108+ var result = getSelectedText ( ) . replace ( / " / g, """ ) . replace ( / ' / g, "'" ) ;
109+ setSelectedText ( result ) ;
110+ }
111+
112+ function htmlDecode ( ) {
113+ var result = getSelectedText ( ) ;
114+ setSelectedText ( result ) ;
115+ }
116+
117+ function createNavigation ( menu ) {
118+ menu . addMenuItem ( STRING_REMOVE_EMPTY_LINES ) ;
119+ menu . addMenuItem ( STRING_REMOVE_BREAK_LINES ) ;
120+ menu . addMenuItem ( STRING_REMOVE_LEADING_NEW_LINES ) ;
121+ menu . addMenuDivider ( ) ;
122+ menu . addMenuItem ( STRING_UPPERCASE ) ;
123+ menu . addMenuItem ( STRING_LOWERCASE ) ;
124+ menu . addMenuItem ( STRING_TITLECASE ) ;
125+ menu . addMenuDivider ( ) ;
126+ menu . addMenuItem ( STRING_HTML_ENCODE ) ;
127+ menu . addMenuItem ( STRING_HTML_DECODE ) ;
128+ menu . addMenuDivider ( ) ;
129+ menu . addMenuItem ( STRING_ENCODE_URI ) ;
130+ menu . addMenuItem ( STRING_DECODE_URI ) ;
131+ }
132+
133+ CommandManager . register ( Localize . STRING_REMOVE_EMPTY_LINES , STRING_REMOVE_EMPTY_LINES , removeEmptyLines ) ;
134+ CommandManager . register ( Localize . STRING_REMOVE_BREAK_LINES , STRING_REMOVE_BREAK_LINES , removeBreaklines ) ;
135+ CommandManager . register ( Localize . STRING_REMOVE_LEADING_NEW_LINES , STRING_REMOVE_LEADING_NEW_LINES , removeLeadingNewLines ) ;
136+
137+ CommandManager . register ( Localize . STRING_UPPERCASE , STRING_UPPERCASE , toUppper ) ;
138+ CommandManager . register ( Localize . STRING_LOWERCASE , STRING_LOWERCASE , toLower ) ;
139+ CommandManager . register ( Localize . STRING_TITLECASE , STRING_TITLECASE , toTitle ) ;
140+
141+ CommandManager . register ( Localize . STRING_HTML_ENCODE , STRING_HTML_ENCODE , htmlEncode ) ;
142+ CommandManager . register ( Localize . STRING_HTML_DECODE , STRING_HTML_DECODE , htmlDecode ) ;
143+
144+ CommandManager . register ( Localize . STRING_ENCODE_URI , STRING_ENCODE_URI , urlEncode ) ;
145+ CommandManager . register ( Localize . STRING_DECODE_URI , STRING_DECODE_URI , urlDecode ) ;
146+
147+ //var menu = Menus.getMenu(Menus.AppMenuBar.EDIT_MENU);
148+ var menu = Menus . addMenu ( Localize . MENU_LABEL , commandId , Menus . BEFORE , Menus . AppMenuBar . HELP_MENU ) ;
149+ createNavigation ( menu ) ;
150+
151+ var contextMenu = Menus . getContextMenu ( Menus . ContextMenuIds . EDITOR_MENU ) ;
152+ createNavigation ( contextMenu ) ;
153+
154+ } ) ;
0 commit comments