in src/executable-code/executable-fragment.js [103:258]
update(state) {
let sample;
let hasMarkers = false;
let platform = state.targetPlatform;
if (
(state.compilerVersion && isJsRelated(platform)) ||
isWasmRelated(platform)
) {
this.jsExecutor = new JsExecutor(state.compilerVersion);
}
if (!state.shorterHeight) {
this.codemirror.display.wrapper.style.maxHeight = '';
}
if (state.code) {
let code = state.code;
if (
state.from &&
state.to &&
state.to >= state.from &&
state.from > 0 &&
state.to > 0
) {
let codeLines = code.split('\n');
codeLines.splice(state.from - 1, 0, SAMPLE_START);
codeLines.splice(state.to + 1, 0, SAMPLE_END);
code = codeLines.join('\n');
}
const startIndex = code.indexOf(SAMPLE_START);
const endIndex = code.indexOf(SAMPLE_END);
hasMarkers = !state.noneMarkers && startIndex > -1 && endIndex > -1;
this.prefix = '';
this.suffix = '';
sample = code;
this.canAddImport = true;
if (hasMarkers) {
this.prefix = code.substring(0, startIndex);
this.canAddImport = this.prefixEmptyOrContainsOnlyImports();
this.suffix = code.substring(endIndex + SAMPLE_END.length);
sample = code.substring(
startIndex + SAMPLE_START.length + 1,
endIndex - 1,
);
}
if (this.suffix.endsWith('\n')) {
this.suffix = this.suffix.substr(0, this.suffix.length - 1);
}
} else {
if (this.state.folded) {
sample = this.codemirror.getValue();
} else {
let editorValue = this.codemirror.getValue();
sample = editorValue.substring(
this.prefix.length,
editorValue.length - this.suffix.length,
);
}
}
this.state = merge.all([
this.state,
state,
{
isShouldBeFolded: this.isShouldBeFolded && state.isFoldedButton,
},
]);
super.update(this.state);
if (!this.initialized) {
this.initializeCodeMirror(state);
this.initialized = true;
} else {
this.showDiagnostics(state.errors);
if (state.folded === undefined) {
return;
}
}
if (this.state.folded) {
this.codemirror.setOption('lineNumbers', state.lines && !hasMarkers);
this.codemirror.setValue(sample);
this.markPlaceHolders();
} else {
this.codemirror.setOption('lineNumbers', true);
this.codemirror.setValue(this.prefix + sample + this.suffix);
this.codemirror.markText(
{ line: 0, ch: 0 },
{ line: countLines(this.prefix), ch: 0 },
{
readOnly: true,
inclusiveLeft: true,
inclusiveRight: false,
},
);
this.codemirror.markText(
{
line: this.codemirror.lineCount() - countLines(this.suffix) - 1,
ch: null,
},
{ line: this.codemirror.lineCount() - 1, ch: null },
{
readOnly: true,
inclusiveLeft: false,
inclusiveRight: true,
},
);
let readOnlyLineClass =
this.state.theme === THEMES.DARCULA
? SELECTORS.UNMODIFIABLE_LINE_DARK
: SELECTORS.UNMODIFIABLE_LINE;
this.codemirror.operation(() => {
for (let i = 0; i < countLines(this.prefix); i++) {
this.codemirror.addLineClass(
i,
SELECTORS.BACKGROUND,
readOnlyLineClass,
);
}
for (
let i = this.codemirror.lineCount() - countLines(this.suffix);
i < this.codemirror.lineCount();
i++
) {
this.codemirror.addLineClass(
i,
SELECTORS.BACKGROUND,
readOnlyLineClass,
);
}
});
}
if (this.state.autoIndent || (this.prefix && this.suffix)) {
for (let i = 0; i < this.codemirror.lineCount(); i++) {
this.codemirror.indentLine(i);
}
}
const shorterHeight = this.state.shorterHeight;
if (shorterHeight) {
const wrapper = this.codemirror.display.wrapper;
if (wrapper.getBoundingClientRect().height + 10 > shorterHeight) {
this.codemirror.display.wrapper.style.maxHeight = `${shorterHeight}px`;
} else {
super.update({ shorterHeight: 0 });
}
}
}