|
| 1 | +var TextDiffBinding = require('text-diff-binding'); |
| 2 | + |
| 3 | +module.exports = StringBinding; |
| 4 | + |
| 5 | +function StringBinding(element, doc, path) { |
| 6 | + TextDiffBinding.call(this, element); |
| 7 | + this.doc = doc; |
| 8 | + this.path = path || []; |
| 9 | + this._opListener = null; |
| 10 | + this._inputListener = null; |
| 11 | +} |
| 12 | +StringBinding.prototype = Object.create(TextDiffBinding.prototype); |
| 13 | +StringBinding.prototype.constructor = StringBinding; |
| 14 | + |
| 15 | +StringBinding.prototype.setup = function() { |
| 16 | + this.update(); |
| 17 | + this.attachDoc(); |
| 18 | + this.attachElement(); |
| 19 | +}; |
| 20 | + |
| 21 | +StringBinding.prototype.destroy = function() { |
| 22 | + this.detachElement(); |
| 23 | + this.detachDoc(); |
| 24 | +}; |
| 25 | + |
| 26 | +StringBinding.prototype.attachElement = function() { |
| 27 | + var binding = this; |
| 28 | + this._inputListener = function() { |
| 29 | + binding.onInput(); |
| 30 | + }; |
| 31 | + this.element.addEventListener('input', this._inputListener, false); |
| 32 | +}; |
| 33 | + |
| 34 | +StringBinding.prototype.detachElement = function() { |
| 35 | + this.element.removeEventListener('input', this._inputListener, false); |
| 36 | +}; |
| 37 | + |
| 38 | +StringBinding.prototype.attachDoc = function() { |
| 39 | + var binding = this; |
| 40 | + this._opListener = function(op, source) { |
| 41 | + binding._onOp(op, source); |
| 42 | + }; |
| 43 | + this.doc.on('op', this._opListener); |
| 44 | +}; |
| 45 | + |
| 46 | +StringBinding.prototype.detachDoc = function() { |
| 47 | + this.doc.removeListener('op', this._opListener); |
| 48 | +}; |
| 49 | + |
| 50 | +StringBinding.prototype._onOp = function(op, source) { |
| 51 | + if (source === this) return; |
| 52 | + if (op.length === 0) return; |
| 53 | + if (op.length > 1) { |
| 54 | + throw new Error('Op with multiple components emitted'); |
| 55 | + } |
| 56 | + var component = op[0]; |
| 57 | + if (isSubpath(this.path, component.p)) { |
| 58 | + this._parseInsertOp(component); |
| 59 | + this._parseRemoveOp(component); |
| 60 | + } else if (isSubpath(component.p, this.path)) { |
| 61 | + this._parseParentOp(); |
| 62 | + } |
| 63 | +}; |
| 64 | + |
| 65 | +StringBinding.prototype._parseInsertOp = function(component) { |
| 66 | + if (!component.si) return; |
| 67 | + var index = component.p[component.p.length - 1]; |
| 68 | + var length = component.si.length; |
| 69 | + this.onInsert(index, length); |
| 70 | +}; |
| 71 | + |
| 72 | +StringBinding.prototype._parseRemoveOp = function(component) { |
| 73 | + if (!component.sd) return; |
| 74 | + var index = component.p[component.p.length - 1]; |
| 75 | + var length = component.sd.length; |
| 76 | + this.onRemove(index, length); |
| 77 | +}; |
| 78 | + |
| 79 | +StringBinding.prototype._parseParentOp = function() { |
| 80 | + this.update(); |
| 81 | +}; |
| 82 | + |
| 83 | +StringBinding.prototype._get = function() { |
| 84 | + var value = this.doc.data; |
| 85 | + for (var i = 0; i < this.path.length; i++) { |
| 86 | + var segment = this.path[i]; |
| 87 | + value = value[segment]; |
| 88 | + } |
| 89 | + return value; |
| 90 | +}; |
| 91 | + |
| 92 | +StringBinding.prototype._insert = function(index, text) { |
| 93 | + var path = this.path.concat(index); |
| 94 | + var op = {p: path, si: text}; |
| 95 | + this.doc.submitOp(op, {source: this}); |
| 96 | +}; |
| 97 | + |
| 98 | +StringBinding.prototype._remove = function(index, text) { |
| 99 | + var path = this.path.concat(index); |
| 100 | + var op = {p: path, sd: text}; |
| 101 | + this.doc.submitOp(op, {source: this}); |
| 102 | +}; |
| 103 | + |
| 104 | +function isSubpath(path, testPath) { |
| 105 | + for (var i = 0; i < path.length; i++) { |
| 106 | + if (testPath[i] !== path[i]) return false; |
| 107 | + } |
| 108 | + return true; |
| 109 | +} |
0 commit comments