Skip to content

Commit

Permalink
Merge pull request 'Macro: save the last cursor position' (#236) from…
Browse files Browse the repository at this point in the history
… fix/bugfix into release/v8.3.0
  • Loading branch information
Julia Radzhabova committed Jan 20, 2025
2 parents 69f32b7 + 3123e16 commit edb9b73
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 18 deletions.
8 changes: 5 additions & 3 deletions apps/common/main/lib/component/AceEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ define([], function () {
if (cmd && cmd.referer == "ace-editor") {
switch (cmd.command) {
case 'changeValue':
this.fireEvent('change', cmd.data);
data = cmd.data || {};
this.fireEvent('change', data.value, data.pos);
break;
case 'aceEditorReady':
this.fireEvent('ready', cmd.data);
Expand All @@ -126,13 +127,14 @@ define([], function () {
this.loadMask.hide();
},

setValue: function(value, readonly) {
setValue: function(value, currentPos, readonly) {
this._postMessage(this.iframe.contentWindow, {
command: 'setValue',
referer: 'ace-editor',
data: {
value: value,
readonly: readonly
readonly: readonly,
currentPos: currentPos
}
});
},
Expand Down
23 changes: 15 additions & 8 deletions apps/common/main/lib/view/MacrosDialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ define([], function () {
macrosItemMenuOpen: null,
functionItemMenuOpen: null,
currentElementMode: this.CurrentElementModeType.Macros,
currentValue: ''
currentValue: '',
currentPos: {row: 2, column: 0}
};

_options.tpl = _.template(this.template)({
Expand Down Expand Up @@ -259,19 +260,21 @@ define([], function () {
this.codeEditor = new Common.UI.AceEditor({parentEl: '#code-editor'});
this.codeEditor.on('ready', function() {
me.codeEditor.updateTheme();
me.codeEditor.setValue(me._state.currentValue);
me.codeEditor.setValue(me._state.currentValue, me._state.currentPos);
setTimeout(function() {
me.aceContainer.removeClass('invisible');
}, 10);
// me.loadMask.hide();
});
this.codeEditor.on('change', function(value) {
this.codeEditor.on('change', function(value, pos) {
var selectedItem = me._state.currentElementMode === me.CurrentElementModeType.Macros
? me.listMacros.getSelectedRec()
: me.listFunctions.getSelectedRec();
if(selectedItem) {
me._state.currentValue = value;
me._state.currentPos = pos;
selectedItem.set('value', value);
selectedItem.set('currentPos', pos);
}
});
},
Expand Down Expand Up @@ -313,6 +316,7 @@ define([], function () {
if(macrosList.length > 0) {
macrosList.forEach(function (macros) {
macros.autostart = !!macros.autostart;
macros.currentPos = {row: 2, column: 0};
});
this.listMacros.store.reset(macrosList);
var selectItem = this.listMacros.store.at(data.current);
Expand Down Expand Up @@ -631,8 +635,9 @@ define([], function () {
this.listMacros.store.add({
guid: this.createGuid(),
name : (macrosTextTranslate + " " + indexMax),
value : "(function()\n{\n})();",
autostart: false
value : "(function()\n{\n\n})();",
autostart: false,
currentPos: {row: 2, column: 0}
});
this.listMacros.selectRecord(this.listMacros.store.at(-1));
},
Expand All @@ -654,8 +659,9 @@ define([], function () {
},
onSelectListMacrosItem: function(listView, itemView, record) {
this._state.currentElementMode = this.CurrentElementModeType.Macros;
this.codeEditor.setValue(record.get('value'));
this.codeEditor.setValue(record.get('value'), record.get('currentPos')!==undefined ? record.get('currentPos') : {row: 2, column: 0});
this._state.currentValue = record.get('value');
this._state.currentPos = record.get('currentPos');

this.btnMacrosRun.setDisabled(false);
this.listFunctions && this.listFunctions.deselectAll();
Expand Down Expand Up @@ -747,7 +753,8 @@ define([], function () {
this.listFunctions.store.add({
guid: this.createGuid(),
name : (macrosTextTranslate + " " + indexMax),
value : "(function()\n{\n\t/**\n\t * Function that returns the argument\n\t * @customfunction\n\t * @param {any} arg Any data.\n * @returns {any} The argumet of the function.\n\t*/\n\tfunction myFunction(arg) {\n\t return arg;\n\t}\n\tApi.AddCustomFunction(myFunction);\n})();"
value : "(function()\n{\n\t/**\n\t * Function that returns the argument\n\t * @customfunction\n\t * @param {any} arg Any data.\n * @returns {any} The argumet of the function.\n\t*/\n\tfunction myFunction(arg) {\n\t\t\n\t return arg;\n\t}\n\tApi.AddCustomFunction(myFunction);\n})();",
currentPos: {row: 9, column: 2}
});
this.listFunctions.selectRecord(this.listFunctions.store.at(-1));
},
Expand All @@ -769,7 +776,7 @@ define([], function () {
},
onSelectListFunctionItem: function(listView, itemView, record) {
this._state.currentElementMode = this.CurrentElementModeType.CustomFunction;
this.codeEditor.setValue(record.get('value'));
this.codeEditor.setValue(record.get('value'), record.get('currentPos')!==undefined ? record.get('currentPos') : {row: 2, column: 0});

this.btnMacrosRun.setDisabled(true);

Expand Down
5 changes: 3 additions & 2 deletions vendor/ace/component/AceEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@
parentEl = document.getElementById(placeholderId),
iframe;

var _setValue = function(value, readonly) {
var _setValue = function(value, currentPos, readonly) {
_postMessage(iframe.contentWindow, {
command: 'setValue',
referer: 'ace-editor',
data: {
value: value,
readonly: readonly
readonly: readonly,
currentPos: currentPos
}
});
};
Expand Down
9 changes: 6 additions & 3 deletions vendor/ace/component/AceEditorCode.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,13 @@ ace.config.loadModule('ace/ext/tern', function () {
});
});

var firstLineNumber = 1;
if (!window.isIE) {
ace.config.loadModule('ace/ext/language_tools', function () {
editor.setOptions({
enableBasicAutocompletion: false,
enableLiveAutocompletion: true
enableLiveAutocompletion: true,
firstLineNumber: firstLineNumber
});
});
}
Expand All @@ -116,7 +118,7 @@ var _postMessage = function(msg) {
if (window.isDisable) return;
_postMessage({
command: 'changeValue',
data: editor.getValue(),
data: { value: editor.getValue(), pos: editor.getCursorPosition() },
referer: 'ace-editor'
});
});
Expand All @@ -130,7 +132,8 @@ var _postMessage = function(msg) {
if (!data.readonly) {
editor.focus();
editor.selection.clearSelection();
editor.scrollToRow(0);
editor.moveCursorToPosition(data.currentPos ? data.currentPos : {row: 0, column : 0});
editor.scrollToLine((data.currentPos ? data.currentPos.row : 0) + firstLineNumber, true);
}
window.isDisable = false;
};
Expand Down
4 changes: 2 additions & 2 deletions vendor/ace/component/example.html
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@
var aceEditor;

var onChangeValue = function (event) {
document.getElementById('editorLog').innerText = event.data;
console.log("onChangeValue");
document.getElementById('editorLog').innerText = event.data ? event.data.value : '';
console.log("onChangeValue at position(" + event.data.pos.row + ", " + event.data.pos.column + ")");
};

var onEditorReady = function (event) {
Expand Down

0 comments on commit edb9b73

Please sign in to comment.