Maintain current selection when swapping editor for CodeMirror
This commit is contained in:
parent
1533a389b8
commit
f6ddf9443e
|
@ -30,6 +30,19 @@ define(['require'], (require) => {
|
||||||
events.forEach((event) => element.addEventListener(event, fn));
|
events.forEach((event) => element.addEventListener(event, fn));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function findPos(content, index) {
|
||||||
|
let p = 0;
|
||||||
|
let line = 0;
|
||||||
|
while(true) {
|
||||||
|
const nextLn = content.indexOf('\n', p) + 1;
|
||||||
|
if(index < nextLn || nextLn === 0) {
|
||||||
|
return {line, ch: index - p};
|
||||||
|
}
|
||||||
|
p = nextLn;
|
||||||
|
++ line;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function simplifyPreview(code) {
|
function simplifyPreview(code) {
|
||||||
code = code.replace(/\{Agent([0-9]*)\}/g, (match, num) => {
|
code = code.replace(/\{Agent([0-9]*)\}/g, (match, num) => {
|
||||||
if(num === undefined) {
|
if(num === undefined) {
|
||||||
|
@ -492,8 +505,13 @@ define(['require'], (require) => {
|
||||||
], (CodeMirror) => {
|
], (CodeMirror) => {
|
||||||
this.diagram.registerCodeMirrorMode(CodeMirror);
|
this.diagram.registerCodeMirrorMode(CodeMirror);
|
||||||
|
|
||||||
|
const selBegin = this.code.selectionStart;
|
||||||
|
const selEnd = this.code.selectionEnd;
|
||||||
|
const value = this.code.value;
|
||||||
|
const focussed = this.code === document.activeElement;
|
||||||
|
|
||||||
const code = new CodeMirror(this.code.parentNode, {
|
const code = new CodeMirror(this.code.parentNode, {
|
||||||
value: this.code.value,
|
value,
|
||||||
mode: 'sequence',
|
mode: 'sequence',
|
||||||
globals: {
|
globals: {
|
||||||
themes: this.diagram.getThemeNames(),
|
themes: this.diagram.getThemeNames(),
|
||||||
|
@ -511,6 +529,10 @@ define(['require'], (require) => {
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
this.code.parentNode.removeChild(this.code);
|
this.code.parentNode.removeChild(this.code);
|
||||||
|
code.getDoc().setSelection(
|
||||||
|
findPos(value, selBegin),
|
||||||
|
findPos(value, selEnd)
|
||||||
|
);
|
||||||
|
|
||||||
let lastKey = 0;
|
let lastKey = 0;
|
||||||
code.on('keydown', (cm, event) => {
|
code.on('keydown', (cm, event) => {
|
||||||
|
@ -539,6 +561,10 @@ define(['require'], (require) => {
|
||||||
this.diagram.setHighlight(Math.min(from, to));
|
this.diagram.setHighlight(Math.min(from, to));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if(focussed) {
|
||||||
|
code.focus();
|
||||||
|
}
|
||||||
|
|
||||||
this.code = code;
|
this.code = code;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,10 @@ define([], () => {
|
||||||
container,
|
container,
|
||||||
options,
|
options,
|
||||||
};
|
};
|
||||||
spy.doc = jasmine.createSpyObj('CodeMirror document', ['getValue']);
|
spy.doc = jasmine.createSpyObj('CodeMirror document', [
|
||||||
|
'getValue',
|
||||||
|
'setSelection',
|
||||||
|
]);
|
||||||
spy.getDoc = () => spy.doc;
|
spy.getDoc = () => spy.doc;
|
||||||
return spy;
|
return spy;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue