Commit 142ca1e5 authored by Alexander Trofimov's avatar Alexander Trofimov Committed by GitHub

Merge pull request #115 from ONLYOFFICE/feature/refactoring-spellcheck

Feature/refactoring spellcheck
parents 9ebbddd9 6ea048f5
......@@ -444,8 +444,7 @@
/**
* Event об отсоединении от сервера
* @param {jQuery} e event об отсоединении с причиной
* @param {Bool} isDisconnectAtAll окончательно ли отсоединяемся(true) или будем пробовать сделать reconnect(false) + сами отключились
* @param {Bool} isCloseCoAuthoring
* @param {Asc.c_oAscError.ID} errorCode
*/
CDocsCoApi.prototype.callback_OnDisconnect = function(e, errorCode) {
if (this.onDisconnect) {
......
/*
* (c) Copyright Ascensio System SIA 2010-2017
*
* This program is a free software product. You can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License (AGPL)
* version 3 as published by the Free Software Foundation. In accordance with
* Section 7(a) of the GNU AGPL its Section 15 shall be amended to the effect
* that Ascensio System SIA expressly excludes the warranty of non-infringement
* of any third-party rights.
*
* This program is distributed WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For
* details, see the GNU AGPL at: http://www.gnu.org/licenses/agpl-3.0.html
*
* You can contact Ascensio System SIA at Lubanas st. 125a-25, Riga, Latvia,
* EU, LV-1021.
*
* The interactive user interfaces in modified source and object code versions
* of the Program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU AGPL version 3.
*
* Pursuant to Section 7(b) of the License you must retain the original Product
* logo when distributing the program. Pursuant to Section 7(e) we decline to
* grant you any rights under trademark law for use of our trademarks.
*
* All the Product's GUI elements, including illustrations and icon sets, as
* well as technical writing content are licensed under the terms of the
* Creative Commons Attribution-ShareAlike 4.0 International. See the License
* terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode
*
*/
"use strict";
( /**
* @param {Window} window
* @param {undefined} undefined
*/
function (window, undefined) {
/*
* Import
* -----------------------------------------------------------------------------
*/
var prot;
/**
* Класс language для получения списка языков в проверке орфографии
* -----------------------------------------------------------------------------
* @constructor
* @memberOf Asc
* @param id
* @param name
* @return {*}
*/
function asc_CLanguage (name, id) {
this.name = name; // имя языка
this.id = id; // уникальный id языка
return this;
}
asc_CLanguage.prototype = {
constructor: asc_CLanguage,
asc_getId: function () { return this.id; },
asc_getName: function () { return this.name; },
asc_setId: function (val) { this.id = val; },
asc_setName: function (val) { this.name = val; }
};
//---------------------------------------------------------export---------------------------------------------------
window['AscCommon'] = window['AscCommon'] || {};
window["AscCommon"].asc_CLanguage = asc_CLanguage;
prot = asc_CLanguage.prototype;
prot["asc_getId"] = prot.asc_getId;
prot["asc_getName"] = prot.asc_getName;
prot["asc_setId"] = prot.asc_setId;
prot["asc_setName"] = prot.asc_setName;
}
)(window);
This diff is collapsed.
......@@ -32,42 +32,44 @@
"use strict";
(function(window) {
(function (window) {
'use strict';
// Класс надстройка, для online и offline работы
var CSpellCheckApi = function(options) {
var CSpellCheckApi = function () {
this._SpellCheckApi = new SpellCheckApi();
this._onlineWork = false;
if (options) {
this.onDisconnect = options.onDisconnect;
this.onSpellCheck = options.onSpellCheck;
}
this.onDisconnect = null;
this.onSpellCheck = null;
this.onSpellCheck = null;
};
CSpellCheckApi.prototype.init = function(docid) {
CSpellCheckApi.prototype.init = function (docid) {
if (this._SpellCheckApi && this._SpellCheckApi.isRightURL()) {
var t = this;
this._SpellCheckApi.onDisconnect = function(e, isDisconnectAtAll, isCloseCoAuthoring) {
this._SpellCheckApi.onDisconnect = function (e, isDisconnectAtAll, isCloseCoAuthoring) {
t.callback_OnDisconnect(e, isDisconnectAtAll, isCloseCoAuthoring);
};
this._SpellCheckApi.onSpellCheck = function(e) {
this._SpellCheckApi.onSpellCheck = function (e) {
t.callback_OnSpellCheck(e);
};
this._SpellCheckApi.onInit = function (e) {
t.callback_OnInit(e);
};
this._SpellCheckApi.init(docid);
this._onlineWork = true;
}
};
CSpellCheckApi.prototype.set_url = function(url) {
CSpellCheckApi.prototype.set_url = function (url) {
if (this._SpellCheckApi) {
this._SpellCheckApi.set_url(url);
}
};
CSpellCheckApi.prototype.get_state = function() {
CSpellCheckApi.prototype.get_state = function () {
if (this._SpellCheckApi) {
return this._SpellCheckApi.get_state();
}
......@@ -75,30 +77,35 @@
return 0;
};
CSpellCheckApi.prototype.disconnect = function() {
CSpellCheckApi.prototype.disconnect = function () {
if (this._SpellCheckApi && this._onlineWork) {
this._SpellCheckApi.disconnect();
}
};
CSpellCheckApi.prototype.spellCheck = function(spellCheckData) {
CSpellCheckApi.prototype.spellCheck = function (spellCheckData) {
if (this._SpellCheckApi && this._onlineWork) {
this._SpellCheckApi.spellCheck(spellCheckData);
}
};
CSpellCheckApi.prototype.callback_OnSpellCheck = function(e) {
CSpellCheckApi.prototype.callback_OnSpellCheck = function (e) {
if (this.onSpellCheck) {
return this.onSpellCheck(e);
}
};
CSpellCheckApi.prototype.callback_OnInit = function (e) {
if (this.onInit) {
return this.onInit(e);
}
};
/**
* Event об отсоединении от сервера
* @param {jQuery} e event об отсоединении с причиной
* @param {Bool} isDisconnectAtAll окончательно ли отсоединяемся(true) или будем пробовать сделать reconnect(false) + сами отключились
*/
CSpellCheckApi.prototype.callback_OnDisconnect = function(e, isDisconnectAtAll, isCloseCoAuthoring) {
CSpellCheckApi.prototype.callback_OnDisconnect = function (e, isDisconnectAtAll, isCloseCoAuthoring) {
if (this.onDisconnect) {
return this.onDisconnect(e, isDisconnectAtAll, isCloseCoAuthoring);
}
......@@ -110,15 +117,16 @@
* 1 - opened
* 3 - closed
*/
var SpellCheckApi = function(options) {
if (options) {
this.onDisconnect = options.onDisconnect;
this.onConnect = options.onConnect;
this.onSpellCheck = options.onSpellCheck;
}
var SpellCheckApi = function () {
this.onDisconnect = null;
this.onConnect = null;
this.onSpellCheck = null;
this.onInit = null;
this._state = 0;
// Мы сами отключились от совместного редактирования
this.isCloseCoAuthoring = false;
this.isInit = false;
// Массив данных, который стоит отправить как только подключимся
this.dataNeedSend = [];
......@@ -126,29 +134,29 @@
this._url = "";
};
SpellCheckApi.prototype.isRightURL = function() {
return ("" != this._url);
SpellCheckApi.prototype.isRightURL = function () {
return ("" !== this._url);
};
SpellCheckApi.prototype.set_url = function(url) {
SpellCheckApi.prototype.set_url = function (url) {
this._url = url;
};
SpellCheckApi.prototype.get_state = function() {
SpellCheckApi.prototype.get_state = function () {
return this._state;
};
SpellCheckApi.prototype.spellCheck = function(spellCheckData) {
SpellCheckApi.prototype.spellCheck = function (spellCheckData) {
this._send({"type": "spellCheck", "spellCheckData": spellCheckData});
};
SpellCheckApi.prototype.disconnect = function() {
SpellCheckApi.prototype.disconnect = function () {
// Отключаемся сами
this.isCloseCoAuthoring = true;
return this.sockjs.close();
};
SpellCheckApi.prototype._send = function(data) {
SpellCheckApi.prototype._send = function (data) {
if (data !== null && typeof data === "object") {
if (this._state > 0) {
this.sockjs.send(JSON.stringify(data));
......@@ -158,26 +166,33 @@
}
};
SpellCheckApi.prototype._sendAfterConnect = function() {
SpellCheckApi.prototype._sendAfterConnect = function () {
var data;
while (this._state > 0 && undefined !== (data = this.dataNeedSend.shift()))
this._send(data);
};
SpellCheckApi.prototype._onSpellCheck = function(data) {
if (undefined !== data["spellCheckData"] && this.onSpellCheck) {
SpellCheckApi.prototype._onSpellCheck = function (data) {
if (data["spellCheckData"] && this.onSpellCheck) {
this.onSpellCheck(data["spellCheckData"]);
}
};
SpellCheckApi.prototype._onInit = function (data) {
if (!this.isInit && data["languages"] && this.onInit) {
this.onInit(data["languages"]);
this.isInit = true;
}
};
var reconnectTimeout, attemptCount = 0;
function initSocksJs(url, docsCoApi) {
//ограничиваем transports WebSocket и XHR / JSONP polling, как и engine.io https://github.com/socketio/engine.io
//при переборе streaming transports у клиента с wirewall происходило зацикливание(не повторялось в версии sock.js 0.3.4)
var sockjs = new (_getSockJs())(url, null, {transports: ['websocket', 'xdr-polling', 'xhr-polling', 'iframe-xhr-polling', 'jsonp-polling']});
var sockjs = new (_getSockJs())(url, null,
{transports: ['websocket', 'xdr-polling', 'xhr-polling', 'iframe-xhr-polling', 'jsonp-polling']});
sockjs.onopen = function() {
sockjs.onopen = function () {
if (reconnectTimeout) {
clearTimeout(reconnectTimeout);
attemptCount = 0;
......@@ -191,7 +206,7 @@
docsCoApi._sendAfterConnect();
};
sockjs.onmessage = function(e) {
sockjs.onmessage = function (e) {
//TODO: add checks and error handling
//Get data type
var dataObject = JSON.parse(e.data);
......@@ -200,9 +215,12 @@
case 'spellCheck' :
docsCoApi._onSpellCheck(dataObject);
break;
case 'init':
docsCoApi._onInit(dataObject);
break;
}
};
sockjs.onclose = function(evt) {
sockjs.onclose = function (evt) {
docsCoApi._state = -1; // Reconnect state
var bIsDisconnectAtAll = attemptCount >= 20 || docsCoApi.isCloseCoAuthoring;
if (bIsDisconnectAtAll) {
......@@ -225,7 +243,7 @@
clearTimeout(reconnectTimeout);
}
attemptCount++;
reconnectTimeout = setTimeout(function() {
reconnectTimeout = setTimeout(function () {
delete docsCoApi.sockjs;
docsCoApi.sockjs = initSocksJs(url, docsCoApi);
}, 500 * attemptCount);
......@@ -239,15 +257,16 @@
return window['SockJS'] ? window['SockJS'] : require('sockjs');
}
SpellCheckApi.prototype.init = function(docid) {
SpellCheckApi.prototype.init = function (docid) {
this._docid = docid;
var re = /^https?:\/\//;
var spellcheckUrl = this._url + '/doc/' + docid + '/c'
var spellcheckUrl = this._url + '/doc/' + docid + '/c';
if(re.test(this._url))
if (re.test(this._url)) {
this.sockjs_url = spellcheckUrl;
else
} else {
this.sockjs_url = AscCommon.getBaseUrl() + "../../../.." + spellcheckUrl;
}
//Begin send auth
this.sockjs = initSocksJs(this.sockjs_url, this);
......
......@@ -446,7 +446,7 @@
this.tmpZoomType = null;
// Spell Checking
this.SpellCheckApi = (window["AscDesktopEditor"] === undefined) ? new AscCommon.CSpellCheckApi() : new CSpellCheckApi_desktop();
this.SpellCheckApi = new AscCommon.CSpellCheckApi();
this.isSpellCheckEnable = true;
// это чтобы сразу показать ридер, без возможности вернуться в редактор/вьюер
......@@ -1228,18 +1228,24 @@ background-repeat: no-repeat;\
}
var t = this;
if (!window["AscDesktopEditor"])
{
if (this.SpellCheckUrl && this.isSpellCheckEnable)
this.SpellCheckApi.set_url(this.SpellCheckUrl);
this.SpellCheckApi.onSpellCheck = function(e)
{
var incomeObject = JSON.parse(e);
t.SpellCheck_CallBack(incomeObject);
if (window["AscDesktopEditor"]) {
this.SpellCheckApi.spellCheck = function (spellData) {
window["AscDesktopEditor"]["SpellCheck"](spellData);
};
this.SpellCheckApi.disconnect = function () {
};
} else {
if (this.SpellCheckUrl && this.isSpellCheckEnable) {
this.SpellCheckApi.set_url(this.SpellCheckUrl);
}
}
this.SpellCheckApi.onInit = function (e) {
t.sendEvent('asc_onSpellCheckInit', e);
};
this.SpellCheckApi.onSpellCheck = function (e) {
t.SpellCheck_CallBack(e);
};
this.SpellCheckApi.init(this.documentId);
};
//----------------------------------------------------------------------------------------------------------------------
......@@ -1269,10 +1275,6 @@ background-repeat: no-repeat;\
}
};
asc_docs_api.prototype.asc_getSpellCheckLanguages = function()
{
return AscCommon.g_spellCheckLanguages;
};
asc_docs_api.prototype.asc_SpellCheckDisconnect = function()
{
if (!this.SpellCheckApi)
......@@ -7552,36 +7554,6 @@ background-repeat: no-repeat;\
}
};
// desktop editor spellcheck
function CSpellCheckApi_desktop()
{
this.docId = undefined;
this.init = function(docid)
{
this.docId = docid;
};
this.set_url = function(url)
{
};
this.spellCheck = function(spellData)
{
window["AscDesktopEditor"]["SpellCheck"](spellData);
};
this.onSpellCheck = function(spellData)
{
editor.SpellCheck_CallBack(spellData);
};
this.disconnect = function()
{
// none
};
}
window["AscDesktopEditor_Save"] = function()
{
return editor.asc_Save(false);
......@@ -7682,7 +7654,6 @@ background-repeat: no-repeat;\
asc_docs_api.prototype['asc_coAuthoringChatGetMessages'] = asc_docs_api.prototype.asc_coAuthoringChatGetMessages;
asc_docs_api.prototype['asc_coAuthoringGetUsers'] = asc_docs_api.prototype.asc_coAuthoringGetUsers;
asc_docs_api.prototype['asc_coAuthoringDisconnect'] = asc_docs_api.prototype.asc_coAuthoringDisconnect;
asc_docs_api.prototype['asc_getSpellCheckLanguages'] = asc_docs_api.prototype.asc_getSpellCheckLanguages;
asc_docs_api.prototype['asc_SpellCheckDisconnect'] = asc_docs_api.prototype.asc_SpellCheckDisconnect;
asc_docs_api.prototype['_onUpdateDocumentCanSave'] = asc_docs_api.prototype._onUpdateDocumentCanSave;
asc_docs_api.prototype['put_FramePr'] = asc_docs_api.prototype.put_FramePr;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment