UNPKG

@flatbiz/antd

Version:
120 lines (116 loc) 4.49 kB
/*! @flatjs/forge MIT @flatbiz/antd */ import { a as _slicedToArray, _ as _objectWithoutProperties, b as _objectSpread2 } from './_rollupPluginBabelHelpers-BYm17lo8.js'; import { classNames } from '@dimjs/utils/class-names/class-names'; import { hooks } from '@wove/react/hooks'; import { useState, useRef } from 'react'; import Ace from 'react-ace'; import { format } from 'sql-formatter'; import { B as ButtonWrapper } from './button-wrapper-l9FUsrWl.js'; import { E as ErrorBoundaryWrapper } from './error-boundary-DtShAEtQ.js'; import 'ace-builds/src-noconflict/ext-language_tools.js'; import 'ace-builds/src-noconflict/mode-mysql.js'; import 'ace-builds/src-noconflict/snippets/mysql.js'; import { jsxs, jsx } from 'react/jsx-runtime'; var _excluded = ["value", "hiddenFormatterBtn", "autoCompleterList", "onChange", "onLoad"]; var formatSql = function formatSql(sqlContent) { return format(sqlContent, { language: 'sql' }).replace(/\$ /g, '$').replace(/\{ /g, '{').replace(/ \}/g, '}'); }; var AceEditorMysql = function AceEditorMysql(props) { var _useState = useState(Date.now()), _useState2 = _slicedToArray(_useState, 2), rootNodekey = _useState2[0], setRootNodekey = _useState2[1]; var value = props.value, hiddenFormatterBtn = props.hiddenFormatterBtn, autoCompleterList = props.autoCompleterList, onChange = props.onChange, onLoad = props.onLoad, otherProps = _objectWithoutProperties(props, _excluded); var editorRef = useRef(); var handleChange = hooks.useCallbackRef(function (content) { onChange === null || onChange === void 0 || onChange(content); }); var getCompletions = hooks.useCallbackRef(function (_a, _b, _c, _d, callback) { callback(null, autoCompleterList === null || autoCompleterList === void 0 ? void 0 : autoCompleterList.map(function (item) { return { name: item.name, value: item.name, // score: 100, meta: item.desc }; })); }); var onLoadHandle = function onLoadHandle(editor) { editorRef.current = editor; /** 向编辑器中添加自动补全列表 */ var findIndex = editor.completers.findIndex(function (item) { return item.id === 'custom'; }); if (findIndex >= 0) { editor.completers[findIndex] = { getCompletions: getCompletions, id: 'custom' }; } else { editor.completers.push({ getCompletions: getCompletions, id: 'custom' }); } onLoad === null || onLoad === void 0 || onLoad(editor); }; var footer = /*#__PURE__*/jsx(ButtonWrapper, { hidden: hiddenFormatterBtn === true, type: "primary", ghost: true, onClick: function onClick() { var _editorRef$current; var currentValue = (_editorRef$current = editorRef.current) === null || _editorRef$current === void 0 ? void 0 : _editorRef$current.getValue(); onChange === null || onChange === void 0 || onChange(formatSql(currentValue || '')); }, children: "\u7F8E\u5316" }); var Element = (Ace === null || Ace === void 0 ? void 0 : Ace['default']) || Ace; return /*#__PURE__*/jsxs("div", { className: classNames('ace-editor-mysql', props.className), style: props.style, children: [/*#__PURE__*/jsx(ErrorBoundaryWrapper, { onRenderReset: function onRenderReset() { onChange === null || onChange === void 0 || onChange(undefined); setRootNodekey(Date.now()); }, children: /*#__PURE__*/jsx(Element, _objectSpread2(_objectSpread2({ fontSize: 14, showPrintMargin: true, showGutter: true, highlightActiveLine: true, placeholder: "\u8F93\u5165SQL", height: "250px", width: "auto" }, otherProps), {}, { setOptions: _objectSpread2({ useWorker: false, enableBasicAutocompletion: false, enableLiveAutocompletion: true, enableSnippets: false, showLineNumbers: true, tabSize: 2 }, otherProps.setOptions), mode: "mysql", onLoad: onLoadHandle, onChange: handleChange, value: value })) }), /*#__PURE__*/jsx("div", { className: "ace-editor-mysql-footer", style: _objectSpread2({ marginTop: 10 }, props.footerStyle), children: props.footerExtraRender ? props.footerExtraRender(footer) : footer })] }, rootNodekey); }; export { AceEditorMysql as A }; //# sourceMappingURL=editor-BeZLWo-d.js.map