gulf-editor-socialcalc
Version:
Gulf bindings for the socialcalc spreadsheet editor
139 lines (108 loc) • 5.06 kB
JavaScript
;
var _promise = require('babel-runtime/core-js/promise');
var _promise2 = _interopRequireDefault(_promise);
var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of');
var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf);
var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
var _createClass2 = require('babel-runtime/helpers/createClass');
var _createClass3 = _interopRequireDefault(_createClass2);
var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');
var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);
var _get2 = require('babel-runtime/helpers/get');
var _get3 = _interopRequireDefault(_get2);
var _inherits2 = require('babel-runtime/helpers/inherits');
var _inherits3 = _interopRequireDefault(_inherits2);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/**
* gulf binding for SocialCalc spreadsheet editor (using ot-socialcalc)
* Copyright (C) 2015 Marcel Klehr <mklehr@gmx.net>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
var gulf = require('gulf'),
socialcalcOT = require('ot-socialcalc'),
SocialCalc = require('socialcalc');
require('./socialcalc_patches');
var SocialcalcDocument = function (_gulf$EditableDocumen) {
(0, _inherits3.default)(SocialcalcDocument, _gulf$EditableDocumen);
function SocialcalcDocument(opts) {
(0, _classCallCheck3.default)(this, SocialcalcDocument);
opts.ottype = opts.ottype || socialcalcOT;
var _this = (0, _possibleConstructorReturn3.default)(this, (SocialcalcDocument.__proto__ || (0, _getPrototypeOf2.default)(SocialcalcDocument)).call(this, opts));
if (!opts.editorInstance) throw new Error('No SocialCalc instance was passed');
_this.socialcalcControl = opts.editorInstance;
SocialCalc.Callbacks.broadcast = function (type, data) {
if ('execute' !== type) return;
var changes = _this.ottype.deserializeEdit(data.cmdstr);
_this.submitChange(changes);
};
return _this;
}
(0, _createClass3.default)(SocialcalcDocument, [{
key: 'close',
value: function close() {
(0, _get3.default)(SocialcalcDocument.prototype.__proto__ || (0, _getPrototypeOf2.default)(SocialcalcDocument.prototype), 'close', this).call(this);
SocialCalc.Callbacks.broadcast = function () {};
}
}, {
key: '_setContent',
value: function _setContent(newcontent) {
socialCalcControl.sheet.ParseSheetSave(newcontent);
socialCalcControl.sheet.ScheduleSheetCommands('recalc', /*saveundo:*/false, /*isRemote:*/false);
return _promise2.default.resolve();
}
}, {
key: '_onChange',
value: function _onChange(changes) {
var _this2 = this;
var editor = socialCalcControl.editor;
// Remember old selection
var oldSel = editor.range.hasrange && SocialCalc.crToCoord(editor.range.left, editor.range.top) + ":" + SocialCalc.crToCoord(editor.range.right, editor.range.bottom),
oldECell = editor.ecell.coord;
// Apply changes
var cmds = this.ottype.serializeEdit(changes),
cb,
promise = new _promise2.default(function (resolve) {
return cb = resolve;
});
socialCalcControl.sheet.ScheduleSheetCommands(cmds, /*saveundo:*/false, /*isRemote:*/true);
socialCalcControl.editor.StatusCallback['gulf-editor-socialcalc#_onChange'] = {
func: function func(editor, status) {
if ('cmdend' !== status) return;
delete socialCalcControl.editor.StatusCallback['gulf-editor-socialcalc#_onChange'];
// commands exectuted!
cb();
}
};
return promise.then(function () {
// Restore transformed selection
var newECell = _this2.ottype.transformCursor(oldECell, changes);
editor.MoveECell(newECell);
var newSel = _this2.ottype.transformCursor(oldSel, changes),
newSelSplit = newSel.split(':');
editor.RangeAnchor(newSelSplit[0]);
editor.RangeExtend(newSelSplit[1]);
});
}
}, {
key: '_onBeforeChange',
value: function _onBeforeChange() {
// changes are automatically collected
return _promise2.default.resolve();
}
}]);
return SocialcalcDocument;
}(gulf.EditableDocument);
module.exports = SocialcalcDocument;