forked from jediwade/brackets-htmlformat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
1263 lines (1102 loc) · 48.4 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
/*jslint vars: true, plusplus: true, devel: true, nomen: true, regexp: true, indent: 4, maxerr: 50, eqeq: true, white: true */
/*global define, $, brackets, Mustache */
define(function (require, exports, module) {
'use strict';
console.log("INITIALIZING HTML FORMAT EXTENSION");
//--------------------------------------------------------------------------------------------------------------------------------------------//
// BRACKETS OBJECTS
//--------------------------------------------------------------------------------------------------------------------------------------------//
// Referencing for reloading Brackets after saving preference changes.
// Reloading Brackets is required for hotkey changes to register.
var Commands = brackets.getModule("command/Commands");
// Used for registering all the menubar actions that can be executed.
var CommandManager = brackets.getModule("command/CommandManager");
// Used for adding and removing menu options to the File/Edit/Find menubar.
var Menus = brackets.getModule("command/Menus");
// Used for inserting tags at cursor position or inserting tags around highlighted copy.
var DocumentManager = brackets.getModule("document/DocumentManager");
// Used for updating cursor position after tag has been inserted.
// Used for keeping highlight around copy after tag has been added around already highlighted text/
var EditorManager = brackets.getModule("editor/EditorManager");
// Used for detecting key presses when adding a blank tag to type into.
var KeyBindingManager = brackets.getModule("command/KeyBindingManager");
// Used for knowing when the file being viewed has changed.
var MainViewManager = brackets.getModule("view/MainViewManager");
// Used for getting the file extension of the file currently being viewed to know if the menu bar
// should appear and if hotkeys should work.
var FileUtils = brackets.getModule("file/FileUtils");
// Used for saving and retrieving hotkey preferences and more.
var PreferencesManager = brackets.getModule("preferences/PreferencesManager");
// Used for generating and showing the preference screen.
var Dialogs = brackets.getModule("widgets/Dialogs");
// Used for styling the preference screen.
var ExtensionUtils = brackets.getModule("utils/ExtensionUtils");
// Strings all used for handling the generation of menu bar options, hotkeys, right-click menu, etc.
var Strings = require("strings");
var PreferenceStrings = require("preferenceStrings");
var AdditionalPrefStrings = require("additionalPrefStrings");
//--------------------------------------------------------------------------------------------------------------------------------------------//
// CONTSTANTS
//--------------------------------------------------------------------------------------------------------------------------------------------//
var COMMAND_ID = "wadedwalker.brackets.extension.htmlFormat";
//------------------------------------------------------------------------------------------------------------//
var HTML_FORMAT_MENU_COMMAND_ID = COMMAND_ID + "." + "htmlFormatMenu";
//------------------------------------------------------------------------------------------------------------//
var BOLD_COMMAND_ID = COMMAND_ID + "." + PreferenceStrings.BOLD_TAG;
var BOLD_STYLE_COMMAND_ID = COMMAND_ID + "." + PreferenceStrings.BOLD_STYLE;
var ITALIC_COMMAND_ID = COMMAND_ID + "." + PreferenceStrings.ITALIC_TAG;
var ITALIC_STYLE_COMMAND_ID = COMMAND_ID + "." + PreferenceStrings.ITALIC_STYLE;
var UNDERLINE_COMMAND_ID = COMMAND_ID + "." + PreferenceStrings.UNDERLINE_TAG;
var UNDERLINE_STYLE_COMMAND_ID = COMMAND_ID + "." + PreferenceStrings.UNDERLINE_STYLE;
//------------------------------------------------------------------------------------------------------------//
var ANCHOR_TAG_COMMAND_ID = COMMAND_ID + "." + PreferenceStrings.ANCHOR_TAG;
//------------------------------------------------------------------------------------------------------------//
var SPAN_COMMAND_ID = COMMAND_ID + "." + PreferenceStrings.SPAN_TAG;
var DIV_COMMAND_ID = COMMAND_ID + "." + PreferenceStrings.DIV_TAG;
var HEADER_COMMAND_ID = COMMAND_ID + "." + PreferenceStrings.HEADER_TAG;
var NAV_COMMAND_ID = COMMAND_ID + "." + PreferenceStrings.NAV_TAG;
var ARTICLE_COMMAND_ID = COMMAND_ID + "." + PreferenceStrings.ARTICLE_TAG;
var SECTION_COMMAND_ID = COMMAND_ID + "." + PreferenceStrings.SECTION_TAG;
var ASIDE_COMMAND_ID = COMMAND_ID + "." + PreferenceStrings.ASIDE_TAG;
var FOOTER_COMMAND_ID = COMMAND_ID + "." + PreferenceStrings.FOOTER_TAG;
//------------------------------------------------------------------------------------------------------------//
var STRIKE_COMMAND_ID = COMMAND_ID + "." + PreferenceStrings.STRIKE_TAG;
var STRIKE_STYLE_COMMAND_ID = COMMAND_ID + "." + PreferenceStrings.STRIKE_STYLE;
var TELETYPE_COMMAND_ID = COMMAND_ID + "." + PreferenceStrings.TELETYPE_TAG;
var TELETYPE_STYLE_COMMAND_ID = COMMAND_ID + "." + PreferenceStrings.TELETYPE_STYLE;
//------------------------------------------------------------------------------------------------------------//
var CODE_COMMAND_ID = COMMAND_ID + "." + PreferenceStrings.CODE_TAG;
var VARIABLE_COMMAND_ID = COMMAND_ID + "." + PreferenceStrings.VARIABLE_TAG;
var SAMPLE_COMMAND_ID = COMMAND_ID + "." + PreferenceStrings.SAMPLE_TAG;
var KEYBOARD_COMMAND_ID = COMMAND_ID + "." + PreferenceStrings.KEYBOARD_TAG;
//------------------------------------------------------------------------------------------------------------//
var CITATION_COMMAND_ID = COMMAND_ID + "." + PreferenceStrings.CITATION_TAG;
var DEFINITION_COMMAND_ID = COMMAND_ID + "." + PreferenceStrings.DEFINITION_TAG;
var DELETED_COMMAND_ID = COMMAND_ID + "." + PreferenceStrings.DELETED_TAG;
var INSERTED_COMMAND_ID = COMMAND_ID + "." + PreferenceStrings.INSERTED_TAG;
//------------------------------------------------------------------------------------------------------------//
var INSERT_TAG_COMMAND_ID = COMMAND_ID + "." + PreferenceStrings.EMPTY_TAG;
var PREFERENCE_COMMAND_ID = COMMAND_ID + "." + PreferenceStrings.PREFERENCES;
//--------------------------------------------------------------------------------------------------------------------------------------------//
// VARIABLES
//--------------------------------------------------------------------------------------------------------------------------------------------//
/**
* HTML for rendering the preference options
* @private
*/
var _preferencePanel = require("text!preferencePanel.html");
/**
* Preferences for managing keyboard shortcuts.
* @private
*/
var _preferences = PreferencesManager.getExtensionPrefs(COMMAND_ID + "." + PreferenceStrings.PREFERENCES);
/**
* Key/value array of all current preference options. Used for checking against when making changes to the current preferences
* @private
*/
var _currentPrefs = {};
/**
* Indicates the OS platform. Used for generating keyboard shortcuts.
* @private
*/
var _platform = (navigator.appVersion.indexOf("Mac") !== -1) ? "mac" : "win";
/**
* Used for detecting the file type to know if the file menu should appear and if hotkeys should work
* @private
*/
var _fileExtension = "";
/**
* Menu bar containing all the format options.
* @type {Menu}
* @private
*/
var _htmlFormatMenu = null;
/**
* Array of all the commands added to the menu bar.
* @type {array}
* @private
*/
var _commands = [];
/**
* Current document being editted.
* @type {Document}
* @private
*/
var _currentDoc;
/**
* Current position of the cursor in the document being editted.
* @type {object}
* @private
*/
var _currentPos;
/**
* Represents the opening and closing tags of the insert empty tag action.
* @property {object} open
* @property {object} open.start
* @property {number} open.start.line
* @property {number} open.start.ch
* @property {object} open.end
* @property {number} open.end.line
* @property {number} open.end.ch
* @property {object} close
* @property {object} close.start
* @property {number} close.start.line
* @property {number} close.start.ch
* @property {object} close.end
* @property {number} close.end.line
* @property {number} close.end.ch
* @private
*/
var _emptyTagPos = {open:{start:{line:0, ch:0}, end:{line:0, ch:0}}, close:{start:{line:0, ch:0}, end:{line:0, ch:0}}};
/**
* Current full editor in use.
* @type {Editor}
* @private
*/
var _editor;
/**
* Currently highlighted copy
* @type {object}
* @private
*/
var _selection;
/**
* The character length of the currently highlighted text
* @type {number}
* @private
*/
var _lng = 0;
/**
* Determines whether or not the <strong> tag is used in place of <b> when adding a bold tag
* @type {boolean}
* @private
*/
var _boldUsesStrong = false;
/**
* Determines whether or not the <em> tag is used in place of <i> when adding an italic tag
* @type {boolean}
* @private
*/
var _italicUsesEm = false;
/**
*
* @type {boolean}
* @private
*/
var _addRightClick = false;
/**
* Used for keeping track of duplicate keyboard shortcuts. Prevents Save button from working if true.
* @private
*/
var _duplicateShortcuts = false;
/**
* @private
*/
var _defaultTagStyleShortcut = [null, null, null, null, ""];
//--------------------------------------------------------------------------------------------------------------------------------------------//
// PREFERENCES
//--------------------------------------------------------------------------------------------------------------------------------------------//
ExtensionUtils.loadStyleSheet(module, "preferencePanel.css");
var _control = (_platform === "mac") ? "Cmd" : "Ctrl";
// command, shift, alt, control, character
if (_preferences.get(PreferenceStrings.BOLD_TAG) === undefined) {
_preferences.set(PreferenceStrings.BOLD_TAG, [_control, null, null, null, "B"]);
_preferences.set(PreferenceStrings.BOLD_STYLE, [_control, "Shift", null, null, "B"]);
_preferences.set(PreferenceStrings.ITALIC_TAG, [_control, null, null, null, "I"]);
_preferences.set(PreferenceStrings.ITALIC_STYLE, [_control, "Shift", null, null, "I"]);
_preferences.set(PreferenceStrings.UNDERLINE_TAG, [_control, null, null, null, "U"]);
_preferences.set(PreferenceStrings.UNDERLINE_STYLE, [_control, "Shift", null, null, "U"]);
_preferences.set(PreferenceStrings.EMPTY_TAG, [_control, null, null, null, "T"]);
_preferences.set(PreferenceStrings.PREFERENCES, [_control, "Shift", null, null, ","]);
}
// set any tag/style input to have the default shortcut if it is undefined
// generate list of keyboard shortcuts for HTML Format Preferences screen
var prefString = "";
var prefStringProp;
for (prefStringProp in PreferenceStrings) {
if (PreferenceStrings.hasOwnProperty(prefStringProp)) {
if (_preferences.get(PreferenceStrings[prefStringProp]) === undefined) {
_preferences.set(PreferenceStrings[prefStringProp], _defaultTagStyleShortcut);
}
prefString += "<tr>";
prefString += "<td>";
prefString += "<div id='" + PreferenceStrings[prefStringProp] + "' class='shortcut'>";
prefString += "<div class='shortcut_label'>{{LABEL_" + prefStringProp + "_SHORTCUT}}:</div>";
prefString += "<div class='shortcut_modifiers'>";
if (_platform === "mac") {
prefString += "<label><input class='shortcut_modifier_ctrlCmd' type='checkbox' title='{{TITLE_CTRL}}' />{{MODIFIER_CMD}}</label>+";
} else {
prefString += "<label><input class='shortcut_modifier_ctrlCmd' type='checkbox' title='{{TITLE_CTRL}}' />{{MODIFIER_CONTROL_CMD}}</label>+";
}
prefString += "<label><input class='shortcut_modifier_shift' type='checkbox' title='{{TITLE_SHIFT}}' />{{MODIFIER_SHIFT}}</label>+";
prefString += "<label><input class='shortcut_modifier_alt' type='checkbox' title='{{TITLE_ALT}}' />{{MODIFIER_ALT}}</label>+";
if (_platform === "mac") {
prefString += "<label><input class='shortcut_modifier_ctrl' type='checkbox' title='{{TITLE_CTRL}}' />{{MODIFIER_CONTROL}}</label>+";
}
prefString += "<input class='shortcut_modifier_char' type='text' maxlength='1' size='1' title='{{TITLE_CHARACTER}}' />";
prefString += "</div>";
prefString += "</div>";
prefString += "</td>";
prefString += "</tr>";
}
}
_preferencePanel = _preferencePanel.replace("<tr id=\"shortcuts\"></tr>", prefString);
var _preferenceHTML = Mustache.render(_preferencePanel, Strings);
if (_preferences.get(AdditionalPrefStrings.BOLD_USES_STRONG) === undefined) {
_preferences.set(AdditionalPrefStrings.BOLD_USES_STRONG, false);
}
else {
_boldUsesStrong = _preferences.get(AdditionalPrefStrings.BOLD_USES_STRONG);
}
if (_preferences.get(AdditionalPrefStrings.ITALIC_USES_EM) === undefined) {
_preferences.set(AdditionalPrefStrings.ITALIC_USES_EM, false);
}
else {
_italicUsesEm = _preferences.get(AdditionalPrefStrings.ITALIC_USES_EM);
}
if (_preferences.get(AdditionalPrefStrings.ADD_RIGHT_CLICK) === undefined) {
_preferences.set(AdditionalPrefStrings.ADD_RIGHT_CLICK, false);
}
else {
_addRightClick = _preferences.get(AdditionalPrefStrings.ADD_RIGHT_CLICK);
}
//--------------------------------------------------------------------------------------------------------------------------------------------//
// FUNCTIONS
//--------------------------------------------------------------------------------------------------------------------------------------------//
//------------------------------------------------------------------------------------------------------------//
/**
* Generates the style needed based on the parameter
* @param {!string} style - The style to be checked for
* @private
*/
function _getStyle(style) {
var _style = "";
if (style === "bold" || style === "b" || style === "strong") {
_style = "font-weight:bold;";
}
else if (style === "italic" || style === "i" || style === "em") {
_style = "font-style:italic;";
}
else if (style === "underline" || style === "u") {
_style = "text-decoration:underline;";
}
else if (style === "strikethrough" || style === "s") {
_style = "text-decoration:line-through;";
}
else if (style === "monospace" || style === "tt") {
_style = "font-family:'Lucida Console', monospace;";
}
return _style;
}
//------------------------------------------------------------------------------------------------------------//
/**
* Checks if the cursor is within a style attribute. If it is, attempt to convert tag or style into inline style.
* @private
*/
function _checkIfWithinInlineStyle(style) {
var entireLine = _currentDoc.getLine(_editor.getCursorPos().line);
var styles = [];
var styleStart = 0;
var styleEnd;
var styleString = "";
var closingQuote;
var cursorPos = _editor.getCursorPos().ch;
while (styleStart !== -1) {
styleStart = entireLine.indexOf("style=", styleStart);
if (styleStart !== -1) {
closingQuote = entireLine.charAt(entireLine.indexOf("style=") + 6);// get quote type, single or double
styleStart += 7;// offset to start within the style attribute
styleEnd = entireLine.indexOf(closingQuote, styleStart);
styleString = entireLine.substring(styleStart, styleEnd);
styles.push({start:styleStart, end:styleEnd, style:styleString});
}
}
while (styles.length !== 0) {
if (cursorPos >= styles[styles.length-1].start && cursorPos <= styles[styles.length-1].end) {
if (style.length !== 0) {
var index = (cursorPos == styles[styles.length-1].start) ? styles[styles.length-1].start - 1 : -1;
// if not -1, meaning cursor was at beginning of the style, add a space to the string after the ";"
if (index !== -1) {
style = style + " ";
}
// else the cursor is not at the beginning of the style attribute
else {
var styleMod = "";
// set index to the last character of the styles
index = (cursorPos == styles[styles.length-1].end) ? styles[styles.length-1].end - 1 : -1;
// if index is -1, cursor was not at end of styles string. search for the closest ";" appearing before the cursor
if (index === -1) {
index = entireLine.lastIndexOf(";", cursorPos);
}
// cursor is at end of styles string and ";" was not detected at the end of the styles
else if (entireLine.charAt(index) !== ";") {
styleMod = ";";
}
// if index is -1, cursor was placed in middle of the first/only style
if (index === -1 || index < styles[styles.length-1].start) {
index = entireLine.indexOf(";", cursorPos);
}
// if index is -1, no ";" was found and you are a bad coder.
if (index === -1 || index > styles[styles.length-1].end) {
index = styles[styles.length-1].start-1;
}
// if index is not at the start, set style equal to styleMod + space + style, else set style equal to style + space
if (index !== styles[styles.length-1].start-1) {
style = styleMod + " " + style;
}
else {
style = style + " ";
}
}
index += 1;
_currentDoc.replaceRange(style, {line:_editor.getCursorPos().line, ch:index});
}
return false;
}
else {
styles.pop();
}
}
return true;
}
//------------------------------------------------------------------------------------------------------------//
/**
* Generates an HTML tag based on the param received.
* @param {string} style The HTML tag that should be generated
* @private
*/
function _addTag(tag) {
_currentDoc = DocumentManager.getCurrentDocument();
_editor = EditorManager.getCurrentFullEditor();
_lng = 0;// reset selected text length
var canAdd = _checkIfWithinInlineStyle(_getStyle(tag));
if (canAdd === true) {
// if copy is highlighted, surround copy in HTML tag.
if (_editor.hasSelection()) {
_selection = _editor.getSelection();
_lng = _selection.end.ch - _selection.start.ch;
_currentDoc.replaceRange("<" + tag + ">" + _editor.getSelectedText() + "</" + tag + ">", _selection.start, _selection.end);
_editor.setSelection({line: _selection.start.line, ch: (_selection.start.ch)}, _editor.getCursorPos());
}
// otherwise, insert HTML tag at cursor location and position the cursor in between the opening/closing tag
else {
_currentDoc.replaceRange("<" + tag + ">" + "</" + tag + ">", _editor.getCursorPos());
_editor.setCursorPos(_editor.getCursorPos().line, _editor.getCursorPos().ch - tag.length - 3);
}
}
// clear out variable references if not using the _insertEmptyTag() method.
if (tag !== "") {
_currentDoc = null;
_editor = null;
_selection = null;
_lng = 0;
}
}
//------------------------------------------------------------------------------------------------------------//
/**
* Generates an HTML <span> tag with a text style based on the param received.
* @param {!string} style - The type of style that should be applied to the <span> tag being generated
* @private
*/
function _addSpanStyle(style) {
_currentDoc = DocumentManager.getCurrentDocument();
_editor = EditorManager.getCurrentFullEditor();
var canAdd = _checkIfWithinInlineStyle(_getStyle(style));
if (canAdd === true) {
var _tagStart = "<span style=";
var _style = _getStyle(style);
var _tagEnd = "</span>";
if (_editor.hasSelection()) {
_selection = _editor.getSelection();
_currentDoc.replaceRange(_tagStart + "\"" + _style + "\"" + ">" + _editor.getSelectedText() + _tagEnd, _selection.start, _selection.end);
_editor.setSelection({line: _selection.start.line, ch: (_selection.start.ch)}, _editor.getCursorPos());
}
else {
_currentDoc.replaceRange(_tagStart + "\"" + _style + "\"" + ">" + _tagEnd, _editor.getCursorPos());
_editor.setCursorPos(_editor.getCursorPos().line, _editor.getCursorPos().ch - _tagEnd.length);
}
}
_currentDoc = null;
_editor = null;
_selection = null;
}
//------------------------------------------------------------------------------------------------------------//
/**
* Removes the Keyboard event listener for the _insertEmptyTag() method once the "Enter" key has been pressed.
* @private
*/
function _disposeAddTagListeners(listener) {
_editor.off("cursorActivity");
KeyBindingManager.removeGlobalKeydownHook(listener);
_lng = 0;
_currentDoc = null;
_currentPos = null;
_editor = null;
}
//------------------------------------------------------------------------------------------------------------//
/**
* Keyboard event listener function to capture key presses. Used for generating an open and close HTML tag.
* Pressing enter releases key press capture.
* @private
*/
function _keyboardListener(event) {
// get the current position of the cursor after the empty tag has been created
_currentPos = _editor.getCursorPos();
// If key pressed is the Enter key, empty tag action is complete so dispose of keyboard event.
if (event.keyCode === 13 || event.keyCode === 27) {
event.preventDefault();
event.stopPropagation();
_disposeAddTagListeners(_keyboardListener);
}
// if a key that was pressed is a letter, allow it to go through and duplicate it in the closing tag.
else if (event.keyCode > 64 && event.keyCode < 91) {
_currentDoc.replaceRange(String.fromCharCode(event.keyCode).toLowerCase(), {line: _currentPos.line, ch: _currentPos.ch + _lng + 3});
_emptyTagPos.open.end = {line:_currentPos.line, ch:_emptyTagPos.open.start.ch + 3 + _lng};
_emptyTagPos.close.start = {line:_currentPos.line, ch:_emptyTagPos.open.end.ch};
_emptyTagPos.close.end = {line:_currentPos.line, ch:_emptyTagPos.close.start.ch + 4 + _lng};
_lng += 1;
}
// If backspace deleting and the tag is just greater/less-than signs before backspace, stop empty tag action. Otherwise, delete character.
else if (event.keyCode === 8) {
if (_lng !== -1) {
_currentDoc.replaceRange("", {line: _currentPos.line, ch: _currentPos.ch + _lng + 2}, {line: _currentPos.line, ch: _currentPos.ch + _lng + 3});
_emptyTagPos.open.end = {line:_currentPos.line, ch:_emptyTagPos.open.start.ch + 1 + _lng};
_emptyTagPos.close.start = {line:_currentPos.line, ch:_emptyTagPos.open.end.ch};
_emptyTagPos.close.end = {line:_currentPos.line, ch:_emptyTagPos.close.start.ch + 2 + _lng};
}
else {
_disposeAddTagListeners(_keyboardListener);
}
_lng -= 1;
}
// If forward deleting and the tag is just greater/less-than signs, stop empty tag action. Otherwise, delete character.
else if (event.keyCode === 46) {
if (_lng !== -1) {
_currentDoc.replaceRange("", {line: _currentPos.line, ch: _currentPos.ch + _lng + 3}, {line: _currentPos.line, ch: _currentPos.ch + _lng + 4});
_emptyTagPos.open.end = {line:_currentPos.line, ch:_emptyTagPos.open.start.ch + 2 + _lng};
_emptyTagPos.close.start = {line:_currentPos.line, ch:_emptyTagPos.open.end.ch};
_emptyTagPos.close.end = {line:_currentPos.line, ch:_emptyTagPos.close.start.ch + 2 + _lng};
}
else {
_disposeAddTagListeners(_keyboardListener);
}
_lng -= 1;
}
// If the shortcut Control/Command + Z is pressed, end empty tag action.
else if (event.keyCode === 90 && (event.ctrlKey || event.metaKey)) {
_disposeAddTagListeners(_keyboardListener);
}
// If the key pressed is the up or down arrow key, end empty tag action.
else if (event.keyCode === 38 || event.keyCode === 40) {
_disposeAddTagListeners(_keyboardListener);
}
// If the key pressed is the left arrow key, check cursor position. if cursor position is outside the open tag, end empty tag action.
// Because this is a keyDown event, we must check to see what the character behind of the cursor currently is
else if (event.keyCode === 37 && _currentDoc.getRange({line:_currentPos.line, ch:_currentPos.ch - 1}, _currentPos) === "<") {
_disposeAddTagListeners(_keyboardListener);
}
// If the key pressed is the right arrow key, check cursor position. if cursor position is outside the open tag, end empty tag action.
// Because this is a keyDown event, we must check to see what the character ahead of the cursor currently is
else if (event.keyCode === 39 && _currentDoc.getRange(_currentPos,{line:_currentPos.line, ch:_currentPos.ch + 1}) === ">") {
_disposeAddTagListeners(_keyboardListener);
}
// If any other key pressed is not the left or right arrow key, prevent normal behavior
else if (event.keyCode !== 37 && event.keyCode !== 39) {
event.preventDefault();
event.stopPropagation();
event.stopImmediatePropagation();
}
}
//------------------------------------------------------------------------------------------------------------//
/**
* Listener for the cursor changing position while in the empty tag action. If the cursor leaves the open tag,
* end the empty tag action.
* @private
*/
function _onCursorChange(event) {
_currentPos = _editor.getCursorPos();
if (_currentPos.line !== _emptyTagPos.open.start.line || ((_currentPos.ch < _emptyTagPos.open.start.ch || _currentPos.ch > _emptyTagPos.open.end.ch) && _currentPos.line !== _emptyTagPos.open.start.line)) {
_disposeAddTagListeners(_keyboardListener);
}
}
//------------------------------------------------------------------------------------------------------------//
/**
* Uses _addTag() method to insert empty tag and gets Brackets ready for the user to begin typing
* to capture input
* @private
*/
function _insertEmptyTag() {
_addTag("");
var curPos = _editor.getCursorPos();
// reset empty tag position variable
_emptyTagPos = {
open:{
start:{
line:curPos.line, ch:curPos.ch - 2
},
end:{
line:curPos.line, ch:curPos.ch
}
},
close:{
start:{
line:curPos.line, ch:curPos.ch
},
end:{
line:curPos.line, ch:curPos.ch + 3 + _lng
}
}
};
// set open tag positions.
_emptyTagPos.open.start = {line:curPos.line, ch:curPos.ch - 2};
_emptyTagPos.open.end = {line:curPos.line, ch:curPos.ch};
// If length is zero, the move the cursor back 1 space to be inside the empty open tag
if (_lng === 0) {
_editor.setCursorPos(curPos.line, curPos.ch - 1);
}
// If length is not zero, then move the cursor back 4 spaces (to account for "></>") + the length of
// the highlighted word to be inside the empty open tag. Length (_lng) is known because its value is
// set when the _addTag() method is called
else {
_editor.setCursorPos(curPos.line, curPos.ch - (4 + _lng));
}
_editor.on("cursorActivity", _onCursorChange);
KeyBindingManager.addGlobalKeydownHook(_keyboardListener);
}
//------------------------------------------------------------------------------------------------------------//
/**
* Creates the string representation of the array of keyboard shortcut keys to use for a keyboard shortcut
* @private
*/
function _getShortcutString(shortcutArray) {
var string = ""; // create string to be used for the keyboard shortcut
shortcutArray.forEach(function (value, index) {
if (value !== undefined && value !== null) {
string += value;
// if not last entry, add "-" to string
if (index !== shortcutArray.length - 1) {
string += "-";
}
}
});
return string;
}
//------------------------------------------------------------------------------------------------------------//
/**
* Generate keyboard shortcut
* @private
*/
function _generateShortcut(shortcutArray) {
var string = _getShortcutString(shortcutArray);
var arr;
if (string !== "") {
arr = [{ "key": string, "platform": _platform }];
}
else {
arr = [];
}
return arr;
}
//------------------------------------------------------------------------------------------------------------//
/**
* @private
*/
function onBeforeContextMenuOpen(e) {
var selection = EditorManager.getCurrentFullEditor().getSelection();
if (selection.end.ch - selection.start.ch <= 2) {
EditorManager.getCurrentFullEditor().setCursorPos(selection.end.line, selection.end.ch);
}
}
//------------------------------------------------------------------------------------------------------------//
/**
* Adds all menu items to menu bar and enabled hotkeys
* @private
*/
function _addMenuItems() {
// Add HTML Format menu to menu bar
if (_htmlFormatMenu === null || _htmlFormatMenu === undefined) {
_htmlFormatMenu = Menus.addMenu(Strings.TITLE_MENU, HTML_FORMAT_MENU_COMMAND_ID, Menus.BEFORE, Menus.AppMenuBar.FIND_MENU);
// add some of the options to the right-click menu
if (_addRightClick === true) {
Menus.getContextMenu(Menus.ContextMenuIds.EDITOR_MENU).addMenuItem(BOLD_COMMAND_ID);
Menus.getContextMenu(Menus.ContextMenuIds.EDITOR_MENU).addMenuItem(ITALIC_COMMAND_ID);
Menus.getContextMenu(Menus.ContextMenuIds.EDITOR_MENU).addMenuItem(UNDERLINE_COMMAND_ID);
Menus.getContextMenu(Menus.ContextMenuIds.EDITOR_MENU).on("beforeContextMenuOpen", onBeforeContextMenuOpen);
}
_htmlFormatMenu.addMenuItem(BOLD_COMMAND_ID, _generateShortcut(_preferences.get(PreferenceStrings.BOLD_TAG)), Menus.LAST);
_htmlFormatMenu.addMenuItem(BOLD_STYLE_COMMAND_ID, _generateShortcut(_preferences.get(PreferenceStrings.BOLD_STYLE)), Menus.LAST);
_htmlFormatMenu.addMenuItem(ITALIC_COMMAND_ID, _generateShortcut(_preferences.get(PreferenceStrings.ITALIC_TAG)), Menus.LAST);
_htmlFormatMenu.addMenuItem(ITALIC_STYLE_COMMAND_ID, _generateShortcut(_preferences.get(PreferenceStrings.ITALIC_STYLE)), Menus.LAST);
_htmlFormatMenu.addMenuItem(UNDERLINE_COMMAND_ID, _generateShortcut(_preferences.get(PreferenceStrings.UNDERLINE_TAG)), Menus.LAST);
_htmlFormatMenu.addMenuItem(UNDERLINE_STYLE_COMMAND_ID, _generateShortcut(_preferences.get(PreferenceStrings.UNDERLINE_STYLE)), Menus.LAST);
_htmlFormatMenu.addMenuDivider();
_htmlFormatMenu.addMenuItem(ANCHOR_TAG_COMMAND_ID, _generateShortcut(_preferences.get(PreferenceStrings.ANCHOR_TAG)), Menus.LAST);
_htmlFormatMenu.addMenuDivider();
_htmlFormatMenu.addMenuItem(SPAN_COMMAND_ID, _generateShortcut(_preferences.get(PreferenceStrings.SPAN_TAG)), Menus.LAST);
_htmlFormatMenu.addMenuItem(DIV_COMMAND_ID, _generateShortcut(_preferences.get(PreferenceStrings.DIV_TAG)), Menus.LAST);
_htmlFormatMenu.addMenuItem(HEADER_COMMAND_ID, _generateShortcut(_preferences.get(PreferenceStrings.HEADER_TAG)), Menus.LAST);
_htmlFormatMenu.addMenuItem(NAV_COMMAND_ID, _generateShortcut(_preferences.get(PreferenceStrings.NAV_TAG)), Menus.LAST);
_htmlFormatMenu.addMenuItem(ARTICLE_COMMAND_ID, _generateShortcut(_preferences.get(PreferenceStrings.ARTICLE_TAG)), Menus.LAST);
_htmlFormatMenu.addMenuItem(SECTION_COMMAND_ID, _generateShortcut(_preferences.get(PreferenceStrings.SECTION_TAG)), Menus.LAST);
_htmlFormatMenu.addMenuItem(ASIDE_COMMAND_ID, _generateShortcut(_preferences.get(PreferenceStrings.ASIDE_TAG)), Menus.LAST);
_htmlFormatMenu.addMenuItem(FOOTER_COMMAND_ID, _generateShortcut(_preferences.get(PreferenceStrings.FOOTER_TAG)), Menus.LAST);
_htmlFormatMenu.addMenuDivider();
_htmlFormatMenu.addMenuItem(STRIKE_COMMAND_ID, _generateShortcut(_preferences.get(PreferenceStrings.STRIKE_TAG)), Menus.LAST);
_htmlFormatMenu.addMenuItem(STRIKE_STYLE_COMMAND_ID, _generateShortcut(_preferences.get(PreferenceStrings.STRIKE_STYLE)), Menus.LAST);
_htmlFormatMenu.addMenuItem(TELETYPE_COMMAND_ID, _generateShortcut(_preferences.get(PreferenceStrings.TELETYPE_TAG)), Menus.LAST);
_htmlFormatMenu.addMenuItem(TELETYPE_STYLE_COMMAND_ID, _generateShortcut(_preferences.get(PreferenceStrings.TELETYPE_TAG)), Menus.LAST);
_htmlFormatMenu.addMenuDivider();
_htmlFormatMenu.addMenuItem(CODE_COMMAND_ID, _generateShortcut(_preferences.get(PreferenceStrings.CODE_TAG)), Menus.LAST);
_htmlFormatMenu.addMenuItem(VARIABLE_COMMAND_ID, _generateShortcut(_preferences.get(PreferenceStrings.VARIABLE_TAG)), Menus.LAST);
_htmlFormatMenu.addMenuItem(SAMPLE_COMMAND_ID, _generateShortcut(_preferences.get(PreferenceStrings.SAMPLE_TAG)), Menus.LAST);
_htmlFormatMenu.addMenuItem(KEYBOARD_COMMAND_ID, _generateShortcut(_preferences.get(PreferenceStrings.KEYBOARD_TAG)), Menus.LAST);
_htmlFormatMenu.addMenuDivider();
_htmlFormatMenu.addMenuItem(CITATION_COMMAND_ID, _generateShortcut(_preferences.get(PreferenceStrings.CITATION_TAG)), Menus.LAST);
_htmlFormatMenu.addMenuItem(DEFINITION_COMMAND_ID, _generateShortcut(_preferences.get(PreferenceStrings.DEFINITION_TAG)), Menus.LAST);
_htmlFormatMenu.addMenuItem(DELETED_COMMAND_ID, _generateShortcut(_preferences.get(PreferenceStrings.DELETED_TAG)), Menus.LAST);
_htmlFormatMenu.addMenuItem(INSERTED_COMMAND_ID, _generateShortcut(_preferences.get(PreferenceStrings.INSERTED_TAG)), Menus.LAST);
_htmlFormatMenu.addMenuDivider();
_htmlFormatMenu.addMenuItem(INSERT_TAG_COMMAND_ID, _generateShortcut(_preferences.get(PreferenceStrings.EMPTY_TAG)), Menus.LAST);
_htmlFormatMenu.addMenuDivider();
_htmlFormatMenu.addMenuItem(PREFERENCE_COMMAND_ID, _generateShortcut(_preferences.get(PreferenceStrings.PREFERENCES)), Menus.LAST);
}
// enable hotkeys
_commands.forEach(function(command) {
command.setEnabled(true);
});
}
//------------------------------------------------------------------------------------------------------------//
/**
* Removes all menu items to menu bar and disables hotkeys
* @private
*/
function _removeMenuItems() {
// Disable all commands
_commands.forEach(function(command) {
command.setEnabled(false);
});
// remove right-click menu options
if (_addRightClick === true) {
Menus.getContextMenu(Menus.ContextMenuIds.EDITOR_MENU).removeMenuItem(BOLD_COMMAND_ID);
Menus.getContextMenu(Menus.ContextMenuIds.EDITOR_MENU).removeMenuItem(ITALIC_COMMAND_ID);
Menus.getContextMenu(Menus.ContextMenuIds.EDITOR_MENU).removeMenuItem(UNDERLINE_COMMAND_ID);
Menus.getContextMenu(Menus.ContextMenuIds.EDITOR_MENU).off("beforeContextMenuOpen");
}
// remove any previously set key bindings generated by Menu.addMenuItem()
var str = "";
var prop;
for (prop in PreferenceStrings) {
if (PreferenceStrings.hasOwnProperty(prop)) {
str = _getShortcutString(_preferences.get(PreferenceStrings[prop]));
if (str !== "") {
KeyBindingManager.removeBinding(str, _platform);
}
}
}
// Remove HTML Format menu from menu bar and set variable reference to null
Menus.removeMenu(HTML_FORMAT_MENU_COMMAND_ID);
_htmlFormatMenu = null;
}
//------------------------------------------------------------------------------------------------------------//
/**
* Gets HTML Format Preferences panel content to update preference values
* @private
*/
function _getPreferenceValues(target) {
var arr = [];
arr.push( ($("#" + target + " .shortcut_modifiers .shortcut_modifier_ctrlCmd").prop("checked")) ? _control : null );
arr.push( ($("#" + target + " .shortcut_modifiers .shortcut_modifier_shift").prop("checked")) ? "Shift" : null );
arr.push( ($("#" + target + " .shortcut_modifiers .shortcut_modifier_alt").prop("checked")) ? "Alt" : null );
if (_platform === "mac") {
arr.push( ($("#" + target + " .shortcut_modifiers .shortcut_modifier_ctrl").prop("checked")) ? "Ctrl" : null );
}
else {
arr.push(null);
}
arr.push( $("#" + target + " .shortcut_modifiers .shortcut_modifier_char").prop("value") );
return arr;
}
//------------------------------------------------------------------------------------------------------------//
/**
* Used for checking changed preferences against saved ones
* @private
*/
function _checkCurrentPrefernces(target) {
var pref = _getPreferenceValues(target);
var isInUse = false;
var prop;
for (prop in PreferenceStrings) {
if (PreferenceStrings.hasOwnProperty(prop)) {
$("#" + PreferenceStrings[prop]).css("background-color", "");// target all elements and reset
if (target !== PreferenceStrings[prop] && pref.toString() === _getPreferenceValues(PreferenceStrings[prop]).toString() &&
pref.toString() !== _defaultTagStyleShortcut.toString()) {
$("#" + PreferenceStrings[prop]).css("background-color", "#ff7777");
isInUse = true;
}
}
}
if (isInUse === true) {
$("#" + target).css("background-color", "#ff0000");
$("#btn_save").css("background-color", "#cc0000");
$("#btn_save").css("border-color", "#550000");
_duplicateShortcuts = true;
}
else {
$("#btn_save").css("background-color", "");
$("#btn_save").css("border-color", "");
_duplicateShortcuts = false;
}
}
//------------------------------------------------------------------------------------------------------------//
/**
* Update HTML Format Preferences panel content to match the saved preferences
* @private
*/
function _updatePreferenceHTML(target, pref) {
$("#" + target + " .shortcut_modifiers .shortcut_modifier_ctrlCmd").prop("checked", (pref[0] !== undefined && pref[0] !== null) ? true : false);
$("#" + target + " .shortcut_modifiers .shortcut_modifier_shift").prop("checked", (pref[1] !== undefined && pref[1] !== null) ? true : false);
$("#" + target + " .shortcut_modifiers .shortcut_modifier_alt").prop("checked", (pref[2] !== undefined && pref[2] !== null) ? true : false);
if (_platform === "mac") {
$("#" + target + " .shortcut_modifiers .shortcut_modifier_ctrl").prop("checked", (pref[3] !== undefined && pref[3] !== null) ? true : false);
}
$("#" + target + " .shortcut_modifiers .shortcut_modifier_char").prop("value", pref[4]);
}
//------------------------------------------------------------------------------------------------------------//
/**
* Gets HTML Format Preferences panel content to update preference values
* @private
*/
function _setPreferenceValues(target) {
var okToSave = false;
var arr = _getPreferenceValues(target);
// check to make sure at least one of the modifier keys has been set
if (arr[0] !== null || arr[1] !== null || arr[2] !== null || arr[3] !== null) {
// a modifier has been set, check to see if a character has been set
if (arr[4] !== "") {
okToSave = true;
}
}
// no modifier key is set, check to see if there is no letter set. If so, keyboard shortcut removed, allow save
else if (arr[4] === "") {
okToSave = true;
}
if (okToSave === true) {
_preferences.set(target, arr);
}
}
//------------------------------------------------------------------------------------------------------------//
/**
* Opens the preferences screen for user modification.
* @private
*/
function _openPreferencesPanel() {
Dialogs.showModalDialogUsingTemplate(_preferenceHTML);
$("#boldStrong input").prop("checked", _boldUsesStrong);
$("#italicEm input").prop("checked", _italicUsesEm);
$("#rightClick input").prop("checked", _addRightClick);
var prefStringProp;
for (prefStringProp in PreferenceStrings) {
if (PreferenceStrings.hasOwnProperty(prefStringProp)) {
_updatePreferenceHTML(PreferenceStrings[prefStringProp], _preferences.get(PreferenceStrings[prefStringProp]));
}
}
// listen for keypresses and checkboxes on any of the form elements within the preference panel
$("#preferenceForm").on("change", function (e) {
if (e.target.type === "checkbox" && $(e.target.parentElement.parentElement).hasClass("bold_italic") == false) {
_checkCurrentPrefernces(e.target.parentElement.parentElement.parentElement.getAttribute("id"));
}
});
$("#preferenceForm").on("keypress", function (e) {
if (e.target.type === "text") {
// If the character is a lowercase letter, display it as uppercase
if ((e.keyCode > 64 && e.keyCode < 91) || ((e.keyCode > 96 && e.keyCode < 123))) {
e.target.value = String.fromCharCode(e.keyCode).toUpperCase();
_checkCurrentPrefernces(e.target.parentElement.parentElement.getAttribute("id"));
}
// else if the character is not a number, grave accent, back/forward slash, bracket, brace, comma,
// period, minus, semicolon, single-quote, or equals, prevent it from being used
else if (e.keyCode < 65 || e.keyCode > 122) {
e.preventDefault();
}
}
});
$("#btn_save").on("click", function (e) {
_preferences.set(AdditionalPrefStrings.BOLD_USES_STRONG, $("#boldStrong input").prop("checked"));
_preferences.set(AdditionalPrefStrings.ITALIC_USES_EM, $("#italicEm input").prop("checked"));
_preferences.set(AdditionalPrefStrings.ADD_RIGHT_CLICK, $("#rightClick input").prop("checked"));
_boldUsesStrong = _preferences.get(AdditionalPrefStrings.BOLD_USES_STRONG);
_italicUsesEm = _preferences.get(AdditionalPrefStrings.ITALIC_USES_EM);
_addRightClick = _preferences.get(AdditionalPrefStrings.ADD_RIGHT_CLICK);
if (_duplicateShortcuts == false) {
var prefStringProp;
for (prefStringProp in PreferenceStrings) {
if (PreferenceStrings.hasOwnProperty(prefStringProp)) {
_setPreferenceValues(PreferenceStrings[prefStringProp]);
}