Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Kick off hawtio-editor directive and upgrade to latest CodeMirror
- Loading branch information
Showing
75 changed files
with
8,266 additions
and
2,226 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<div class="editor-autoresize"> | ||
<textarea name="{{name}}" ng-model="text"></textarea> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
module UI { | ||
|
||
export class Editor { | ||
|
||
public restrict = 'A'; | ||
public replace = true; | ||
|
||
public templateUrl = UI.templatePath + "editor.html"; | ||
|
||
public scope = { | ||
text: '=hawtioEditor', | ||
mode: '@', | ||
name: '@', | ||
readonly: '@', | ||
id: '@' | ||
}; | ||
|
||
public controller = ($scope, $element, $attrs) => { | ||
|
||
$scope.codeMirror = null; | ||
$scope.options = []; | ||
|
||
observe($scope, $attrs, 'name', 'editor'); | ||
observe($scope, $attrs, 'mode', 'text'); | ||
observe($scope, $attrs, 'readonly', 'false'); | ||
|
||
$scope.applyOptions = () => { | ||
if ($scope.codeMirror) { | ||
$scope.options.each(function(option) { | ||
$scope.codeMirror.setOption(option.key, option['value']); | ||
}); | ||
$scope.options = []; | ||
} | ||
} | ||
|
||
}; | ||
|
||
public link = ($scope, $element, $attrs) => { | ||
|
||
$scope.$watch('options.length', $scope.applyOptions); | ||
|
||
$scope.$watch('text', function() { | ||
if (!$scope.codeMirror) { | ||
|
||
var options:any = { | ||
value: $scope.text | ||
}; | ||
|
||
options = CodeEditor.createEditorSettings(options); | ||
$scope.codeMirror = CodeMirror.fromTextArea($element.find('textarea').get(0), options); | ||
$scope.applyOptions(); | ||
$scope.doc = $scope.codeMirror.getDoc(); | ||
$scope.codeMirror.on('change', function(changeObj) { | ||
$scope.text = $scope.doc.getValue(); | ||
$scope.$apply(); | ||
}); | ||
} | ||
}); | ||
|
||
$scope.$watch('mode', function() { | ||
if ($scope.mode) { | ||
$scope.options.push({ | ||
key: 'mode', | ||
'value': $scope.mode | ||
}); | ||
} | ||
}); | ||
|
||
$scope.$watch('readonly', function() { | ||
$scope.options.push({ | ||
key: 'readonly', | ||
'value': $scope.readonly | ||
}); | ||
}); | ||
}; | ||
|
||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
hawtio-web/src/main/webapp/lib/codemirror/addon/display/placeholder.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
(function() { | ||
CodeMirror.defineOption("placeholder", "", function(cm, val, old) { | ||
var prev = old && old != CodeMirror.Init; | ||
if (val && !prev) { | ||
cm.on("focus", onFocus); | ||
cm.on("blur", onBlur); | ||
cm.on("change", onChange); | ||
onChange(cm); | ||
} else if (!val && prev) { | ||
cm.off("focus", onFocus); | ||
cm.off("blur", onBlur); | ||
cm.off("change", onChange); | ||
clearPlaceholder(cm); | ||
var wrapper = cm.getWrapperElement(); | ||
wrapper.className = wrapper.className.replace(" CodeMirror-empty", ""); | ||
} | ||
|
||
if (val && !cm.hasFocus()) onBlur(cm); | ||
}); | ||
|
||
function clearPlaceholder(cm) { | ||
if (cm._placeholder) { | ||
cm._placeholder.parentNode.removeChild(cm._placeholder); | ||
cm._placeholder = null; | ||
} | ||
} | ||
function setPlaceholder(cm) { | ||
clearPlaceholder(cm); | ||
var elt = cm._placeholder = document.createElement("pre"); | ||
elt.style.cssText = "height: 0; overflow: visible"; | ||
elt.className = "CodeMirror-placeholder"; | ||
elt.appendChild(document.createTextNode(cm.getOption("placeholder"))); | ||
cm.display.lineSpace.insertBefore(elt, cm.display.lineSpace.firstChild); | ||
} | ||
|
||
function onFocus(cm) { | ||
clearPlaceholder(cm); | ||
} | ||
function onBlur(cm) { | ||
if (isEmpty(cm)) setPlaceholder(cm); | ||
} | ||
function onChange(cm) { | ||
var wrapper = cm.getWrapperElement(), empty = isEmpty(cm); | ||
wrapper.className = wrapper.className.replace(" CodeMirror-empty", "") + (empty ? " CodeMirror-empty" : ""); | ||
|
||
if (cm.hasFocus()) return; | ||
if (empty) setPlaceholder(cm); | ||
else clearPlaceholder(cm); | ||
} | ||
|
||
function isEmpty(cm) { | ||
return (cm.lineCount() === 1) && (cm.getLine(0) === ""); | ||
} | ||
})(); |
54 changes: 54 additions & 0 deletions
54
hawtio-web/src/main/webapp/lib/codemirror/addon/edit/closebrackets.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
(function() { | ||
var DEFAULT_BRACKETS = "()[]{}''\"\""; | ||
var SPACE_CHAR_REGEX = /\s/; | ||
|
||
CodeMirror.defineOption("autoCloseBrackets", false, function(cm, val, old) { | ||
var wasOn = old && old != CodeMirror.Init; | ||
if (val && !wasOn) | ||
cm.addKeyMap(buildKeymap(typeof val == "string" ? val : DEFAULT_BRACKETS)); | ||
else if (!val && wasOn) | ||
cm.removeKeyMap("autoCloseBrackets"); | ||
}); | ||
|
||
function buildKeymap(pairs) { | ||
var map = { | ||
name : "autoCloseBrackets", | ||
Backspace: function(cm) { | ||
if (cm.somethingSelected()) return CodeMirror.Pass; | ||
var cur = cm.getCursor(), line = cm.getLine(cur.line); | ||
if (cur.ch && cur.ch < line.length && | ||
pairs.indexOf(line.slice(cur.ch - 1, cur.ch + 1)) % 2 == 0) | ||
cm.replaceRange("", CodeMirror.Pos(cur.line, cur.ch - 1), CodeMirror.Pos(cur.line, cur.ch + 1)); | ||
else | ||
return CodeMirror.Pass; | ||
} | ||
}; | ||
var closingBrackets = []; | ||
for (var i = 0; i < pairs.length; i += 2) (function(left, right) { | ||
if (left != right) closingBrackets.push(right); | ||
function surround(cm) { | ||
var selection = cm.getSelection(); | ||
cm.replaceSelection(left + selection + right); | ||
} | ||
function maybeOverwrite(cm) { | ||
var cur = cm.getCursor(), ahead = cm.getRange(cur, CodeMirror.Pos(cur.line, cur.ch + 1)); | ||
if (ahead != right || cm.somethingSelected()) return CodeMirror.Pass; | ||
else cm.execCommand("goCharRight"); | ||
} | ||
map["'" + left + "'"] = function(cm) { | ||
if (left == "'" && cm.getTokenAt(cm.getCursor()).type == "comment") | ||
return CodeMirror.Pass; | ||
if (cm.somethingSelected()) return surround(cm); | ||
if (left == right && maybeOverwrite(cm) != CodeMirror.Pass) return; | ||
var cur = cm.getCursor(), ahead = CodeMirror.Pos(cur.line, cur.ch + 1); | ||
var line = cm.getLine(cur.line), nextChar = line.charAt(cur.ch); | ||
if (line.length == cur.ch || closingBrackets.indexOf(nextChar) >= 0 || SPACE_CHAR_REGEX.test(nextChar)) | ||
cm.replaceSelection(left + right, {head: ahead, anchor: ahead}); | ||
else | ||
return CodeMirror.Pass; | ||
}; | ||
if (left != right) map["'" + right + "'"] = maybeOverwrite; | ||
})(pairs.charAt(i), pairs.charAt(i + 1)); | ||
return map; | ||
} | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 20 additions & 23 deletions
43
hawtio-web/src/main/webapp/lib/codemirror/addon/edit/continuelist.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,25 @@ | ||
(function() { | ||
'use strict'; | ||
|
||
var listRE = /^(\s*)([*+-]|(\d+)\.)(\s*)/, | ||
unorderedBullets = '*+-'; | ||
|
||
CodeMirror.commands.newlineAndIndentContinueMarkdownList = function(cm) { | ||
var pos = cm.getCursor(), token = cm.getTokenAt(pos); | ||
var space; | ||
if (token.className == "string") { | ||
var full = cm.getRange({line: pos.line, ch: 0}, {line: pos.line, ch: token.end}); | ||
var listStart = /\*|\d+\./, listContinue; | ||
if (token.string.search(listStart) == 0) { | ||
var reg = /^[\W]*(\d+)\./g; | ||
var matches = reg.exec(full); | ||
if(matches) | ||
listContinue = (parseInt(matches[1]) + 1) + ". "; | ||
else | ||
listContinue = "* "; | ||
space = full.slice(0, token.start); | ||
if (!/^\s*$/.test(space)) { | ||
space = ""; | ||
for (var i = 0; i < token.start; ++i) space += " "; | ||
} | ||
} | ||
var pos = cm.getCursor(), | ||
inList = cm.getStateAfter(pos.line).list, | ||
match; | ||
|
||
if (!inList || !(match = cm.getLine(pos.line).match(listRE))) { | ||
cm.execCommand('newlineAndIndent'); | ||
return; | ||
} | ||
|
||
if (space != null) | ||
cm.replaceSelection("\n" + space + listContinue, "end"); | ||
else | ||
cm.execCommand("newlineAndIndent"); | ||
var indent = match[1], after = match[4]; | ||
var bullet = unorderedBullets.indexOf(match[2]) >= 0 | ||
? match[2] | ||
: (parseInt(match[3], 10) + 1) + '.'; | ||
|
||
cm.replaceSelection('\n' + indent + bullet + after, 'end'); | ||
}; | ||
})(); | ||
|
||
}()); |
Oops, something went wrong.