Commit be4d574e authored by Alexander.Trofimov's avatar Alexander.Trofimov

fix find and replace

parent a409cd22
...@@ -1753,7 +1753,7 @@ ...@@ -1753,7 +1753,7 @@
this.isReplaceAll = false; // заменить все (если у нас замена) this.isReplaceAll = false; // заменить все (если у нас замена)
// внутренние переменные // внутренние переменные
this.activeRange = null; this.activeCell = null;
this.indexInArray = 0; this.indexInArray = 0;
this.countFind = 0; this.countFind = 0;
this.countReplace = 0; this.countReplace = 0;
...@@ -1774,7 +1774,7 @@ ...@@ -1774,7 +1774,7 @@
result.replaceWith = this.replaceWith; result.replaceWith = this.replaceWith;
result.isReplaceAll = this.isReplaceAll; result.isReplaceAll = this.isReplaceAll;
result.activeRange = this.activeRange ? this.activeRange.clone() : null; result.activeCell = this.activeCell ? this.activeCell.clone() : null;
result.indexInArray = this.indexInArray; result.indexInArray = this.indexInArray;
result.countFind = this.countFind; result.countFind = this.countFind;
result.countReplace = this.countReplace; result.countReplace = this.countReplace;
......
...@@ -2136,7 +2136,7 @@ ...@@ -2136,7 +2136,7 @@
// Поиск текста в листе // Поиск текста в листе
WorkbookView.prototype.findCellText = function(options) { WorkbookView.prototype.findCellText = function(options) {
// Для поиска эта переменная не нужна (но она может остаться от replace) // Для поиска эта переменная не нужна (но она может остаться от replace)
options.activeRange = null; options.activeCell = null;
var ws = this.getWorksheet(); var ws = this.getWorksheet();
// Останавливаем ввод данных в редакторе ввода // Останавливаем ввод данных в редакторе ввода
...@@ -2198,7 +2198,7 @@ ...@@ -2198,7 +2198,7 @@
} }
if (result) { if (result) {
return ws._setActiveCell(result.c1, result.r1); return ws.setSelection(result);
} }
this._cleanFindResults(); this._cleanFindResults();
return null; return null;
......
...@@ -10453,53 +10453,29 @@ ...@@ -10453,53 +10453,29 @@
// ----- Search ----- // ----- Search -----
WorksheetView.prototype._isCellEqual = function (c, r, options) {
WorksheetView.prototype._setActiveCell = function ( col, row ) {
var ar = this.activeRange, sc = ar.startCol, sr = ar.startRow, offs;
this.cleanSelection();
ar.assign( col, row, col, row );
ar.type = c_oAscSelectionType.RangeCells;
ar.startCol = col;
ar.startRow = row;
this._fixSelectionOfMergedCells();
this._fixSelectionOfHiddenCells();
this._drawSelection();
var x1 = this.getCellLeftRelative( this.activeRange.c1, /*pt*/0 ), y1 = this.getCellTopRelative( this.activeRange.r1, /*pt*/0 );
offs = this._calcActiveRangeOffset( x1, y1 );
if ( sc !== ar.startCol || sr !== ar.startRow ) {
this.handlers.trigger( "selectionNameChanged", this.getSelectionName( /*bRangeText*/false ) );
this.handlers.trigger( "selectionChanged", this.getSelectionInfo() );
}
return offs;
};
WorksheetView.prototype._isCellEqual = function ( c, r, options ) {
var cell, cellText; var cell, cellText;
// Не пользуемся RegExp, чтобы не возиться со спец.символами // Не пользуемся RegExp, чтобы не возиться со спец.символами
var mc = this.model.getMergedByCell( r, c ); var mc = this.model.getMergedByCell(r, c);
cell = mc ? this._getVisibleCell( mc.c1, mc.r1 ) : this._getVisibleCell( c, r ); cell = mc ? this._getVisibleCell(mc.c1, mc.r1) : this._getVisibleCell(c, r);
cellText = (options.lookIn === Asc.c_oAscFindLookIn.Formulas) ? cell.getValueForEdit() : cell.getValue(); cellText = (options.lookIn === Asc.c_oAscFindLookIn.Formulas) ? cell.getValueForEdit() : cell.getValue();
if ( true !== options.isMatchCase ) { if (true !== options.isMatchCase) {
cellText = cellText.toLowerCase(); cellText = cellText.toLowerCase();
} }
if ( (cellText.indexOf( options.findWhat ) >= 0) && (true !== options.isWholeCell || options.findWhat.length === cellText.length) ) { if ((cellText.indexOf(options.findWhat) >= 0) &&
return (mc ? new asc_Range( mc.c1, mc.r1, mc.c1, mc.r1 ) : new asc_Range( c, r, c, r )); (true !== options.isWholeCell || options.findWhat.length === cellText.length)) {
return (mc ? new asc_Range(mc.c1, mc.r1, mc.c1, mc.r1) : new asc_Range(c, r, c, r));
} }
return null; return null;
}; };
WorksheetView.prototype.findCellText = function ( options ) { WorksheetView.prototype.findCellText = function (options) {
var self = this; var self = this;
if ( true !== options.isMatchCase ) { if (true !== options.isMatchCase) {
options.findWhat = options.findWhat.toLowerCase(); options.findWhat = options.findWhat.toLowerCase();
} }
var ar = options.activeRange ? options.activeRange : this.activeRange; var ar = options.activeCell ? options.activeCell : this.model.selectionRange.activeCell;
var c = ar.startCol; var c = ar.col;
var r = ar.startRow; var r = ar.row;
var minC = 0; var minC = 0;
var minR = 0; var minR = 0;
var maxC = this.cols.length - 1; var maxC = this.cols.length - 1;
...@@ -10508,86 +10484,82 @@ ...@@ -10508,86 +10484,82 @@
var isEqual; var isEqual;
// ToDo стоит переделать это место, т.к. для поиска не нужны измерения, а нужен только сам текст (http://bugzilla.onlyoffice.com/show_bug.cgi?id=26136) // ToDo стоит переделать это место, т.к. для поиска не нужны измерения, а нужен только сам текст (http://bugzilla.onlyoffice.com/show_bug.cgi?id=26136)
this._prepareCellTextMetricsCache( new Asc.Range( 0, 0, this.model.getColsCount(), this.model.getRowsCount() ) ); this._prepareCellTextMetricsCache(new Asc.Range(0, 0, this.model.getColsCount(), this.model.getRowsCount()));
function findNextCell() { function findNextCell() {
var ct = undefined; var ct = undefined;
do { do {
if ( options.scanByRows ) { if (options.scanByRows) {
c += inc; c += inc;
if ( c < minC || c > maxC ) { if (c < minC || c > maxC) {
c = options.scanForward ? minC : maxC; c = options.scanForward ? minC : maxC;
r += inc; r += inc;
} }
} } else {
else {
r += inc; r += inc;
if ( r < minR || r > maxR ) { if (r < minR || r > maxR) {
r = options.scanForward ? minR : maxR; r = options.scanForward ? minR : maxR;
c += inc; c += inc;
} }
} }
if ( c < minC || c > maxC || r < minR || r > maxR ) { if (c < minC || c > maxC || r < minR || r > maxR) {
return undefined; return undefined;
} }
ct = self._getCellTextCache( c, r, true ); ct = self._getCellTextCache(c, r, true);
} while ( !ct ); } while (!ct);
return ct; return ct;
} }
while ( findNextCell() ) { while (findNextCell()) {
isEqual = this._isCellEqual( c, r, options ); isEqual = this._isCellEqual(c, r, options);
if ( null !== isEqual ) { if (null !== isEqual) {
return isEqual; return isEqual;
} }
} }
// Продолжаем циклический поиск // Продолжаем циклический поиск
if ( options.scanForward ) { if (options.scanForward) {
// Идем вперед с первой ячейки // Идем вперед с первой ячейки
minC = 0; minC = 0;
minR = 0; minR = 0;
if ( options.scanByRows ) { if (options.scanByRows) {
c = -1; c = -1;
r = 0; r = 0;
maxC = this.cols.length - 1; maxC = this.cols.length - 1;
maxR = ar.startRow; maxR = ar.row;
} } else {
else {
c = 0; c = 0;
r = -1; r = -1;
maxC = ar.startCol; maxC = ar.col;
maxR = this.rows.length - 1; maxR = this.rows.length - 1;
} }
} } else {
else {
// Идем назад с последней // Идем назад с последней
c = this.cols.length - 1; c = this.cols.length - 1;
r = this.rows.length - 1; r = this.rows.length - 1;
if ( options.scanByRows ) { if (options.scanByRows) {
minC = 0; minC = 0;
minR = ar.startRow; minR = ar.row;
} } else {
else { minC = ar.col;
minC = ar.startCol;
minR = 0; minR = 0;
} }
maxC = this.cols.length - 1; maxC = this.cols.length - 1;
maxR = this.rows.length - 1; maxR = this.rows.length - 1;
} }
while ( findNextCell() ) { while (findNextCell()) {
isEqual = this._isCellEqual( c, r, options ); isEqual = this._isCellEqual(c, r, options);
if ( null !== isEqual ) { if (null !== isEqual) {
return isEqual; return isEqual;
} }
} }
return null; return null;
}; };
WorksheetView.prototype.replaceCellText = function ( options, lockDraw, callback ) { WorksheetView.prototype.replaceCellText = function (options, lockDraw, callback) {
if ( true !== options.isMatchCase ) { if (true !== options.isMatchCase) {
options.findWhat = options.findWhat.toLowerCase(); options.findWhat = options.findWhat.toLowerCase();
} }
...@@ -10596,42 +10568,41 @@ ...@@ -10596,42 +10568,41 @@
options.countReplace = 0; options.countReplace = 0;
var t = this; var t = this;
var ar = this.activeRange.clone(); var activeCell = this.model.selectionRange.activeCell.clone();
var aReplaceCells = []; var aReplaceCells = [];
if ( options.isReplaceAll ) { if (options.isReplaceAll) {
var aReplaceCellsIndex = {}; var aReplaceCellsIndex = {};
options.activeRange = ar; options.activeCell = activeCell;
var findResult, index; var findResult, index;
while ( true ) { while (true) {
findResult = t.findCellText( options ); findResult = t.findCellText(options);
if ( null === findResult ) { if (null === findResult) {
break; break;
} }
index = findResult.c1 + '-' + findResult.r1; index = findResult.c1 + '-' + findResult.r1;
if ( aReplaceCellsIndex[index] ) { if (aReplaceCellsIndex[index]) {
break; break;
} }
aReplaceCellsIndex[index] = true; aReplaceCellsIndex[index] = true;
aReplaceCells.push( findResult ); aReplaceCells.push(findResult);
ar.startCol = findResult.c1; activeCell.col = findResult.c1;
ar.startRow = findResult.r1; activeCell.row = findResult.r1;
} }
} } else {
else {
// Попробуем сначала найти // Попробуем сначала найти
var isEqual = this._isCellEqual( ar.startCol, ar.startRow, options ); var isEqual = this._isCellEqual(activeCell.col, activeCell.row, options);
if ( null === isEqual ) { if (null === isEqual) {
return callback( options ); return callback(options);
} }
aReplaceCells.push( isEqual ); aReplaceCells.push(isEqual);
} }
if ( 0 > aReplaceCells.length ) { if (0 > aReplaceCells.length) {
return callback( options ); return callback(options);
} }
return this._replaceCellsText( aReplaceCells, options, lockDraw, callback ); return this._replaceCellsText(aReplaceCells, options, lockDraw, callback);
}; };
WorksheetView.prototype._replaceCellsText = function (aReplaceCells, options, lockDraw, callback) { WorksheetView.prototype._replaceCellsText = function (aReplaceCells, options, lockDraw, callback) {
...@@ -10668,47 +10639,49 @@ ...@@ -10668,47 +10639,49 @@
} }
}; };
WorksheetView.prototype._replaceCellText = function ( aReplaceCells, valueForSearching, options, lockDraw, callback, oneUser ) { WorksheetView.prototype._replaceCellText =
var t = this; function (aReplaceCells, valueForSearching, options, lockDraw, callback, oneUser) {
if ( options.indexInArray >= aReplaceCells.length ) { var t = this;
this.draw( lockDraw ); if (options.indexInArray >= aReplaceCells.length) {
return callback( options ); this.draw(lockDraw);
} return callback(options);
}
var onReplaceCallback = function ( isSuccess ) {
var cell = aReplaceCells[options.indexInArray];
++options.indexInArray;
if ( false !== isSuccess ) {
++options.countReplace;
var c = t._getVisibleCell( cell.c1, cell.r1 );
if ( c === undefined ) { var onReplaceCallback = function (isSuccess) {
asc_debug( "log", "Unknown cell's info: col = " + cell.c1 + ", row = " + cell.r1 ); var cell = aReplaceCells[options.indexInArray];
} ++options.indexInArray;
else { if (false !== isSuccess) {
var cellValue = c.getValueForEdit(); ++options.countReplace;
cellValue = cellValue.replace( valueForSearching, options.replaceWith );
var oCellEdit = new asc_Range( cell.c1, cell.r1, cell.c1, cell.r1 ); var c = t._getVisibleCell(cell.c1, cell.r1);
var v, newValue;
// get first fragment and change its text
v = c.getValueForEdit2().slice( 0, 1 );
// Создаем новый массив, т.к. getValueForEdit2 возвращает ссылку
newValue = [];
newValue[0] = new AscCommonExcel.Fragment( {text: cellValue, format: v[0].format.clone()} );
t._saveCellValueAfterEdit( oCellEdit, c, newValue, /*flags*/undefined, /*skipNLCheck*/false, /*isNotHistory*/true, /*lockDraw*/true ); if (c === undefined) {
} asc_debug("log", "Unknown cell's info: col = " + cell.c1 + ", row = " + cell.r1);
} } else {
var cellValue = c.getValueForEdit();
cellValue = cellValue.replace(valueForSearching, options.replaceWith);
var oCellEdit = new asc_Range(cell.c1, cell.r1, cell.c1, cell.r1);
var v, newValue;
// get first fragment and change its text
v = c.getValueForEdit2().slice(0, 1);
// Создаем новый массив, т.к. getValueForEdit2 возвращает ссылку
newValue = [];
newValue[0] = new AscCommonExcel.Fragment({text: cellValue, format: v[0].format.clone()});
t._saveCellValueAfterEdit(oCellEdit, c, newValue, /*flags*/undefined, /*skipNLCheck*/false,
/*isNotHistory*/true, /*lockDraw*/true);
}
}
window.setTimeout( function () { window.setTimeout(function () {
t._replaceCellText( aReplaceCells, valueForSearching, options, lockDraw, callback, oneUser ); t._replaceCellText(aReplaceCells, valueForSearching, options, lockDraw, callback, oneUser);
}, 1 ); }, 1);
}; };
return oneUser ? onReplaceCallback( true ) : this._isLockedCells( aReplaceCells[options.indexInArray], /*subType*/null, onReplaceCallback ); return oneUser ? onReplaceCallback(true) :
}; this._isLockedCells(aReplaceCells[options.indexInArray], /*subType*/null, onReplaceCallback);
};
WorksheetView.prototype.findCell = function (reference, isViewerMode) { WorksheetView.prototype.findCell = function (reference, isViewerMode) {
var mc, bNew = false; var mc, bNew = false;
......
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