Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Code review cleanup. addInlineWidget/removeInlineWidget() now use IDs…

… rather than the actual internal data structures.
  • Loading branch information...
commit 8ed8efe5309356da91c04cfdb1b50f44c0e062d0 1 parent 77a9770
@njx njx authored
Showing with 62 additions and 36 deletions.
  1. +25 −25 demo/inline.html
  2. +37 −11 lib/codemirror.js
View
50 demo/inline.html
@@ -27,31 +27,31 @@
return find(1, "1");
}</textarea></form>
-<p>
- <input type="button" value="Add Inline Widget" onclick="addInline()"/>
- <input type="button" value="Remove Last Inline Widget" onclick="removeInline()"/>
- <input type="checkbox" id="wordwrap" onclick="toggleWordWrap()"/><label for="wordwrap">Word Wrap</label>
-</p>
+ <p>
+ <input type="button" value="Add Inline Widget" onclick="addInline()"/>
+ <input type="button" value="Remove Last Inline Widget" onclick="removeInline()"/>
+ <input type="checkbox" id="wordwrap" onclick="toggleWordWrap()"/><label for="wordwrap">Word Wrap</label>
+ </p>
-<script>
- var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
- lineNumbers: true,
- mode: "javascript"
- });
- var inlines = [], nextWidgetId = 1;
- function addInline() {
- var inlineContent = document.createElement('div');
- inlineContent.style.background = '#CCCCCC';
- inlineContent.innerHTML = "Inline editor #" + nextWidgetId++;
- inlines.push(editor.addInlineWidget(editor.getCursor(), inlineContent, 100));
- }
- function removeInline() {
- if (inlines.length > 0)
- editor.removeInlineWidget(inlines.pop());
- }
- function toggleWordWrap() {
- editor.setOption("lineWrapping", !editor.getOption("lineWrapping"));
- }
-</script>
+ <script>
+ var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
+ lineNumbers: true,
+ mode: "javascript"
+ });
+ var inlines = [], nextWidgetId = 1;
+ function addInline() {
+ var inlineContent = document.createElement('div');
+ inlineContent.style.background = '#CCCCCC';
+ inlineContent.innerHTML = "Inline editor #" + nextWidgetId++;
+ inlines.push(editor.addInlineWidget(editor.getCursor(), inlineContent, 100));
+ }
+ function removeInline() {
+ if (inlines.length > 0)
+ editor.removeInlineWidget(inlines.pop());
+ }
+ function toggleWordWrap() {
+ editor.setOption("lineWrapping", !editor.getOption("lineWrapping"));
+ }
+ </script>
</body>
</html>
View
48 lib/codemirror.js
@@ -258,6 +258,7 @@ var CodeMirror = (function() {
addInlineWidget: addInlineWidget,
removeInlineWidget: removeInlineWidget,
+ setInlineWidgetHeight: setInlineWidgetHeight,
lineCount: function() {return doc.size;},
clipPos: clipPos,
@@ -1053,7 +1054,7 @@ var CodeMirror = (function() {
}
}
for (i = 0; i < destroyedInlines.length; i++) {
- removeInlineWidget(destroyedInlines[i], true);
+ removeInlineWidget(destroyedInlines[i].id, true);
}
// Since this is all rather error prone, it is honoured with the
@@ -1497,10 +1498,16 @@ var CodeMirror = (function() {
function addInlineWidget(pos, node, initialHeight) {
// If there's already an inline widget for the given line, remove it
var i;
- for (i = 0; i < inlines.length; i++) {
- if (inlines[i].exists() && inlines[i].getLineNumber() == pos.line) {
- removeInlineWidget(inlines[i]);
- break;
+ for (i = 0; i < inlines.length ; i++) {
+ if (inlines[i].exists()) {
+ var lineNum = inlines[i].getLineNumber();
+ if (lineNum == pos.line) {
+ removeInlineWidget(inlines[i].id);
+ break;
+ }
+ else if (lineNum > pos.line) {
+ break;
+ }
}
}
@@ -1526,12 +1533,12 @@ var CodeMirror = (function() {
updateDisplay(true);
var inlineTop = inlineWidget.node.offsetTop + displayOffsetPx - paddingTop();
scrollIntoView(0, inlineTop, 0, inlineTop + inlineWidget.height);
- return inlineWidget;
+ return inlineWidget.id;
}
- function removeInlineWidget(inlineWidget, inUpdate) {
+ function removeInlineWidget(id, inUpdate) {
for (var i = 0; i < inlines.length; i++) {
- if (inlines[i] === inlineWidget)
+ if (inlines[i].id == id)
break;
}
if (i < inlines.length) {
@@ -1546,15 +1553,34 @@ var CodeMirror = (function() {
}
}
}
+
+ function setInlineWidgetHeight(id, height) {
+ var widget = getInlineWidgetById(id);
+ if (widget)
+ widget.setHeight(height);
+ }
function isInlinePlaceholder(node) {
return node && node.getAttribute(inlinePlaceholderAttr);
}
+
+ function getInlineWidgetById(id) {
+ for (var i = 0; i < inlines.length; i++) {
+ if (inlines[i].exists() && inlines[i].id == id)
+ return inlines[i];
+ }
+ return null;
+ }
function getInlineWidgetForLine(line) {
+ var lineNum = lineNo(line);
for (var i = 0; i < inlines.length; i++) {
- if (inlines[i].exists() && inlines[i].getLine() === line)
- return inlines[i];
+ if (inlines[i].exists()) {
+ if (inlines[i].getLine() === line)
+ return inlines[i];
+ else if (inlines[i].getLineNumber() > lineNum)
+ break;
+ }
}
return null;
}
@@ -1684,7 +1710,7 @@ var CodeMirror = (function() {
// Add the height of each inline widget that is before the given line.
for (var i = 0; i < inlines.length; i++) {
var inline = inlines[i];
- if (inlines[i].exists() && inline.getLineNumber() < pos.line)
+ if (inline.exists() && inline.getLineNumber() < pos.line)
y += inline.height;
else
break;
Please sign in to comment.
Something went wrong with that request. Please try again.