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

Commit

Permalink
Merge pull request #1012 from adobe/tvoliter/context-menus
Browse files Browse the repository at this point in the history
Merging
  • Loading branch information
tvoliter committed Jun 14, 2012
2 parents 53b27b4 + ebb511c commit ea3d26f
Show file tree
Hide file tree
Showing 12 changed files with 523 additions and 126 deletions.
5 changes: 3 additions & 2 deletions src/command/CommandManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,12 @@ define(function (require, exports, module) {
* execute() (after the id) are passed as arguments to the function. If the function is asynchronous,
* it must return a jQuery promise that is resolved when the command completes. Otherwise, the
* CommandManager will assume it is synchronous, and return a promise that is already resolved.
* @return {Command}
* @return {?Command}
*/
function register(name, id, commandFn) {
if (_commands[id]) {
throw new Error("Attempting to register an already-registered command: " + id);
console.log("Attempting to register an already-registered command: " + id);
return null;
}
if (!name || !id || !commandFn) {
throw new Error("Attempting to register a command with a missing name, id, or command function:" + name + " " + id);
Expand Down
383 changes: 305 additions & 78 deletions src/command/Menus.js

Large diffs are not rendered by default.

37 changes: 36 additions & 1 deletion src/editor/Editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -673,7 +673,34 @@ define(function (require, exports, module) {
Editor.prototype.setCursorPos = function (line, ch) {
this._codeMirror.setCursor(line, ch);
};


/**
* @param {line:number, ch:number}
* @return {number}
*/
Editor.prototype.indexFromPos = function (coords) {
return this._codeMirror.indexFromPos(coords);
};

/**
* Returns true if coords is between start and end (inclusive)
* @param {line:number, ch:number} coords
* @param {line:number, ch:number} start
* @param {line:number, ch:number} end
*
*/
Editor.prototype.coordsWithinRange = function (coords, start, end) {
var startIndex = this.indexFromPos(start),
endIndex = this.indexFromPos(end),
coordIndex = this.indexFromPos(coords);

return coordIndex >= startIndex && coordIndex <= endIndex;
};

Editor.prototype.coordsChar = function (coords) {
return this._codeMirror.coordsChar(coords);
};

/**
* Gets the current selection. Start is inclusive, end is exclusive. If there is no selection,
* returns the current cursor position as both the start and end of the range (i.e. a selection
Expand Down Expand Up @@ -704,6 +731,14 @@ define(function (require, exports, module) {
this._codeMirror.setSelection(start, end);
};

/**
* Selects a the word nearest to the position
* @param {line:number, ch:number}
*/
Editor.prototype.selectWordAt = function (pos) {
this._codeMirror.selectWordAt(pos);
};


/**
* Gets the total number of lines in the the document (includes lines not visible in the viewport)
Expand Down
2 changes: 1 addition & 1 deletion src/editor/InlineTextEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ define(function (require, exports, module) {

/**
* @constructor
*
* @extends {InlineWidget}
*/
function InlineTextEditor() {
InlineWidget.call(this);
Expand Down
103 changes: 103 additions & 0 deletions src/extensions/disabled/ContextMenuTest/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
* Copyright (c) 2012 Adobe Systems Incorporated. All rights reserved.
*
* 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, indent: 4, maxerr: 50 */
/*global define, brackets, $ */

define(function (require, exports, module) {
'use strict';

// Brackets modules
var CommandManager = brackets.getModule("command/CommandManager"),
EditorManager = brackets.getModule("editor/EditorManager"),
DocumentManager = brackets.getModule("document/DocumentManager"),
Menus = brackets.getModule("command/Menus");

function TestCommand1() {
var command1 = CommandManager.get("custom.command1");
if (!command1) {
return;
}
var command2 = CommandManager.get("custom.command2");
if (!command2) {
return;
}

var checked = command1.getChecked();
if (checked) {
alert("Unchecking self. Disabling next.");
command2.setEnabled(false);
} else {
alert("Checking self. Enabling next.");
command2.setEnabled(true);
}
command1.setChecked(!checked);
}

function TestCommand2() {
alert("Executing command 2");
}

function TestCommand3() {
alert("Executing command 3");
}

// Create command
var command1 = CommandManager.register("Toggle Checkmark", "custom.command1", TestCommand1);
var command2 = CommandManager.register("Enabled when previous is Checked", "custom.command2", TestCommand2);
var command3 = CommandManager.register("Enabled when text selected", "custom.command3", TestCommand3);

command1.setChecked(true);
command2.setEnabled(true);
command3.setEnabled(false);


var handleDocChanged = function () {
var editor = EditorManager.getFocusedEditor();

var handleEnableState = function () {
command3.setEnabled(editor.getSelectedText() !== "");
};

if (editor) {
$(editor).off("cursorActivity", handleEnableState);
$(editor).on("cursorActivity", handleEnableState);
}
};

$(DocumentManager).on("currentDocumentChange", handleDocChanged);
handleDocChanged();


// Get editor context menu
var editor_cmenu = Menus.getContextMenu(Menus.ContextMenuIds.EDITOR_MENU);

// Add our MenuItem at the end
if (editor_cmenu) {
editor_cmenu.addMenuDivider();
editor_cmenu.addMenuItem("custom.command1");
editor_cmenu.addMenuItem("custom.command2");
editor_cmenu.addMenuItem("custom.command3");
}
});
3 changes: 3 additions & 0 deletions src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -210,5 +210,8 @@ <h2>Brackets</h2>
<a href="#" class="dialog-button btn primary" data-button-id="ok">Close</a>
</div>
</div>
<div id="context-menu-bar">
<ul data-dropdown="dropdown"></ul>
</div>
</body>
</html>
2 changes: 1 addition & 1 deletion src/project/WorkingSetView.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ define(function (require, exports, module) {
_updateFileStatusIcon($newItem, isOpenAndDirty(file), false);
_updateListItemSelection($newItem, curDoc);

$newItem.click(function () {
$newItem.mousedown(function () {
FileViewController.openAndSelectDocument(file.fullPath, FileViewController.WORKING_SET_VIEW);
});

Expand Down
22 changes: 21 additions & 1 deletion src/styles/brackets_patterns_override.less
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@


/* Menu-related styles */
.toolbar .nav {
.toolbar .nav, .context-menu {
@menubar-top-padding: 8px;
@menubar-bottom-padding: 6px;
@menubar-h-padding: 9px;
Expand Down Expand Up @@ -258,6 +258,26 @@
}
}

/* Context menu styles */

.context-menu {
position: absolute;
z-index: @z-index-brackets-context-menu-base;
list-style-type: none;

.dropdown-menu {
.border-radius(3px);
}

.menu-shortcut {
float: right;
}
}

#context-menu-bar {
margin: 0;
}

/* Dialog-related styles */

.modal-footer .btn.left {
Expand Down
2 changes: 2 additions & 0 deletions src/styles/brackets_variables.less
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,5 @@

@z-index-brackets-sidebar-resizer: @z-index-brackets-ui + 2;
@z-index-brackets-resizer-div: @z-index-brackets-sidebar-resizer + 1;

@z-index-brackets-context-menu-base: 1000;
9 changes: 8 additions & 1 deletion src/utils/StringUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ define(function (require, exports, module) {
return str.replace(/([.?*+\^$\[\]\\(){}|\-])/g, "\\$1");
}

// Periods (aka "dots") are allowed in HTML identifiers, but jQuery interprets
// them as the start of a class selector, so they need to be escaped
function jQueryIdEscape(str) {
return str.replace(/\./g, "\\.");
}

/**
* Splits the text by new line characters and returns an array of lines
* @param {string} text
Expand Down Expand Up @@ -117,6 +123,7 @@ define(function (require, exports, module) {
exports.format = format;
exports.htmlEscape = htmlEscape;
exports.regexEscape = regexEscape;
exports.jQueryIdEscape = jQueryIdEscape;
exports.getLines = getLines;
exports.offsetToLineNum = offsetToLineNum;
});
});
2 changes: 1 addition & 1 deletion test/spec/CommandManager-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ define(function (require, exports, module) {
expect(command._commandFn).toBe(testCommandFn);

// duplicate command
expect(function () { CommandManager.register("test command", commandID, testCommandFn); }).toThrow();
expect(CommandManager.register("test command", commandID, testCommandFn)).toBeFalsy();

// missing arguments
expect(function () { CommandManager.register(null, "test-command-id2", testCommandFn); }).toThrow();
Expand Down
Loading

0 comments on commit ea3d26f

Please sign in to comment.