Поэтому я использую редактор codemirror для текстовых кодов, в настоящее время я включил редактор javascript, чтобы пользователи могли вводить ObjectId, так как иногда пользователь вводит запрос в Mongodb Compass и вставляет сюда на входе, так как я могу безопасно преобразовать объект js в mongodb запрос
Код: Выделить всё
initJsonEditor() {
if (this.jsonEditorView) {
this.jsonEditorView.destroy();
}
let editorTarget: ElementRef | null = null;
let form: FormGroup | null = null;
if (this.selected_widget_type === DASHBOARD_CONSTANTS.html_content) {
editorTarget = this.htmlQueryEditor;
form = this.html_form;
} else if (this.selected_widget_type === DASHBOARD_CONSTANTS.chart) {
editorTarget = this.chartQueryEditor;
form = this.chart_form;
} else if (this.selected_widget_type === DASHBOARD_CONSTANTS.table) {
editorTarget = this.tableQueryEditor;
form = this.table_form;
}
if (!editorTarget?.nativeElement) {
setTimeout(() => this.initJsonEditor(), 50);
return;
}
if (!editorTarget || !form) return;
this.jsonEditorErrorMessage = null;
this.jsonEditorView = new EditorView({
parent: editorTarget.nativeElement,
state: EditorState.create({
doc: form.get("query")?.value || "",
extensions: [
basicSetup,
oneDark,
javascript({
jsx : false,
typescript : false,
}),
lintGutter(),
// this.jsonLint(),
EditorView.updateListener.of((update) => {
if (update.docChanged) {
form.get("query")?.setValue(update.state.doc.toString());
}
}),
],
}),
});
}
И при отправке формы я проверяю корректность этого синтаксического анализа
Код: Выделить всё
const fn = new Function (
const ObjectId = (id) => ( { $oid : id });
return query
)
Подробнее здесь: https://stackoverflow.com/questions/798 ... ry-as-pars
Мобильная версия