Skip to content
This repository was archived by the owner on Sep 6, 2021. It is now read-only.

Commit ea3d26f

Browse files
committed
Merge pull request #1012 from adobe/tvoliter/context-menus
Merging
2 parents 53b27b4 + ebb511c commit ea3d26f

12 files changed

Lines changed: 523 additions & 126 deletions

File tree

src/command/CommandManager.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,11 +166,12 @@ define(function (require, exports, module) {
166166
* execute() (after the id) are passed as arguments to the function. If the function is asynchronous,
167167
* it must return a jQuery promise that is resolved when the command completes. Otherwise, the
168168
* CommandManager will assume it is synchronous, and return a promise that is already resolved.
169-
* @return {Command}
169+
* @return {?Command}
170170
*/
171171
function register(name, id, commandFn) {
172172
if (_commands[id]) {
173-
throw new Error("Attempting to register an already-registered command: " + id);
173+
console.log("Attempting to register an already-registered command: " + id);
174+
return null;
174175
}
175176
if (!name || !id || !commandFn) {
176177
throw new Error("Attempting to register a command with a missing name, id, or command function:" + name + " " + id);

src/command/Menus.js

Lines changed: 305 additions & 78 deletions
Large diffs are not rendered by default.

src/editor/Editor.js

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,34 @@ define(function (require, exports, module) {
673673
Editor.prototype.setCursorPos = function (line, ch) {
674674
this._codeMirror.setCursor(line, ch);
675675
};
676-
676+
677+
/**
678+
* @param {line:number, ch:number}
679+
* @return {number}
680+
*/
681+
Editor.prototype.indexFromPos = function (coords) {
682+
return this._codeMirror.indexFromPos(coords);
683+
};
684+
685+
/**
686+
* Returns true if coords is between start and end (inclusive)
687+
* @param {line:number, ch:number} coords
688+
* @param {line:number, ch:number} start
689+
* @param {line:number, ch:number} end
690+
*
691+
*/
692+
Editor.prototype.coordsWithinRange = function (coords, start, end) {
693+
var startIndex = this.indexFromPos(start),
694+
endIndex = this.indexFromPos(end),
695+
coordIndex = this.indexFromPos(coords);
696+
697+
return coordIndex >= startIndex && coordIndex <= endIndex;
698+
};
699+
700+
Editor.prototype.coordsChar = function (coords) {
701+
return this._codeMirror.coordsChar(coords);
702+
};
703+
677704
/**
678705
* Gets the current selection. Start is inclusive, end is exclusive. If there is no selection,
679706
* returns the current cursor position as both the start and end of the range (i.e. a selection
@@ -704,6 +731,14 @@ define(function (require, exports, module) {
704731
this._codeMirror.setSelection(start, end);
705732
};
706733

734+
/**
735+
* Selects a the word nearest to the position
736+
* @param {line:number, ch:number}
737+
*/
738+
Editor.prototype.selectWordAt = function (pos) {
739+
this._codeMirror.selectWordAt(pos);
740+
};
741+
707742

708743
/**
709744
* Gets the total number of lines in the the document (includes lines not visible in the viewport)

src/editor/InlineTextEditor.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ define(function (require, exports, module) {
7373

7474
/**
7575
* @constructor
76-
*
76+
* @extends {InlineWidget}
7777
*/
7878
function InlineTextEditor() {
7979
InlineWidget.call(this);
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
* Copyright (c) 2012 Adobe Systems Incorporated. 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+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20+
* DEALINGS IN THE SOFTWARE.
21+
*
22+
*/
23+
24+
25+
/*jslint vars: true, plusplus: true, devel: true, nomen: true, indent: 4, maxerr: 50 */
26+
/*global define, brackets, $ */
27+
28+
define(function (require, exports, module) {
29+
'use strict';
30+
31+
// Brackets modules
32+
var CommandManager = brackets.getModule("command/CommandManager"),
33+
EditorManager = brackets.getModule("editor/EditorManager"),
34+
DocumentManager = brackets.getModule("document/DocumentManager"),
35+
Menus = brackets.getModule("command/Menus");
36+
37+
function TestCommand1() {
38+
var command1 = CommandManager.get("custom.command1");
39+
if (!command1) {
40+
return;
41+
}
42+
var command2 = CommandManager.get("custom.command2");
43+
if (!command2) {
44+
return;
45+
}
46+
47+
var checked = command1.getChecked();
48+
if (checked) {
49+
alert("Unchecking self. Disabling next.");
50+
command2.setEnabled(false);
51+
} else {
52+
alert("Checking self. Enabling next.");
53+
command2.setEnabled(true);
54+
}
55+
command1.setChecked(!checked);
56+
}
57+
58+
function TestCommand2() {
59+
alert("Executing command 2");
60+
}
61+
62+
function TestCommand3() {
63+
alert("Executing command 3");
64+
}
65+
66+
// Create command
67+
var command1 = CommandManager.register("Toggle Checkmark", "custom.command1", TestCommand1);
68+
var command2 = CommandManager.register("Enabled when previous is Checked", "custom.command2", TestCommand2);
69+
var command3 = CommandManager.register("Enabled when text selected", "custom.command3", TestCommand3);
70+
71+
command1.setChecked(true);
72+
command2.setEnabled(true);
73+
command3.setEnabled(false);
74+
75+
76+
var handleDocChanged = function () {
77+
var editor = EditorManager.getFocusedEditor();
78+
79+
var handleEnableState = function () {
80+
command3.setEnabled(editor.getSelectedText() !== "");
81+
};
82+
83+
if (editor) {
84+
$(editor).off("cursorActivity", handleEnableState);
85+
$(editor).on("cursorActivity", handleEnableState);
86+
}
87+
};
88+
89+
$(DocumentManager).on("currentDocumentChange", handleDocChanged);
90+
handleDocChanged();
91+
92+
93+
// Get editor context menu
94+
var editor_cmenu = Menus.getContextMenu(Menus.ContextMenuIds.EDITOR_MENU);
95+
96+
// Add our MenuItem at the end
97+
if (editor_cmenu) {
98+
editor_cmenu.addMenuDivider();
99+
editor_cmenu.addMenuItem("custom.command1");
100+
editor_cmenu.addMenuItem("custom.command2");
101+
editor_cmenu.addMenuItem("custom.command3");
102+
}
103+
});

src/index.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,5 +210,8 @@ <h2>Brackets</h2>
210210
<a href="#" class="dialog-button btn primary" data-button-id="ok">Close</a>
211211
</div>
212212
</div>
213+
<div id="context-menu-bar">
214+
<ul data-dropdown="dropdown"></ul>
215+
</div>
213216
</body>
214217
</html>

src/project/WorkingSetView.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ define(function (require, exports, module) {
167167
_updateFileStatusIcon($newItem, isOpenAndDirty(file), false);
168168
_updateListItemSelection($newItem, curDoc);
169169

170-
$newItem.click(function () {
170+
$newItem.mousedown(function () {
171171
FileViewController.openAndSelectDocument(file.fullPath, FileViewController.WORKING_SET_VIEW);
172172
});
173173

src/styles/brackets_patterns_override.less

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@
160160

161161

162162
/* Menu-related styles */
163-
.toolbar .nav {
163+
.toolbar .nav, .context-menu {
164164
@menubar-top-padding: 8px;
165165
@menubar-bottom-padding: 6px;
166166
@menubar-h-padding: 9px;
@@ -258,6 +258,26 @@
258258
}
259259
}
260260

261+
/* Context menu styles */
262+
263+
.context-menu {
264+
position: absolute;
265+
z-index: @z-index-brackets-context-menu-base;
266+
list-style-type: none;
267+
268+
.dropdown-menu {
269+
.border-radius(3px);
270+
}
271+
272+
.menu-shortcut {
273+
float: right;
274+
}
275+
}
276+
277+
#context-menu-bar {
278+
margin: 0;
279+
}
280+
261281
/* Dialog-related styles */
262282

263283
.modal-footer .btn.left {

src/styles/brackets_variables.less

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,5 @@
5050

5151
@z-index-brackets-sidebar-resizer: @z-index-brackets-ui + 2;
5252
@z-index-brackets-resizer-div: @z-index-brackets-sidebar-resizer + 1;
53+
54+
@z-index-brackets-context-menu-base: 1000;

src/utils/StringUtils.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ define(function (require, exports, module) {
6262
return str.replace(/([.?*+\^$\[\]\\(){}|\-])/g, "\\$1");
6363
}
6464

65+
// Periods (aka "dots") are allowed in HTML identifiers, but jQuery interprets
66+
// them as the start of a class selector, so they need to be escaped
67+
function jQueryIdEscape(str) {
68+
return str.replace(/\./g, "\\.");
69+
}
70+
6571
/**
6672
* Splits the text by new line characters and returns an array of lines
6773
* @param {string} text
@@ -117,6 +123,7 @@ define(function (require, exports, module) {
117123
exports.format = format;
118124
exports.htmlEscape = htmlEscape;
119125
exports.regexEscape = regexEscape;
126+
exports.jQueryIdEscape = jQueryIdEscape;
120127
exports.getLines = getLines;
121128
exports.offsetToLineNum = offsetToLineNum;
122-
});
129+
});

0 commit comments

Comments
 (0)