codekingpro/portable-devtools
114k
1/////////////////////////////////////////////////////////////2//3// pgAdmin 4 - PostgreSQL Tools4//5// Copyright (C) 2013 - 2024, The pgAdmin Development Team6// This software is released under the PostgreSQL Licence7//8//////////////////////////////////////////////////////////////9 10import { makeStyles } from '@mui/styles';11import PropTypes from 'prop-types';12 13import React, { useContext, useEffect } from 'react';14 15import gettext from 'sources/gettext';16import url_for from 'sources/url_for';17 18import getApiInstance from '../../../../../static/js/api_instance';19import CodeMirror from '../../../../../static/js/components/ReactCodeMirror';20import { DEBUGGER_EVENTS } from '../DebuggerConstants';21import { DebuggerEventsContext } from './DebuggerComponent';22import { usePgAdmin } from '../../../../../static/js/BrowserComponent';23 24 25const useStyles = makeStyles(() => ({26 sql: {27 height: '100%',28 }29}));30 31export default function DebuggerEditor({ getEditor, params }) {32 const classes = useStyles();33 const editor = React.useRef();34 const eventBus = useContext(DebuggerEventsContext);35 const pgAdmin = usePgAdmin();36 37 const api = getApiInstance();38 39 function setBreakpoint(lineNo, setType) {40 // Make ajax call to set/clear the break point by user41 let baseUrl = url_for('debugger.set_breakpoint', {42 'trans_id': params.transId,43 'line_no': lineNo,44 'set_type': setType,45 });46 api({47 url: baseUrl,48 method: 'GET',49 })50 .then(function(res) {51 if (res.data.data.status) {52 // Breakpoint has been set by the user53 }54 })55 .catch(function() {56 pgAdmin.Browser.notifier.alert(57 gettext('Debugger Error'),58 gettext('Error while setting debugging breakpoint.')59 );60 });61 }62 63 eventBus.registerListener(DEBUGGER_EVENTS.EDITOR_SET_SQL, (value, focus = true) => {64 focus && editor.current?.focus();65 editor.current?.setValue(value);66 });67 68 useEffect(() => {69 self = this;70 getEditor(editor.current);71 }, [editor.current]);72 73 return (74 <CodeMirror75 currEditor={(obj) => {76 editor.current = obj;77 }}78 value={''}79 onBreakPointChange={(line, on)=>{80 setBreakpoint(line, on ? 1 : 0);81 }}82 className={classes.sql}83 readonly={true}84 breakpoint85 />);86}87 88DebuggerEditor.propTypes = {89 getEditor: PropTypes.func,90 params: PropTypes.object91};92 