apply_delta.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /* ***** BEGIN LICENSE BLOCK *****
  2. * Distributed under the BSD license:
  3. *
  4. * Copyright (c) 2010, Ajax.org B.V.
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * * Neither the name of Ajax.org B.V. nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL AJAX.ORG B.V. BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * ***** END LICENSE BLOCK ***** */
  30. define(function(require, exports, module) {
  31. "use strict";
  32. function throwDeltaError(delta, errorText){
  33. console.log("Invalid Delta:", delta);
  34. throw "Invalid Delta: " + errorText;
  35. }
  36. function positionInDocument(docLines, position) {
  37. return position.row >= 0 && position.row < docLines.length &&
  38. position.column >= 0 && position.column <= docLines[position.row].length;
  39. }
  40. function validateDelta(docLines, delta) {
  41. // Validate action string.
  42. if (delta.action != "insert" && delta.action != "remove")
  43. throwDeltaError(delta, "delta.action must be 'insert' or 'remove'");
  44. // Validate lines type.
  45. if (!(delta.lines instanceof Array))
  46. throwDeltaError(delta, "delta.lines must be an Array");
  47. // Validate range type.
  48. if (!delta.start || !delta.end)
  49. throwDeltaError(delta, "delta.start/end must be an present");
  50. // Validate that the start point is contained in the document.
  51. var start = delta.start;
  52. if (!positionInDocument(docLines, delta.start))
  53. throwDeltaError(delta, "delta.start must be contained in document");
  54. // Validate that the end point is contained in the document (remove deltas only).
  55. var end = delta.end;
  56. if (delta.action == "remove" && !positionInDocument(docLines, end))
  57. throwDeltaError(delta, "delta.end must contained in document for 'remove' actions");
  58. // Validate that the .range size matches the .lines size.
  59. var numRangeRows = end.row - start.row;
  60. var numRangeLastLineChars = (end.column - (numRangeRows == 0 ? start.column : 0));
  61. if (numRangeRows != delta.lines.length - 1 || delta.lines[numRangeRows].length != numRangeLastLineChars)
  62. throwDeltaError(delta, "delta.range must match delta lines");
  63. }
  64. exports.applyDelta = function(docLines, delta, doNotValidate) {
  65. // disabled validation since it breaks autocompletion popup
  66. // if (!doNotValidate)
  67. // validateDelta(docLines, delta);
  68. var row = delta.start.row;
  69. var startColumn = delta.start.column;
  70. var line = docLines[row] || "";
  71. switch (delta.action) {
  72. case "insert":
  73. var lines = delta.lines;
  74. if (lines.length === 1) {
  75. docLines[row] = line.substring(0, startColumn) + delta.lines[0] + line.substring(startColumn);
  76. } else {
  77. var args = [row, 1].concat(delta.lines);
  78. docLines.splice.apply(docLines, args);
  79. docLines[row] = line.substring(0, startColumn) + docLines[row];
  80. docLines[row + delta.lines.length - 1] += line.substring(startColumn);
  81. }
  82. break;
  83. case "remove":
  84. var endColumn = delta.end.column;
  85. var endRow = delta.end.row;
  86. if (row === endRow) {
  87. docLines[row] = line.substring(0, startColumn) + line.substring(endColumn);
  88. } else {
  89. docLines.splice(
  90. row, endRow - row + 1,
  91. line.substring(0, startColumn) + docLines[endRow].substring(endColumn)
  92. );
  93. }
  94. break;
  95. }
  96. }
  97. });